Skip to content

Commit 20cbbff

Browse files
authored
Use all cores by default in grdfilter (#8523)
* Let x option synopsis be printed in grdfilter independently of having OMP or not. * Let all 3 modules built with Glib use max number of cores by default like the OMP case. * Do not use multi-threads with -Ff (custom filter). It's broken. * Use API->n_cores instead of gmtlib_get_num_processors() to see if it pleases Mac * Do not use all cores by default in potential modules. Some tests are failing. Integrate the ideas of #8319 * Remove mention to -x option (not in current surface module). Variable filter width (grdfilter) does not support -x
1 parent f9064b2 commit 20cbbff

File tree

8 files changed

+37
-15
lines changed

8 files changed

+37
-15
lines changed

doc/rst/source/explain_core.rst_

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
**-x**\ [[-]\ *n*] :ref:`(more ...) <core_full>`
2-
Limit number of cores used in multi-threaded algorithms (OpenMP required).
2+
Limit number of cores used in multi-threaded algorithms. Multi-threaded behavior is enabled by default.
3+
That covers the modules that implement the OpenMP API (required at compiling stage) and GThreads (Glib)
4+
for the grdfilter module.

src/gmt_dev.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ struct GMT_CTRL; /* forward declaration of GMT_CTRL */
154154
#include "gmt_nan.h" /* Machine-dependent macros for making and testing NaNs */
155155
#include "gmt_error.h" /* Only contains error codes */
156156
#include "gmt_synopsis.h" /* Only contains macros for synopsis lines */
157+
#include "gmt_glib.h" /* Make the GMT_xg_OPT define visible, even if HAVE_GLIB_GTHREAD is not defined */
157158
#include "gmt_version.h" /* Only contains the current GMT version number */
158159
#include "gmt_project.h" /* Define GMT->current.proj and GMT->current.map.frame structures */
159160
#include "gmt_grd.h" /* Define grd file header structure */

src/gmt_glib.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
These are used only GLIB based multi-threading */
3131

3232
#ifndef GMT_GLIB_H
33+
#define GMT_GLIB_H
3334

3435
#ifdef HAVE_GLIB_GTHREAD
3536
#include <glib.h>

src/gmt_init.c

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2607,8 +2607,8 @@ GMT_LOCAL int gmtinit_parse_U_option (struct GMT_CTRL *GMT, char *item) {
26072607
}
26082608

26092609
/*! -x[[-]<ncores>] */
2610+
GMT_LOCAL int gmtinit_parse_x_option (struct GMT_CTRL *GMT, char *arg) { /* Only effective if MP is enabled */
26102611
#ifdef GMT_MP_ENABLED
2611-
GMT_LOCAL int gmtinit_parse_x_option (struct GMT_CTRL *GMT, char *arg) {
26122612
GMT->common.x.active = true;
26132613
if (!arg) return (GMT_PARSE_ERROR); /* -x requires a non-NULL argument */
26142614
if (arg[0] == '\0') /* Use all processors */
@@ -2622,9 +2622,9 @@ GMT_LOCAL int gmtinit_parse_x_option (struct GMT_CTRL *GMT, char *arg) {
26222622
GMT->common.x.n_threads = MAX(gmtlib_get_num_processors() - abs (GMT->common.x.n_threads), 1); /* Max-n but at least one */
26232623
if (GMT->current.setting.max_cores) /* Limit to max core defaults setting */
26242624
GMT->common.x.n_threads = GMT->current.setting.max_cores;
2625+
#endif
26252626
return (GMT_NOERROR);
26262627
}
2627-
#endif
26282628

26292629
/*! . */
26302630
GMT_LOCAL int gmtinit_parse_colon_option (struct GMT_CTRL *GMT, char *item) {
@@ -8124,9 +8124,9 @@ void gmtlib_explain_options (struct GMT_CTRL *GMT, char *options) {
81248124

81258125
#if defined(GMT_MP_ENABLED)
81268126
case 'y': /* Number of threads (reassigned from -x in GMT_Option) */
8127-
if (strlen (GMT_x_OPT) > 1) { /* Only print this if it is in fact available */
8127+
if (strlen(GMT_x_OPT) > 1 || strlen(GMT_xg_OPT) > 1) { /* Only print this if it is in fact available */
81288128
cores = gmtlib_get_num_processors();
8129-
GMT_Usage (API, 1, "\n%s", GMT_x_OPT);
8129+
(strlen(GMT_x_OPT) > 1) ? GMT_Usage(API, 1, "\n%s", GMT_x_OPT) : GMT_Usage(API, 1, "\n%s", GMT_xg_OPT);
81308130
GMT_Usage (API, -2, "Limit the number of cores used in multi-threaded algorithms [Default uses all %d cores]. "
81318131
"If <n> is negative then we select (%d - <n>) cores (or at least 1).", cores, cores);
81328132
}
@@ -9151,9 +9151,12 @@ int gmt_default_error (struct GMT_CTRL *GMT, char option) {
91519151
case 's': error += GMT->common.s.active == false; break;
91529152
case 't': error += GMT->common.t.active == false; break;
91539153
case 'w': error += GMT->common.w.active == false; break;
9154-
#if defined(GMT_MP_ENABLED)
9155-
case 'x': error += GMT->common.x.active == false; break;
9154+
case 'x': error += GMT->common.x.active == false;
9155+
#if !defined(GMT_MP_ENABLED)
9156+
error --;
9157+
GMT_Report (GMT->parent, GMT_MSG_INFORMATION, "Option -x: GMT is not compiled with parallel support. Only one core is used\n");
91569158
#endif
9159+
break;
91579160
case ':': error += GMT->common.colon.active == false; break;
91589161

91599162
default:
@@ -18792,12 +18795,13 @@ int gmt_parse_common_options (struct GMT_CTRL *GMT, char *list, char option, cha
1879218795
error += gmt_M_more_than_once (GMT, GMT->common.w.active) || gmtinit_parse_w_option (GMT, item);
1879318796
break;
1879418797

18795-
#ifdef GMT_MP_ENABLED
1879618798
case 'x':
1879718799
error += (gmt_M_more_than_once (GMT, GMT->common.x.active) || gmtinit_parse_x_option (GMT, item));
1879818800
GMT->common.x.active = true;
18799-
break;
18801+
#if !defined(GMT_MP_ENABLED)
18802+
GMT_Report (GMT->parent, GMT_MSG_WARNING, "Option -x: GMT is not compiled with parallel support. Only one core is used\n");
1880018803
#endif
18804+
break;
1880118805

1880218806
case ':':
1880318807
error += (gmt_M_more_than_once (GMT, GMT->common.colon.active) || gmtinit_parse_colon_option (GMT, item));

src/grdfilter.c

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ Use option -x to set the number of threads. e.g. -x2, -x4, ... or -xa to use all
4747

4848
#include "gmt_dev.h"
4949
#include "longopt/grdfilter_inc.h"
50-
#include "gmt_glib.h"
50+
//#include "gmt_glib.h" /* Now included in gmt_dev.h */
5151

5252
#define THIS_MODULE_CLASSIC_NAME "grdfilter"
5353
#define THIS_MODULE_MODERN_NAME "grdfilter"
@@ -828,7 +828,7 @@ static int parse (struct GMT_CTRL *GMT, struct GRDFILTER_CTRL *Ctrl, struct GMT_
828828
Ctrl->F.rect = true;
829829
}
830830
else
831-
Ctrl->F.width = grdfilter_get_filter_width (API, Ctrl, &txt[1]);
831+
Ctrl->F.width = grdfilter_get_filter_width (API, Ctrl, &txt[1]); /* This has an undocumented option of reading a grid */
832832
if (Ctrl->F.width < 0.0) { /* Old-style specification for high-pass filtering */
833833
if (gmt_M_compat_check (GMT, 5)) {
834834
GMT_Report (API, GMT_MSG_COMPAT,
@@ -893,6 +893,20 @@ static int parse (struct GMT_CTRL *GMT, struct GRDFILTER_CTRL *Ctrl, struct GMT_
893893
}
894894
}
895895

896+
#ifdef HAVE_GLIB_GTHREAD
897+
/* Make the default equal to the OMP case where we use all threads if not stated otherwise. */
898+
if (Ctrl->F.varwidth) { /* Variable filter width. This option is currently undocumented. */
899+
if (GMT->common.x.active)
900+
GMT_Report (GMT->parent, GMT_MSG_WARNING, "Sorry, variable filter width does not support multiple threads. Reset to 1.\n" );
901+
GMT->common.x.n_threads = 1;
902+
GMT->common.x.active = true;
903+
}
904+
if (!GMT->common.x.active) {
905+
GMT->common.x.n_threads = API->n_cores;
906+
GMT_Report (GMT->parent, GMT_MSG_INFORMATION, "Enable all available threads (up to %d)\n", API->n_cores);
907+
}
908+
#endif
909+
896910
n_errors += gmt_M_check_condition (GMT, !Ctrl->G.active, "Option -G: Must specify output file\n");
897911
n_errors += gmt_M_check_condition (GMT, !Ctrl->In.file, "Must specify input file\n");
898912
n_errors += gmt_M_check_condition (GMT, !Ctrl->D.active, "Option -D: Choose from p or 0-5\n");

src/potential/gmtgravmag3d.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ static int usage (struct GMTAPI_CTRL *API, int level) {
252252
return (GMT_MODULE_USAGE);
253253
}
254254

255-
static int parse (struct GMT_CTRL *GMT, struct GMTGRAVMAG3D_CTRL *Ctrl, struct GMT_OPTION *options) {
255+
static int parse(struct GMT_CTRL *GMT, struct GMTGRAVMAG3D_CTRL *Ctrl, struct GMT_OPTION *options) {
256256

257257
/* This parses the options provided to gmtgravmag3d and sets parameters in Ctrl.
258258
* Note Ctrl has already been initialized and non-zero default values set.

src/potential/grdgravmag3d.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ static int usage (struct GMTAPI_CTRL *API, int level) {
262262
return (GMT_MODULE_USAGE);
263263
}
264264

265-
static int parse (struct GMT_CTRL *GMT, struct GRDGRAVMAG3D_CTRL *Ctrl, struct GMT_OPTION *options) {
265+
static int parse(struct GMT_CTRL *GMT, struct GRDGRAVMAG3D_CTRL *Ctrl, struct GMT_OPTION *options) {
266266

267267
/* This parses the options provided to grdgravmag3d and sets parameters in Ctrl.
268268
* Note Ctrl has already been initialized and non-zero default values set.

src/surface.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
#define THIS_MODULE_PURPOSE "Grid table data using adjustable tension continuous curvature splines"
5858
#define THIS_MODULE_KEYS "<D{,DD(=,LG(,GG}"
5959
#define THIS_MODULE_NEEDS "R"
60-
#define THIS_MODULE_OPTIONS "-:RVabdefhiqrw" GMT_ADD_x_OPT GMT_OPT("FH")
60+
#define THIS_MODULE_OPTIONS "-:RVabdefhiqrw" GMT_OPT("FH")
6161

6262
struct SURFACE_CTRL {
6363
struct SURFACE_A { /* -A<aspect_ratio> */
@@ -1790,7 +1790,7 @@ static int usage (struct GMTAPI_CTRL *API, int level) {
17901790
GMT_Usage (API, -2, "Change over-relaxation parameter [Default = %g]. Use a value "
17911791
"between 1 and 2. Larger number accelerates convergence but can be unstable. "
17921792
"Use 1 if you want to be sure to have (slow) stable convergence.", SURFACE_OVERRELAXATION);
1793-
GMT_Option (API, "a,bi3,di,e,f,h,i,qi,r,w,x,:,.");
1793+
GMT_Option (API, "a,bi3,di,e,f,h,i,qi,r,w,:,.");
17941794
if (gmt_M_showusage (API)) GMT_Usage (API, -2, "Note: Geographic data with 360-degree range use periodic boundary condition in longitude. "
17951795
"For additional details, see Smith & Wessel, Geophysics, 55, 293-305, 1990.");
17961796

0 commit comments

Comments
 (0)