| 1 | = OpenMP C = |
| 2 | |
| 3 | == Hello World == |
| 4 | {{{#!C |
| 5 | /* |
| 6 | hellp_omp.c: display a message on the screen |
| 7 | */ |
| 8 | #include <stdio.h> |
| 9 | #include <omp.h> |
| 10 | |
| 11 | int main () { |
| 12 | int id, nthreads; |
| 13 | printf("C Start\n"); |
| 14 | #pragma omp parallel private(id) |
| 15 | { |
| 16 | id = omp_get_thread_num(); |
| 17 | printf("hello from %d\n", id); |
| 18 | #pragma omp barrier |
| 19 | if ( id == 0 ) { |
| 20 | nthreads = omp_get_num_threads(); |
| 21 | printf("%d threads said hello!\n",nthreads); |
| 22 | } |
| 23 | } |
| 24 | printf("End\n"); |
| 25 | } |
| 26 | }}} |
| 27 | |
| 28 | === Compile with GNU C === |
| 29 | {{{gcc hello_omp.c -fopemp}}} |
| 30 | |
| 31 | === Compile with Intel C === |
| 32 | {{{icc hello_omp.c -openmp}}} |
| 33 | |
| 34 | === Example of Jobscript === |
| 35 | {{{#!sh |
| 36 | #SBATCH --job-name=HelloC_OMP |
| 37 | #SBATCH --qos=normal |
| 38 | #SBATCH --time=00:10:00 |
| 39 | #SBATCH --nodes=1 |
| 40 | #SBATCH --ntasks-per-node=1 |
| 41 | #SBATCH --cpus-per-task=4 |
| 42 | |
| 43 | ########## THE JOB ITSELF ########### |
| 44 | export OMP_NUM_THREADS=$SLURM_CPUS_PER_TASK |
| 45 | |
| 46 | echo Start Job |
| 47 | pwd |
| 48 | cat $SLURM_JOB_NODELIST |
| 49 | |
| 50 | ./a.out |
| 51 | |
| 52 | echo End job |
| 53 | }}} |