Loading...
Searching...
No Matches
omp_threads.c
Go to the documentation of this file.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#if defined(_OPENMP)
#include <omp.h>
#endif
#include <grass/gis.h>
#include <grass/glocale.h>
/*! \brief Set the number of threads for OpenMP
The intended usage is at the beginning of a C tool when parameters are
processed, namely the G_OPT_M_NPROCS standard option.
If \em nprocs is set to 0, default OpenMP internal logic is used.
If \em nprocs is a positive number, specified number of threads is used.
If \em nprocs is a negative number, then <em>maximum threads - number</em>
is used instead (e.g. to keep \em number of cores free for other use.
\param opt A nprocs Option struct to specify the number of threads
\return the number of threads set up for OpenMP parallel computing
\since version 8.5
*/
int G_set_omp_num_threads(struct Option *opt)
{
/* make sure Option is not null */
if (opt == NULL)
G_fatal_error(_("Option is NULL."));
else if (opt->key == NULL)
G_fatal_error(_("Option key is NULL."));
int threads = atoi(opt->answer);
#if defined(_OPENMP)
if (threads == 0) {
threads = omp_get_max_threads();
}
else {
int num_logic_procs = omp_get_num_procs();
if (threads < 1) {
threads += num_logic_procs;
threads = (threads < 1) ? 1 : threads;
}
omp_set_num_threads(threads);
}
G_verbose_message(n_("One thread is set up for parallel computing.",
"%d threads are set up for parallel computing.",
threads),
threads);
#else
if (!(threads == 0 || threads == 1)) {
G_warning(_("GRASS is not compiled with OpenMP support, parallel "
"computation is disabled. Only one thread will be used."));
}
threads = 1;
#endif
return threads;
}
NULL
#define NULL
Definition ccmath.h:32
G_verbose_message
void G_verbose_message(const char *msg,...)
Print a message to stderr but only if module is in verbose mode.
Definition gis/error.c:108
G_fatal_error
void G_fatal_error(const char *msg,...)
Print a fatal error message to stderr.
Definition gis/error.c:159
G_warning
void G_warning(const char *msg,...)
Print a warning message to stderr.
Definition gis/error.c:203
G_set_omp_num_threads
int G_set_omp_num_threads(struct Option *opt)
Set the number of threads for OpenMP The intended usage is at the beginning of a C tool when paramete...
Definition omp_threads.c:26