Exemple #1
0
 public static unsafe extern IntPtr CreateContext(
     IntPtr* properties,
     Int32 num_devices,
     IntPtr* devices,
     /* void (*pfn_notify)(const char *, const IntPtr, IntPtr, IntPtr) */ IntPtr pfn_notify,
     /* void* */ IntPtr user_data,
     ComputeErrorCode* errcode_ret);
Exemple #2
0
 public extern static CLContextHandle CreateContext(
     [MarshalAs(UnmanagedType.LPArray)] IntPtr[] properties,
     Int32 num_devices,
     [MarshalAs(UnmanagedType.LPArray)] CLDeviceHandle[] devices,
     ComputeContextNotifier pfn_notify,
     IntPtr user_data,
     out ComputeErrorCode errcode_ret);
Exemple #3
0
        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
        }
        /// <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>
        /// 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)
        {
            CLEventHandle[]  eventHandles = ComputeTools.ExtractHandles(events, out var eventWaitListSize);
            ComputeErrorCode error        = CL12.WaitForEvents(eventWaitListSize, eventHandles);

            ComputeException.ThrowOnError(error);
        }
        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));
        }
Exemple #7
0
        /// <summary>
        ///
        /// </summary>
        protected void HookNotifier()
        {
            statusNotify = new ComputeEventCallback(StatusNotify);
            ComputeErrorCode error = CLInterface.CL11.SetEventCallback(Handle, (int)ComputeCommandExecutionStatus.Complete, statusNotify, IntPtr.Zero);

            ComputeException.ThrowOnError(error);
        }
Exemple #8
0
        static ComputePlatform()
        {
            lock (typeof(ComputePlatform))
            {
                try
                {
                    if (platforms != null)
                    {
                        return;
                    }

                    CLPlatformHandle[] handles;
                    int handlesLength;
                    ComputeErrorCode error = CL10.GetPlatformIDs(0, null, out handlesLength);
                    ComputeException.ThrowOnError(error);
                    handles = new CLPlatformHandle[handlesLength];

                    error = CL10.GetPlatformIDs(handlesLength, handles, out handlesLength);
                    ComputeException.ThrowOnError(error);

                    List <ComputePlatform> 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();
                }
            }
        }
 public extern static CLMemoryHandle CreateFromGLTexture3D(
     CLContextHandle context,
     ComputeMemoryFlags flags,
     Int32 target,
     Int32 miplevel,
     Int32 texture,
     out ComputeErrorCode errcode_ret);
Exemple #10
0
 public extern static CLProgramHandle CreateProgramWithBuiltInKernels(
     CLContextHandle context,
     Int32 num_devices,
     [MarshalAs(UnmanagedType.LPArray)] CLDeviceHandle[] device_list,
     String kernel_names,
     [MarshalAs(UnmanagedType.LPArray)] IntPtr[] lengths,
     out ComputeErrorCode errcode_ret);
        /// <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 = CL12.BuildProgram(Handle, handleCount, deviceHandles, options, buildNotify, notifyDataPtr);

#if DEBUG
            if (error != ComputeErrorCode.Success)
            {
                string str  = String.Empty;
                var    ptr  = Marshal.StringToHGlobalAnsi(str);
                IntPtr size = IntPtr.Zero;

                var errorCode = CL12.GetProgramBuildInfo(Handle, deviceHandles[0], ComputeProgramBuildInfo.BuildLog,
                                                         new IntPtr(4096), ptr,
                                                         out size);
                if (errorCode == ComputeErrorCode.Success)
                {
                    Debug.WriteLine($"Build Error:{Marshal.PtrToStringAnsi(ptr, (int) size)}");
                }
            }
