#include <omp.h> #include <stdio.h> #include <unistd.h> int main (int argc, char **argv) { int nthreads, tid; char host[32]; #pragma omp parallel private(nthreads, tid, host) { tid = omp_get_thread_num(); gethostname(host,32); printf("Hello world from the thread %d running on %s\n", tid,host); if (tid == 0) { nthreads = omp_get_num_threads(); printf("[Thread 0 say: there are %d threads]\n", nthreads); } } }
gcc -fopenmp -O2 omp_hello.c -o omp_hello.out
omp_hello.out : omp_hello.c gcc -fopenmp -O2 -o $@ omp_hello.c
#!/bin/sh #PBS -l walltime=00:00:30 #PBS -l select=1:ncpus=4:ompthreads=4 cd $PBS_O_WORKDIR echo "OMP_NUM_THREADS = $OMP_NUM_THREADS" echo ./omp_hello.out
OMP_NUM_THREADS = 4 Hello world from the thread 3 running on cn101 Hello world from the thread 0 running on cn101 [Thread 0 say: there are 4 threads] Hello world from the thread 1 running on cn101 Hello world from the thread 2 running on cn101
program hello integer id, nthrds integer omp_get_thread_num, omp_get_num_threads !$omp parallel private(id) id = omp_get_thread_num() print *, 'hello world from thread', id !$omp barrier if ( id .eq. 0 ) then nthrds = omp_get_num_threads() print *, 'there are', nthrds, 'threads' end if !$omp end parallel end
gfortran -O2 -fopenmp -o omp_hello.out omp_hello.f90
omp_hello.out : omp_hello.f90 gfortran -O2 -fopenmp -o $@ omp_hello.f90