Exemplo n.º 1
0
 public Image CreateImage(MemoryFlags flags, ImageFormat format, ImageDescriptor descriptor)
 {
     return CreateImage(flags, format, descriptor, IntPtr.Zero);
 }
Exemplo n.º 2
0
 internal BufferMemoryUsageConfiguration(IOpenClApi api, Context context, Action <Buffer <T> > memoryObjectCreatedCallback, MemoryFlags flags, Memory <T> hostMemory)
     : base(api, context, memoryObjectCreatedCallback, flags)
 {
     _hostMemory = hostMemory;
 }
Exemplo n.º 3
0
        private unsafe void ExecuteKernel(
            Context context,
            Device device,
            CommandQueue commandQueue,
            Kernel kernel,
            float[] input,
            float[] output,
            int globalWorkSize,
            int localWorkSize,
            bool warming,
            bool useHostPointer,
            bool autoGroupSize,
            bool enableProfiling,
            out TimeSpan stopwatchTime,
            out TimeSpan profiledTime,
            out TimeSpan readTime)
        {
            MemoryFlags inFlags  = (useHostPointer ? MemoryFlags.UseHostPointer : MemoryFlags.CopyHostPointer) | MemoryFlags.ReadOnly;
            MemoryFlags outFlags = (useHostPointer ? MemoryFlags.UseHostPointer : MemoryFlags.CopyHostPointer) | MemoryFlags.ReadWrite;

            int taskSize = input.Length;

            // allocate buffers
            fixed(float *pinput = input, poutput = output)
            {
                using (Buffer inputBuffer = context.CreateBuffer(inFlags, sizeof(float) * taskSize, (IntPtr)pinput),
                       outputBuffer = context.CreateBuffer(outFlags, sizeof(float) * taskSize, (IntPtr)poutput))
                {
                    kernel.Arguments[0].SetValue(inputBuffer);
                    kernel.Arguments[1].SetValue(outputBuffer);

                    Console.WriteLine("Original global work size {0}", globalWorkSize);
                    Console.WriteLine("Original local work size {0}", localWorkSize);
                    if (autoGroupSize)
                    {
                        Console.WriteLine("Run-time determines optimal workgroup size");
                    }

                    IntPtr workGroupSizeMaximum = kernel.GetWorkGroupSize(device);
                    Console.WriteLine("Maximum workgroup size for this kernel  {0}", workGroupSizeMaximum.ToInt64());

                    if (warming)
                    {
                        Console.Write("Warming up OpenCL execution...");
                        using (commandQueue.EnqueueNDRangeKernel(kernel, new[] { (IntPtr)globalWorkSize }, autoGroupSize ? null : new[] { (IntPtr)localWorkSize }))
                        {
                        }

                        commandQueue.Finish();
                        Console.WriteLine("Done");
                    }

                    Console.Write("Executing OpenCL kernel...");
                    var timer = Stopwatch.StartNew();

                    // execute kernel, pls notice autoGroupSize
                    using (Event perfEvent = commandQueue.EnqueueNDRangeKernel(kernel, new[] { (IntPtr)globalWorkSize }, autoGroupSize ? null : new[] { (IntPtr)localWorkSize }))
                    {
                        Event.WaitAll(perfEvent);
                        stopwatchTime = timer.Elapsed;

                        Console.WriteLine("Done");

                        if (enableProfiling)
                        {
                            ulong start = perfEvent.CommandStartTime;
                            ulong end   = perfEvent.CommandEndTime;

                            // a tick is 100ns
                            profiledTime = TimeSpan.FromTicks((long)(end - start) / 100);
                        }
                        else
                        {
                            profiledTime = TimeSpan.Zero;
                        }
                    }

                    timer.Restart();
                    if (useHostPointer)
                    {
                        IntPtr tmpPtr;
                        using (commandQueue.EnqueueMapBuffer(outputBuffer, true, MapFlags.Read, 0, sizeof(float) * taskSize, out tmpPtr))
                        {
                        }

                        Assert.AreEqual((IntPtr)poutput, tmpPtr, "EnqueueMapBuffer failed to return original pointer");
                        using (commandQueue.EnqueueUnmapMemObject(outputBuffer, tmpPtr))
                        {
                        }
                    }
                    else
                    {
                        using (commandQueue.EnqueueReadBuffer(outputBuffer, true, 0, sizeof(float) * taskSize, (IntPtr)poutput))
                        {
                        }
                    }

                    commandQueue.Finish();
                    readTime = timer.Elapsed;
                }
            }
        }
