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

Merged
merged 13 commits into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions include/grass/defs/gis.h
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,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_setup_threads(char *);
cyliang368 marked this conversation as resolved.
Show resolved Hide resolved

/* 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
44 changes: 44 additions & 0 deletions lib/gis/omp_threads.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#if defined(_OPENMP)
#include <omp.h>
#endif

#include <stdio.h>
#include <stdlib.h>
#include <grass/gis.h>
#include <grass/glocale.h>

/*! \brief A helper function to setup the number of threads for C modules
supported by OpenMP
cyliang368 marked this conversation as resolved.
Show resolved Hide resolved
\param nprocs the number of threads specified by the user
\return the number of threads set up for OpenMP parallel computing
*/

int G_setup_threads(char *nprocs)
{
int threads = atoi(nprocs);
#if defined(_OPENMP)
int max_cores = omp_get_num_procs();
if (threads > max_cores) {
G_warning(
_("The number of threads specified is greater than the "
"number of processors available (%d). The number of threads "
"will be set to %d."),
cyliang368 marked this conversation as resolved.
Show resolved Hide resolved
max_cores, max_cores);
threads = max_cores;
cyliang368 marked this conversation as resolved.
Show resolved Hide resolved
}
else if (threads < 1) {
threads += max_cores;
cyliang368 marked this conversation as resolved.
Show resolved Hide resolved
threads = (threads < 1) ? 1 : threads;
cyliang368 marked this conversation as resolved.
Show resolved Hide resolved
}
omp_set_num_threads(threads);
G_message(_("%d threads are set up for parallel computing."), threads);
cyliang368 marked this conversation as resolved.
Show resolved Hide resolved
#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