Example #1
0
        private OpenCLImage3D(CLMemoryHandle handle, OpenCLContext context, OpenCLMemoryFlags flags)
            : base(context, flags)
        {
            Handle = handle;

            Init();
        }
        private OpenCLImage3D(CLMemoryHandle handle, OpenCLContext context, OpenCLMemoryFlags flags)
            : base(context, flags)
        {
            Handle = handle;

            Init();
        }
        /// <summary>
        /// Creates a new <see cref="OpenCLImage3D"/>.
        /// </summary>
        /// <param name="context"> A valid <see cref="OpenCLContext"/> in which the <see cref="OpenCLImage3D"/> is created. </param>
        /// <param name="flags"> A bit-field that is used to specify allocation and usage information about the <see cref="OpenCLImage3D"/>. </param>
        /// <param name="format"> A structure that describes the format properties of the <see cref="OpenCLImage3D"/>. </param>
        /// <param name="width"> The width of the <see cref="OpenCLImage3D"/> in pixels. </param>
        /// <param name="height"> The height of the <see cref="OpenCLImage3D"/> in pixels. </param>
        /// <param name="depth"> The depth of the <see cref="OpenCLImage3D"/> in pixels. </param>
        /// <param name="rowPitch"> The size in bytes of each row of elements of the <see cref="OpenCLImage3D"/>. If <paramref name="rowPitch"/> is zero, OpenCL will compute the proper value based on <see cref="OpenCLImage.Width"/> and <see cref="OpenCLImage.ElementSize"/>. </param>
        /// <param name="slicePitch"> The size in bytes of each 2D slice in the <see cref="OpenCLImage3D"/>. If <paramref name="slicePitch"/> is zero, OpenCL will compute the proper value based on <see cref="OpenCLImage.RowPitch"/> and <see cref="OpenCLImage.Height"/>. </param>
        /// <param name="data"> The data to initialize the <see cref="OpenCLImage3D"/>. Can be <c>IntPtr.Zero</c>. </param>
        public OpenCLImage3D(OpenCLContext context, OpenCLMemoryFlags flags, OpenCLImageFormat format, int width, int height, int depth, long rowPitch, long slicePitch, IntPtr data)
            : base(context, flags)
        {
            OpenCLErrorCode error = OpenCLErrorCode.Success;
            Handle = CL10.CreateImage3D(context.Handle, flags, ref format, new IntPtr(width), new IntPtr(height), new IntPtr(depth), new IntPtr(rowPitch), new IntPtr(slicePitch), data, out error);
            OpenCLException.ThrowOnError(error);

            Init();
        }
Example #4
0
        /// <summary>
        /// Creates a new <see cref="OpenCLImage2D"/> from an OpenGL 2D texture object.
        /// </summary>
        /// <param name="context"> A <see cref="OpenCLContext"/> with enabled CL/GL sharing. </param>
        /// <param name="flags"> A bit-field that is used to specify usage information about the <see cref="OpenCLImage2D"/>. Only <c>OpenCLMemoryFlags.ReadOnly</c>, <c>OpenCLMemoryFlags.WriteOnly</c> and <c>OpenCLMemoryFlags.ReadWrite</c> are allowed. </param>
        /// <param name="textureTarget"> One of the following values: GL_TEXTURE_2D, GL_TEXTURE_CUBE_MAP_POSITIVE_X, GL_TEXTURE_CUBE_MAP_POSITIVE_Y, GL_TEXTURE_CUBE_MAP_POSITIVE_Z, GL_TEXTURE_CUBE_MAP_NEGATIVE_X, GL_TEXTURE_CUBE_MAP_NEGATIVE_Y, GL_TEXTURE_CUBE_MAP_NEGATIVE_Z, or GL_TEXTURE_RECTANGLE. Using GL_TEXTURE_RECTANGLE for texture_target requires OpenGL 3.1. Alternatively, GL_TEXTURE_RECTANGLE_ARB may be specified if the OpenGL extension GL_ARB_texture_rectangle is supported. </param>
        /// <param name="mipLevel"> The mipmap level of the OpenGL 2D texture object to be used. </param>
        /// <param name="textureId"> The OpenGL 2D texture object id to use. </param>
        /// <returns> The created <see cref="OpenCLImage2D"/>. </returns>
        public static OpenCLImage2D CreateFromGLTexture2D(OpenCLContext context, OpenCLMemoryFlags flags, int textureTarget, int mipLevel, int textureId)
        {
            OpenCLErrorCode error = OpenCLErrorCode.Success;
            CLMemoryHandle  image = CL10.CreateFromGLTexture2D(context.Handle, flags, textureTarget, mipLevel, textureId, out error);

            OpenCLException.ThrowOnError(error);

            return(new OpenCLImage2D(image, context, flags));
        }
