예제 #1
0
        public static ComputeProgram CreateProgram(string source)
        {
            //string realType = Real.Size == sizeof(double) ? "double" : "float";
            string realType = "float";

            //For setting precision of floating point
            source = REAL_HEADER_STRING + source;

            //Add at double precision
            if (realType == "double")
            {
                source = USE_DOUBLE_HEADER_STRING + source;
            }

            ComputeProgram program = new ComputeProgram(Context, source);

            try
            {
                program.Build(Devices, $"-D REAL={realType} -Werror", null, IntPtr.Zero);
            }
            catch
            {
                MessageBox.Show(program.GetBuildLog(Devices[DeviceIndex]));
            }

            return(program);
        }
예제 #2
0
파일: Gpgpu.cs 프로젝트: emalron/simserver
        public string vectorSum()
        {
            string vecSum = @"
                __kernel void vectorSum(__global float *v1, __global float *v2, __global float *v3) {
                    int i = get_global_id(0);
                    v3[i] = v1[i] + v2[i];
                }
            ";
            int    size   = 100000;

            float[] v1_ = new float[size];
            float[] v2_ = new float[size];
            float[] v3_ = new float[size];
            for (var i = 0; i < size; i++)
            {
                v1_[i] = (float)i;
                v2_[i] = (float).5f;
            }
            var platform_ = ComputePlatform.Platforms[0];
            ComputeContextPropertyList properties = new ComputeContextPropertyList(platform_);
            ComputeContext             ctx        = new ComputeContext(ComputeDeviceTypes.Gpu, properties, null, IntPtr.Zero);
            ComputeCommandQueue        commands   = new ComputeCommandQueue(ctx, ctx.Devices[0], ComputeCommandQueueFlags.None);
            ComputeProgram             program    = new ComputeProgram(ctx, vecSum);

            try
            {
                program.Build(null, null, null, IntPtr.Zero);
                Console.WriteLine("program build completed");
            }
            catch
            {
                string log = program.GetBuildLog(ctx.Devices[0]);
            }
            ComputeBuffer <float> v1, v2, v3;

            v1 = new ComputeBuffer <float>(ctx, ComputeMemoryFlags.ReadOnly | ComputeMemoryFlags.CopyHostPointer, v1_);
            v2 = new ComputeBuffer <float>(ctx, ComputeMemoryFlags.ReadOnly | ComputeMemoryFlags.CopyHostPointer, v2_);
            v3 = new ComputeBuffer <float>(ctx, ComputeMemoryFlags.WriteOnly | ComputeMemoryFlags.CopyHostPointer, v3_);
            long[] worker = { size };
            commands.WriteToBuffer(v1_, v1, false, null);
            commands.WriteToBuffer(v2_, v2, false, null);
            ComputeKernel sumKernal = program.CreateKernel("vectorSum");

            Console.WriteLine("kernal created");
            sumKernal.SetMemoryArgument(0, v1);
            sumKernal.SetMemoryArgument(1, v2);
            sumKernal.SetMemoryArgument(2, v3);
            commands.Execute(sumKernal, null, worker, null, null);
            Console.WriteLine("Executed");
            commands.ReadFromBuffer <float>(v3, ref v3_, false, null);
            StringBuilder sb = new StringBuilder();

            for (int i = 0; i < size; i++)
            {
                sb.AppendFormat("{0} + {1} = {2}<br>", v1_[i].ToString(), v2_[i].ToString(), v3_[i].ToString());
            }
            var sum_expression_result = sb.ToString();

            return(sum_expression_result);
        }
예제 #3
0
        private void Initialize()
        {
            // get the intel integrated GPU
            _integratedIntelGPUPlatform = ComputePlatform.Platforms.Where(n => n.Name.Contains("Intel")).First();

            // create the compute context.
            _context = new ComputeContext(
                ComputeDeviceTypes.Gpu,                                      // use the gpu
                new ComputeContextPropertyList(_integratedIntelGPUPlatform), // use the intel openCL platform
                null,
                IntPtr.Zero);

            // the command queue is the, well, queue of commands sent to the "device" (GPU)
            _commandQueue = new ComputeCommandQueue(
                _context,                       // the compute context
                _context.Devices[0],            // first device matching the context specifications
                ComputeCommandQueueFlags.None); // no special flags

            string kernelSource = null;

            using (StreamReader sr = new StreamReader("kernel.cl"))
            {
                kernelSource = sr.ReadToEnd();
            }

            // create the "program"
            _program = new ComputeProgram(_context, new string[] { kernelSource });

            // compile.
            _program.Build(null, null, null, IntPtr.Zero);
            _kernel = _program.CreateKernel("ComputeMatrix");
        }
