Example #1
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();
        }
        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());
            }
        }
Example #3
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);
        }
Example #4
0
        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);
        }
Example #5
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();
    }
Example #6
0
        public ComputeProgram BuildProgramm(string code)
        {
            var programm = new ComputeProgram(Context, code);

            programm.Build(new[] { Device }, null, null, IntPtr.Zero);
            return(programm);
        }
Example #7
0
        public static bool SetParallel <T>(this ICompressibleFunction <T> compressibleFunction, bool enable) where T : unmanaged, IComparable <T>
        {
            compressibleFunction.IsParallel = enable & OpenCL.Enable;

            if (compressibleFunction.IsParallel)
            {
                string kernelNameBase = compressibleFunction.FunctionName.Replace(" ", "");
                compressibleFunction.ForwardKernelName    = kernelNameBase + "Forward";
                compressibleFunction.BackwardgWKernelName = kernelNameBase + "gWBackward";
                compressibleFunction.BackwardgXKernelName = kernelNameBase + "gXBackward";

                string kernelSource = compressibleFunction.KernelSource;

                if (compressibleFunction.Activation is ICompressibleActivation <T> activator)
                {
                    //アクティベーションを活性化
                    kernelSource = activator.KernelSource + kernelSource.Replace("/*ForwardActivate*/", "ForwardActivate");
                }

                ComputeProgram program = OpenCL.CreateProgram <T>(kernelSource);
                compressibleFunction.ForwardKernel    = program.CreateKernel(compressibleFunction.ForwardKernelName);
                compressibleFunction.BackwardgWKernel = program.CreateKernel(compressibleFunction.BackwardgWKernelName);
                compressibleFunction.BackwardgXKernel = program.CreateKernel(compressibleFunction.BackwardgXKernelName);
            }

            return(compressibleFunction.IsParallel);
        }
Example #8
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;
     }
 }
        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) { }
        }
Example #10
0
        public void CreateKernel()
        {
            if (this.GpuEnable)
            {
                string kernelSource = this.KernelString;

                if (this.Activator != null)
                {
                    string activationSource = this.Activator.ActivateFunctionString;

                    foreach (var activationParameter in this.Activator.ActivationParameters)
                    {
                        activationSource = activationSource.Replace(activationParameter.Key, activationParameter.Value);
                    }

                    //アクティベーションを活性化
                    kernelSource = activationSource + kernelSource.Replace("/*ForwardActivate*/", "ForwardActivate");
                }

                ComputeProgram program = Weaver.CreateProgram(kernelSource);
                this.ForwardKernel    = program.CreateKernel(this.ForwardKernelName);
                this.BackwardgWKernel = program.CreateKernel(this.BackwardgWKernelName);
                this.BackwardgXKernel = program.CreateKernel(this.BackwardgXKernelName);
            }
        }
Example #11
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);
        }
Example #12
0
        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);
        }
Example #13
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");
        }
Example #14
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
		
	}
Example #15
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;
            }
        }
        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) { }
        }
Example #17
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);
        }
Example #18
0
        public rayTracerUI()
        {
            InitializeComponent();

            // pick first platform
            platform = ComputePlatform.Platforms[0];

            // create context with all gpu devices
            context = new ComputeContext(ComputeDeviceTypes.Gpu,
                                         new ComputeContextPropertyList(platform), null, IntPtr.Zero);

            // create a command queue with first gpu found
            queue = new ComputeCommandQueue(context,
                                            context.Devices[0], ComputeCommandQueueFlags.None);

            // load opencl source
            streamReader = new StreamReader("../../kernels.cl");
            string clSource = streamReader.ReadToEnd();

            streamReader.Close();

            // create program with opencl source
            program = new ComputeProgram(context, clSource);

            // compile opencl source
            //program.Build(null, null, null, IntPtr.Zero);

            // load chosen kernel from program
            // kernel = program.CreateKernel("findIntersection");

            threadsTxt.Text = Environment.ProcessorCount.ToString();
        }
Example #19
0
        void InitComputeKernel()
        {
            if (this.openCLSources == null)
            {
                GetOpenCLSources();
            }

            var properties = new ComputeContextPropertyList(this.computeDevice.Platform);

            this.computeContext = new ComputeContext(new[] { this.computeDevice }, properties, null, IntPtr.Zero);
            this.computeProgram = new ComputeProgram(this.computeContext, this.openCLSources);

            try
            {
                this.computeProgram.Build(new[] { this.computeDevice }, null, null, IntPtr.Zero);
            }

            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                Console.WriteLine(computeProgram.GetBuildLog(this.computeDevice));
            }

            this.computeKernels = this.computeProgram.CreateAllKernels().ToList();
            this.computeKernel  = this.computeKernels.First(k => k.FunctionName == openCLKernelFunction);
        }