#endif

            ComputeException.ThrowOnError(error);
        }
        /// <summary>
        /// Creates a new <see cref="ComputeBuffer{T}"/> from an existing OpenGL buffer object.
        /// </summary>
        /// <typeparam name="DataType"> The type of the elements of the <see cref="ComputeBuffer{T}"/>. <typeparamref name="T"/> should match the type of the elements in the OpenGL buffer. </typeparam>
        /// <param name="context"> A <see cref="ComputeContext"/> with enabled CL/GL sharing. </param>
        /// <param name="flags"> A bit-field that is used to specify usage information about the <see cref="ComputeBuffer{T}"/>. 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 <see cref="ComputeBuffer{T}"/>. </param>
        /// <returns> The created <see cref="ComputeBuffer{T}"/>. </returns>
        public static ComputeBuffer <DataType> CreateFromGLBuffer <DataType>(ComputeContext context, ComputeMemoryFlags flags, int bufferId) where DataType : struct
        {
            ComputeErrorCode error  = ComputeErrorCode.Success;
            CLMemoryHandle   handle = CL10.CreateFromGLBuffer(context.Handle, flags, bufferId, out error);

            ComputeException.ThrowOnError(error);
            return(new ComputeBuffer <DataType>(handle, context, flags));
        }
        /// <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));
        }
Exemple #14
0
 public extern static CLProgramHandle CreateProgramWithBinary(
     CLContextHandle context,
     Int32 num_devices,
     [MarshalAs(UnmanagedType.LPArray)] CLDeviceHandle[] device_list,
     [MarshalAs(UnmanagedType.LPArray)] IntPtr[] lengths,
     [MarshalAs(UnmanagedType.LPArray)] IntPtr[] binaries,
     [MarshalAs(UnmanagedType.LPArray)] Int32[] binary_status,
     out ComputeErrorCode errcode_ret);
Exemple #15
0
 public extern static CLMemoryHandle CreateImage2D(
     CLContextHandle context,
     ComputeMemoryFlags flags,
     ref ComputeImageFormat image_format,
     IntPtr image_width,
     IntPtr image_height,
     IntPtr image_row_pitch,
     IntPtr host_ptr,
     out ComputeErrorCode errcode_ret);
Exemple #16
0
        ///<summary>
        ///Creates a new <see cref = "ComputeImage 2 D"/> from an OpenGL 2D texture object.
        ///</summary>
        ///<param name = "context"> A <see cref = "ComputeContext"/> with enabled CL / GL sharing. </param>
        ///Only <c> ComputeMemoryFlags.ReadOnly </c>, <c> ComputeMemoryFlags.WriteOnly </c>, where <param name = "flags"> A bit-field that is used to specify usage information about the <   / 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(ComputeContext context, ComputeMemoryFlags flags, int textureTarget, int mipLevel, int textureId)
        {
            ComputeErrorCode error = ComputeErrorCode.Success;
            CLMemoryHandle   image = CL12.CreateFromGLTexture2D(context.Handle, flags, textureTarget, mipLevel, textureId, out error);

            ComputeException.ThrowOnError(error);

            return(new ComputeImage2D(image, context, flags));
        }