예제 #4
0
	// initialiseer OpenCL
	static void InitCL()
	{
		// kies platform 0 (op sommige machines moet dit 1 of 2 zijn)
		var platform = ComputePlatform.Platforms[CLPlatform];
		Console.Write( "initializing OpenCL... " + platform.Name + " (" + platform.Profile + ").\n" );
		Console.Write( platform.Devices.First().Name + " (" + platform.Devices.First().Type + ")\n");
		Console.Write( (platform.Devices.First().GlobalMemorySize / 1024 / 1024) );
		Console.WriteLine( " MiB global memory / " + (platform.Devices.First().LocalMemorySize / 1024) + " KiB local memory");
		// maak een compute context
		context = new ComputeContext( ComputeDeviceTypes.Gpu, new ComputeContextPropertyList( platform ), null, IntPtr.Zero );
		// laad opencl programma
		var streamReader = new StreamReader( "../../program.cl" );
		string clSource = streamReader.ReadToEnd();
		streamReader.Close();
		// compileer opencl source code
		program = new ComputeProgram( context, clSource );
		try
		{
			program.Build( null, null, null, IntPtr.Zero );
		}
		catch
		{
			// fout in OpenCL code; check console window voor details.
			Console.Write( "error in kernel code:\n" );
			Console.Write( program.GetBuildLog( context.Devices[0] ) + "\n" );
		}
		// maak een commandorij
		queue = new ComputeCommandQueue( context, context.Devices[0], 0 );
		// lokaliseer de gewenste kernel in het programma
		kernel = program.CreateKernel( "device_function" );
		// alloceer data in RAM
		
	}
예제 #5
0
 public bool CreateProgram(string name, string source)
 {
     try
     {
         Console.WriteLine("\"{0}\" ProgramKernel Build.", name);
         Console.WriteLine(source);
         ComputeProgram program;
         try
         {
             program = new ComputeProgram(Context, source);
             program.Build(Platform.Devices, null, null, IntPtr.Zero);
         }
         catch (Exception ex)
         {
             if (ex is BuildProgramFailureComputeException)
             {
             }
             throw ex;
         }
         Programs.Add(new ProgramKernel(name, program));
         Console.WriteLine("\"{0}\" ProgramKernel Created.", name);
         return(true);
     }
     catch (Exception exception)
     {
         throw exception;
     }
 }
예제 #6
0
        public override void Init(WaveFormat AFormat)
        {
            base.Init(AFormat);

            try
            {
                FDevice  = App.Settings.Preferences.OpenCL.GetComputeDevice();
                FContext = new ComputeContext(new ComputeDevice[] { FDevice }, new ComputeContextPropertyList(FDevice.Platform), null, IntPtr.Zero);

                program = new ComputeProgram(FContext, FProgramSource);

                program.Build(new[] { FDevice }, null, null, IntPtr.Zero);

                kernel   = program.CreateKernel("Wave");
                commands = new ComputeCommandQueue(FContext, FContext.Devices[0], ComputeCommandQueueFlags.None);
            }
            catch (BuildProgramFailureComputeException bex)
            {
                Debug.WriteLine(bex.Message);
                Debug.WriteLine(program.GetBuildLog(FDevice));
                throw;
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
                throw;
            }
        }
예제 #7
0
        public OpenCLProgram(string sourceFile)
        {
            // pick first platform
            SelectBestDevice();
            // load opencl source
            string clSource = "";

            try
            {
                var streamReader = new StreamReader(sourceFile);
                clSource = streamReader.ReadToEnd();
                streamReader.Close();
            }
            catch
            {
                FatalError("File not found:\n" + sourceFile);
            }
            // create program with opencl source
            program = new ComputeProgram(context, clSource);
            // compile opencl source
            try
            {
                program.Build(null, null, null, IntPtr.Zero);
            }
            catch
            {
                FatalError("Error in kernel code:\n" + program.GetBuildLog(context.Devices[0]));
            }
            // create a command queue with first gpu found
            queue = new ComputeCommandQueue(context, context.Devices[0], 0);
        }
        private void BuildEthashProgram()
        {
            ComputeDevice computeDevice = OpenCLDevice.GetComputeDevice();

            try { mProgramArrayMutex.WaitOne(5000); } catch (Exception) { }

            if (mEthashProgramArray.ContainsKey(new long[] { DeviceIndex, mEthashLocalWorkSizeArray[0] }))
            {
                mEthashProgram      = mEthashProgramArray[new long[] { DeviceIndex, mEthashLocalWorkSizeArray[0] }];
                mEthashDAGKernel    = mEthashDAGKernelArray[new long[] { DeviceIndex, mEthashLocalWorkSizeArray[0] }];
                mEthashSearchKernel = mEthashSearchKernelArray[new long[] { DeviceIndex, mEthashLocalWorkSizeArray[0] }];
            }
            else
            {
                try
                {
                    if (mEthashLocalWorkSizeArray[0] != 192)
                    {
                        throw new Exception("No suitable binary file was found.");
                    }
                    string fileName = @"BinaryKernels\" + computeDevice.Name + "_ethash.bin";
                    byte[] binary   = System.IO.File.ReadAllBytes(fileName);
                    mEthashProgram = new ComputeProgram(Context, new List <byte[]>()
                    {
                        binary
                    }, new List <ComputeDevice>()
                    {
                        computeDevice
                    });
                    MainForm.Logger("Loaded " + fileName + " for Device #" + DeviceIndex + ".");
                }
                catch (Exception)
                {
                    //MainForm.Logger("ex.message: " + ex.Message);
                    String source = System.IO.File.ReadAllText(@"Kernels\ethash.cl");
                    mEthashProgram = new ComputeProgram(Context, source);
                    MainForm.Logger(@"Loaded Kernels\ethash.cl for Device #" + DeviceIndex + ".");
                }
                String buildOptions = (OpenCLDevice.GetVendor() == "AMD"    ? "-O1 " :
                                       OpenCLDevice.GetVendor() == "NVIDIA" ? "" : // "-cl-nv-opt-level=1 -cl-nv-maxrregcount=256 " :
                                       "")
                                      + " -IKernels -DWORKSIZE=" + mEthashLocalWorkSizeArray[0];
                try
                {
                    mEthashProgram.Build(OpenCLDevice.DeviceList, buildOptions, null, IntPtr.Zero);
                }
                catch (Exception)
                {
                    MainForm.Logger(mEthashProgram.GetBuildLog(computeDevice));
                    throw;
                }
                MainForm.Logger("Built Ethash program for Device #" + DeviceIndex + ".");
                MainForm.Logger("Build options: " + buildOptions);
                mEthashProgramArray[new long[] { DeviceIndex, mEthashLocalWorkSizeArray[0] }]      = mEthashProgram;
                mEthashDAGKernelArray[new long[] { DeviceIndex, mEthashLocalWorkSizeArray[0] }]    = mEthashDAGKernel = mEthashProgram.CreateKernel("GenerateDAG");
                mEthashSearchKernelArray[new long[] { DeviceIndex, mEthashLocalWorkSizeArray[0] }] = mEthashSearchKernel = mEthashProgram.CreateKernel("search");
            }

            try { mProgramArrayMutex.ReleaseMutex(); } catch (Exception) { }
        }
