/* anvil_hello.c: Where is this job running, and on which cores?
 *
 * Compile: gcc -Wall -O2 -fopenmp -o anvil_hello anvil_hello.c
 * Run:     through Slurm, with omp_job.sh
 */
#define _GNU_SOURCE
#include <stdio.h>
#include <sched.h>
#include <unistd.h>
#include <omp.h>

int main(void) {
    char host[256];
    gethostname(host, sizeof host);

    printf("Node: %s\n", host);
    printf("Cores this job may use: %d\n", omp_get_num_procs());

    double start = omp_get_wtime();
#   pragma omp parallel
    {
        int my_rank = omp_get_thread_num();
        int thread_count = omp_get_num_threads();
        int core = sched_getcpu();   /* the core this thread is on right now */

#       pragma omp critical
        printf("Thread %3d of %3d is on core %3d\n", my_rank, thread_count, core);
    }
    printf("Parallel region took %.6f seconds\n", omp_get_wtime() - start);
    return 0;
}
