示例#1
0
        /// <summary>
        /// Creates a new radix sort operation.
        /// </summary>
        /// <typeparam name="T">The underlying type of the sort operation.</typeparam>
        /// <typeparam name="TRadixSortMapper">The type of the radix sort mapper.</typeparam>
        /// <param name="accelerator">The accelerator.</param>
        /// <param name="kind">The radix sort kind.</param>
        /// <returns>The created radix sort handler.</returns>
        public static RadixSort <T, TRadixSortMapper> CreateRadixSort <T, TRadixSortMapper>(
            this Accelerator accelerator,
            RadixSortKind kind)
            where T : struct
            where TRadixSortMapper : struct, IRadixSortMapper <T>
        {
            var ascending   = kind == RadixSortKind.Ascending;
            var initializer = accelerator.CreateInitializer <Index>();
            var radixSort   = accelerator.LoadAutoGroupedKernel <
                Action <AcceleratorStream, Index, ArrayView <T>, ArrayView <T>, TRadixSortMapper, ArrayView <Index>, ArrayView <Index>, int, int> >(
                RadixSortImpl <T, TRadixSortMapper> .KernelMethod,
                out int groupSize, out int minGridSize);
            var minDataSize = groupSize * minGridSize;

            return((stream, input, output, counterView, mapper) =>
            {
                if (!input.IsValid)
                {
                    throw new ArgumentNullException(nameof(input));
                }
                if (!output.IsValid)
                {
                    throw new ArgumentNullException(nameof(output));
                }
                if (!counterView.IsValid)
                {
                    throw new ArgumentNullException(nameof(counterView));
                }
                var tempSize = ComputeRadixSortTempStorageSize(accelerator, input.Length);
                if (counterView.Length < tempSize)
                {
                    throw new ArgumentOutOfRangeException(nameof(counterView));
                }

                var dimension = Math.Min(minDataSize, input.Length);

                var leftCounterView = counterView.GetSubView(0, 1);
                var rightCounterView = counterView.GetSubView(1, 1);

                int leftTrue = ascending ? 0 : 1;
                int rightTrue = ascending ? 1 : 0;

                var endBitIdx = mapper.NumRadixBits;
                if ((endBitIdx & 1) != 0)
                {
                    throw new ArgumentOutOfRangeException(nameof(mapper));
                }

                for (int bitIdx = 0; bitIdx < endBitIdx; ++bitIdx)
                {
                    initializer(stream, counterView, Index.Zero);

                    // Sort to buckets
                    radixSort(stream, dimension, input, output, mapper, rightCounterView, leftCounterView, bitIdx, leftTrue);
                    radixSort(stream, dimension, input, output, mapper, leftCounterView, rightCounterView, bitIdx, rightTrue);

                    Utilities.Swap(ref input, ref output);
                }
            });
        }
示例#2
0
        /// <summary>
        /// Creates a new radix sort operation.
        /// </summary>
        /// <typeparam name="T">The underlying type of the sort operation.</typeparam>
        /// <typeparam name="TRadixSortMapper">The type of the radix sort mapper.</typeparam>
        /// <param name="accelerator">The accelerator.</param>
        /// <param name="kind">The radix sort kind.</param>
        /// <param name="mapper">The radix sort mapper.</param>
        /// <returns>The created radix sort handler.</returns>
        public static RadixSort <T> CreateRadixSort <T, TRadixSortMapper>(
            this Accelerator accelerator,
            RadixSortKind kind,
            TRadixSortMapper mapper)
            where T : struct
            where TRadixSortMapper : struct, IRadixSortMapper <T>
        {
            var radixSort = CreateRadixSort <T, TRadixSortMapper>(accelerator, kind);

            return((stream, input, output, temp) =>
                   radixSort(stream, input, output, temp, mapper));
        }
示例#3
0
        /// <summary>
        /// Creates a new radix sort operation.
        /// </summary>
        /// <typeparam name="T">The underlying type of the sort operation.</typeparam>
        /// <typeparam name="TRadixSortMapper">The type of the radix sort mapper.</typeparam>
        /// <param name="kind">The radix sort kind.</param>
        /// <param name="mapper">The radix sort mapper.</param>
        /// <returns>The created radix sort handler.</returns>
        public BufferedRadixSort <T> CreateRadixSort <T, TRadixSortMapper>(
            RadixSortKind kind,
            TRadixSortMapper mapper)
            where T : struct
            where TRadixSortMapper : struct, IRadixSortMapper <T>
        {
            var radixSort = Accelerator.CreateRadixSort <T, TRadixSortMapper>(kind, mapper);

            return((stream, input, output) =>
            {
                var tempView = AllocateTempRadixSortView(input);
                radixSort(stream, input, output, tempView);
            });
        }