Example #5
0
        /// <summary>
        /// Creates a new <see cref="OpenCLImage2D"/> from an OpenGL renderbuffer object.
        /// </summary>
        /// <param name="context"> A <see cref="OpenCLContext"/> with enabled CL/GL sharing. </param>
        /// <param name="flags"> A bit-field that is used to specify usage information about the <see cref="OpenCLImage2D"/>. Only <c>OpenCLMemoryFlags.ReadOnly</c>, <c>OpenCLMemoryFlags.WriteOnly</c> and <c>OpenCLMemoryFlags.ReadWrite</c> are allowed. </param>
        /// <param name="renderbufferId"> The OpenGL renderbuffer object id to use. </param>
        /// <returns> The created <see cref="OpenCLImage2D"/>. </returns>
        public static OpenCLImage2D CreateFromGLRenderbuffer(OpenCLContext context, OpenCLMemoryFlags flags, int renderbufferId)
        {
            OpenCLErrorCode error = OpenCLErrorCode.Success;
            CLMemoryHandle  image = CL10.CreateFromGLRenderbuffer(context.Handle, flags, renderbufferId, out error);

            OpenCLException.ThrowOnError(error);

            return(new OpenCLImage2D(image, context, flags));
        }
Example #6
0
        /// <summary>
        /// Creates a new <see cref="OpenCLImage3D"/>.
        /// </summary>
        /// <param name="context"> A valid <see cref="OpenCLContext"/> in which the <see cref="OpenCLImage3D"/> is created. </param>
        /// <param name="flags"> A bit-field that is used to specify allocation and usage information about the <see cref="OpenCLImage3D"/>. </param>
        /// <param name="format"> A structure that describes the format properties of the <see cref="OpenCLImage3D"/>. </param>
        /// <param name="width"> The width of the <see cref="OpenCLImage3D"/> in pixels. </param>
        /// <param name="height"> The height of the <see cref="OpenCLImage3D"/> in pixels. </param>
        /// <param name="depth"> The depth of the <see cref="OpenCLImage3D"/> in pixels. </param>
        /// <param name="rowPitch"> The size in bytes of each row of elements of the <see cref="OpenCLImage3D"/>. If <paramref name="rowPitch"/> is zero, OpenCL will compute the proper value based on <see cref="OpenCLImage.Width"/> and <see cref="OpenCLImage.ElementSize"/>. </param>
        /// <param name="slicePitch"> The size in bytes of each 2D slice in the <see cref="OpenCLImage3D"/>. If <paramref name="slicePitch"/> is zero, OpenCL will compute the proper value based on <see cref="OpenCLImage.RowPitch"/> and <see cref="OpenCLImage.Height"/>. </param>
        /// <param name="data"> The data to initialize the <see cref="OpenCLImage3D"/>. Can be <c>IntPtr.Zero</c>. </param>
        public OpenCLImage3D(OpenCLContext context, OpenCLMemoryFlags flags, OpenCLImageFormat format, int width, int height, int depth, long rowPitch, long slicePitch, IntPtr data)
            : base(context, flags)
        {
            OpenCLErrorCode error = OpenCLErrorCode.Success;

            Handle = CL10.CreateImage3D(context.Handle, flags, ref format, new IntPtr(width), new IntPtr(height), new IntPtr(depth), new IntPtr(rowPitch), new IntPtr(slicePitch), data, out error);
            OpenCLException.ThrowOnError(error);

            Init();
        }
Example #7
0
        internal OpenCLKernel(CLKernelHandle handle, OpenCLProgram program)
        {
            Handle = handle;
            SetID(Handle.Value);

            context      = program.Context;
            functionName = GetStringInfo <CLKernelHandle, OpenCLKernelInfo>(Handle, OpenCLKernelInfo.FunctionName, CL10.GetKernelInfo);
            this.program = program;

            //Trace.WriteLine("Create " + this + " in Thread(" + Thread.CurrentThread.ManagedThreadId + ").", "Information");
        }
