GRASS GIS 8 Programmer's Manual  8.5.0dev(2024)-602118adcc
omp_threads.c
Go to the documentation of this file.
1 #if defined(_OPENMP)
2 #include <omp.h>
3 #endif
4 
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include <grass/gis.h>
9 #include <grass/glocale.h>
10 
11 /*! \brief Set the number of threads for OpenMP
12  The intended usage is at the beginning of a C tool when parameters are
13  processed, namely the G_OPT_M_NPROCS standard option.
14 
15  \param opt A nprocs Option struct to specify the number of threads
16  \return the number of threads set up for OpenMP parallel computing
17 */
18 
19 int G_set_omp_num_threads(struct Option *opt)
20 {
21  /* make sure Option is not null */
22  if (opt == NULL)
23  G_fatal_error(_("Option is NULL."));
24  else if (opt->key == NULL)
25  G_fatal_error(_("Option key is NULL."));
26 
27  int threads = atoi(opt->answer);
28 #if defined(_OPENMP)
29  int num_logic_procs = omp_get_num_procs();
30  if (threads < 1) {
31  threads += num_logic_procs;
32  threads = (threads < 1) ? 1 : threads;
33  }
34  omp_set_num_threads(threads);
35  G_verbose_message(_("%d threads are set up for parallel computing."),
36  threads);
37 #else
38  if (threads != 1) {
39  G_warning(_("GRASS GIS is not compiled with OpenMP support, parallel "
40  "computation is disabled. Only one thread will be used."));
41  threads = 1;
42  }
43 #endif
44  return threads;
45 }
#define NULL
Definition: ccmath.h:32
void void void void G_fatal_error(const char *,...) __attribute__((format(printf
void G_warning(const char *,...) __attribute__((format(printf
void void G_verbose_message(const char *,...) __attribute__((format(printf
#define _(str)
Definition: glocale.h:10
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:19
Structure that stores option information.
Definition: gis.h:557
const char * key
Definition: gis.h:558
char * answer
Definition: gis.h:571