Exemplo n.º 1
0
        /// <summary>
        /// Loads an implicitly grouped kernel on the current accelerator.
        /// </summary>
        /// <param name="kernel">The compiled kernel to load.</param>
        /// <param name="customGroupSize">The user-defined group size.</param>
        /// <param name="kernelInfo">The resolved kernel information.</param>
        /// <returns>The loaded kernel.</returns>
        protected sealed override Kernel LoadImplicitlyGroupedKernelInternal(
            CompiledKernel kernel,
            int customGroupSize,
            out KernelInfo kernelInfo)
        {
            if (kernel == null)
            {
                throw new ArgumentNullException(nameof(kernel));
            }
            if (customGroupSize < 0 || customGroupSize > MaxNumThreadsPerGroup)
            {
                throw new ArgumentOutOfRangeException(nameof(customGroupSize));
            }
            if (!(kernel is TCompiledKernel compiledKernel))
            {
                throw new NotSupportedException(RuntimeErrorMessages.NotSupportedKernel);
            }
            if (kernel.EntryPoint.IsExplicitlyGrouped)
            {
                throw new NotSupportedException(
                          RuntimeErrorMessages.NotSupportedExplicitlyGroupedKernel);
            }

            kernelInfo = KernelInfo.CreateFrom(
                kernel.Info,
                customGroupSize,
                null);
            return(CreateKernel(
                       compiledKernel,
                       GenerateKernelLauncherMethod(compiledKernel, customGroupSize)));
        }
Exemplo n.º 2
0
 /// <summary>
 /// Loads the given implicitly-grouped kernel while using an automatically
 /// computed grouping configuration.
 /// </summary>
 /// <param name="kernel">The kernel to load.</param>
 /// <param name="kernelInfo">
 /// Detailed kernel information about the loaded kernel.
 /// </param>
 /// <returns>The loaded kernel.</returns>
 /// <remarks>
 /// Note that the returned kernel will not be managed by the kernel cache.
 /// </remarks>
 public Kernel LoadAutoGroupedKernel(
     CompiledKernel kernel,
     out KernelInfo kernelInfo)
 {
     Bind();
     return(LoadAutoGroupedKernelInternal(kernel, out kernelInfo));
 }
Exemplo n.º 3
0
        /// <summary>
        /// Loads an auto grouped kernel on the current accelerator.
        /// </summary>
        /// <param name="kernel">The compiled kernel to load.</param>
        /// <param name="kernelInfo">The resolved kernel information.</param>
        /// <returns>The loaded kernel.</returns>
        protected sealed override Kernel LoadAutoGroupedKernelInternal(
            CompiledKernel kernel,
            out KernelInfo kernelInfo)
        {
            if (kernel == null)
            {
                throw new ArgumentNullException(nameof(kernel));
            }
            if (!(kernel is TCompiledKernel compiledKernel))
            {
                throw new NotSupportedException(RuntimeErrorMessages.NotSupportedKernel);
            }
            if (kernel.EntryPoint.IsExplicitlyGrouped)
            {
                throw new NotSupportedException(
                          RuntimeErrorMessages.NotSupportedExplicitlyGroupedKernel);
            }

            var result    = CreateKernel(compiledKernel);
            int groupSize = EstimateGroupSizeInternal(result, 0, 0, out int minGridSize);

            result.Launcher = GenerateKernelLauncherMethod(compiledKernel, groupSize);
            kernelInfo      = KernelInfo.CreateFrom(
                kernel.Info,
                groupSize,
                minGridSize);
            return(result);
        }
Exemplo n.º 4
0
 /// <summary>
 /// Loads the given implicitly-grouped kernel while using an automatically
 /// computed grouping configuration.
 /// </summary>
 /// <param name="kernel">The kernel to load.</param>
 /// <param name="groupSize">The estimated group size to gain maximum occupancy on this device.</param>
 /// <param name="minGridSize">The minimum grid size to gain maximum occupancy on this device.</param>
 /// <returns>The loaded kernel.</returns>
 /// <remarks>
 /// Note that the returned kernel will not be managed by the kernel cache.
 /// </remarks>
 public Kernel LoadAutoGroupedKernel(
     CompiledKernel kernel,
     out int groupSize,
     out int minGridSize)
 {
     Bind(); return(LoadAutoGroupedKernelInternal(kernel, out groupSize, out minGridSize));
 }
Exemplo n.º 5
0
 /// <summary>
 /// Loads an automatically grouped kernel.
 /// </summary>
 public Kernel LoadKernel(
     Accelerator accelerator,
     CompiledKernel compiledKernel,
     out KernelInfo kernelInfo) =>
 accelerator.LoadAutoGroupedKernel(
     compiledKernel,
     out kernelInfo);
