Example #1
0
        public                             Tuple <ChannelOrder, ChannelType>[] GetSupportedImageFormats(MemoryFlags flags, ImageType type)
        {
            unsafe
            {
                uint num_image_formats = 0;
                ClHelper.GetError(Cl.GetSupportedImageFormats(
                                      Handle, (ulong)flags, (uint)type, 0, null, &num_image_formats));

                Cl.image_format *image_formats = stackalloc Cl.image_format[(int)num_image_formats];

                ClHelper.GetError(Cl.GetSupportedImageFormats(
                                      Handle, (ulong)flags, (uint)type, num_image_formats, image_formats, null));

                Tuple <ChannelOrder, ChannelType>[] formats = new Tuple <ChannelOrder, ChannelType> [num_image_formats];

                for (uint i = 0; i < num_image_formats; ++i)
                {
                    formats[i] = Tuple.Create(
                        (ChannelOrder)image_formats[i].image_channel_order,
                        (ChannelType)image_formats[i].image_channel_data_type);
                }

                return(formats);
            }
        }
Example #2
0
        public Event EnqueueUnmapBuffer(
            Buffer buffer, IntPtr pointer, Event[] events)
        {
            ClHelper.ThrowNullException(Handle);

            if (buffer == Buffer.Null)
            {
                throw new ArgumentNullException("buffer");
            }

            unsafe
            {
                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.EnqueueUnmapMemObject(Handle,
                                                           buffer.Handle, pointer.ToPointer(),
                                                           (uint)num_events_in_wait_list, wait_list, &event_ptr));

                return(new Event(event_ptr));
            }
        }
Example #3
0
        public Event EnqueueWriteBuffer(Buffer buffer, bool blocking, ulong offset, ulong count, IntPtr destination, Event[] events)
        {
            ClHelper.ThrowNullException(Handle);

            if (buffer == Buffer.Null)
            {
                throw new ArgumentNullException("buffer");
            }
            if (destination == IntPtr.Zero)
            {
                throw new ArgumentNullException("destination");
            }

            unsafe
            {
                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.EnqueueWriteBuffer(Handle, buffer.Handle,
                                                        blocking ? 1u : 0u, new UIntPtr(offset), new UIntPtr(count), destination.ToPointer(),
                                                        (uint)num_events_in_wait_list, wait_list, &event_ptr));

                return(new Event(event_ptr));
            }
        }
Example #4
0
        public Program(Context context, string source)
            : this()
        {
            if (context == Context.Null)
            {
                throw new ArgumentNullException("context");
            }
            if (source == null)
            {
                throw new ArgumentNullException("source");
            }

            unsafe
            {
                var   bytes   = Encoding.ASCII.GetByteCount(source);
                byte *strings = stackalloc byte[bytes];

                fixed(char *source_ptr = source)
                {
                    Encoding.ASCII.GetBytes(source_ptr, source.Length, strings, bytes);
                }

                UIntPtr length = new UIntPtr((uint)bytes);

                int errcode = 0;
                Handle = Cl.CreateProgramWithSource(context.Handle, 1, &strings, &length, &errcode);
                ClHelper.GetError(errcode);
            }
        }
Example #5
0
        public Event EnqueueBarrier(Event[] events)
        {
            ClHelper.ThrowNullException(Handle);

            try
            {
                unsafe
                {
                    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.EnqueueBarrierWithWaitList(
                                          Handle, (uint)num_events_in_wait_list, wait_list, &event_ptr));

                    return(new Event(event_ptr));
                }
            }
            catch (EntryPointNotFoundException)
            {
                throw ClHelper.VersionException(1, 2);
            }
        }
Example #6
0
        public void SetCallback(
            Action <Event, CommandExecutionStatus, object> notify,
            object user_data)
        {
            ClHelper.ThrowNullException(Handle);

            if (notify == null)
            {
                throw new ArgumentNullException("notify");
            }

            unsafe
            {
                var function_ptr = IntPtr.Zero;
                var data_handle  = new GCHandle();
                var data         = Tuple.Create(notify, user_data);
                data_handle = GCHandle.Alloc(data);

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

                try
                {
                    ClHelper.GetError(Cl.SetEventCallback(
                                          Handle, (int)CommandExecutionStatus.Complete, function_ptr, GCHandle.ToIntPtr(data_handle).ToPointer()));
                }
                catch (Exception)
                {
                    data_handle.Free();
                    throw;
                }
            }
        }
