diff --git a/fortran/omp.f90 b/fortran/omp.f90 index bea1974..dce6b1d 100644 --- a/fortran/omp.f90 +++ b/fortran/omp.f90 @@ -10,6 +10,19 @@ MODULE omp_constants END MODULE omp_constants +FUNCTION fomp_enabled() + + IMPLICIT NONE + + !f2py threadsafe + + LOGICAL :: fomp_enabled + + fomp_enabled = .FALSE. + +END FUNCTION fomp_enabled + + SUBROUTINE fomp_set_num_threads(num_threads) IMPLICIT NONE diff --git a/fortran/ompgen.F90 b/fortran/ompgen.F90 index dea4246..6ae48e1 100644 --- a/fortran/ompgen.F90 +++ b/fortran/ompgen.F90 @@ -61,6 +61,23 @@ MODULE omp_constants END MODULE omp_constants +FUNCTION fomp_enabled() + + IMPLICIT NONE + + !f2py threadsafe + + LOGICAL :: fomp_enabled + +#ifdef _OPENMP + fomp_enabled = .TRUE. +#else + fomp_enabled = .FALSE. +#endif + +END FUNCTION fomp_enabled + + SUBROUTINE fomp_set_num_threads(num_threads) #ifdef _OPENMP USE omp_lib diff --git a/fortran/wrffortran.pyf b/fortran/wrffortran.pyf index b74da12..7211ff6 100644 --- a/fortran/wrffortran.pyf +++ b/fortran/wrffortran.pyf @@ -42,6 +42,10 @@ python module _wrffortran ! in integer, parameter,optional :: fomp_lock_kind=4 integer(kind=4), parameter,optional :: fomp_sched_auto=4 end module omp_constants + function fomp_enabled() ! in :_wrffortran:omp.f90 + threadsafe + logical :: fomp_enabled + end function fomp_enabled subroutine fomp_set_num_threads(num_threads) ! in :_wrffortran:omp.f90 threadsafe integer intent(in) :: num_threads @@ -229,7 +233,7 @@ python module _wrffortran ! in end subroutine dpfcalc subroutine dcapecalc3d(prs,tmk,qvp,ght,ter,sfp,cape,cin,cmsg,mix,mjy,mkzh,ter_follow,psafile,errstat,errmsg) ! in :_wrffortran:rip_cape.f90 threadsafe - use wrf_constants, only: tlclc2,gamma,tlclc1,rgasmd,tlclc4,g,tlclc3,thtecon3,eps,rd,cpmd,celkel,gammamd,eslcon2,eslcon1,cp,thtecon1,algerr,ezero,thtecon2 + use wrf_constants, only: tlclc2,gamma,tlclc1,rgasmd,tlclc4,g,tlclc3,thtecon3,eps,rd,cpmd,celkel,gammamd,eslcon2,eslcon1,cp,thtecon1,ezero,thtecon2 real(kind=8) dimension(mix,mjy,mkzh),intent(in) :: prs real(kind=8) dimension(mix,mjy,mkzh),intent(in),depend(mix,mjy,mkzh) :: tmk real(kind=8) dimension(mix,mjy,mkzh),intent(in),depend(mix,mjy,mkzh) :: qvp @@ -249,7 +253,7 @@ python module _wrffortran ! in end subroutine dcapecalc3d subroutine dcapecalc2d(prs,tmk,qvp,ght,ter,sfp,cape,cin,cmsg,mix,mjy,mkzh,ter_follow,psafile,errstat,errmsg) ! in :_wrffortran:rip_cape.f90 threadsafe - use wrf_constants, only: tlclc2,gamma,tlclc1,rgasmd,tlclc4,g,tlclc3,thtecon3,eps,rd,cpmd,celkel,gammamd,eslcon2,eslcon1,cp,thtecon1,algerr,ezero,thtecon2 + use wrf_constants, only: tlclc2,gamma,tlclc1,rgasmd,tlclc4,g,tlclc3,thtecon3,eps,rd,cpmd,celkel,gammamd,eslcon2,eslcon1,cp,thtecon1,ezero,thtecon2 real(kind=8) dimension(mix,mjy,mkzh),intent(in) :: prs real(kind=8) dimension(mix,mjy,mkzh),intent(in),depend(mix,mjy,mkzh) :: tmk real(kind=8) dimension(mix,mjy,mkzh),intent(in),depend(mix,mjy,mkzh) :: qvp diff --git a/src/wrf/api.py b/src/wrf/api.py index 0542972..c774505 100644 --- a/src/wrf/api.py +++ b/src/wrf/api.py @@ -2,7 +2,7 @@ from .config import (xarray_enabled, disable_xarray, enable_xarray, cartopy_enabled, disable_cartopy, enable_cartopy, basemap_enabled, disable_basemap, enable_basemap, pyngl_enabled, enable_pyngl, disable_pyngl, - set_cache_size, get_cache_size) + set_cache_size, get_cache_size, omp_enabled) from .constants import (ALL_TIMES, Constants, ConversionFactors, ProjectionTypes, default_fill) from .destag import destagger @@ -58,7 +58,7 @@ __all__ += ["xarray_enabled", "disable_xarray", "enable_xarray", "cartopy_enabled", "disable_cartopy", "enable_cartopy", "basemap_enabled", "disable_basemap", "enable_basemap", "pyngl_enabled", "enable_pyngl", "disable_pyngl", - "set_cache_size", "get_cache_size"] + "set_cache_size", "get_cache_size", "omp_enabled"] __all__ += ["ALL_TIMES", "Constants", "ConversionFactors", "ProjectionTypes", "default_fill"] __all__ += ["destagger"] diff --git a/src/wrf/config.py b/src/wrf/config.py index 556f632..adf9705 100644 --- a/src/wrf/config.py +++ b/src/wrf/config.py @@ -4,6 +4,8 @@ from __future__ import (absolute_import, division, print_function, from threading import local import wrapt +from ._wrffortran import fomp_enabled + _local_config = local() def _init_local(): @@ -198,5 +200,17 @@ def get_cache_size(): global _local_config return int(_local_config.cache_size) - + +def omp_enabled(): + """Return True if OpenMP is enabled. + + OpenMP is only enabled if compiled with OpenMP features. + + Returns: + + :obj:`bool`: True if OpenMP is enabled, otherwise False. + + """ + + return True if fomp_enabled() else False diff --git a/src/wrf/constants.py b/src/wrf/constants.py index 96dcc02..ab6da60 100755 --- a/src/wrf/constants.py +++ b/src/wrf/constants.py @@ -6,7 +6,7 @@ import struct import numpy as np from .py3compat import viewitems -from wrf._wrffortran import wrf_constants, omp_constants +from ._wrffortran import wrf_constants, omp_constants #: Indicates that all times should be used in a diagnostic routine. ALL_TIMES = None