Exemplo n.º 6
0
 /// <summary>
 /// Loads an implicitly grouped kernel.
 /// </summary>
 public Kernel LoadKernel(
     Accelerator accelerator,
     CompiledKernel compiledKernel,
     out KernelInfo kernelInfo) =>
 accelerator.LoadImplicitlyGroupedKernel(
     compiledKernel,
     GroupSize,
     out kernelInfo);
Exemplo n.º 7
0
 /// <summary cref="Accelerator.LoadAutoGroupedKernelInternal(CompiledKernel, out int, out int)"/>
 protected override Kernel LoadAutoGroupedKernelInternal(
     CompiledKernel kernel,
     out int groupSize,
     out int minGridSize)
 {
     groupSize   = WarpSize;
     minGridSize = NumThreads / WarpSize;
     return(LoadKernel(kernel, groupSize));
 }
Exemplo n.º 8
0
 internal CudaKernel(
     CudaAccelerator accelerator,
     CompiledKernel kernel,
     MethodInfo launcher)
     : base(accelerator, kernel, launcher)
 {
     CudaException.ThrowIfFailed(CudaAPI.Current.LoadModule(out modulePtr, kernel.GetBuffer()));
     CudaException.ThrowIfFailed(CudaAPI.Current.GetModuleFunction(out functionPtr, modulePtr, kernel.EntryName));
 }
Exemplo n.º 9
0
 /// <summary>
 /// Constructs a new kernel.
 /// </summary>
 /// <param name="accelerator">The associated accelerator.</param>
 /// <param name="compiledKernel">The source kernel.</param>
 /// <param name="launcher">The launcher method for the given kernel.</param>
 protected Kernel(
     Accelerator accelerator,
     CompiledKernel compiledKernel,
     MethodInfo launcher)
     : base(accelerator)
 {
     CompiledKernel = compiledKernel ?? throw new ArgumentNullException(nameof(compiledKernel));
     Launcher       = launcher;
 }
Exemplo n.º 10
0
 /// <summary>
 /// Loads a compiled kernel into the given Cuda context as kernel program.
 /// </summary>
 /// <param name="accelerator">The associated accelerator.</param>
 /// <param name="kernel">The source kernel.</param>
 /// <param name="launcher">The launcher method for the given kernel.</param>
 /// <param name="kernelExecutionDelegate">The execution method.</param>
 internal CPUKernel(
     CPUAccelerator accelerator,
     CompiledKernel kernel,
     MethodInfo launcher,
     CPUKernelExecutionHandler kernelExecutionDelegate)
     : base(accelerator, kernel, launcher)
 {
     KernelExecutionDelegate = kernelExecutionDelegate ?? throw new ArgumentNullException(nameof(kernelExecutionDelegate));
 }
Exemplo n.º 11
0
        /// <summary>
        /// Loads an auto grouped kernel.
        /// </summary>
        protected override Kernel LoadAutoGroupedKernelInternal(
            CompiledKernel kernel,
            out KernelInfo kernelInfo)
        {
            var result = LoadKernel(kernel, WarpSize);

            kernelInfo = new KernelInfo(WarpSize, NumThreads / WarpSize);
            return(result);
        }
Exemplo n.º 12
0
 /// <summary>
 /// Loads the given implicitly-grouped kernel.
 /// </summary>
 /// <param name="kernel">The kernel to load.</param>
 /// <param name="customGroupSize">The custom group size to use.</param>
 /// <returns>The loaded kernel.</returns>
 /// <remarks>
 /// Note that implicitly-grouped kernel will be launched with the given group
 /// size.
 /// </remarks>
 /// <remarks>
 /// Note that the returned kernel will not be managed by the kernel cache.
 /// </remarks>
 public Kernel LoadImplicitlyGroupedKernel(
     CompiledKernel kernel,
     int customGroupSize)
 {
     Bind();
     return(LoadImplicitlyGroupedKernelInternal(
                kernel,
                customGroupSize,
                out var _));
 }
Exemplo n.º 13
0
 /// <summary cref="Accelerator.LoadImplicitlyGroupedKernelInternal(CompiledKernel, int)"/>
 protected override Kernel LoadImplicitlyGroupedKernelInternal(
     CompiledKernel kernel,
     int customGroupSize)
 {
     if (customGroupSize < 0)
     {
         throw new ArgumentOutOfRangeException(nameof(customGroupSize));
     }
     return(LoadKernel(kernel, customGroupSize));
 }
