Beispiel #1
0
        public void Release()
        {
            ClHelper.ThrowNullException(Handle);
            int error = Cl.ReleaseContext(Handle);

            try
            {
                // try to get the ref count
                var test = ReferenceCount;
            }
            catch (OpenCLException)
            {
                // if the context is released ReferenceCount will throw
                var data = CallbackPointers[Handle];
                data.Free();
                CallbackPointers.Remove(Handle);
            }
            finally
            {
                Handle = IntPtr.Zero;
            }

            ClHelper.GetError(error);
        }
Beispiel #2
0
        public static                 Kernel[] CreateKernelsInProgram(Program program)
        {
            if (program == Program.Null)
            {
                throw new ArgumentNullException("context");
            }

            unsafe
            {
                uint num_kernels = 0;
                ClHelper.GetError(Cl.CreateKernelsInProgram(program.Handle, 0, null, &num_kernels));

                IntPtr *kernel_ptrs = stackalloc IntPtr[(int)num_kernels];
                ClHelper.GetError(Cl.CreateKernelsInProgram(program.Handle, num_kernels, kernel_ptrs, null));

                Kernel[] kernels = new Kernel[(int)num_kernels];
                for (int i = 0; i < kernels.Length; ++i)
                {
                    kernels[i] = new Kernel(kernel_ptrs[i]);
                }

                return(kernels);
            }
        }
Beispiel #3
0
        public static Program LinkProgram(Context context, Device[] devices, string options, Program[] programs, Action <Program, object> notify, object user_data)
        {
            if (context == Context.Null)
            {
                throw new ArgumentNullException("context");
            }

            unsafe
            {
                var devices_length = devices == null ? 0 : devices.Length;
                var device_list    = stackalloc IntPtr[devices_length];

                for (int i = 0; i < devices_length; ++i)
                {
                    device_list[i] = devices[i].Handle;
                }

                device_list = devices_length == 0 ? null : device_list;

                int   length = options == null ? 0 : Encoding.ASCII.GetByteCount(options);
                byte *chars  = stackalloc byte[length + 1];

                if (options != null)
                {
                    fixed(char *options_ptr = options)
                    {
                        Encoding.ASCII.GetBytes(options_ptr, options.Length, chars, length);
                    }

                    chars[length] = 0; //null terminator
                }
                else
                {
                    chars = null;
                }

                var num_programs = programs == null ? 0 : programs.Length;

                IntPtr *input_programs = stackalloc IntPtr[num_programs];

                for (int i = 0; i < num_programs; ++i)
                {
                    input_programs[i] = programs[i].Handle;
                }

                var function_ptr = IntPtr.Zero;
                var data_ptr     = new GCHandle();

                if (notify != null)
                {
                    var data = Tuple.Create(notify, user_data);
                    data_ptr = GCHandle.Alloc(data);

                    function_ptr = Marshal.GetFunctionPointerForDelegate(new CallbackDelegate(Callback));
                }

                try
                {
                    int errcode = 0;
                    var program = Cl.LinkProgram(context.Handle, (uint)devices_length, device_list, chars,
                                                 (uint)num_programs, input_programs,
                                                 function_ptr, GCHandle.ToIntPtr(data_ptr).ToPointer(), &errcode);
                    ClHelper.GetError(errcode);

                    return(new Program(program));
                }
                catch (Exception)
                {
                    data_ptr.Free();
                    throw;
                }
            }
        }
