コード例 #1
0
ファイル: GPURadixSort.cs プロジェクト: Schw1ng/GPURadixSort
        public GPURadixSort(
            ComputeCommandQueue commandQue,
            ComputeContext context,
            ComputeDevice device
            )
        {
            gpuConstants   = new GPUConstants();
            gpuConstants.L = radix_BitsL;
            gpuConstants.numGroupsPerBlock = NumGroupsPerBlock;
            gpuConstants.R = R;
            gpuConstants.numThreadsPerGroup = numThreadsPerBlock / NumGroupsPerBlock;
            gpuConstants.numThreadsPerBlock = numThreadsPerBlock;
            gpuConstants.numBlocks          = numBlocks;
            gpuConstants.numRadices         = num_Radices;
            gpuConstants.numRadicesPerBlock = num_Radices / numBlocks;
            gpuConstants.bitMask            = BIT_MASK_START;
            counters.Initialize();
            ComputeErrorCode error;

            cxGPUContext   = context.Handle;
            cqCommandQueue = commandQue.Handle;
            _device        = device.Handle;
            //Create a command queue, where all of the commands for execution will be added

            /*cqCommandQueue = CL10.CreateCommandQueue(cxGPUContext, _device, (CommandQueueProperties)0, out  error);
             * CheckErr(error, "CL10.CreateCommandQueue");*/
            string programSource = System.IO.File.ReadAllText(programPath);

            IntPtr[] progSize = new IntPtr[] { (IntPtr)programSource.Length };
            string   flags    = "-cl-fast-relaxed-math";

            ComputeProgram prog = new ComputeProgram(context, programSource);

            prog.Build(new List <ComputeDevice>()
            {
                device
            }, flags, null, IntPtr.Zero);


            if (prog.GetBuildStatus(device) != ComputeProgramBuildStatus.Success)
            {
                Debug.WriteLine(prog.GetBuildLog(device));
                throw new ArgumentException("UNABLE to build programm");
            }
            //            ComputeProgram clProgramRadix = CL10.CreateProgramWithSource(cxGPUContext, 1, new[] { programSource },progSize,
            //                out error);

            CLProgramHandle clProgramRadix = prog.Handle;



            ckSetupAndCount = CL10.CreateKernel(clProgramRadix, "SetupAndCount", out error);
            CheckErr(error, "CL10.CreateKernel");
            ckSumIt = CL10.CreateKernel(clProgramRadix, "SumIt", out error);
            CheckErr(error, "CL10.CreateKernel");
            ckReorderingKeysOnly = CL10.CreateKernel(clProgramRadix, "ReorderingKeysOnly", out error);
            CheckErr(error, "CL10.CreateKernel");
            ckReorderingKeyValue = CL10.CreateKernel(clProgramRadix, "ReorderingKeyValue", out error);
            CheckErr(error, "CL10.CreateKernel");
        }
コード例 #2
0
        private void OnProgramBuilt(ComputeProgram program, ComputeDevice device)
        {
            var status = program.GetBuildStatus(device);

            if (status == ComputeProgramBuildStatus.Error)
            {
                var log = program.GetBuildLog(device);
                Console.WriteLine(log);
            }
        }
コード例 #3
0
        static void Main(string[] args)
        {
            //Test2();

            Test1();

            ComputePlatform plat = ComputePlatform.Platforms[0];

            Console.WriteLine("Plat:" + plat.Name);

            ComputeContext context = new ComputeContext(ComputeDeviceTypes.Gpu, new ComputeContextPropertyList(plat), null, IntPtr.Zero);

            ComputeCommandQueue queue = new ComputeCommandQueue(context, context.Devices[0], ComputeCommandQueueFlags.None);

            StreamReader rs = new StreamReader("Foom/CL/testProg.txt");

            string clSrc = rs.ReadToEnd();

            rs.Close();

            ComputeProgram prog = new ComputeProgram(context, clSrc);

            prog.Build(null, null, null, IntPtr.Zero);

            Console.WriteLine("BS:" + prog.GetBuildStatus(context.Devices[0]).ToString());
            Console.WriteLine("Info:" + prog.GetBuildLog(context.Devices[0]));

            ComputeKernel kern = prog.CreateKernel("vector_add");

            int[] data = new int[1024];

            for (int i = 0; i < 1024; i++)
            {
                data[i] = 100;
            }

            ComputeBuffer <int> b1 = new ComputeBuffer <int>(context, ComputeMemoryFlags.CopyHostPointer, data);

            ComputeBuffer <int> b2 = new ComputeBuffer <int>(context, ComputeMemoryFlags.WriteOnly, 1024);

//            queue.WriteToBuffer<int>(data, b1, true, null);



            kern.SetMemoryArgument(0, b1);
            kern.SetMemoryArgument(1, b2);

            long[] wo = new long[1];
            wo[0] = 0;

            long[] ws = new long[1];
            ws[0] = 1024;

            long[] tc = new long[1];
            tc[0] = 16;

            queue.Execute(kern, wo, ws, tc, null);
            int c = Environment.TickCount;

            queue.Finish();

            c = Environment.TickCount - c;



            queue.ReadFromBuffer <int>(b2, ref data, true, null);


            for (int i = 0; i < 10; i++)
            {
                Console.WriteLine("C:" + (int)data[i]);
            }

            Console.WriteLine("Done:" + c);


            while (true)
            {
            }
        }
