示例#1
0
        /// <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)
        {
            var size = ComputeTools.SizeOf <T>() * count;

            Handle = CL12.CreateBuffer(context.Handle, flags, new IntPtr(size), dataPtr, out var error);
            ComputeException.ThrowOnError(error);
            Init(size, count);
        }
示例#2
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));
        }
示例#3
0
        /// <summary>
        /// Creates a new buffer from a specified buffer.
        /// </summary>
        /// <param name="buffer"> The buffer to create the buffer from. </param>
        /// <param name="flags"> A bit-field that is used to specify allocation and usage information about the buffer. </param>
        /// <param name="offset"> The index of the element of <paramref name="buffer"/>, where the buffer starts. </param>
        /// <param name="count"> The number of elements of <paramref name="buffer"/> to include in the buffer. </param>
        public ComputeSubBuffer(ComputeBuffer <T> buffer, ComputeMemoryFlags flags, long offset, long count)
            : base(buffer.Context, flags)
        {
            SysIntX2 region = new SysIntX2(offset * Marshal.SizeOf(typeof(T)), count * Marshal.SizeOf(typeof(T)));

            CL11.CreateSubBuffer(Handle, flags, ComputeBufferCreateType.Region, ref region, out ComputeErrorCode error);
            ComputeException.ThrowOnError(error);
            Init();
        }
        ////////////////////////////////////////////////////////////////////////////////////////////////////
        /// <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        = CL12.WaitForEvents(eventWaitListSize, eventHandles);

            ComputeException.ThrowOnError(error);
        }
示例#5
0
        /// <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(HDSPUtils.SizeOf(typeof(T)) * count), dataPtr, out error);
            ComputeException.ThrowOnError(error);
            Init();
        }
示例#6
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));
        }
示例#7
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();
        }
示例#8
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);
        }
示例#9
0
        /// <summary>
        /// Creates a new <see cref="ComputeSubBuffer{T}"/> from a specified <see cref="ComputeBuffer{T}"/>.
        /// </summary>
        /// <param name="buffer"> The buffer to create the <see cref="ComputeSubBuffer{T}"/> from. </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="offset"> The index of the element of <paramref name="buffer"/>, where the <see cref="ComputeSubBuffer{T}"/> starts. </param>
        /// <param name="count"> The number of elements of <paramref name="buffer"/> to include in the <see cref="ComputeSubBuffer{T}"/>. </param>
        public ComputeSubBuffer(ComputeBuffer <T> buffer, ComputeMemoryFlags flags, long offset, long count)
            : base(buffer.Context, flags)
        {
            var sizeofT = ComputeTools.SizeOf <T>();

            SysIntX2 region = new SysIntX2(offset * sizeofT, count * sizeofT);

            Handle = CL11.CreateSubBuffer(buffer.Handle, flags, ComputeBufferCreateType.Region, ref region, out var error);
            ComputeException.ThrowOnError(error);

            Init();
        }
示例#10
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);
        }
示例#12
0
        /// <summary>
        /// Creates a new <see cref="ComputeImage3D"/> from an OpenGL 3D 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="ComputeImage3D"/>. Only <c>ComputeMemoryFlags.ReadOnly</c>, <c>ComputeMemoryFlags.WriteOnly</c> and <c>ComputeMemoryFlags.ReadWrite</c> are allowed. </param>
        /// <param name="textureTarget"> The image type of texture. Must be GL_TEXTURE_3D. </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 ComputeImage3D CreateFromGLTexture3D(IComputeContext context, ComputeMemoryFlags flags, int textureTarget, int mipLevel, int textureId)
        {
            var image = CL10.CreateFromGLTexture3D(
                context.Handle,
                flags,
                textureTarget,
                mipLevel,
                textureId,
                out ComputeErrorCode error);

            ComputeException.ThrowOnError(error);
            return(new ComputeImage3D(image, context, flags));
        }
示例#13
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;
        }
示例#14
0
        /// <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 });
        }
