From: Szilard Pall Date: Fri, 15 Jun 2012 15:03:41 +0000 (+0200) Subject: introduce gmx_omp wrapper for the OpenMP API X-Git-Url: http://biod.pnpi.spb.ru/gitweb/?a=commitdiff_plain;h=697bcdcaf414f8800727ce1f3b67b11f290fdc6c;p=alexxy%2Fgromacs.git introduce gmx_omp wrapper for the OpenMP API The gmx_omp_* OpenMP API wrappers should be used instead of directly calling OpenMP functions. Therefore, gmx_omp.h should be included instead of omp.h so OpenMP API functions don't need #ifdef-ing. Additionally, moved GMX_OPENMP from command-line define to config.h. Change-Id: If30f433946fef908c26a29f1bff3671580e90629 --- diff --git a/CMakeLists.txt b/CMakeLists.txt index f1ed2488e2..9760d3a83a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -185,7 +185,6 @@ if(GMX_OPENMP) if(OPENMP_FOUND) set(GROMACS_C_FLAGS "${OpenMP_C_FLAGS} ${GROMACS_C_FLAGS}") set(GROMACS_CXX_FLAGS "${OpenMP_CXX_FLAGS} ${GROMACS_CXX_FLAGS}") - add_definitions(-DGMX_OPENMP) else(OPENMP_FOUND) message(WARNING "Compiler not supporting OpenMP. This might hurt your performance a lot, " diff --git a/include/gmx_omp.h b/include/gmx_omp.h new file mode 100644 index 0000000000..3fe53b00a7 --- /dev/null +++ b/include/gmx_omp.h @@ -0,0 +1,48 @@ +/* -*- mode: c; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; c-file-style: "stroustrup"; -*- + * + * + * This source code is part of + * + * G R O M A C S + * + * GROningen MAchine for Chemical Simulations + * + * Written by the Gromacs development team under coordination of + * David van der Spoel, Berk Hess, and Erik Lindahl. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * To help us fund GROMACS development, we humbly ask that you cite + * the research papers on the package. Check out http://www.gromacs.org + * + * And Hey: + * GROup of MAchos and Cynical Suckers + */ + +#ifndef GMX_OMP_H +#define GMX_OMP_H + +/* This module defines wrappers for OpenMP API functions and enables compiling + * code even when OpenMP is turned off in the build system. + * Therfore, OpenMP API functions should always be used through these wrappers + * and omp.h should never be directly included. Instead, this header should be + * used whnever OpenMP API functions are needed. + */ + +/*! Sets the number of threads in subsequent parallel regions, unless overridden + * by a num_threads clause. Acts as a wrapper for omp_get_max_threads(void). */ +int gmx_omp_get_max_threads(void); + +/*! Returns the thread number of the thread executing within its thread team. + * Acts as a warpper for omp_get_thread_num(void). */ +int gmx_omp_get_thread_num(void); + +/*! Returns an integer that is equal to or greater than the number of threads + * that would be available if a parallel region without num_threads were + * defined at that point in the code. Acts as a wapepr for omp_set_num_threads(void). */ +void gmx_omp_set_num_threads(int num_threads); + +#endif /* GMX_OMP_H */ diff --git a/src/config.h.cmakein b/src/config.h.cmakein index 0472b8412d..81fd750a99 100644 --- a/src/config.h.cmakein +++ b/src/config.h.cmakein @@ -131,9 +131,12 @@ (MPI or thread_mpi) */ #cmakedefine GMX_MPI -/* Use threads for parallelization */ +/* Use threads_mpi for parallelization */ #cmakedefine GMX_THREAD_MPI +/* Use OpenMP multithreading */ +#cmakedefine GMX_OPENMP + /* Use old threading (domain decomp force calc) code */ #cmakedefine GMX_THREAD_SHM_FDECOMP diff --git a/src/gmxlib/gmx_omp.c b/src/gmxlib/gmx_omp.c new file mode 100644 index 0000000000..09b2a192e8 --- /dev/null +++ b/src/gmxlib/gmx_omp.c @@ -0,0 +1,61 @@ +/* -*- mode: c; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; c-file-style: "stroustrup"; -*- + * + * + * This source code is part of + * + * G R O M A C S + * + * GROningen MAchine for Chemical Simulations + * + * Written by the Gromacs development team under coordination of + * David van der Spoel, Berk Hess, and Erik Lindahl. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * To help us fund GROMACS development, we humbly ask that you cite + * the research papers on the package. Check out http://www.gromacs.org + * + * And Hey: + * Gnomes, ROck Monsters And Chili Sauce + */ + +#ifdef HAVE_CONFIG +#include "config.h" +#endif + +#ifdef GMX_OPENMP +#include +#endif + +#include "gmx_omp.h" + +int gmx_omp_get_max_threads(void) +{ +#ifdef GMX_OPENMP + return omp_get_max_threads(); +#else + return 1; +#endif +} + + +int gmx_omp_get_thread_num(void) +{ +#ifdef GMX_OPENMP + return omp_get_thread_num(); +#else + return 0; +#endif +} + +void gmx_omp_set_num_threads(int num_threads) +{ +#ifdef GMX_OPENMP + omp_set_num_threads(num_threads); +#else + return; +#endif +} diff --git a/src/kernel/runner.c b/src/kernel/runner.c index 1cb9a4bbca..87e2568e1e 100644 --- a/src/kernel/runner.c +++ b/src/kernel/runner.c @@ -73,6 +73,8 @@ #include "membed.h" #include "md_openmm.h" +#include "gmx_omp.h" + #ifdef GMX_LIB_MPI #include #endif @@ -88,10 +90,6 @@ #include "md_openmm.h" #endif -#ifdef GMX_OPENMP -#include -#endif - typedef struct { gmx_integrator_t *func; @@ -829,7 +827,7 @@ int mdrunner(int nthreads_requested, FILE *fplog,t_commrec *cr,int nfile, { cpu_set_t mask; CPU_ZERO(&mask); - core+=omp_get_thread_num(); + core+=gmx_omp_get_thread_num(); CPU_SET(core,&mask); sched_setaffinity((pid_t) syscall (SYS_gettid),sizeof(cpu_set_t),&mask); } diff --git a/src/mdlib/fft5d.c b/src/mdlib/fft5d.c index 1337c11195..c606b6ad67 100644 --- a/src/mdlib/fft5d.c +++ b/src/mdlib/fft5d.c @@ -58,10 +58,11 @@ #endif #ifdef GMX_OPENMP +/* TODO: Do we still need this? Are we still planning ot use fftw + OpenMP? */ #define FFT5D_THREADS #endif #ifdef FFT5D_THREADS -#include +#include "gmx_omp.h" /* requires fftw compiled with openmp */ /* #define FFT5D_FFTW_THREADS (now set by cmake) */ #endif diff --git a/src/mdlib/pme.c b/src/mdlib/pme.c index 55e4270397..fac7dbb962 100644 --- a/src/mdlib/pme.c +++ b/src/mdlib/pme.c @@ -67,9 +67,7 @@ #include "tmpi.h" #endif -#ifdef GMX_OPENMP -#include -#endif +#include "gmx_omp.h" #include #include diff --git a/src/tools/geminate.c b/src/tools/geminate.c index 0423d7028b..672422932c 100644 --- a/src/tools/geminate.c +++ b/src/tools/geminate.c @@ -52,13 +52,8 @@ #include "smalloc.h" #include "vec.h" #include "geminate.h" +#include "gmx_omp.h" -#ifdef DOUSEOPENMP -#define HAVE_OPENMP -#endif -#ifdef HAVE_OPENMP -#include -#endif /* The first few sections of this file contain functions that were adopted, * and to some extent modified, by Erik Marklund (erikm[aT]xray.bmc.uu.se, @@ -473,13 +468,11 @@ static double eq10v2(double theoryCt[], double time[], int manytimes, part3 = gem_cxmul(gamma, gem_cxmul(gem_cxadd(alpha, beta) , gem_cxsub(alpha, beta))); /* 3(1+2)(1-2) */ part4 = gem_cxmul(gem_cxsub(gamma, alpha), gem_cxmul(gem_cxsub(alpha, beta), gem_cxsub(beta, gamma))); /* (3-1)(1-2)(2-3) */ -#ifdef HAVE_OPENMP #pragma omp parallel for \ private(i, tsqrt, oma, omb, omc, c1, c2, c3, c4), \ reduction(+:sumimaginary), \ default(shared), \ schedule(guided) -#endif for (i=0; ictTheory, nFitPoints); /* Removing a bunch of points from the log-part. */ -#ifdef HAVE_OPENMP #pragma omp parallel for schedule(dynamic) \ firstprivate(nData, ctTheory, y, nFitPoints) \ private (i, iLog, r) \ reduction(+:residual2) \ default(shared) -#endif for(i=0; ilogtime[i]; @@ -667,8 +658,8 @@ extern real fitGemRecomb(double *ct, double *time, double **ctFit, #ifdef HAVE_LIBGSL #ifdef HAVE_OPENMP - nThreads = omp_get_num_procs(); - omp_set_num_threads(nThreads); + nThreads = gmx_omp_get_max_threads(); + gmx_omp_set_num_threads(nThreads); fprintf(stdout, "We will be using %i threads.\n", nThreads); #endif diff --git a/src/tools/gmx_hbond.c b/src/tools/gmx_hbond.c index 3193e67588..5981ffe7af 100644 --- a/src/tools/gmx_hbond.c +++ b/src/tools/gmx_hbond.c @@ -2332,10 +2332,10 @@ static void do_hbac(const char *fn,t_hbdata *hb, /* #if (_OPENMP >= 200805) /\* =====================\ *\/ */ /* nThreads = min((nThreads <= 0) ? INT_MAX : nThreads, omp_get_thread_limit()); */ /* #else */ - nThreads = min((nThreads <= 0) ? INT_MAX : nThreads, omp_get_num_procs()); + nThreads = min((nThreads <= 0) ? INT_MAX : nThreads, gmx_omp_get_num_procs()); /* #endif /\* _OPENMP >= 200805 ====================/ *\/ */ - omp_set_num_threads(nThreads); + gmx_omp_set_num_threads(nThreads); snew(dondata, nThreads); for (i=0; imaxhydro); snew(g,hb->maxhydro); mMax = INT_MIN; @@ -3521,9 +3521,9 @@ int gmx_hbond(int argc,char *argv[]) /* #if (_OPENMP > 200805) */ /* actual_nThreads = min((nThreads <= 0) ? INT_MAX : nThreads, omp_get_thread_limit()); */ /* #else */ - actual_nThreads = min((nThreads <= 0) ? INT_MAX : nThreads, omp_get_num_procs()); + actual_nThreads = min((nThreads <= 0) ? INT_MAX : nThreads, gmx_omp_get_num_procs()); /* #endif */ - omp_set_num_threads(actual_nThreads); + gmx_omp_set_num_threads(actual_nThreads); printf("Frame loop parallelized with OpenMP using %i threads.\n", actual_nThreads); fflush(stdout); } @@ -3591,7 +3591,7 @@ int gmx_hbond(int argc,char *argv[]) bGem, oenv, fnm, fpnhb, trrStatus, natoms, \ status, nabin, nrbin, adist, rdist, debug) { /* Start of parallel region */ - threadNr = omp_get_thread_num(); + threadNr = gmx_omp_get_thread_num(); #endif /* HAVE_OPENMP ================================================= */ do { diff --git a/src/tools/gmx_sans.c b/src/tools/gmx_sans.c index 2cae68baa5..fcf99c8e75 100644 --- a/src/tools/gmx_sans.c +++ b/src/tools/gmx_sans.c @@ -54,11 +54,7 @@ #include "matio.h" #include "gmx_ana.h" #include "nsfactor.h" - -#ifdef GMX_OPENMP -#include -#endif - +#include "gmx_omp.h" int gmx_sans(int argc,char *argv[]) { @@ -153,9 +149,7 @@ int gmx_sans(int argc,char *argv[]) { efXVG, "-pr", "pr", ffWRITE } }; -#ifdef GMX_OPENMP - nthreads = omp_get_max_threads(); -#endif + nthreads = gmx_omp_get_max_threads(); CopyRight(stderr,argv[0]); parse_common_args(&argc,argv,PCA_BE_NICE, @@ -166,9 +160,8 @@ int gmx_sans(int argc,char *argv[]) check_mcover(mcover); /* setting number of omp threads globaly */ -#ifdef GMX_OPENMP - omp_set_num_threads(nthreads); -#endif + gmx_omp_set_num_threads(nthreads); + /* Now try to parse opts for modes */ switch(emethod[0][0]) { case 'd': diff --git a/src/tools/nsfactor.c b/src/tools/nsfactor.c index e7879e7ca0..863b658e6e 100644 --- a/src/tools/nsfactor.c +++ b/src/tools/nsfactor.c @@ -45,10 +45,7 @@ #include "strdb.h" #include "vec.h" #include "nsfactor.h" - -#ifdef GMX_OPENMP -#include -#endif +#include "gmx_omp.h" void check_binwidth(real binwidth) { real smallest_bin=0.1; @@ -202,7 +199,7 @@ gmx_radial_distribution_histogram_t *calc_radial_distribution_histogram ( } rng=gmx_rng_init(seed); #ifdef GMX_OPENMP - nthreads = omp_get_max_threads(); + nthreads = gmx_omp_get_max_threads(); snew(tgr,nthreads); snew(trng,nthreads); for(i=0;i