예제 #9
0
        public void Load(string directory, string kernelFileName, string function)
        {
#if DEBUG
            DeleteCache();
#endif

            m_platform = ComputePlatform.Platforms[0];

            Context = new ComputeContext(ComputeDeviceTypes.Gpu,
                                         new ComputeContextPropertyList(m_platform), null, IntPtr.Zero);

            m_graphicsCard = Context.Devices[0];

            Queue = new ComputeCommandQueue(Context, m_graphicsCard, ComputeCommandQueueFlags.None);

            ComputeProgram program = new ComputeProgram(Context, GetKernelSource(directory + "/" + kernelFileName));
            try
            {
                program.Build(null, "-I " + directory, null, IntPtr.Zero);
            }
            catch
            {
                string error = program.GetBuildLog(m_graphicsCard);
                throw new Exception(error);
            }

            Program = program.CreateKernel(function);
        }
예제 #10
0
        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");
        }
예제 #11
0
        public BlockedMatMulThread(ComputePlatform platform, ComputeDevice device, int inN, float[] inA, float[] inB)
        {
            ComputeDevice[] devices = { device };
            context = new ComputeContext(devices, new ComputeContextPropertyList(platform), null, IntPtr.Zero);
            program = new ComputeProgram(context, clProgramSource);

            try
            {
                program.Build(null, null, null, IntPtr.Zero);
            }
            catch (BuildProgramFailureComputeException e)
            {
                string buildLog = program.GetBuildLog(device);
                Console.WriteLine(buildLog);
                throw;
            }
            kernel = program.CreateKernel("mmul");
            queue  = new ComputeCommandQueue(context, device, ComputeCommandQueueFlags.None);

            n = inN;
            a = new ComputeBuffer <float>(context, ComputeMemoryFlags.ReadOnly | ComputeMemoryFlags.CopyHostPointer, inA);
            b = new ComputeBuffer <float>(context, ComputeMemoryFlags.ReadOnly | ComputeMemoryFlags.CopyHostPointer, inB);
            int resultCount = (int)(device.MaxMemoryAllocationSize / n / n / sizeof(float));

            results = new ComputeBuffer <float> [resultCount];
            for (int i = 0; i < results.Length; ++i)
            {
                results[i] = new ComputeBuffer <float>(context, ComputeMemoryFlags.WriteOnly, n * n);
            }

            HaltRequested     = false;
            operationCounter  = 0;
            completionCounter = 0;
            startTime         = 0;
        }
예제 #12
0
        public ComputeProgram BuildProgramm(string code)
        {
            var programm = new ComputeProgram(Context, code);

            programm.Build(new[] { Device }, null, null, IntPtr.Zero);
            return(programm);
        }
예제 #13
0
        public void Run(ComputeContext context, TextWriter log)
        {
            try
            {
                ComputeProgram program = new ComputeProgram(context, kernelSources);
                program.Build(null, null, null, IntPtr.Zero);
                log.WriteLine("Program successfully built.");
                ICollection <ComputeKernel> kernels = program.CreateAllKernels();
                log.WriteLine("Kernels successfully created.");

                // cleanup kernels
                foreach (ComputeKernel kernel in kernels)
                {
                    kernel.Dispose();
                }
                kernels.Clear();

                // cleanup program
                program.Dispose();
            }
            catch (Exception e)
            {
                log.WriteLine(e.ToString());
            }
        }
예제 #14
0
파일: Weaver.cs 프로젝트: Kawaian/KelpNet
        public static ComputeProgram CreateProgram(string source)
        {
            string realType = Marshal.SizeOf(typeof(Real)) == Marshal.SizeOf(typeof(double)) ? "double" : "float";

            //for precision setting of floating point
            source = REAL_HEADER_STRING + source;

            //Add at double precision
            if (realType == "double")
            {
                source = USE_DOUBLE_HEADER_STRING + source;
            }

            ComputeProgram program = new ComputeProgram(Context, source);

            try
            {
                program.Build(Devices, string.Format("-D REAL={0} -Werror", realType), null, IntPtr.Zero);
            }
            catch
            {
                MessageBox.Show(program.GetBuildLog(Devices[DeviceIndex]));
            }

            return(program);
        }
