Exemple #1
0
        /// <summary>
        ///     Retrieves the specified information about the OpenCL event.
        /// </summary>
        /// <typeparam name="T">
        ///     The type of the data that is to be returned.</param>
        ///     <param name="eventInformation">The kind of information that is to be retrieved.</param>
        ///     <exception cref="OpenClException">
        ///         If the information could not be retrieved, then an <see cref="OpenClException" />
        ///         is thrown.
        ///     </exception>
        ///     <returns>Returns the specified information.</returns>
        private T GetEventInformation <T>(EventInformation eventInformation)
        {
            // Retrieves the size of the return value in bytes, this is used to later get the full information
            UIntPtr returnValueSize;
            Result  result =
                EventsNativeApi.GetEventInformation(Handle, eventInformation, UIntPtr.Zero, null, out returnValueSize);

            if (result != Result.Success)
            {
                throw new OpenClException("The event information could not be retrieved.", result);
            }

            // Allocates enough memory for the return value and retrieves it
            byte[] output = new byte[returnValueSize.ToUInt32()];
            result = EventsNativeApi.GetEventInformation(
                Handle,
                eventInformation,
                new UIntPtr((uint)output.Length),
                output,
                out returnValueSize
                );
            if (result != Result.Success)
            {
                throw new OpenClException("The event information could not be retrieved.", result);
            }

            // Returns the output
            return(InteropConverter.To <T>(output));
        }
 /// <summary>
 /// Initializes a new <see cref="AwaitableEvent"/> instance.
 /// </summary>
 /// <param name="handle">The handle to the OpenCL event.</param>
 public AwaitableEvent(IntPtr handle)
     : base(handle)
 {
     // Subscribes to the event callbacks of the OpenCL event, so that a CLR event can be raised
     EventsNativeApi.SetEventCallback(
         this.Handle,
         (int)CommandExecutionStatus.Queued,
         Marshal.GetFunctionPointerForDelegate(new AwaitableEventCallback((waitEvent, userData) => this.OnQueued?.Invoke(this, new EventArgs()))),
         IntPtr.Zero);
     EventsNativeApi.SetEventCallback(
         this.Handle,
         (int)CommandExecutionStatus.Submitted,
         Marshal.GetFunctionPointerForDelegate(new AwaitableEventCallback((waitEvent, userData) => this.OnSubmitted?.Invoke(this, new EventArgs()))),
         IntPtr.Zero);
     EventsNativeApi.SetEventCallback(
         this.Handle,
         (int)CommandExecutionStatus.Running,
         Marshal.GetFunctionPointerForDelegate(new AwaitableEventCallback((waitEvent, userData) => this.OnRunning?.Invoke(this, new EventArgs()))),
         IntPtr.Zero);
     EventsNativeApi.SetEventCallback(
         this.Handle,
         (int)CommandExecutionStatus.Complete,
         Marshal.GetFunctionPointerForDelegate(new AwaitableEventCallback((waitEvent, userData) => this.OnCompleted?.Invoke(this, new EventArgs()))),
         IntPtr.Zero);
 }
        /// <summary>
        /// Disposes of the resources that have been acquired by the event.
        /// </summary>
        /// <param name="disposing">Determines whether managed object or managed and unmanaged resources should be disposed of.</param>
        protected override void Dispose(bool disposing)
        {
            // Checks if the event has already been disposed of, if not, then it is disposed of
            if (!this.IsDisposed)
            {
                EventsNativeApi.ReleaseEvent(this.Handle);
            }

            // Makes sure that the base class can execute its dispose logic
            base.Dispose(disposing);
        }
