예제 #1
0
        public void SetArgument(uint arg_index, object value, int size)
        {
            GCHandle handle = GCHandle.Alloc(value, GCHandleType.Pinned);

            _pinnedObjects.Add(handle);
            OpenCLException.Check(Native.clSetKernelArg(_handle, arg_index, new IntPtr(size), handle.AddrOfPinnedObject()));
        }
예제 #2
0
        void ExecuteInternal(bool outputEventHandle, Kernel kernel, int[] global_work_offset, int[] global_work_size, int[] local_work_size, EventHandle[] wait_list, out EventHandle eventHandle)
        {
            if (global_work_size == null || local_work_size == null || global_work_size.Length != local_work_size.Length)
            {
                throw new ArgumentException();
            }

            uint work_dim = (uint)global_work_size.Length;

            IntPtr[] goffsets = null;
            IntPtr[] gthreads = new IntPtr[work_dim];
            IntPtr[] lthreads = new IntPtr[work_dim];
            for (int i = 0; i < work_dim; i++)
            {
                gthreads[i] = new IntPtr(global_work_size[i]);
                lthreads[i] = new IntPtr(local_work_size[i]);
            }

            IntPtr[] waits     = wait_list == null ? null : EventHandle.ToIntPtrArray(wait_list);
            uint     num_waits = waits == null ? 0 : (uint)waits.Length;

            if (outputEventHandle)
            {
                IntPtr event_handle;
                OpenCLException.Check(Native.clEnqueueNDRangeKernel(_handle, kernel.Handle, work_dim, goffsets, gthreads, lthreads, num_waits, waits, out event_handle));
                eventHandle = new EventHandle(event_handle);
            }
            else
            {
                OpenCLException.Check(Native.clEnqueueNDRangeKernel(_handle, kernel.Handle, work_dim, goffsets, gthreads, lthreads, num_waits, waits, IntPtr.Zero));
                eventHandle = null;
            }
        }
예제 #3
0
        public Memory CreateBuffer(MemoryFlags flags, int size)
        {
            int    errcode;
            IntPtr membuf = Native.clCreateBuffer(_handle, flags, new IntPtr(size), IntPtr.Zero, out errcode);

            OpenCLException.Check(errcode);
            return(new Memory(this, membuf));
        }
예제 #4
0
        public CommandQueue CreateCommandQueue(Device device, CommandQueueProperties properties)
        {
            int    errcode;
            IntPtr command_queue = Native.clCreateCommandQueue(_handle, device.Handle, properties, out errcode);

            OpenCLException.Check(errcode);
            return(new CommandQueue(this, device, command_queue));
        }
예제 #5
0
        public Kernel CreateKernel(string name)
        {
            int    errcode;
            IntPtr kernel = Native.clCreateKernel(_handle, name, out errcode);

            OpenCLException.Check(errcode);
            return(new Kernel(this, kernel));
        }
예제 #6
0
        public Memory CreateBuffer(MemoryFlags flags, int size, GCHandle hostPtr)
        {
            int    errcode;
            IntPtr membuf = Native.clCreateBuffer(_handle, flags, new IntPtr(size), hostPtr.AddrOfPinnedObject(), out errcode);

            OpenCLException.Check(errcode);
            return(new Memory(this, membuf));
        }
예제 #7
0
        IntPtr MappingAsync(bool blocking, Memory buffer, MapFlags flags, int offset, int cb)
        {
            int    errcode;
            IntPtr ptr = Native.clEnqueueMapBuffer(_handle, buffer.Handle, blocking ? CL_Bool.True : CL_Bool.False,
                                                   flags, new IntPtr(offset), new IntPtr(cb), 0, null, IntPtr.Zero, out errcode);

            OpenCLException.Check(errcode);
            return(ptr);
        }
예제 #8
0
 public static void WaitAll(EventHandle[] waits)
 {
     IntPtr[] array = ToIntPtrArray(waits);
     if (array == null)
     {
         return;
     }
     OpenCLException.Check(Native.clWaitForEvents((uint)array.Length, array));
 }
예제 #9
0
        public static void QueryInfoDirect(QueryType type, byte[] buf, params object[] args)
        {
            MethodInfo mi = GetMethodInfo(type);

            object[] args2 = new object[args.Length + 3];
            Array.Copy(args, 0, args2, 0, args.Length);
            args2[args.Length]     = new IntPtr(buf.Length);
            args2[args.Length + 1] = buf;
            args2[args.Length + 2] = null;
            OpenCLException.Check((int)mi.Invoke(null, args2));
        }