示例#15
0
        ////////////////////////////////////////////////////////////////////////////////////////////////////
        /// <summary>   Initializes a new instance of the Cloo.ComputeKernel class. </summary>
        ///
        /// <param name="functionName"> Gets the function name of the <see cref="ComputeKernel"/>. </param>
        /// <param name="program">      Gets the <see cref="ComputeProgram"/> that the
        ///                             <see cref="ComputeKernel"/> belongs to. </param>
        ////////////////////////////////////////////////////////////////////////////////////////////////////

        internal ComputeKernel(string functionName, ComputeProgram program)
        {
            Handle = CL12.CreateKernel(program.Handle, functionName, out var error);
            ComputeException.ThrowOnError(error);

            SetID(Handle.Value);

            context           = program.Context;
            this.functionName = functionName;
            this.program      = program;

            RILogManager.Default?.SendTrace(string.Intern("Create ") + this + string.Intern(" in Thread(") + Thread.CurrentThread.ManagedThreadId + string.Intern(")."), string.Intern("Information"));
        }
示例#16
0
        /// <summary>
        /// Creates a new <see cref="ComputeUserEvent"/>.
        /// </summary>
        /// <param name="context"> The <see cref="ComputeContext"/> in which the <see cref="ComputeUserEvent"/> is created. </param>
        /// <remarks> Requires OpenCL 1.1. </remarks>
        public ComputeUserEvent(ComputeContext context)
        {
            Handle = CL11.CreateUserEvent(context.Handle, out var error);
            ComputeException.ThrowOnError(error);

            SetID(Handle.Value);

            Type    = (ComputeCommandType)GetInfo <CLEventHandle, ComputeEventInfo, uint>(Handle, ComputeEventInfo.CommandType, CL12.GetEventInfo);
            Context = context;
            //HookNotifier();

            //Debug.WriteLine("Create " + this + " in Thread(" + Thread.CurrentThread.ManagedThreadId + ").", "Information");
        }
示例#17
0
        ////////////////////////////////////////////////////////////////////////////////////////////////////
        /// <summary>
        /// Creates a new <see cref="ComputeProgram"/> from an array of source code strings.
        /// </summary>
        ///
        /// <remarks>
        /// The created <see cref="ComputeProgram"/> is associated with the
        /// <see cref="ComputeContext.Devices"/>.
        /// </remarks>
        ///
        /// <param name="context">  A <see cref="ComputeContext"/>. </param>
        /// <param name="source">   The source code lines for the <see cref="ComputeProgram"/>. </param>
        ////////////////////////////////////////////////////////////////////////////////////////////////////

        public ComputeProgram(ComputeContext context, string[] source)
        {
            Ensure.Argument(context).NotNull("context is null");

            Handle = CL12.CreateProgramWithSource(context.Handle, source.Length, source, null, out var error);
            ComputeException.ThrowOnError(error);

            this.context = context;
            devices      = context.Devices;
            this.source  = new ReadOnlyCollection <string>(source);

            RILogManager.Default?.SendTrace("Create " + this + " in Thread(" + Thread.CurrentThread.ManagedThreadId + ").", "Information");
        }
示例#18
0
        /// <summary>
        /// Creates a new <see cref="ComputeUserEvent"/>.
        /// </summary>
        /// <param name="context"> The <see cref="ComputeContext"/> in which the <see cref="ComputeUserEvent"/> is created. </param>
        /// <remarks> Requires OpenCL 1.1. </remarks>
        public ComputeUserEvent(ComputeContext context)
        {
            ComputeErrorCode error;

            Handle = CLInterface.CL11.CreateUserEvent(context.Handle, out error);
            ComputeException.ThrowOnError(error);

            SetID(Handle.Value);

            Type    = (ComputeCommandType)GetInfo <CLEventHandle, ComputeEventInfo, uint>(Handle, ComputeEventInfo.CommandType, CLInterface.CL12.GetEventInfo);
            Context = context;
            HookNotifier();
        }
示例#19
0
        ////////////////////////////////////////////////////////////////////////////////////////////////////
        /// <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)
        {
            Handle = CL12.CreateSampler(context.Handle, normalizedCoords, addressing, filtering, out var error);
            ComputeException.ThrowOnError(error);

            SetID(Handle.Value);

            this.addressing       = addressing;
            this.context          = context;
            this.filtering        = filtering;
            this.normalizedCoords = normalizedCoords;

            RILogManager.Default?.SendTrace(string.Intern("Create ") + this + string.Intern(" in Thread(") + Thread.CurrentThread.ManagedThreadId + string.Intern(")."), string.Intern("Information"));
        }