Exemple #17
0
        ///<summary>
        ///Creates a new <see cref = "ComputeImage 2 D"/> from an OpenGL renderbuffer object.
        ///</summary>
        ///<param name = "context"> A <see cref = "ComputeContext"/> with enabled CL / GL sharing. </param>
        ///Only <c> ComputeMemoryFlags.ReadOnly </c>, <c> ComputeMemoryFlags.WriteOnly </c>, where <param name = "flags"> A bit-field that is used to specify usage information about the <   / 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(ComputeContext context, ComputeMemoryFlags flags, int renderbufferId)
        {
            ComputeErrorCode error = ComputeErrorCode.Success;
            CLMemoryHandle   image = CL12.CreateFromGLRenderbuffer(context.Handle, flags, renderbufferId, 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>
        /// ComputeSamplerWithProperties
        /// </summary>
        /// <param name="context"></param>
        /// <param name="sampler_properties"></param>
        /// <param name="error"></param>
        public ComputeSampler(ComputeContext context, ComputeSamplerInfo[] sampler_properties, out ComputeErrorCode error)
        {
            error = ComputeErrorCode.Success;
            Handle = CLInterface.CL20.CreateSamplerWithProperties(context.Handle, sampler_properties, out error);
            ComputeException.ThrowOnError(error);

            SetID(Handle.Value);

            this.context = context;
        }
Exemple #20
0
        ////////////////////////////////////////////////////////////////////////////////////////////////////
        /// <summary>   Creates a new <see cref="ComputeImage3D"/>. </summary>
        ///
        /// <param name="context">      A valid <see cref="ComputeContext"/> in which the
        ///                             <see cref="ComputeImage3D"/> is created. </param>
        /// <param name="flags">        A bit-field that is used to specify allocation and usage
        ///                             information about the <see cref="ComputeImage3D"/>. </param>
        /// <param name="format">       A structure that describes the format properties of the
        ///                             <see cref="ComputeImage3D"/>. </param>
        /// <param name="width">        The width of the <see cref="ComputeImage3D"/> in pixels. </param>
        /// <param name="height">       The height of the <see cref="ComputeImage3D"/> in pixels. </param>
        /// <param name="depth">        The depth of the <see cref="ComputeImage3D"/> in pixels. </param>
        /// <param name="rowPitch">     The size in bytes of each row of elements of the
        ///                             <see cref="ComputeImage3D"/>. 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="slicePitch">   The size in bytes of each 2D slice in the
        ///                             <see cref="ComputeImage3D"/>. If <paramref name="slicePitch"/> is
        ///                             zero, OpenCL will compute the proper value based on
        ///                             <see cref="ComputeImage.RowPitch"/> and
        ///                             <see cref="ComputeImage.Height"/>. </param>
        /// <param name="data">         The data to initialize the <see cref="ComputeImage3D"/>. Can be
        ///                             <c>IntPtr.Zero</c>. </param>
        ////////////////////////////////////////////////////////////////////////////////////////////////////

        public ComputeImage3D(ComputeContext context, ComputeMemoryFlags flags, ComputeImageFormat format, int width, int height, int depth, long rowPitch, long slicePitch, IntPtr data)
            : base(context, flags)
        {
            ComputeErrorCode error = ComputeErrorCode.Success;

            Handle = CL12.CreateImage3D(context.Handle, flags, ref format, new IntPtr(width), new IntPtr(height), new IntPtr(depth), new IntPtr(rowPitch), new IntPtr(slicePitch), data, out error);
            ComputeException.ThrowOnError(error);

            Init();
        }
        /// <summary>
        /// Enqueues a wait command for a collection of <see cref="ComputeEvent"/>s to complete before any future commands queued in the <see cref="ComputeCommandQueue"/> are executed.
        /// </summary>
        /// <param name="events"> The <see cref="ComputeEvent"/>s that this command will wait for. </param>
        public void Wait(ICollection <ComputeEventBase> events)
        {
            int eventWaitListSize;

            CLEventHandle[] eventHandles = ComputeTools.ExtractHandles(events, out eventWaitListSize);

            ComputeErrorCode error = CL10.EnqueueWaitForEvents(Handle, eventWaitListSize, eventHandles);

            ComputeException.ThrowOnError(error);
        }
Exemple #22
0
 public new static CLMemoryHandle CreateFromGLTexture3D(
     CLContextHandle context,
     ComputeMemoryFlags flags,
     Int32 target,
     Int32 miplevel,
     Int32 texture,
     out ComputeErrorCode errcode_ret)
 {
     Trace.WriteLine("WARNING! clCreateFromGLTexture3D has been deprecated in OpenCL 1.2.");
     return(CL11.CreateFromGLTexture3D(context, flags, target, miplevel, texture, out errcode_ret));
 }
Exemple #23
0
 public extern static IntPtr EnqueueMapBuffer(
     CLCommandQueueHandle command_queue,
     CLMemoryHandle buffer,
     [MarshalAs(UnmanagedType.Bool)] bool blocking_map,
     ComputeMemoryMappingFlags map_flags,
     IntPtr offset,
     IntPtr cb,
     Int32 num_events_in_wait_list,
     [MarshalAs(UnmanagedType.LPArray)] CLEventHandle[] event_wait_list,
     [Out, MarshalAs(UnmanagedType.LPArray, SizeConst = 1)] CLEventHandle[] new_event,
     out ComputeErrorCode errcode_ret);
Exemple #24
0
 public static new CLMemoryHandle CreateFromGLTexture3D(
     CLContextHandle context,
     ComputeMemoryFlags flags,
     Int32 target,
     Int32 miplevel,
     Int32 texture,
     out ComputeErrorCode errcode_ret)
 {
     Trace.WriteLine("WARNING! clCreateFromGLTexture3D has been deprecated in OpenCL 1.2.");
     return CL11.CreateFromGLTexture3D(context, flags, target, miplevel, texture, out errcode_ret);
 }
Exemple #25
0
        ////////////////////////////////////////////////////////////////////////////////////////////////////
        /// <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)
        {
            Ensure.Argument(devices).NotNull("devices is null");

            CLDeviceHandle[] deviceHandles = ComputeTools.ExtractHandles(devices, out var handleCount);
            buildOptions = options ?? "";
            buildNotify  = notify;

            ComputeErrorCode error = CL12.BuildProgram(Handle, handleCount, deviceHandles, options, buildNotify, notifyDataPtr);

            ComputeException.ThrowOnError(error);
        }
Exemple #26
0
        /// <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)
        {
            ComputeErrorCode error = CL12.GetSupportedImageFormats(context.Handle, flags, type, 0, null, out var formatCountRet);

            ComputeException.ThrowOnError(error);

            ComputeImageFormat[] formats = new ComputeImageFormat[formatCountRet];
            error = CL12.GetSupportedImageFormats(context.Handle, flags, type, formatCountRet, formats, out formatCountRet);
            ComputeException.ThrowOnError(error);

            return(new Collection <ComputeImageFormat>(formats));
        }
        /// <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);
        }
Exemple #28
0
        internal ComputeKernel(string functionName, ComputeProgram program)
        {
            ComputeErrorCode error = ComputeErrorCode.Success;

            Handle = CLInterface.CL12.CreateKernel(program.Handle, functionName, out error);
            ComputeException.ThrowOnError(error);

            SetID(Handle.Value);

            context           = program.Context;
            this.functionName = functionName;
            this.program      = program;
        }
        /// <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 = CLInterface.CL12.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 });
        }
