GRASS 8 Programmer's Manual
8.5.0(2026)-8d6ceba290
Toggle main menu visibility
Loading...
Searching...
No Matches
omp_threads.c
Go to the documentation of this file.
1
#include <stdio.h>
2
#include <stdlib.h>
3
#include <string.h>
4
5
#if defined(_OPENMP)
6
#include <omp.h>
7
#endif
8
9
#include <grass/gis.h>
10
#include <grass/glocale.h>
11
12
/*! \brief Set the number of threads for OpenMP
13
The intended usage is at the beginning of a C tool when parameters are
14
processed, namely the G_OPT_M_NPROCS standard option.
15
16
If \em nprocs is set to 0, default OpenMP internal logic is used.
17
If \em nprocs is a positive number, specified number of threads is used.
18
If \em nprocs is a negative number, then <em>maximum threads - number</em>
19
is used instead (e.g. to keep \em number of cores free for other use.
20
21
\param opt A nprocs Option struct to specify the number of threads
22
\return the number of threads set up for OpenMP parallel computing
23
24
\since version 8.5
25
*/
26
int
G_set_omp_num_threads
(
struct
Option *opt)
27
{
28
/* make sure Option is not null */
29
if
(opt ==
NULL
)
30
G_fatal_error
(_(
"Option is NULL."
));
31
else
if
(opt->key ==
NULL
)
32
G_fatal_error
(_(
"Option key is NULL."
));
33
34
int
threads = atoi(opt->answer);
35
#if defined(_OPENMP)
36
if
(threads == 0) {
37
threads = omp_get_max_threads();
38
}
39
else
{
40
int
num_logic_procs = omp_get_num_procs();
41
if
(threads < 1) {
42
threads += num_logic_procs;
43
threads = (threads < 1) ? 1 : threads;
44
}
45
omp_set_num_threads(threads);
46
}
47
G_verbose_message
(n_(
"One thread is set up for parallel computing."
,
48
"%d threads are set up for parallel computing."
,
49
threads),
50
threads);
51
#else
52
if
(!(threads == 0 || threads == 1)) {
53
G_warning
(_(
"GRASS is not compiled with OpenMP support, parallel "
54
"computation is disabled. Only one thread will be used."
));
55
}
56
threads = 1;
57
#endif
58
return
threads;
59
}
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
gis
omp_threads.c
Generated on
for GRASS 8 Programmer's Manual by
1.17.0