示例#20
0
        /// <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;
        }
        /// <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");
        }
示例#22
0
        /// <summary>
        /// Builds (compiles and links) a program executable from the program source or binary for all or some of the devices.
        /// </summary>
        /// <param name="devices"> A subset or all of devices. If devices is <c>null</c>, the executable is built for every item of 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>, build does not need to wait for the build to complete and can return immediately. If <paramref name="notify"/> is <c>null</c>, 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 <IComputeDevice> devices, string options,
                          ComputeProgramBuildNotifier notify, IntPtr notifyDataPtr)
        {
            var deviceHandles = ComputeTools.ExtractHandles(devices, out int handleCount);
            var BuildOptions  = options ?? "";
            var error         = OpenCL100.BuildProgram(
                Handle,
                handleCount,
                deviceHandles,
                options,
                notify,
                notifyDataPtr);

            ComputeException.ThrowOnError(error);
        }
        /// <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");
        }
示例#24
0
        ////////////////////////////////////////////////////////////////////////////////////////////////////
        /// <summary>   Creates a new <see cref="ComputeUserEvent"/>. </summary>
        ///
        /// <remarks>   Requires OpenCL 1.1. </remarks>
        ///
        /// <param name="context">  The <see cref="ComputeContext"/> in which the
        ///                         <see cref="ComputeUserEvent"/> is created. </param>
        ////////////////////////////////////////////////////////////////////////////////////////////////////

        public ComputeUserEvent(ComputeContext context)
        {
            ComputeErrorCode error;

            Handle = CL11.CreateUserEvent(context.Handle, out error);
            ComputeException.ThrowOnError(error);

            SetID(Handle.Value);

            Type    = (ComputeCommandType)GetInfo <CLEventHandle, ComputeEventInfo, uint>(Handle, ComputeEventInfo.CommandType, CL12.GetEventInfo);
            Context = context;
            HookNotifier();

            RILogManager.Default?.SendTrace("Create " + this + " in Thread(" + Thread.CurrentThread.ManagedThreadId + ").", "Information");
        }
        ////////////////////////////////////////////////////////////////////////////////////////////////////
        /// <summary>   Initializes a new instance of the Cloo.ComputeKernel class. </summary>
        ///
        /// <param name="functionName"> Gets the function name of the <see cref="ComputeKernel"/>. </param>
        /// <param name="program">      Gets the <see cref="ComputeProgram"/> that the
        ///                             <see cref="ComputeKernel"/> belongs to. </param>
        ////////////////////////////////////////////////////////////////////////////////////////////////////

        internal ComputeKernel(string functionName, ComputeProgram program)
        {
            ComputeErrorCode error = ComputeErrorCode.Success;

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

            SetID(Handle.Value);

            context           = program.Context;
            this.functionName = functionName;
            this.program      = program;

            Trace.WriteLine("Create " + this + " in Thread(" + Thread.CurrentThread.ManagedThreadId + ").", "Information");
        }
示例#26
0
        /// <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 = 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;

            Trace.WriteLine("Create " + this + " in Thread(" + Thread.CurrentThread.ManagedThreadId + ").", "Information");
        }