コード例 #4
0
        private void CalculateConvolution(ComputeContext computeContext)
        {
            Stopwatch stopwatch = new Stopwatch();

            stopwatch.Start();

            float dx;
            bool  shiftXParse = float.TryParse(textBoxShiftX.Text, NumberStyles.Float, CultureInfo.InvariantCulture.NumberFormat, out dx);

            if (!shiftXParse)
            {
                throw new SyntaxErrorException(", needs to be .");
            }

            float dy;
            bool  shiftYParse = float.TryParse(textBoxShiftX.Text, NumberStyles.Float, CultureInfo.InvariantCulture.NumberFormat, out dy);

            if (!shiftYParse)
            {
                throw new SyntaxErrorException(", needs to be  .");
            }

            float dz;
            bool  shiftZParse = float.TryParse(textBoxShiftX.Text, NumberStyles.Float, CultureInfo.InvariantCulture.NumberFormat, out dz);

            if (!shiftZParse)
            {
                throw new SyntaxErrorException(", needs to be  .");
            }

            int pixelCount = _imageDimensionX * _imageDimensionY * _imageDimensionZ;

            Console.WriteLine("Computing...");
            Console.WriteLine("Reading kernel...");

            String kernelPath = Directory.GetParent(Directory.GetCurrentDirectory()).Parent.Parent.FullName;

            String kernelString;

            using (var sr = new StreamReader(kernelPath + "\\convolution.cl"))
                kernelString = sr.ReadToEnd();

            Console.WriteLine("Reading kernel... done");

            float[] selectedTransformation = Transformations.GetTransformation((TransformationType)comboBoxTransform.SelectedItem, 1.0f / float.Parse(textBoxPixelSize.Text), 1.0f / float.Parse(textBoxPixelSize.Text), 1.0f / float.Parse(textBoxPixelSize.Text), dx, dy, dz);

            //create openCL program
            ComputeProgram computeProgram = new ComputeProgram(computeContext, kernelString);

            computeProgram.Build(computeContext.Devices, null, null, IntPtr.Zero);

            ComputeProgramBuildStatus computeProgramBuildStatus = computeProgram.GetBuildStatus(_selectedComputeDevice);

            Console.WriteLine("computeProgramBuildStatus\n\t" + computeProgramBuildStatus);

            String buildLog = computeProgram.GetBuildLog(_selectedComputeDevice);

            Console.WriteLine("buildLog");
            if (buildLog.Equals("\n"))
            {
                Console.WriteLine("\tbuildLog is empty...");
            }
            else
            {
                Console.WriteLine("\t" + buildLog);
            }


            float[] fluorophores = CsvData.ReadFluorophores(_sourceFilename);

/////////////////////////////////////////////
// Create a Command Queue & Event List
/////////////////////////////////////////////
            ComputeCommandQueue computeCommandQueue = new ComputeCommandQueue(computeContext, _selectedComputeDevice, ComputeCommandQueueFlags.None);

////////////////////////////////////////////////////////////////
// Create Buffers Transform
////////////////////////////////////////////////////////////////
            ComputeBuffer <float> fluorophoresCoords = new ComputeBuffer <float>(computeContext, ComputeMemoryFlags.ReadWrite, fluorophores.LongLength);

            ComputeBuffer <float> transformationMatrix = new ComputeBuffer <float>(computeContext, ComputeMemoryFlags.ReadOnly, selectedTransformation.LongLength);

/////////////////////////////////////////////
// Create the transformFluorophoresKernel
///////////////////////////////////////////////////////////
            ComputeKernel transformFluorophoresKernel = computeProgram.CreateKernel("transform_fluorophores");

/////////////////////////////////////////////
// Set the transformFluorophoresKernel arguments
/////////////////////////////////////////////
            transformFluorophoresKernel.SetMemoryArgument(0, fluorophoresCoords);
            transformFluorophoresKernel.SetMemoryArgument(1, transformationMatrix);

/////////////////////////////////////////////
// Configure the work-item structure
/////////////////////////////////////////////
            long[] globalWorkOffsetTransformFluorophoresKernel = null;
            long[] globalWorkSizeTransformFluorophoresKernel   = new long[]   { fluorophores.Length / 4 };
            long[] localWorkSizeTransformFluorophoresKernel    = null;

////////////////////////////////////////////////////////
// Enqueue the transformFluorophoresKernel for execution
////////////////////////////////////////////////////////

            computeCommandQueue.WriteToBuffer(fluorophores, fluorophoresCoords, true, null);
            computeCommandQueue.WriteToBuffer(selectedTransformation, transformationMatrix, true, null);

            computeCommandQueue.Execute(transformFluorophoresKernel, globalWorkOffsetTransformFluorophoresKernel, globalWorkSizeTransformFluorophoresKernel, localWorkSizeTransformFluorophoresKernel, null);
//            computeCommandQueue.ExecuteTask(transformFluorophoresKernel, transformFluorophoresEvents);

            float[] transformedFluorophores = new float[fluorophores.Length];

            computeCommandQueue.ReadFromBuffer(fluorophoresCoords, ref transformedFluorophores, true, null);

            computeCommandQueue.Finish();

            //TODO remove, only for testing
//            for (int i = 0; i < transformedFluorophores.Length; i++)
//            {
//                Console.WriteLine(transformedFluorophores[i]);
//            }
            // /TODO remove, only for testing

            stopwatch.Stop();
            Console.WriteLine("Transform fluophores duration:\n\t" + stopwatch.Elapsed);
            stopwatch.Reset();
            stopwatch.Start();
            // fluorophoresCoords are now transformed (done in place)

////////////////////////////////////////////////////////////////
// Create Buffers Convolve Fluorophores
////////////////////////////////////////////////////////////////

            const int convolve_kernel_lwgs = 16;
            int       totalBuffer          = (int)Math.Ceiling(pixelCount / (float)convolve_kernel_lwgs) * convolve_kernel_lwgs;

            ComputeBuffer <float> resultImage = new ComputeBuffer <float>(computeContext, ComputeMemoryFlags.WriteOnly, totalBuffer);

/////////////////////////////////////////////
// Create the transformFluorophoresKernel
/////////////////////////////////////////////
            ComputeKernel convolveFluorophoresKernel = computeProgram.CreateKernel("convolve_fluorophores");

/////////////////////////////////////////////
// Set the convolveFluorophoresKernel arguments
/////////////////////////////////////////////

            convolveFluorophoresKernel.SetMemoryArgument(0, resultImage);
            convolveFluorophoresKernel.SetValueArgument(1, _imageDimensionX);
            convolveFluorophoresKernel.SetValueArgument(2, _imageDimensionY);
            convolveFluorophoresKernel.SetMemoryArgument(3, fluorophoresCoords);
            convolveFluorophoresKernel.SetLocalArgument(4, convolve_kernel_lwgs);
            convolveFluorophoresKernel.SetValueArgument(5, fluorophores.Length / 4);

/////////////////////////////////////////////
// Configure the work-item structure
/////////////////////////////////////////////
            long[] globalWorkOffsetTransformConvolveFluorophoresKernel = null;
            long[] globalWorkSizeTransformConvolveFluorophoresKernel   = new long[] { pixelCount };
            long[] localWorkSizeTransformConvolveFluorophoresKernel    = new long[] { convolve_kernel_lwgs };

////////////////////////////////////////////////////////
// Enqueue the convolveFluorophoresKernel for execution
////////////////////////////////////////////////////////

            computeCommandQueue.Execute(convolveFluorophoresKernel, globalWorkOffsetTransformConvolveFluorophoresKernel, globalWorkSizeTransformConvolveFluorophoresKernel, localWorkSizeTransformConvolveFluorophoresKernel, null);

            float[] resultImageData = new float[totalBuffer];
            computeCommandQueue.ReadFromBuffer(resultImage, ref resultImageData, true, null);

            computeCommandQueue.Finish();

            for (int i = 0; i < pixelCount; i++)
            {
                Console.WriteLine(resultImageData[i]);
            }

            Console.WriteLine("Writing data to file...");
//            CsvData.WriteToDisk("..\\..\\..\\output.csv", resultImageData);
            TiffData.WriteToDisk(resultImageData, _saveFilename, _imageDimensionX, _imageDimensionY);

            Bitmap bitmap = new Bitmap(_imageDimensionX, _imageDimensionY);

            float max = resultImageData.Max();

            float scale = 255 / (float)max;

//            for (int r = 0; r < _imageDimensionY; r++)
//            {
//                for (int c = 0; c < _imageDimensionX; c++)
//                {
//                    float value = resultImageData[c*(r + 1)];
//                    Color newColor = Color.FromArgb((int)(value * scale), (int)(value * scale), (int)(value * scale));
//                    bitmap.SetPixel(c,r, newColor);
//                }
//            }

            ushort[] ushortdata = new ushort[resultImageData.Length];

            for (int i = 0; i < resultImageData.Length; i++)
            {
                ushortdata[i] = (ushort)resultImageData[i];
            }

            uint[] convertGray16ToRgb = ConvertGray16ToRGB(ushortdata, 16);

            byte[] bytes = new byte[convertGray16ToRgb.Length * 4];
//
//            int[] resultImageData2 = new int[resultImageData.Length];
//
            for (int index = 0; index < convertGray16ToRgb.Length; index++)
            {
//                resultImageData2[index] = (int)(scale*resultImageData[index]);

                byte[] bytes1 = BitConverter.GetBytes(convertGray16ToRgb[index]);
                bytes[index]         = bytes1[0];
                bytes[4 * index + 1] = bytes1[1];
                bytes[4 * index + 2] = bytes1[2];
                bytes[4 * index + 3] = bytes1[3];
            }
//
//            for (int r = 0; r < _imageDimensionY; r++)
//            {
//                for (int c = 0; c < _imageDimensionX; c++)
//                {
//                    float value = resultImageData2[c*(r + 1)];
//                    Color newColor = Color.FromArgb((int)(value), (int)(value), (int)(value));
//                    bitmap.SetPixel(c,r, newColor);
//                }
//            }
//            bitmap.Save("c:\\temp.bmp");


            using (MemoryStream ms = new MemoryStream(bytes))
            {
                Image image = Bitmap.FromStream(ms);
                image.Save("c:\\temp.bmp");
            }

            Console.WriteLine("Writing data to file... done");

            stopwatch.Stop();
            Console.WriteLine("Convolve fluophores duration:\n\t" + stopwatch.Elapsed);
            Console.WriteLine("Computing... done");
        }
