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

Feature 1926 gridstat openmp #1977

Merged
merged 30 commits into from
Jan 13, 2022
Merged
Show file tree
Hide file tree
Changes from 21 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
ed6934b
Feature 1926. Introduce OpenMP in GridStat.
mo-mglover Oct 18, 2021
86c1a50
Feature 1926. Add MET_OPENMP flag to automake setup.
mo-mglover Nov 4, 2021
b3a063a
Feature 1926. Indentation tweaks.
mo-mglover Nov 4, 2021
e0257c6
Feature 1926. Revert temporary C++17 temporary file code tweak.
mo-mglover Nov 5, 2021
7d81d41
Feature 1926. Add _OPENMP ifdef for omp.h.
mo-mglover Nov 18, 2021
3ff12fe
Feature 1926. Revert tweak to CXX optimisation level with MET_DEVELOP…
mo-mglover Nov 22, 2021
9a045e0
Per #1926, switch the OpenMP configuration option from being like MET…
JohnHalleyGotway Nov 22, 2021
4c176c4
Per #1926, just like --enable-grib2 and --enable-python, add a test p…
JohnHalleyGotway Nov 22, 2021
7043995
Feature #1926: Ensure OpenMP flag is passed to the linker. Revert to …
mo-mglover Nov 26, 2021
81c98b9
Feature #1926. Write thread count via logging, not cout.
mo-mglover Nov 30, 2021
1abb2be
Feature #1926: Add note about --enable-openmp in installation.rst
mo-mglover Nov 30, 2021
d6969c6
Per #1926, adding a call to Grid-Stat to run on 2 threads. This shoul…
JohnHalleyGotway Dec 2, 2021
2b37a69
Per #1926, updating GridStatConfig_rtma to switch width = 1 to 3. Thi…
JohnHalleyGotway Dec 2, 2021
4b2982f
Per #1926, tweak the openmp log message slightly.
JohnHalleyGotway Dec 3, 2021
3c1a5db
Per #1926, switch from manual --enable-openmp support in autoconf to …
JohnHalleyGotway Dec 3, 2021
b595de4
Per #1926, after switching to the AC_OPENMP() macro, these changes to…
JohnHalleyGotway Dec 3, 2021
359d823
Merge branch 'develop' into feature_1926_gridstat_openmp
JohnHalleyGotway Dec 3, 2021
ecaa653
Per #1926, since openmp support will now be compiled by default, swit…
JohnHalleyGotway Dec 3, 2021
9594505
Feature #1926. Use `default(none)` to force explicit `private` or `s…
mo-mglover Dec 6, 2021
e8d928b
Feature #1926: Default to single thread if OMP_NUM_THREADS is left un…
mo-mglover Dec 6, 2021
dd35aa4
Feature #1926: Add OpenMP initialisation to other tools calling fract…
mo-mglover Dec 6, 2021
e0d7840
Update met/src/basic/vx_util/handle_openmp.cc
mo-mglover Dec 16, 2021
78e4354
Update met/src/basic/vx_util/Makefile.am
mo-mglover Dec 16, 2021
b2a6c1e
Feature #1926: Lengthen OMP_NUM_THREADS debug message, plus user guid…
mo-mglover Dec 16, 2021
f39313d
Set up the Runtime Environment Variables Section and started adding i…
Dec 17, 2021
63b1fce
Updated documenation and fixed typos
Dec 21, 2021
3e89e56
removed openmp.rst in favor of having the information contained in a …
Dec 22, 2021
83688f5
Fixed typo
Dec 22, 2021
d73d537
Merge branch 'develop' into feature_1926_gridstat_openmp
JohnHalleyGotway Jan 12, 2022
821ca68
Merge branch 'develop' into feature_1926_gridstat_openmp
JohnHalleyGotway Jan 13, 2022
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
7 changes: 7 additions & 0 deletions met/configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ AC_CONFIG_HEADERS([config.h])

AM_INIT_AUTOMAKE([1.9 foreign])

# OpenMP

AC_OPENMP()

CPPFLAGS="${CPPFLAGS} ${OPENMP_CFLAGS}"
LDFLAGS="${LDFLAGS} ${OPENMP_CFLAGS}"

#
# Look for the NetCDF library
#
Expand Down
8 changes: 8 additions & 0 deletions met/docs/Users_Guide/installation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,14 @@ Enable compilation of the MODE-Graphics tool. Requires $MET_CAIRO and $MET_FREET

Disable use of BLOCK4 in the compilation. Use this if you have trouble using PrepBUFR files.

.. code-block:: none

--disable-openmp

Disable compilation of OpenMP directives within the code which allows some code
regions to benefit from thread-parallel execution. Runtime environment variable
:code:`OMP_NUM_THREADS` controls the number of threads.

