コード例 #1
0
        /// <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 = CL12.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);

            Trace.WriteLine("Create " + this + " in Thread(" + Thread.CurrentThread.ManagedThreadId + ").", "Information");
        }
コード例 #2
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 = 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");
        }
コード例 #3
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);
        }
コード例 #4
0
        ////////////////////////////////////////////////////////////////////////////////////////////////////
        /// <summary>   Creates a new <see cref="ComputeProgram"/> from a source code string. </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 for the <see cref="ComputeProgram"/>. </param>
        ////////////////////////////////////////////////////////////////////////////////////////////////////

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

            Handle = CL12.CreateProgramWithSource(context.Handle, 1, new string[] { source }, null, out var error);
            ComputeException.ThrowOnError(error);

            SetID(Handle.Value);

            this.context = context;
            devices      = context.Devices;
            this.source  = new ReadOnlyCollection <string>(new string[] { source });

            RILogManager.Default?.SendTrace("Create " + this + " in Thread(" + Thread.CurrentThread.ManagedThreadId + ").", "Information");
        }
コード例 #5
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)
        {
            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");
        }
コード例 #6
0
ファイル: ComputeUserEvent.cs プロジェクト: Kawaian/KelpNet
        ///<summary>
        ///Creates a new <see cref = "ComputeUserEvent"/>.
        ///</summary>
        ///<para 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 = 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();

            Trace.WriteLine("Create " + this + " in Thread(" + Thread.CurrentThread.ManagedThreadId + ").", "Information");
        }
コード例 #7
0
ファイル: ComputeSampler.cs プロジェクト: scenevista/Cloo
        /// <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");
        }
コード例 #8
0
        /// <summary>
        /// Creates a new <see cref="ComputeProgram"/> from an array of source code strings.
        /// </summary>
        /// <param name="context"> A <see cref="ComputeContext"/>. </param>
        /// <param name="source"> The source code lines 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,
                source.Length,
                source,
                null,
                out error);
            ComputeException.ThrowOnError(error);

            this.context = context;
            this.devices = context.Devices;
            this.source  = new ReadOnlyCollection <string>(source);
        }
コード例 #9
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();
        }
コード例 #10
0
ファイル: ComputeProgram.cs プロジェクト: scenevista/Cloo
        /// <summary>
        /// Creates a new <see cref="ComputeProgram"/> from an array of source code strings.
        /// </summary>
        /// <param name="context"> A <see cref="ComputeContext"/>. </param>
        /// <param name="source"> The source code lines 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 = CL12.CreateProgramWithSource(
                context.Handle,
                source.Length,
                source,
                null,
                out error);
            ComputeException.ThrowOnError(error);

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

            Trace.WriteLine("Create " + this + " in Thread(" + Thread.CurrentThread.ManagedThreadId + ").", "Information");
        }
