Logo Cineca Logo SCAI

You are here

Exercise 1

Compile and run the following "Hello World" and experiment with the OMP_NUM_THREADS variable.

If you encounter any error during the execution, try to fix it!

 

C source code

#include <stdio.h>
#ifdef _OPENMP 
#include<omp.h> 
#endif

int main(int argc, char* argv[])
{
#ifdef _OPENMP
int iam;
#pragma omp parallel
    { /* the parallel block starts here */
        iam=omp_get_thread_num();
#pragma omp critical
        printf("Hello from %d\n",iam);
    } /* the parallel block ends here */

#else
        printf("Hello, this is a serial program.\n");
#endif
    return 0;
}

 

FORTRAN source code

Program Hello_from_Threads #ifdef _OPENMP use omp_lib #endif implicit none integer :: iam

#ifdef _OPENMP !$omp parallel iam=omp_get_thread_num() !$omp critical write(*,*) 'Hello from', iam !$omp end critical !$omp end parallel #else write(*,*) 'Hello, this is a serial program' #endif end program Hello_from_Threads