Run the configure script with the **-help** argument to see the full list of configuration options.

Make Targets
Expand Down
1 change: 1 addition & 0 deletions met/src/basic/vx_util/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ libvx_util_a_SOURCES = ascii_table.cc ascii_table.h \
GridOffset.h GridOffset.cc \
observation.h observation.cc \
stat_column_defs.h \
handle_openmp.h handle_openmp.cc \
mo-mglover marked this conversation as resolved.
Show resolved Hide resolved
RectangularTemplate.h RectangularTemplate.cc $(OPT_PYTHON_SOURCES)

libvx_util_a_CPPFLAGS = ${MET_CPPFLAGS}
Expand Down
170 changes: 93 additions & 77 deletions met/src/basic/vx_util/data_plane_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ using namespace std;
#include <string.h>
#include <unistd.h>

#ifdef _OPENMP
#include "omp.h"
#endif

#include "data_plane_util.h"
#include "interp_util.h"
#include "two_to_one.h"
Expand Down Expand Up @@ -247,83 +251,95 @@ void fractional_coverage(const DataPlane &dp, DataPlane &frac_dp,
}
}

// Build the grid template
GridTemplateFactory gtf;
GridTemplate* gt = gtf.buildGT(shape, width, wrap_lon);

mlog << Debug(3)
<< "Computing fractional coverage field using the "
<< t.get_str() << " threshold and the "
<< interpmthd_to_string(InterpMthd_Nbrhd) << "(" << gt->size()
<< ") " << gt->getClassName() << " interpolation method.\n";

// Initialize the fractional coverage field
frac_dp = dp;
frac_dp.set_constant(bad_data_double);

// Compute the fractional coverage meeting the threshold criteria
for(x=0; x<dp.nx(); x++) {
for(y=0; y<dp.ny(); y++) {

// For a new column, reset the grid template and counts.
if(y == 0) {

// Initialize counts
n_vld = n_thr = 0;

// Sum all the points
for(gp = gt->getFirstInGrid(x, y, dp.nx(), dp.ny());
gp != NULL;
gp = gt->getNextInGrid()) {
if(is_bad_data(v = dp.get(gp->x, gp->y))) continue;
n_vld++;
if(t.check(v,
(use_climo ? cmn->get(gp->x, gp->y) : bad),
(use_climo ? csd->get(gp->x, gp->y) : bad))) n_thr++;
}
}
// Subtract off the bottom edge, shift up, and add the top.
else {

// Subtract points from the the bottom edge
for(gp = gt->getFirstInBotEdge();
gp != NULL;
gp = gt->getNextInBotEdge()) {
if(is_bad_data(v = dp.get(gp->x, gp->y))) continue;
n_vld--;
if(t.check(v,
(use_climo ? cmn->get(gp->x, gp->y) : bad),
(use_climo ? csd->get(gp->x, gp->y) : bad))) n_thr--;
}

// Increment Y
gt->incBaseY(1);

// Add points from the the top edge
for(gp = gt->getFirstInTopEdge();
gp != NULL;
gp = gt->getNextInTopEdge()) {
if(is_bad_data(v = dp.get(gp->x, gp->y))) continue;
n_vld++;
if(t.check(v,
(use_climo ? cmn->get(gp->x, gp->y) : bad),
(use_climo ? csd->get(gp->x, gp->y) : bad))) n_thr++;
}
}

// Check for enough valid data and compute fractional coverage
if((double)(n_vld)/gt->size() >= vld_t && n_vld != 0) {
frac_dp.set((double) n_thr/n_vld, x, y);
}

} // end for y

// Increment X
if(x < (dp.nx() - 1)) gt->incBaseX(1);

} // end for x