Example #8
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="context"></param>
        /// <param name="flags"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        protected static ICollection <OpenCLImageFormat> GetSupportedFormats(OpenCLContext context, OpenCLMemoryFlags flags, OpenCLMemoryType type)
        {
            int             formatCountRet = 0;
            OpenCLErrorCode error          = CL10.GetSupportedImageFormats(context.Handle, flags, type, 0, null, out formatCountRet);

            OpenCLException.ThrowOnError(error);

            OpenCLImageFormat[] formats = new OpenCLImageFormat[formatCountRet];
            error = CL10.GetSupportedImageFormats(context.Handle, flags, type, formatCountRet, formats, out formatCountRet);
            OpenCLException.ThrowOnError(error);

            return(new Collection <OpenCLImageFormat>(formats));
        }
        /// <summary>
        /// Creates a new <see cref="OpenCLUserEvent"/>.
        /// </summary>
        /// <param name="context"> The <see cref="OpenCLContext"/> in which the <see cref="OpenCLUserEvent"/> is created. </param>
        /// <remarks> Requires OpenCL 1.1. </remarks>
        public OpenCLUserEvent(OpenCLContext context)
        {
            OpenCLErrorCode error;
            Handle = CL11.CreateUserEvent(context.Handle, out error);
            OpenCLException.ThrowOnError(error);
            
            SetID(Handle.Value);

            Type = (OpenCLCommandType)GetInfo<CLEventHandle, OpenCLEventInfo, uint>(Handle, OpenCLEventInfo.CommandType, CL10.GetEventInfo);
            Context = context;
            HookNotifier();

            //Trace.WriteLine("Create " + this + " in Thread(" + Thread.CurrentThread.ManagedThreadId + ").", "Information");
        }
Example #10
0
        /// <summary>
        /// Creates a new <see cref="OpenCLProgram"/> from a specified list of binaries.
        /// </summary>
        /// <param name="context"> A <see cref="OpenCLContext"/>. </param>
        /// <param name="binaries"> A list of binaries, one for each item in <paramref name="devices"/>. </param>
        /// <param name="devices"> A subset of the <see cref="OpenCLContext.Devices"/>. If <paramref name="devices"/> is <c>null</c>, OpenCL will associate every binary from <see cref="OpenCLProgram.Binaries"/> with a corresponding <see cref="OpenCLDevice"/> from <see cref="OpenCLContext.Devices"/>. </param>
        public OpenCLProgram(OpenCLContext context, IList <byte[]> binaries, IList <OpenCLDevice> devices)
        {
            int count;

            CLDeviceHandle[] deviceHandles = (devices != null) ?
                                             OpenCLTools.ExtractHandles(devices, out count) :
                                             OpenCLTools.ExtractHandles(context.Devices, out count);

            IntPtr[]        binariesPtrs    = new IntPtr[count];
            IntPtr[]        binariesLengths = new IntPtr[count];
            int[]           binariesStats   = new int[count];
            OpenCLErrorCode error           = OpenCLErrorCode.Success;

            GCHandle[] binariesGCHandles = new GCHandle[count];

            try
            {
                for (int i = 0; i < count; i++)
                {
                    binariesGCHandles[i] = GCHandle.Alloc(binaries[i], GCHandleType.Pinned);
                    binariesPtrs[i]      = binariesGCHandles[i].AddrOfPinnedObject();
                    binariesLengths[i]   = new IntPtr(binaries[i].Length);
                }

                Handle = CL10.CreateProgramWithBinary(
                    context.Handle,
                    count,
                    deviceHandles,
                    binariesLengths,
                    binariesPtrs,
                    binariesStats,
                    out error);
                OpenCLException.ThrowOnError(error);
            }
            finally
            {
                for (int i = 0; i < count; i++)
                {
                    binariesGCHandles[i].Free();
                }
            }


            this.binaries = new ReadOnlyCollection <byte[]>(binaries);
            this.context  = context;
            this.devices  = new ReadOnlyCollection <OpenCLDevice>(
                (devices != null) ? devices : context.Devices);

            //Trace.WriteLine("Create " + this + " in Thread(" + Thread.CurrentThread.ManagedThreadId + ").", "Information");
        }
Example #11
0
        /// <summary>
        /// Creates a new <see cref="OpenCLProgram"/> from a source code string.
        /// </summary>
        /// <param name="context"> A <see cref="OpenCLContext"/>. </param>
        /// <param name="source"> The source code for the <see cref="OpenCLProgram"/>. </param>
        /// <remarks> The created <see cref="OpenCLProgram"/> is associated with the <see cref="OpenCLContext.Devices"/>. </remarks>
        public OpenCLProgram(OpenCLContext context, string source)
        {
            OpenCLErrorCode error = OpenCLErrorCode.Success;

            Handle = CL10.CreateProgramWithSource(context.Handle, 1, new string[] { source }, null, out error);
            OpenCLException.ThrowOnError(error);

            SetID(Handle.Value);

            this.context = context;
            this.devices = context.Devices;
            this.source  = new ReadOnlyCollection <string>(new string[] { source });

            //Trace.WriteLine("Create " + this + " in Thread(" + Thread.CurrentThread.ManagedThreadId + ").", "Information");
        }
