diff --git a/.gitignore b/.gitignore index 11d620216..a808b3552 100644 --- a/.gitignore +++ b/.gitignore @@ -5,6 +5,7 @@ dist/ netCDF4/_netCDF4.c netCDF4/*.so include/constants.pyx +include/parallel_support_imports.pxi netcdftime/_netcdftime.c venv/ .eggs/ diff --git a/include/no_parallel_support_imports.pxi.in b/include/no_parallel_support_imports.pxi.in new file mode 100644 index 000000000..e64ba6c5a --- /dev/null +++ b/include/no_parallel_support_imports.pxi.in @@ -0,0 +1,8 @@ +# Stubs for when parallel support is not enabled + +ctypedef int Comm +ctypedef int Info +cdef Comm MPI_COMM_WORLD +cdef Info MPI_INFO_NULL +MPI_COMM_WORLD = 0 +MPI_INFO_NULL = 0 diff --git a/include/parallel_support_imports.pxi.in b/include/parallel_support_imports.pxi.in new file mode 100644 index 000000000..5379bedc4 --- /dev/null +++ b/include/parallel_support_imports.pxi.in @@ -0,0 +1,16 @@ +# Imports and typedefs required at compile time for enabling parallel support + +cimport mpi4py.MPI as MPI +from mpi4py.libmpi cimport ( + MPI_Comm, + MPI_Info, + MPI_Comm_dup, + MPI_Info_dup, + MPI_Comm_free, + MPI_Info_free, + MPI_INFO_NULL, + MPI_COMM_WORLD, +) + +ctypedef MPI.Comm Comm +ctypedef MPI.Info Info diff --git a/setup.py b/setup.py index bea9581a4..6d7787b50 100644 --- a/setup.py +++ b/setup.py @@ -487,7 +487,20 @@ def _populate_hdf5_info(dirstosearch, inc_dirs, libs, lib_dirs): inc_dirs.append(mpi4py.get_include()) # mpi_incdir should not be needed if using nc-config # (should be included in nc-config --cflags) - if mpi_incdir is not None: inc_dirs.append(mpi_incdir) + if mpi_incdir is not None: + inc_dirs.append(mpi_incdir) + + # Name of file containing imports required for parallel support + parallel_support_imports = "parallel_support_imports.pxi.in" + else: + parallel_support_imports = "no_parallel_support_imports.pxi.in" + + # Copy the specific version of the file containing parallel + # support imports + shutil.copyfile( + osp.join("include", parallel_support_imports), + osp.join("include", "parallel_support_imports.pxi") + ) ext_modules = [Extension("netCDF4._netCDF4", [netcdf4_src_pyx], diff --git a/src/netCDF4/_netCDF4.pyx b/src/netCDF4/_netCDF4.pyx index da7452ed3..70036267d 100644 --- a/src/netCDF4/_netCDF4.pyx +++ b/src/netCDF4/_netCDF4.pyx @@ -1244,7 +1244,7 @@ from numpy import ma from libc.string cimport memcpy, memset from libc.stdlib cimport malloc, free numpy.import_array() -include "constants.pyx" +include "parallel_support_imports.pxi" include "membuf.pyx" include "netCDF4.pxi" @@ -1266,21 +1266,6 @@ __has_set_alignment__ = HAS_SET_ALIGNMENT __has_ncfilter__ = HAS_NCFILTER -IF HAS_PARALLEL_SUPPORT: - cimport mpi4py.MPI as MPI - from mpi4py.libmpi cimport MPI_Comm, MPI_Info, MPI_Comm_dup, MPI_Info_dup, \ - MPI_Comm_free, MPI_Info_free, MPI_INFO_NULL,\ - MPI_COMM_WORLD - ctypedef MPI.Comm Comm - ctypedef MPI.Info Info -ELSE: - ctypedef int Comm - ctypedef int Info - cdef Comm MPI_COMM_WORLD - cdef Info MPI_INFO_NULL - MPI_COMM_WORLD = 0 - MPI_INFO_NULL = 0 - # set path to SSL certificates (issue #1246) # available starting in version 4.9.1 if HAS_NCRCSET: