Пример #1
0
        static string TestCublasHandle()
        {
            CublasOp o = CublasClr.CublasOp.N;

            string testName  = "TestCublasHandle";
            var    cuby      = new CublasClr.Cublas();
            IntPtr devHandle = new IntPtr();
            var    aa        = new CudaArray();

            try
            {
                var res = aa.ResetDevice();
                res = res + cuby.MakeCublasHandle(ref devHandle);
                res = res + cuby.DestroyCublasHandle(devHandle);
                if (res != String.Empty)
                {
                    return(testName + " fail: " + res);
                }
                return(testName + " pass");
            }
            catch
            {
                return(testName + " fail");
            }
            finally
            {
                //aa.ReleaseDevicePtr(devData);
                aa.ResetDevice();
            }
        }
Пример #2
0
        public static string Init(int[] inputs, uint span)
        {
            _span = span;
            _area = _span * _span;

            d_rands        = new IntPtr();
            d_gridA        = new IntPtr();
            d_gridB        = new IntPtr();
            d_energy       = new IntPtr();
            d_energyBlocks = new IntPtr();

            _cudaArray  = new CudaArray();
            _gridProcs  = new GridProcs();
            _randoProcs = new RandoProcs();

            var strRet = _cudaArray.ResetDevice();

            strRet = strRet + _randoProcs.MakeGenerator64(SEED);
            strRet = strRet + _cudaArray.MallocIntsOnDevice(ref d_gridA, _area);
            strRet = strRet + _cudaArray.MallocIntsOnDevice(ref d_energy, _area);
            strRet = strRet + _cudaArray.MallocIntsOnDevice(ref d_energyBlocks, _area / 1024);
            strRet = strRet + _cudaArray.MallocIntsOnDevice(ref d_gridB, _area);
            strRet = strRet + _cudaArray.CopyIntsToDevice(inputs, d_gridA, _area);
            strRet = strRet + _cudaArray.CopyIntsToDevice(inputs, d_gridB, _area);
            strRet = strRet + _cudaArray.MallocFloatsOnDevice(ref d_rands, _area);
            strRet = strRet + _cudaArray.MallocFloatsOnDevice(ref d_betas, 10);

            return(strRet);
        }
Пример #3
0
        public static string Init(int[] inputs, uint span, uint blockSize)
        {
            _span       = span;
            _block_size = blockSize;

            _area            = _span * _span;
            _blocks_per_span = span / blockSize;
            _blockCount      = _blocks_per_span * _blocks_per_span;

            d_indexRands = new IntPtr();
            d_grid       = new IntPtr();

            _cudaArray  = new CudaArray();
            _gridProcs  = new GridProcs();
            _randoProcs = new RandoProcs();

            var strRet = _cudaArray.ResetDevice();

            strRet = strRet + _randoProcs.MakeGenerator32(SEED);
            strRet = strRet + _cudaArray.MallocIntsOnDevice(ref d_grid, _area);
            strRet = strRet + _cudaArray.MallocIntsOnDevice(ref d_energy, _area);
            strRet = strRet + _cudaArray.MallocIntsOnDevice(ref d_energyBlocks, _area / 1024);
            strRet = strRet + _cudaArray.CopyIntsToDevice(inputs, d_grid, _area);
            strRet = strRet + _cudaArray.MallocIntsOnDevice(ref d_indexRands, _blockCount);
            strRet = strRet + _cudaArray.MallocFloatsOnDevice(ref d_tempRands, _blockCount);
            strRet = strRet + _gridProcs.Runk_Energy4(d_energy, d_grid, _span);
            strRet = strRet + _cudaArray.MallocFloatsOnDevice(ref d_betas, 5);

            return(strRet);
        }
Пример #4
0
        static string TestcublasSgemm2()
        {
            string    testName = "TestcublasSgemm2";
            uint      aw       = 2;
            uint      bh       = aw;
            uint      ah       = 3;
            uint      bw       = 3;
            uint      ch       = ah;
            uint      cw       = bw;
            GpuMatrix gpuA;
            GpuMatrix gpuB;
            GpuMatrix gpuC;

            var dataA = MatrixUtils.AA();
            var dataB = MatrixUtils.BB();
            var cRes  = new float[ch * cw];

            var cuby = new CublasClr.Cublas();
            var aa   = new CudaArray();

            var res = aa.ResetDevice();

            res = res + GpuMatrixOps.SetupGpuMatrix(
                out gpuA,
                new Matrix <float>(_rows: ch, _cols: cw,
                                   host_data: ImmutableArray.Create(dataA),
                                   matrixFormat: MatrixFormat.Column_Major));

            res = res + GpuMatrixOps.SetupGpuMatrix(
                out gpuB,
                new Matrix <float>(_rows: bh, _cols: bw,
                                   host_data: ImmutableArray.Create(dataB),
                                   matrixFormat: MatrixFormat.Column_Major));

            res = res + GpuMatrixOps.SetupGpuMatrix(
                out gpuC,
                new Matrix <float>(_rows: ch, _cols: cw,
                                   host_data: ImmutableArray.Create(cRes),
                                   matrixFormat: MatrixFormat.Column_Major));


            IntPtr cublasHandle = new IntPtr();

            res = res + cuby.MakeCublasHandle(ref cublasHandle);

            GpuMatrix gpuProd;

            res = res + GpuMatrixOps.Multiply(
                gmOut: out gpuProd,
                cublasHandle: cublasHandle,
                gmA: gpuA, gmB: gpuB, gmC: gpuC);

            GpuMatrix gpuSynched;

            res = res + GpuMatrixOps.CopyToHost(out gpuSynched, gpuProd);

            // GpuMatrixUtils.MatrixMult(C: cRes, A: dataA, B: dataB, wA: aw, hA: ah, wB: bw);

            return(string.Empty);
        }
