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; }
public CpuGpuArray(CpuGpuArray array, int rows, int cols) { _gpuModule = array._gpuModule; CPUArray = array.CPUArray; GPUArray = array.GPUArray; RowCount = rows; ColCount = cols; }
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 ); }
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); } }
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 ); }
//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); }
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; }
//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); }
public static string SerializeArrayValues(CpuGpuArray array) { var res = String.Join("\n", array.CPUArray.Select(x => x.ToString(CultureInfo.InvariantCulture))); return res; }
public void SetSparseGpuValues(CpuGpuArray target, CpuGpuArray indices, CpuGpuArray values) { SetSparseGpuValues(target.GPUArray, target.RowCount, target.ColCount, indices, values); }
//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 ); }
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 ); }
public static string SerializeArrayValues(CpuGpuArray array) { var res = String.Join("\n", array.CPUArray.Select(x => x.ToString(CultureInfo.InvariantCulture))); return(res); }
//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 ); }