delete gt;
#pragma omp parallel default(none) \
shared(mlog, dp, frac_dp, width, wrap_lon, t) \
shared(use_climo, cmn, csd, vld_t, bad) \
private(x, y, n_vld, n_thr, gp, v)
{

// Build the grid template
GridTemplateFactory gtf;
GridTemplate* gt = gtf.buildGT(shape, width, wrap_lon);

#pragma omp single
{
mlog << Debug(3)
<< "Computing fractional coverage field using the "
<< t.get_str() << " threshold and the "
<< interpmthd_to_string(InterpMthd_Nbrhd) << "(" << gt->size()
<< ") " << gt->getClassName() << " interpolation method.\n";

// Initialize the fractional coverage field
frac_dp = dp;
frac_dp.set_constant(bad_data_double);
}

// Compute the fractional coverage meeting the threshold criteria
#pragma omp for schedule (static)
for(x=0; x<dp.nx(); x++) {
for(y=0; y<dp.ny(); y++) {

// For a new column, reset the grid template and counts.
if(y == 0) {

// Initialize counts
n_vld = n_thr = 0;

// Sum all the points
for(gp = gt->getFirstInGrid(x, y, dp.nx(), dp.ny());
gp != NULL;
gp = gt->getNextInGrid()) {
if(is_bad_data(v = dp.get(gp->x, gp->y))) continue;
n_vld++;
if(t.check(v,
(use_climo ? cmn->get(gp->x, gp->y) : bad),
(use_climo ? csd->get(gp->x, gp->y) : bad))) n_thr++;
}
}
// Subtract off the bottom edge, shift up, and add the top.
else {

// Subtract points from the the bottom edge
for(gp = gt->getFirstInBotEdge();
gp != NULL;
gp = gt->getNextInBotEdge()) {
if(is_bad_data(v = dp.get(gp->x, gp->y))) continue;
n_vld--;
if(t.check(v,
(use_climo ? cmn->get(gp->x, gp->y) : bad),
(use_climo ? csd->get(gp->x, gp->y) : bad))) n_thr--;
}

// Increment Y
gt->incBaseY(1);

// Add points from the the top edge
for(gp = gt->getFirstInTopEdge();
gp != NULL;
gp = gt->getNextInTopEdge()) {
if(is_bad_data(v = dp.get(gp->x, gp->y))) continue;
n_vld++;
if(t.check(v,
(use_climo ? cmn->get(gp->x, gp->y) : bad),
(use_climo ? csd->get(gp->x, gp->y) : bad))) n_thr++;
}
}

// Check for enough valid data and compute fractional coverage
if((double)(n_vld)/gt->size() >= vld_t && n_vld != 0) {
frac_dp.set((double) n_thr/n_vld, x, y);
}

} // end for y

// Increment X
if(x < (dp.nx() - 1)) gt->incBaseX(1);

} // end for x

delete gt;

} // End of omp parallel

return;
}
Expand Down
51 changes: 51 additions & 0 deletions met/src/basic/vx_util/handle_openmp.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// *=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
// ** Copyright UCAR (c) 1992 - 2021
// ** University Corporation for Atmospheric Research (UCAR)
// ** National Center for Atmospheric Research (NCAR)
// ** Research Applications Lab (RAL)
// ** P.O.Box 3000, Boulder, Colorado, 80307-3000, USA
// *=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*

///////////////////////////////////////////////////////////////////////////////

#include <cstdlib>

#ifdef _OPENMP
#include "omp.h"
#endif

#include "vx_log.h"
#include "handle_openmp.h"

///////////////////////////////////////////////////////////////////////////////

void init_openmp() {

#ifdef _OPENMP

// If OMP_NUM_THREADS was not set, use the OpenMP API to set the thread count
// to 1 thread only.
const char* env_omp_num_threads = std::getenv("OMP_NUM_THREADS");
if (!env_omp_num_threads) {
mlog << Debug(2) << "OMP_NUM_THREADS is not set."
<< " Defaulting to 1 thread. \n";
mo-mglover marked this conversation as resolved.
Show resolved Hide resolved
omp_set_num_threads(1);
}

#pragma omp parallel
#pragma omp single
{
mlog << Debug(2) << "OpenMP running on "
<< omp_get_num_threads() << " thread(s).\n";
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
<< omp_get_num_threads() << " thread(s).\n";
<< omp_get_num_threads() << " thread(s).\n";

Just lining code up.

}

#else /* _OPENMP */

mlog << Debug(2) << "OpenMP disabled.\n";

#endif /* _OPENMP */

}

///////////////////////////////////////////////////////////////////////////////

24 changes: 24 additions & 0 deletions met/src/basic/vx_util/handle_openmp.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// *=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
// ** Copyright UCAR (c) 1992 - 2021
// ** University Corporation for Atmospheric Research (UCAR)
// ** National Center for Atmospheric Research (NCAR)
// ** Research Applications Lab (RAL)
// ** P.O.Box 3000, Boulder, Colorado, 80307-3000, USA
// *=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*

///////////////////////////////////////////////////////////////////////////////

#ifndef __HANDLE_OPENMP_H___
#define __HANDLE_OPENMP_H___

///////////////////////////////////////////////////////////////////////////////

void init_openmp();

///////////////////////////////////////////////////////////////////////////////

#endif /* __HANDLE_OPENMP_H__ */

///////////////////////////////////////////////////////////////////////////////


5 changes: 5 additions & 0 deletions met/src/tools/core/ensemble_stat/ensemble_stat.cc
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ using namespace std;
#include "nc_obs_util.h"
#include "nc_point_obs_in.h"

#include "handle_openmp.h"

////////////////////////////////////////////////////////////////////////

