/// <summary> /// Gets a collection of supported <see cref="ComputeImage3D"/> <see cref="ComputeImageFormat"/>s in a context. /// </summary> /// <param name="context"> The context for which the collection of <see cref="ComputeImageFormat"/>s is queried. </param> /// <param name="flags"> The <c>ComputeMemoryFlags</c> for which the collection of <see cref="ComputeImageFormat"/>s is queried. </param> /// <returns> The collection of the required <see cref="ComputeImageFormat"/>s. </returns> public static ICollection <ComputeImageFormat> GetSupportedFormats(IComputeContext context, ComputeMemoryFlags flags) { return(GetSupportedFormats(context, flags, ComputeMemoryType.Image3D)); }
public void Run(IComputeContext context, TextWriter log) { log.WriteLine(Description); }
/// <summary> /// /// </summary> /// <param name="context"></param> /// <param name="flags"></param> protected ComputeMemory(IComputeContext context, ComputeMemoryFlags flags) { Context = context; Flags = flags; }
private void InternalCreateBuffer(IComputeContext context, ComputeMemoryFlags flags, long count, IntPtr dataPtr) { Handle = CL10.CreateBuffer(context.Handle, flags, new IntPtr(Marshal.SizeOf(typeof(T)) * count), dataPtr, out ComputeErrorCode error); ComputeException.ThrowOnError(error); Init(); }
private ComputeBuffer(CLMemoryHandle handle, IComputeContext context, ComputeMemoryFlags flags) : base(context, flags) { Handle = handle; Init(); }
/// <summary> /// Creates a new buffer from the given pointer. /// </summary> /// <param name="context"> A context used to create the buffer. </param> /// <param name="flags"> A bit-field that is used to specify allocation and usage information about the buffer. </param> /// <param name="count"> The number of elements of the buffer. </param> /// <param name="dataPtr"> A pointer to the data for the buffer. </param> /// <remarks> Note, that if <paramref name="dataPtr"/> does not persist for the life of this buffer, <c>ComputeMemoryFlags.CopyHostPointer</c> should be set in flags to ensure that the underlying buffer remains available. </remarks> public ComputeBuffer(IComputeContext context, ComputeMemoryFlags flags, long count, IntPtr dataPtr) : base(context, flags) { InternalCreateBuffer(context, flags, count, dataPtr); }
/// <summary> /// Creates a new buffer. /// </summary> /// <param name="context"> A context used to create the buffer. </param> /// <param name="flags"> A bit-field that is used to specify allocation and usage information about the buffer. </param> /// <param name="count"> The number of elements of the buffer. </param> public ComputeBuffer(IComputeContext context, ComputeMemoryFlags flags, long count) : this(context, flags, count, IntPtr.Zero) { }
public void Run(IComputeContext context, TextWriter log) { var builder = new OpenCL100Factory(); try { // Create the arrays and fill them with random data. int count = 10; float[] arrA = new float[count]; float[] arrB = new float[count]; float[] arrC = new float[count]; Random rand = new Random(); for (int i = 0; i < count; i++) { arrA[i] = (float)(rand.NextDouble() * 100); arrB[i] = (float)(rand.NextDouble() * 100); } // Create the input buffers and fill them with data from the arrays. // Access modifiers should match those in a kernel. // CopyHostPointer means the buffer should be filled with the data provided in the last argument. var a = new ComputeBuffer <float>(context, ComputeMemoryFlags.ReadOnly | ComputeMemoryFlags.CopyHostPointer, arrA); var b = new ComputeBuffer <float>(context, ComputeMemoryFlags.ReadOnly | ComputeMemoryFlags.CopyHostPointer, arrB); // The output buffer doesn't need any data from the host. Only its size is specified (arrC.Length). var c = new ComputeBuffer <float>(context, ComputeMemoryFlags.WriteOnly, arrC.Length); // Create and build the opencl program. program = builder.BuildComputeProgram(context, clProgramSource); program.Build(null, null, null, IntPtr.Zero); // Create the kernel function and set its arguments. var kernel = builder.CreateKernel(program, "VectorAdd"); kernel.SetMemoryArgument(0, a); kernel.SetMemoryArgument(1, b); kernel.SetMemoryArgument(2, c); // Create the event wait list. An event list is not really needed for this example but it is important to see how it works. // Note that events (like everything else) consume OpenCL resources and creating a lot of them may slow down execution. // For this reason their use should be avoided if possible. var eventList = new List <IComputeEvent>(); // Create the command queue. This is used to control kernel execution and manage read/write/copy operations. var commands = new ComputeCommandQueue(context, context.Devices[0], ComputeCommandQueueFlags.None); // Execute the kernel "count" times. After this call returns, "eventList" will contain an event associated with this command. // If eventList == null or typeof(eventList) == ReadOnlyCollection<ComputeEventBase>, a new event will not be created. commands.Execute(kernel, null, new long[] { count }, null, eventList); // Read back the results. If the command-queue has out-of-order execution enabled (default is off), ReadFromBuffer // will not execute until any previous events in eventList (in our case only eventList[0]) are marked as complete // by OpenCL. By default the command-queue will execute the commands in the same order as they are issued from the host. // eventList will contain two events after this method returns. commands.ReadFromBuffer(c, ref arrC, false, eventList); // A blocking "ReadFromBuffer" (if 3rd argument is true) will wait for itself and any previous commands // in the command queue or eventList to finish execution. Otherwise an explicit wait for all the opencl commands // to finish has to be issued before "arrC" can be used. // This explicit synchronization can be achieved in two ways: // 1) Wait for the events in the list to finish, //eventList.Wait(); // 2) Or simply use commands.Finish(); // Print the results to a log/console. for (int i = 0; i < count; i++) { log.WriteLine("{0} + {1} = {2}", arrA[i], arrB[i], arrC[i]); } } catch (Exception e) { log.WriteLine(e.ToString()); } }
/// <summary> /// /// </summary> /// <param name="context"></param> /// <param name="flags"></param> protected ComputeBufferBase(IComputeContext context, ComputeMemoryFlags flags) : base(context, flags) { }
/// <summary> /// /// </summary> /// <param name="context"></param> /// <param name="flags"></param> protected ComputeImage(IComputeContext context, ComputeMemoryFlags flags) : base(context, flags) { }