Skip to content

Instantly share code, notes, and snippets.

@AndiH
Last active September 30, 2019 16:03
Show Gist options
  • Save AndiH/f11cfdc45db7f6c4d554a5b3d7eedef4 to your computer and use it in GitHub Desktop.
Save AndiH/f11cfdc45db7f6c4d554a5b3d7eedef4 to your computer and use it in GitHub Desktop.
MPI Rank, OpenMP Thread ID, Core ID
CC = mpicc
C_OPT ?= -fopenmp
.PHONY: all
all: omp_id
omp_id: omp_id.c Makefile
$(CC) $(C_OPT) -o $@ $<
#define _GNU_SOURCE // sched_getcpu(3) is glibc-specific (see the man page)
#include <stdio.h>
#include <mpi.h>
#include <sched.h>
#include <omp.h>
int main(int argc, char** argv) {
MPI_Init(NULL, NULL);
int world_size;
MPI_Comm_size(MPI_COMM_WORLD, &world_size);
int world_rank;
MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);
#pragma omp parallel
{
int thread_num = omp_get_thread_num();
int cpu_num = sched_getcpu();
printf("MPI Rank: %3d, OpenMP Thread ID: %3d, Physical CPU ID: %3d\n", world_rank, thread_num, cpu_num);
}
MPI_Finalize();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment