Parallizing Loops with C#

G

Guest

Hello,

I´m having several loops with independent operations.
Now I want to parallelize these loops for my two test-machines
(a 4xXeon Server and a DualCore 2 Notebook).

Does anybody know a good tutorial about parallizing with C#?

Which solutions are possible?

At the moment I only can imagine to do it with several threads.
Are there other ways?

Is it for example possible to run the OpenMP library
in an unmanaged code area of C#?


Regards,

Martin
 
B

Bruce Wood

Martin said:
Hello,

I´m having several loops with independent operations.
Now I want to parallelize these loops for my two test-machines
(a 4xXeon Server and a DualCore 2 Notebook).

Does anybody know a good tutorial about parallizing with C#?

Which solutions are possible?

At the moment I only can imagine to do it with several threads.
Are there other ways?

Is it for example possible to run the OpenMP library
in an unmanaged code area of C#?

Multithreading would be the way to go.
 
W

William Stacey [C# MVP]

Not sure of exact needs, but you could try my Port Concurrency Runtime
library at www.codeplex.com/PCR. The library is tiny and open source.
It allows various kinds of continuations and arbitrations such as:

private static void ParallelCalcTest()
{
Port<int> inPort = new Port<int>();

// Register a handler that is both persistent and concurrent
(i.e. true, true).
Selector.SelectOne(true, true, inPort,
delegate(int num)
{
Console.WriteLine("Result:{0} ThreadID:{1}",num*2,
Thread.CurrentThread.ManagedThreadId);
Thread.Sleep(10); // Force a sleep to help kick-in
multiple threads for demo.
}).Run();

for (int i = 0; i < 20; i++)
{
inPort.Push(i);
}
}

The current default is using the .Net ThreadPool as the dispatcher so actual
number of concurrent threads running will depend on ThreadPool Min settings,
cpus, and what is going on in the system and if you block at all (i.e. Sleep
above) in your code.

Output on my single proc system:
Result:0 ThreadID:7
Result:2 ThreadID:11
Result:4 ThreadID:12
Result:6 ThreadID:13
Result:8 ThreadID:7
Result:10 ThreadID:11
Result:12 ThreadID:12
Result:14 ThreadID:13
Result:16 ThreadID:7
Result:18 ThreadID:11
Result:20 ThreadID:12
Result:22 ThreadID:13
Result:24 ThreadID:7
Result:26 ThreadID:11
Result:28 ThreadID:12
Result:30 ThreadID:13
Result:32 ThreadID:7
Result:34 ThreadID:11
Result:36 ThreadID:12
Result:38 ThreadID:13
--
William Stacey [C# MVP]

| Hello,
|
| I´m having several loops with independent operations.
| Now I want to parallelize these loops for my two test-machines
| (a 4xXeon Server and a DualCore 2 Notebook).
|
| Does anybody know a good tutorial about parallizing with C#?
|
| Which solutions are possible?
|
| At the moment I only can imagine to do it with several threads.
| Are there other ways?
|
| Is it for example possible to run the OpenMP library
| in an unmanaged code area of C#?
|
|
| Regards,
|
| Martin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top