예제 #15
0
    static void Main(string[] args)
    {
        int[] r1 = new int[]
        { 8, 2, 3, 4 };
        int[] r2 = new int[]
        { 4, 3, 2, 5 };
        int[] r3      = new int[4];
        int   rowSize = r1.Length;
        // pick first platform
        ComputePlatform platform = ComputePlatform.Platforms[0];
        // create context with all gpu devices
        ComputeContext context = new ComputeContext(ComputeDeviceTypes.Gpu,
                                                    new ComputeContextPropertyList(platform), null, IntPtr.Zero);
        // create a command queue with first gpu found
        ComputeCommandQueue queue = new ComputeCommandQueue(context,
                                                            context.Devices[0], ComputeCommandQueueFlags.None);
        // load opencl source and
        // create program with opencl source
        ComputeProgram program = new ComputeProgram(context, CalculateKernel);

        // compile opencl source
        program.Build(null, null, null, IntPtr.Zero);
        // load chosen kernel from program
        ComputeKernel kernel = program.CreateKernel("Calc");
        // allocate a memory buffer with the message (the int array)
        ComputeBuffer <int> row1Buffer = new ComputeBuffer <int>(context,
                                                                 ComputeMemoryFlags.ReadOnly | ComputeMemoryFlags.UseHostPointer, r1);
        // allocate a memory buffer with the message (the int array)
        ComputeBuffer <int> row2Buffer = new ComputeBuffer <int>(context,
                                                                 ComputeMemoryFlags.ReadOnly | ComputeMemoryFlags.UseHostPointer, r2);
        // allocate a memory buffer with the message (the int array)
        ComputeBuffer <int> resultBuffer = new ComputeBuffer <int>(context,
                                                                   ComputeMemoryFlags.ReadOnly | ComputeMemoryFlags.UseHostPointer, new int[4]);

        kernel.SetMemoryArgument(0, row1Buffer);   // set the integer array
        kernel.SetMemoryArgument(1, row2Buffer);   // set the integer array
        kernel.SetValueArgument(2, rowSize);       // set the array size
        kernel.SetMemoryArgument(3, resultBuffer); // set the integer array
        // execute kernel
        queue.ExecuteTask(kernel, null);
        // wait for completion
        queue.Finish();
        GCHandle arrCHandle = GCHandle.Alloc(r3, GCHandleType.Pinned);

        queue.Read <int>(resultBuffer, true, 0, r3.Length, arrCHandle.AddrOfPinnedObject(), null);
        Console.WriteLine("display result from gpu buffer:");
        for (int i = 0; i < r3.Length; i++)
        {
            Console.WriteLine(r3[i]);
        }
        arrCHandle.Free();
        row1Buffer.Dispose();
        row2Buffer.Dispose();
        kernel.Dispose();
        program.Dispose();
        queue.Dispose();
        context.Dispose();
        Console.WriteLine("Finished");
        Console.ReadKey();
    }
예제 #16
0
파일: Kernels.cs 프로젝트: abasilak/phd
        public Kernel(string name, string compile_options, string [] kernel_names)
        {
            _path = _paths + name + _exts;
            StreamReader streamReader = new StreamReader(_path);
            string       source       = streamReader.ReadToEnd();

            streamReader.Close();

            // Create and build the opencl program.
            _program = new ComputeProgram(Example.context, source);
            _program.Build(Example.context.Devices, compile_options, null, IntPtr.Zero);

            // Create the kernel function and set its arguments.
            ComputeKernel kernel;

            for (int i = 0; i < kernel_names.Length; i++)
            {
                kernel = _program.CreateKernel(kernel_names[i]);
                _kernels.Add(kernel);
            }
            _eventList = new ComputeEventList();
            // Create the command queue. This is used to control kernel execution and manage read/write/copy operations.
            _commands = new ComputeCommandQueue(Example.context, Example.context.Devices[0], ComputeCommandQueueFlags.None);

            Console.WriteLine(_path);
        }
예제 #17
0
        public void SetKernel(string OpenCLBody, string EntryPoint)
        {
            this.OpenCLBody = OpenCLBody;
            this.EntryPoint = EntryPoint;
            ComputeProgram program = new ComputeProgram(context, OpenCLBody);

            try
            {
                program.Build(null, null, null, IntPtr.Zero);
                kernel = program.CreateKernel(EntryPoint);
            }
            catch (BuildProgramFailureComputeException)
            {
                string message = program.GetBuildLog(Accelerator.Device);
                throw new ArgumentException(message);
            }
            catch (ComputeException)
            {
                string message = program.GetBuildLog(Accelerator.Device);
                throw new ArgumentException(message);
            }

            MethodInfo = new CLMethod(EntryPoint, OpenCLBody);
            MethodSet  = true;
        }
