Open mp library functions and environment variables
The document discusses OpenMP library functions and environment variables. It provides examples of some of the most heavily used OpenMP library functions like omp_get_num_threads(), omp_set_num_threads(), and omp_get_thread_num(). It also lists some additional OpenMP library functions for initializing, destroying, setting, and testing simple locks. An example code shows how these functions can be used within an OpenMP parallel region to distribute a loop workload across threads. Finally, it discusses some common OpenMP environment variables for setting the schedule, number of threads, and enabling dynamic thread adjustment.
OpenMP Library Functions *Inaddition to programs, OpenMP provides a set of functions And environment variables. *OpenMP requires conditional compilation in programs #include<omp.h> #ifdef_OPENMP Omp_set_num_threads (4); #endif
3.
The Most Heavilyused OpenMP Library Functions • int omp_get_num_threads (void); Returns the number of threads currently in use. • int omp_set_num_threads (int NumThreads); This function sets the number of threads that will be used when entering a parallel section. It overrides the OMP_NUM_THREADS environment. • int omp_get_thread_num(void); Returns the current thread number between 0 and total number of threads -1. • int omp_get_num_procs(void); Returns the number of available cores. HyperThreading Technology cores will count as two cores. • int omp_get_max_threads(void); Returns the maximum value that can be returned by calls to omp_get_num_threads.
4.
*Some more OpenMplibrary functions, omp_init_lock Initializes a simple lock. omp_destroy_lock Removes a simple lock. omp_set_lock Waits until a simple lock is available. omp_unset_lock Releases a simple lock. omp_test_lock Tests a simple lock.
5.
Float x[8000]; Omp_set_num_threads[4]; #pragma ompparallel private(k) { int num_thds = omp_get_num_threads(); Int ElementsperThread = 8000/ num_thds; Int Tid = omp_get_thread_num(); Int LowBound = Tid*ElementsPerThread; Int UpperBound = LowBound+ElementsPerThread; for( k = LowBound; k<UpperBound ; k++ ) DataProcess(x[k]); } Loop that uses OpenMP function
6.
OpenMP Environment Variables *OMP_SCHEDULEsets the run time schedule type and chunk size Ex: setenv OMP_SCHEDULE "guided,4" setenv OMP_SCHEDULE "dynamic" *OMP_NUM_THREADS sets the number of threads to use during execution Ex: setenv OMP_NUM_THREADS 16 *OMP_DYNAMIC enables or disables dynamic adjustment of the number of threads Ex: setenv OMP_DYNAMIC TRUE