/// <summary> /// Creates a new memory buffer with the specified flags for the specified array. The size of memory 1allocated for the memory buffer is determined by <see cref="T"/> and the number of elements in the array. /// </summary> /// <typeparam name="T">The size of the memory buffer will be determined by the structure specified in the type parameter.</typeparam> /// <param name="memoryFlags">The flags, that determines the how the memory buffer is created and how it can be accessed.</param> /// <param name="value">The value that is to be copied over to the device.</param> /// <exception cref="OpenClException">If the memory buffer could not be created, then an <see cref="OpenClException"/> is thrown.</exception> /// <returns>Returns the created memory buffer.</returns> public MemoryBuffer CreateBuffer <T>(OpenCl.DotNetCore.Memory.MemoryFlag memoryFlags, T[] value) where T : struct { return(CreateBuffer(memoryFlags, typeof(T), Array.ConvertAll(value, x => (object)x))); //// Tries to create the memory buffer, if anything goes wrong, then it is crucial to free the allocated memory //IntPtr hostBufferPointer = IntPtr.Zero; //try //{ // // Determines the size of the specified value and creates a pointer that points to the data inside the structure // int size = Marshal.SizeOf<T>() * value.Length; // hostBufferPointer = Marshal.AllocHGlobal(size); // for (int i = 0; i < value.Length; i++) // Marshal.StructureToPtr(value[i], IntPtr.Add(hostBufferPointer, i * Marshal.SizeOf<T>()), false); // // Creates a new memory buffer for the specified value // Result result; // IntPtr memoryBufferPointer = MemoryNativeApi.CreateBuffer(this.Handle, (Interop.Memory.MemoryFlag)memoryFlags, new UIntPtr((uint)size), hostBufferPointer, out result); // // Checks if the creation of the memory buffer was successful, if not, then an exception is thrown // if (result != Result.Success) // throw new OpenClException("The memory buffer could not be created.", result); // // Creates the memory buffer from the pointer to the memory buffer and returns it // return new MemoryBuffer(memoryBufferPointer); //} //finally //{ // // Deallocates the host memory allocated for the value // if (hostBufferPointer != IntPtr.Zero) // Marshal.FreeHGlobal(hostBufferPointer); //} }
/// <summary> /// Creates a new memory buffer with the specified flags and of the specified size. /// </summary> /// <param name="memoryFlags">The flags, that determines the how the memory buffer is created and how it can be accessed.</param> /// <param name="size">The size of memory that should be allocated for the memory buffer.</param> /// <exception cref="OpenClException">If the memory buffer could not be created, then an <see cref="OpenClException"/> is thrown.</exception> /// <returns>Returns the created memory buffer.</returns> public MemoryBuffer CreateBuffer(OpenCl.DotNetCore.Memory.MemoryFlag memoryFlags, long size) { // Creates a new memory buffer of the specified size and with the specified memory flags Result result; IntPtr memoryBufferPointer = MemoryNativeApi.CreateBuffer(this.Handle, (Interop.Memory.MemoryFlag)memoryFlags, new UIntPtr((ulong)size), IntPtr.Zero, out result); // Checks if the creation of the memory buffer was successful, if not, then an exception is thrown if (result != Result.Success) { throw new OpenClException("The memory buffer could not be created.", result); } // Creates the memory buffer from the pointer to the memory buffer and returns it return(new MemoryBuffer(memoryBufferPointer)); }
public MemoryBuffer CreateBuffer(OpenCl.DotNetCore.Memory.MemoryFlag memoryFlags, Type t, object[] value) { // Tries to create the memory buffer, if anything goes wrong, then it is crucial to free the allocated memory IntPtr hostBufferPointer = IntPtr.Zero; try { // Determines the size of the specified value and creates a pointer that points to the data inside the structure int size = Marshal.SizeOf(t) * value.Length; hostBufferPointer = Marshal.AllocHGlobal(size); for (int i = 0; i < value.Length; i++) { Marshal.StructureToPtr(value[i], IntPtr.Add(hostBufferPointer, i * Marshal.SizeOf(t)), false); } // Creates a new memory buffer for the specified value Result result; IntPtr memoryBufferPointer = MemoryNativeApi.CreateBuffer(this.Handle, (Interop.Memory.MemoryFlag)memoryFlags, new UIntPtr((uint)size), hostBufferPointer, out result); // Checks if the creation of the memory buffer was successful, if not, then an exception is thrown if (result != Result.Success) { throw new OpenClException("The memory buffer could not be created.", result); } // Creates the memory buffer from the pointer to the memory buffer and returns it return(new MemoryBuffer(memoryBufferPointer)); } finally { // Deallocates the host memory allocated for the value if (hostBufferPointer != IntPtr.Zero) { Marshal.FreeHGlobal(hostBufferPointer); } } }
/// <summary> /// Creates a new memory buffer with the specified flags. The size of memory allocated for the memory buffer is determined by <see cref="T"/> and the number of elements. /// </summary> /// <typeparam name="T">The size of the memory buffer will be determined by the structure specified in the type parameter.</typeparam> /// <param name="memoryFlags">The flags, that determines the how the memory buffer is created and how it can be accessed.</param> /// <exception cref="OpenClException">If the memory buffer could not be created, then an <see cref="OpenClException"/> is thrown.</exception> /// <returns>Returns the created memory buffer.</returns> public MemoryBuffer CreateBuffer <T>(OpenCl.DotNetCore.Memory.MemoryFlag memoryFlags, long size) where T : struct => this.CreateBuffer(memoryFlags, Marshal.SizeOf <T>() * size);