예제 #18
0
        public void SetDevice(int deviceIndx)
        {
            if ((deviceIndx < 0) || (deviceIndx >= oclDevices.Count))
            {
                throw new IndexOutOfRangeException("Invalid OpenCL device index.");
            }

            if (oclContext != null)
            {
                oclContext.Dispose();
                oclContext = null;
            }

            if (oclCommandQueue != null)
            {
                oclCommandQueue.Dispose();
                oclCommandQueue = null;
            }

            if (oclKernel != null)
            {
                oclKernel.Dispose();
                oclKernel = null;
            }

            ComputeProgram oclProgram = null;

            try
            {
                oclContext = new ComputeContext(new ComputeDevice[] { oclDevices[deviceIndx] },
                                                new ComputeContextPropertyList(oclDevices[deviceIndx].Platform), null, IntPtr.Zero);

                oclCommandQueue = new ComputeCommandQueue(oclContext, oclDevices[deviceIndx],
                                                          ComputeCommandQueueFlags.None);

                oclProgram = new ComputeProgram(oclContext,
                                                Encoding.Default.GetString(Properties.Resources.Test));

                oclProgram.Build(new ComputeDevice[] { oclDevices[deviceIndx] }, "", null, IntPtr.Zero);

                oclKernel = oclProgram.CreateKernel("Test");
            }
            catch (BuildProgramFailureComputeException ex)
            {
                string buildLog = oclProgram.GetBuildLog(oclDevices[deviceIndx]);
                throw new Exception(buildLog, ex);
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (oclProgram != null)
                {
                    oclProgram.Dispose();
                    oclProgram = null;
                }
            }
        }
예제 #19
0
        /// <summary>
        /// Initializes a new instance of the <see cref="Julia"/> class.
        /// </summary>
        public Julia()
            : base()
        {
            this.Mode = ConcurrencyMode.SequentialCPU;

            this.options = new ParallelOptions();

            this.options.MaxDegreeOfParallelism = Environment.ProcessorCount;

            // Initialize OpenCL.
            platform   = ComputePlatform.Platforms[0];
            properties = new ComputeContextPropertyList(platform);
            context    = new ComputeContext(platform.Devices,
                                            properties, null, IntPtr.Zero);

            // Create the OpenCL kernel.
            program = new ComputeProgram(context, new string[] { kernelSource });
            program.Build(null, "-cl-mad-enable", null, IntPtr.Zero);
            kernel = program.CreateKernel("julia");

            // Create objects needed for kernel launch/execution.
            commands = new ComputeCommandQueue(context,
                                               context.Devices[0], ComputeCommandQueueFlags.None);
            events = new ComputeEventList();
        }
        public void BuildNeoScryptProgram()
        {
            ComputeDevice computeDevice = OpenCLDevice.GetComputeDevice();

            try { mProgramArrayMutex.WaitOne(5000); } catch (Exception) { }

            if (mNeoScryptProgramArray.ContainsKey(new ProgramArrayIndex(DeviceIndex, mNeoScryptLocalWorkSizeArray[0])))
            {
                mNeoScryptProgram      = mNeoScryptProgramArray[new ProgramArrayIndex(DeviceIndex, mNeoScryptLocalWorkSizeArray[0])];
                mNeoScryptSearchKernel = mNeoScryptSearchKernelArray[new ProgramArrayIndex(DeviceIndex, mNeoScryptLocalWorkSizeArray[0])];
            }
            else
            {
                String source = System.IO.File.ReadAllText(@"Kernels\neoscrypt.cl");
                mNeoScryptProgram = new ComputeProgram(Context, source);
                MainForm.Logger(@"Loaded Kernels\neoscrypt.cl for Device #" + DeviceIndex + ".");
                String buildOptions = (OpenCLDevice.GetVendor() == "AMD" ? "-O5 -legacy" : // "-legacy" :
                                       OpenCLDevice.GetVendor() == "NVIDIA" ? "" :         //"-cl-nv-opt-level=1 -cl-nv-maxrregcount=256 " :
                                       "")
                                      + " -IKernels -DWORKSIZE=" + mNeoScryptLocalWorkSizeArray[0];
                try {
                    mNeoScryptProgram.Build(OpenCLDevice.DeviceList, buildOptions, null, IntPtr.Zero);
                } catch (Exception) {
                    MainForm.Logger(mNeoScryptProgram.GetBuildLog(computeDevice));
                    throw;
                }
                MainForm.Logger("Built NeoScrypt program for Device #" + DeviceIndex + ".");
                MainForm.Logger("Build options: " + buildOptions);
                mNeoScryptProgramArray[new ProgramArrayIndex(DeviceIndex, mNeoScryptLocalWorkSizeArray[0])]      = mNeoScryptProgram;
                mNeoScryptSearchKernelArray[new ProgramArrayIndex(DeviceIndex, mNeoScryptLocalWorkSizeArray[0])] = mNeoScryptSearchKernel = mNeoScryptProgram.CreateKernel("search");
            }

            try { mProgramArrayMutex.ReleaseMutex(); } catch (Exception) { }
        }
예제 #21
0
        public static ComputeProgram CreateProgram(string source)
        {
            string realType = Real.Size == sizeof(double) ? "double" : "float";

            //浮動小数点の精度設定用
            source = REAL_HEADER_STRING + source;

            //倍精度時に追加
            if (realType == "double")
            {
                source = USE_DOUBLE_HEADER_STRING + source;
            }

            ComputeProgram program = new ComputeProgram(Context, source);

            try
            {
                program.Build(ComputePlatform.Platforms[PlatformId].Devices, string.Format("-D REAL={0} -Werror", realType), null, IntPtr.Zero);
            }
            catch (Exception e)
            {
                throw new Exception(program.GetBuildLog(ComputePlatform.Platforms[PlatformId].Devices[DeviceIndex]), e);
            }

            return(program);
        }
