Exemple #1
0
        /// <summary>
        /// Initializes handle
        /// </summary>
        /// <returns> whether or not the handle was intialized succesfully </returns>
        public void Init()
        {
            Platform[] platforms = Cl.GetPlatformIDs(out _error);
            CLException.CheckException(_error);

            Console.WriteLine("Found " + platforms.Length + " platform(s)");

            InfoBuffer platformNameBuffer = Cl.GetPlatformInfo(platforms[0], PlatformInfo.Name, out _error);

            CLException.CheckException(_error);
            Console.WriteLine(platformNameBuffer.ToString());

            Device[] devices = Cl.GetDeviceIDs(platforms[0], DeviceType.Gpu, out _error);
            CLException.CheckException(_error);
            _device = devices[0];
            InfoBuffer deviceNameBuffer = Cl.GetDeviceInfo(Device, DeviceInfo.Platform, out _error);

            CLException.CheckException(_error);
            Console.WriteLine(deviceNameBuffer.ToString());

            _context = Cl.CreateContext(null, 1, new[] { devices[0] }, null, IntPtr.Zero, out _error);
            CLException.CheckException(_error);
            _queue = Cl.CreateCommandQueue(Context, Device, CommandQueueProperties.None, out _error);
            CLException.CheckException(_error);
        }
Exemple #2
0
        /// <summary>
        /// Creates a kernel from a program
        /// </summary>
        /// <param name="kernelName">the name of the kernel</param>
        public void CreateKernel(string kernelName)
        {
            Kernel kernel = Cl.CreateKernel(_program, kernelName, out _error);

            CLException.CheckException(_error);
            _kernels.Add(kernelName, kernel);
        }
Exemple #3
0
        /// <summary>
        /// Executes a 1D kernel
        /// </summary>
        /// <param name="kernel">Name of the kernel to execute</param>
        /// <param name="globalWorkSize">Total Number of work items</param>
        /// <param name="localWorkSize">Number of work items per local group</param>
        public void ExecuteKernel1D(string kernel, int globalWorkSize, int localWorkSize)
        {
            Event e;

            _error = Cl.EnqueueNDRangeKernel(_handle.Queue, _kernels[kernel], 1, null, new[] { new IntPtr(globalWorkSize) }, new[] { new IntPtr(localWorkSize) }, 0, null, out e);
            CLException.CheckException(_error);
        }
Exemple #4
0
 public void Dispose()
 {
     foreach (var kernel in _kernels.Values)
     {
         Cl.ReleaseKernel(kernel);
     }
     _error = Cl.ReleaseProgram(_program);
     CLException.CheckException(_error);
 }
Exemple #5
0
 /// <summary>
 /// Creates all potential kernels in program
 /// </summary>
 public void CreateAllKernels()
 {
     Kernel[] kernels = Cl.CreateKernelsInProgram(_program, out _error);
     CLException.CheckException(_error);
     for (int i = 0; i < kernels.Length; i++)
     {
         InfoBuffer name = Cl.GetKernelInfo(kernels[i], KernelInfo.FunctionName, out _error);
         _kernels.Add(name.ToString(), kernels[i]);
     }
 }
Exemple #6
0
        /// <summary>
        /// Creates the program from source code
        /// </summary>
        /// <param name="sourceCode">source code</param>
        public void CreateProgram(string[] sourceCode)
        {
            string source = "";

            for (int i = 0; i < sourceCode.Length; i++)
            {
                source += sourceCode[i];
            }

            _program = Cl.CreateProgramWithSource(_handle.Context, 1,
                                                  new[] { source }, new[] { new IntPtr(source.Length) }, out _error);
            CLException.CheckException(_error);
        }
Exemple #7
0
 /// <summary>
 /// Builds Program
 /// </summary>
 public void BuildProgram()
 {
     try
     {
         _error = Cl.BuildProgram(_program, 0, null, null, null, IntPtr.Zero);
         CLException.CheckException(_error);
     }
     catch (CLException)
     {
         Console.WriteLine("Could not build program!");
         InfoBuffer buildLog = Cl.GetProgramBuildInfo(_program, _handle.Device, ProgramBuildInfo.Log, out _error);
         CLException.CheckException(_error);
         Console.WriteLine("Build Log: " + buildLog.ToString());
     }
 }
