1
1
openmpi/orte/test/mpi/binding.c
Charles Shereda cbc6feaab2 Created opal_gethostname() as safer gethostname substitute.
The opal_gethostname() function provides a more robust mechanism
to retrieve the hostname than gethostname(), which can return
results that are not null-terminated, and which can vary in its
behavior from system to system.

opal_gethostname() just returns the value in opal_process_info.nodename;
this is populated in opal_init_gethostname() inside opal_init.c.

-Changed all gethostname calls in opal subtree to opal_gethostname
-Changed all gethostname calls in orte subtree to opal_gethostname
-Changed all gethostname calls in ompi subdir to opal_gethostname
-Changed all gethostname calls in oshmem subdir to opal_gethostname
-Changed opal_if.c in test subdir to use opal_gethostname
-Changed opal_init.c to include opal_init_gethostname. This function
 returns an int and directly sets opal_process_info.nodename per
 jsquyres' modifications.

Relates to open-mpi#6801

Signed-off-by: Charles Shereda <cpshereda@lanl.gov>
2020-01-13 08:52:17 -08:00

64 строки
1.6 KiB
C

/* -*- C -*-
*
* $HEADER$
*
* The most basic of MPI applications
*/
#define _GNU_SOURCE
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <sched.h>
#include "opal/mca/hwloc/base/base.h"
#include "opal/runtime/opal.h"
#include "mpi.h"
#include "orte/util/proc_info.h"
int main(int argc, char* argv[])
{
int rank, size, rc;
hwloc_cpuset_t cpus;
char *bindings = NULL;
cpu_set_t *mask;
int nrcpus, c;
size_t csize;
const char *hostname;
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
hostname = opal_gethostname();
if (OPAL_SUCCESS == opal_hwloc_base_get_topology()) {
cpus = hwloc_bitmap_alloc();
rc = hwloc_get_cpubind(opal_hwloc_topology, cpus, HWLOC_CPUBIND_PROCESS);
hwloc_bitmap_list_asprintf(&bindings, cpus);
}
printf("[%s;%d] Hello, World, I am %d of %d [%d local peers]: get_cpubind: %d bitmap %s\n",
hostname, (int)getpid(), rank, size, orte_process_info.num_local_peers, rc,
(NULL == bindings) ? "NULL" : bindings);
nrcpus = sysconf(_SC_NPROCESSORS_ONLN);
mask = CPU_ALLOC(nrcpus);
csize = CPU_ALLOC_SIZE(nrcpus);
CPU_ZERO_S(csize, mask);
if ( sched_getaffinity(0, csize, mask) == -1 ) {
perror("sched_getaffinity");
} else {
for ( c = 0; c < nrcpus; c++ ) {
if ( CPU_ISSET_S(c, csize, mask) ) {
printf("[%s:%d] CPU %d is set\n", hostname, (int)getpid(), c);
}
}
}
CPU_FREE(mask);
MPI_Finalize();
return 0;
}