/// <summary> /// Enqueues a command to map a part of a <see cref="OpenCLImage"/> into the host address space. /// </summary> /// <param name="image"> The <see cref="OpenCLImage"/> to map. </param> /// <param name="blocking"> The mode of operation of this command. If <c>true</c> this call will not return until the command has finished execution. </param> /// <param name="flags"> A list of properties for the mapping mode. </param> /// <param name="offset"> The <paramref name="image"/> element position where mapping starts. </param> /// <param name="region"> The region of elements to map. </param> /// <param name="events"> A collection of events that need to complete before this particular command can be executed. If <paramref name="events"/> is not <c>null</c> or read-only a new <see cref="OpenCLEvent"/> identifying this command is created and attached to the end of the collection. </param> /// <remarks> If <paramref name="blocking"/> is <c>true</c> this method will not return until the command completes. If <paramref name="blocking"/> is <c>false</c> this method will return immediately after the command is enqueued. </remarks> public IntPtr Map(OpenCLImage image, bool blocking, OpenCLMemoryMappingFlags flags, SysIntX3 offset, SysIntX3 region, IReadOnlyList <OpenCLEventBase> events = null, IList <OpenCLEventBase> newEvents = null) { int eventWaitListSize; CLEventHandle[] eventHandles = OpenCLTools.ExtractHandles(events, out eventWaitListSize); CLEventHandle[] newEventHandle = (newEvents != null) ? new CLEventHandle[1] : null; IntPtr mappedPtr, rowPitch, slicePitch; OpenCLErrorCode error = OpenCLErrorCode.Success; mappedPtr = CL10.EnqueueMapImage(Handle, image.Handle, blocking, flags, ref offset, ref region, out rowPitch, out slicePitch, eventWaitListSize, eventHandles, newEventHandle, out error); OpenCLException.ThrowOnError(error); if (newEvents != null) { lock (newEvents) { newEvents.Add(new OpenCLEvent(newEventHandle[0], this)); } } return(mappedPtr); }
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"); }
static ComputePlatform() { lock (typeof(ComputePlatform)) { try { if (Platforms != null) { return; } CLPlatformHandle[] handles; var error = CL10.GetPlatformIDs(0, null, out int handlesLength); ComputeException.ThrowOnError(error); handles = new CLPlatformHandle[handlesLength]; error = CL10.GetPlatformIDs(handlesLength, handles, out handlesLength); ComputeException.ThrowOnError(error); var platformList = new List <ComputePlatform>(handlesLength); foreach (CLPlatformHandle handle in handles) { platformList.Add(new ComputePlatform(handle)); } Platforms = platformList.AsReadOnly(); } catch (DllNotFoundException) { Platforms = new List <ComputePlatform>().AsReadOnly(); } } }
private ReadOnlyCollection <byte[]> GetBinaries() { IntPtr[] binaryLengths = GetArrayInfo <CLProgramHandle, ComputeProgramInfo, IntPtr>(Handle, ComputeProgramInfo.BinarySizes, CL10.GetProgramInfo); GCHandle[] binariesGCHandles = new GCHandle[binaryLengths.Length]; IntPtr[] binariesPtrs = new IntPtr[binaryLengths.Length]; IList <byte[]> binaries = new List <byte[]>(); GCHandle binariesPtrsGCHandle = GCHandle.Alloc(binariesPtrs, GCHandleType.Pinned); try { for (int i = 0; i < binaryLengths.Length; i++) { byte[] binary = new byte[binaryLengths[i].ToInt64()]; binariesGCHandles[i] = GCHandle.Alloc(binary, GCHandleType.Pinned); binariesPtrs[i] = binariesGCHandles[i].AddrOfPinnedObject(); binaries.Add(binary); } IntPtr sizeRet; ComputeErrorCode error = CL10.GetProgramInfo(Handle, ComputeProgramInfo.Binaries, new IntPtr(binariesPtrs.Length * IntPtr.Size), binariesPtrsGCHandle.AddrOfPinnedObject(), out sizeRet); ComputeException.ThrowOnError(error); } finally { for (int i = 0; i < binaryLengths.Length; i++) { binariesGCHandles[i].Free(); } binariesPtrsGCHandle.Free(); } return(new ReadOnlyCollection <byte[]>(binaries)); }
/// <summary> /// Creates a new buffer from an existing OpenGL buffer object. /// </summary> /// <typeparam name="DataType"> The type of the elements of the buffer. <typeparamref name="T"/> should match the type of the elements in the OpenGL buffer. </typeparam> /// <param name="context"> A context with enabled CL/GL sharing. </param> /// <param name="flags"> A bit-field that is used to specify usage information about the buffer. Only <see cref="ComputeMemoryFlags.ReadOnly"/>, <see cref="ComputeMemoryFlags.WriteOnly"/> and <see cref="ComputeMemoryFlags.ReadWrite"/> are allowed. </param> /// <param name="bufferId"> The OpenGL buffer object id to use for the creation of the buffer. </param> /// <returns> The created buffer. </returns> public static ComputeBuffer <DataType> CreateFromGLBuffer <DataType>(IComputeContext context, ComputeMemoryFlags flags, int bufferId) where DataType : struct { var handle = CL10.CreateFromGLBuffer(context.Handle, flags, bufferId, out ComputeErrorCode error); ComputeException.ThrowOnError(error); return(new ComputeBuffer <DataType>(handle, context, flags)); }
static OpenCLPlatform() { try { if (platforms != null) { return; } CLPlatformHandle[] handles; int handlesLength; OpenCLErrorCode error = CL10.GetPlatformIDs(0, null, out handlesLength); OpenCLException.ThrowOnError(error); handles = new CLPlatformHandle[handlesLength]; error = CL10.GetPlatformIDs(handlesLength, handles, out handlesLength); OpenCLException.ThrowOnError(error); List <OpenCLPlatform> platformList = new List <OpenCLPlatform>(handlesLength); foreach (CLPlatformHandle handle in handles) { platformList.Add(new OpenCLPlatform(handle)); } platforms = platformList.AsReadOnly(); } catch (DllNotFoundException) { platforms = new List <OpenCLPlatform>().AsReadOnly(); } }
/// <summary> /// Enqueues a command to map a part of a buffer into the host address space. /// </summary> /// <param name="buffer"> The buffer to map. </param> /// <param name="blocking"> The mode of operation of this call. </param> /// <param name="flags"> A list of properties for the mapping mode. </param> /// <param name="offset"> The <paramref name="buffer"/> element position where mapping starts. </param> /// <param name="region"> The region of elements to map. </param> /// <param name="events"> A collection of events that need to complete before this particular command can be executed. If <paramref name="events"/> is not <c>null</c> or read-only a new <see cref="ComputeEvent"/> identifying this command is created and attached to the end of the collection. </param> /// <remarks> If <paramref name="blocking"/> is <c>true</c> this method will not return until the command completes. If <paramref name="blocking"/> is <c>false</c> this method will return immediately after the command is enqueued. </remarks> public IntPtr Map <T>(ComputeBufferBase <T> buffer, bool blocking, ComputeMemoryMappingFlags flags, long offset, long region, ICollection <ComputeEventBase> events) where T : struct { int sizeofT = HDSPUtils.SizeOf(typeof(T)); int eventWaitListSize; CLEventHandle[] eventHandles = ComputeTools.ExtractHandles(events, out eventWaitListSize); bool eventsWritable = (events != null && !events.IsReadOnly); CLEventHandle[] newEventHandle = (eventsWritable) ? new CLEventHandle[1] : null; IntPtr mappedPtr = IntPtr.Zero; ComputeErrorCode error = ComputeErrorCode.Success; mappedPtr = CL10.EnqueueMapBuffer(Handle, buffer.Handle, blocking, flags, new IntPtr(offset * sizeofT), new IntPtr(region * sizeofT), eventWaitListSize, eventHandles, newEventHandle, out error); ComputeException.ThrowOnError(error); if (eventsWritable) { events.Add(new ComputeEvent(newEventHandle[0], this)); } return(mappedPtr); }
/// <summary> /// Enqueues a command to map a part of a buffer into the host address space. /// </summary> /// <param name="buffer"> The buffer to map. </param> /// <param name="blocking"> The mode of operation of this call. </param> /// <param name="flags"> A list of properties for the mapping mode. </param> /// <param name="offset"> The <paramref name="buffer"/> element position where mapping starts. </param> /// <param name="region"> The region of elements to map. </param> /// <param name="events"> A collection of events that need to complete before this particular command can be executed. If <paramref name="events"/> is not <c>null</c> or read-only a new <see cref="OpenCLEvent"/> identifying this command is created and attached to the end of the collection. </param> /// <remarks> If <paramref name="blocking"/> is <c>true</c> this method will not return until the command completes. If <paramref name="blocking"/> is <c>false</c> this method will return immediately after the command is enqueued. </remarks> public IntPtr Map(OpenCLBufferBase buffer, bool blocking, OpenCLMemoryMappingFlags flags, long offset, long region, IReadOnlyList <OpenCLEventBase> events = null, IList <OpenCLEventBase> newEvents = null) { int sizeofT = Marshal.SizeOf(buffer.ElementType); int eventWaitListSize; CLEventHandle[] eventHandles = OpenCLTools.ExtractHandles(events, out eventWaitListSize); CLEventHandle[] newEventHandle = (newEvents != null) ? new CLEventHandle[1] : null; IntPtr mappedPtr = IntPtr.Zero; OpenCLErrorCode error = OpenCLErrorCode.Success; mappedPtr = CL10.EnqueueMapBuffer(Handle, buffer.Handle, blocking, flags, new IntPtr(offset * sizeofT), new IntPtr(region * sizeofT), eventWaitListSize, eventHandles, newEventHandle, out error); OpenCLException.ThrowOnError(error); if (newEvents != null) { lock (newEvents) { newEvents.Add(new OpenCLEvent(newEventHandle[0], this)); } } return(mappedPtr); }
public ComputeContext(ICollection <ComputeDevice> devices, ComputeContextPropertyList properties, ComputeContextNotifier notify, IntPtr notifyDataPtr) { int handleCount; CLDeviceHandle[] deviceHandles = ComputeTools.ExtractHandles(devices, out handleCount); IntPtr[] propertyArray = (properties != null) ? properties.ToIntPtrArray() : null; callback = notify; ComputeErrorCode error = ComputeErrorCode.Success; Handle = CL10.CreateContext(propertyArray, handleCount, deviceHandles, notify, notifyDataPtr, out error); ComputeException.ThrowOnError(error); SetID(Handle.Value); this.properties = properties; ComputeContextProperty platformProperty = properties.GetByName(ComputeContextPropertyName.Platform); this.platform = ComputePlatform.GetByHandle(platformProperty.Value); this.devices = GetDevices(); #if DEBUG Trace.WriteLine("Create " + this + " in Thread(" + Thread.CurrentThread.ManagedThreadId + ").", "Information"); #endif }
static ComputePlatform() { if (Platforms != null) { return; } lock (lockObj) { try { CL10.GetPlatformIDsWrapper(0, null, out int handlesLength); CLPlatformHandle[] handles = new CLPlatformHandle[handlesLength]; CL10.GetPlatformIDsWrapper(handlesLength, handles, out handlesLength); var platformList = new List <ComputePlatform>(handlesLength); foreach (CLPlatformHandle handle in handles) { platformList.Add(new ComputePlatform(handle)); } Platforms = platformList.AsReadOnly(); } catch (DllNotFoundException) { Platforms = new List <ComputePlatform>().AsReadOnly(); } } }
/// <summary> /// Enqueues a marker. /// </summary> public OpenCLEvent AddMarker() { CLEventHandle newEventHandle; OpenCLErrorCode error = CL10.EnqueueMarker(Handle, out newEventHandle); OpenCLException.ThrowOnError(error); return(new OpenCLEvent(newEventHandle, this)); }
/// <summary> /// Enqueues a marker. /// </summary> public ComputeEvent AddMarker() { CLEventHandle newEventHandle; ComputeErrorCode error = CL10.EnqueueMarker(Handle, out newEventHandle); ComputeException.ThrowOnError(error); return(new ComputeEvent(newEventHandle, this)); }
/// <summary> /// Waits on the host thread for the specified events to complete. /// </summary> /// <param name="events"> The events to be waited for completition. </param> public static void Wait(List <OpenCLEventBase> events) { int eventWaitListSize; CLEventHandle[] eventHandles = OpenCLTools.ExtractHandles(events, out eventWaitListSize); OpenCLErrorCode error = CL10.WaitForEvents(eventWaitListSize, eventHandles); OpenCLException.ThrowOnError(error); }
/// <summary> /// Waits on the host thread for the specified events to complete. /// </summary> /// <param name="events"> The events to be waited for completition. </param> public static void Wait(ICollection <ComputeEventBase> events) { int eventWaitListSize; CLEventHandle[] eventHandles = ComputeTools.ExtractHandles(events, out eventWaitListSize); ComputeErrorCode error = CL10.WaitForEvents(eventWaitListSize, eventHandles); ComputeException.ThrowOnError(error); }
/// <summary> /// Creates a new <see cref="OpenCLImage2D"/> from an OpenGL 2D texture object. /// </summary> /// <param name="context"> A <see cref="OpenCLContext"/> with enabled CL/GL sharing. </param> /// <param name="flags"> A bit-field that is used to specify usage information about the <see cref="OpenCLImage2D"/>. Only <c>OpenCLMemoryFlags.ReadOnly</c>, <c>OpenCLMemoryFlags.WriteOnly</c> and <c>OpenCLMemoryFlags.ReadWrite</c> are allowed. </param> /// <param name="textureTarget"> One of the following values: GL_TEXTURE_2D, GL_TEXTURE_CUBE_MAP_POSITIVE_X, GL_TEXTURE_CUBE_MAP_POSITIVE_Y, GL_TEXTURE_CUBE_MAP_POSITIVE_Z, GL_TEXTURE_CUBE_MAP_NEGATIVE_X, GL_TEXTURE_CUBE_MAP_NEGATIVE_Y, GL_TEXTURE_CUBE_MAP_NEGATIVE_Z, or GL_TEXTURE_RECTANGLE. Using GL_TEXTURE_RECTANGLE for texture_target requires OpenGL 3.1. Alternatively, GL_TEXTURE_RECTANGLE_ARB may be specified if the OpenGL extension GL_ARB_texture_rectangle is supported. </param> /// <param name="mipLevel"> The mipmap level of the OpenGL 2D texture object to be used. </param> /// <param name="textureId"> The OpenGL 2D texture object id to use. </param> /// <returns> The created <see cref="OpenCLImage2D"/>. </returns> public static OpenCLImage2D CreateFromGLTexture2D(OpenCLContext context, OpenCLMemoryFlags flags, int textureTarget, int mipLevel, int textureId) { OpenCLErrorCode error = OpenCLErrorCode.Success; CLMemoryHandle image = CL10.CreateFromGLTexture2D(context.Handle, flags, textureTarget, mipLevel, textureId, out error); OpenCLException.ThrowOnError(error); return(new OpenCLImage2D(image, context, flags)); }
/// <summary> /// Creates a new <see cref="ComputeImage2D"/> from an OpenGL renderbuffer object. /// </summary> /// <param name="context"> A context with enabled CL/GL sharing. </param> /// <param name="flags"> A bit-field that is used to specify usage information about the <see cref="ComputeImage2D"/>. Only <c>ComputeMemoryFlags.ReadOnly</c>, <c>ComputeMemoryFlags.WriteOnly</c> and <c>ComputeMemoryFlags.ReadWrite</c> are allowed. </param> /// <param name="renderbufferId"> The OpenGL renderbuffer object id to use. </param> /// <returns> The created <see cref="ComputeImage2D"/>. </returns> public static ComputeImage2D CreateFromGLRenderbuffer(IComputeContext context, ComputeMemoryFlags flags, int renderbufferId) { ComputeErrorCode error = ComputeErrorCode.Success; CLMemoryHandle image = CL10.CreateFromGLRenderbuffer(context.Handle, flags, renderbufferId, out error); ComputeException.ThrowOnError(error); return(new ComputeImage2D(image, context, flags)); }
/// <summary> /// Creates a new <see cref="ComputeImage2D"/> from an OpenGL 2D texture object. /// </summary> /// <param name="context"> A context with enabled CL/GL sharing. </param> /// <param name="flags"> A bit-field that is used to specify usage information about the <see cref="ComputeImage2D"/>. Only <c>ComputeMemoryFlags.ReadOnly</c>, <c>ComputeMemoryFlags.WriteOnly</c> and <c>ComputeMemoryFlags.ReadWrite</c> are allowed. </param> /// <param name="textureTarget"> One of the following values: GL_TEXTURE_2D, GL_TEXTURE_CUBE_MAP_POSITIVE_X, GL_TEXTURE_CUBE_MAP_POSITIVE_Y, GL_TEXTURE_CUBE_MAP_POSITIVE_Z, GL_TEXTURE_CUBE_MAP_NEGATIVE_X, GL_TEXTURE_CUBE_MAP_NEGATIVE_Y, GL_TEXTURE_CUBE_MAP_NEGATIVE_Z, or GL_TEXTURE_RECTANGLE. Using GL_TEXTURE_RECTANGLE for texture_target requires OpenGL 3.1. Alternatively, GL_TEXTURE_RECTANGLE_ARB may be specified if the OpenGL extension GL_ARB_texture_rectangle is supported. </param> /// <param name="mipLevel"> The mipmap level of the OpenGL 2D texture object to be used. </param> /// <param name="textureId"> The OpenGL 2D texture object id to use. </param> /// <returns> The created <see cref="ComputeImage2D"/>. </returns> public static ComputeImage2D CreateFromGLTexture2D(IComputeContext context, ComputeMemoryFlags flags, int textureTarget, int mipLevel, int textureId) { ComputeErrorCode error = ComputeErrorCode.Success; CLMemoryHandle image = CL10.CreateFromGLTexture2D(context.Handle, flags, textureTarget, mipLevel, textureId, out error); ComputeException.ThrowOnError(error); return(new ComputeImage2D(image, context, flags)); }
/// <summary> /// Creates a new <see cref="ComputeBuffer{T}"/>. /// </summary> /// <param name="context"> A <see cref="ComputeContext"/> used to create the <see cref="ComputeBuffer{T}"/>. </param> /// <param name="flags"> A bit-field that is used to specify allocation and usage information about the <see cref="ComputeBuffer{T}"/>. </param> /// <param name="count"> The number of elements of the <see cref="ComputeBuffer{T}"/>. </param> /// <param name="dataPtr"> A pointer to the data for the <see cref="ComputeBuffer{T}"/>. </param> public ComputeBuffer(ComputeContext context, ComputeMemoryFlags flags, long count, IntPtr dataPtr) : base(context, flags) { ComputeErrorCode error = ComputeErrorCode.Success; Handle = CL10.CreateBuffer(context.Handle, flags, new IntPtr(Marshal.SizeOf(typeof(T)) * count), dataPtr, out error); ComputeException.ThrowOnError(error); Init(); }
/// <summary> /// Releases the associated OpenCL object. /// </summary> /// <param name="manual"> Specifies the operation mode of this method. </param> /// <remarks> <paramref name="manual"/> must be <c>true</c> if this method is invoked directly by the application. </remarks> protected override void Dispose(bool manual) { if (Handle.IsValid) { logger.Info("Dispose " + this + " in Thread(" + Thread.CurrentThread.ManagedThreadId + ").", "Information"); CL10.ReleaseEvent(Handle); Handle.Invalidate(); } }
/// <summary> /// Releases the associated OpenCL object. /// </summary> /// <param name="manual"> Specifies the operation mode of this method. </param> /// <remarks> <paramref name="manual"/> must be <c>true</c> if this method is invoked directly by the application. </remarks> protected override void Dispose(bool manual) { if (Handle.IsValid) { //Console.WriteLine("Dispose " + this + " in Thread(" + Thread.CurrentThread.ManagedThreadId + ").", "Information"); CL10.ReleaseProgram(Handle); Handle.Invalidate(); } }
/// <summary> /// Creates a new <see cref="OpenCLImage2D"/> from an OpenGL renderbuffer object. /// </summary> /// <param name="context"> A <see cref="OpenCLContext"/> with enabled CL/GL sharing. </param> /// <param name="flags"> A bit-field that is used to specify usage information about the <see cref="OpenCLImage2D"/>. Only <c>OpenCLMemoryFlags.ReadOnly</c>, <c>OpenCLMemoryFlags.WriteOnly</c> and <c>OpenCLMemoryFlags.ReadWrite</c> are allowed. </param> /// <param name="renderbufferId"> The OpenGL renderbuffer object id to use. </param> /// <returns> The created <see cref="OpenCLImage2D"/>. </returns> public static OpenCLImage2D CreateFromGLRenderbuffer(OpenCLContext context, OpenCLMemoryFlags flags, int renderbufferId) { OpenCLErrorCode error = OpenCLErrorCode.Success; CLMemoryHandle image = CL10.CreateFromGLRenderbuffer(context.Handle, flags, renderbufferId, out error); OpenCLException.ThrowOnError(error); return(new OpenCLImage2D(image, context, flags)); }
/* * /// <summary> * /// Creates a new <see cref="ComputeImage2D"/> from a <c>Bitmap</c>. * /// </summary> * /// <param name="context"> A valid <see cref="ComputeContext"/> in which the <see cref="ComputeImage2D"/> is created. </param> * /// <param name="flags"> A bit-field that is used to specify allocation and usage information about the <see cref="ComputeImage2D"/>. </param> * /// <param name="bitmap"> The bitmap to use. </param> * /// <remarks> Note that only bitmaps with <c>Format32bppArgb</c> pixel format are currently supported. </remarks> * public ComputeImage2D(ComputeContext context, ComputeMemoryFlags flags, Bitmap bitmap) * :base(context, flags) * { * unsafe * { * if(bitmap.PixelFormat != PixelFormat.Format32bppArgb) * throw new ArgumentException("Pixel format not supported."); * * //ComputeImageFormat format = Tools.ConvertImageFormat(bitmap.PixelFormat); * ComputeImageFormat format = new ComputeImageFormat(ComputeImageChannelOrder.Bgra, ComputeImageChannelType.UnsignedInt8); * BitmapData bitmapData = bitmap.LockBits(new Rectangle(new Point(), bitmap.Size), ImageLockMode.ReadOnly, bitmap.PixelFormat); * * try * { * ComputeErrorCode error = ComputeErrorCode.Success; * Handle = CL10.CreateImage2D( * context.Handle, * flags, * &format, * new IntPtr(bitmap.Width), * new IntPtr(bitmap.Height), * new IntPtr(bitmapData.Stride), * bitmapData.Scan0, * &error); * ComputeException.ThrowOnError(error); * } * finally * { * bitmap.UnlockBits(bitmapData); * } * * Init(); * } * }*/ /// <summary> /// Creates a new <see cref="ComputeImage2D"/>. /// </summary> /// <param name="context"> A valid <see cref="ComputeContext"/> in which the <see cref="ComputeImage2D"/> is created. </param> /// <param name="flags"> A bit-field that is used to specify allocation and usage information about the <see cref="ComputeImage2D"/>. </param> /// <param name="format"> A structure that describes the format properties of the <see cref="ComputeImage2D"/>. </param> /// <param name="width"> The width of the <see cref="ComputeImage2D"/> in pixels. </param> /// <param name="height"> The height of the <see cref="ComputeImage2D"/> in pixels. </param> /// <param name="rowPitch"> The size in bytes of each row of elements of the <see cref="ComputeImage2D"/>. If <paramref name="rowPitch"/> is zero, OpenCL will compute the proper value based on <see cref="ComputeImage.Width"/> and <see cref="ComputeImage.ElementSize"/>. </param> /// <param name="data"> The data to initialize the <see cref="ComputeImage2D"/>. Can be <c>IntPtr.Zero</c>. </param> public ComputeImage2D(IComputeContext context, ComputeMemoryFlags flags, ComputeImageFormat format, int width, int height, long rowPitch, IntPtr data) : base(context, flags) { ComputeErrorCode error = ComputeErrorCode.Success; Handle = CL10.CreateImage2D(context.Handle, flags, ref format, new IntPtr(width), new IntPtr(height), new IntPtr(rowPitch), data, out error); ComputeException.ThrowOnError(error); Init(); }
/// <summary> /// Creates a new <see cref="OpenCLImage3D"/>. /// </summary> /// <param name="context"> A valid <see cref="OpenCLContext"/> in which the <see cref="OpenCLImage3D"/> is created. </param> /// <param name="flags"> A bit-field that is used to specify allocation and usage information about the <see cref="OpenCLImage3D"/>. </param> /// <param name="format"> A structure that describes the format properties of the <see cref="OpenCLImage3D"/>. </param> /// <param name="width"> The width of the <see cref="OpenCLImage3D"/> in pixels. </param> /// <param name="height"> The height of the <see cref="OpenCLImage3D"/> in pixels. </param> /// <param name="depth"> The depth of the <see cref="OpenCLImage3D"/> in pixels. </param> /// <param name="rowPitch"> The size in bytes of each row of elements of the <see cref="OpenCLImage3D"/>. If <paramref name="rowPitch"/> is zero, OpenCL will compute the proper value based on <see cref="OpenCLImage.Width"/> and <see cref="OpenCLImage.ElementSize"/>. </param> /// <param name="slicePitch"> The size in bytes of each 2D slice in the <see cref="OpenCLImage3D"/>. If <paramref name="slicePitch"/> is zero, OpenCL will compute the proper value based on <see cref="OpenCLImage.RowPitch"/> and <see cref="OpenCLImage.Height"/>. </param> /// <param name="data"> The data to initialize the <see cref="OpenCLImage3D"/>. Can be <c>IntPtr.Zero</c>. </param> public OpenCLImage3D(OpenCLContext context, OpenCLMemoryFlags flags, OpenCLImageFormat format, int width, int height, int depth, long rowPitch, long slicePitch, IntPtr data) : base(context, flags) { OpenCLErrorCode error = OpenCLErrorCode.Success; Handle = CL10.CreateImage3D(context.Handle, flags, ref format, new IntPtr(width), new IntPtr(height), new IntPtr(depth), new IntPtr(rowPitch), new IntPtr(slicePitch), data, out error); OpenCLException.ThrowOnError(error); Init(); }
protected override void Dispose(bool manual) { if (Handle.IsValid) { #if DEBUG Trace.WriteLine("Dispose " + this + " in Thread(" + Thread.CurrentThread.ManagedThreadId + ").", "Information"); #endif CL10.ReleaseKernel(Handle); Handle.Invalidate(); } }
/// <summary> /// Builds (compiles and links) a program executable from the program source or binary for all or some of the <see cref="OpenCLProgram.Devices"/>. /// </summary> /// <param name="devices"> A subset or all of <see cref="OpenCLProgram.Devices"/>. If <paramref name="devices"/> is <c>null</c>, the executable is built for every item of <see cref="OpenCLProgram.Devices"/> for which a source or a binary has been loaded. </param> /// <param name="options"> A set of options for the OpenCL compiler. </param> /// <param name="notify"> A delegate instance that represents a reference to a notification routine. This routine is a callback function that an application can register and which will be called when the program executable has been built (successfully or unsuccessfully). If <paramref name="notify"/> is not <c>null</c>, <see cref="OpenCLProgram.Build"/> does not need to wait for the build to complete and can return immediately. If <paramref name="notify"/> is <c>null</c>, <see cref="OpenCLProgram.Build"/> does not return until the build has completed. The callback function may be called asynchronously by the OpenCL implementation. It is the application's responsibility to ensure that the callback function is thread-safe and that the delegate instance doesn't get collected by the Garbage Collector until the build operation triggers the callback. </param> /// <param name="notifyDataPtr"> Optional user data that will be passed to <paramref name="notify"/>. </param> public void Build(IList <OpenCLDevice> devices, string options, OpenCLProgramBuildNotifier notify, IntPtr notifyDataPtr) { int handleCount; CLDeviceHandle[] deviceHandles = OpenCLTools.ExtractHandles(devices, out handleCount); buildOptions = (options != null) ? options : ""; buildNotify = notify; OpenCLErrorCode error = CL10.BuildProgram(Handle, handleCount, deviceHandles, options, buildNotify, notifyDataPtr); OpenCLException.ThrowOnError(error); }
private void SetupAndCount(CLMemoryHandle input, int bitOffset) { ComputeErrorCode error; IntPtr agentPtrSize = (IntPtr)0; agentPtrSize = (IntPtr)Marshal.SizeOf(typeof(IntPtr)); var ptrSize = (IntPtr)Marshal.SizeOf(typeof(Mem)); int globalWorkSize = gpuConstants.numThreadsPerBlock * gpuConstants.numBlocks; int localWorkSize = gpuConstants.numThreadsPerBlock; IntPtr[] workGroupSizePtr = new IntPtr[] { (IntPtr)globalWorkSize }; IntPtr[] localWorkGroupSizePtr = new IntPtr[] { (IntPtr)localWorkSize }; ComputeEvent clevent; error = CL10.SetKernelArg(ckSetupAndCount, 0, ptrSize, input); CheckErr(error, "CL10.SetKernelArg"); error = CL10.SetKernelArg(ckSetupAndCount, 1, ptrSize, mCounters); CheckErr(error, "CL10.SetKernelArg"); //if(DEBUG_CONSOLE_OUTPUT) Console.WriteLine((Marshal.SizeOf(typeof(GPUConstants)))); error = CL10.SetKernelArg(ckSetupAndCount, 2, (IntPtr)(Marshal.SizeOf(typeof(GPUConstants))), gpuConstants); CheckErr(error, "CL10.SetKernelArg"); error = CL10.SetKernelArg(ckSetupAndCount, 3, (IntPtr)4, bitOffset); CheckErr(error, "CL10.SetKernelArg"); error = CL10.EnqueueNDRangeKernel(cqCommandQueue, ckSetupAndCount, 1, null, workGroupSizePtr, localWorkGroupSizePtr, 0, null, out clevent); CheckErr(error, "CL10.EnqueueNDRangeKernel"); error = CL10.Finish(cqCommandQueue); CheckErr(error, "CL10.Finish"); if (DEBUG) { ComputeEvent eve; CL10.EnqueueReadBuffer(cqCommandQueue, input, Bool.True, IntPtr.Zero, (IntPtr)(gpuConstants.numTotalElements * 4), debugRead, 0, null, out eve); CheckErr(error, "CL10.EnqueueReadBuffer"); PrintElementBuffer(debugRead, gpuConstants.numTotalElements, "Setup and Count -> Input -> bitoffset = " + bitOffset); CL10.EnqueueReadBuffer(cqCommandQueue, mCounters, Bool.True, IntPtr.Zero, (IntPtr)(numCounters * sizeof(int)), debugRead, 0, null, out eve); CheckErr(error, "CL10.EnqueueReadBuffer"); PrintCounterBuffer(debugRead, "Setup and Count -> bitoffset = " + bitOffset); if (DEBUG_CONSOLE_OUTPUT) { Console.WriteLine("Setup and Count -> bitoffset = " + bitOffset); } if (DEBUG_CONSOLE_OUTPUT) { Console.WriteLine(); } } }
/// <summary> /// Builds (compiles and links) a program executable from the program source or binary for all or some of the <see cref="ComputeProgram.Devices"/>. /// </summary> /// <param name="devices"> A subset or all of <see cref="ComputeProgram.Devices"/>. If <paramref name="devices"/> is <c>null</c>, the executable is built for every item of <see cref="ComputeProgram.Devices"/> for which a source or a binary has been loaded. </param> /// <param name="options"> A set of options for the OpenCL compiler. </param> /// <param name="notify"> A delegate instance that represents a reference to a notification routine. This routine is a callback function that an application can register and which will be called when the program executable has been built (successfully or unsuccessfully). If <paramref name="notify"/> is not <c>null</c>, <see cref="ComputeProgram.Build"/> does not need to wait for the build to complete and can return immediately. If <paramref name="notify"/> is <c>null</c>, <see cref="ComputeProgram.Build"/> does not return until the build has completed. The callback function may be called asynchronously by the OpenCL implementation. It is the application's responsibility to ensure that the callback function is thread-safe and that the delegate instance doesn't get collected by the Garbage Collector until the build operation triggers the callback. </param> /// <param name="notifyDataPtr"> Optional user data that will be passed to <paramref name="notify"/>. </param> public void Build(ICollection <ComputeDevice> devices, string options, ComputeProgramBuildNotifier notify, IntPtr notifyDataPtr) { int handleCount; CLDeviceHandle[] deviceHandles = ComputeTools.ExtractHandles(devices, out handleCount); buildOptions = (options != null) ? options : ""; buildNotify = notify; ComputeErrorCode error = CL10.BuildProgram(Handle, handleCount, deviceHandles, options, buildNotify, notifyDataPtr); ComputeException.ThrowOnError(error); }
/// <summary> /// /// </summary> /// <param name="context"></param> /// <param name="flags"></param> /// <param name="type"></param> /// <returns></returns> protected static ICollection <ComputeImageFormat> GetSupportedFormats(ComputeContext context, ComputeMemoryFlags flags, ComputeMemoryType type) { int formatCountRet = 0; ComputeErrorCode error = CL10.GetSupportedImageFormats(context.Handle, flags, type, 0, null, out formatCountRet); ComputeException.ThrowOnError(error); ComputeImageFormat[] formats = new ComputeImageFormat[formatCountRet]; error = CL10.GetSupportedImageFormats(context.Handle, flags, type, formatCountRet, formats, out formatCountRet); ComputeException.ThrowOnError(error); return(new Collection <ComputeImageFormat>(formats)); }
/// <summary> /// Creates a new <see cref="ComputeProgram"/> from a specified list of binaries. /// </summary> /// <param name="context"> A <see cref="ComputeContext"/>. </param> /// <param name="binaries"> A list of binaries, one for each item in <paramref name="devices"/>. </param> /// <param name="devices"> A subset of the <see cref="ComputeContext.Devices"/>. If <paramref name="devices"/> is <c>null</c>, OpenCL will associate every binary from <see cref="ComputeProgram.Binaries"/> with a corresponding <see cref="ComputeDevice"/> from <see cref="ComputeContext.Devices"/>. </param> public ComputeProgram(ComputeContext context, IList <byte[]> binaries, IList <ComputeDevice> devices) { int count; CLDeviceHandle[] deviceHandles = (devices != null) ? ComputeTools.ExtractHandles(devices, out count) : ComputeTools.ExtractHandles(context.Devices, out count); IntPtr[] binariesPtrs = new IntPtr[count]; IntPtr[] binariesLengths = new IntPtr[count]; int[] binariesStats = new int[count]; ComputeErrorCode error = ComputeErrorCode.Success; GCHandle[] binariesGCHandles = new GCHandle[count]; try { for (int i = 0; i < count; i++) { binariesGCHandles[i] = GCHandle.Alloc(binaries[i], GCHandleType.Pinned); binariesPtrs[i] = binariesGCHandles[i].AddrOfPinnedObject(); binariesLengths[i] = new IntPtr(binaries[i].Length); } Handle = CL10.CreateProgramWithBinary( context.Handle, count, deviceHandles, binariesLengths, binariesPtrs, binariesStats, out error); ComputeException.ThrowOnError(error); } finally { for (int i = 0; i < count; i++) { binariesGCHandles[i].Free(); } } this.binaries = new ReadOnlyCollection <byte[]>(binaries); this.context = context; this.devices = new ReadOnlyCollection <ComputeDevice>( (devices != null) ? devices : context.Devices); //Console.WriteLine("Create " + this + " in Thread(" + Thread.CurrentThread.ManagedThreadId + ").", "Information"); }
/// <summary> /// Creates a new <see cref="ComputeProgram"/> from a source code string. /// </summary> /// <param name="context"> A <see cref="ComputeContext"/>. </param> /// <param name="source"> The source code for the <see cref="ComputeProgram"/>. </param> /// <remarks> The created <see cref="ComputeProgram"/> is associated with the <see cref="ComputeContext.Devices"/>. </remarks> public ComputeProgram(ComputeContext context, string source) { ComputeErrorCode error = ComputeErrorCode.Success; Handle = CL10.CreateProgramWithSource(context.Handle, 1, new string[] { source }, null, out error); ComputeException.ThrowOnError(error); SetID(Handle.Value); this.context = context; this.devices = context.Devices; this.source = new ReadOnlyCollection <string>(new string[] { source }); //Console.WriteLine("Create " + this + " in Thread(" + Thread.CurrentThread.ManagedThreadId + ").", "Information"); }