Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I wish to write a code of the following format:
main()
{
}
a(){}
b(){}
c(){}
d(){}
.
.
.
.

k(){}


I wish to run the code in parallel such that 1st 5 functions have a processor to themselves(assuming 12 cores are available on the system)
After any of the 1st 5 finishes execution, the 6th function is assigned to that and so on.

The functions are independent of each other and run in a sequential manner. I wish to reduce the execution time by making them run in parallel. How do I write such a program?
Please help.
Thanks :)
Posted
Comments
Sergey Alexandrovich Kryukov 16-Jun-11 5:15am    
Yes, but why, why?! (Just learning, or what?)
--SA

Your best bet would be to use the Task Parallel Library (TPL). The TPL will automatically scale to include all cores on the machine in which it runs. So you could do something like this

C#
void q1()
       { }
       void q2()
       { }
       void q3()
       {}


C#
void Run()
      {
          Action[] actions = new Action[] { q1, q2, q3 };
          Parallel.Invoke(actions);
      }


If you need to do things like cancel the threads, run certain actions sequentially or return values from the function then you can use the Task Class Like this

C#
Task.Factory.StartNew(q1).ContinueWith(t => q2()).ContinueWith(t => q3());


Here is a good set of Articles from Sacha Barber on the TPL

Task Parallel Library: 1 of n[^]

Hope this helps
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 16-Jun-11 5:14am    
I think TPL is a good idea is some (not many) cases. OP's problem (if there is a practical problem at all, it could be just research or learning) may or may not be the case. So, I voted 5.

At the same time I'm sure this is not what OP wanted or thinks of. So, I answered how to deal with CPU affinity. (Yes, I warned against it.)

Please see.
--SA
First of all: in most cases, you should not control it explicitly. Could you explain why doing so?

The system runs several threads on several cores. Which core is loaded with which thread at each thread time spice is decided by the system threading scheduler.

However, there are ways to affect this behavior programmatically.
Remember, in almost all cases this is not recommended! The cases when it can help are very rare.

See the following Windows API: GetThreadAffinityMask/SetThreadAffinityMask, GetProcessAffinityMask/SetProcessAffinityMask and SetThreadIdealProcessor.

General information on thread affinity: http://en.wikipedia.org/wiki/Processor_affinity[^],
see also this discussion: The perils of thread affinity:
http://www.bluebytesoftware.com/blog/PermaLink,guid,8c2fed10-75b2-416b-aabc-c18ce8fe2ed4.aspx[^].

On MSDN, start here:
http://msdn.microsoft.com/en-us/library/ms685096(v=VS.85).aspx[^],
http://msdn.microsoft.com/en-us/library/ms684251(v=vs.85).aspx[^].

Again, don't interfere in thread/process affinity unless you know exactly what are you doing!

(I know just one case when modification of affinity can be helpful: working with standard hardware (typically industrial) which is "bad" in the sense that it requires extensive polling as interrupt-driven operation is node accessible via API; in this case it is beneficial to dedicate on code to some threads serving these "bad" parts of equipment, which makes other threads' timing to be more predictable.)

—SA
 
Share this answer
 
Comments
Wayne Gaylard 16-Jun-11 5:24am    
+5. Although I will stick to the TPL. :)
Sergey Alexandrovich Kryukov 16-Jun-11 5:28am    
Thank you, Wayne.
My answer is more of a warning against affinity. TPL is healthy approach, not taking care of affinity -- too, depending on application.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900