コード例 #11
0
        /// <summary>
        /// Creates a new <see cref="ComputeContext"/> on all the <see cref="ComputeDevice"/>s that match the specified <see cref="ComputeDeviceTypes"/>.
        /// </summary>
        /// <param name="deviceType"> A bit-field that identifies the type of <see cref="ComputeDevice"/> to associate with the <see cref="ComputeContext"/>. </param>
        /// <param name="properties"> A <see cref="ComputeContextPropertyList"/> of the <see cref="ComputeContext"/>. </param>
        /// <param name="notify"> A delegate instance that refers to a notification routine. This routine is a callback function that will be used by the OpenCL implementation to report information on errors that occur in the <see cref="ComputeContext"/>. 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 <see cref="ComputeContext"/> is disposed. If <paramref name="notify"/> is <c>null</c>, no callback function is registered. </param>
        /// <param name="userDataPtr"> Optional user data that will be passed to <paramref name="notify"/>. </param>
        public ComputeContext(ComputeDeviceTypes deviceType, ComputeContextPropertyList properties, ComputeContextNotifier notify, IntPtr userDataPtr)
        {
            IntPtr[] propertyArray = (properties != null) ? properties.ToIntPtrArray() : null;
            callback = notify;

            ComputeErrorCode error = ComputeErrorCode.Success;

            Handle = CLInterface.CL12.CreateContextFromType(propertyArray, deviceType, notify, userDataPtr, 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();
        }
コード例 #12
0
        ////////////////////////////////////////////////////////////////////////////////////////////////////
        /// <summary>
        /// Creates a new <see cref="ComputeContext"/> on all the <see cref="ComputeDevice"/>s that match
        /// the specified <see cref="ComputeDeviceTypes"/>.
        /// </summary>
        ///
        /// <param name="deviceType">   A bit-field that identifies the type of
        ///                             <see cref="ComputeDevice"/> to associate with the
        ///                             <see cref="ComputeContext"/>. </param>
        /// <param name="properties">   A <see cref="ComputeContextPropertyList"/> of the
        ///                             <see cref="ComputeContext"/>. </param>
        /// <param name="notify">       A delegate instance that refers to a notification routine. This
        ///                             routine is a callback function that will be used by the OpenCL
        ///                             implementation to report information on errors that occur in the
        ///                             <see cref="ComputeContext"/>. 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
        ///                             <see cref="ComputeContext"/> is disposed. If
        ///                             <paramref name="notify"/> is <c>null</c>, no callback function is
        ///                             registered. </param>
        /// <param name="userDataPtr">  Optional user data that will be passed to
        ///                             <paramref name="notify"/>. </param>
        ////////////////////////////////////////////////////////////////////////////////////////////////////

        public ComputeContext(ComputeDeviceTypes deviceType, ComputeContextPropertyList properties, ComputeContextNotifier notify, IntPtr userDataPtr)
        {
            IntPtr[] propertyArray = properties?.ToIntPtrArray();
            callback = notify;

            Handle = CL12.CreateContextFromType(propertyArray, deviceType, notify, userDataPtr, out var error);
            ComputeException.ThrowOnError(error);

            SetID(Handle.Value);

            this.properties = properties;
            ComputeContextProperty platformProperty = properties.GetByName(ComputeContextPropertyName.Platform);

            platform = ComputePlatform.GetByHandle(platformProperty.Value);
            devices  = GetDevices();

            RILogManager.Default?.SendTrace("Create " + this + " in Thread(" + Thread.CurrentThread.ManagedThreadId + ").", "Information");
        }
コード例 #13
0
 /// <summary>
 /// Creates a new <see cref="ComputeImage3D"/>.
 /// </summary>
 /// <param name="context"> A valid context 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 image width and image element size. </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 image row pitch and image height. </param>
 /// <param name="data"> The data to initialize the <see cref="ComputeImage3D"/>. Can be <c>IntPtr.Zero</c>. </param>
 public ComputeImage3D(IComputeContext context, ComputeMemoryFlags flags, ComputeImageFormat format,
                       int width, int height, int depth, long rowPitch, long slicePitch, IntPtr data)
     : base(context, flags)
 {
     Handle = CL10.CreateImage3D(
         context.Handle,
         flags,
         ref format,
         new IntPtr(width),
         new IntPtr(height),
         new IntPtr(depth),
         new IntPtr(rowPitch),
         new IntPtr(slicePitch),
         data,
         out ComputeErrorCode error);
     ComputeException.ThrowOnError(error);
     Init();
 }
コード例 #14
0
        ///<summary>
        ///<See cref = "ComputeDevice"/> s on the <see cref = "ComputePlatform"/>.
        ///</summary>
        ///<returns> A read-only collection of the available <see cref = "ComputeDevice"/> s on the <see cref = "ComputePlatform"/>. </returns>
        ///<c> ComputeDevice.Available </c> is <c> false </c> </remarks> This method resets the <c> ComputePlatform.Devices </c>   after see <cref = "ComputeContext"/> and <see cref = "ComputeCommandQueue"/> s that use the <see cref = "ComputeDevice"/> have been created and commands have been queued to them. Further calls will trigger an   <c> cref = "ComputeDevice"/>. </c> </c> After this method is executed.   remarks>
        public ReadOnlyCollection<ComputeDevice> QueryDevices()
        {
            int handlesLength = 0;
            ComputeErrorCode error = CL12.GetDeviceIDs(Handle, ComputeDeviceTypes.All, 0, null, out handlesLength);
            ComputeException.ThrowOnError(error);

            CLDeviceHandle[] handles = new CLDeviceHandle[handlesLength];
            error = CL12.GetDeviceIDs(Handle, ComputeDeviceTypes.All, handlesLength, handles, out handlesLength);
            ComputeException.ThrowOnError(error);

            ComputeDevice[] devices = new ComputeDevice[handlesLength];
            for (int i = 0; i < handlesLength; i++)
                devices[i] = new ComputeDevice(this, handles[i]);

            this.devices = new ReadOnlyCollection<ComputeDevice>(devices);

            return this.devices;
        }
コード例 #15
0
        /// <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));
            }
        }
