예제 #1
0
        public void SetArgument <T>(int argumentIndex, T value) where T : struct
        {
            GCHandle handle = GCHandle.Alloc(value, GCHandleType.Pinned);

            CLfunc.clSetKernelArg(InternalPointer, argumentIndex, Marshal.SizeOf(typeof(T)), handle.AddrOfPinnedObject());

            handle.Free();
        }
예제 #2
0
        public CLKernel(CLProgram program, string functionName)
        {
            int errorCode;

            InternalPointer = CLfunc.clCreateKernel(
                program.InternalPointer,
                functionName,
                out errorCode
                );
        }
예제 #3
0
        public void Build(params IntPtr[] devices)
        {
            int error = CLfunc.clBuildProgram(InternalPointer, devices.Length, devices, null, null, IntPtr.Zero);

            if (error != 0)
            {
                int paramValueSize = 0;
                CLfunc.clGetProgramBuildInfo(InternalPointer, devices.First(), ProgramBuildInfoString.Log, 0, null, out paramValueSize);
                StringBuilder text = new StringBuilder(paramValueSize);
                CLfunc.clGetProgramBuildInfo(InternalPointer, devices.First(), ProgramBuildInfoString.Log, paramValueSize, text, out paramValueSize);
                throw new Exception(text.ToString());
            }
        }
예제 #4
0
        private static IntPtr[] getDevices(IntPtr platform, DeviceType deviceType)
        {
            // デバイスの数を取得
            int deviceCount;

            CLfunc.clGetDeviceIDs(platform, deviceType, 0, null, out deviceCount);

            // デバイスを取得
            IntPtr[] result = new IntPtr[deviceCount];
            CLfunc.clGetDeviceIDs(platform, deviceType, deviceCount, result, out deviceCount);

            Devices = result;
            return(result);
        }
예제 #5
0
        private static IntPtr[] getPlatforms()
        {
            // プラットフォームの数を取得
            int platformCount;

            CLfunc.clGetPlatformIDs(0, null, out platformCount);

            // プラットフォームを取得
            IntPtr[] result = new IntPtr[platformCount];
            CLfunc.clGetPlatformIDs(platformCount, result, out platformCount);

            Platforms = result;
            return(result);
        }
예제 #6
0
        /// <summary>
        /// 現在のデバイスで扱える2次元画像の大きさを取得します
        /// </summary>
        /// <param name="paramName"></param>
        /// <param name="result"></param>
        public static void GetDeviceImage2DMaxSize(out int W, out int H)
        {
            int valueSize;

            CLfunc.clGetDeviceInfo(Devices[0], DeviceInfo.DeviceImage2DMaxWidth, 0, null, out valueSize);
            int[] resultWidth = new int[1];
            CLfunc.clGetDeviceInfo(Devices[0], DeviceInfo.DeviceImage2DMaxWidth, valueSize, resultWidth, out valueSize);
            W = resultWidth[0];

            CLfunc.clGetDeviceInfo(Devices[0], DeviceInfo.DeviceImage2DMaxHeight, 0, null, out valueSize);
            int[] resultHeight = new int[1];
            CLfunc.clGetDeviceInfo(Devices[0], DeviceInfo.DeviceImage2DMaxHeight, valueSize, resultHeight, out valueSize);
            H = resultHeight[0];
        }
예제 #7
0
        public void EnqueueFillColor(CLImage2D image, CLColor fillColor)
        {
            int          errorCode;
            Coordinate2D origin = new Coordinate2D(0);
            GCHandle     handle = GCHandle.Alloc(fillColor, GCHandleType.Pinned);

            errorCode = CLfunc.clEnqueueFillImage(InternalPointer, image.InternalPointer, handle.AddrOfPinnedObject(),
                                                  ref origin, ref image._Size, 0, null, IntPtr.Zero);

            handle.Free();
            if (errorCode != 0)
            {
                throw new Exception("OpenCL 2D Image オブジェクトの塗りつぶしに失敗しまた。\nエラーコード:" + errorCode.ToString() + "\n");
            }
        }
예제 #8
0
        /// <summary>
        /// カーネルを並列実行します
        /// </summary>
        /// <param name="kernel"></param>
        /// <param name="globalWorkSize"></param>
        /// <param name="localWorkSize"></param>
        public void EnqueueRange(CLKernel kernel, MultiDimension globalWorkSize, MultiDimension localWorkSize)
        {
            MultiDimension offset = new MultiDimension();

            CLfunc.clEnqueueNDRangeKernel(
                InternalPointer,
                kernel.InternalPointer,
                globalWorkSize.Dimension,
                ref offset,
                ref globalWorkSize,
                ref localWorkSize,
                0,
                null,
                IntPtr.Zero);
        }
