2. OpenMP Library Functions
*In addition 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 Heavily used 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 OpenMp library 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 omp parallel 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_SCHEDULE sets 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