Example #7
0
        public void SetDestructorCallback(Action <Image, object> notify, object user_data)
        {
            ClHelper.ThrowNullException(Handle);

            if (notify == null)
            {
                throw new ArgumentNullException("notify");
            }

            unsafe
            {
                var function_ptr = IntPtr.Zero;
                var data         = Tuple.Create(notify, user_data);
                var data_handle  = GCHandle.Alloc(data);

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

                try
                {
                    ClHelper.GetError(Cl.SetMemObjectDestructorCallback(
                                          Handle, function_ptr, GCHandle.ToIntPtr(data_handle).ToPointer()));
                }
                catch (EntryPointNotFoundException)
                {
                    data_handle.Free();
                    throw ClHelper.VersionException(1, 1);
                }
                catch (Exception)
                {
                    data_handle.Free();
                    throw;
                }
            }
        }
Example #8
0
        public Event EnqueueTask(Kernel kernel, Event[] events)
        {
            ClHelper.ThrowNullException(Handle);

            if (kernel == Kernel.Null)
            {
                throw new ArgumentNullException("kernel");
            }

            unsafe
            {
                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.EnqueueTask(Handle, kernel.Handle,
                                                 (uint)num_events_in_wait_list, wait_list, &event_ptr));

                return(new Event(event_ptr));
            }
        }
Example #9
0
        public void Release()
        {
            ClHelper.ThrowNullException(Handle);
            var error = Cl.ReleaseCommandQueue(Handle);

            Handle = IntPtr.Zero;
            ClHelper.GetError(error);
        }
Example #10
0
        public void ReleaseImage()
        {
            ClHelper.ThrowNullException(Handle);
            int error = Cl.ReleaseMemObject(Handle);

            Handle = IntPtr.Zero;
            ClHelper.GetError(error);
        }
Example #11
0
        internal static OpenCLException VersionException(string version, int major, int minor)
        {
            var clversion = ClHelper.ParseCLVersion(version);

            return(new OpenCLException(string.Format(
                                           "This method requires OpenCL {0}.{1}, currently running OpenCL {2}.{3}.",
                                           major, minor, clversion.Major, clversion.Minor)));
        }
Example #12
0
        public void ReleaseProgram()
        {
            ClHelper.ThrowNullException(Handle);
            int error = Cl.ReleaseProgram(Handle);

            Handle = IntPtr.Zero;
            ClHelper.GetError(error);
        }
Example #13
0
 public override bool Equals(object obj)
 {
     ClHelper.ThrowNullException(Handle);
     if (obj is CommandQueue)
     {
         return(Equals((CommandQueue)obj));
     }
     return(false);
 }
Example #14
0
        public void EnqueueBarrier()
        {
            ClHelper.ThrowNullException(Handle);

            unsafe
            {
                ClHelper.GetError(Cl.EnqueueBarrier(Handle));
            }
        }
Example #15
0
 public override bool Equals(object obj)
 {
     ClHelper.ThrowNullException(Handle);
     if (obj is Program)
     {
         return(Equals((Program)obj));
     }
     return(false);
 }
Example #16
0
        public Event EnqueueCopyImage(
            Image source, Ibasa.Numerics.Vector3ul sourceOrigin,
            Image destination, Ibasa.Numerics.Vector3ul destinationOrigin,
            Ibasa.Numerics.Vector3ul region,
            ulong rowPitch, ulong slicePitch,
            Event[] events)
        {
            ClHelper.ThrowNullException(Handle);

            if (source == Image.Null)
            {
                throw new ArgumentNullException("source");
            }
            if (destination == Image.Null)
            {
                throw new ArgumentNullException("destination");
            }

            unsafe
            {
                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;

                UIntPtr *sourceOrigin_ptr = stackalloc UIntPtr[3];
                sourceOrigin_ptr[0] = new UIntPtr(sourceOrigin.X);
                sourceOrigin_ptr[1] = new UIntPtr(sourceOrigin.Y);
                sourceOrigin_ptr[2] = new UIntPtr(sourceOrigin.Z);

                UIntPtr *destinationOrigin_ptr = stackalloc UIntPtr[3];
                destinationOrigin_ptr[0] = new UIntPtr(destinationOrigin.X);
                destinationOrigin_ptr[1] = new UIntPtr(destinationOrigin.Y);
                destinationOrigin_ptr[2] = new UIntPtr(destinationOrigin.Z);

                UIntPtr *region_ptr = stackalloc UIntPtr[3];
                region_ptr[0] = new UIntPtr(region.X);
                region_ptr[1] = new UIntPtr(region.Y);
                region_ptr[2] = new UIntPtr(region.Z);

                ClHelper.GetError(Cl.EnqueueCopyImage(Handle,
                                                      source.Handle, destination.Handle,
                                                      sourceOrigin_ptr, destinationOrigin_ptr, region_ptr,
                                                      (uint)num_events_in_wait_list, wait_list, &event_ptr));

                return(new Event(event_ptr));
            }
        }
