Exemple #1
0
        public CudaMatrixDouble Add(CudaMatrixDouble matrix)
        {
            int rows = Math.Max(_Rows, matrix._Rows);
            int cols = Math.Max(_Cols, matrix._Cols);
            CudaMatrixDouble result = new CudaMatrixDouble(rows, cols);

            UpdateDeviceMemory();
            matrix.UpdateDeviceMemory();

            IntPtr handle = cuBLAS.Create_v2();

            cuBLAS.Dgeam(
                handle,
                cublasOperation.CUBLAS_OP_N,
                cublasOperation.CUBLAS_OP_N,
                _Rows, _Cols,
                1d,
                _Device, _Rows,
                1d,
                matrix._Device, _Rows,
                result._Device, _Rows
                );
            cuBLAS.Destroy_v2(handle);

            result.UpdateHostMemory();
            return(result);
        }
Exemple #2
0
        public CudaMatrixDouble Dot(CudaMatrixDouble matrix)
        {
            CudaMatrixDouble result = new CudaMatrixDouble(_Rows, matrix._Cols);

            UpdateDeviceMemory();
            matrix.UpdateDeviceMemory();

            IntPtr handle = cuBLAS.Create_v2();

            cuBLAS.Dgemm_v2(
                handle,
                cublasOperation.CUBLAS_OP_N,
                cublasOperation.CUBLAS_OP_N,
                _Rows, matrix._Cols, _Cols,
                1f,
                _Device, _Rows,
                matrix._Device, matrix._Rows,
                1f,
                result._Device, _Rows
                );
            cuBLAS.Destroy_v2(handle);

            result.UpdateHostMemory();
            return(result);
        }
Exemple #3
0
        public static CudaMatrixDouble FromByteArray(byte[] bytes)
        {
            if (bytes == null || bytes.Length < 12)
            {
                return(null);
            }

            int intSize  = sizeof(int);
            int intSize2 = intSize * 2;

            int[]    matrixSize = new int[2];
            double[] data       = new double[(bytes.Length - intSize2) / ItemSize];
            Buffer.BlockCopy(bytes, 0, matrixSize, 0, intSize2);
            Buffer.BlockCopy(bytes, intSize2, data, 0, bytes.Length - intSize2);

            int rows = matrixSize[0];
            int cols = matrixSize[1];
            CudaMatrixDouble matrix = new CudaMatrixDouble(rows, cols, false);

            matrix._Host        = data;
            matrix._DirtyDevice = true;
            matrix.UpdateDeviceMemory();
            return(matrix);
        }
Exemple #4
0
        public CudaMatrixDouble Mul(CudaMatrixDouble matrix)
        {
            int rows = Math.Max(_Rows, matrix._Rows);
            int cols = Math.Max(_Cols, matrix._Cols);
            CudaMatrixDouble result = new CudaMatrixDouble(_Rows, matrix._Cols);

            UpdateDeviceMemory();
            matrix.UpdateDeviceMemory();

            IntPtr handle = cuBLAS.Create_v2();

            cuBLAS.Ddgmm(
                handle,
                cublasSideMode.CUBLAS_SIDE_LEFT,
                _Rows, _Cols,
                _Device, _Rows,
                matrix._Device, _Rows,
                result._Device, _Rows
                );
            cuBLAS.Destroy_v2(handle);

            result.UpdateHostMemory();
            return(result);
        }