Delegates and Parallel.For
-
for (number=0;primes<10001;number++)
That is a valid for loop. However, I am trying to parallelize this, and Parallel.For will only accept one delegate. Therefore, if I wanted
for (number=0;number<10001;number++)
I could do
Parallel.For(0,10001, delegate(int number))
Parallel.For has no overload which accepts two delegates. What is the Parallel.For equivalent of the first snippet? Thanks, Fahad Sadah
-
for (number=0;primes<10001;number++)
That is a valid for loop. However, I am trying to parallelize this, and Parallel.For will only accept one delegate. Therefore, if I wanted
for (number=0;number<10001;number++)
I could do
Parallel.For(0,10001, delegate(int number))
Parallel.For has no overload which accepts two delegates. What is the Parallel.For equivalent of the first snippet? Thanks, Fahad Sadah
Fahad Sadah wrote:
for (number=0;primes<10001;number++)
If primes doesn't change in the body of the loop, why doesn't
Fahad Sadah wrote:
Parallel.For(0,10001, delegate(int number))
work?
Regards Senthil [MVP - Visual C#] _____________________________ My Home Page |My Blog | My Articles | My Flickr | WinMacro
-
Fahad Sadah wrote:
for (number=0;primes<10001;number++)
If primes doesn't change in the body of the loop, why doesn't
Fahad Sadah wrote:
Parallel.For(0,10001, delegate(int number))
work?
Regards Senthil [MVP - Visual C#] _____________________________ My Home Page |My Blog | My Articles | My Flickr | WinMacro
-
Well, if it does, the code isn't really parallelizable. If each iteration of the for loop overwrites the variable in a way that is dependent on previous iterations, it doesn't really make sense to parellilize it. For example, if it really is calculation of primes, then numbers = 10000 could run before numbers = 2.
Regards Senthil [MVP - Visual C#] _____________________________ My Home Page |My Blog | My Articles | My Flickr | WinMacro
-
Well, if it does, the code isn't really parallelizable. If each iteration of the for loop overwrites the variable in a way that is dependent on previous iterations, it doesn't really make sense to parellilize it. For example, if it really is calculation of primes, then numbers = 10000 could run before numbers = 2.
Regards Senthil [MVP - Visual C#] _____________________________ My Home Page |My Blog | My Articles | My Flickr | WinMacro
Impossible? Thanks.
-
for (number=0;primes<10001;number++)
That is a valid for loop. However, I am trying to parallelize this, and Parallel.For will only accept one delegate. Therefore, if I wanted
for (number=0;number<10001;number++)
I could do
Parallel.For(0,10001, delegate(int number))
Parallel.For has no overload which accepts two delegates. What is the Parallel.For equivalent of the first snippet? Thanks, Fahad Sadah
Good question! The problem you have is that you don't know the bounds of your input - you don't know how big
number
will have to get to generate the required number ofprimes
. However, you can still parellelise chunks of work - something like this:const int N = ( int ) 1e6;
static readonly int STEP = Environment.ProcessorCount * ( int ) 1e6;...
var primes = new List<int>( N );
for ( int start = 2 ; primes.Count < N ; start += STEP )
primes.AddRange(
Enumerable.Range( start, STEP )
.AsParallel().AsOrdered()
.Where( i => IsPrime( i ) ) );primes = primes.Take( N ).ToList();
You'll have to tune the values of
N
andSTEP
for your specific case. Nick---------------------------------- Be excellent to each other :)