Example #20
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;
                }
            }
        }
Example #21
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 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;
        }
Example #23
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;
        }
Example #24
0
        /// <summary>
        /// 栅格化,范围gridlib
        /// </summary>
        /// <param name="pointlist"></param>
        /// <param name="W"></param>
        /// <param name="H"></param>
        /// <param name="T"></param>
        /// <param name="program"></param>
        /// <returns></returns>
        public GridLib GetGridValue(PlateModel pm, float T, ComputeProgram program)
        {
            int[,] gridarray = GetGridArray(pm, T, program);
            int WI = gridarray.GetLength(0);
            int HI = gridarray.GetLength(1);

            int[,] gridarrayzero = new int[WI, HI];
            List <GridData> grid = new List <GridData>();

            for (int i = WI - 1; i >= 0; i--)
            {
                int v  = 1;
                int vz = 1;
                for (int j = HI - 1; j >= 0; j--)
                {
                    if (gridarray[i, j] == 1)
                    {
                        gridarray[i, j] = v;
                        grid.Insert(0, new GridData(i, j, v));
                        v++;

                        gridarrayzero[i, j] = 0;
                        vz = 1;
                    }
                    else
                    {
                        v = 1;

                        gridarrayzero[i, j] = vz;
                        vz++;
                    }
                }
            }
            return(new GridLib(grid, gridarray, gridarrayzero));
        }
Example #25
0
        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);
        }
Example #26
0
        /// <summary>
        /// 栅格化
        /// </summary>
        /// <param name="pointlist"></param>
        /// <param name="W"></param>
        /// <param name="H"></param>
        /// <param name="T"></param>
        /// <param name="program"></param>
        /// <returns></returns>
        public int[] Insert(int[] test1, int[] test2, ComputeProgram program)
        {
            int c1 = test1.GetLength(0);
            int c2 = test2.GetLength(0);
            ComputeBuffer <int> buffer1 = new ComputeBuffer <int>(context, ComputeMemoryFlags.ReadOnly | ComputeMemoryFlags.CopyHostPointer, test1);
            ComputeBuffer <int> buffer2 = new ComputeBuffer <int>(context, ComputeMemoryFlags.ReadOnly | ComputeMemoryFlags.CopyHostPointer, test2);
            ComputeBuffer <int> result  = new ComputeBuffer <int>(context, ComputeMemoryFlags.ReadWrite, c1);

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

            kernel.SetMemoryArgument(0, buffer1);
            kernel.SetMemoryArgument(1, buffer2);
            kernel.SetMemoryArgument(2, result);
            commands.Execute(kernel, null, new long[] { c1 }, null, events);

            int[]    resultnum  = new int[c1];
            GCHandle arrCHandle = GCHandle.Alloc(resultnum, GCHandleType.Pinned);

            commands.Read(result, true, 0, c1, arrCHandle.AddrOfPinnedObject(), events);

            arrCHandle.Free();
            kernel.Dispose();
            buffer1.Dispose();
            buffer2.Dispose();
            result.Dispose();
            return(resultnum);
        }
Example #27
0
        public static bool SetParallel <T>(this ICompressibleActivation <T> compressibleActivation, bool enable, KeyValuePair <string, string>[] activationParameters = null) where T : unmanaged, IComparable <T>
        {
            compressibleActivation.IsParallel = enable & OpenCL.Enable;

            if (compressibleActivation.IsParallel)
            {
                string kernelNameBase = compressibleActivation.FunctionName.Replace(" ", "");
                compressibleActivation.ActivateKernelString = OpenCL.GetKernelSource(Resources.Activation).Replace("/*kernelNameBase*/", kernelNameBase);
                compressibleActivation.ForwardKernelName    = kernelNameBase + "Forward";
                compressibleActivation.BackwardKernelName   = kernelNameBase + "Backward";

                string kernelSource = compressibleActivation.KernelSource;

                if (activationParameters != null)
                {
                    foreach (var parameter in activationParameters)
                    {
                        kernelSource = kernelSource.Replace(parameter.Key, parameter.Value);
                    }
                }

                kernelSource += compressibleActivation.ActivateKernelString;

                ComputeProgram program = OpenCL.CreateProgram <T>(kernelSource);
                compressibleActivation.ForwardKernel  = program.CreateKernel(compressibleActivation.ForwardKernelName);
                compressibleActivation.BackwardKernel = program.CreateKernel(compressibleActivation.BackwardKernelName);
            }

            return(compressibleActivation.IsParallel);
        }
Example #28
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]);
        }
Example #29
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);
        }
Example #30
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");
        }