示例#1
0
文件: Context.cs 项目: ByteChkR/Byt3
        /// <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));
        }
示例#2
0
        /// <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));
        }
示例#4
0
        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);
                }
            }
        }