Example #17
0
        public Context(Dictionary <IntPtr, IntPtr> properties, DeviceType deviceType, Action <string, byte[], object> notify, object user_data)
            : this()
        {
            unsafe
            {
                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 Action <IntPtr, IntPtr, UIntPtr, IntPtr>(Callback));
                }

                try
                {
                    int errcode = 0;
                    Handle = Cl.CreateContextFromType(properties_ptr, (int)deviceType,
                                                      function_ptr, data_ptr.ToPointer(), &errcode);
                    ClHelper.GetError(errcode);

                    CallbackPointers.Add(Handle, data_handle);
                }
                catch (Exception)
                {
                    if (data_handle.IsAllocated)
                    {
                        data_handle.Free();
                    }
                    throw;
                }
            }
        }
Example #18
0
        public WorkGroupInfo GetWorkGroupInfo(Device device)
        {
            ClHelper.ThrowNullException(Handle);
            if (device == Device.Null)
            {
                throw new ArgumentNullException("device");
            }

            return(new WorkGroupInfo(Handle, device.Handle));
        }
Example #19
0
        public void SetArgument(int index, int value)
        {
            ClHelper.ThrowNullException(Handle);

            unsafe
            {
                ClHelper.GetError(Cl.SetKernelArg(Handle,
                                                  (uint)index, (UIntPtr)sizeof(int), &value));
            }
        }
Example #20
0
        public void BuildProgram(Device[] devices, string options, 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 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.BuildProgram(Handle, (uint)devices_length, device_list, chars,
                                                      function_ptr, GCHandle.ToIntPtr(data_ptr).ToPointer()));
                }
                catch (Exception)
                {
                    data_ptr.Free();
                    throw;
                }
            }
        }
Example #21
0
        public Event EnqueueMarker()
        {
            ClHelper.ThrowNullException(Handle);

            unsafe
            {
                IntPtr event_ptr = IntPtr.Zero;

                ClHelper.GetError(Cl.EnqueueMarker(Handle, &event_ptr));

                return(new Event(event_ptr));
            }
        }
Example #22
0
        public static void Wait(Event @event)
        {
            if (@event == null)
            {
                throw new ArgumentNullException("event");
            }

            unsafe
            {
                IntPtr list  = @event.Handle;
                int    error = Cl.WaitForEvents(1, &list);
                ClHelper.GetError(error);
            }
        }
Example #23
0
        public CommandQueue(Context context, Device device, CommandQueueProperties properties) : this()
        {
            if (context == Context.Null)
            {
                throw new ArgumentNullException("context");
            }

            unsafe
            {
                int error = 0;
                Handle = Cl.CreateCommandQueue(context.Handle, device.Handle, (ulong)properties, &error);
                ClHelper.GetError(error);
            }
        }
Example #24
0
        public Event EnqueueFillBuffer(Image image,
                                       IntPtr fillColor,
                                       Ibasa.Numerics.Vector3ul origin,
                                       Ibasa.Numerics.Vector3ul region,
                                       ulong offset, ulong size, Event[] events)
        {
            ClHelper.ThrowNullException(Handle);

            if (image == Image.Null)
            {
                throw new ArgumentNullException("image");
            }
            if (fillColor == IntPtr.Zero)
            {
                throw new ArgumentNullException("fillColor");
            }

            unsafe
            {
                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;

                UIntPtr *origin_ptr = stackalloc UIntPtr[3];
                origin_ptr[0] = new UIntPtr(origin.X);
                origin_ptr[1] = new UIntPtr(origin.Y);
                origin_ptr[2] = new UIntPtr(origin.Z);

                UIntPtr *region_ptr = stackalloc UIntPtr[3];
                region_ptr[0] = new UIntPtr(region.X);
                region_ptr[1] = new UIntPtr(region.Y);
                region_ptr[2] = new UIntPtr(region.Z);

                ClHelper.GetError(Cl.EnqueueFillImage(Handle, image.Handle,
                                                      fillColor.ToPointer(), origin_ptr, region_ptr,
                                                      (uint)num_events_in_wait_list, wait_list, &event_ptr));

                return(new Event(event_ptr));
            }
        }