コード例 #16
0
        /// <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));
            }
        }
コード例 #17
0
        /// <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));
            }
        }
コード例 #18
0
        /// <summary>
        /// Enqueues a command to copy data between <see cref="ComputeImage"/>s.
        /// </summary>
        /// <param name="source"> The <see cref="ComputeImage"/> to copy from. </param>
        /// <param name="destination"> The <see cref="ComputeImage"/> to copy to. </param>
        /// <param name="sourceOffset"> The <paramref name="source"/> element position where reading starts. </param>
        /// <param name="destinationOffset"> The <paramref name="destination"/> element position where writing starts. </param>
        /// <param name="region"> The region of elements to copy. </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 Copy(ComputeImage source, ComputeImage destination, SysIntX3 sourceOffset, SysIntX3 destinationOffset, SysIntX3 region, 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.EnqueueCopyImage(Handle, source.Handle, destination.Handle, ref sourceOffset, ref destinationOffset, ref region, eventWaitListSize, eventHandles, newEventHandle);

            ComputeException.ThrowOnError(error);

            if (eventsWritable)
            {
                events.Add(new ComputeEvent(newEventHandle[0], this));
            }
        }
コード例 #19
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)
        {
            var size = ComputeTools.SizeOf <T>() * data.Length;

            GCHandle dataPtr = GCHandle.Alloc(data, GCHandleType.Pinned);

            try
            {
                Handle = CL12.CreateBuffer(context.Handle, flags, new IntPtr(size), dataPtr.AddrOfPinnedObject(), out var error);
                ComputeException.ThrowOnError(error);
            }
            finally
            {
                dataPtr.Free();
            }

            Init(size, data.Length);
        }
コード例 #20
0
        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="THandleType"></typeparam>
        /// <typeparam name="TInfoType"></typeparam>
        /// <typeparam name="TQueriedType"></typeparam>
        /// <param name="handle"></param>
        /// <param name="paramName"></param>
        /// <param name="getInfoDelegate"></param>
        /// <returns></returns>
        protected static TQueriedType GetInfo <THandleType, TInfoType, TQueriedType>
            (THandleType handle, TInfoType paramName, GetInfoDelegate <THandleType, TInfoType> getInfoDelegate)
            where TQueriedType : struct
        {
            TQueriedType result   = new TQueriedType();
            GCHandle     gcHandle = GCHandle.Alloc(result, GCHandleType.Pinned);

            try
            {
                ComputeErrorCode error = getInfoDelegate(handle, paramName, (IntPtr)Marshal.SizeOf(result), gcHandle.AddrOfPinnedObject(), out _);
                ComputeException.ThrowOnError(error);
            }
            finally
            {
                result = (TQueriedType)gcHandle.Target;
                gcHandle.Free();
            }
            return(result);
        }