Exemple #30
0
 public new static CLMemoryHandle CreateImage2D(
     CLContextHandle context,
     ComputeMemoryFlags flags,
     ref ComputeImageFormat image_format,
     IntPtr image_width,
     IntPtr image_height,
     IntPtr image_row_pitch,
     IntPtr host_ptr,
     out ComputeErrorCode errcode_ret)
 {
     Trace.WriteLine("WARNING! clCreateImage2D has been deprecated in OpenCL 1.2.");
     return(CL11.CreateImage2D(context, flags, ref image_format, image_width, image_height, image_row_pitch, host_ptr, out errcode_ret));
 }
Exemple #31
0
 public extern static IntPtr EnqueueMapImage(
     CLCommandQueueHandle command_queue,
     CLMemoryHandle image,
     [MarshalAs(UnmanagedType.Bool)] bool blocking_map,
     ComputeMemoryMappingFlags map_flags,
     ref SysIntX3 origin,
     ref SysIntX3 region,
     out IntPtr image_row_pitch,
     out IntPtr image_slice_pitch,
     Int32 num_events_in_wait_list,
     [MarshalAs(UnmanagedType.LPArray)] CLEventHandle[] event_wait_list,
     [Out, MarshalAs(UnmanagedType.LPArray, SizeConst = 1)] CLEventHandle[] new_event,
     out ComputeErrorCode errcode_ret);
        /// <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="ComputeSampler"/>.
        /// </summary>
        /// <param name="context"> A <see cref="ComputeContext"/>. </param>
        /// <param name="normalizedCoords"> The usage state of normalized coordinates when accessing a <see cref="ComputeImage"/> in a <see cref="ComputeKernel"/>. </param>
        /// <param name="addressing"> The <see cref="ComputeImageAddressing"/> mode of the <see cref="ComputeSampler"/>. Specifies how out-of-range image coordinates are handled while reading. </param>
        /// <param name="filtering"> The <see cref="ComputeImageFiltering"/> mode of the <see cref="ComputeSampler"/>. Specifies the type of filter that must be applied when reading data from an image. </param>
        public ComputeSampler(ComputeContext context, bool normalizedCoords, ComputeImageAddressing addressing, ComputeImageFiltering filtering)
        {
            ComputeErrorCode error = ComputeErrorCode.Success;

            Handle = CLInterface.CL12.CreateSampler(context.Handle, normalizedCoords, addressing, filtering, out error);
            ComputeException.ThrowOnError(error);

            SetID(Handle.Value);

            this.addressing       = addressing;
            this.context          = context;
            this.filtering        = filtering;
            this.normalizedCoords = normalizedCoords;
        }