예제 #22
0
        public KernelManager(GraphicsInterop interop, InputManager input, string source)
        {
            _input = input;
            var localSizeSingle = (long)Math.Sqrt(interop.Device.MaxWorkGroupSize);

            _localSize = new[] { localSizeSingle, localSizeSingle };
            //_localSize = new[] { interop.Device.MaxWorkGroupSize, 1 };

            _program = new ComputeProgram(interop.Context, source);
            try
            {
                _program.Build(new[] { interop.Device }, "", null, IntPtr.Zero);
            }
            catch (InvalidBinaryComputeException)
            {
                Console.WriteLine(_program.GetBuildLog(interop.Device));
                return;
            }
            catch (BuildProgramFailureComputeException)
            {
                Console.WriteLine(_program.GetBuildLog(interop.Device));
                return;
            }
            Console.WriteLine(_program.GetBuildLog(interop.Device));
            _kernels = _program.CreateAllKernels().ToArray();
        }
예제 #23
0
        public TFP Hello(float num)
        {
            ComputeBuffer <TFP> result = new ComputeBuffer <TFP>(context, ComputeMemoryFlags.WriteOnly, 1);
            string source = Encoding.ASCII.GetString(FZYK.Nest.Properties.Resources.nest);

            if (fpType == FPType.FP64AMD)
            {
                source = "#define AMDFP64\n" + source;
            }
            else if (fpType == FPType.FP64)
            {
                source = "#define FP64\n" + source;
            }
            ComputeProgram program = new ComputeProgram(context, source);

            try
            {
                program.Build(null, null, null, IntPtr.Zero);
            }
            catch (Exception)
            {
                var log = program.GetBuildLog(context.Devices[0]);
                Debugger.Break();
            }

            ComputeKernel kernel = program.CreateKernel("hello");

            TFP[] myresult = RunKernalTest(num, result, kernel);
            return(myresult[0]);
        }
