コード例 #1
0
ファイル: ComputeBuffer.cs プロジェクト: myso42/Amplifier.NET
        /// <summary>
        /// Creates a new <see cref="ComputeBuffer{T}"/>.
        /// </summary>
        /// <param name="context"> A <see cref="ComputeContext"/> used to create the <see cref="ComputeBuffer{T}"/>. </param>
        /// <param name="flags"> A bit-field that is used to specify allocation and usage information about the <see cref="ComputeBuffer{T}"/>. </param>
        /// <param name="data"> The data for the <see cref="ComputeBuffer{T}"/>. </param>
        /// <remarks> Note, that <paramref name="data"/> cannot be an "immediate" parameter, i.e.: <c>new T[100]</c>, because it could be quickly collected by the GC causing Amplifier.OpenCL.Cloo to send and invalid reference to OpenCL. </remarks>
        public ComputeBuffer(ComputeContext context, ComputeMemoryFlags flags, T[] data)
            : base(context, flags)
        {
            var size = ComputeTools.SizeOf <T>() * data.Length;

            GCHandle dataPtr = GCHandle.Alloc(data, GCHandleType.Pinned);

            try
            {
                Handle = CL12.CreateBuffer(context.Handle, flags, new IntPtr(size), dataPtr.AddrOfPinnedObject(), out var error);
                ComputeException.ThrowOnError(error);
            }
            finally
            {
                dataPtr.Free();
            }

            Init(size, data.Length);
        }
コード例 #2
0
        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="THandleType"></typeparam>
        /// <typeparam name="TInfoType"></typeparam>
        /// <typeparam name="TQueriedType"></typeparam>
        /// <param name="handle"></param>
        /// <param name="paramName"></param>
        /// <param name="getInfoDelegate"></param>
        /// <returns></returns>
        protected static TQueriedType GetInfo <THandleType, TInfoType, TQueriedType>
            (THandleType handle, TInfoType paramName, GetInfoDelegate <THandleType, TInfoType> getInfoDelegate)
            where TQueriedType : struct
        {
            TQueriedType result   = new TQueriedType();
            GCHandle     gcHandle = GCHandle.Alloc(result, GCHandleType.Pinned);

            try
            {
                ComputeErrorCode error = getInfoDelegate(handle, paramName, (IntPtr)Marshal.SizeOf(result), gcHandle.AddrOfPinnedObject(), out _);
                ComputeException.ThrowOnError(error);
            }
            finally
            {
                result = (TQueriedType)gcHandle.Target;
                gcHandle.Free();
            }
            return(result);
        }
コード例 #3
0
        /// <summary>
        /// Gets a read-only collection of available <see cref="ComputeDevice"/>s on the <see cref="ComputePlatform"/>.
        /// </summary>
        /// <returns> A read-only collection of the available <see cref="ComputeDevice"/>s on the <see cref="ComputePlatform"/>. </returns>
        /// <remarks> This method resets the <c>ComputePlatform.Devices</c>. This is useful if one or more of them become unavailable (<c>ComputeDevice.Available</c> is <c>false</c>) after a <see cref="ComputeContext"/> and <see cref="ComputeCommandQueue"/>s that use the <see cref="ComputeDevice"/> have been created and commands have been queued to them. Further calls will trigger an <c>OutOfResourcesComputeException</c> until this method is executed. You will also need to recreate any <see cref="ComputeResource"/> that was created on the no longer available <see cref="ComputeDevice"/>. </remarks>
        public ReadOnlyCollection <ComputeDevice> QueryDevices()
        {
            ComputeErrorCode error = CL12.GetDeviceIDs(Handle, ComputeDeviceTypes.All, 0, null, out var handlesLength);

            ComputeException.ThrowOnError(error);

            CLDeviceHandle[] handles = new CLDeviceHandle[handlesLength];
            error = CL12.GetDeviceIDs(Handle, ComputeDeviceTypes.All, handlesLength, handles, out handlesLength);
            ComputeException.ThrowOnError(error);

            ComputeDevice[] devices = new ComputeDevice[handlesLength];
            for (int i = 0; i < handlesLength; i++)
            {
                devices[i] = new ComputeDevice(this, handles[i]);
            }

            _devices = new ReadOnlyCollection <ComputeDevice>(devices);

            return(_devices);
        }
コード例 #4
0
        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="TMainHandleType"></typeparam>
        /// <typeparam name="TSecondHandleType"></typeparam>
        /// <typeparam name="TInfoType"></typeparam>
        /// <typeparam name="TQueriedType"></typeparam>
        /// <param name="mainHandle"></param>
        /// <param name="secondHandle"></param>
        /// <param name="paramName"></param>
        /// <param name="getInfoDelegate"></param>
        /// <returns></returns>
        protected static TQueriedType[] GetArrayInfo <TMainHandleType, TSecondHandleType, TInfoType, TQueriedType>
            (TMainHandleType mainHandle, TSecondHandleType secondHandle, TInfoType paramName, GetInfoDelegateEx <TMainHandleType, TSecondHandleType, TInfoType> getInfoDelegate)
        {
            var error = getInfoDelegate(mainHandle, secondHandle, paramName, IntPtr.Zero, IntPtr.Zero, out var bufferSizeRet);

            ComputeException.ThrowOnError(error);
            var      buffer   = new TQueriedType[bufferSizeRet.ToInt64() / Marshal.SizeOf(typeof(TQueriedType))];
            GCHandle gcHandle = GCHandle.Alloc(buffer, GCHandleType.Pinned);

            try
            {
                error = getInfoDelegate(mainHandle, secondHandle, paramName, bufferSizeRet, gcHandle.AddrOfPinnedObject(), out bufferSizeRet);
                ComputeException.ThrowOnError(error);
            }
            finally
            {
                gcHandle.Free();
            }
            return(buffer);
        }
コード例 #5
0
        public static void Unload()
        {
            ComputeErrorCode error = CL12.UnloadCompiler();

            ComputeException.ThrowOnError(error);
        }
コード例 #6
0
        /// <summary>
        /// Sets the new status of the <see cref="ComputeUserEvent"/> to an error value.
        /// </summary>
        /// <param name="status"> The error status of the <see cref="ComputeUserEvent"/>. This should be a negative value. </param>
        public void SetStatus(int status)
        {
            ComputeErrorCode error = CL11.SetUserEventStatus(Handle, status);

            ComputeException.ThrowOnError(error);
        }