Exemplo n.º 1
0
        public static int[] FindAllPrimesUnder(int n)
        {
            if (n <= 1024)
            {
                throw new Exception("Input too small to compuate in parallel.");
            }
            ComputeNode rootNode    = new ComputeNode(2, n);
            var         listOftasks = new Queue <Task <SortedSet <int> > >();

            rootNode.AddAllLeafTasks(listOftasks);

            Console.WriteLine($"List of Tasks count: {listOftasks.Count}");

            var taskRunner = new BagBasedTaskRunner <SortedSet <int> >(listOftasks);

            taskRunner.RunParallel();

            List <int> primes = new List <int>();

            foreach (SortedSet <int> batch in taskRunner.results_)
            {
                foreach (int prime in batch)
                {
                    primes.Add(prime);
                }
            }
            primes.Sort();

            return(primes.ToArray());
        }
Exemplo n.º 2
0
 public void AddAllLeafTasks(Queue <Task <SortedSet <int> > > taskBucket)
 {
     if (_left is null) // leaf node, return tasks
     {
         Task <SortedSet <int> > baseTask = new Task <SortedSet <int> >(
             () => {
             SortedSet <int> res = new SortedSet <int>();
             for (int I = _start; I < _end; I++)
             {
                 if (BruteForcePrimeTest(I))
                 {
                     res.Add(I);
                 }
             }
             return(res);
         }
             );
         taskBucket.Enqueue(baseTask);
     }
     else
     {
         _left.AddAllLeafTasks(taskBucket);
         _right.AddAllLeafTasks(taskBucket);
     }
 }