1

Config : Windows 10, mingw-x64, Core 2 (4 logical)

The following code

#include <cstdio>
#include <omp.h>
#include <windows.h>

int main() {

    #pragma omp parallel
    {
        std::printf ("Doing Stuff on thread: %d  on Core #%d\n", omp_get_thread_num(),\
                      GetCurrentProcessorNumber());
    }

    return 0;
}

Sometimes it gives output:

Doing Stuff on thread: 1  on Core #1
Doing Stuff on thread: 0  on Core #2
Doing Stuff on thread: 2  on Core #2
Doing Stuff on thread: 3  on Core #2

Although most of the times it gives

Doing Stuff on thread: 2  on Core #1
Doing Stuff on thread: 1  on Core #3
Doing Stuff on thread: 0  on Core #2
Doing Stuff on thread: 3  on Core #0

which is expected.

How can I make sure that the threads have their affinity set for a different core? Is there a way to force the scheduler to do this?

Evg
  • 25,259
  • 5
  • 41
  • 83
Recoder
  • 818
  • 8
  • 10
  • why you think it would make any difference to your program? – apple apple Aug 13 '18 at 12:53
  • @appleapple We are supposed to demonstrate parallel algo for coursework. Just don't want to make it run sequential just in case. Although I could run in linux if needed, wanted to know for windows too. – Recoder Aug 13 '18 at 13:22
  • even if it runs on same core, it may run in parallel (interleaved). otherwise you cannot have more threads than core. – apple apple Aug 13 '18 at 13:24
  • Since my laptop is dual core + hyperthreaded, I am not entirely sure whether the proc ID 2, 3 makes much of a difference. – Recoder Aug 13 '18 at 13:31
  • Related question: https://stackoverflow.com/questions/663958/how-to-control-which-core-a-process-runs-on – Evg Aug 13 '18 at 13:43
  • run in sequential (randomly) **is** what would really happen. avoid it is not good for learning parallel processing, I think. – apple apple Aug 13 '18 at 13:54
  • you can `sleep` and force some thread finish later, it may be a better demo. – apple apple Aug 13 '18 at 13:57
  • HW threads are not cores so forcing the affinity on the them might not be a good idea anyway. You should restrict your number of OpenMP threads to the number of core (2 here) with `OMP_NUM_THREADS=2`. Now, if you want also to force affinity, you can set `OMP_PROC_BIND=true` and `OMP_PLACES=cores` – Gilles Aug 13 '18 at 14:21
  • Guessing you may be using gcc, libgomp on Windows doesn't support omp_places so the placement of threads will get vary. Although Windows was supposed to have hyperthreads scheduled since win7sp1 it doesn't keep 2 threads on separate cores and best performance is likely for 3 threads unless you can disable hyperthreads. – tim18 Aug 14 '18 at 13:22
  • https://stackoverflow.com/q/24862488/2542702 – Z boson Aug 20 '18 at 07:21

0 Answers0