Exemple #8
0
        /// <summary>
        /// Writes an image to GPU memeory
        /// </summary>
        /// <param name="image">the image to write</param>
        /// <param name="memFlags">the memory flags</param>
        public void WriteImage(Bitmap image, MemFlags memFlags)
        {
            ErrorCode error;

            _image = Cl.CreateImage2D(_handle.Context, memFlags, new OpenCL.Net.ImageFormat(ChannelOrder.RGBA, ChannelType.Unorm_Int8),
                                      new IntPtr(image.Width), new IntPtr(image.Height), new IntPtr(0), new IntPtr(0), out error);
            CLException.CheckException(error);

            byte[]   data        = BmpToBytes(image);
            IntPtr[] originArray = new IntPtr[] { new IntPtr(0), new IntPtr(0), new IntPtr(0) };
            IntPtr[] sizeArray   = new IntPtr[] { new IntPtr(image.Width), new IntPtr(image.Height), new IntPtr(1) };

            Event e;

            error = Cl.EnqueueWriteImage(_handle.Queue, _image, Bool.True,
                                         originArray, sizeArray, new IntPtr(0), new IntPtr(0),
                                         data, 0, null, out e);
            CLException.CheckException(error);
        }
Exemple #9
0
 /// <summary>
 /// Sets an image as a kernel argument
 /// </summary>
 /// <param name="kernel">Name of the kernel to set the image paratmeter of</param>
 /// <param name="argIndex">The argument index to set</param>
 /// <param name="image">The image to set the argument of</param>
 public void SetKernelArg(string kernel, uint argIndex, GPUImage image)
 {
     _error = Cl.SetKernelArg(_kernels[kernel], argIndex, image.ImageBufferHandle);
     CLException.CheckException(_error);
 }
Exemple #10
0
 /// <summary>
 /// Sets the argument of the kernel's
 /// </summary>
 /// <param name="kernel">Name of the kernel to set the paramter of</param>
 /// <param name="argIndex">Index of argument to set</param>
 /// <param name="value">Value of argument</param>
 public void SetKernelArg <T>(string kernel, uint argIndex, T value) where T : struct
 {
     _error = Cl.SetKernelArg(_kernels[kernel], argIndex, value);
     CLException.CheckException(_error);
 }
Exemple #11
0
 /// <summary>
 /// Sets the argument of the kernel's
 /// </summary>
 /// <param name="kernel">Name of the kernel to set the paramter of</param>
 /// <param name="argIndex">Index of argument to set</param>
 /// <param name="value">Value of argument</param>
 public void SetKernelArg <T>(string kernel, uint argIndex, GPUBuffer <T> buffer) where T : struct
 {
     _error = Cl.SetKernelArg(_kernels[kernel], argIndex, buffer.BufferHandle);
     CLException.CheckException(_error);
 }
Exemple #12
0
 /// <summary>
 /// Creates a new GPU Buffer or predefined size
 /// </summary>
 /// <param name="handle">What GPU Instance to create it on</param>
 /// <param name="flags">What Read/Write flags to use</param>
 /// <param name="size">Size of memory of the buffer</param>
 public GPUBuffer(GPUHandle handle, MemFlags flags, int size) : this(handle)
 {
     ErrorCode error;
     _buffer = Cl.CreateBuffer <T>(handle.Context, flags, size, out error);
     CLException.CheckException(error);
 }
Exemple #13
0
 /// <summary>
 /// Clean up memory on GPU
 /// </summary>
 public void Dispose()
 {
     _error = Cl.ReleaseCommandQueue(Queue);
     CLException.CheckException(_error);
 }
Exemple #14
0
 /// <summary>
 /// Waits until the queue is empty
 /// </summary>
 public void WaitUntilDone()
 {
     _error = Cl.Finish(Queue);
     CLException.CheckException(_error);
 }