Пример #1
0
 public CpuGpuArray AddArray(ArrayName arrayName, int dim1, int dim2)
 {
     var dim = dim1 * dim2;
     var res = new CpuGpuArray(this._gpuModule, dim1, dim2);
     _Arrays.Add(arrayName, res);
     return res;
 }
Пример #2
0
 public CpuGpuArray(CpuGpuArray array, int rows, int cols)
 {
     _gpuModule = array._gpuModule;
     CPUArray   = array.CPUArray;
     GPUArray   = array.GPUArray;
     RowCount   = rows;
     ColCount   = cols;
 }
Пример #3
0
 public CpuGpuArray(CpuGpuArray array, int rows, int cols)
 {
     _gpuModule = array._gpuModule;
     CPUArray = array.CPUArray;
     GPUArray = array.GPUArray;
     RowCount = rows;
     ColCount = cols;
 }
Пример #4
0
        public void FillArray(CpuGpuArray array, float value)
        {
            var kernelCount = array.Length;
            var dim         = ComputeBlocksTreads(kernelCount);

            Gpu.Launch(dim.Item1, dim.Item2).FillArrayGPU(
                kernelCount,
                array.GPUArray,
                value
                );
        }
Пример #5
0
        void InitArrays(int dataSize, int rowCount)
        {
            if (NonZeroCount > dataSize)
            {
                throw new ArgumentException();
            }
            DataSize   = dataSize;
            RowCount   = rowCount;
            CPUValues  = new float[DataSize];
            CPUIndices = new int[DataSize];

            if (_gpuModule != null)
            {
                GPUValues  = CpuGpuArray.AllocateGPUArray(_gpuModule, DataSize);
                GPUIndices = CpuGpuArrayInt.AllocateGPUArray(_gpuModule, DataSize);
            }
        }
Пример #6
0
        public void ElementwiseMultiplication(CpuGpuArray vector, CpuGpuArray matrix)
        {
            if (vector.Length != matrix.Height)
            {
                throw new Exception("Matrix.height <> vector.length");
            }

            var kernelCount = matrix.Length;
            var dim         = ComputeBlocksTreads(kernelCount);

            Gpu.Launch(dim.Item1, dim.Item2).ElementwiseMultiplicationGPU(
                kernelCount,
                vector.GPUArray,
                matrix.GPUArray,
                matrix.Width
                );
        }
Пример #7
0
        //http://peterwittek.com/2013/06/cublas-matrix-c-style/
        //row major to col major "trick"
        public void GemmRowMajor(CpuGpuArray A, CpuGpuArray B, CpuGpuArray C, float cMultiplier = 0f, bool transposeA = false, bool transposeB = false)
        {
            var blasA = B;
            var blasB = A;
            // M = rowcount A(T),C
            // N = colcount B(T),C
            // K = colcount A(T), rowcount B(T)
            // However, we flip so
            // M = colcount B(T),C
            // N = rowcoun A(T), C
            // K = cols A(T), rows B
            var m = blasA.ColCount;
            var n = blasB.RowCount;
            var k = blasB.ColCount;

            var lda = blasA.ColCount;
            var ldb = blasB.ColCount;
            var ldc = blasA.ColCount;

            var transb = cublasOperation.N;

            if (transposeA)
            {
                transb = cublasOperation.T;
                n      = blasB.ColCount;
                k      = blasB.RowCount;
            }

            var transa = cublasOperation.N;

            if (transposeB)
            {
                transa = cublasOperation.T;
                m      = blasA.RowCount;
                ldc    = blasA.RowCount;
            }

            Blas.GEMM(m, k, n, 1f, B.GPUArray, A.GPUArray, cMultiplier, C.GPUArray, lda: lda, ldb: ldb, ldc: ldc, transb: transb, transa: transa);
        }
Пример #8
0
        public void MultiplySparse(CpuGpuMatrixSparse A, CpuGpuArray B, CpuGpuArray C, bool transposeA)
        {
            FillArray(C, 0f);

            var kernelCount  = B.ColCount * A.NonZeroCount;
            var dim          = ComputeBlocksTreads(kernelCount);
            var colcountA    = A.ColCount;
            var transposeInt = transposeA ? 1 : 0;

            Gpu.Launch(dim.Item1, dim.Item2).MultiplySparseGPU2(
                kernelCount,
                A.GPUIndices,
                A.GPUValues,
                A.NonZeroCount,
                colcountA,
                B.GPUArray,
                B.ColCount,
                C.GPUArray,
                transposeInt
                );
            return;
        }
