2-OpenMP.pdf
Document Details
Uploaded by CoherentYtterbium
Instituto Politécnico do Cávado e do Ave
Tags
Related
- DS 642: Applications of Parallel Computing Lecture 6 PDF
- Parallel & Distributed Computing PDF
- 1- Introduction to Parallel Computing.pdf
- Theoretical and Practical Foundations of Parallel Computing in Numerical Methods PDF
- CS621 Parallel and Distributed Computing Short Notes PDF
- Parallel Computing Unit 1 - Introduction to Parallel Computing PDF
Full Transcript
OpenMP High Performance Computing Nuno Lopes Motivation u Library to develop parallel applications using shared memory. u High level abstraction that allows adaptation of sequential applications in a simple way. u Pthreads library is more complex in terms of API. P...
OpenMP High Performance Computing Nuno Lopes Motivation u Library to develop parallel applications using shared memory. u High level abstraction that allows adaptation of sequential applications in a simple way. u Pthreads library is more complex in terms of API. Pragma Mechanism in C u OMP commands are based on pragmas. u Pragmas allow you to add functionality to a C program, without invalidating its generic compilation. u # pragma omp u Compilers that support these directives make use of them. u Compilers that do not support these directives ignore them and compile the program as usual. u Correctly coded OpenMP program always compiles on any platform, even without support. Compiler Extensions for OpenMP u Auxiliary Library “omp.h” #include u Pre-compiler directive to validate support: # ifdef _OPENMP... #else... #endif Compilação e execução de programas OpenMP u Compiling option: u -fopenmp u gcc -fopenmp hello_omp.c -o hello_omp u Linker option: u -lomp u Optional if including -f Parallel Directive # pragma omp parallel u Creates multiple threads of execution, threads, in the same process, which each execute the code immediately following the pragma. u When the code is finished, all threads wait for the completion of the remaining threads. Parallel Directive u Code: # pragma omp parallel printf("Esta é a thread %d, num threads %d\n", omp_get_thread_num(), omp_get_num_threads()); printf(“Fim\n”); u Result: Esta é a thread 2, num threads 3 Esta é a thread 1, num threads 3 Esta é a thread 0, num threads 3 Fim numthreads Option # pragma omp parallel num_threads(thrcnt) u numthreads() defines thrcnt number of threads to parallelise the application. u If not specified, the number of threads created is set by the system during execution, e.g. number of cores. Auxiliary Functions in Library u int omp_get_thread_num(void) Returns the identifier of the calling thread. u int omp_get_num_threads(void) Returns the number of existing threads. u omp_set_num_threads(int) Sets number of threads to use. u OMP_NUM_THREADS=4 Environment variable, which allows to define number of threads. u Option -lomp allows to compile code to access these functions, even without the OpenMP parallelism. Parallel For Diretive # pragma omp parallel for for( int i=0; i