Backends/SiaNet.Backend.TensorSharp/Cuda/KernelOps/ReductionOp.cs
/// <summary>
/// Invokes the specified reduce kernels.
/// </summary>
/// <param name="reduceKernels">The reduce kernels.</param>
/// <param name="kernelName">Name of the kernel.</param>
/// <param name="init">The initialize.</param>
/// <param name="initType">Type of the initialize.</param>
/// <param name="result">The result.</param>
/// <param name="src">The source.</param>
/// <param name="dim">The dim.</param>
/// <param name="extraArg">The extra argument.</param>
/// <returns>Tensor.</returns>
Invokes the specified reduce kernels.
Invokes the specified reduce kernels .
public static NDArray Invoke(CudaReduceKernels reduceKernels, string kernelName, float init, ReduceInitType initType, NDArray result, NDArray src, int dim, object extraArg = null)
        {
            if (src.DimensionCount == 0)
                return result;
            var context = CudaHelpers.TSContextForTensor(src);
            var cudaContext = context.CudaContextForTensor(src);
            var requiredOutputSize = (long[])src.Shape.Clone();
            requiredOutputSize[dim] = 1;
            var writeTarget = TensorResultBuilder.GetWriteTarget(result, src, false, requiredOutputSize);
            ThrowIfAnyTensorInvalid(writeTarget, src);
            var inElements = src.ElementCount();
            var reductionSize = src.Shape[dim];
            var reductionStride = src.Strides[dim];
            var outElements = inElements / reductionSize;
            var contigReduction = reductionStride == 1;
            var newSizes = (long[])src.Shape.Clone();
            newSizes[dim] = 1;
            var srcSlim = new NDArray(newSizes, src.Strides, src.Storage, src.StorageOffset);
            var config = new ApplySpecialization(writeTarget, srcSlim);
            object totalSlices = config.Use32BitIndices ? (uint)outElements : (ulong)outElements;
            object reductionSizeTyped = config.Use32BitIndices ? (uint)reductionSize : (ulong)reductionSize;
            object reductionStrideTyped = config.Use32BitIndices ? (uint)reductionStride : (ulong)reductionStride;
            object initValueTyped = ReduceInitConverter.GetInitValue(init, initType, src.ElementType);
            var ptx = reduceKernels.GetPtx(context.Compiler);
            if (contigReduction)
            {
                var block = GetContigReduceBlock(cudaContext, outElements, reductionSize);
                var grid = GetContigReduceGrid(outElements);
                uint smemSize = (uint)src.ElementType.Size() * block.x;
                var fullName = "contig_" + PermutationGenerator.GetMangledName(kernelName, config);
                if(extraArg == null)
                    InvokeReduce(context, cudaContext, ptx, fullName, grid, block, smemSize, config, writeTarget, srcSlim, reductionSizeTyped, totalSlices, initValueTyped);
                else
                    InvokeReduce(context, cudaContext, ptx, fullName, grid, block, smemSize, config, writeTarget, srcSlim, reductionSizeTyped, totalSlices, initValueTyped, extraArg);
            }
            else
            {
                var deviceProps = context.DeviceInfoForContext(cudaContext);
                var block = GetNonContigReduceBlock(deviceProps);
                var grid = GetNoncontigReduceGrid(deviceProps, outElements);
                uint smemSize = 0;
                var fullName = "noncontig_" + PermutationGenerator.GetMangledName(kernelName, config);
                if(extraArg == null)
                    InvokeReduce(context, cudaContext, ptx, fullName, grid, block, smemSize, config, writeTarget, srcSlim, reductionStrideTyped, reductionSizeTyped, totalSlices, initValueTyped);
                else
                    InvokeReduce(context, cudaContext, ptx, fullName, grid, block, smemSize, config, writeTarget, srcSlim, reductionStrideTyped, reductionSizeTyped, totalSlices, initValueTyped, extraArg);
            }
            return writeTarget;
        }
