Esempio n. 1
0
 public int GetAsyncCallbackData(int value, int asyncCallbacksToMake)
 {
     int result = value;
     var tasks = new Task<int>[asyncCallbacksToMake];
     for (int i = 0; i < asyncCallbacksToMake; i++)
     {
         tasks[i] = OperationContext.Current.GetCallbackChannel<IDuplexCallback>().EchoGetAsyncCallbackData(result + i);
     }
     Task.WhenAll(tasks).Wait();
     result = tasks.Sum((t) => t.Result);
     return result;
 }
Esempio n. 2
0
        public static float Sum(float[] values, Func<float, float> selector, int threadsCount)
        {
            if (threadsCount < 2 || values.Length <= threadsCount)
                return selector == null ? values.Sum() : values.Sum(selector);

            Task<float>[] tasks = new Task<float>[threadsCount];
            int chunk = (int)Math.Ceiling((double)values.Length/threadsCount);
            for (int i = 0; i < threadsCount; i++)
            {
                int start = i*chunk;
                int end = Math.Min(start + chunk, values.Length);
                tasks[i] = Task.Run(() => Sum(values, selector, start, end));
            }

            return tasks.Sum(t => t.Result);
        }
        public static PerformanceResult RunConcurrentPerformanceTest(int numIterations, int degreeParallelism,
            Action operation)
        {
            int i;
            var taskList = new Task<PerformanceResult>[degreeParallelism];
            long startTime = HiResTimer.Ticks;
            int subIterations = numIterations/degreeParallelism;

            for (i = 0; i < degreeParallelism; i++)
            {
                var t = new Task<PerformanceResult>(() => RunPerformanceTest(subIterations, operation, true));
                taskList[i] = t;
            }

            for (i = 0; i < degreeParallelism; i++) taskList[i].Start();

            Task.WaitAll(taskList);
            long stopTime = HiResTimer.Ticks;

            var rawData = new List<double>();

            for (i = 0; i < degreeParallelism; i++)
            {
                rawData.AddRange(taskList[i].Result.DescriptiveResult.RawData);
            }

            var desc = new DescriptiveAnalysis(rawData);
            desc.Analyze(false);
            desc.AnalyzeHistogram(cHistogramBuckets);

            var res = new PerformanceResult
            {
                IsValid = true,
                Iterations = taskList.Sum(p => p.Result.Iterations),
                DegreeOfParallelism = degreeParallelism,
                TotalMilliseconds = ConvertToMs(startTime, stopTime),
                TotalSeconds = ConvertToSeconds(startTime, stopTime),
                TotalTicks = stopTime - startTime,
                DescriptiveResult = desc.Result
            };

            for (i = 0; i < degreeParallelism; i++) taskList[i].Dispose();

            return res;
        }
	    /// <summary>
	    /// Executes each <see cref="Searchable"/>'s docFreq() in its own thread and 
	    /// waits for each search to complete and merge the results back together.
	    /// </summary>
		public override int DocFreq(Term term)
	    {
	        Task<int>[] tasks = new Task<int>[searchables.Length];
            for (int i = 0; i < searchables.Length; i++)
            {
                Searchable searchable = searchables[i];
                tasks[i] = Task.Factory.StartNew(() => searchable.DocFreq(term));
            }

	        Task.WaitAll(tasks);
	        return tasks.Sum(task => task.Result);
	    }
        private static double CalculateIntegral(Calculator calc, Func<Calculator, double> distanceMethod, double leftBound, double rightBound)
        {
            // Protip: the Wolfram Alpha way of verifying the results from this is:
            // "integral of (<<top>> - <<bottom>>)dx between <<start>> and <<stop>>"

            int numCores = Environment.ProcessorCount;

            int iterCount = (int)Math.Ceiling((rightBound - leftBound) / Increment);
            int itersPerCore = (int)Math.Floor((double)iterCount / numCores);

            // The number of iterations that the last thread should do (to accomodate for rounding errors)
            int lastCoreIters = itersPerCore + (iterCount % numCores);

            // Assemble the worker threads
            Task<double>[] tasks = new Task<double>[numCores];
            int core = 0;

            bool lastTask = false;
            while (!lastTask)
            {
                lastTask = core + 1 >= numCores;

                double start = leftBound + ((core * itersPerCore) * Increment);
                int numIters = (lastTask ? lastCoreIters : itersPerCore);

                tasks[core++] = new Task<double>(() => CalculateAreaChunk(new Calculator(calc), distanceMethod, start, rightBound, numIters));
            }

            foreach (Task<double> t in tasks)
            {
                t.Start();
            }

            Task.WaitAll(tasks);

            double totalArea = tasks.Sum(t => t.Result);

            // Find the number of decimal places the increment goes to
            int decPlace = 0;
            double slicedIncrement = Increment;

            // Move the decimal point of the increment to the right on each iteration. We keep
            // moving the decimal over until the first significant digit of the increment is found
            while ((int)(slicedIncrement *= 10) == 0)
            {
                decPlace++;
            }

            // Assume the area is only accurate to the number of decimal places that the increment has
            return Math.Round(totalArea, decPlace + 1);
        }