Exemplo n.º 14
0
 /// <summary>
 /// Constructs a new kernel.
 /// </summary>
 /// <param name="accelerator">The associated accelerator.</param>
 /// <param name="compiledKernel">The source kernel.</param>
 /// <param name="launcher">The launcher method for the given kernel.</param>
 protected Kernel(
     Accelerator accelerator,
     CompiledKernel compiledKernel,
     MethodInfo launcher)
     : base(accelerator)
 {
     Debug.Assert(compiledKernel != null, "Invalid compiled kernel");
     Specialization = compiledKernel.Specialization;
     Launcher       = launcher;
     NumParameters  = compiledKernel.EntryPoint.Parameters.NumParameters;
 }
Exemplo n.º 15
0
            /// <summary cref="IKernelLoader.LoadKernel(Accelerator, CompiledKernel)"/>
            public Kernel LoadKernel(Accelerator accelerator, CompiledKernel compiledKernel)
            {
                var result = accelerator.LoadAutoGroupedKernel(
                    compiledKernel,
                    out int groupSize,
                    out int minGridSize);

                GroupSize   = groupSize;
                MinGridSize = minGridSize;
                return(result);
            }
Exemplo n.º 16
0
            /// <summary>
            /// Loads an explicitly grouped kernel.
            /// </summary>
            public Kernel LoadKernel(
                Accelerator accelerator,
                CompiledKernel compiledKernel,
                out KernelInfo kernelInfo)
            {
                var kernel = accelerator.LoadKernel(compiledKernel);

                kernelInfo = KernelInfo.CreateFrom(
                    compiledKernel.Info,
                    null,
                    null);
                return(kernel);
            }
Exemplo n.º 17
0
 /// <summary cref="Accelerator.LoadKernelInternal(CompiledKernel)"/>
 protected sealed override Kernel LoadKernelInternal(CompiledKernel kernel)
 {
     if (kernel == null)
     {
         throw new ArgumentNullException(nameof(kernel));
     }
     if (!(kernel is TCompiledKernel compiledKernel))
     {
         throw new NotSupportedException(RuntimeErrorMessages.NotSupportedKernel);
     }
     return(CreateKernel(
                compiledKernel,
                GenerateKernelLauncherMethod(compiledKernel, 0)));
 }
Exemplo n.º 18
0
 /// <summary>
 /// Loads an implicitly grouped kernel.
 /// </summary>
 protected override Kernel LoadImplicitlyGroupedKernelInternal(
     CompiledKernel kernel,
     int customGroupSize,
     out KernelInfo kernelInfo)
 {
     if (customGroupSize < 0)
     {
         throw new ArgumentOutOfRangeException(nameof(customGroupSize));
     }
     kernelInfo = KernelInfo.CreateFrom(
         kernel.Info,
         customGroupSize,
         null);
     return(LoadKernel(kernel, customGroupSize));
 }
Exemplo n.º 19
0
        /// <summary>
        /// Loads the given kernel.
        /// </summary>
        /// <param name="kernel">The kernel to load.</param>
        /// <param name="customGroupSize">The custom group size.</param>
        /// <returns>The loaded kernel</returns>
        private Kernel LoadKernel(CompiledKernel kernel, int customGroupSize)
        {
            if (kernel == null)
            {
                throw new ArgumentNullException(nameof(kernel));
            }
            if (!(kernel is ILCompiledKernel ilKernel))
            {
                throw new NotSupportedException(RuntimeErrorMessages.NotSupportedKernel);
            }

            var launcherMethod = GenerateKernelLauncherMethod(ilKernel, customGroupSize);

            return(new CPUKernel(
                       this,
                       kernel,
                       launcherMethod,
                       ilKernel.ExecutionHandler));
        }
Exemplo n.º 20
0
        /// <summary>
        /// Loads the given kernel.
        /// </summary>
        /// <param name="kernel">The kernel to load.</param>
        /// <param name="customGroupSize">The custom group size.</param>
        /// <returns>The loaded kernel</returns>
        private Kernel LoadKernel(CompiledKernel kernel, int customGroupSize)
        {
            if (kernel == null)
            {
                throw new ArgumentNullException(nameof(kernel));
            }
            var launcherMethod = GenerateKernelLauncherMethod(
                kernel,
                out Type taskType,
                out FieldInfo[] taskArgumentMapping,
                customGroupSize);
            var executionMethod = GenerateKernelExecutionMethod(
                kernel,
                taskType,
                taskArgumentMapping);

            return(new CPUKernel(
                       this,
                       kernel,
                       launcherMethod,
                       (CPUKernelExecutionHandler)executionMethod.CreateDelegate(
                           typeof(CPUKernelExecutionHandler))));
        }