static void process_command_line (int, char **);
Expand Down Expand Up @@ -171,6 +173,9 @@ static void set_compress(const StringArray &);

int main(int argc, char *argv[]) {

// Set up OpenMP (if enabled)
init_openmp();

// Set handler to be called for memory allocation error
set_new_handler(oom);

Expand Down
5 changes: 5 additions & 0 deletions met/src/tools/core/grid_stat/grid_stat.cc
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ using namespace std;
#include <sys/types.h>
#include <unistd.h>

#include "handle_openmp.h"

#include "grid_stat.h"

#include "vx_statistics.h"
Expand Down Expand Up @@ -185,6 +187,9 @@ static bool read_data_plane(VarInfo* info, DataPlane& dp, Met2dDataFile* mtddf,

int main(int argc, char *argv[]) {

// Set up OpenMP (if enabled)
init_openmp();

// Set handler to be called for memory allocation error
set_new_handler(oom);

Expand Down
5 changes: 5 additions & 0 deletions met/src/tools/other/gen_ens_prod/gen_ens_prod.cc
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ using namespace std;
#include "nc_obs_util.h"
#include "nc_point_obs_in.h"

#include "handle_openmp.h"

////////////////////////////////////////////////////////////////////////

static void process_command_line(int, char **);
Expand Down Expand Up @@ -81,6 +83,9 @@ static void set_ctrl_file (const StringArray &);

int main(int argc, char *argv[]) {

// Set up OpenMP (if enabled)
init_openmp();

// Set handler to be called for memory allocation error
set_new_handler(oom);

Expand Down
2 changes: 1 addition & 1 deletion test/config/GridStatConfig_rtma
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ interp = {
type = [
{ method = NEAREST; width = 1; },
{ method = UW_MEAN; width = 3; },
{ method = MIN; width = 1; },
{ method = MIN; width = 3; },
{ method = MAX; width = 3; }
];
}
Expand Down
26 changes: 26 additions & 0 deletions test/xml/unit_grid_stat.xml
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,32 @@
</output>
</test>

<test name="grid_stat_GRIB2_NAM_RTMA_NP2">
<exec>&MET_BIN;/grid_stat</exec>
<env>
<pair><name>OMP_NUM_THREADS</name> <value>2</value></pair>
<pair><name>OUTPUT_PREFIX</name> <value>GRIB2_NAM_RTMA_NP2</value></pair>
</env>
<param> \
&DATA_DIR_MODEL;/grib2/nam/nam_2012040900_F012_gRtma.grib2 \
&DATA_DIR_OBS;/rtma/rtma_2012051712_F000.grib2 \
&CONFIG_DIR;/GridStatConfig_rtma \
-outdir &OUTPUT_DIR;/grid_stat -v 1
</param>
<output>
<stat>&OUTPUT_DIR;/grid_stat/grid_stat_GRIB2_NAM_RTMA_NP2_120000L_20120409_120000V.stat</stat>
<stat>&OUTPUT_DIR;/grid_stat/grid_stat_GRIB2_NAM_RTMA_NP2_120000L_20120409_120000V_fho.txt</stat>
<stat>&OUTPUT_DIR;/grid_stat/grid_stat_GRIB2_NAM_RTMA_NP2_120000L_20120409_120000V_ctc.txt</stat>
<stat>&OUTPUT_DIR;/grid_stat/grid_stat_GRIB2_NAM_RTMA_NP2_120000L_20120409_120000V_cts.txt</stat>
<stat>&OUTPUT_DIR;/grid_stat/grid_stat_GRIB2_NAM_RTMA_NP2_120000L_20120409_120000V_cnt.txt</stat>
<stat>&OUTPUT_DIR;/grid_stat/grid_stat_GRIB2_NAM_RTMA_NP2_120000L_20120409_120000V_sl1l2.txt</stat>
<stat>&OUTPUT_DIR;/grid_stat/grid_stat_GRIB2_NAM_RTMA_NP2_120000L_20120409_120000V_nbrctc.txt</stat>
<stat>&OUTPUT_DIR;/grid_stat/grid_stat_GRIB2_NAM_RTMA_NP2_120000L_20120409_120000V_nbrcts.txt</stat>
<stat>&OUTPUT_DIR;/grid_stat/grid_stat_GRIB2_NAM_RTMA_NP2_120000L_20120409_120000V_nbrcnt.txt</stat>
<grid_nc>&OUTPUT_DIR;/grid_stat/grid_stat_GRIB2_NAM_RTMA_NP2_120000L_20120409_120000V_pairs.nc</grid_nc>
</output>
</test>

<test name="grid_stat_GRIB1_NAM_STAGE4">
<exec>&MET_BIN;/grid_stat</exec>
<env>
Expand Down