Example #25
0
        public Event EnqueueWriteImage(Image image, bool blocking,
                                       Ibasa.Numerics.Vector3ul origin,
                                       Ibasa.Numerics.Vector3ul region,
                                       ulong rowPitch, ulong slicePitch,
                                       IntPtr source, Event[] events)
        {
            ClHelper.ThrowNullException(Handle);

            if (image == Image.Null)
            {
                throw new ArgumentNullException("image");
            }
            if (source == IntPtr.Zero)
            {
                throw new ArgumentNullException("source");
            }

            unsafe
            {
                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;

                UIntPtr *origin_ptr = stackalloc UIntPtr[3];
                origin_ptr[0] = new UIntPtr(origin.X);
                origin_ptr[1] = new UIntPtr(origin.Y);
                origin_ptr[2] = new UIntPtr(origin.Z);

                UIntPtr *region_ptr = stackalloc UIntPtr[3];
                region_ptr[0] = new UIntPtr(region.X);
                region_ptr[1] = new UIntPtr(region.Y);
                region_ptr[2] = new UIntPtr(region.Z);

                ClHelper.GetError(Cl.EnqueueReadImage(Handle, image.Handle,
                                                      blocking ? 1u : 0u, origin_ptr, region_ptr, new UIntPtr(rowPitch), new UIntPtr(slicePitch),
                                                      source.ToPointer(), (uint)num_events_in_wait_list, wait_list, &event_ptr));

                return(new Event(event_ptr));
            }
        }
Example #26
0
        public void SetArgument(int index, Buffer buffer)
        {
            ClHelper.ThrowNullException(Handle);
            if (buffer == Buffer.Null)
            {
                throw new ArgumentNullException("buffer");
            }

            unsafe
            {
                IntPtr value = buffer.Handle;
                ClHelper.GetError(Cl.SetKernelArg(Handle,
                                                  (uint)index, (UIntPtr)IntPtr.Size, &value));
            }
        }
Example #27
0
        public Sampler(Context context, bool normalizedCoords, AddressingMode addressingMode, FilterMode filterMode)
            : this()
        {
            if (context == Context.Null)
            {
                throw new ArgumentNullException("context");
            }

            unsafe
            {
                int error;
                Handle = Cl.CreateSampler(context.Handle,
                                          normalizedCoords ? 1u : 0u, (uint)addressingMode, (uint)filterMode, &error);
                ClHelper.GetError(error);
            }
        }
Example #28
0
        public Buffer(Context context, MemoryFlags flags, ulong size)
            : 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 (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 (size == 0)
            {
                throw new ArgumentOutOfRangeException("size", size, "size is 0.");
            }

            unsafe
            {
                int error;
                Handle = Cl.CreateBuffer(context.Handle, (ulong)flags, new UIntPtr(size), null, &error);
                ClHelper.GetError(error);
            }
        }
Example #29
0
        public Kernel(Program program, string kernel_name)
            : this()
        {
            if (program == Program.Null)
            {
                throw new ArgumentNullException("context");
            }

            unsafe
            {
                int    error;
                IntPtr str = Marshal.StringToHGlobalAnsi(kernel_name);
                Handle = Cl.CreateKernel(program.Handle, (byte *)str.ToPointer(), &error);
                Marshal.FreeHGlobal(str);
                ClHelper.GetError(error);
            }
        }
Example #30
0
        public void SetUserEventStatus(int status)
        {
            ClHelper.ThrowNullException(Handle);

            if (status > 0)
            {
                throw new ArgumentException("status must be zero or negative.");
            }

            try
            {
                ClHelper.GetError(Cl.SetUserEventStatus(Handle, status));
            }
            catch (EntryPointNotFoundException)
            {
                throw ClHelper.VersionException(1, 1);
            }
        }