Exemplo n.º 4
0
 public Buffer CreateBuffer(MemoryFlags flags, long size)
 {
     return CreateBuffer(flags, size, IntPtr.Zero);
 }
Exemplo n.º 5
0
        public static IntPtr Create(IntPtr address, uint size, MemoryProtection protection, MemoryFlags memoryflags, int fileDescriptor, uint offset)
        {
            var result = mmap(address, new UIntPtr(size), (int)protection, (int)memoryflags, fileDescriptor, new UIntPtr(offset));

            ThrowOnError <MemoryMapFailedException>(result);
            return(result);
        }
Exemplo n.º 6
0
 public CLArgumentInfo SetReturnArgument <T>(int index, long size, MemoryFlags flags) where T : struct
 {
     MethodInfo.Arguments[index].CopyBack      = true;
     MethodInfo.Arguments[index].ComputeMemory = new ComputeBuffer <T>(context, (ComputeMemoryFlags)flags, size);
     return(MethodInfo.Arguments[index]);
 }
Exemplo n.º 7
0
 /// <summary>
 /// Creates a 3D <see cref="Image"/>.
 /// </summary>
 /// <param name="width">The width of the image.</param>
 /// <param name="height">The height of the image.</param>
 /// <param name="depth">The depth of the image.</param>
 /// <param name="memoryFlags">Is used to specify allocation and usage information about the image memory object being created.</param>
 /// <param name="channelOrder">Specifies the number of channels and the channel layout.</param>
 /// <param name="channelType">Describes the size of the channel data type.</param>
 /// <returns>A new image.</returns>
 public Image CreateImage3D(UInt64 width, UInt64 height, UInt64 depth, MemoryFlags memoryFlags, ImageChannelOrder channelOrder, ImageChannelType channelType)
 {
     return(CreateImage3D <dynamic>(null, width, height, depth, memoryFlags, channelOrder, channelType));
 }
Exemplo n.º 8
0
 public Buffer <T> CreateBuffer <T>(MemoryFlags flags, long size)
     where T : struct
 {
     return(CreateBuffer <T>(flags, size, IntPtr.Zero));
 }
Exemplo n.º 9
0
 /// <summary>
 /// Creates an array of 2D <see cref="Image"/>s.
 /// </summary>
 /// <param name="arraySize">The number of images in the image array.</param>
 /// <param name="width">The width of the image.</param>
 /// <param name="height">The height of the image.</param>
 /// <param name="memoryFlags">Is used to specify allocation and usage information about the image memory object being created.</param>
 /// <param name="channelOrder">Specifies the number of channels and the channel layout.</param>
 /// <param name="channelType">Describes the size of the channel data type.</param>
 /// <returns>A new image.</returns>
 public Image CreateImage2DArray(UInt64 arraySize, UInt64 width, UInt64 height, MemoryFlags memoryFlags, ImageChannelOrder channelOrder, ImageChannelType channelType)
 {
     return(CreateImage2DArray <dynamic>(null, arraySize, width, height, memoryFlags, channelOrder, channelType));
 }
Exemplo n.º 10
0
        /// <summary>
        /// Creates a 3D <see cref="Image"/>.
        /// </summary>
        /// <typeparam name="DataType">The type of image data.</typeparam>
        /// <param name="data">An array of data to use with the image.</param>
        /// <param name="width">The width of the image.</param>
        /// <param name="height">The height of the image.</param>
        /// <param name="depth">The depth of the image.</param>
        /// <param name="memoryFlags">Is used to specify allocation and usage information about the image memory object being created.</param>
        /// <param name="channelOrder">Specifies the number of channels and the channel layout.</param>
        /// <param name="channelType">Describes the size of the channel data type.</param>
        /// <returns>A new image.</returns>
        public Image CreateImage3D <DataType>(DataType[] data, UInt64 width, UInt64 height, UInt64 depth, MemoryFlags memoryFlags, ImageChannelOrder channelOrder, ImageChannelType channelType)
        {
            ImageFormat format = new ImageFormat
            {
                ChannelOrder = channelOrder,
                ChannelType  = channelType
            };
            ImageDescriptor descriptor = new ImageDescriptor
            {
                ImageType       = MemoryObjectType.Image3D,
                Width           = new UIntPtr(width),
                Height          = new UIntPtr(height),
                Depth           = new UIntPtr(depth),
                ImageArraySize  = UIntPtr.Zero,
                ImageRowPitch   = UIntPtr.Zero,
                ImageSlicePitch = UIntPtr.Zero,
                NumMipLevels    = 0,
                NumSamples      = 0,
                Buffer          = IntPtr.Zero
            };

            return(CreateImage <DataType>(data, memoryFlags, format, descriptor));
        }