示例#27
0
        /// <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="data"> The data for the <see cref="ComputeBuffer{T}"/>. </param>
        /// <remarks> Note, that <paramref name="data"/> cannot be an "immediate" parameter, i.e.: <c>new T[100]</c>, because it could be quickly collected by the GC causing Cloo to send and invalid reference to OpenCL. </remarks>
        public ComputeBuffer(ComputeContext context, ComputeMemoryFlags flags, T[] data)
            : base(context, flags)
        {
            GCHandle dataPtr = GCHandle.Alloc(data, GCHandleType.Pinned);

            try
            {
                ComputeErrorCode error = ComputeErrorCode.Success;
                Handle = CL10.CreateBuffer(context.Handle, flags, new IntPtr(Marshal.SizeOf(typeof(T)) * data.Length), dataPtr.AddrOfPinnedObject(), out error);
                ComputeException.ThrowOnError(error);
            }
            finally
            {
                dataPtr.Free();
            }
            Init();
        }
        /// <summary>
        /// Enqueues a command to write data to a <see cref="ComputeImage"/>.
        /// </summary>
        /// <param name="destination"> The <see cref="ComputeImage"/> to write to. </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="destinationOffset"> The <paramref name="destination"/> element position where writing starts. </param>
        /// <param name="region"> The region of elements to write. </param>
        /// <param name="rowPitch"> The <see cref="ComputeImage.RowPitch"/> of <paramref name="destination"/> or 0. </param>
        /// <param name="slicePitch"> The <see cref="ComputeImage.SlicePitch"/> of <paramref name="destination"/> or 0. </param>
        /// <param name="source"> The content written to the <see cref="ComputeImage"/>. </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 void Write(ComputeImage destination, bool blocking, SysIntX3 destinationOffset, SysIntX3 region, long rowPitch, long slicePitch, IntPtr source, ICollection <ComputeEventBase> events)
        {
            int eventWaitListSize;

            CLEventHandle[] eventHandles   = ComputeTools.ExtractHandles(events, out eventWaitListSize);
            bool            eventsWritable = (events != null && !events.IsReadOnly);

            CLEventHandle[] newEventHandle = (eventsWritable) ? new CLEventHandle[1] : null;

            ComputeErrorCode error = CL10.EnqueueWriteImage(Handle, destination.Handle, blocking, ref destinationOffset, ref region, new IntPtr(rowPitch), new IntPtr(slicePitch), source, eventWaitListSize, eventHandles, newEventHandle);

            ComputeException.ThrowOnError(error);

            if (eventsWritable)
            {
                events.Add(new ComputeEvent(newEventHandle[0], this));
            }
        }
        /// <summary>
        /// Enqueues a command to execute a range of <see cref="ComputeKernel"/>s in parallel.
        /// </summary>
        /// <param name="kernel"> The <see cref="ComputeKernel"/> to execute. </param>
        /// <param name="globalWorkOffset"> An array of values that describe the offset used to calculate the global ID of a work-item instead of having the global IDs always start at offset (0, 0,... 0). </param>
        /// <param name="globalWorkSize"> An array of values that describe the number of global work-items in dimensions that will execute the kernel function. The total number of global work-items is computed as global_work_size[0] *...* global_work_size[work_dim - 1]. </param>
        /// <param name="localWorkSize"> An array of values that describe the number of work-items that make up a work-group (also referred to as the size of the work-group) that will execute the <paramref name="kernel"/>. The total number of work-items in a work-group is computed as local_work_size[0] *... * local_work_size[work_dim - 1]. </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>
        public void Execute(ComputeKernel kernel, long[] globalWorkOffset, long[] globalWorkSize, long[] localWorkSize, ICollection <ComputeEventBase> events)
        {
            int eventWaitListSize;

            CLEventHandle[] eventHandles   = ComputeTools.ExtractHandles(events, out eventWaitListSize);
            bool            eventsWritable = (events != null && !events.IsReadOnly);

            CLEventHandle[] newEventHandle = (eventsWritable) ? new CLEventHandle[1] : null;

            ComputeErrorCode error = CL10.EnqueueNDRangeKernel(Handle, kernel.Handle, globalWorkSize.Length, ComputeTools.ConvertArray(globalWorkOffset), ComputeTools.ConvertArray(globalWorkSize), ComputeTools.ConvertArray(localWorkSize), eventWaitListSize, eventHandles, newEventHandle);

            ComputeException.ThrowOnError(error);

            if (eventsWritable)
            {
                events.Add(new ComputeEvent(newEventHandle[0], this));
            }
        }
        /// <summary>
        /// Enqueues a command to execute a single <see cref="ComputeKernel"/>.
        /// </summary>
        /// <param name="kernel"> The <see cref="ComputeKernel"/> to execute. </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>
        public void ExecuteTask(ComputeKernel kernel, ICollection <ComputeEventBase> events)
        {
            int eventWaitListSize;

            CLEventHandle[] eventHandles   = ComputeTools.ExtractHandles(events, out eventWaitListSize);
            bool            eventsWritable = (events != null && !events.IsReadOnly);

            CLEventHandle[] newEventHandle = (eventsWritable) ? new CLEventHandle[1] : null;

            ComputeErrorCode error = CL10.EnqueueTask(Handle, kernel.Handle, eventWaitListSize, eventHandles, newEventHandle);

            ComputeException.ThrowOnError(error);

            if (eventsWritable)
            {
                events.Add(new ComputeEvent(newEventHandle[0], this));
            }
        }