/// <summary> /// Retrieves the specified information about the OpenCL memory object. /// </summary> /// <typeparam name="T"> /// The type of the data that is to be returned.</param> /// <param name="memoryObjectInformation">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 GetMemoryObjectInformation <T>(MemoryObjectInformation memoryObjectInformation) { // Retrieves the size of the return value in bytes, this is used to later get the full information Result result = MemoryNativeApi.GetMemoryObjectInformation( Handle, memoryObjectInformation, UIntPtr.Zero, null, out UIntPtr returnValueSize ); if (result != Result.Success) { throw new OpenClException("The memory object information could not be retrieved.", result); } // Allocates enough memory for the return value and retrieves it byte[] output = new byte[returnValueSize.ToUInt32()]; result = MemoryNativeApi.GetMemoryObjectInformation( Handle, memoryObjectInformation, new UIntPtr((uint)output.Length), output, out returnValueSize ); if (result != Result.Success) { throw new OpenClException("The memory object information could not be retrieved.", result); } // Returns the output return(InteropConverter.To <T>(output)); }
internal void Release() { if (buffer != IntPtr.Zero) { allocatedMemory -= bufferSizeInBytes; MemoryNativeApi.ReleaseMemoryObject(buffer); buffer = IntPtr.Zero; } }
/// <summary> /// Disposes of the resources that have been acquired by the memory object. /// </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 memory object has already been disposed of, if not, then the memory object is disposed of if (!IsDisposed) { MemoryNativeApi.ReleaseMemoryObject(Handle); } // Makes sure that the base class can execute its dispose logic base.Dispose(); }
/// <summary> /// Disposes of the resources that have been acquired by the memory object. /// </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 memory object has already been disposed of, if not, then the memory object is disposed of if (!this.IsDisposed) { MemoryNativeApi.ReleaseMemoryObject(this.Handle); } // Makes sure that the base class can execute its dispose logic base.Dispose(disposing); }
/// <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(Memory.MemoryFlag memoryFlags, int size, object handleIdentifier) { // Creates a new memory buffer of the specified size and with the specified memory flags IntPtr memoryBufferPointer = MemoryNativeApi.CreateBuffer(Handle, (Interop.Memory.MemoryFlag)memoryFlags, new UIntPtr((uint)size), IntPtr.Zero, out Result 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, handleIdentifier)); }
/// <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 MemoryAllocation CreateMemoryAllocation(IntPtr clContext, uint byteSize, MemoryFlag flags, IntPtr data) { Result err; var buffer = MemoryNativeApi.CreateBuffer(clContext, flags, new UIntPtr(byteSize), data, out err); if (err != Result.Success) { OnLowDeviceMemory(); buffer = MemoryNativeApi.CreateBuffer(clContext, flags, new UIntPtr(byteSize), data, out err); if (err != Result.Success) { ThrowOnError(err, String.Format("Failed to allocate device memory. Size: {0}", byteSize)); } } return(new MemoryAllocation(buffer, byteSize, flags)); }
public MemoryBuffer CreateBuffer(MemoryFlag memoryFlags, Type t, object[] value, object handleIdentifier) { // 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 IntPtr memoryBufferPointer = MemoryNativeApi.CreateBuffer( Handle, (Interop.Memory.MemoryFlag)memoryFlags, new UIntPtr(( uint )size), hostBufferPointer, out Result 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, handleIdentifier)); } finally { // Deallocates the host memory allocated for the value if (hostBufferPointer != IntPtr.Zero) { Marshal.FreeHGlobal(hostBufferPointer); } } }