Skip to content

Commit

Permalink
lib/gis: Add a helper function to determine the number of threads for…
Browse files Browse the repository at this point in the history
… 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
cyliang368 authored Aug 5, 2024
1 parent f9f7482 commit e87f788
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 2 deletions.
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;
}
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;
}

0 comments on commit e87f788

Please sign in to comment.