Пример #9
0
        //http://peterwittek.com/2013/06/cublas-matrix-c-style/
        //row major to col major "trick"
        public void GemmRowMajor(CpuGpuArray A, CpuGpuArray B, CpuGpuArray C,  float cMultiplier = 0f, bool transposeA = false, bool transposeB = false)
        {
            var blasA = B;
            var blasB = A;
            // M = rowcount A(T),C
            // N = colcount B(T),C
            // K = colcount A(T), rowcount B(T)
            // However, we flip so 
            // M = colcount B(T),C
            // N = rowcoun A(T), C
            // K = cols A(T), rows B
            var m = blasA.ColCount;
            var n = blasB.RowCount;
            var k = blasB.ColCount;

            var lda = blasA.ColCount;
            var ldb = blasB.ColCount;
            var ldc = blasA.ColCount;

            var transb = cublasOperation.N;
            if (transposeA)
            {
                transb = cublasOperation.T;
                n = blasB.ColCount;
                k = blasB.RowCount;
            }

            var transa = cublasOperation.N;
            if (transposeB)
            {
                transa = cublasOperation.T;
                m = blasA.RowCount;
                ldc = blasA.RowCount;
            }

            Blas.GEMM(m, k, n, 1f, B.GPUArray, A.GPUArray, cMultiplier, C.GPUArray, lda: lda, ldb: ldb, ldc: ldc, transb:transb, transa: transa);
        }
Пример #10
0
 public static string SerializeArrayValues(CpuGpuArray array)
 {
     var res = String.Join("\n", array.CPUArray.Select(x => x.ToString(CultureInfo.InvariantCulture)));
     return res;
 }
Пример #11
0
 public void SetSparseGpuValues(CpuGpuArray target, CpuGpuArray indices, CpuGpuArray values)
 {
     SetSparseGpuValues(target.GPUArray, target.RowCount, target.ColCount, indices, values);
 }
Пример #12
0
 public void FillArray(CpuGpuArray array, float value)
 {
     var kernelCount = array.Length;
     var dim = ComputeBlocksTreads(kernelCount);
     Gpu.Launch(dim.Item1, dim.Item2).FillArrayGPU(
         kernelCount,
         array.GPUArray,
         value
     );
 }
Пример #13
0
        public void MultiplySparse(CpuGpuMatrixSparse A, CpuGpuArray B, CpuGpuArray C, bool transposeA)
        {
            FillArray(C, 0f);

            var kernelCount = B.ColCount * A.NonZeroCount;
            var dim = ComputeBlocksTreads(kernelCount);
            var colcountA = A.ColCount;
            var transposeInt = transposeA ? 1 : 0;
            Gpu.Launch(dim.Item1, dim.Item2).MultiplySparseGPU2(
                kernelCount,
                A.GPUIndices,
                A.GPUValues,
                A.NonZeroCount,
                colcountA,
                B.GPUArray,
                B.ColCount,
                C.GPUArray,
                transposeInt
            );
            return;
        }
Пример #14
0
 //float[] indices, float[] values, int rowCount, int colCount
 public void SetSparseGpuValues(float[] gpuTarget, int targetRowCount, int targetColCount, CpuGpuArray indices, CpuGpuArray values)
 {
     if (indices.Length != values.Length) throw new ArgumentException();
     var kernelCount = indices.RowCount * indices.ColCount;
     var dim = ComputeBlocksTreads(kernelCount);
     Gpu.Launch(dim.Item1, dim.Item2).SetSparseGpuValuesGPU(
         kernelCount,
         gpuTarget,
         targetRowCount,
         targetColCount,
         indices.GPUArray,
         values.GPUArray,
         indices.RowCount,
         indices.ColCount
     );
 }
Пример #15
0
 public void SetSparseGpuValues(CpuGpuArray target, CpuGpuArray indices, CpuGpuArray values)
 {
     SetSparseGpuValues(target.GPUArray, target.RowCount, target.ColCount, indices, values);
 }
Пример #16
0
        public void ElementwiseMultiplication(CpuGpuArray vector, CpuGpuArray matrix)
        {
            if (vector.Length != matrix.Height) throw new Exception("Matrix.height <> vector.length");

            var kernelCount = matrix.Length;
            var dim = ComputeBlocksTreads(kernelCount);
            Gpu.Launch(dim.Item1, dim.Item2).ElementwiseMultiplicationGPU(
               kernelCount,
               vector.GPUArray,
               matrix.GPUArray,
               matrix.Width
           );
        }
Пример #17
0
        public static string SerializeArrayValues(CpuGpuArray array)
        {
            var res = String.Join("\n", array.CPUArray.Select(x => x.ToString(CultureInfo.InvariantCulture)));

            return(res);
        }
Пример #18
0
        //float[] indices, float[] values, int rowCount, int colCount
        public void SetSparseGpuValues(float[] gpuTarget, int targetRowCount, int targetColCount, CpuGpuArray indices, CpuGpuArray values)
        {
            if (indices.Length != values.Length)
            {
                throw new ArgumentException();
            }
            var kernelCount = indices.RowCount * indices.ColCount;
            var dim         = ComputeBlocksTreads(kernelCount);

            Gpu.Launch(dim.Item1, dim.Item2).SetSparseGpuValuesGPU(
                kernelCount,
                gpuTarget,
                targetRowCount,
                targetColCount,
                indices.GPUArray,
                values.GPUArray,
                indices.RowCount,
                indices.ColCount
                );
        }