Example #12
0
        /// <summary>
        /// Creates a new <see cref="OpenCLUserEvent"/>.
        /// </summary>
        /// <param name="context"> The <see cref="OpenCLContext"/> in which the <see cref="OpenCLUserEvent"/> is created. </param>
        /// <remarks> Requires OpenCL 1.1. </remarks>
        public OpenCLUserEvent(OpenCLContext context)
        {
            OpenCLErrorCode error;

            Handle = CL11.CreateUserEvent(context.Handle, out error);
            OpenCLException.ThrowOnError(error);

            SetID(Handle.Value);

            Type    = (OpenCLCommandType)GetInfo <CLEventHandle, OpenCLEventInfo, uint>(Handle, OpenCLEventInfo.CommandType, CL10.GetEventInfo);
            Context = context;
            HookNotifier();

            //Trace.WriteLine("Create " + this + " in Thread(" + Thread.CurrentThread.ManagedThreadId + ").", "Information");
        }
Example #13
0
        internal OpenCLKernel(string functionName, OpenCLProgram program)
        {
            OpenCLErrorCode error = OpenCLErrorCode.Success;

            Handle = CL10.CreateKernel(program.Handle, functionName, out error);
            OpenCLException.ThrowOnError(error);

            SetID(Handle.Value);

            context           = program.Context;
            this.functionName = functionName;
            this.program      = program;

            //Trace.WriteLine("Create " + this + " in Thread(" + Thread.CurrentThread.ManagedThreadId + ").", "Information");
        }
Example #14
0
        /// <summary>
        /// Creates a new <see cref="OpenCLSampler"/>.
        /// </summary>
        /// <param name="context"> A <see cref="OpenCLContext"/>. </param>
        /// <param name="normalizedCoords"> The usage state of normalized coordinates when accessing a <see cref="OpenCLImage"/> in a <see cref="OpenCLKernel"/>. </param>
        /// <param name="addressing"> The <see cref="OpenCLImageAddressing"/> mode of the <see cref="OpenCLSampler"/>. Specifies how out-of-range image coordinates are handled while reading. </param>
        /// <param name="filtering"> The <see cref="OpenCLImageFiltering"/> mode of the <see cref="OpenCLSampler"/>. Specifies the type of filter that must be applied when reading data from an image. </param>
        public OpenCLSampler(OpenCLContext context, bool normalizedCoords, OpenCLImageAddressing addressing, OpenCLImageFiltering filtering)
        {
            OpenCLErrorCode error = OpenCLErrorCode.Success;

            Handle = CL10.CreateSampler(context.Handle, normalizedCoords, addressing, filtering, out error);
            OpenCLException.ThrowOnError(error);

            SetID(Handle.Value);

            this.addressing       = addressing;
            this.context          = context;
            this.filtering        = filtering;
            this.normalizedCoords = normalizedCoords;

            //Trace.WriteLine("Create " + this + " in Thread(" + Thread.CurrentThread.ManagedThreadId + ").", "Information");
        }