Beispiel #4
0
        public void CompileProgram(Device[] devices, string options, Tuple <Program, string>[] headers, Action <Program, object> notify, object user_data)
        {
            unsafe
            {
                var devices_length = devices == null ? 0 : devices.Length;
                var device_list    = stackalloc IntPtr[devices_length];

                for (int i = 0; i < devices_length; ++i)
                {
                    device_list[i] = devices[i].Handle;
                }

                device_list = devices_length == 0 ? null : device_list;

                int   length = options == null ? 0 : Encoding.ASCII.GetByteCount(options);
                byte *chars  = stackalloc byte[length + 1];

                if (options != null)
                {
                    fixed(char *options_ptr = options)
                    {
                        Encoding.ASCII.GetBytes(options_ptr, options.Length, chars, length);
                    }

                    chars[length] = 0; //null terminator
                }
                else
                {
                    chars = null;
                }

                var num_headers = headers == null ? 0 : headers.Length;

                IntPtr *input_headers        = stackalloc IntPtr[num_headers];
                byte ** header_include_names = stackalloc byte *[num_headers];

                for (int i = 0; i < num_headers; ++i)
                {
                    input_headers[i] = headers[i].Item1.Handle;

                    var   name          = headers[i].Item2;
                    var   header_length = Encoding.ASCII.GetByteCount(name);
                    byte *header_chars  = (byte *)Marshal.AllocHGlobal(length + 1).ToPointer();
                    fixed(char *name_ptr = name)
                    {
                        Encoding.ASCII.GetBytes(name_ptr, name.Length, header_chars, header_length);
                    }

                    header_chars[header_length] = 0; //null terminator
                    header_include_names[i]     = header_chars;
                }

                if (headers == null)
                {
                    input_headers        = null;
                    header_include_names = null;
                }

                var function_ptr = IntPtr.Zero;
                var data_ptr     = new GCHandle();

                if (notify != null)
                {
                    var data = Tuple.Create(notify, user_data);
                    data_ptr = GCHandle.Alloc(data);

                    function_ptr = Marshal.GetFunctionPointerForDelegate(new CallbackDelegate(Callback));
                }

                try
                {
                    ClHelper.GetError(Cl.CompileProgram(Handle, (uint)devices_length, device_list, chars,
                                                        (uint)num_headers, input_headers, header_include_names,
                                                        function_ptr, GCHandle.ToIntPtr(data_ptr).ToPointer()));
                }
                catch (Exception)
                {
                    data_ptr.Free();
                    throw;
                }
                finally
                {
                    for (int i = 0; i < num_headers; ++i)
                    {
                        Marshal.FreeHGlobal(new IntPtr(header_include_names[i]));
                    }
                }
            }
        }
Beispiel #5
0
 public void RetainImage()
 {
     ClHelper.ThrowNullException(Handle);
     ClHelper.GetError(Cl.RetainMemObject(Handle));
 }
Beispiel #6
0
        public static Image CreateImage3D(Context context, MemoryFlags flags,
                                          ChannelOrder order, ChannelType type, ulong width, ulong height, ulong depth,
                                          ulong rowPitch, ulong slicePitch, IntPtr hostPtr)
        {
            if (context == Context.Null)
            {
                throw new ArgumentNullException("context");
            }

            if (flags.HasFlag(MemoryFlags.WriteOnly) & flags.HasFlag(MemoryFlags.ReadOnly))
            {
                throw new ArgumentException("MemoryFlags.WriteOnly and MemoryFlags.ReadOnly are mutually exclusive.");
            }
            if (flags.HasFlag(MemoryFlags.HostWriteOnly) & flags.HasFlag(MemoryFlags.HostReadOnly))
            {
                throw new ArgumentException("MemoryFlags.HostWriteOnly and MemoryFlags.HostReadOnly are mutually exclusive.");
            }
            if (flags.HasFlag(MemoryFlags.HostWriteOnly) & flags.HasFlag(MemoryFlags.HostNoAccess))
            {
                throw new ArgumentException("MemoryFlags.HostWriteOnly and MemoryFlags.HostNoAccess are mutually exclusive.");
            }
            if (flags.HasFlag(MemoryFlags.HostReadOnly) & flags.HasFlag(MemoryFlags.HostNoAccess))
            {
                throw new ArgumentException("MemoryFlags.HostReadOnly and MemoryFlags.HostNoAccess are mutually exclusive.");
            }

            if (width == 0)
            {
                throw new ArgumentOutOfRangeException("width", width, "width is 0.");
            }
            if (height == 0)
            {
                throw new ArgumentOutOfRangeException("height", height, "height is 0.");
            }
            if (depth == 0)
            {
                throw new ArgumentOutOfRangeException("depth", depth, "depth is 0.");
            }

            if (hostPtr == IntPtr.Zero)
            {
                if (flags.HasFlag(MemoryFlags.UseHostPtr))
                {
                    throw new ArgumentException("MemoryFlags.UseHostPtr is not valid.");
                }
                if (flags.HasFlag(MemoryFlags.CopyHostPtr))
                {
                    throw new ArgumentException("MemoryFlags.CopyHostPtr is not valid.");
                }

                if (rowPitch != 0)
                {
                    throw new ArgumentOutOfRangeException("rowPitch", rowPitch, "rowPitch is not 0.");
                }
                if (slicePitch != 0)
                {
                    throw new ArgumentOutOfRangeException("slicePitch", slicePitch, "slicePitch is not 0.");
                }
            }
            else
            {
                if (!flags.HasFlag(MemoryFlags.UseHostPtr) & !flags.HasFlag(MemoryFlags.CopyHostPtr))
                {
                    throw new ArgumentException("MemoryFlags.UseHostPtr or MemoryFlags.CopyHostPtr is required.");
                }
                if (flags.HasFlag(MemoryFlags.UseHostPtr) & flags.HasFlag(MemoryFlags.CopyHostPtr))
                {
                    throw new ArgumentException("MemoryFlags.UseHostPtr and MemoryFlags.CopyHostPtr are mutually exclusive.");
                }
                if (flags.HasFlag(MemoryFlags.UseHostPtr) & flags.HasFlag(MemoryFlags.AllocHostPtr))
                {
                    throw new ArgumentException("MemoryFlags.UseHostPtr and MemoryFlags.AllocHostPtr are mutually exclusive.");
                }


                if (rowPitch != 0 && rowPitch < width)
                {
                    throw new ArgumentOutOfRangeException("rowPitch", rowPitch, "rowPitch is not 0 and is less than width.");
                }
                if (slicePitch != 0 && slicePitch < height)
                {
                    throw new ArgumentOutOfRangeException("slicePitch", slicePitch, "slicePitch is not 0 and is less than height.");
                }
            }

            unsafe
            {
                Cl.image_format image_format = new Cl.image_format()
                {
                    image_channel_order     = (uint)order,
                    image_channel_data_type = (uint)type,
                };

                int error;
                var handle = Cl.CreateImage3D(
                    context.Handle, (ulong)flags,
                    &image_format,
                    new UIntPtr(width), new UIntPtr(height), new UIntPtr(depth),
                    new UIntPtr(rowPitch), new UIntPtr(slicePitch), hostPtr.ToPointer(), &error);
                ClHelper.GetError(error);

                return(new Image(handle));
            }
        }
