Difference between revisions of "OpenMP hello world in C"
From ScientificComputing
(→openMP Hello World) |
|||
(5 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
+ | __NOTOC__ | ||
+ | {{back_to_tutorials}} | ||
+ | |||
== Load modules == | == Load modules == | ||
We will use the new software stack in this tutorial: | We will use the new software stack in this tutorial: | ||
Line 17: | Line 20: | ||
[jarunanp@eu-login-11 test_openmp]$ | [jarunanp@eu-login-11 test_openmp]$ | ||
− | * Create a file named '' | + | * Create a file named ''hello_omp.c'' and edit it with a text editor (vi, nano or emacs) |
// OpenMP header | // OpenMP header | ||
#include <omp.h> | #include <omp.h> | ||
Line 24: | Line 27: | ||
int main(int argc, char* argv[]) | int main(int argc, char* argv[]) | ||
{ | { | ||
− | //Parallel | + | //Parallel code |
#pragma omp parallel | #pragma omp parallel | ||
{ | { | ||
Line 33: | Line 36: | ||
* Compile the code | * Compile the code | ||
− | [jarunanp@eu-login-11 test_openmp]$ gcc -o | + | [jarunanp@eu-login-11 test_openmp]$ gcc -o hello_omp -fopenmp hello_omp.c |
[jarunanp@eu-login-11 test_openmp]$ ll | [jarunanp@eu-login-11 test_openmp]$ ll | ||
total 20 | total 20 | ||
− | -rwxr-x--- 1 user user-group 8680 Jun 14 10:55 | + | -rwxr-x--- 1 user user-group 8680 Jun 14 10:55 hello_omp |
− | -rw-r----- 1 user user-group 231 Jun 14 10:55 | + | -rw-r----- 1 user user-group 231 Jun 14 10:55 hello_omp.c |
+ | == Run a BSUB interactive session == | ||
* Request an interactive session with a compute node | * Request an interactive session with a compute node | ||
[jarunanp@eu-login-11 test_openmp]$ bsub -n 4 -Is bash | [jarunanp@eu-login-11 test_openmp]$ bsub -n 4 -Is bash | ||
Line 50: | Line 54: | ||
* Execute the code | * Execute the code | ||
[jarunanp@eu-ms-001-08 test_openmp]$ export OMP_NUM_THREADS=4 | [jarunanp@eu-ms-001-08 test_openmp]$ export OMP_NUM_THREADS=4 | ||
− | [jarunanp@eu-ms-001-08 test_openmp]$ ./ | + | [jarunanp@eu-ms-001-08 test_openmp]$ ./hello_omp |
Hello world from thread 1 | Hello world from thread 1 | ||
Hello world from thread 3 | Hello world from thread 3 | ||
Line 58: | Line 62: | ||
[jarunanp@eu-ms-001-08 test_openmp]$ export OMP_NUM_THREADS=8 | [jarunanp@eu-ms-001-08 test_openmp]$ export OMP_NUM_THREADS=8 | ||
− | [jarunanp@eu-ms-001-08 test_openmp]$ ./ | + | [jarunanp@eu-ms-001-08 test_openmp]$ ./hello_omp |
Hello world from thread 0 | Hello world from thread 0 | ||
Hello world from thread 5 | Hello world from thread 5 | ||
Line 68: | Line 72: | ||
Hello world from thread 6 | Hello world from thread 6 | ||
[jarunanp@eu-ms-001-08 test_openmp]$ | [jarunanp@eu-ms-001-08 test_openmp]$ | ||
+ | |||
+ | * Unset the environment variable OMP_NUM_THREADS | ||
+ | [jarunanp@eu-ms-001-08 test_openmp]$ unset OMP_NUM_THREADS | ||
* Exit the interactive session | * Exit the interactive session | ||
Line 73: | Line 80: | ||
exit | exit | ||
[jarunanp@eu-login-11 test_openmp]$ | [jarunanp@eu-login-11 test_openmp]$ | ||
+ | |||
+ | == Submit a batch job with BSUB command line == | ||
+ | * submit a job | ||
+ | [jarunanp@eu-login-11 test_openmp]$ export OMP_NUM_THREADS=4 | ||
+ | [jarunanp@eu-login-11 test_openmp]$ bsub -n 4 -W 10 ./hello_omp | ||
+ | Generic job. | ||
+ | Job <175495653> is submitted to queue <normal.4h>. | ||
+ | |||
+ | == Create a job script == | ||
+ | |||
+ | * Create a job script called ''job_script.bsub'' | ||
+ | #!/usr/bin/bash | ||
+ | #BSUB -n 4 | ||
+ | #BSUB -W 10 | ||
+ | #BSUB -J test_openmp | ||
+ | ./hello_omp | ||
+ | |||
+ | * Submit a job using the job script | ||
+ | [jarunanp@eu-login-11 test_openmp]$ bsub < job_script.bsub | ||
+ | Generic job. | ||
+ | Job <175495839> is submitted to queue <normal.4h>. | ||
+ | |||
+ | |||
+ | |||
+ | {{back_to_tutorials}} |
Latest revision as of 12:17, 14 June 2021
< Examples |
Load modules
We will use the new software stack in this tutorial:
[jarunanp@eu-login-11 ~]$ env2lmod [jarunanp@eu-login-11 ~]$ module list Currently Loaded Modules: 1) StdEnv 2) gcc/4.8.5
openMP Hello World
- Go to $SCRATCH and create a work directory
[jarunanp@eu-login-11 ~]$ cd $SCRATCH [jarunanp@eu-login-11 jarunanp]$ pwd /cluster/scratch/jarunanp [jarunanp@eu-login-11 jarunanp]$ mkdir test_openmp [jarunanp@eu-login-11 jarunanp]$ cd test_openmp [jarunanp@eu-login-11 test_openmp]$
- Create a file named hello_omp.c and edit it with a text editor (vi, nano or emacs)
// OpenMP header #include <omp.h> #include <stdio.h> int main(int argc, char* argv[]) { //Parallel code #pragma omp parallel { printf("Hello world from thread %d\n", omp_get_thread_num()); } }
- Compile the code
[jarunanp@eu-login-11 test_openmp]$ gcc -o hello_omp -fopenmp hello_omp.c [jarunanp@eu-login-11 test_openmp]$ ll total 20 -rwxr-x--- 1 user user-group 8680 Jun 14 10:55 hello_omp -rw-r----- 1 user user-group 231 Jun 14 10:55 hello_omp.c
Run a BSUB interactive session
- Request an interactive session with a compute node
[jarunanp@eu-login-11 test_openmp]$ bsub -n 4 -Is bash Generic job. Job <175495032> is submitted to queue <normal.4h>. <<Waiting for dispatch ...>> <<Starting on eu-ms-001-08>> FILE: /sys/fs/cgroup/cpuset/lsf/euler/job.175495032.32022.1623661994/tasks [jarunanp@eu-ms-001-08 test_openmp]$
- Execute the code
[jarunanp@eu-ms-001-08 test_openmp]$ export OMP_NUM_THREADS=4 [jarunanp@eu-ms-001-08 test_openmp]$ ./hello_omp Hello world from thread 1 Hello world from thread 3 Hello world from thread 0 Hello world from thread 2 [jarunanp@eu-ms-001-08 openmp]$
[jarunanp@eu-ms-001-08 test_openmp]$ export OMP_NUM_THREADS=8 [jarunanp@eu-ms-001-08 test_openmp]$ ./hello_omp Hello world from thread 0 Hello world from thread 5 Hello world from thread 3 Hello world from thread 4 Hello world from thread 1 Hello world from thread 7 Hello world from thread 2 Hello world from thread 6 [jarunanp@eu-ms-001-08 test_openmp]$
- Unset the environment variable OMP_NUM_THREADS
[jarunanp@eu-ms-001-08 test_openmp]$ unset OMP_NUM_THREADS
- Exit the interactive session
[jarunanp@eu-ms-001-08 test_openmp]$ exit exit [jarunanp@eu-login-11 test_openmp]$
Submit a batch job with BSUB command line
- submit a job
[jarunanp@eu-login-11 test_openmp]$ export OMP_NUM_THREADS=4 [jarunanp@eu-login-11 test_openmp]$ bsub -n 4 -W 10 ./hello_omp Generic job. Job <175495653> is submitted to queue <normal.4h>.
Create a job script
- Create a job script called job_script.bsub
#!/usr/bin/bash #BSUB -n 4 #BSUB -W 10 #BSUB -J test_openmp ./hello_omp
- Submit a job using the job script
[jarunanp@eu-login-11 test_openmp]$ bsub < job_script.bsub Generic job. Job <175495839> is submitted to queue <normal.4h>.
< Examples |