コード例 #21
0
        ////////////////////////////////////////////////////////////////////////////////////////////////////
        /// <summary>
        /// Creates a new <see cref="ComputeContext"/> on a collection of <see cref="ComputeDevice"/>s.
        /// </summary>
        ///
        /// <param name="devices">          A collection of <see cref="ComputeDevice"/>s to associate
        ///                                 with the <see cref="ComputeContext"/>. </param>
        /// <param name="properties">       A <see cref="ComputeContextPropertyList"/> of the
        ///                                 <see cref="ComputeContext"/>. </param>
        /// <param name="notify">           A delegate instance that refers to a notification routine.
        ///                                 This routine is a callback function that will be used by the
        ///                                 OpenCL implementation to report information on errors that occur
        ///                                 in the <see cref="ComputeContext"/>. 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
        ///                                 <see cref="ComputeContext"/> is disposed. If
        ///                                 <paramref name="notify"/> is <c>null</c>, no callback function is
        ///                                 registered. </param>
        /// <param name="notifyDataPtr">    Optional user data that will be passed to
        ///                                 <paramref name="notify"/>. </param>
        ////////////////////////////////////////////////////////////////////////////////////////////////////

        public ComputeContext(ICollection <ComputeDevice> devices, ComputeContextPropertyList properties, ComputeContextNotifier notify, IntPtr notifyDataPtr)
        {
            CLDeviceHandle[] deviceHandles = ComputeTools.ExtractHandles(devices, out var handleCount);
            IntPtr[]         propertyArray = properties?.ToIntPtrArray();
            callback = notify;

            Handle = CL12.CreateContext(propertyArray, handleCount, deviceHandles, notify, notifyDataPtr, out var error);
            ComputeException.ThrowOnError(error);

            SetID(Handle.Value);

            this.properties = properties;
            ComputeContextProperty platformProperty = properties.GetByName(ComputeContextPropertyName.Platform);

            platform     = ComputePlatform.GetByHandle(platformProperty.Value);
            this.devices = GetDevices();

            RILogManager.Default?.SendTrace("Create " + this + " in Thread(" + Thread.CurrentThread.ManagedThreadId + ").", "Information");
        }
コード例 #22
0
        /// <summary>
        /// Creates a new <see cref="ComputeCommandQueue"/>.
        /// </summary>
        /// <param name="context"> A <see cref="ComputeContext"/>. </param>
        /// <param name="device"> A <see cref="ComputeDevice"/> associated with the <paramref name="context"/>. It can either be one of <see cref="ComputeContext.Devices"/> or have the same <see cref="ComputeDeviceTypes"/> as the <paramref name="device"/> specified when the <paramref name="context"/> is created. </param>
        /// <param name="properties"> The properties for the <see cref="ComputeCommandQueue"/>. </param>
        public ComputeCommandQueue(ComputeContext context, ComputeDevice device, ComputeCommandQueueFlags properties)
        {
            ComputeErrorCode error = ComputeErrorCode.Success;

            Handle = CL10.CreateCommandQueue(context.Handle, device.Handle, properties, out error);
            ComputeException.ThrowOnError(error);

            SetID(Handle.Value);

            this.device  = device;
            this.context = context;

            outOfOrderExec = ((properties & ComputeCommandQueueFlags.OutOfOrderExecution) == ComputeCommandQueueFlags.OutOfOrderExecution);
            profiling      = ((properties & ComputeCommandQueueFlags.Profiling) == ComputeCommandQueueFlags.Profiling);

            Events = new List <ComputeEventBase>();

            Trace.WriteLine("Create " + this + " in Thread(" + Thread.CurrentThread.ManagedThreadId + ").", "Information");
        }
コード例 #23
0
        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="TMainHandleType"></typeparam>
        /// <typeparam name="TSecondHandleType"></typeparam>
        /// <typeparam name="TInfoType"></typeparam>
        /// <typeparam name="TQueriedType"></typeparam>
        /// <param name="mainHandle"></param>
        /// <param name="secondHandle"></param>
        /// <param name="paramName"></param>
        /// <param name="getInfoDelegate"></param>
        /// <returns></returns>
        protected static TQueriedType[] GetArrayInfo <TMainHandleType, TSecondHandleType, TInfoType, TQueriedType>
            (TMainHandleType mainHandle, TSecondHandleType secondHandle, TInfoType paramName, GetInfoDelegateEx <TMainHandleType, TSecondHandleType, TInfoType> getInfoDelegate)
        {
            var error = getInfoDelegate(mainHandle, secondHandle, paramName, IntPtr.Zero, IntPtr.Zero, out var bufferSizeRet);

            ComputeException.ThrowOnError(error);
            var      buffer   = new TQueriedType[bufferSizeRet.ToInt64() / Marshal.SizeOf(typeof(TQueriedType))];
            GCHandle gcHandle = GCHandle.Alloc(buffer, GCHandleType.Pinned);

            try
            {
                error = getInfoDelegate(mainHandle, secondHandle, paramName, bufferSizeRet, gcHandle.AddrOfPinnedObject(), out bufferSizeRet);
                ComputeException.ThrowOnError(error);
            }
            finally
            {
                gcHandle.Free();
            }
            return(buffer);
        }