Exemple #34
0
 public static extern IntPtr EnqueueMapBuffer(
     CLCommandQueueHandle command_queue,
     CLMemoryHandle buffer,
     [MarshalAs(UnmanagedType.Bool)] bool blocking_map,
     ComputeMemoryMappingFlags map_flags,
     IntPtr offset,
     IntPtr cb,
     Int32 num_events_in_wait_list,
     [MarshalAs(UnmanagedType.LPArray)] CLEventHandle[] event_wait_list,
     [Out, MarshalAs(UnmanagedType.LPArray, SizeConst = 1)] CLEventHandle[] new_event,
     out ComputeErrorCode errcode_ret);
Exemple #35
0
 public static extern IntPtr EnqueueMapImage(
     CLCommandQueueHandle command_queue,
     CLMemoryHandle image,
     [MarshalAs(UnmanagedType.Bool)] bool blocking_map,
     ComputeMemoryMappingFlags map_flags,
     ref SysIntX3 origin,
     ref SysIntX3 region,
     out IntPtr image_row_pitch,
     out IntPtr image_slice_pitch,
     Int32 num_events_in_wait_list,
     [MarshalAs(UnmanagedType.LPArray)] CLEventHandle[] event_wait_list,
     [Out, MarshalAs(UnmanagedType.LPArray, SizeConst = 1)] CLEventHandle[] new_event,
     out ComputeErrorCode errcode_ret);
Exemple #36
0
 public static extern CLProgramHandle CreateProgramWithSource(
     CLContextHandle context,
     Int32 count,
     [In] String[] strings,
     [MarshalAs(UnmanagedType.LPArray)] IntPtr[] lengths,
     out ComputeErrorCode errcode_ret);
Exemple #37
0
 public static extern CLSamplerHandle CreateSampler(
     CLContextHandle context,
     [MarshalAs(UnmanagedType.Bool)] bool normalized_coords,
     ComputeImageAddressing addressing_mode,
     ComputeImageFiltering filter_mode,
     out ComputeErrorCode errcode_ret);
Exemple #38
0
 public IntPtr EnqueueMapBuffer(CLCommandQueueHandle command_queue, CLMemoryHandle buffer, [MarshalAs(UnmanagedType.Bool)] bool blocking_map, ComputeMemoryMappingFlags map_flags, IntPtr offset, IntPtr cb, Int32 num_events_in_wait_list, [MarshalAs(UnmanagedType.LPArray)] CLEventHandle[] event_wait_list, out CLEventHandle new_event, out ComputeErrorCode errcode_ret)
 {
     return StaticEnqueueMapBuffer(command_queue, buffer, blocking_map, map_flags, offset, cb, num_events_in_wait_list, event_wait_list, out new_event, out errcode_ret);
 }
Exemple #39
0
 public CLMemoryHandle CreatePipe(CLContextHandle context, ComputeMemoryFlags flags, int pipe_packet_size, int pipe_max_packets, [MarshalAs(UnmanagedType.LPArray)] IntPtr[] properties, out ComputeErrorCode errcode_ret)
 {
     throw new NotImplementedException();
 }
