Backends/SiaNet.Backend.TensorSharp/Cuda/KernelOps/ReduceAllOp.cs
/// <summary>
/// Invokes the specified reduce all kernels.
/// </summary>
/// <param name="reduceAllKernels">The reduce all kernels.</param>
/// <param name="init">The initialize.</param>
/// <param name="initType">Type of the initialize.</param>
/// <param name="kernelName">Name of the kernel.</param>
/// <param name="result">The result.</param>
/// <param name="src">The source.</param>
/// <param name="extraArg">The extra argument.</param>
/// <returns>Tensor.</returns>
/// <exception cref="InvalidOperationException">Tensors with dimension count > " + TSCudaContext.MaxDims + " are not supported</exception>
Invokes the specified reduce all kernels.
Invokes the specified reduce all kernels .
public static NDArray Invoke(CudaReduceAllKernels reduceAllKernels, float init, ReduceInitType initType, string kernelName, NDArray result, NDArray src, object extraArg = null)
        {
            var deviceId = CudaHelpers.GetDeviceId(src);
            var context = CudaHelpers.TSContextForTensor(src);
            var cudaContext = context.CudaContextForDevice(deviceId);
            if (src.DimensionCount > TSCudaContext.MaxDims)
                throw new InvalidOperationException("Tensors with dimension count > " + TSCudaContext.MaxDims + " are not supported");
            var writeTarget = TensorResultBuilder.GetWriteTarget(result, src, false, 1);
            if (src.DimensionCount == 0)
            {
                return result;
            }
            var totalElements = src.ElementCount();
            var config = new ApplySpecialization(src);
            object totalElementsTyped = config.Use32BitIndices ? (uint)totalElements : (ulong)totalElements;
            object initValueTyped = ReduceInitConverter.GetInitValue(init, initType, src.ElementType);
            dim3 grid;
            dim3 block;
            var ptx = reduceAllKernels.GetPtx(context.Compiler);
            var fullKernelName = PermutationGenerator.GetMangledName(kernelName, config);
            var outputDevicePtr = CudaHelpers.GetBufferStart(writeTarget);
            if (isTwoPassReductionSize(totalElements))
            {
                getPass1ReduceBlockGrid(context, deviceId, totalElements, out grid, out block);
                uint smemSize = block.x * sizeof(float);
                var scratchSpace = context.ScratchSpaceForDevice(deviceId).buffer;
                if(extraArg == null)
                    InvokeReduceAll(context, cudaContext, ptx, "twoPassA_" + fullKernelName, grid, block, smemSize, config, src, totalElementsTyped, initValueTyped, scratchSpace);
                else
                    InvokeReduceAll(context, cudaContext, ptx, "twoPassA_" + fullKernelName, grid, block, smemSize, config, src, totalElementsTyped, initValueTyped, scratchSpace, extraArg);
                uint numPass1Blocks = grid.x;
                getPass2ReduceBlockGrid(context, deviceId, totalElements, out grid, out block);
                smemSize = block.x * sizeof(float);
                InvokeReduceAllPass2(context, cudaContext, ptx, "twoPassB_" + fullKernelName, grid, block, smemSize, config.Use32BitIndices, numPass1Blocks, initValueTyped, scratchSpace, outputDevicePtr);
            }
            else {
                getSinglePassReduceBlockGrid(totalElements, out grid, out block);
                uint smemSize = block.x * sizeof(float);
                if(extraArg == null)
                    InvokeReduceAll(context, cudaContext, ptx, "onePass_" + fullKernelName, grid, block, smemSize, config, src, totalElementsTyped, initValueTyped, outputDevicePtr);
                else
                    InvokeReduceAll(context, cudaContext, ptx, "onePass_" + fullKernelName, grid, block, smemSize, config, src, totalElementsTyped, initValueTyped, outputDevicePtr, extraArg);
            }
            return writeTarget;
        }
public static NDArray Invoke ( CudaReduceAllKernels reduceAllKernels , float init , ReduceInitType initType , string kernelName , NDArray result , NDArray src , object extraArg = null ) { var deviceId = CudaHelpers . GetDeviceId ( src ) ; var context = CudaHelpers . TSContextForTensor ( src ) ; var cudaContext = context . CudaContextForDevice ( deviceId ) ; if ( src . DimensionCount > TSCudaContext . MaxDims ) throw new InvalidOperationException ( " " + TSCudaContext . MaxDims + " " ) ; var writeTarget = TensorResultBuilder . GetWriteTarget ( result , src , false , 1 ) ; if ( src . DimensionCount == 0 ) { return result ; } var totalElements = src . ElementCount ( ) ; var config = new ApplySpecialization ( src ) ; object totalElementsTyped = config . Use32BitIndices ? ( uint ) totalElements : ( ulong ) totalElements ; object initValueTyped = ReduceInitConverter . GetInitValue ( init , initType , src . ElementType ) ; dim3 grid ; dim3 block ; var ptx = reduceAllKernels . GetPtx ( context . Compiler ) ; var fullKernelName = PermutationGenerator . GetMangledName ( kernelName , config ) ; var outputDevicePtr = CudaHelpers . GetBufferStart ( writeTarget ) ; if ( isTwoPassReductionSize ( totalElements ) ) { getPass1ReduceBlockGrid ( context , deviceId , totalElements , out grid , out block ) ; uint smemSize = block . x * sizeof ( float ) ; var scratchSpace = context . ScratchSpaceForDevice ( deviceId ) . buffer ; if ( extraArg == null ) InvokeReduceAll ( context , cudaContext , ptx , " " + fullKernelName , grid , block , smemSize , config , src , totalElementsTyped , initValueTyped , scratchSpace ) ; else InvokeReduceAll ( context , cudaContext , ptx , " " + fullKernelName , grid , block , smemSize , config , src , totalElementsTyped , initValueTyped , scratchSpace , extraArg ) ; uint numPass1Blocks = grid . x ; getPass2ReduceBlockGrid ( context , deviceId , totalElements , out grid , out block ) ; smemSize = block . x * sizeof ( float ) ; InvokeReduceAllPass2 ( context , cudaContext , ptx , " " + fullKernelName , grid , block , smemSize , config . Use32BitIndices , numPass1Blocks , initValueTyped , scratchSpace , outputDevicePtr ) ; } else { getSinglePassReduceBlockGrid ( totalElements , out grid , out block ) ; uint smemSize = block . x * sizeof ( float ) ; if ( extraArg == null ) InvokeReduceAll ( context , cudaContext , ptx , " " + fullKernelName , grid , block , smemSize , config , src , totalElementsTyped , initValueTyped , outputDevicePtr ) ; else InvokeReduceAll ( context , cudaContext , ptx , " " + fullKernelName , grid , block , smemSize , config , src , totalElementsTyped , initValueTyped , outputDevicePtr , extraArg ) ; } return writeTarget ; }
Invokes the specified reduce all kernels.
Invokes the specified reduce all kernels .
None
Tensors with dimension count > " + TSCudaContext.MaxDims + " are not supported
Tensors with dimension count > " + TSCudaContext . MaxDims + " are not supported
reduceAllKernels
CudaReduceAllKernels
The reduce all kernels.
The reduce all kernels .
init
float
None
initType
ReduceInitType
Type of the initialize.
Type of the initialize .
kernelName
string
Name of the kernel.
Name of the kernel .
result
NDArray
None
src
NDArray
None
extraArg
object
The extra argument.
The extra argument .