/// <summary>
        /// Computes the required number of temp-storage elements for a radix sort
        /// operation and the given data length.
        /// </summary>
        /// <typeparam name="T">The underlying type of the sort operation.</typeparam>
        /// <typeparam name="TRadixSortOperation">
        /// The type of the radix-sort operation.
        /// </typeparam>
        /// <param name="accelerator">The accelerator.</param>
        /// <param name="dataLength">The number of data elements to sort.</param>
        /// <returns>
        /// The required number of temp-storage elements in 32 bit ints.
        /// </returns>
        public static Index1 ComputeRadixSortTempStorageSize <T, TRadixSortOperation>(
            this Accelerator accelerator,
            Index1 dataLength)
            where T : unmanaged
            where TRadixSortOperation : struct, IRadixSortOperation <T>
        {
            LongIndex1 tempScanMemoryLong =
                accelerator.ComputeScanTempStorageSize <T>(dataLength);

            IndexTypeExtensions.AssertIntIndexRange(tempScanMemoryLong);
            Index1 tempScanMemory = tempScanMemoryLong.ToIntIndex();

            int numGroups;

            if (accelerator.AcceleratorType == AcceleratorType.CPU)
            {
                numGroups = accelerator.MaxNumThreads;
            }
            else
            {
                var(gridDim, _) = accelerator.ComputeGridStrideLoopExtent(
                    dataLength,
                    out int numIterationsPerGroup);
                numGroups = gridDim * numIterationsPerGroup;
            }

            long numIntTElementsLong = Interop.ComputeRelativeSizeOf <int, T>(dataLength);

            IndexTypeExtensions.AssertIntIndexRange(numIntTElementsLong);
            int numIntTElements = (int)numIntTElementsLong;

            const int unrollFactor = 4;

            return(numGroups * unrollFactor * 2 + numIntTElements + tempScanMemory);
        }
Ejemplo n.º 2
0
        private ArrayView <int> AllocateTempScanView <T>(ArrayView <T> input)
            where T : unmanaged
        {
            var tempSize = Accelerator.ComputeScanTempStorageSize <T>(input.Length);

            IndexTypeExtensions.AssertIntIndexRange(tempSize);
            return(bufferCache.Allocate <int>(tempSize.ToIntIndex()));
        }
Ejemplo n.º 3
0
 /// <summary>
 /// Reconstructs a 4D index from a linear index.
 /// </summary>
 /// <param name="linearIndex">The lienar index.</param>
 /// <returns>The reconstructed 4D index.</returns>
 public MyIndex4 ReconstructIndex(long linearIndex)
 {
     IndexTypeExtensions.AssertIntIndexRange(linearIndex);
     return(ReconstructIndex((int)linearIndex, this));
 }