Intel® Moderncode for Parallel Architectures
Support for developing parallel programming applications on Intel® Architecture.

Chosing CPU for thread programatically

nately
Beginner
364 Views
Hello, I have a following question:
I'm going to write a program where one thread is busy on data aquisition from PCI card, and other threads are doing all other things. Now, I would like to assign that aquisition thread to one CPU, and other threads to the other CPU (It may be really two CPU's, or just hyper-threaded). How do I choose CPU for thread programatically in e.g. C++? Is it possible, or will it work as assigning CPU according to how much one is busy, so the other will be then assigned?

thank you very much for answering to this question
sincerely
Peter Sliwka
0 Kudos
4 Replies
Alexander_L_Intel
364 Views
You check out SetThreadAffinityMask() from MS Platform SDK.
There are very limited number of cases when it is beneficial to use. In most cases it is better to let Windows scheduler assigningCPUs (or cores) forevery thread for every time slice. Operating system cares about the whole system functioning and takes into account every system parameter (number of CPUs, cores/hipersthreadingof CPUs, shared or split cache) while an application (programmer) cares about it'sparticular function only.
When you have to make sure that the CPU core is the same for every instruction of your code, you use thread affinity.
0 Kudos
nately
Beginner
364 Views
Thank you, that was very helpful for me :)
0 Kudos
Anonymous
Not applicable
364 Views

I've done some testing via Managed C# code with Thread Affinity and noticed a performance decline when tasking a single HT/Multi-core processor by thread as opposed to usingThreadPool Queue.

Im far (quite literally in outter space) from being an expert in this area but heck I might as well throw my comments out and see what other have to say.. I know I have quite a bit of learning to do anyway..

Kevin (Intel ASP.Net Web dude...)

0 Kudos
jimdempseyatthecove
Honored Contributor III
364 Views

From your description "single HT/Multi-core processor" Windows will percieve 4 processors when in fact you have 2 cores, each with two HT thread capability. What you want is one core dedicated to your applicationi (both HT threads on that core reserved but only one in use).

I think your best results (for the priorityapplication thread) would be to isolate each core. With "processors" 0 and 1 on one core and "processors" 2 and 3 on the second core. Using the Task Manager | Processes tab | right-click on each ofnot-your-application processies and set affinity to restrict processing to the 1st core (cpu0 and cpu1). Then modify your application to set affinity to CPU2 (2nd core).

If you start any applications after initialization (Task Manager setting of affinity) then you will have to reset affinity on the new processes.

Experiment running this way and check results.

Jim Dempsey

0 Kudos
Reply