Beispiel #7
0
        public Image(Context context, MemoryFlags flags,
                     ChannelOrder order, ChannelType type,
                     ImageType imageType, ulong width, ulong height, ulong depth,
                     ulong arraySize, ulong rowPitch, ulong slicePitch, IntPtr hostPtr)
            : this()
        {
            if (context == Context.Null)
            {
                throw new ArgumentNullException("context");
            }

            if (flags.HasFlag(MemoryFlags.WriteOnly) & flags.HasFlag(MemoryFlags.ReadOnly))
            {
                throw new ArgumentException("MemoryFlags.WriteOnly and MemoryFlags.ReadOnly are mutually exclusive.");
            }
            if (flags.HasFlag(MemoryFlags.HostWriteOnly) & flags.HasFlag(MemoryFlags.HostReadOnly))
            {
                throw new ArgumentException("MemoryFlags.HostWriteOnly and MemoryFlags.HostReadOnly are mutually exclusive.");
            }
            if (flags.HasFlag(MemoryFlags.HostWriteOnly) & flags.HasFlag(MemoryFlags.HostNoAccess))
            {
                throw new ArgumentException("MemoryFlags.HostWriteOnly and MemoryFlags.HostNoAccess are mutually exclusive.");
            }
            if (flags.HasFlag(MemoryFlags.HostReadOnly) & flags.HasFlag(MemoryFlags.HostNoAccess))
            {
                throw new ArgumentException("MemoryFlags.HostReadOnly and MemoryFlags.HostNoAccess are mutually exclusive.");
            }

            if (width == 0)
            {
                throw new ArgumentOutOfRangeException("width", width, "width is 0.");
            }
            if (height == 0)
            {
                throw new ArgumentOutOfRangeException("height", height, "height is 0.");
            }
            if (depth == 0)
            {
                throw new ArgumentOutOfRangeException("depth", depth, "depth is 0.");
            }

            if (hostPtr == IntPtr.Zero)
            {
                if (flags.HasFlag(MemoryFlags.UseHostPtr))
                {
                    throw new ArgumentException("MemoryFlags.UseHostPtr is not valid.");
                }
                if (flags.HasFlag(MemoryFlags.CopyHostPtr))
                {
                    throw new ArgumentException("MemoryFlags.CopyHostPtr is not valid.");
                }

                if (rowPitch != 0)
                {
                    throw new ArgumentOutOfRangeException("rowPitch", rowPitch, "rowPitch is not 0.");
                }
                if (slicePitch != 0)
                {
                    throw new ArgumentOutOfRangeException("slicePitch", slicePitch, "slicePitch is not 0.");
                }
            }
            else
            {
                if (!flags.HasFlag(MemoryFlags.UseHostPtr) & !flags.HasFlag(MemoryFlags.CopyHostPtr))
                {
                    throw new ArgumentException("MemoryFlags.UseHostPtr or MemoryFlags.CopyHostPtr is required.");
                }
                if (flags.HasFlag(MemoryFlags.UseHostPtr) & flags.HasFlag(MemoryFlags.CopyHostPtr))
                {
                    throw new ArgumentException("MemoryFlags.UseHostPtr and MemoryFlags.CopyHostPtr are mutually exclusive.");
                }
                if (flags.HasFlag(MemoryFlags.UseHostPtr) & flags.HasFlag(MemoryFlags.AllocHostPtr))
                {
                    throw new ArgumentException("MemoryFlags.UseHostPtr and MemoryFlags.AllocHostPtr are mutually exclusive.");
                }


                if (rowPitch != 0 && rowPitch < width)
                {
                    throw new ArgumentOutOfRangeException("rowPitch", rowPitch, "rowPitch is not 0 and is less than width.");
                }
                if (slicePitch != 0 && slicePitch < height)
                {
                    throw new ArgumentOutOfRangeException("slicePitch", slicePitch, "slicePitch is not 0 and is less than height.");
                }
            }

            unsafe
            {
                Cl.image_format image_format = new Cl.image_format()
                {
                    image_channel_order     = (uint)order,
                    image_channel_data_type = (uint)type,
                };

                Cl.image_desc image_desc = new Cl.image_desc()
                {
                    image_type        = (uint)imageType,
                    image_width       = new UIntPtr(width),
                    image_height      = new UIntPtr(height),
                    image_depth       = new UIntPtr(depth),
                    image_array_size  = new UIntPtr(arraySize),
                    image_row_pitch   = new UIntPtr(rowPitch),
                    image_slice_pitch = new UIntPtr(slicePitch),
                    num_mip_level     = 0,
                    num_samples       = 0,
                    buffer            = IntPtr.Zero,
                };

                int error;
                Handle = Cl.CreateImage(
                    context.Handle, (ulong)flags,
                    &image_format, &image_desc, hostPtr.ToPointer(), &error);
                ClHelper.GetError(error);
            }
        }
