Пример #1
0
        /// <summary>
        /// Demonstrates the reduce functionality.
        /// </summary>
        /// <param name="accl">The target accelerator.</param>
        static void AtomicReduce(Accelerator accl)
        {
            using (var buffer = accl.Allocate <int>(64))
            {
                using (var target = accl.Allocate <int>(1))
                {
                    accl.Sequence(buffer.View, new Int32Sequencer());

                    // This overload requires an explicit output buffer but
                    // uses an implicit temporary cache from the associated accelerator.
                    // Call a different overload to use a user-defined memory cache.
                    accl.AtomicReduce(
                        buffer.View,
                        target.View,
                        new ShuffleDownInt32(),
                        new AtomicAddInt32());

                    accl.Synchronize();

                    var data = target.GetAsArray();
                    for (int i = 0, e = data.Length; i < e; ++i)
                    {
                        Console.WriteLine($"AtomicReduced[{i}] = {data[i]}");
                    }
                }
            }
        }
 /// <summary>
 /// Performs a reduction using an atomic reduction logic.
 /// </summary>
 /// <typeparam name="T">The underlying type of the reduction.</typeparam>
 /// <typeparam name="TShuffleDown">The type of the shuffle logic.</typeparam>
 /// <typeparam name="TReduction">The type of the reduction logic.</typeparam>
 /// <param name="accelerator">The accelerator.</param>
 /// <param name="input">The input elements to reduce.</param>
 /// <param name="shuffleDown">The shuffle logic.</param>
 /// <param name="reduction">The reduction logic.</param>
 /// <remarks>Uses the internal cache to realize a temporary output buffer.</remarks>
 /// <returns>The reduced value.</returns>
 public static T AtomicReduce <T, TShuffleDown, TReduction>(
     this Accelerator accelerator,
     ArrayView <T> input,
     TShuffleDown shuffleDown,
     TReduction reduction)
     where T : struct
     where TShuffleDown : struct, IShuffleDown <T>
     where TReduction : struct, IAtomicReduction <T>
 {
     return(accelerator.AtomicReduce(
                accelerator.DefaultStream,
                input,
                shuffleDown,
                reduction));
 }
 /// <summary>
 /// Performs a reduction using an atomic reduction logic.
 /// </summary>
 /// <typeparam name="T">The underlying type of the reduction.</typeparam>
 /// <typeparam name="TShuffleDown">The type of the shuffle logic.</typeparam>
 /// <typeparam name="TReduction">The type of the reduction logic.</typeparam>
 /// <param name="accelerator">The accelerator.</param>
 /// <param name="stream">The accelerator stream.</param>
 /// <param name="input">The input elements to reduce.</param>
 /// <param name="shuffleDown">The shuffle logic.</param>
 /// <param name="reduction">The reduction logic.</param>
 /// <remarks>Uses the internal cache to realize a temporary output buffer.</remarks>
 /// <returns>The reduced value.</returns>
 public static Task <T> AtomicReduceAsync <T, TShuffleDown, TReduction>(
     this Accelerator accelerator,
     AcceleratorStream stream,
     ArrayView <T> input,
     TShuffleDown shuffleDown,
     TReduction reduction)
     where T : struct
     where TShuffleDown : struct, IShuffleDown <T>
     where TReduction : struct, IAtomicReduction <T>
 {
     return(Task.Run(() =>
     {
         return accelerator.AtomicReduce(
             stream,
             input,
             shuffleDown,
             reduction);
     }));
 }
        /// <summary>
        /// Performs a reduction using an atomic reduction logic.
        /// </summary>
        /// <typeparam name="T">The underlying type of the reduction.</typeparam>
        /// <typeparam name="TShuffleDown">The type of the shuffle logic.</typeparam>
        /// <typeparam name="TReduction">The type of the reduction logic.</typeparam>
        /// <param name="accelerator">The accelerator.</param>
        /// <param name="stream">The accelerator stream.</param>
        /// <param name="input">The input elements to reduce.</param>
        /// <param name="shuffleDown">The shuffle logic.</param>
        /// <param name="reduction">The reduction logic.</param>
        /// <remarks>Uses the internal cache to realize a temporary output buffer.</remarks>
        /// <returns>The reduced value.</returns>
        public static T AtomicReduce <T, TShuffleDown, TReduction>(
            this Accelerator accelerator,
            AcceleratorStream stream,
            ArrayView <T> input,
            TShuffleDown shuffleDown,
            TReduction reduction)
            where T : struct
            where TShuffleDown : struct, IShuffleDown <T>
            where TReduction : struct, IAtomicReduction <T>
        {
            var output = accelerator.MemoryCache.Allocate <T>(1);

            accelerator.AtomicReduce(
                stream,
                input,
                output,
                shuffleDown,
                reduction);
            stream.Synchronize();
            accelerator.MemoryCache.CopyTo(out T result, 0);
            return(result);
        }