This source file includes following definitions.
- main
1
2
3
4
5
6
7
8 #define _GNU_SOURCE
9 #include <stdio.h>
10 #include <sys/types.h>
11 #include <unistd.h>
12 #include <sched.h>
13 #include "opal/mca/hwloc/base/base.h"
14 #include "mpi.h"
15
16 #include "orte/util/proc_info.h"
17
18 int main(int argc, char* argv[])
19 {
20 int rank, size, rc;
21 hwloc_cpuset_t cpus;
22 char *bindings = NULL;
23 cpu_set_t *mask;
24 int nrcpus, c;
25 size_t csize;
26 char hostname[1024];
27
28 MPI_Init(&argc, &argv);
29 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
30 MPI_Comm_size(MPI_COMM_WORLD, &size);
31
32 gethostname(hostname, 1024);
33
34 if (OPAL_SUCCESS == opal_hwloc_base_get_topology()) {
35 cpus = hwloc_bitmap_alloc();
36 rc = hwloc_get_cpubind(opal_hwloc_topology, cpus, HWLOC_CPUBIND_PROCESS);
37 hwloc_bitmap_list_asprintf(&bindings, cpus);
38 }
39
40 printf("[%s;%d] Hello, World, I am %d of %d [%d local peers]: get_cpubind: %d bitmap %s\n",
41 hostname, (int)getpid(), rank, size, orte_process_info.num_local_peers, rc,
42 (NULL == bindings) ? "NULL" : bindings);
43
44 nrcpus = sysconf(_SC_NPROCESSORS_ONLN);
45 mask = CPU_ALLOC(nrcpus);
46 csize = CPU_ALLOC_SIZE(nrcpus);
47 CPU_ZERO_S(csize, mask);
48 if ( sched_getaffinity(0, csize, mask) == -1 ) {
49 perror("sched_getaffinity");
50 } else {
51 for ( c = 0; c < nrcpus; c++ ) {
52 if ( CPU_ISSET_S(c, csize, mask) ) {
53 printf("[%s:%d] CPU %d is set\n", hostname, (int)getpid(), c);
54 }
55 }
56 }
57
58 CPU_FREE(mask);
59
60 MPI_Finalize();
61 return 0;
62 }