Exemple #40
0
 public CLEventHandle CreateUserEvent(CLContextHandle context, out ComputeErrorCode errcode_ret)
 {
     throw new NotImplementedException();
 }
Exemple #41
0
 public static extern CLMemoryHandle StaticCreateFromGLBuffer(CLContextHandle context, ComputeMemoryFlags flags, Int32 bufobj, out ComputeErrorCode errcode_ret);
        /// <summary>
        /// Checks for an OpenCL error code and throws a <see cref="ComputeException"/> if such is encountered.
        /// </summary>
        /// <param name="errorCode"> The OpenCL error code. </param>
        public static void ThrowOnError(ComputeErrorCode errorCode)
        {
            switch (errorCode)
            {
                case ComputeErrorCode.Success:
                    return;

                case ComputeErrorCode.DeviceNotFound:
                    throw new DeviceNotFoundComputeException();

                case ComputeErrorCode.DeviceNotAvailable:
                    throw new DeviceNotAvailableComputeException();

                case ComputeErrorCode.CompilerNotAvailable:
                    throw new CompilerNotAvailableComputeException();

                case ComputeErrorCode.MemoryObjectAllocationFailure:
                    throw new MemoryObjectAllocationFailureComputeException();

                case ComputeErrorCode.OutOfResources:
                    throw new OutOfResourcesComputeException();

                case ComputeErrorCode.OutOfHostMemory:
                    throw new OutOfHostMemoryComputeException();

                case ComputeErrorCode.ProfilingInfoNotAvailable:
                    throw new ProfilingInfoNotAvailableComputeException();

                case ComputeErrorCode.MemoryCopyOverlap:
                    throw new MemoryCopyOverlapComputeException();

                case ComputeErrorCode.ImageFormatMismatch:
                    throw new ImageFormatMismatchComputeException();

                case ComputeErrorCode.ImageFormatNotSupported:
                    throw new ImageFormatNotSupportedComputeException();

                case ComputeErrorCode.BuildProgramFailure:
                    throw new BuildProgramFailureComputeException();

                case ComputeErrorCode.MapFailure:
                    throw new MapFailureComputeException();

                case ComputeErrorCode.InvalidValue:
                    throw new InvalidValueComputeException();

                case ComputeErrorCode.InvalidDeviceType:
                    throw new InvalidDeviceTypeComputeException();

                case ComputeErrorCode.InvalidPlatform:
                    throw new InvalidPlatformComputeException();

                case ComputeErrorCode.InvalidDevice:
                    throw new InvalidDeviceComputeException();

                case ComputeErrorCode.InvalidContext:
                    throw new InvalidContextComputeException();

                case ComputeErrorCode.InvalidCommandQueueFlags:
                    throw new InvalidCommandQueueFlagsComputeException();

                case ComputeErrorCode.InvalidCommandQueue:
                    throw new InvalidCommandQueueComputeException();

                case ComputeErrorCode.InvalidHostPointer:
                    throw new InvalidHostPointerComputeException();

                case ComputeErrorCode.InvalidMemoryObject:
                    throw new InvalidMemoryObjectComputeException();

                case ComputeErrorCode.InvalidImageFormatDescriptor:
                    throw new InvalidImageFormatDescriptorComputeException();

                case ComputeErrorCode.InvalidImageSize:
                    throw new InvalidImageSizeComputeException();

                case ComputeErrorCode.InvalidSampler:
                    throw new InvalidSamplerComputeException();

                case ComputeErrorCode.InvalidBinary:
                    throw new InvalidBinaryComputeException();

                case ComputeErrorCode.InvalidBuildOptions:
                    throw new InvalidBuildOptionsComputeException();

                case ComputeErrorCode.InvalidProgram:
                    throw new InvalidProgramComputeException();

                case ComputeErrorCode.InvalidProgramExecutable:
                    throw new InvalidProgramExecutableComputeException();

                case ComputeErrorCode.InvalidKernelName:
                    throw new InvalidKernelNameComputeException();

                case ComputeErrorCode.InvalidKernelDefinition:
                    throw new InvalidKernelDefinitionComputeException();

                case ComputeErrorCode.InvalidKernel:
                    throw new InvalidKernelComputeException();

                case ComputeErrorCode.InvalidArgumentIndex:
                    throw new InvalidArgumentIndexComputeException();

                case ComputeErrorCode.InvalidArgumentValue:
                    throw new InvalidArgumentValueComputeException();

                case ComputeErrorCode.InvalidArgumentSize:
                    throw new InvalidArgumentSizeComputeException();

                case ComputeErrorCode.InvalidKernelArguments:
                    throw new InvalidKernelArgumentsComputeException();

                case ComputeErrorCode.InvalidWorkDimension:
                    throw new InvalidWorkDimensionsComputeException();

                case ComputeErrorCode.InvalidWorkGroupSize:
                    throw new InvalidWorkGroupSizeComputeException();

                case ComputeErrorCode.InvalidWorkItemSize:
                    throw new InvalidWorkItemSizeComputeException();

                case ComputeErrorCode.InvalidGlobalOffset:
                    throw new InvalidGlobalOffsetComputeException();

                case ComputeErrorCode.InvalidEventWaitList:
                    throw new InvalidEventWaitListComputeException();

                case ComputeErrorCode.InvalidEvent:
                    throw new InvalidEventComputeException();

                case ComputeErrorCode.InvalidOperation:
                    throw new InvalidOperationComputeException();

                case ComputeErrorCode.InvalidGLObject:
                    throw new InvalidGLObjectComputeException();

                case ComputeErrorCode.InvalidBufferSize:
                    throw new InvalidBufferSizeComputeException();

                case ComputeErrorCode.InvalidMipLevel:
                    throw new InvalidMipLevelComputeException();

                default:
                    throw new ComputeException(errorCode);
            }
        }