Exemple #4
0
        /// <summary>
        ///     Disposes of the resources that have been acquired by the event.
        /// </summary>
        /// <param name="disposing">Determines whether managed object or managed and unmanaged resources should be disposed of.</param>
        public override void Dispose()
        {
            // Checks if the event has already been disposed of, if not, then it is disposed of
            if (!IsDisposed)
            {
                EventsNativeApi.ReleaseEvent(Handle);
            }

            // Makes sure that the base class can execute its dispose logic
            base.Dispose();
        }
        public void EnqueueKernel(string kernelName, IntPtr[] globalWorkSize, IntPtr[] localWorkSize)
        {
            IntPtr ev = IntPtr.Zero;
            var    errCodeRunKernel = EnqueuedCommandsNativeApi.EnqueueNDRangeKernel(commandQueue, kernels[kernelName], (uint)globalWorkSize.Length, null, globalWorkSize, localWorkSize, 0, null, out ev);

            ThrowOnError(errCodeRunKernel, String.Format("Failed to enqueue kernel {0}.", kernelName));

            var errCodeEv = EventsNativeApi.ReleaseEvent(ev);

            ThrowOnError(errCodeEv, String.Format("Failed release event (EnqueueNDRangeKernel)"));
        }
        public void ReadBuffer(MemoryAllocation mem, bool isBlocking, UIntPtr offset, UIntPtr lengthInBytes, IntPtr output)
        {
            IntPtr ev          = IntPtr.Zero;
            var    errCodeRead = EnqueuedCommandsNativeApi.EnqueueReadBuffer(commandQueue, mem.buffer, isBlocking ? 1U : 0U, offset, lengthInBytes, output, 0, null, out ev);

            ThrowOnError(errCodeRead, String.Format("Failed to enqueue read buffer. Read Size: {0}, Buffer size: {1}", lengthInBytes, mem.bufferSizeInBytes));

            var errCodeEv = EventsNativeApi.ReleaseEvent(ev);

            ThrowOnError(errCodeEv, String.Format("Failed release event (EnqueueReadBuffer)"));
        }
        public void UploadToMemory(MemoryAllocation mem, int mem_offset, int sizeInBytes, IntPtr data, bool IsBlocking)
        {
            IntPtr ev           = IntPtr.Zero;
            var    errCodeWrite = EnqueuedCommandsNativeApi.EnqueueWriteBuffer(commandQueue, mem.buffer, IsBlocking ? 1U : 0U, new UIntPtr((uint)mem_offset * 4U), new UIntPtr((uint)sizeInBytes), data, 0, null, out ev);

            ThrowOnError(errCodeWrite, String.Format("Failed to enqueue write buffer. Write-size:{0}, Target buffer size: {1}", sizeInBytes, mem.bufferSizeInBytes));

            var errCodeEv = EventsNativeApi.ReleaseEvent(ev);

            ThrowOnError(errCodeEv, String.Format("Failed release event (EnqueueWriteBuffer, UploadToMemory_1)"));
        }
        public MemoryAllocation GetMemoryFor(int requiredSizeInBytes, MemoryFlag flags, IntPtr data, int data_size_in_bytes = -1)
        {
            if (!IsInitialized())
            {
                return(null);
            }

            var accessFlags = flags & (MemoryFlag.ReadOnly | MemoryFlag.WriteOnly | MemoryFlag.ReadWrite);

            MemoryAllocation candidate        = null;
            uint             bestMatchingSize = uint.MaxValue;
            int itemToSwap = -1;

            for (int i = 0; i < freeMemoryAllocations.Count; i++)
            {
                var item = freeMemoryAllocations[i];
                if (item.flags == accessFlags && item.bufferSizeInBytes >= requiredSizeInBytes && item.bufferSizeInBytes < bestMatchingSize) //Select the smallest sufficient memory allocation from our allocations
                {
                    bestMatchingSize = item.bufferSizeInBytes;
                    candidate        = item;
                    itemToSwap       = i;
                    if (item.bufferSizeInBytes == requiredSizeInBytes)
                    {
                        break;
                    }
                }
            }

            if (candidate == null)
            {
                candidate = CreateMemoryAllocation(clContext, (uint)requiredSizeInBytes, accessFlags, IntPtr.Zero);
                usedMemoryAllocations.Add(candidate);
            }
            else
            {
                freeMemoryAllocations.RemoveAt(itemToSwap);
                usedMemoryAllocations.Add(candidate);
            }

            if (flags.HasFlag(MemoryFlag.CopyHostPointer) && data != IntPtr.Zero)
            {
                int upload_size_in_bytes = data_size_in_bytes >= 0 ? data_size_in_bytes : requiredSizeInBytes;

                IntPtr ev           = IntPtr.Zero;
                var    errCodeWrite = EnqueuedCommandsNativeApi.EnqueueWriteBuffer(commandQueue, candidate.buffer, 0U, UIntPtr.Zero, new UIntPtr((uint)upload_size_in_bytes), data, 0, null, out ev);
                ThrowOnError(errCodeWrite, String.Format("Failed to enqueue write buffer. Write-size:{0}, Target buffer size: {1}", requiredSizeInBytes, candidate.bufferSizeInBytes));

                var errCodeEv = EventsNativeApi.ReleaseEvent(ev);
                ThrowOnError(errCodeEv, String.Format("Failed release event (EnqueueWriteBuffer)"));
            }

            return(candidate);
        }
        public void UploadToMemory(MemoryAllocation mem, int mem_offset, int data_offset, float[] data, bool IsBlocking, int size = -1)
        {
            uint   uploadSize = size < 0 ? ((uint)data.Length * 4U) : (uint)size * 4U;
            IntPtr ev         = IntPtr.Zero;
            Result errCodeWrite;

            unsafe
            {
                fixed(float *dataPtr = data)
                {
                    errCodeWrite = EnqueuedCommandsNativeApi.EnqueueWriteBuffer(commandQueue, mem.buffer, IsBlocking ? 1U : 0U, new UIntPtr((uint)mem_offset), new UIntPtr(uploadSize), new IntPtr(dataPtr + data_offset), 0, null, out ev);
                }
            }
            ThrowOnError(errCodeWrite, String.Format("Failed to enqueue write buffer. Write-size:{0}, Target buffer size: {1}, data_offset:{2}", uploadSize, mem.bufferSizeInBytes, data_offset * 4));

            var errCodeEv = EventsNativeApi.ReleaseEvent(ev);

            ThrowOnError(errCodeEv, String.Format("Failed to release event (EnqueueWriteBuffer, UploadToMemory_3)"));
        }