Exemplo n.º 21
0
 /// <summary>
 /// Loads the given kernel.
 /// Note that implictly-grouped kernels will be launched with a group size
 /// of the current warp size of the accelerator.
 /// </summary>
 /// <param name="kernel">The kernel to load.</param>
 /// <returns>The loaded kernel.</returns>
 /// <remarks>
 /// Note that the returned kernel will not be managed by the kernel cache.
 /// </remarks>
 protected abstract Kernel LoadKernelInternal(CompiledKernel kernel);
Exemplo n.º 22
0
 /// <summary>
 /// Loads the given kernel.
 /// Note that implictly-grouped kernels will be launched with a group size
 /// of the current warp size of the accelerator.
 /// </summary>
 /// <param name="kernel">The kernel to load.</param>
 /// <returns>The loaded kernel.</returns>
 /// <remarks>
 /// Note that the returned kernel will not be managed by the kernel cache.
 /// </remarks>
 public Kernel LoadKernel(CompiledKernel kernel)
 {
     Bind(); return(LoadKernelInternal(kernel));
 }
Exemplo n.º 23
0
 /// <summary cref="IKernelLoader.LoadKernel(Accelerator, CompiledKernel)"/>
 public Kernel LoadKernel(Accelerator accelerator, CompiledKernel compiledKernel)
 {
     return(accelerator.LoadImplicitlyGroupedKernel(compiledKernel, GroupSize));
 }
Exemplo n.º 24
0
 /// <summary>
 /// Loads the given implicitly-grouped kernel while using an automatically
 /// computed grouping configuration.
 /// </summary>
 /// <param name="kernel">The kernel to load.</param>
 /// <param name="groupSize">The estimated group size to gain maximum occupancy on this device.</param>
 /// <param name="minGridSize">The minimum grid size to gain maximum occupancy on this device.</param>
 /// <returns>The loaded kernel.</returns>
 /// <remarks>
 /// Note that the returned kernel will not be managed by the kernel cache.
 /// </remarks>
 protected abstract Kernel LoadAutoGroupedKernelInternal(
     CompiledKernel kernel,
     out int groupSize,
     out int minGridSize);
Exemplo n.º 25
0
 /// <summary cref="IKernelLoader.LoadKernel(Accelerator, CompiledKernel)"/>
 public Kernel LoadKernel(Accelerator accelerator, CompiledKernel compiledKernel)
 {
     return(accelerator.LoadKernel(compiledKernel));
 }
Exemplo n.º 26
0
 /// <summary>
 /// Loads the given implicitly-grouped kernel while using an automatically
 /// computed grouping configuration.
 /// </summary>
 /// <param name="kernel">The kernel to load.</param>
 /// <returns>The loaded kernel.</returns>
 /// <remarks>
 /// Note that the returned kernel will not be managed by the kernel cache.
 /// </remarks>
 public Kernel LoadAutoGroupedKernel(CompiledKernel kernel) =>
 LoadAutoGroupedKernel(kernel, out var _, out var _);
Exemplo n.º 27
0
 /// <summary>
 /// Loads the given implicitly-grouped kernel while using an automatically
 /// computed grouping configuration.
 /// </summary>
 /// <param name="kernel">The kernel to load.</param>
 /// <returns>The loaded kernel.</returns>
 /// <remarks>
 /// Note that the returned kernel will not be managed by the kernel cache.
 /// </remarks>
 public Kernel LoadAutoGroupedKernel(CompiledKernel kernel)
 {
     return(LoadAutoGroupedKernel(kernel, out int groupSize, out int minGridSize));
 }
Exemplo n.º 28
0
 /// <summary>
 /// Loads the given implicitly-grouped kernel.
 /// </summary>
 /// <param name="kernel">The kernel to load.</param>
 /// <param name="customGroupSize">The custom group size to use.</param>
 /// <returns>The loaded kernel.</returns>
 /// <remarks>
 /// Note that implictly-grouped kernel will be launched with the given
 /// group size.
 /// </remarks>
 /// <remarks>
 /// Note that the returned kernel will not be managed by the kernel cache.
 /// </remarks>
 protected abstract Kernel LoadImplicitlyGroupedKernelInternal(
     CompiledKernel kernel,
     int customGroupSize);
Exemplo n.º 29
0
 /// <summary cref="Accelerator.LoadKernelInternal(CompiledKernel)"/>
 protected override Kernel LoadKernelInternal(CompiledKernel kernel) =>
 LoadKernel(kernel, 0);
Exemplo n.º 30
0
 /// <summary cref="Accelerator.LoadKernelInternal(CompiledKernel)"/>
 protected override Kernel LoadKernelInternal(CompiledKernel kernel)
 {
     return(LoadKernel(kernel, 0));
 }