예제 #10
0
        public void Build(Device[] devices, string options)
        {
            IntPtr[] device_list = new IntPtr[devices.Length];
            for (int i = 0; i < device_list.Length; i++)
            {
                device_list[i] = devices[i].Handle;
            }
            int errcode = Native.clBuildProgram(_handle,
                                                (uint)devices.Length, device_list, options, IntPtr.Zero, IntPtr.Zero);

            OpenCLException.Check(errcode);
        }
예제 #11
0
        public Device[] GetDevices(DeviceType type)
        {
            uint num_devices;

            OpenCLException.Check(Native.clGetDeviceIDs(_handle, type, 0, null, out num_devices));
            IntPtr[] ids = new IntPtr[num_devices];
            OpenCLException.Check(Native.clGetDeviceIDs(_handle, type, num_devices, ids, out num_devices));
            Device[] devices = new Device[ids.Length];
            for (int i = 0; i < devices.Length; i++)
            {
                devices[i] = Device.CreateDevice(ids[i]);
            }
            return(devices);
        }
예제 #12
0
        public static Platform[] GetPlatforms()
        {
            uint num_platforms;

            OpenCLException.Check(Native.clGetPlatformIDs(0, null, out num_platforms));
            IntPtr[] ids = new IntPtr[num_platforms];
            OpenCLException.Check(Native.clGetPlatformIDs(num_platforms, ids, out num_platforms));
            Platform[] platforms = new Platform[ids.Length];
            for (int i = 0; i < platforms.Length; i++)
            {
                platforms[i] = Platform.CreatePlatform(ids[i]);
            }
            return(platforms);
        }
예제 #13
0
        public Context(Platform platform, DeviceType deviceType) : base(IntPtr.Zero)
        {
            if (platform == null)
            {
                platform = Platform.GetPlatforms()[0];
            }
            int errcode;

            IntPtr[] properties = new IntPtr[] {
                new IntPtr((int)ContextProperties.Platform),
                platform.Handle,
                IntPtr.Zero
            };
            _handle = Native.clCreateContextFromType(properties, deviceType, IntPtr.Zero, IntPtr.Zero, out errcode);
            OpenCLException.Check(errcode);
        }
예제 #14
0
        public Program CreateProgram(string src)
        {
            byte[]   raw    = Encoding.UTF8.GetBytes(src);
            GCHandle handle = GCHandle.Alloc(raw, GCHandleType.Pinned);

            try {
                int    errcode;
                IntPtr p_src = handle.AddrOfPinnedObject();
                IntPtr len   = new IntPtr(raw.Length);
                IntPtr prog  = Native.clCreateProgramWithSource(_handle, 1, ref p_src, ref len, out errcode);
                OpenCLException.Check(errcode);
                return(new Program(this, prog));
            } finally {
                handle.Free();
            }
        }
예제 #15
0
        public Context(Platform platform, Device[] devices) : base(IntPtr.Zero)
        {
            if (platform == null)
            {
                platform = Platform.GetPlatforms()[0];
            }
            int errcode;

            IntPtr[] properties = new IntPtr[] {
                new IntPtr((int)ContextProperties.Platform),
                platform.Handle,
                IntPtr.Zero
            };
            _handle = Native.clCreateContext(properties, (uint)devices.Length,
                                             HandleBase.ToHandleArray <Device> (devices), IntPtr.Zero, IntPtr.Zero, out errcode);
            OpenCLException.Check(errcode);
        }
예제 #16
0
        void ExecuteInternal(bool outputEventHandle, Kernel kernel, EventHandle[] wait_list, out EventHandle eventHandle)
        {
            IntPtr[] waits     = wait_list == null ? null : EventHandle.ToIntPtrArray(wait_list);
            uint     num_waits = waits == null ? 0 : (uint)waits.Length;

            if (outputEventHandle)
            {
                IntPtr event_handle;
                OpenCLException.Check(Native.clEnqueueTask(_handle, kernel.Handle, num_waits, waits, out event_handle));
                eventHandle = new EventHandle(event_handle);
            }
            else
            {
                OpenCLException.Check(Native.clEnqueueTask(_handle, kernel.Handle, num_waits, waits, IntPtr.Zero));
                eventHandle = null;
            }
        }