Пример #5
0
        public static string Init(int[] inputs, uint span)
        {
            // init libs
            _cudaArray  = new CudaArray();
            _gridProcs  = new GridProcs();
            _randoProcs = new RandoProcs();

            // Set grid sizes
            _span = span;
            _area = _span * _span;

            // Set block and thread sizes
            _blockSize = (_span < MAXTHREADS) ? _span : MAXTHREADS;
            _gridSize  = _area / _blockSize;

            // Set memory sizes
            _mem_N    = sizeof(int) * (_area);
            _mem_rand = sizeof(double) * (3 * _area);
            _mem_1    = sizeof(int) * (1);
            _mem_measured_quantity = sizeof(int) * (_gridSize);
            _mem_measured_magnet   = sizeof(int) * (_gridSize);

            // Allocate device arrays
            d_rands = new IntPtr();
            d_grid  = new IntPtr();

            var strRet = _cudaArray.ResetDevice();

            strRet = strRet + _randoProcs.MakeGenerator64(SEED);
            strRet = strRet + _cudaArray.MallocIntsOnDevice(ref d_grid, _area);
            strRet = strRet + _cudaArray.CopyIntsToDevice(inputs, d_grid, _area);
            strRet = strRet + _cudaArray.MallocFloatsOnDevice(ref d_rands, _area);

            return(strRet);
        }
Пример #6
0
        static string TestCopyIntsDeviceToDevice()
        {
            string testName = "TestCopyIntsDeviceToDevice";
            uint   arrayLen = 1000;
            var    alist    = Enumerable.Range(4, (int)arrayLen).ToArray();
            var    aa       = new CudaArray();
            IntPtr devDataA = new System.IntPtr();
            IntPtr devDataB = new System.IntPtr();
            var    retlist  = new int[(int)arrayLen];

            try
            {
                var res = aa.ResetDevice();
                res = res + aa.MallocIntsOnDevice(ref devDataA, arrayLen);
                res = res + aa.MallocIntsOnDevice(ref devDataB, arrayLen);
                res = res + aa.CopyIntsToDevice(alist, devDataA, arrayLen);
                res = res + aa.CopyIntsDeviceToDevice(devDataB, devDataA, arrayLen);
                res = res + aa.CopyIntsFromDevice(retlist, devDataB, arrayLen);
                res = res + aa.ReleaseDevicePtr(devDataA);
                res = res + aa.ReleaseDevicePtr(devDataB);

                if (!alist.SequenceEqual(retlist))
                {
                    return(testName + " fail: sequences do not match");
                }

                if (res != String.Empty)
                {
                    return(testName + " fail: " + res);
                }
                return(testName + " pass");
            }
            catch (Exception ex)
            {
                return(testName + " exception " + ex.Message);
            }
            finally
            {
                aa.ReleaseDevicePtr(devDataA);
                aa.ReleaseDevicePtr(devDataB);
                aa.ResetDevice();
            }
        }
Пример #7
0
        static string TestMakeNormalRands()
        {
            string testName = "TestMakeNormalRands";
            var    rdo      = new RandoClr.RandoProcs();
            var    aa       = new CudaArray();
            uint   arrayLen = 1000;
            int    seed     = 1234;
            IntPtr devRando = new IntPtr();
            IntPtr devData  = new IntPtr();
            var    retlist  = new float[(int)arrayLen];

            try
            {
                var res = aa.ResetDevice();

                res = res + rdo.MakeGenerator64(ref devRando, seed);
                res = res + aa.MallocFloatsOnDevice(ref devData, arrayLen);
                res = res + rdo.MakeNormalRands(devData, devRando, arrayLen, 0.0f, 1.0f);
                res = res + aa.CopyFloatsFromDevice(retlist, devData, arrayLen);

                res = res + aa.ReleaseDevicePtr(devData);
                res = res + rdo.DestroyGenerator(devRando);

                if (res != String.Empty)
                {
                    return(testName + " fail: " + res);
                }
                return(testName + " pass");
            }
            catch
            {
                return(testName + " fail");
            }
            finally
            {
                //rdo.DestroyGenerator(devRando);
                aa.ReleaseDevicePtr(devData);
                aa.ResetDevice();
            }
        }