예제 #24
0
        protected ComputeProgram BuildProgram(string programName, long localWorkSize, string optionsAMD, string optionsNVIDIA, string optionsOthers)
        {
            ComputeProgram program;
            string         defultBinaryFilePath = @"BinaryKernels\" + ComputeDevice.Name + "_" + programName + "_" + localWorkSize + ".bin";
            string         savedBinaryFilePath  = (MainForm.SavedOpenCLBinaryKernelPathBase + @"\") + ComputeDevice.Name + "_" + programName + "_" + localWorkSize + ".bin";
            string         sourceFilePath       = @"Kernels\" + programName + ".cl";
            String         buildOptions         = (OpenCLDevice.GetVendor() == "AMD" ? optionsAMD : OpenCLDevice.GetVendor() == "NVIDIA" ? optionsNVIDIA : optionsOthers) + " -IKernels -DWORKSIZE=" + localWorkSize;

            try {
                if (!MainForm.UseDefaultOpenCLBinariesChecked)
                {
                    throw new Exception();
                }
                byte[] binary = System.IO.File.ReadAllBytes(defultBinaryFilePath);
                program = new ComputeProgram(Context, new List <byte[]>()
                {
                    binary
                }, new List <ComputeDevice>()
                {
                    ComputeDevice
                });
                MainForm.Logger("Loaded " + defultBinaryFilePath + " for Device #" + DeviceIndex + ".");
            } catch (Exception) {
                try {
                    if (!MainForm.ReuseCompiledBinariesChecked)
                    {
                        throw new Exception();
                    }
                    byte[] binary = System.IO.File.ReadAllBytes(savedBinaryFilePath);
                    program = new ComputeProgram(Context, new List <byte[]>()
                    {
                        binary
                    }, new List <ComputeDevice>()
                    {
                        ComputeDevice
                    });
                    MainForm.Logger("Loaded " + savedBinaryFilePath + " for Device #" + DeviceIndex + ".");
                } catch (Exception) {
                    String source = System.IO.File.ReadAllText(sourceFilePath);
                    program = new ComputeProgram(Context, source);
                    MainForm.Logger(@"Loaded " + sourceFilePath + " for Device #" + DeviceIndex + ".");
                }
            }
            try {
                program.Build(OpenCLDevice.DeviceList, buildOptions, null, IntPtr.Zero);
                if (MainForm.ReuseCompiledBinariesChecked)
                {
                    System.IO.File.WriteAllBytes(savedBinaryFilePath, program.Binaries[0]);
                }
            } catch (Exception) {
                MainForm.Logger(program.GetBuildLog(ComputeDevice));
                program.Dispose();
                throw;
            }
            MainForm.Logger("Built " + programName + " program for Device #" + DeviceIndex + ".");
            MainForm.Logger("Build options: " + buildOptions);

            return(program);
        }
예제 #25
0
 public FindPointOpenCl()
 {
     cpl     = new ComputeContextPropertyList(ComputePlatform.Platforms[0]);
     context = new ComputeContext(ComputeDeviceTypes.Gpu, cpl, null, IntPtr.Zero);
     program = new ComputeProgram(context, new string[] { FindPointCalculationGpu });
     program.Build(null, null, null, IntPtr.Zero);
     kernel = program.CreateKernel("FindPointCalculationGpu");
 }
예제 #26
0
        public static Tuple <List <List <int> >, TimeSpan> MultiplyParallel(List <List <int> > matrixOne, List <List <int> > matrixTwo)
        {
            if (!isRegularMatrix(matrixOne) || !isRegularMatrix(matrixTwo))
            {
                throw new ArgumentException("Non regular matrix detected. Rows size mismatch detected.");
            }
            if (matrixOne[0].Count != matrixTwo.Count)
            {
                throw new ArgumentException("Matrixes is not compatible. Columns count of first matrix is not equal to rows count of second matrix.");
            }
            List <List <int> > result   = new List <List <int> >();
            ComputePlatform    platform = GetGPU();

            if (platform is null)
            {
                throw new PlatformNotSupportedException("Platform doesn't have a dedicated GPU. Run is impossible.");
            }
            ComputeContext      context = new ComputeContext(ComputeDeviceTypes.Gpu, new ComputeContextPropertyList(platform), null, IntPtr.Zero);
            ComputeCommandQueue queue   = new ComputeCommandQueue(context, context.Devices[0], ComputeCommandQueueFlags.None);
            ComputeProgram      program = new ComputeProgram(context, CalculateKernel);

            program.Build(null, null, null, IntPtr.Zero);
            ComputeKernel kernel = program.CreateKernel("Multiply");

            List <ComputeBuffer <int> > rowsMatrixOne    = matrixOne.TransformMatrixToComputerBuffersOfRows(context);
            List <ComputeBuffer <int> > columnsMatrixTwo = matrixTwo.TransformMatrixToComputerBuffersOfColumns(context);
            List <ComputeBuffer <int> > resultRowsMatrix = TwoDToOneDResult(matrixOne.Count, matrixTwo[0].Count, context);

            Stopwatch stopwatch = new Stopwatch();

            stopwatch.Start();
            for (int i = 0; i < resultRowsMatrix.Count; ++i)
            {
                for (int j = 0; j < resultRowsMatrix[i].Count; ++j)
                {
                    kernel.SetMemoryArgument(0, rowsMatrixOne[i]);
                    kernel.SetMemoryArgument(1, columnsMatrixTwo[j]);
                    kernel.SetMemoryArgument(2, resultRowsMatrix[i]);
                    kernel.SetValueArgument(3, matrixTwo.Count);
                    kernel.SetValueArgument(4, j);

                    queue.ExecuteTask(kernel, null);
                }
            }

            queue.Finish();
            stopwatch.Stop();

            for (int i = 0; i < resultRowsMatrix.Count; ++i)
            {
                int[]    res      = new int[resultRowsMatrix[i].Count];
                GCHandle gCHandle = GCHandle.Alloc(res, GCHandleType.Pinned);
                queue.Read <int>(resultRowsMatrix[i], true, 0, res.Length, gCHandle.AddrOfPinnedObject(), null);
                result.Add(new List <int>(res));
            }

            return(new Tuple <List <List <int> >, TimeSpan>(result, stopwatch.Elapsed));
        }
        public async Task Can_use_struct()
        {
            var platform = ComputePlatform.Platforms.First();
            var device   = platform.Devices.First();
            var context  = new ComputeContext(new[] { device }, new ComputeContextPropertyList(platform), null, IntPtr.Zero);

            using var program = new ComputeProgram(context, Struct_kernel);

            try
            {
                program.Build(new[] { device }, "-cl-std=CL1.2", null, IntPtr.Zero);
            }
            catch (Exception ex)
            {
                OnProgramBuilt(program, device);
                return;
            }

            using var queue  = new ComputeCommandQueue(context, device, ComputeCommandQueueFlags.None);
            using var kernel = program.CreateKernel("fill");

            var target   = new Target[10];
            var gcHandle = GCHandle.Alloc(target, GCHandleType.Pinned);
            var ptr      = gcHandle.AddrOfPinnedObject();
            var buffer   = new ComputeBuffer <Target>(
                context,
                ComputeMemoryFlags.ReadWrite | ComputeMemoryFlags.UseHostPointer,
                10,
                ptr);


            kernel.SetMemoryArgument(0, buffer);

            var events = new List <ComputeEventBase>();

            queue.Execute(kernel, null, new long[] { 10 }, null, events);
            //await events.WaitForEvents();

            unsafe
            {
                target[0].a.Should().Be(0);
                target[0].b.Should().BeApproximately(0.5f, 0.01f);
                target[0].c.Should().Be(1);
                target[0].d[0].Should().Be(1);
                target[0].d[1].Should().Be(2);
                target[1].a.Should().Be(2);
                target[1].b.Should().BeApproximately(1.5f, 0.01f);
                target[1].c.Should().Be(2);
                target[1].d[0].Should().Be(1);
                target[1].d[1].Should().Be(2);
                target[9].a.Should().Be(18);
                target[9].b.Should().BeApproximately(9.5f, 0.01f);
                target[9].c.Should().Be(10);
                target[9].d[0].Should().Be(1);
                target[9].d[1].Should().Be(2);
            }
        }
        public async Task Can_multiply_using_pinned_host_pointer()
        {
            // This doesn't seem to work on NVidia platforms
            var platform = ComputePlatform.Platforms.First();
            var device   = platform.Devices.First();
            var context  = new ComputeContext(new[] { device }, new ComputeContextPropertyList(platform), null,
                                              IntPtr.Zero);

            using var program = new ComputeProgram(context, Sum_kernel);

            program.Build(new[] { device }, "-cl-std=CL1.2", (handle, ptr) => OnProgramBuilt(program, device),
                          IntPtr.Zero);

            using var queue  = new ComputeCommandQueue(context, device, ComputeCommandQueueFlags.None);
            using var kernel = program.CreateKernel("sum");

            var source       = new byte[] { 1, 2, 3, 4, 5 };
            var sourceHandle = GCHandle.Alloc(source, GCHandleType.Pinned);
            var sourcePtr    = sourceHandle.AddrOfPinnedObject();

            using var sourceBuffer = new ComputeBuffer <byte>(
                      context,
                      ComputeMemoryFlags.ReadWrite | ComputeMemoryFlags.UseHostPointer,
                      source.LongLength,
                      sourcePtr);

            var target       = new byte[5];
            var targetHandle = GCHandle.Alloc(target, GCHandleType.Pinned);
            var targetPtr    = targetHandle.AddrOfPinnedObject();

            using var targetBuffer = new ComputeBuffer <byte>(
                      context,
                      ComputeMemoryFlags.ReadWrite | ComputeMemoryFlags.UseHostPointer,
                      target.LongLength,
                      targetPtr);

            try
            {
                kernel.SetMemoryArgument(0, sourceBuffer);
                kernel.SetMemoryArgument(1, targetBuffer);

                var events = new List <ComputeEventBase>();
                queue.Execute(kernel, null, new long[] { source.Length }, null, events);

                //await events.WaitForEvents();

                for (var i = 0; i < target.Length; i++)
                {
                    Console.WriteLine($"{source[i]} * 2 = {target[i]}");
                }
            }
            finally
            {
                sourceHandle.Free();
                targetHandle.Free();
            }
        }
예제 #29
0
        public void BuildLbryProgram()
        {
            ComputeDevice computeDevice = OpenCLDevice.GetComputeDevice();

            try { mProgramArrayMutex.WaitOne(5000); } catch (Exception) { }

            if (mLbryProgramArray.ContainsKey(new ProgramArrayIndex(DeviceIndex, mLbryLocalWorkSizeArray[0])))
            {
                mLbryProgram      = mLbryProgramArray[new ProgramArrayIndex(DeviceIndex, mLbryLocalWorkSizeArray[0])];
                mLbrySearchKernel = mLbrySearchKernelArray[new ProgramArrayIndex(DeviceIndex, mLbryLocalWorkSizeArray[0])];
            }
            else
            {
                try
                {
                    if (mLbryLocalWorkSizeArray[0] != 256)
                    {
                        throw new Exception("No suitable binary file was found.");
                    }
                    string fileName = @"BinaryKernels\" + computeDevice.Name + "_lbry.bin";
                    byte[] binary   = System.IO.File.ReadAllBytes(fileName);
                    mLbryProgram = new ComputeProgram(Context, new List <byte[]>()
                    {
                        binary
                    }, new List <ComputeDevice>()
                    {
                        computeDevice
                    });
                    //MainForm.Logger("Loaded " + fileName + " for Device #" + DeviceIndex + ".");
                }
                catch (Exception)
                {
                    String source = System.IO.File.ReadAllText(@"Kernels\lbry.cl");
                    mLbryProgram = new ComputeProgram(Context, source);
                    //MainForm.Logger(@"Loaded Kernels\lbry.cl for Device #" + DeviceIndex + ".");
                }
                String buildOptions = (OpenCLDevice.GetVendor() == "AMD" ? "-O1 " : //"-O1 " :
                                       OpenCLDevice.GetVendor() == "NVIDIA" ? "" :  //"-cl-nv-opt-level=1 -cl-nv-maxrregcount=256 " :
                                       "")
                                      + " -IKernels -DWORKSIZE=" + mLbryLocalWorkSizeArray[0] + " -DITERATIONS=" + mIterations;
                try
                {
                    mLbryProgram.Build(OpenCLDevice.DeviceList, buildOptions, null, IntPtr.Zero);
                }
                catch (Exception)
                {
                    MainForm.Logger(mLbryProgram.GetBuildLog(computeDevice));
                    throw;
                }
                //MainForm.Logger("Built Lbry program for Device #" + DeviceIndex + ".");
                //MainForm.Logger("Build options: " + buildOptions);
                mLbryProgramArray[new ProgramArrayIndex(DeviceIndex, mLbryLocalWorkSizeArray[0])]      = mLbryProgram;
                mLbrySearchKernelArray[new ProgramArrayIndex(DeviceIndex, mLbryLocalWorkSizeArray[0])] = mLbrySearchKernel = mLbryProgram.CreateKernel("search_combined");
            }

            try { mProgramArrayMutex.ReleaseMutex(); } catch (Exception) { }
        }
예제 #30
0
파일: GPU.cs 프로젝트: afercin/TestGPUCopy
        public GPU()
        {
            context = new ComputeContext(ComputeDeviceTypes.Gpu, new ComputeContextPropertyList(ComputePlatform.Platforms[0]), null, IntPtr.Zero);
            queue   = new ComputeCommandQueue(context, context.Devices[0], ComputeCommandQueueFlags.None);
            var program = new ComputeProgram(context, ArrayCopy);

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

            kernel = program.CreateKernel("ArrayCopy");
        }