Example #15
0
        /// <summary>
        /// Creates a new <see cref="OpenCLCommandQueue"/>.
        /// </summary>
        /// <param name="context"> A <see cref="OpenCLContext"/>. </param>
        /// <param name="device"> A <see cref="OpenCLDevice"/> associated with the <paramref name="context"/>. It can either be one of <see cref="OpenCLContext.Devices"/> or have the same <see cref="OpenCLDeviceTypes"/> as the <paramref name="device"/> specified when the <paramref name="context"/> is created. </param>
        /// <param name="properties"> The properties for the <see cref="OpenCLCommandQueue"/>. </param>
        public OpenCLCommandQueue(OpenCLContext context, OpenCLDevice device, OpenCLCommandQueueProperties properties)
        {
            OpenCLErrorCode error = OpenCLErrorCode.Success;

            Handle = CL10.CreateCommandQueue(context.Handle, device.Handle, properties, out error);
            OpenCLException.ThrowOnError(error);

            SetID(Handle.Value);

            this.device  = device;
            this.context = context;

            outOfOrderExec = ((properties & OpenCLCommandQueueProperties.OutOfOrderExecution) == OpenCLCommandQueueProperties.OutOfOrderExecution);
            profiling      = ((properties & OpenCLCommandQueueProperties.Profiling) == OpenCLCommandQueueProperties.Profiling);

            Events = new List <OpenCLEventBase>();

            //Trace.WriteLine("Create " + this + " in Thread(" + Thread.CurrentThread.ManagedThreadId + ").", "Information");
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="context"></param>
        /// <param name="flags"></param>
        protected OpenCLBufferBase(OpenCLContext context, OpenCLMemoryFlags flags, Type elementType, long[] count)
            : base(context, flags)
        {
            ElementType = elementType;
			this.Count = count;
        }
Example #17
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="context"></param>
        /// <param name="flags"></param>
        protected OpenCLImage(OpenCLContext context, OpenCLMemoryFlags flags)
			: base(context, flags)
        { }
Example #18
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="context"></param>
        /// <param name="flags"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        protected static ICollection<OpenCLImageFormat> GetSupportedFormats(OpenCLContext context, OpenCLMemoryFlags flags, OpenCLMemoryType type)
        {
            int formatCountRet = 0;
            OpenCLErrorCode error = CL10.GetSupportedImageFormats(context.Handle, flags, type, 0, null, out formatCountRet);
            OpenCLException.ThrowOnError(error);

            OpenCLImageFormat[] formats = new OpenCLImageFormat[formatCountRet];
            error = CL10.GetSupportedImageFormats(context.Handle, flags, type, formatCountRet, formats, out formatCountRet);
            OpenCLException.ThrowOnError(error);

            return new Collection<OpenCLImageFormat>(formats);
        }
        private static void RunKernel(OpenCLPlatform platform, OpenCLDevice device)
        {
            var context = new OpenCLContext(new List<OpenCLDevice> {device}, new OpenCLContextPropertyList(platform), null, IntPtr.Zero);
            var program = LoadProgram(context, device, "ReductionUsingFSCLOpenCLManagedWrapper.reduction.cl");
            var kernel1 = program.CreateKernel("reductionVector");
            var kernel2 = program.CreateKernel("reductionComplete");

            const int numValues = 1024 * 1024;
            const int numValuesPerWorkItem = 4;
            var globalWorkSize = numValues / numValuesPerWorkItem;
            const int localWorkSize = 32;
            var initialNumWorkGroups = globalWorkSize/localWorkSize;
            const int value = 42;
            var data = Enumerable.Repeat(value, numValues).Select(n => (float)n).ToArray();

            var commandQueue = new OpenCLCommandQueue(context, device, OpenCLCommandQueueProperties.None);

            var floatType = typeof (float);
            var floatSize = sizeof (float);

            var dataBuffer1 = new OpenCLBuffer(context, OpenCLMemoryFlags.ReadWrite | OpenCLMemoryFlags.AllocateHostPointer, floatType, new long[] {numValues});
            var dataBuffer2 = new OpenCLBuffer(context, OpenCLMemoryFlags.ReadWrite | OpenCLMemoryFlags.AllocateHostPointer, floatType, new long[] {initialNumWorkGroups*numValuesPerWorkItem});
            var sumBuffer = new OpenCLBuffer(context, OpenCLMemoryFlags.WriteOnly | OpenCLMemoryFlags.AllocateHostPointer, floatType, new long[] { 1 });
            var resultDataBuffer = dataBuffer2;

            using (var pinnedData = new PinnedObject(data))
            {
                commandQueue.WriteToBuffer(pinnedData, dataBuffer1, true, 0L, numValues);
            }

            foreach (var index in Enumerable.Range(0, int.MaxValue))
            {
                var dataBufferIn = index%2 == 0 ? dataBuffer1 : dataBuffer2;
                var dataBufferOut = index%2 == 0 ? dataBuffer2 : dataBuffer1;
                resultDataBuffer = dataBufferOut;

                kernel1.SetMemoryArgument(0, dataBufferIn);
                kernel1.SetMemoryArgument(1, dataBufferOut);
                kernel1.SetLocalArgument(2, localWorkSize*numValuesPerWorkItem*floatSize);

                Console.WriteLine($"Calling commandQueue.Execute(kernel1) with globalWorkSize: {globalWorkSize}; localWorkSize: {localWorkSize}; num work groups: {globalWorkSize / localWorkSize}");

                commandQueue.Execute(kernel1, null, new long[] {globalWorkSize}, new long[] {localWorkSize});

                globalWorkSize /= localWorkSize;
                if (globalWorkSize <= localWorkSize) break;
            }

            kernel2.SetMemoryArgument(0, resultDataBuffer);
            kernel2.SetLocalArgument(1, globalWorkSize*numValuesPerWorkItem*floatSize);
            kernel2.SetMemoryArgument(2, sumBuffer);

            Console.WriteLine($"Calling commandQueue.Execute(kernel2) with globalWorkSize: {globalWorkSize}; localWorkSize: {globalWorkSize}");

            commandQueue.Execute(kernel2, null, new long[] { globalWorkSize }, new long[] { globalWorkSize });

            commandQueue.Finish();

            var sum = new float[1];
            using (var pinnedSum = new PinnedObject(sum))
            {
                commandQueue.ReadFromBuffer(sumBuffer, pinnedSum, true, 0L, 1L);
            }

            const int correctAnswer = numValues * value;

            Console.WriteLine($"OpenCL final answer: {Math.Truncate(sum[0]):N0}; Correct answer: {correctAnswer:N0}");
        }
        /// <summary>
        /// Creates a new <see cref="OpenCLImage2D"/> from an OpenGL renderbuffer object.
        /// </summary>
        /// <param name="context"> A <see cref="OpenCLContext"/> with enabled CL/GL sharing. </param>
        /// <param name="flags"> A bit-field that is used to specify usage information about the <see cref="OpenCLImage2D"/>. Only <c>OpenCLMemoryFlags.ReadOnly</c>, <c>OpenCLMemoryFlags.WriteOnly</c> and <c>OpenCLMemoryFlags.ReadWrite</c> are allowed. </param>
        /// <param name="renderbufferId"> The OpenGL renderbuffer object id to use. </param>
        /// <returns> The created <see cref="OpenCLImage2D"/>. </returns>
        public static OpenCLImage2D CreateFromGLRenderbuffer(OpenCLContext context, OpenCLMemoryFlags flags, int renderbufferId)
        {
            OpenCLErrorCode error = OpenCLErrorCode.Success;
            CLMemoryHandle image = CL10.CreateFromGLRenderbuffer(context.Handle, flags, renderbufferId, out error);
            OpenCLException.ThrowOnError(error);

            return new OpenCLImage2D(image, context, flags);
        }
        /// <summary>
        /// Creates a new <see cref="OpenCLCommandQueue"/>.
        /// </summary>
        /// <param name="context"> A <see cref="OpenCLContext"/>. </param>
        /// <param name="device"> A <see cref="OpenCLDevice"/> associated with the <paramref name="context"/>. It can either be one of <see cref="OpenCLContext.Devices"/> or have the same <see cref="OpenCLDeviceTypes"/> as the <paramref name="device"/> specified when the <paramref name="context"/> is created. </param>
        /// <param name="properties"> The properties for the <see cref="OpenCLCommandQueue"/>. </param>
        public OpenCLCommandQueue(OpenCLContext context, OpenCLDevice device, OpenCLCommandQueueProperties properties)
        {
            OpenCLErrorCode error = OpenCLErrorCode.Success;
            Handle = CL10.CreateCommandQueue(context.Handle, device.Handle, properties, out error);
            OpenCLException.ThrowOnError(error);
            
            SetID(Handle.Value);
            
            this.device = device;
            this.context = context;

            outOfOrderExec = ((properties & OpenCLCommandQueueProperties.OutOfOrderExecution) == OpenCLCommandQueueProperties.OutOfOrderExecution);
            profiling = ((properties & OpenCLCommandQueueProperties.Profiling) == OpenCLCommandQueueProperties.Profiling);
            
            Events = new List<OpenCLEventBase>();

            //Trace.WriteLine("Create " + this + " in Thread(" + Thread.CurrentThread.ManagedThreadId + ").", "Information");
        }
Example #22
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="context"></param>
 /// <param name="flags"></param>
 protected OpenCLBufferBase(OpenCLContext context, OpenCLMemoryFlags flags, Type elementType, long[] count)
     : base(context, flags)
 {
     ElementType = elementType;
     this.Count  = count;
 }
Example #23
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="context"></param>
 /// <param name="flags"></param>
 protected OpenCLMemory(OpenCLContext context, OpenCLMemoryFlags flags)
 {
     this.context = context;
     this.flags   = flags;
 }
Example #24
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="context"></param>
 /// <param name="flags"></param>
 protected OpenCLImage(OpenCLContext context, OpenCLMemoryFlags flags)
     : base(context, flags)
 {
 }
Example #25
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="context"></param>
        /// <param name="flags"></param>
        protected OpenCLMemory(OpenCLContext context, OpenCLMemoryFlags flags)
        {
            this.context = context;
            this.flags = flags;

        }
        /// <summary>
        /// Creates a new <see cref="OpenCLProgram"/> from a specified list of binaries.
        /// </summary>
        /// <param name="context"> A <see cref="OpenCLContext"/>. </param>
        /// <param name="binaries"> A list of binaries, one for each item in <paramref name="devices"/>. </param>
        /// <param name="devices"> A subset of the <see cref="OpenCLContext.Devices"/>. If <paramref name="devices"/> is <c>null</c>, OpenCL will associate every binary from <see cref="OpenCLProgram.Binaries"/> with a corresponding <see cref="OpenCLDevice"/> from <see cref="OpenCLContext.Devices"/>. </param>
        public OpenCLProgram(OpenCLContext context, IList<byte[]> binaries, IList<OpenCLDevice> devices)
        {
            int count;

            CLDeviceHandle[] deviceHandles = (devices != null) ?
                OpenCLTools.ExtractHandles(devices, out count) :
                OpenCLTools.ExtractHandles(context.Devices, out count);

            IntPtr[] binariesPtrs = new IntPtr[count];
            IntPtr[] binariesLengths = new IntPtr[count];
            int[] binariesStats = new int[count];
            OpenCLErrorCode error = OpenCLErrorCode.Success;
            GCHandle[] binariesGCHandles = new GCHandle[count];

            try
            {
                for (int i = 0; i < count; i++)
                {
                    binariesGCHandles[i] = GCHandle.Alloc(binaries[i], GCHandleType.Pinned);
                    binariesPtrs[i] = binariesGCHandles[i].AddrOfPinnedObject();
                    binariesLengths[i] = new IntPtr(binaries[i].Length);
                }

                Handle = CL10.CreateProgramWithBinary(
                    context.Handle,
                    count,
                    deviceHandles,
                    binariesLengths,
                    binariesPtrs,
                    binariesStats,
                    out error);
                OpenCLException.ThrowOnError(error);
            }
            finally
            {
                for (int i = 0; i < count; i++)
                    binariesGCHandles[i].Free();
            }


            this.binaries = new ReadOnlyCollection<byte[]>(binaries);
            this.context = context;
            this.devices = new ReadOnlyCollection<OpenCLDevice>(
                (devices != null) ? devices : context.Devices);

            //Trace.WriteLine("Create " + this + " in Thread(" + Thread.CurrentThread.ManagedThreadId + ").", "Information");
        }
        /// <summary>
        /// Creates a new <see cref="OpenCLProgram"/> from an array of source code strings.
        /// </summary>
        /// <param name="context"> A <see cref="OpenCLContext"/>. </param>
        /// <param name="source"> The source code lines for the <see cref="OpenCLProgram"/>. </param>
        /// <remarks> The created <see cref="OpenCLProgram"/> is associated with the <see cref="OpenCLContext.Devices"/>. </remarks>
        public OpenCLProgram(OpenCLContext context, string[] source)
        {
            OpenCLErrorCode error = OpenCLErrorCode.Success;
            Handle = CL10.CreateProgramWithSource(
                context.Handle,
                source.Length,
                source,
                null,
                out error);
            OpenCLException.ThrowOnError(error);

            this.context = context;
            this.devices = context.Devices;
            this.source = new ReadOnlyCollection<string>(source);

            //Trace.WriteLine("Create " + this + " in Thread(" + Thread.CurrentThread.ManagedThreadId + ").", "Information");
        }
        /// <summary>
        /// Creates a new <see cref="OpenCLImage3D"/> from an OpenGL 3D texture object.
        /// </summary>
        /// <param name="context"> A <see cref="OpenCLContext"/> with enabled CL/GL sharing. </param>
        /// <param name="flags"> A bit-field that is used to specify usage information about the <see cref="OpenCLImage3D"/>. Only <c>OpenCLMemoryFlags.ReadOnly</c>, <c>OpenCLMemoryFlags.WriteOnly</c> and <c>OpenCLMemoryFlags.ReadWrite</c> are allowed. </param>
        /// <param name="textureTarget"> The image type of texture. Must be GL_TEXTURE_3D. </param>
        /// <param name="mipLevel"> The mipmap level of the OpenGL 2D texture object to be used. </param>
        /// <param name="textureId"> The OpenGL 2D texture object id to use. </param>
        /// <returns> The created <see cref="OpenCLImage2D"/>. </returns>
        public static OpenCLImage3D CreateFromGLTexture3D(OpenCLContext context, OpenCLMemoryFlags flags, int textureTarget, int mipLevel, int textureId)
        {
            CLMemoryHandle image;
            OpenCLErrorCode error = OpenCLErrorCode.Success;
            image = CL10.CreateFromGLTexture3D(context.Handle, flags, textureTarget, mipLevel, textureId, out error);
            OpenCLException.ThrowOnError(error);

            return new OpenCLImage3D(image, context, flags);
        }
Example #29
0
 /// <summary>
 /// Gets a collection of supported <see cref="OpenCLImage3D"/> <see cref="OpenCLImageFormat"/>s in a <see cref="OpenCLContext"/>.
 /// </summary>
 /// <param name="context"> The <see cref="OpenCLContext"/> for which the collection of <see cref="OpenCLImageFormat"/>s is queried. </param>
 /// <param name="flags"> The <c>OpenCLMemoryFlags</c> for which the collection of <see cref="OpenCLImageFormat"/>s is queried. </param>
 /// <returns> The collection of the required <see cref="OpenCLImageFormat"/>s. </returns>
 public static ICollection <OpenCLImageFormat> GetSupportedFormats(OpenCLContext context, OpenCLMemoryFlags flags)
 {
     return(GetSupportedFormats(context, flags, OpenCLMemoryType.Image3D));
 }
        /// <summary>
        /// Creates a new <see cref="OpenCLSampler"/>.
        /// </summary>
        /// <param name="context"> A <see cref="OpenCLContext"/>. </param>
        /// <param name="normalizedCoords"> The usage state of normalized coordinates when accessing a <see cref="OpenCLImage"/> in a <see cref="OpenCLKernel"/>. </param>
        /// <param name="addressing"> The <see cref="OpenCLImageAddressing"/> mode of the <see cref="OpenCLSampler"/>. Specifies how out-of-range image coordinates are handled while reading. </param>
        /// <param name="filtering"> The <see cref="OpenCLImageFiltering"/> mode of the <see cref="OpenCLSampler"/>. Specifies the type of filter that must be applied when reading data from an image. </param>
        public OpenCLSampler(OpenCLContext context, bool normalizedCoords, OpenCLImageAddressing addressing, OpenCLImageFiltering filtering)
        {
            OpenCLErrorCode error = OpenCLErrorCode.Success;
            Handle = CL10.CreateSampler(context.Handle, normalizedCoords, addressing, filtering, out error);
            OpenCLException.ThrowOnError(error);

            SetID(Handle.Value);
            
            this.addressing = addressing;
            this.context = context;
            this.filtering = filtering;
            this.normalizedCoords = normalizedCoords;

            //Trace.WriteLine("Create " + this + " in Thread(" + Thread.CurrentThread.ManagedThreadId + ").", "Information");
        }
 /// <summary>
 /// Gets a collection of supported <see cref="OpenCLImage3D"/> <see cref="OpenCLImageFormat"/>s in a <see cref="OpenCLContext"/>.
 /// </summary>
 /// <param name="context"> The <see cref="OpenCLContext"/> for which the collection of <see cref="OpenCLImageFormat"/>s is queried. </param>
 /// <param name="flags"> The <c>OpenCLMemoryFlags</c> for which the collection of <see cref="OpenCLImageFormat"/>s is queried. </param>
 /// <returns> The collection of the required <see cref="OpenCLImageFormat"/>s. </returns>
 public static ICollection<OpenCLImageFormat> GetSupportedFormats(OpenCLContext context, OpenCLMemoryFlags flags)
 {
     return GetSupportedFormats(context, flags, OpenCLMemoryType.Image3D);
 }
        private static OpenCLProgram LoadProgram(OpenCLContext context, OpenCLDevice device, string resourceName)
        {
            var source = GetProgramSourceFromResource(Assembly.GetExecutingAssembly(), resourceName);
            var program = new OpenCLProgram(context, source);

            try
            {
                program.Build(new List<OpenCLDevice> {device}, string.Empty, null, IntPtr.Zero);
            }
            catch (BuildProgramFailureOpenCLException)
            {
                var buildLog = program.GetBuildLog(device);
                throw new ApplicationException($"Error building program \"{resourceName}\":{Environment.NewLine}{buildLog}");
            }

            return program;
        }