Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lib/gis: Add a helper function to determine the number of threads for OpenMP #3929

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
3 changes: 3 additions & 0 deletions include/grass/defs/gis.h
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,9 @@ int G_name_is_fully_qualified(const char *, char *, char *);
char *G_fully_qualified_name(const char *, const char *);
int G_unqualified_name(const char *, const char *, char *, char *);

/* omp_threads.c */
int G_set_omp_num_threads(struct Option *);

/* open.c */
int G_open_new(const char *, const char *);
int G_open_old(const char *, const char *, const char *);
Expand Down
5 changes: 3 additions & 2 deletions lib/gis/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ MODULE_TOPDIR = ../..

LIB = GIS

EXTRA_INC = $(ZLIBINCPATH) $(BZIP2INCPATH) $(ZSTDINCPATH) $(PTHREADINCPATH) $(REGEXINCPATH)
EXTRA_CFLAGS = -DGRASS_VERSION_DATE=\"'$(GRASS_VERSION_DATE)'\"
LIBES = $(OPENMP_LIBPATH) $(OPENMP_LIB)
EXTRA_INC = $(ZLIBINCPATH) $(BZIP2INCPATH) $(ZSTDINCPATH) $(PTHREADINCPATH) $(REGEXINCPATH) $(OPENMP_INCPATH)
EXTRA_CFLAGS = $(OPENMP_CFLAGS) -DGRASS_VERSION_DATE=\"'$(GRASS_VERSION_DATE)'\"

PROJSRC = ellipse.table ellipse.table.solar.system datum.table \
datumtransform.table FIPS.code state27 state83 projections
Expand Down
45 changes: 45 additions & 0 deletions lib/gis/omp_threads.c
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;
cyliang368 marked this conversation as resolved.
Show resolved Hide resolved
}
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;
cyliang368 marked this conversation as resolved.
Show resolved Hide resolved
}
#endif
return threads;
}
Loading