public static NDArray Invoke ( CudaReduceKernels reduceKernels , string kernelName , float init , ReduceInitType initType , NDArray result , NDArray src , int dim , object extraArg = null ) { if ( src . DimensionCount == 0 ) return result ; var context = CudaHelpers . TSContextForTensor ( src ) ; var cudaContext = context . CudaContextForTensor ( src ) ; var requiredOutputSize = ( long [ ] ) src . Shape . Clone ( ) ; requiredOutputSize [ dim ] = 1 ; var writeTarget = TensorResultBuilder . GetWriteTarget ( result , src , false , requiredOutputSize ) ; ThrowIfAnyTensorInvalid ( writeTarget , src ) ; var inElements = src . ElementCount ( ) ; var reductionSize = src . Shape [ dim ] ; var reductionStride = src . Strides [ dim ] ; var outElements = inElements / reductionSize ; var contigReduction = reductionStride == 1 ; var newSizes = ( long [ ] ) src . Shape . Clone ( ) ; newSizes [ dim ] = 1 ; var srcSlim = new NDArray ( newSizes , src . Strides , src . Storage , src . StorageOffset ) ; var config = new ApplySpecialization ( writeTarget , srcSlim ) ; object totalSlices = config . Use32BitIndices ? ( uint ) outElements : ( ulong ) outElements ; object reductionSizeTyped = config . Use32BitIndices ? ( uint ) reductionSize : ( ulong ) reductionSize ; object reductionStrideTyped = config . Use32BitIndices ? ( uint ) reductionStride : ( ulong ) reductionStride ; object initValueTyped = ReduceInitConverter . GetInitValue ( init , initType , src . ElementType ) ; var ptx = reduceKernels . GetPtx ( context . Compiler ) ; if ( contigReduction ) { var block = GetContigReduceBlock ( cudaContext , outElements , reductionSize ) ; var grid = GetContigReduceGrid ( outElements ) ; uint smemSize = ( uint ) src . ElementType . Size ( ) * block . x ; var fullName = " " + PermutationGenerator . GetMangledName ( kernelName , config ) ; if ( extraArg == null ) InvokeReduce ( context , cudaContext , ptx , fullName , grid , block , smemSize , config , writeTarget , srcSlim , reductionSizeTyped , totalSlices , initValueTyped ) ; else InvokeReduce ( context , cudaContext , ptx , fullName , grid , block , smemSize , config , writeTarget , srcSlim , reductionSizeTyped , totalSlices , initValueTyped , extraArg ) ; } else { var deviceProps = context . DeviceInfoForContext ( cudaContext ) ; var block = GetNonContigReduceBlock ( deviceProps ) ; var grid = GetNoncontigReduceGrid ( deviceProps , outElements ) ; uint smemSize = 0 ; var fullName = " " + PermutationGenerator . GetMangledName ( kernelName , config ) ; if ( extraArg == null ) InvokeReduce ( context , cudaContext , ptx , fullName , grid , block , smemSize , config , writeTarget , srcSlim , reductionStrideTyped , reductionSizeTyped , totalSlices , initValueTyped ) ; else InvokeReduce ( context , cudaContext , ptx , fullName , grid , block , smemSize , config , writeTarget , srcSlim , reductionStrideTyped , reductionSizeTyped , totalSlices , initValueTyped , extraArg ) ; } return writeTarget ; }
Invokes the specified reduce kernels.
Invokes the specified reduce kernels .
// We must make sure that when the tensor is passed to the kernel, src.Sizes[dim] is set to 1 // This includes for the purposes of determining which tensor specializations to use (changing // the dimension size to 1 may make the tensor non-contiguous
None
reduceKernels
CudaReduceKernels
The reduce kernels.
The reduce kernels .
kernelName
string
Name of the kernel.
Name of the kernel .
init
float
None
initType
ReduceInitType
Type of the initialize.
Type of the initialize .
result
NDArray
None
src
NDArray
None
dim
int
None
extraArg
object
The extra argument.
The extra argument .