|
| 1 | +/* -*- C -*- |
| 2 | + * |
| 3 | + * $HEADER$ |
| 4 | + * |
| 5 | + * The most basic of MPI applications |
| 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/hwloc.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; |
| 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 | + cpus = hwloc_bitmap_alloc(); |
| 34 | + rc = hwloc_get_cpubind(opal_hwloc_topology, cpus, HWLOC_CPUBIND_PROCESS); |
| 35 | + hwloc_bitmap_list_asprintf(&bindings, cpus); |
| 36 | + |
| 37 | + printf("[%s;%d] Hello, World, I am %d of %d [%d local peers]: get_cpubind: %d bitmap %s\n", |
| 38 | + hostname, (int)getpid(), rank, size, orte_process_info.num_local_peers, rc, |
| 39 | + (NULL == bindings) ? "NULL" : bindings); |
| 40 | + |
| 41 | + nrcpus = sysconf(_SC_NPROCESSORS_ONLN); |
| 42 | + mask = CPU_ALLOC(nrcpus); |
| 43 | + csize = CPU_ALLOC_SIZE(nrcpus); |
| 44 | + CPU_ZERO_S(csize, mask); |
| 45 | + if ( sched_getaffinity(0, csize, mask) == -1 ) { |
| 46 | + CPU_FREE(mask); |
| 47 | + perror("sched_getaffinity"); |
| 48 | + return -1; |
| 49 | + } |
| 50 | + |
| 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 | + CPU_FREE(mask); |
| 58 | + |
| 59 | + MPI_Finalize(); |
| 60 | + return 0; |
| 61 | +} |
0 commit comments