예제 #17
0
        public static byte[] QueryInfo(QueryType type, params object[] args)
        {
            MethodInfo mi = GetMethodInfo(type);

            object[] args2 = new object[args.Length + 3];
            Array.Copy(args, 0, args2, 0, args.Length);
            args2[args.Length]     = IntPtr.Zero;
            args2[args.Length + 1] = null;
            args2[args.Length + 2] = null;
            OpenCLException.Check((int)mi.Invoke(null, args2));
            IntPtr size = (IntPtr)args2[args.Length + 2];

            byte[] value = new byte[size.ToInt32()];
            args2[args.Length]     = size;
            args2[args.Length + 1] = value;
            OpenCLException.Check((int)mi.Invoke(null, args2));
            return(value);
        }
예제 #18
0
        void CopyBuferInternal(bool outputEventHandle, Memory src, int src_offset, Memory dst, int dst_offset, int size, EventHandle[] wait_list, out EventHandle eventHandle)
        {
            IntPtr[] waits     = EventHandle.ToIntPtrArray(wait_list);
            uint     num_waits = waits == null ? 0 : (uint)waits.Length;

            if (outputEventHandle)
            {
                IntPtr event_handle;
                OpenCLException.Check(Native.clEnqueueCopyBuffer(_handle, src.Handle, dst.Handle, new IntPtr(src_offset), new IntPtr(dst_offset),
                                                                 new IntPtr(size), num_waits, waits, out event_handle));
                eventHandle = new EventHandle(event_handle);
            }
            else
            {
                OpenCLException.Check(Native.clEnqueueCopyBuffer(_handle, src.Handle, dst.Handle, new IntPtr(src_offset), new IntPtr(dst_offset),
                                                                 new IntPtr(size), num_waits, waits, IntPtr.Zero));
                eventHandle = null;
            }
        }
예제 #19
0
        void ReadBufferInternal(bool blocking, bool outputEventHandle, Memory buf, int buf_offset, object dst, int dst_offset, int size, EventHandle[] wait_list, out EventHandle eventHandle)
        {
            IntPtr   dst_handle;
            GCHandle dst_gc_handle;

            if (dst is IntPtr)
            {
                dst_handle    = (IntPtr)dst;
                dst_gc_handle = new GCHandle();
            }
            else
            {
                dst_gc_handle = GCHandle.Alloc(dst, GCHandleType.Pinned);
                dst_handle    = dst_gc_handle.AddrOfPinnedObject();
            }

            try {
                IntPtr   dst_ptr     = new IntPtr(dst_handle.ToInt64() + dst_offset);
                IntPtr[] waits       = EventHandle.ToIntPtrArray(wait_list);
                uint     num_waits   = waits == null ? 0 : (uint)waits.Length;
                CL_Bool  is_blocking = (blocking ? CL_Bool.True : CL_Bool.False);
                if (outputEventHandle)
                {
                    IntPtr event_handle;
                    OpenCLException.Check(Native.clEnqueueReadBuffer(_handle, buf.Handle, is_blocking, new IntPtr(buf_offset), new IntPtr(size),
                                                                     dst_ptr, num_waits, waits, out event_handle));
                    eventHandle = new EventHandle(event_handle);
                }
                else
                {
                    OpenCLException.Check(Native.clEnqueueReadBuffer(_handle, buf.Handle, is_blocking, new IntPtr(buf_offset), new IntPtr(size),
                                                                     dst_ptr, num_waits, waits, IntPtr.Zero));
                    eventHandle = null;
                }
            } finally {
                if (dst_gc_handle.IsAllocated)
                {
                    dst_gc_handle.Free();
                }
            }
        }
예제 #20
0
 public void Finish()
 {
     OpenCLException.Check(Native.clFinish(_handle));
 }
예제 #21
0
 public void WaitOne()
 {
     OpenCLException.Check(Native.clWaitForEvents(1, new IntPtr[] { _handle }));
 }
예제 #22
0
 public void SetLocalDataShare(uint arg_index, int size)
 {
     OpenCLException.Check(Native.clSetKernelArg(_handle, arg_index, new IntPtr(size), IntPtr.Zero));
 }
예제 #23
0
 public void Unmapping(Memory buffer, IntPtr mapped)
 {
     OpenCLException.Check(Native.clEnqueueUnmapMemObject(_handle, buffer.Handle, mapped, 0, null, IntPtr.Zero));
 }