コード例 #24
0
ファイル: ComputeObject.cs プロジェクト: rblenis/cudafy
        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="HandleType"></typeparam>
        /// <typeparam name="InfoType"></typeparam>
        /// <typeparam name="QueriedType"></typeparam>
        /// <param name="handle"></param>
        /// <param name="paramName"></param>
        /// <param name="getInfoDelegate"></param>
        /// <returns></returns>
        protected QueriedType GetInfo <HandleType, InfoType, QueriedType>
            (HandleType handle, InfoType paramName, GetInfoDelegate <HandleType, InfoType> getInfoDelegate)
            where QueriedType : struct
        {
            QueriedType result   = new QueriedType();
            GCHandle    gcHandle = GCHandle.Alloc(result, GCHandleType.Pinned);

            try
            {
                IntPtr           sizeRet;
                ComputeErrorCode error = getInfoDelegate(handle, paramName, (IntPtr)HDSPUtils.SizeOf(result.GetType()), gcHandle.AddrOfPinnedObject(), out sizeRet);
                ComputeException.ThrowOnError(error);
            }
            finally
            {
                result = (QueriedType)gcHandle.Target;
                gcHandle.Free();
            }
            return(result);
        }
コード例 #25
0
        /// <summary>
        /// Added by Hybrid DSP
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="destinationHandle">The destination.</param>
        /// <param name="blocking">if set to <c>true</c> [blocking].</param>
        /// <param name="destinationOffset">The destination offset.</param>
        /// <param name="region">The region.</param>
        /// <param name="source">The source.</param>
        /// <param name="events">The events.</param>
        public void WriteEx <T>(CLMemoryHandle destinationHandle, bool blocking, long destinationOffset, long region, IntPtr source, 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;

            ComputeErrorCode error = CL10.EnqueueWriteBuffer(Handle, destinationHandle, blocking, new IntPtr(destinationOffset * sizeofT), new IntPtr(region * sizeofT), source, eventWaitListSize, eventHandles, newEventHandle);

            ComputeException.ThrowOnError(error);

            if (eventsWritable)
            {
                events.Add(new ComputeEvent(newEventHandle[0], this));
            }
        }
コード例 #26
0
        /// <summary>
        /// Enqueues a command to unmap a buffer or a <see cref="ComputeImage"/> from the host address space.
        /// </summary>
        /// <param name="memory"> The <see cref="ComputeMemory"/>. </param>
        /// <param name="mappedPtr"> The host address returned by a previous call to <see cref="ComputeCommandQueue.Map"/>. This pointer is <c>IntPtr.Zero</c> after this method returns. </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 Unmap(ComputeMemory memory, ref IntPtr mappedPtr, 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.EnqueueUnmapMemObject(Handle, memory.Handle, mappedPtr, eventWaitListSize, eventHandles, newEventHandle);

            ComputeException.ThrowOnError(error);

            mappedPtr = IntPtr.Zero;

            if (eventsWritable)
            {
                events.Add(new ComputeEvent(newEventHandle[0], this));
            }
        }