Beispiel #8
0
        public Event EnqueueKernel(Kernel kernel, ulong[] global_work_offset, ulong[] global_work_size, ulong[] local_work_size, Event[] events)
        {
            ClHelper.ThrowNullException(Handle);

            if (kernel == Kernel.Null)
            {
                throw new ArgumentNullException("kernel");
            }
            if (global_work_size == null)
            {
                throw new ArgumentNullException("global_work_size");
            }
            if (global_work_offset != null && global_work_size.Length != global_work_offset.Length)
            {
                throw new ArgumentException("global_work_size and global_work_offset must be the same length.");
            }
            if (local_work_size != null && global_work_size.Length != local_work_size.Length)
            {
                throw new ArgumentException("global_work_size and local_work_size must be the same length.");
            }

            int work_dim = global_work_size.Length;

            unsafe
            {
                UIntPtr *global_offset = stackalloc UIntPtr[work_dim];
                UIntPtr *global_size   = stackalloc UIntPtr[work_dim];
                UIntPtr *local_size    = stackalloc UIntPtr[work_dim];

                for (int i = 0; i < work_dim; ++i)
                {
                    if (global_work_offset != null)
                    {
                        global_offset[i] = new UIntPtr(global_work_offset[i]);
                    }

                    global_size[i] = new UIntPtr(global_work_size[i]);

                    if (local_work_size != null)
                    {
                        local_size[i] = new UIntPtr(local_work_size[i]);
                    }
                }

                if (global_work_offset == null)
                {
                    global_offset = null;
                }
                if (local_work_size == null)
                {
                    local_size = null;
                }

                int     num_events_in_wait_list = events == null ? 0 : events.Length;
                IntPtr *wait_list = stackalloc IntPtr[num_events_in_wait_list];
                for (int i = 0; i < num_events_in_wait_list; ++i)
                {
                    wait_list[i] = events[i].Handle;
                }
                if (events == null)
                {
                    wait_list = null;
                }

                IntPtr event_ptr = IntPtr.Zero;

                ClHelper.GetError(Cl.EnqueueNDRangeKernel(Handle, kernel.Handle,
                                                          (uint)work_dim, global_offset, global_size, local_size,
                                                          (uint)num_events_in_wait_list, wait_list, &event_ptr));

                return(new Event(event_ptr));
            }
        }