Exemplo n.º 11
0
 /// <summary>
 /// Creates a monodimensional <see cref="Image"/> using the data from a buffer.
 /// </summary>
 /// <param name="buffer">The buffer from where the image pixels are taken.</param>
 /// <param name="width">The width of the image.</param>
 /// <param name="memoryFlags">Is used to specify allocation and usage information about the image memory object being created.</param>
 /// <param name="channelOrder">Specifies the number of channels and the channel layout.</param>
 /// <param name="channelType">Describes the size of the channel data type.</param>
 /// <returns>A new image.</returns>
 public Image CreateImage1DBuffer(MemoryObject buffer, UInt64 width, MemoryFlags memoryFlags, ImageChannelOrder channelOrder, ImageChannelType channelType)
 {
     return(CreateImage1DBuffer <dynamic>(buffer, null, width, memoryFlags, channelOrder, channelType));
 }
Exemplo n.º 12
0
 /// <summary>
 /// Creates a monodimensional <see cref="Image"/>.
 /// </summary>
 /// <param name="width">The width of the image.</param>
 /// <param name="memoryFlags">Is used to specify allocation and usage information about the image memory object being created.</param>
 /// <param name="channelOrder">Specifies the number of channels and the channel layout.</param>
 /// <param name="channelType">Describes the size of the channel data type.</param>
 /// <returns>A new image.</returns>
 public Image CreateImage1D(UInt64 width, MemoryFlags memoryFlags, ImageChannelOrder channelOrder, ImageChannelType channelType)
 {
     return(CreateImage1D <dynamic>(null, width, memoryFlags, channelOrder, channelType));
 }
Exemplo n.º 13
0
		public extern static IntPtr clCreateBuffer (
			IntPtr context,
			MemoryFlags flags,
			IntPtr size,
			IntPtr host_ptr,
			out int errcode_ret);
Exemplo n.º 14
0
 public extern static IntPtr clCreateBuffer(
     IntPtr context,
     MemoryFlags flags,
     IntPtr size,
     IntPtr host_ptr,
     out int errcode_ret);
Exemplo n.º 15
0
 private extern static IntPtr clCreateBuffer(
     IntPtr context,
     MemoryFlags flags,
     UIntPtr size,
     IntPtr host_ptr,
     out ErrorCode errcode_ret);
Exemplo n.º 16
0
 public static extern CLBuffer CreateFromGLRenderbuffer(
     [In] CLContext context,
     [In] MemoryFlags flags,
     [In] int renderBuffer,
     [Out] out CLResultCode error
     );
Exemplo n.º 17
0
 public static Buffer Create <T>(Context context, MemoryFlags flags, T[] data) where T : struct
 {
     return(Create(context, flags, data, 0, data.Length));
 }
Exemplo n.º 18
0
 public Image CreateImage(MemoryFlags flags, ImageFormat format, ImageDescriptor descriptor)
 {
     return(CreateImage(flags, format, descriptor, IntPtr.Zero));
 }
