Controlling
the OpenMP Runtime Environment
The OpenMP runtime environment can be controlled in up to three
different ways. We have already encountered the environment variable OMP_NUM_THREADS to set the num-ber of threads that the program uses. However, it is
also possible to set this through pro-grammatic calls to the runtime library or
even as clauses placed onto the directives in the source code. Clauses will
override the settings from calls to API functions, and these will override any
environment settings. This section discusses the various settings that can be
configured and the options available for configuring them.
Setting
the Number of Threads
As previously seen, the number of threads used by an OpenMP application
can be set through the environment variable OMP_NUM_THREADS. It is also possible to set the number of threads using the function
call omp_set_num_threads(), as
shown in Listing 7.46. Calls to omp_set_num_threads() change the default value for all subsequent parallel regions. It is
possible to determine the number of threads using the function call omp_get_max_threads(). The function call omp_get_thread_num() will return a unique ID for each thread.
Listing 7.46 Setting
the Number of Threads
#include <omp.h> #include <stdio.h>
int main()
{
double total = 0.0; double array[1000];
omp_set_num_threads(
2 );
#pragma
omp parallel for reduction( +: total ) for ( int i=0; i<1000; i++ )
{
total += array[i];
}
printf( "Total=%f\n", total );
printf( "Threads=%i\n", omp_get_max_threads()
);
}
The number of threads for a parallel region can be
specified in the source code using the num_threads(threads) clause.
The value for the number of threads can be fixed, or it can be an integer
calculated based on some other factors. Listing 7.47 shows an example of using
a fixed value for this.
Listing 7.47 Setting
the Number of Threads
#include
<omp.h>
#include
<stdio.h>
int
main()
{
double total = 0.0;
double array[1000];
#pragma omp parallel for reduction( +: total ) num_threads( 2 ) for ( int i=0; i<1000; i++ )
{
total += array[i];
}
printf( "Total=%f\n", total);
printf( "Threads=%i\n", omp_get_max_threads()
);
}
The num_threads clause will override the default value just for this single parallel
region. The next parallel region will again take the default value for the
number of threads, unless this too has a num_threads clause.
There is one other environment variable that can
set the number of threads. OMP_THREAD_LIMIT sets the
maximum number of threads that are allowed. It is an
implementation defined as to whether this limit
will be imposed on all attempts to use more threads than this limit. The value
for this limit can be obtained through the func-tion call omp_get_thread_limit().
An OpenMP implementation can honor the environment variable OMP_DYNAMIC. This environment variable can be set to either true or false. If it
is set to true, then
the OpenMP implementation can react to runtime conditions and use fewer threads
than requested for any parallel region. This variable can be set at runtime
with a call to omp_set_dynamic(), and its
value can be read by a call to omp_get_dynamic().
Setting
Runtime Loop Scheduling
The scheduling for loops with the runtime scheduling clause is
controlled with the envi-ronment variable OMP_SCHEDULE. The schedule can also be set at runtime through a call to omp_set_schedule(), and the current schedule can be obtained through a call to omp_get_schedule(). The function calls to get and set the schedule take two parame-ters.
The first is an integer that indicates the scheduling requested. The second is
the chunk size (the second parameter will be ignored for schedules that do not
require a chunk size). The available schedules are omp_sched_static, omp_sched_dynamic, omp_sched_guided, and omp_sched_auto. Listing
7.48 demonstrates using the calls to get and set the schedule.
Listing 7.48 Getting
and Setting the Schedule
#include <omp.h> #include <stdio.h>
int main()
{
omp_sched_t schedule; int chunksize;
omp_get_schedule(
&schedule, &chunksize );
printf( "Schedule = %i, chunksize = %i\n", schedule, chunksize
);
omp_set_schedule(
omp_sched_guided, 10 );
}
Specifying
the Stack Size for Worker Threads
The stack size of the master thread is set through the normal operating
system environ-ment. This can be changed using the ulimit command on UNIX-like platforms. On Windows, the default stack size is
set at link time.
The default stack size for each worker thread
created by the OpenMP runtime library is implementation specific. Depending on
the requirements for stack space, this default may not be sufficient. The
environment variable OMP_STACKSIZE
determines the stack space for the worker threads. There is no call into the
runtime library that sets this size.
The environment variable takes a number with an optional suffix. A number with no suffix is interpreted as kilobytes, the suffix B indicates that the number is in bytes, the suffix K indicates that it is in kilobytes, the suffix M is interpreted as megabytes, and the suffix G indicates gigabytes.
Related Topics
Privacy Policy, Terms and Conditions, DMCA Policy and Compliant
Copyright © 2018-2023 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.