Beispiel #9
0
 public static void UnloadCompiler()
 {
     ClHelper.GetError(Cl.UnloadCompiler());
 }
Beispiel #10
0
 public void Retain()
 {
     ClHelper.ThrowNullException(Handle);
     ClHelper.GetError(Cl.RetainCommandQueue(Handle));
 }
Beispiel #11
0
        public Context(Dictionary <IntPtr, IntPtr> properties, Device[] devices, Action <string, byte[], object> notify, object user_data)
            : this()
        {
            if (devices == null)
            {
                throw new ArgumentNullException("devices");
            }

            unsafe
            {
                IntPtr *device_ptrs = stackalloc IntPtr[devices.Length];

                for (int i = 0; i < devices.Length; ++i)
                {
                    device_ptrs[i] = devices[i].Handle;
                }

                int property_count = properties == null ? 0 : properties.Count;

                IntPtr *properties_ptr = stackalloc IntPtr[property_count * 2 + 1];

                int index = 0;
                if (properties != null)
                {
                    foreach (var pair in properties)
                    {
                        properties_ptr[index++] = pair.Key;
                        properties_ptr[index++] = pair.Value;
                    }
                    properties_ptr[index] = IntPtr.Zero;
                }
                else
                {
                    properties_ptr = null;
                }

                var function_ptr = IntPtr.Zero;
                var data_handle  = new GCHandle();
                var data_ptr     = IntPtr.Zero;

                if (notify != null)
                {
                    var data = Tuple.Create(notify, user_data);
                    data_handle = GCHandle.Alloc(data);
                    data_ptr    = GCHandle.ToIntPtr(data_handle);

                    function_ptr = Marshal.GetFunctionPointerForDelegate(new CallbackDelegete(Callback));
                }

                try
                {
                    int errcode = 0;
                    Handle = Cl.CreateContext(properties_ptr, (uint)devices.Length, device_ptrs,
                                              function_ptr, data_ptr.ToPointer(), &errcode);
                    ClHelper.GetError(errcode);

                    CallbackPointers.Add(Handle, data_handle);
                }
                catch (Exception)
                {
                    if (data_handle.IsAllocated)
                    {
                        data_handle.Free();
                    }
                    throw;
                }
            }
        }
Beispiel #12
0
 public void Retain()
 {
     ClHelper.ThrowNullException(Handle);
     ClHelper.GetError(Cl.RetainKernel(Handle));
 }
Beispiel #13
0
 public void UnloadPlatformCompiler()
 {
     ClHelper.ThrowNullException(Handle);
     ClHelper.GetError(Cl.UnloadPlatformCompiler(Handle));
 }
Beispiel #14
0
 public void RetainProgram()
 {
     ClHelper.ThrowNullException(Handle);
     ClHelper.GetError(Cl.RetainProgram(Handle));
 }
Beispiel #15
0
 public void Finish()
 {
     ClHelper.ThrowNullException(Handle);
     ClHelper.GetError(Cl.Finish(Handle));
 }
