QDP++
qdp_bgq_threadbind.cc
Go to the documentation of this file.
1#include "qdp.h"
2#include "qdp_threadbind.h"
3
4
5#include <sys/types.h>
6#include <unistd.h>
7#include <sys/syscall.h>
8#include <sched.h>
9#include <spi/include/kernel/location.h>
10
11#ifdef QDP_USE_OMP_THREADS
12#include <omp.h>
13#endif
14
15namespace QDP
16{
17
18#ifdef QDP_USE_OMP_THREADS
19 void setThreadAffinity(int nCores, int threadsPerCore)
20 {
21 #pragma omp parallel
22 {
23
24 // Get the OpenMP thread number
25 int tid = omp_get_thread_num();
26
27 // Split into core and SIMT ID (assuming SIMT runs fastest)
28 int core = tid/threadsPerCore;
29 int smtid = tid - core*threadsPerCore;
30
31 // Convert to hardware processor ID, basically using same scheme as
32 // Table 3-2 of the IBM redbook:
33 // http://www.redbooks.ibm.com/redbooks/pdfs/sg247948.pdf
34
35 // NB: 4 is hardwired here for BG/Q. (NB: This would let us run with
36 // 'gaps' i.e. even 2 threads per core but still get the binding right.)
37
38 int hw_procid = smtid + 4*core;
39
40 cpu_set_t set;
41
42 CPU_ZERO(&set);
43 CPU_SET(hw_procid, &set);
44
45 // Bind the OMP threads to hardware threads
46 if((sched_setaffinity(0, sizeof(set), &set)) == -1) {
47 QDPIO::cout << "WARN: Cound not do sched_setaffinity for thread " << tid << "\n";
48 }
49 } // pragma omp parallel
50 } // function
51
52
53
54 // BlueGene/Q reporting via SPI Locations interface
55 void reportAffinity()
56 {
57
58 uint32_t cids[64], htids[64];
59
60#pragma omp parallel
61 {
62 htids[omp_get_thread_num()] = Kernel_ProcessorThreadID();
63 cids[omp_get_thread_num()] = Kernel_ProcessorCoreID();
64 }
65
66 QDPIO::cout << "Thread Bindings\n";
67 for (int i = 0; i < omp_get_max_threads(); ++i)
68 QDPIO::cout <<"OMP thread " << i << ": core = " << cids[i] << " hardware thread = " << htids[i] << std::endl;
69 }
70
71#else
72 void setThreadAffinity(int nCores, int threadsPerCore)
73 {
74 QDPIO::cout << "Thread binding only implemented for OpenMP Threads\n";
75 }
76
78 {
79 QDPIO::cout << "reportAffinity: only implemented for OpenMP Threads \n";
80 }
81#endif
82 }; // END namespace
StandardOutputStream cout
Definition qdp_stdio.cc:21
Yet another random number generator.
void setThreadAffinity(int nCores, int threadsPerCore)
void reportAffinity()
Primary include file for QDP.