Skip to content

Commit

Permalink
Move compile-time conditional import of parallel modules to code-gen
Browse files Browse the repository at this point in the history
To avoid the last deprecated compile-time `IF`, we instead separate
the two branches of the conditional into different files and choose
which one to include at build time.
  • Loading branch information
ZedThree committed Sep 29, 2023
1 parent b0a030f commit 2578063
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 17 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ dist/
netCDF4/_netCDF4.c
netCDF4/*.so
include/constants.pyx
include/parallel_support_imports.pxi
netcdftime/_netcdftime.c
venv/
.eggs/
Expand Down
8 changes: 8 additions & 0 deletions include/no_parallel_support_imports.pxi.in
Original file line number Diff line number Diff line change
@@ -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
16 changes: 16 additions & 0 deletions include/parallel_support_imports.pxi.in
Original file line number Diff line number Diff line change
@@ -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
15 changes: 14 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down
17 changes: 1 addition & 16 deletions src/netCDF4/_netCDF4.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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:
Expand Down

0 comments on commit 2578063

Please sign in to comment.