Пример #8
0
        public static string Init(float[] temp_inputs, int[] flip_inputs, uint span, uint blockSize, int seed)
        {
            _span       = span;
            _block_size = blockSize;

            _area            = _span * _span;
            _blocks_per_span = span / blockSize;
            _blockCount      = _blocks_per_span * _blocks_per_span;

            d_flipData = new IntPtr();
            d_tempData = new IntPtr();

            d_flipRands  = new IntPtr();
            d_indexRands = new IntPtr();
            d_threshes   = new IntPtr();

            _cudaArray  = new CudaArray();
            _gridProcs  = new GridProcs();
            _randoProcs = new RandoProcs();

            var strRet = _cudaArray.ResetDevice();

            strRet = strRet + _randoProcs.MakeGenerator32(seed);
            strRet = strRet + _cudaArray.MallocFloatsOnDevice(ref d_tempData, _area);
            strRet = strRet + _cudaArray.MallocFloatsOnDevice(ref d_heatBlocks, _area / 1024);
            strRet = strRet + _cudaArray.MallocIntsOnDevice(ref d_flipData, _area);

            strRet = strRet + _cudaArray.MallocIntsOnDevice(ref d_indexRands, _blockCount);
            strRet = strRet + _cudaArray.MallocIntsOnDevice(ref d_flipRands, _blockCount);
            strRet = strRet + _cudaArray.MallocFloatsOnDevice(ref d_threshes, _allTempSteps);

            strRet = strRet + _cudaArray.CopyIntsToDevice(flip_inputs, d_flipData, _area);
            strRet = strRet + _cudaArray.CopyFloatsToDevice(temp_inputs, d_tempData, _area);


            var res9 = new int[_area];

            strRet = strRet + _cudaArray.CopyIntsFromDevice(res9, d_flipData, _area);

            return(strRet);
        }
Пример #9
0
        public static string Init(float[] inputs, uint span)
        {
            _span = span;
            _area = _span * _span;

            d_gridA = new IntPtr();
            d_gridB = new IntPtr();

            _cudaArray  = new CudaArray();
            _gridProcs  = new GridProcs();
            _randoProcs = new RandoProcs();

            var strRet = _cudaArray.ResetDevice();

            strRet = strRet + _cudaArray.MallocFloatsOnDevice(ref d_gridA, _area);
            strRet = strRet + _cudaArray.MallocFloatsOnDevice(ref d_gridB, _area);
            strRet = strRet + _cudaArray.CopyFloatsToDevice(inputs, d_gridA, _area);
            strRet = strRet + _cudaArray.CopyFloatsToDevice(inputs, d_gridB, _area);

            return(strRet);
        }
Пример #10
0
        static string TestcublasSgemm1()
        {
            string testName = "TestcublasSgemm";
            uint   aw       = 5;
            uint   bh       = aw;
            uint   ah       = 5;
            uint   bw       = 5;
            uint   ch       = ah;
            uint   cw       = bw;

            var cuby = new CublasClr.Cublas();
            var aa   = new CudaArray();
            var res  = aa.ResetDevice();

            var       dataA = MatrixUtils.MakeIdentity(rows: ah, cols: aw);
            GpuMatrix gpuA;

            res = res + GpuMatrixOps.SetupGpuMatrix(
                out gpuA,
                new Matrix <float>(_rows: ah, _cols: aw,
                                   host_data: ImmutableArray.Create(dataA),
                                   matrixFormat: MatrixFormat.Column_Major));


            var       dataB = MatrixUtils.MakeIdentiPoke(rows: bh, cols: bw);
            GpuMatrix gpuB;

            res = res + GpuMatrixOps.SetupGpuMatrix(
                out gpuB,
                new Matrix <float>(_rows: bh, _cols: bw,
                                   host_data: ImmutableArray.Create(dataB),
                                   matrixFormat: MatrixFormat.Column_Major));

            var       dataC = MatrixUtils.MakeZeroes(rows: bh, cols: bw);
            GpuMatrix gpuC;

            res = res + GpuMatrixOps.SetupGpuMatrix(
                out gpuC,
                new Matrix <float>(_rows: ch, _cols: cw,
                                   host_data: ImmutableArray.Create(dataC),
                                   matrixFormat: MatrixFormat.Column_Major));

            IntPtr cublasHandle = new IntPtr();

            res = res + cuby.MakeCublasHandle(ref cublasHandle);

            GpuMatrix gpuProd;

            res = res + GpuMatrixOps.Multiply(
                gmOut: out gpuProd,
                cublasHandle: cublasHandle,
                gmA: gpuA, gmB: gpuB, gmC: gpuC);

            GpuMatrix gpuSynched;

            res = res + GpuMatrixOps.CopyToHost(out gpuSynched, gpuProd);

            var cpuRes = new float[ah * bw];

            MatrixUtils.RowMajorMatrixMult(C: cpuRes, A: dataA, B: dataB, wA: aw, hA: ah, wB: bw);

            var cpuRes2 = new float[bh * aw];

            MatrixUtils.RowMajorMatrixMult(C: cpuRes2, A: dataB, B: dataA, wA: bw, hA: bh, wB: aw);

            return(res);
        }