-
-
Notifications
You must be signed in to change notification settings - Fork 307
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
lib/gis: Add a helper function to determine the number of threads for…
… OpenMP (#3929) * determine the number of threads for OpenMP in opt parser * add OpenMP library paths * solve segfault when nprocs option is not specified in the command * only change nprocs for C module * update the description of the function, which does not return any value * add helper function, left parser unchanged * return threads rather than change the answer of nprocs * modify code based on review comments * remove threads > logic cores check, update based on review comments
- Loading branch information
1 parent
f9f7482
commit e87f788
Showing
3 changed files
with
51 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
#if defined(_OPENMP) | ||
#include <omp.h> | ||
#endif | ||
|
||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
#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. | ||
\param opt A nprocs Option struct to specify the number of threads | ||
\return the number of threads set up for OpenMP parallel computing | ||
*/ | ||
|
||
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) | ||
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(_("%d threads are set up for parallel computing."), | ||
threads); | ||
#else | ||
if (threads != 1) { | ||
G_warning(_("GRASS GIS is not compiled with OpenMP support, parallel " | ||
"computation is disabled. Only one thread will be used.")); | ||
threads = 1; | ||
} | ||
#endif | ||
return threads; | ||
} |