예제 #9
0
        public CLImage2D(CLContext context, MemoryFlags flags, int W, int H, byte[] data)
        {
            int           errorCode;
            CLImageFormat format = new CLImageFormat(ChannelOrder.Bgra, ChannelType.UnsignedInt8);
            GCHandle      handle = GCHandle.Alloc(data, GCHandleType.Pinned);

            InternalPointer = CLfunc.clCreateImage2D(context.InternalPointer, flags, ref format, W, H, 0, handle.AddrOfPinnedObject(), out errorCode);

            handle.Free();
            Size = new Coordinate2D(W, H);
            if (errorCode != 0)
            {
                throw new Exception("OpenCL 2D Image オブジェクトの生成に失敗しまた。\nエラーコード:" + errorCode.ToString() + "\n");
            }
        }
예제 #10
0
        public void EnqueueReadImageData(CLImage2D image, byte[] data)
        {
            int          errorCode;
            Coordinate2D origin = new Coordinate2D(0);
            GCHandle     handle = GCHandle.Alloc(data, GCHandleType.Pinned);

            errorCode = CLfunc.clEnqueueReadImage(InternalPointer, image.InternalPointer, true,
                                                  ref origin, ref image._Size, image._Size.X * 4, 0, handle.AddrOfPinnedObject(), 0, null, IntPtr.Zero);

            handle.Free();
            if (errorCode != 0)
            {
                throw new Exception("OpenCL 2D Image オブジェクトからのデータ読み出しに失敗しまた。\nエラーコード:" + errorCode.ToString() + "\n");
            }
        }
예제 #11
0
        /// <summary>
        /// バッファからデータを読み出します
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="buffer"></param>
        /// <param name="systemBuffer"></param>
        public void ReadBuffer <T>(CLBuffer buffer, T[] systemBuffer) where T : struct
        {
            GCHandle handle = GCHandle.Alloc(systemBuffer, GCHandleType.Pinned);

            CLfunc.clEnqueueReadBuffer(
                InternalPointer,
                buffer.InternalPointer,
                true,
                0,
                Math.Min(buffer.SizeInBytes, Marshal.SizeOf(typeof(T)) * systemBuffer.Length),
                handle.AddrOfPinnedObject(),
                0,
                IntPtr.Zero,
                IntPtr.Zero);

            handle.Free();
        }
예제 #12
0
        /// <summary>
        /// バッファにデータを書き込みます
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="context"></param>
        /// <param name="initialData"></param>
        /// <returns></returns>
        public static CLBuffer FromCopiedHostMemory <T>(CLContext context, T[] initialData) where T : struct
        {
            CLBuffer result = new CLBuffer();

            result.SizeInBytes = Marshal.SizeOf(typeof(T)) * initialData.Length;

            int      errorCode;
            GCHandle handle = GCHandle.Alloc(initialData, GCHandleType.Pinned);

            result.InternalPointer = CLfunc.clCreateBuffer(
                context.InternalPointer,
                MemoryFlags.CopyHostMemory,
                result.SizeInBytes,
                handle.AddrOfPinnedObject(),
                out errorCode);

            handle.Free();
            return(result);
        }
예제 #13
0
 /// <summary>
 /// カーネルを実行します
 /// </summary>
 /// <param name="kernel"></param>
 public void EnqueueTask(CLKernel kernel)
 {
     CLfunc.clEnqueueTask(InternalPointer, kernel.InternalPointer, 0, null, IntPtr.Zero);
 }
예제 #14
0
        public CLProgram(CLContext context, params string[] sources)
        {
            int errorCode;

            InternalPointer = CLfunc.clCreateProgramWithSource(context.InternalPointer, sources.Length, sources, null, out errorCode);
        }
예제 #15
0
        public CLCommandQueue(CLContext context, IntPtr device)
        {
            int error;

            InternalPointer = CLfunc.clCreateCommandQueue(context.InternalPointer, device, 0, out error);
        }
예제 #16
0
        public CLContext(IntPtr[] devices)
        {
            int error;

            InternalPointer = CLfunc.clCreateContext(null, devices.Length, devices, null, IntPtr.Zero, out error);
        }
예제 #17
0
        public void SetArgument(int argumentIndex, CLBuffer buffer)
        {
            IntPtr bufferPointer = buffer.InternalPointer;

            CLfunc.clSetKernelArg(InternalPointer, argumentIndex, Marshal.SizeOf(typeof(IntPtr)), ref bufferPointer);
        }