Пример #1
0
 /// <summary>
 /// Constructs a blas using a maximum of <i>maxThreads<i> threads; each executing the given sequential algos.
 /// </summary>
 /// <param name="maxThreads"></param>
 /// <param name="seqBlas"></param>
 public SmpBlas(int maxThreads, IBlas seqBlas)
 {
     this.seqBlas    = seqBlas;
     this.maxThreads = maxThreads;
     this.smp        = new Smp(maxThreads);
     //Smp.smp = new Smp(maxThreads);
 }
Пример #2
0
        public DataFrame()
        {
            _blas = new DotNetBlas();
            _rng  = new Random();

            //_indices = new int[_baseFrame.RowCount];
            //for (var i = 0; i < _indices.Length; i++)
            //{
            //    _indices[i] = i;
            //}
        }
Пример #3
0
        /// <summary>
        /// Sets the public global variable <i>SmpBlas.smpBlas</i> to a blas using a maximum of <i>maxThreads</i> threads, each executing the given sequential algorithm; <i>maxThreads</i> is normally the number of CPUs.
        /// Call this method at the very beginning of your programd
        /// Normally there is no need to call this method more than once.
        /// </summary>
        /// <param name="maxThreads">the maximum number of threads (= CPUs) to be used</param>
        /// <param name="seqBlas">the sequential blas algorithms to be used on concurrently processed matrix blocks.</param>
        public static void AllocateBlas(int maxThreads, IBlas seqBlas)
        {
            if (smpBlas is SmpBlas)
            { // no need to change anything?
                SmpBlas s = (SmpBlas)smpBlas;
                if (s.maxThreads == maxThreads && s.seqBlas == seqBlas)
                {
                    return;
                }
            }

            if (maxThreads <= 1)
            {
                smpBlas = seqBlas;
            }
            else
            {
                smpBlas = new SmpBlas(maxThreads, seqBlas);
            }
        }
Пример #4
0
 public StatsFunctions(IBlas blas)
 {
     _blas = blas;
 }
Пример #5
0
        /// <summary>
        /// Linear algebraic matrix power; <i>B = A<sup>k</sup> &lt;==> B = A*A*...*A</i>.
        /// <ul>
        /// <li><i>p &gt;= 1: B = A*A*...*A</i>.</li>
        /// <li><i>p == 0: B = identity matrix</i>.</li>
        /// <li><i>p &lt;  0: B = pow(inverse(A),-p)</i>.</li>
        /// </ul>
        /// Implementation: Based on logarithms of 2, memory usage minimized.
        /// </summary>
        /// <param name="A">the source matrix; must be square; stays unaffected by this operation.</param>
        /// <param name="p">the exponent, can be any number.</param>
        /// <returns><i>B</i>, a newly constructed result matrix; storage-independent of <i>A</i>.</returns>
        ///<exception cref="ArgumentException">if <i>!property().isSquare(A)</i>.</exception>
        public static DoubleMatrix2D Pow(DoubleMatrix2D A, int p)
        {
            // matrix multiplication based on log2 method: A*A*....*A is slow, ((A * A)^2)^2 * ..D is faster
            // allocates two auxiliary matrices as work space

            IBlas blas = SmpBlas.smpBlas; // for parallel matrix mult; if not initialized defaults to sequential blas

            Property.DEFAULT.CheckSquare(A);
            if (p < 0)
            {
                A = Inverse(A);
                p = -p;
            }
            if (p == 0)
            {
                return(DoubleFactory2D.Dense.Identity(A.Rows));
            }
            DoubleMatrix2D T = A.Like(); // temporary

            if (p == 1)
            {
                return(T.Assign(A));         // safes one auxiliary matrix allocation
            }
            if (p == 2)
            {
                blas.Dgemm(false, false, 1, A, A, 0, T); // mult(A,A); // safes one auxiliary matrix allocation
                return(T);
            }

            int k = Cern.Colt.Bitvector.QuickBitVector.MostSignificantBit(p); // index of highest bit in state "true"

            /*
             * this is the naive version:
             * DoubleMatrix2D B = A.Copy();
             * for (int i=0; i<p-1; i++) {
             *  B = mult(B,A);
             * }
             * return B;
             */

            // here comes the optimized version:
            //cern.colt.Timer timer = new cern.colt.Timer().start();

            int i = 0;

            while (i <= k && (p & (1 << i)) == 0)
            {                                             // while (bit i of p == false)
              // A = mult(A,A); would allocate a lot of temporary memory
                blas.Dgemm(false, false, 1, A, A, 0, T);  // A.zMult(A,T);
                DoubleMatrix2D swap = A; A = T; T = swap; // swap A with T
                i++;
            }

            DoubleMatrix2D B = A.Copy();

            i++;
            for (; i <= k; i++)
            {
                // A = mult(A,A); would allocate a lot of temporary memory
                blas.Dgemm(false, false, 1, A, A, 0, T);  // A.zMult(A,T);
                DoubleMatrix2D swap = A; A = T; T = swap; // swap A with T

                if ((p & (1 << i)) != 0)
                {                                            // if (bit i of p == true)
                  // B = mult(B,A); would allocate a lot of temporary memory
                    blas.Dgemm(false, false, 1, B, A, 0, T); // B.zMult(A,T);
                    swap = B; B = T; T = swap;               // swap B with T
                }
            }
            //timer.stop().Display();
            return(B);
        }
Пример #6
0
 public void SetBlas(IBlas blas)
 {
     _blas = blas;
 }