コード例 #5
0
        public static void CompileKernel()
        {
            Console.WriteLine("OpenCL: Compiling kernel code...");
            platform = ComputePlatform.Platforms[0]; // todo find amd gpus..
            IList <ComputeDevice> devices;

            devices = new List <ComputeDevice>();

            object[] availableDevices = new object[platform.Devices.Count];
            for (int i = 0; i < availableDevices.Length; i++)
            {
                availableDevices[i] = platform.Devices[i].Name;
            }

            properties = new ComputeContextPropertyList(platform);
            devices.Add(platform.Devices[0]);
            context = new ComputeContext(devices, properties, null, IntPtr.Zero);


            /* if binary file was saved load it */
            if (File.Exists("./KernelBinary.bin") && !Params.FORCE_KERNEL_RECOMPILE)
            {
                try
                {
                    byte[]        binaryIn = File.ReadAllBytes("KernelBinary.bin");
                    List <byte[]> binaries = new List <byte[]>();
                    binaries.Add(binaryIn);

                    program = new ComputeProgram(context, binaries, devices);

                    ComputeProgramBuildStatus error = program.GetBuildStatus(context.Devices[0]);
                    try
                    {
                        program.Build(null, null, null, IntPtr.Zero);
                    }
                    catch
                    {
                        Console.WriteLine(program.GetBuildLog(devices[0]));
                        throw;
                    }
                }
                catch
                {
                    Console.WriteLine("Could not read binary opencl kernel ");
                }
            }
            else
            {
                // Create and build the opencl program.
                StreamReader streamReader = new StreamReader("../../NeuralNetwork.cl");
                string       openClCode   = streamReader.ReadToEnd();
                streamReader.Close();
                program = new ComputeProgram(context, openClCode);

                ComputeProgramBuildStatus error = program.GetBuildStatus(context.Devices[0]);
                try
                {
                    program.Build(null, null, null, IntPtr.Zero);
                }
                catch
                {
                    Console.WriteLine(program.GetBuildLog(devices[0]));
                    throw;
                }

                try
                {
                    byte[]       binaryOut = program.Binaries[0];
                    BinaryWriter Writer    = null;

                    // Create a new stream to write to the file
                    Writer = new BinaryWriter(File.OpenWrite("./KernelBinary.bin"));

                    // Writer raw data
                    Writer.Write(binaryOut);
                    Writer.Flush();
                    Writer.Close();
                }
                catch
                {
                    Console.WriteLine("Could not write binary opencl kernel to file ");
                }
            }

            // Create the kernel function and set its arguments.
            kernel = program.CreateKernel("NN");

            // Create the command queue. This is used to control kernel execution and manage read/write/copy operations.
            commandQueue1 = new ComputeCommandQueue(context, context.Devices[0], ComputeCommandQueueFlags.None);
            commandQueue2 = new ComputeCommandQueue(context, context.Devices[0], ComputeCommandQueueFlags.None);
        }