Esempio n. 1
0
        public override float GetElementAsFloat(long index)
        {
            CUdeviceptr ptr = DevicePtrAtElement(index);

            if (ElementType == DType.Float32)
            {
                float[] result = new float[1]; context.CopyToHost(result, ptr); return(result[0]);
            }
            else if (ElementType == DType.Float64)
            {
                double[] result = new double[1]; context.CopyToHost(result, ptr); return((float)result[0]);
            }
            else if (ElementType == DType.Int32)
            {
                int[] result = new int[1]; context.CopyToHost(result, ptr); return(result[0]);
            }
            else if (ElementType == DType.UInt8)
            {
                byte[] result = new byte[1]; context.CopyToHost(result, ptr); return(result[0]);
            }
            else
            {
                throw new NotSupportedException("Element type " + ElementType + " not supported");
            }
        }
Esempio n. 2
0
        static double[] SumMatrixManagedCuda(double[][,] matrix)
        {
            int Z = matrix.Length;
            int Y = matrix[0].GetLength(0);
            int X = matrix[0].GetLength(1);

            var result = new double[Y * X];
            var lm     = ToLinearArray(matrix);
            int N      = lm.Length;

            matrixSumCude.SetComputeSize((uint)X, (uint)Y);
            //matrixSumCude.BlockDimensions = 128;
            //matrixSumCude.GridDimensions = (N + 127) / 128;

            var da = cntxt.AllocateMemory(N * sizeof(double));
            var db = cntxt.AllocateMemory(result.Length * sizeof(double));

            cntxt.CopyToDevice(da, lm);
            cntxt.CopyToDevice(db, result);

            //CudaDeviceVariable<int> dA = a;
            //CudaDeviceVariable<int> dB = b;
            //CudaDeviceVariable<int> dC = new CudaDeviceVariable<int>(N);

            // Invoke kernel
            //kernel.Run(dA.DevicePointer, dC.DevicePointer, dimX, dimY, dimZ);
            matrixSumCude.Run(db, da, X, Y, Z);

            cntxt.CopyToHost <double>(result, db);

            return(result);
        }
Esempio n. 3
0
        unsafe int Sum(CUdeviceptr array_ptr, int size)
        {
            if (size == 0)
            {
                return(0);
            }

            if (reductions_tmp == null || reductions_tmp.Length < size)
            {
                reductions_tmp = new int[size];
            }
            int nresult_alt = 0;

            fixed(int *ptr = reductions_tmp)
            {
                IntPtr intptr = (IntPtr)ptr;

                ctx.CopyToHost(intptr, array_ptr, (uint)(size * sizeof(int)));
                for (int i = 0; i < size; i++)
                {
                    nresult_alt += ptr[i];
                }
                return(nresult_alt);
            }
        }
Esempio n. 4
0
        public override int[] GetElementsAsInt(long index, int length)
        {
            CUdeviceptr ptr = DevicePtrAtElement(index);

            if (ElementType == DType.Int32)
            {
                int[] result = new int[length]; context.CopyToHost(result, ptr); return(result);
            }
            else
            {
                throw new NotSupportedException("Element type " + ElementType + " not supported");
            }
        }
Esempio n. 5
0
        public uint[] Run()
        {
            var ptx = @"C:\Src\_Tree\SmallPrograms\Buddhabrot\Buddhabrot.Cuda70\x64\Release\Buddhabrot.ptx";

            var context = new CudaContext();
            var module = new CudaModuleHelper(context, ptx);

            var init = module.GetKernel("Init");
            var setSettings = module.GetKernel("SetSettings");
            var runBuddha = module.GetKernel("RunBuddha");

            var nBlocks = 4196;
            var nThreads = 256;

            var dSettings = context.AllocateMemoryFor(settings);
            context.CopyToDevice(dSettings, settings);

            var array = new uint[settings.Width * settings.Height];
            var dState = context.AllocateMemory(nThreads * nBlocks * SizeOfCurandState);
            var dArray = context.AllocateMemoryFor(array);
            context.CopyToDevice(dArray, array);

            init.Launch(nBlocks, nThreads, dState);
            setSettings.Launch(1, 1, dSettings);

            Console.WriteLine("Starting...");
            var sw = Stopwatch.StartNew();
            long i = 0;

            while (!IsStopping)
            {
                runBuddha.Launch(nBlocks, nThreads, dArray, dState);

                double count = (++i * nBlocks * nThreads);
                if (i % 5 == 0)
                {
                    Console.WriteLine("Generated {0:0.0} Million samples in {1:0.000} sec", count / 1000000.0, sw.ElapsedMilliseconds / 1000.0);
                }

                if (maxSamples.HasValue && count >= maxSamples)
                    break;
            }

            context.CopyToHost(array, dArray);
            return array;
        }
Esempio n. 6
0
 public void CopyToHost(float[] target)
 {
     _context.CopyToHost <float>(target, _ptr.DevicePointer);
 }
Esempio n. 7
0
 public void CopyToHost(float[] target)
 {
     _context.CopyToHost <float>(target, DeviceVariable.DevicePointer);
 }