Exemple #43
0
 public static unsafe extern IntPtr CreateUserEvent(
     IntPtr context,
     ComputeErrorCode* errcode_ret);
Exemple #44
0
 public CLSamplerHandle CreateSamplerWithProperties(CLContextHandle context, [MarshalAs(UnmanagedType.LPArray)] ComputeSamplerInfo[] normalized_coords, out ComputeErrorCode errcode_ret)
 {
     throw new NotImplementedException();
 }
Exemple #45
0
 public static extern CLCommandQueueHandle CreateCommandQueue(
     CLContextHandle context,
     CLDeviceHandle device,
     ComputeCommandQueueFlags properties,
     out ComputeErrorCode errcode_ret);
Exemple #46
0
 public CLProgramHandle CreateProgramWithBuiltInKernels(CLContextHandle context, int num_devices, [MarshalAs(UnmanagedType.LPArray)] CLDeviceHandle[] device_list, string kernel_names, out ComputeErrorCode errcode_ret)
 {
     throw new NotImplementedException();
 }
Exemple #47
0
 public static extern CLMemoryHandle CreateBuffer(
     CLContextHandle context,
     ComputeMemoryFlags flags,
     IntPtr size,
     IntPtr host_ptr,
     out ComputeErrorCode errcode_ret);
Exemple #48
0
 public static extern CLMemoryHandle CreateImage3D(
     CLContextHandle context,
     ComputeMemoryFlags flags,
     ref ComputeImageFormat image_format,
     IntPtr image_width,
     IntPtr image_height,
     IntPtr image_depth,
     IntPtr image_row_pitch,
     IntPtr image_slice_pitch,
     IntPtr host_ptr,
     out ComputeErrorCode errcode_ret);
Exemple #49
0
 public ComputeErrorCode LinkProgram(CLContextHandle context, int num_devices, [MarshalAs(UnmanagedType.LPArray)] CLDeviceHandle[] device_list, string options, int num_input_programs, ComputeProgramBuildNotifier pfn_notify, IntPtr user_data, out ComputeErrorCode errcode_ret)
 {
     throw new NotImplementedException();
 }
