Esempio n. 1
0
        /// <summary>
        /// Reads the specified memory object associated with this command queue.
        /// </summary>
        /// <param name="memoryObject">The memory object that is to be read.</param>
        /// <param name="outputSize">The number of array elements that are to be returned.</param>
        /// <typeparam name="T">The type of the array that is to be returned.</typeparam>
        /// <returns>Returns the value of the memory object.</param>
        public void EnqueueWriteBuffer <T>(MemoryObject memoryObject, T[] value) where T : struct
        {
            // Tries to read the memory object
            GCHandle h = GCHandle.Alloc(value, GCHandleType.Pinned);
            IntPtr   resultValuePointer = h.AddrOfPinnedObject();

            try
            {
                // Allocates enough memory for the result value
                int size = Marshal.SizeOf <T>() * value.Length;
                // Reads the memory object, by enqueuing the read operation to the command queue
                IntPtr waitEventPointer;
                Result result = EnqueuedCommandsNativeApi.EnqueueWriteBuffer(Handle, memoryObject.Handle, 1,
                                                                             UIntPtr.Zero,
                                                                             new UIntPtr((uint)size), resultValuePointer, 0, null, out waitEventPointer);

                // Checks if the read operation was queued successfuly, if not, an exception is thrown
                if (result != Result.Success)
                {
                    throw new OpenClException("The memory object could not be written to.", result);
                }
            }
            finally
            {
                // Finally the allocated memory has to be freed
                if (resultValuePointer != IntPtr.Zero)
                {
                    h.Free();
                }
            }
        }
Esempio n. 2
0
        public void EnqueueWriteBuffer <T>(MemoryObject memoryObject, T[] buffer, int length)
        {
#if UNSAFE
            switch (buffer)
            {
            case long[] longArray:
                unsafe
                {
                    fixed(long *longPtr = longArray)
                    {
                        Result result = EnqueuedCommandsNativeApi.EnqueueWriteBuffer(this.Handle, memoryObject.Handle, 1, new UIntPtr(0), new UIntPtr((uint)(length * Marshal.SizeOf <T>())), (IntPtr)((void *)longPtr), 0, null, out IntPtr waitEventPointer);

                        // Checks if the read operation was queued successfuly, if not, an exception is thrown
                        if (result != Result.Success)
                        {
                            throw new OpenClException("The memory object could not be read.", result);
                        }
                    }
                }
                break;

            default:
                byte[] tempBuffer = new byte[length * Marshal.SizeOf <T>()];
                Buffer.BlockCopy(buffer, 0, tempBuffer, 0, tempBuffer.Length);

                IntPtr bufferPtr = Marshal.AllocHGlobal(tempBuffer.Length);
                try
                {
                    Marshal.Copy(tempBuffer, 0, bufferPtr, tempBuffer.Length);

                    Result result = EnqueuedCommandsNativeApi.EnqueueWriteBuffer(this.Handle, memoryObject.Handle, 1, new UIntPtr(0), new UIntPtr((uint)tempBuffer.Length), bufferPtr, 0, null, out IntPtr waitEventPointer);

                    // Checks if the read operation was queued successfuly, if not, an exception is thrown
                    if (result != Result.Success)
                    {
                        throw new OpenClException("The memory object could not be read.", result);
                    }
                }
                finally { Marshal.FreeHGlobal(bufferPtr); }
                break;
            }
#else
            byte[] tempBuffer = new byte[length * Marshal.SizeOf <T>()];
            Buffer.BlockCopy(buffer, 0, tempBuffer, 0, tempBuffer.Length);

            IntPtr bufferPtr = Marshal.AllocHGlobal(tempBuffer.Length);
            try
            {
                Marshal.Copy(tempBuffer, 0, bufferPtr, tempBuffer.Length);

                Result result = EnqueuedCommandsNativeApi.EnqueueWriteBuffer(this.Handle, memoryObject.Handle, 1, new UIntPtr(0), new UIntPtr((uint)tempBuffer.Length), bufferPtr, 0, null, out IntPtr waitEventPointer);

                // Checks if the read operation was queued successfuly, if not, an exception is thrown
                if (result != Result.Success)
                {
                    throw new OpenClException("The memory object could not be read.", result);
                }
            }
#endif
        }
        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)"));
        }
Esempio n. 6
0
        public void EnqueueWriteBufferEdges(MemoryObject memoryObject, long[] edges)
        {
            IntPtr waitEventPointer = IntPtr.Zero;
            IntPtr edgesPtr         = Marshal.AllocHGlobal(8 * edges.Length);

            try
            {
                Marshal.Copy(edges, 0, edgesPtr, edges.Length);

                Result result = EnqueuedCommandsNativeApi.EnqueueWriteBuffer(this.Handle, memoryObject.Handle, 1, new UIntPtr((uint)0), new UIntPtr((uint)edges.Length * 8), edgesPtr, 0, null, waitEventPointer);

                // Checks if the read operation was queued successfuly, if not, an exception is thrown
                if (result != Result.Success)
                {
                    throw new OpenClException("The memory object could not be read.", result);
                }
            }
            finally
            {
                Marshal.FreeHGlobal(edgesPtr);
            }
        }