| | 1 | = MPI C = |
| | 2 | == Hello World == |
| | 3 | {{{#!C |
| | 4 | /* |
| | 5 | hello_mpi.c: display a message on the screen |
| | 6 | */ |
| | 7 | #include <stdio.h> |
| | 8 | #include <mpi.h> |
| | 9 | |
| | 10 | int main (int argc, char *argv[]) { |
| | 11 | int myid,numproc; |
| | 12 | |
| | 13 | /* Initialize */ |
| | 14 | MPI_Init(&argc,&argv); |
| | 15 | |
| | 16 | /* get myid and # of processors */ |
| | 17 | MPI_Comm_size(MPI_COMM_WORLD,&numproc); |
| | 18 | MPI_Comm_rank(MPI_COMM_WORLD,&myid); |
| | 19 | |
| | 20 | printf("hello from %d\n", myid); |
| | 21 | |
| | 22 | /* wait until all processors come here */ |
| | 23 | MPI_Barrier (MPI_COMM_WORLD); |
| | 24 | |
| | 25 | if ( myid == 0 ) { |
| | 26 | /* only id = 0 do this */ |
| | 27 | printf("%d processors said hello!\n",numproc); |
| | 28 | } |
| | 29 | |
| | 30 | MPI_Finalize(); |
| | 31 | } |
| | 32 | }}} |
| | 33 | |
| | 34 | === Compile === |
| | 35 | {{{mpicc hello_mpi.c}}} |
| | 36 | |
| | 37 | == Example of Jobscript == |
| | 38 | {{{#!sh |
| | 39 | #!/bin/bash |
| | 40 | #SBATCH --job-name=HelloC_MPI |
| | 41 | #SBATCH --qos=normal |
| | 42 | #SBATCH --time=0-00:10:00 |
| | 43 | #SBATCH --nodes=4 |
| | 44 | #SBATCH --ntasks-per-node=1 |
| | 45 | |
| | 46 | ########## THE JOB ITSELF ########### |
| | 47 | |
| | 48 | echo Start Job |
| | 49 | pwd |
| | 50 | cat $SLURM_JOB_NODELIST |
| | 51 | echo Number of tasks: $SLURM_NTASKS |
| | 52 | |
| | 53 | mpirun ./a.out |
| | 54 | |
| | 55 | echo End job |
| | 56 | }}} |