Beispiel #16
0
        public Program(Context context, Device[] devices, byte[][] binaries)
            : this()
        {
            if (context == Context.Null)
            {
                throw new ArgumentNullException("context");
            }
            if (devices == null)
            {
                throw new ArgumentNullException("devices");
            }
            if (binaries == null)
            {
                throw new ArgumentNullException("binaries");
            }
            if (devices.Length == 0)
            {
                throw new ArgumentException("devices is empty.", "devices");
            }
            if (devices.Length != binaries.Length)
            {
                throw new ArgumentException("binaries must be the same length as devices.", "binaries");
            }

            unsafe
            {
                var device_list = stackalloc IntPtr[devices.Length];

                for (int i = 0; i < devices.Length; ++i)
                {
                    device_list[i] = devices[i].Handle;
                }

                var lengths_list  = stackalloc UIntPtr[binaries.Length];
                var binary_list   = stackalloc byte *[binaries.Length];
                var binary_status = stackalloc int[binaries.Length];

                for (int i = 0; i < binaries.Length; ++i)
                {
                    lengths_list[i] = new UIntPtr((uint)binaries[i].Length);

                    var handle = GCHandle.Alloc(binaries[i], GCHandleType.Pinned);
                    binary_list[i] = (byte *)GCHandle.ToIntPtr(handle).ToPointer();
                }

                int errcode = 0;
                Handle = Cl.CreateProgramWithBinary(context.Handle,
                                                    (uint)devices.Length, device_list, lengths_list, binary_list, binary_status, &errcode);

                for (int i = 0; i < binaries.Length; ++i)
                {
                    var handel = GCHandle.FromIntPtr(new IntPtr(binary_list[i]));
                    handel.Free();
                }

                List <int> errors = new List <int>(binaries.Length);

                for (int i = 0; i < binaries.Length; ++i)
                {
                    if (binary_status[i] != Cl.SUCCESS)
                    {
                        errors.Add(i);
                    }
                }

                if (errors.Count != 0)
                {
                    throw new OpenCLException(string.Format("{0} {1} failed.",
                                                            errors.Count == 1 ? "Binary" : "Binaries",
                                                            string.Join(", ", errors)));
                }

                ClHelper.GetError(errcode);
            }
        }
Beispiel #17
0
        public Buffer(Context context, MemoryFlags flags, ulong size, IntPtr hostPtr)
            : this()
        {
            if (context == Context.Null)
            {
                throw new ArgumentNullException("context");
            }

            if (flags.HasFlag(MemoryFlags.WriteOnly) & flags.HasFlag(MemoryFlags.ReadOnly))
            {
                throw new ArgumentException("MemoryFlags.WriteOnly and MemoryFlags.ReadOnly are mutually exclusive.");
            }
            if (flags.HasFlag(MemoryFlags.HostWriteOnly) & flags.HasFlag(MemoryFlags.HostReadOnly))
            {
                throw new ArgumentException("MemoryFlags.HostWriteOnly and MemoryFlags.HostReadOnly are mutually exclusive.");
            }
            if (flags.HasFlag(MemoryFlags.HostWriteOnly) & flags.HasFlag(MemoryFlags.HostNoAccess))
            {
                throw new ArgumentException("MemoryFlags.HostWriteOnly and MemoryFlags.HostNoAccess are mutually exclusive.");
            }
            if (flags.HasFlag(MemoryFlags.HostReadOnly) & flags.HasFlag(MemoryFlags.HostNoAccess))
            {
                throw new ArgumentException("MemoryFlags.HostReadOnly and MemoryFlags.HostNoAccess are mutually exclusive.");
            }

            if (hostPtr == IntPtr.Zero)
            {
                if (flags.HasFlag(MemoryFlags.UseHostPtr))
                {
                    throw new ArgumentException("MemoryFlags.UseHostPtr is not valid.");
                }
                if (flags.HasFlag(MemoryFlags.CopyHostPtr))
                {
                    throw new ArgumentException("MemoryFlags.CopyHostPtr is not valid.");
                }
            }
            else
            {
                if (!flags.HasFlag(MemoryFlags.UseHostPtr) & !flags.HasFlag(MemoryFlags.CopyHostPtr))
                {
                    throw new ArgumentException("MemoryFlags.UseHostPtr or MemoryFlags.CopyHostPtr is required.");
                }
                if (flags.HasFlag(MemoryFlags.UseHostPtr) & flags.HasFlag(MemoryFlags.CopyHostPtr))
                {
                    throw new ArgumentException("MemoryFlags.UseHostPtr and MemoryFlags.CopyHostPtr are mutually exclusive.");
                }
                if (flags.HasFlag(MemoryFlags.UseHostPtr) & flags.HasFlag(MemoryFlags.AllocHostPtr))
                {
                    throw new ArgumentException("MemoryFlags.UseHostPtr and MemoryFlags.AllocHostPtr are mutually exclusive.");
                }
            }

            if (size == 0)
            {
                throw new ArgumentOutOfRangeException("size", size, "size is 0.");
            }

            unsafe
            {
                int error;
                Handle = Cl.CreateBuffer(context.Handle, (ulong)flags, new UIntPtr(size), hostPtr.ToPointer(), &error);
                ClHelper.GetError(error);
            }
        }