Exemplo n.º 19
0
        public static Buffer Create <T>(Context context, MemoryFlags flags, T[] data, int index, int count) where T : struct
        {
            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 (data == null)
            {
                throw new ArgumentNullException("data");
            }

            if (index < 0)
            {
                throw new ArgumentOutOfRangeException("index", index, "index is less than 0.");
            }
            if (count == 0)
            {
                throw new ArgumentOutOfRangeException("count", count, "count is 0.");
            }
            if (index + count > data.Length)
            {
                throw new ArgumentOutOfRangeException("count", count, "index + count is greater than data.Length.");
            }

            var handle = GCHandle.Alloc(data, GCHandleType.Pinned);
            var size   = Marshal.SizeOf(typeof(T));
            var ptr    = handle.AddrOfPinnedObject();

            flags |= MemoryFlags.CopyHostPtr;

            Buffer buffer = Buffer.Null;

            try
            {
                buffer = new Buffer(context, flags, (ulong)(size * count), IntPtr.Add(ptr, size * index));
            }
            finally
            {
                handle.Free();
            }

            return(buffer);
        }
Exemplo n.º 20
0
 public CLArgumentInfo SetArgument <T>(int index, T[] Value, MemoryFlags flags) where T : struct
 {
     MethodInfo.Arguments[index].ComputeMemory = new ComputeBuffer <T>(context, (ComputeMemoryFlags)flags, Value);
     return(MethodInfo.Arguments[index]);
 }
Exemplo n.º 21
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);
            }
        }
Exemplo n.º 22
0
 public CLArgument(bool copyback, T[] data, MemoryFlags flags)
 {
     CopyBack = copyback;
     Data     = data;
     Flags    = flags;
 }
Exemplo n.º 23
0
 public Buffer CreateBuffer(MemoryFlags flags, long size)
 {
     return(CreateBuffer(flags, size, IntPtr.Zero));
 }
 public void SetFlag(MemoryFlags flag)
 {
     _flags = _flags | flag;
 }
Exemplo n.º 25
0
        public Buffer CreateSubBuffer(MemoryFlags flags, BufferRegion regionInfo)
        {
            BufferSafeHandle subBuffer = UnsafeNativeMethods.CreateSubBuffer(Handle, flags, regionInfo);

            return(new Buffer(Context, this, subBuffer));
        }
Exemplo n.º 26
0
        public Buffer CreateBuffer(MemoryFlags flags, long size, IntPtr hostAddress)
        {
            if (size < 0)
                throw new ArgumentOutOfRangeException("size");
            else if (size == 0)
                throw new ArgumentException("Invalid buffer size.");

            if (hostAddress == IntPtr.Zero && (flags & (MemoryFlags.UseHostPointer | MemoryFlags.CopyHostPointer)) != 0)
                throw new ArgumentException("Invalid host address.");
            if (hostAddress != IntPtr.Zero && (flags & (MemoryFlags.UseHostPointer | MemoryFlags.CopyHostPointer)) == 0)
                throw new ArgumentException("Invalid host address.");

            BufferSafeHandle handle = UnsafeNativeMethods.CreateBuffer(Handle, flags, (IntPtr)size, hostAddress);
            return new Buffer(this, handle);
        }
Exemplo n.º 27
0
 public static extern IntPtr LocalAlloc(
     [In, MarshalAs(UnmanagedType.I4)] MemoryFlags flags,
     int size);
Exemplo n.º 28
0
        public Image CreateImage(MemoryFlags flags, ImageFormat format, ImageDescriptor descriptor, IntPtr hostAddress)
        {
            if (hostAddress == IntPtr.Zero && (flags & (MemoryFlags.UseHostPointer | MemoryFlags.CopyHostPointer)) != 0)
                throw new ArgumentException("Invalid host address.");
            if (hostAddress != IntPtr.Zero && (flags & (MemoryFlags.UseHostPointer | MemoryFlags.CopyHostPointer)) == 0)
                throw new ArgumentException("Invalid host address.");

            ImageSafeHandle handle = UnsafeNativeMethods.CreateImage(Handle, flags, ref format, ref descriptor, hostAddress);
            return new Image(this, handle);
        }
Exemplo n.º 29
0
 /// <summary>
 /// Constructor of the <see cref="Buffer"/> object. Don't use this constructor, use <see cref="Context.CreateBuffer{DataType}(DataType[], MemoryFlags)"/> or <see cref="Context.CreateBuffer{DataType}(ulong, MemoryFlags)"/> instead.
 /// </summary>
 public Buffer(IntPtr handle, Context context, UInt64 length, UInt64 itemSize, MemoryObjectType objectType, MemoryFlags memoryFlags) : base(handle, context, objectType, memoryFlags)
 {
     Length   = length;
     ItemSize = itemSize;
 }