コード例 #27
0
ファイル: ComputeContext.cs プロジェクト: scenevista/Cloo
        /// <summary>
        /// Creates a new <see cref="ComputeContext"/> on all the <see cref="ComputeDevice"/>s that match the specified <see cref="ComputeDeviceTypes"/>.
        /// </summary>
        /// <param name="deviceType"> A bit-field that identifies the type of <see cref="ComputeDevice"/> to associate with the <see cref="ComputeContext"/>. </param>
        /// <param name="properties"> A <see cref="ComputeContextPropertyList"/> of the <see cref="ComputeContext"/>. </param>
        /// <param name="notify"> A delegate instance that refers to a notification routine. This routine is a callback function that will be used by the OpenCL implementation to report information on errors that occur in the <see cref="ComputeContext"/>. 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 <see cref="ComputeContext"/> is disposed. If <paramref name="notify"/> is <c>null</c>, no callback function is registered. </param>
        /// <param name="userDataPtr"> Optional user data that will be passed to <paramref name="notify"/>. </param>
        public ComputeContext(ComputeDeviceTypes deviceType, ComputeContextPropertyList properties, ComputeContextNotifier notify, IntPtr userDataPtr)
        {
            IntPtr[] propertyArray = (properties != null) ? properties.ToIntPtrArray() : null;
            callback = notify;

            ComputeErrorCode error = ComputeErrorCode.Success;

            Handle = CL12.CreateContextFromType(propertyArray, deviceType, notify, userDataPtr, 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();

            Trace.WriteLine("Create " + this + " in Thread(" + Thread.CurrentThread.ManagedThreadId + ").", "Information");
        }
コード例 #28
0
        /// <summary>
        /// Enqueues a command to copy data from <see cref="ComputeImage"/> to buffer.
        /// </summary>
        /// <param name="source"> The image to copy from. </param>
        /// <param name="destination"> The buffer to copy to. </param>
        /// <param name="sourceOffset"> The <paramref name="source"/> element position where reading starts. </param>
        /// <param name="destinationOffset"> The <paramref name="destination"/> element position where writing starts. </param>
        /// <param name="region"> The region of elements to copy. </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 Copy <T>(ComputeImage source, ComputeBufferBase <T> destination, SysIntX3 sourceOffset, long destinationOffset, SysIntX3 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;

            ComputeErrorCode error = CL10.EnqueueCopyImageToBuffer(Handle, source.Handle, destination.Handle, ref sourceOffset, ref region, new IntPtr(destinationOffset * sizeofT), eventWaitListSize, eventHandles, newEventHandle);

            ComputeException.ThrowOnError(error);

            if (eventsWritable)
            {
                events.Add(new ComputeEvent(newEventHandle[0], this));
            }
        }
コード例 #29
0
        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="MainHandleType"></typeparam>
        /// <typeparam name="SecondHandleType"></typeparam>
        /// <typeparam name="InfoType"></typeparam>
        /// <typeparam name="QueriedType"></typeparam>
        /// <param name="mainHandle"></param>
        /// <param name="secondHandle"></param>
        /// <param name="paramName"></param>
        /// <param name="getInfoDelegate"></param>
        /// <returns></returns>
        protected QueriedType GetInfo <MainHandleType, SecondHandleType, InfoType, QueriedType>
            (MainHandleType mainHandle, SecondHandleType secondHandle, InfoType paramName, GetInfoDelegateEx <MainHandleType, SecondHandleType, InfoType> getInfoDelegate)
            where QueriedType : struct
        {
            QueriedType result   = new QueriedType();
            GCHandle    gcHandle = GCHandle.Alloc(result, GCHandleType.Pinned);

            try
            {
                IntPtr           sizeRet;
                ComputeErrorCode error = getInfoDelegate(mainHandle, secondHandle, paramName, new IntPtr(Marshal.SizeOf(result)), gcHandle.AddrOfPinnedObject(), out sizeRet);
                ComputeException.ThrowOnError(error);
            }
            finally
            {
                result = (QueriedType)gcHandle.Target;
                gcHandle.Free();
            }

            return(result);
        }
コード例 #30
0
        /// <summary>
        /// Creates a new <see cref="ComputeContext"/> on a collection of <see cref="ComputeDevice"/>s.
        /// </summary>
        /// <param name="devices"> A collection of <see cref="ComputeDevice"/>s to associate with the <see cref="ComputeContext"/>. </param>
        /// <param name="properties"> A <see cref="ComputeContextPropertyList"/> of the <see cref="ComputeContext"/>. </param>
        /// <param name="notify"> A delegate instance that refers to a notification routine. This routine is a callback function that will be used by the OpenCL implementation to report information on errors that occur in the <see cref="ComputeContext"/>. 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 <see cref="ComputeContext"/> is disposed. If <paramref name="notify"/> is <c>null</c>, no callback function is registered. </param>
        /// <param name="notifyDataPtr"> Optional user data that will be passed to <paramref name="notify"/>. </param>
        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 = CLInterface.CL12.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();
        }