Exemple #50
0
 public static extern CLKernelHandle CreateKernel(
     CLProgramHandle program,
     String kernel_name,
     out ComputeErrorCode errcode_ret);
Exemple #51
0
 public IntPtr EnqueueMapImage(CLCommandQueueHandle command_queue, CLMemoryHandle image, [MarshalAs(UnmanagedType.Bool)] bool blocking_map, ComputeMemoryMappingFlags map_flags, ref SysIntX3 origin, ref SysIntX3 region, out IntPtr image_row_pitch, out IntPtr image_slice_pitch, Int32 num_events_in_wait_list, [MarshalAs(UnmanagedType.LPArray)] CLEventHandle[] event_wait_list, out CLEventHandle new_event, out ComputeErrorCode errcode_ret)
 {
     return StaticEnqueueMapImage(command_queue, image, blocking_map, map_flags, ref origin, ref region, out image_row_pitch, out image_slice_pitch, num_events_in_wait_list, event_wait_list, out new_event, out errcode_ret);
 }
Exemple #52
0
 public static extern CLProgramHandle CreateProgramWithBinary(
     CLContextHandle context,
     Int32 num_devices,
     [MarshalAs(UnmanagedType.LPArray)] CLDeviceHandle[] device_list,
     [MarshalAs(UnmanagedType.LPArray)] IntPtr[] lengths,
     [MarshalAs(UnmanagedType.LPArray)] IntPtr[] binaries,
     [MarshalAs(UnmanagedType.LPArray)] Int32[] binary_status,
     out ComputeErrorCode errcode_ret);
Exemple #53
0
 public static extern CLContextHandle CreateContext(
     [MarshalAs(UnmanagedType.LPArray)] IntPtr[] properties,
     Int32 num_devices,
     [MarshalAs(UnmanagedType.LPArray)] CLDeviceHandle[] devices,
     ComputeContextNotifier pfn_notify,
     IntPtr user_data,
     out ComputeErrorCode errcode_ret);
Exemple #54
0
 public CLKernelHandle CreateKernel(CLProgramHandle program, String kernel_name, out ComputeErrorCode errcode_ret)
 {
     return StaticCreateKernel(program, kernel_name, out errcode_ret);
 }
 /// <summary>
 /// Creates a new <see cref="ComputeException"/> with a specified <see cref="ComputeErrorCode"/>.
 /// </summary>
 /// <param name="code"> A <see cref="ComputeErrorCode"/>. </param>
 public ComputeException(ComputeErrorCode code)
     : base("OpenCL error code detected: " + code.ToString() + ".")
 {
     this.code = code;
 }
Exemple #56
0
 public static extern CLContextHandle CreateContextFromType(
     [MarshalAs(UnmanagedType.LPArray)] IntPtr[] properties,
     ComputeDeviceTypes device_type,
     ComputeContextNotifier pfn_notify,
     IntPtr user_data,
     out ComputeErrorCode errcode_ret);
Exemple #57
0
 public static unsafe extern IntPtr CreateSubBuffer(
     IntPtr buffer,
     ComputeMemoryFlags flags,
     ComputeBufferCreateType buffer_create_type,
     /* const void * */ IntPtr buffer_create_info,
     ComputeErrorCode* errcode_ret);
Exemple #58
0
 public static extern CLMemoryHandle CreateFromGLRenderbuffer(
     CLContextHandle context,
     ComputeMemoryFlags flags,
     Int32 renderbuffer,
     out ComputeErrorCode errcode_ret);
Exemple #59
0
 public static extern CLMemoryHandle CreateFromGLTexture3D(
     CLContextHandle context,
     ComputeMemoryFlags flags,
     Int32 target,
     Int32 miplevel,
     Int32 texture,
     out ComputeErrorCode errcode_ret);
Exemple #60
0
 public CLMemoryHandle CreateSubBuffer(CLMemoryHandle buffer, ComputeMemoryFlags flags, ComputeBufferCreateType buffer_create_type, ref SysIntX2 buffer_create_info, out ComputeErrorCode errcode_ret)
 {
     throw new NotImplementedException();
 }