/// <summary>
        /// A very customisable aggregation routine
        /// </summary>
        /// <param name="deals"></param>
        /// <param name="vectorLength"></param>
        /// <returns></returns>
        private NArray AggregateValuations(IList <Deal> deals, int vectorLength)
        {
            object lockObject       = new object();
            var    rangePartitioner = Partitioner.Create(0, deals.Count);

            var sum = new NArray(StorageLocation.Host, vectorLength);

            var options = new ParallelOptions();// { MaxDegreeOfParallelism = 2 };

            Parallel.ForEach(
                // input intervals
                rangePartitioner,

                options,

                // local initial partial result
                () => new NArray(StorageLocation.Host, vectorLength),

                // loop body for each interval
                (range, loopState, initialValue) =>
            {
                var partialSum = initialValue;
                for (int i = range.Item1; i < range.Item2; i++)
                {
                    partialSum.Add(
                        NArray.Evaluate(() =>
                    {
                        return(deals[i].Price(0));
                    }));
                }
                return(partialSum);
            },

                // final step of each local context
                (localPartialSum) =>
            {
                // using a lock to enforce serial access to shared result
                lock (lockObject)
                {
                    sum.Add(localPartialSum);
                }
            });

            return(sum);
        }
Exemplo n.º 2
0
        public static int Simple_search_optimized(int n)
        {
            int          count = 0;
            NArray <int> arr   = new NArray <int>();

            arr.SingleArray();

            for (int i = 2; i <= n; i++)
            {
                if (prime_optimized(i, arr))
                {
                    arr.Add(i);
                    count++;
                }
            }

            return(count);
        }
        /// <summary>
        /// A very customisable aggregation routine
        /// </summary>
        /// <param name="deals"></param>
        /// <param name="vectorLength"></param>
        /// <returns></returns>
        private NArray AggregateValuations(IList<Deal> deals, int vectorLength)
        {
            object lockObject = new object();
            var rangePartitioner = Partitioner.Create(0, deals.Count);

            var sum = new NArray(StorageLocation.Host, vectorLength);

            var options = new ParallelOptions();// { MaxDegreeOfParallelism = 2 };

            Parallel.ForEach(
                // input intervals
              rangePartitioner,

              options,

              // local initial partial result
              () => new NArray(StorageLocation.Host, vectorLength),

              // loop body for each interval
              (range, loopState, initialValue) =>
              {
                  var partialSum = initialValue;
                  for (int i = range.Item1; i < range.Item2; i++)
                  {
                      partialSum.Add(
                          NArray.Evaluate(() =>
                          {
                              return deals[i].Price(0);
                          }));
                  }
                  return partialSum;
              },

              // final step of each local context
              (localPartialSum) =>
              {
                  // using a lock to enforce serial access to shared result
                  lock (lockObject)
                  {
                      sum.Add(localPartialSum);
                  }
              });

            return sum;
        }