Skip to content

Commit

Permalink
Merge pull request #54 from wkliao/libver
Browse files Browse the repository at this point in the history
Add pnetcdf.libver() and pnetcdf.inq_clibvers()
  • Loading branch information
wkliao authored Sep 9, 2024
2 parents dd3c0bf + a91a976 commit a005a80
Show file tree
Hide file tree
Showing 10 changed files with 113 additions and 13 deletions.
9 changes: 1 addition & 8 deletions docs/source/api/file_api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ relations among data fields stored in a netCDF file.
detach_buff, set_fill, inq_buff_usage, inq_buff_size, inq_num_rec_vars,
inq_num_fix_vars, inq_striping, inq_recsize, inq_version, inq_info,
inq_header_size, inq_put_size, inq_header_extent, inq_nreqs
:exclude-members: dimensions, variables, file_format, libver, indep_mode,
path
:exclude-members: dimensions, variables, file_format, indep_mode, path

Read-only Python Attributes of File Class
The following class members are read-only and should not be modified by the
Expand Down Expand Up @@ -44,9 +43,3 @@ Read-only Python Attributes of File Class

**Type:** `str`

.. attribute:: libver

The PnetCDF-Python version string.

**Type:** `str`

2 changes: 2 additions & 0 deletions docs/source/api/function_api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
Utility Functions
================

.. autofunction:: pnetcdf::libver
.. autofunction:: pnetcdf::strerror
.. autofunction:: pnetcdf::strerrno
.. autofunction:: pnetcdf::chartostring
.. autofunction:: pnetcdf::stringtochar
.. autofunction:: pnetcdf::set_default_format
.. autofunction:: pnetcdf::inq_default_format
.. autofunction:: pnetcdf::inq_file_format
.. autofunction:: pnetcdf::inq_clibvers
1 change: 1 addition & 0 deletions include/PnetCDF.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ cdef extern from "pnetcdf.h":
ctypedef int MPI_Datatype
const_char_ptr ncmpi_strerror(int err);
const_char_ptr ncmpi_strerrno(int err);
const_char_ptr ncmpi_inq_libvers();


cdef const int NC_BYTE_C "NC_BYTE"
Expand Down
10 changes: 9 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,21 @@
import mpi4py
import json
from packaging import version
import shutil

open_kwargs = {'encoding': 'utf-8'}

if 'CC' not in os.environ:
# environment variable CC is not set
# check if command 'mpicc' is available in PATH
path = shutil.which("mpicc")
if path is None:
raise RuntimeError("Error: could not find an mpicc, please set it in environment variable CC")
os.environ["CC"] = path

PnetCDF_dir = os.environ.get('PNETCDF_DIR')
pnc_config = None


try:
if pnc_config is None:
if PnetCDF_dir is not None:
Expand Down
10 changes: 10 additions & 0 deletions src/pnetcdf/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,13 @@
from ._Dimension import *
from ._Variable import *
from ._utils import *

def libver():
"""
libver()
:return: The PnetCDF-Python version string, for example "1.0.0".
:rtype: str
"""
return __version__

1 change: 1 addition & 0 deletions src/pnetcdf/_utils.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,4 @@ cpdef inq_default_format()
cpdef inq_file_format(str file_name)
cpdef inq_malloc_max_size()
cpdef inq_malloc_size()
cpdef inq_clibvers()
18 changes: 16 additions & 2 deletions src/pnetcdf/_utils.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -791,6 +791,21 @@ cdef _get_format(int ncid):


# external C functions.
cpdef inq_clibvers():
"""
inq_clibvers()
This function returns a string describing the version of the PnetCDF-C
library used to build this PnetCDF-Python module, and when the PnetCDF-C
library was built.
:return: A string about PnetCDF-C library, for example, "1.13.0 of March 29, 2024".
:rtype: str
"""
ver_str = (<char *>ncmpi_inq_libvers()).decode('ascii')
return ver_str

cpdef strerror(err_code):
"""
strerror(err_code)
Expand All @@ -807,7 +822,6 @@ cpdef strerror(err_code):
"""
cdef int ierr
ierr = err_code

err_str = (<char *>ncmpi_strerror(ierr)).decode('ascii')
return err_str

Expand Down Expand Up @@ -873,7 +887,7 @@ cpdef inq_default_format():
:Operational mode: This function is an independent subroutine.
"""
cdef int curformat
cdef int ierr, curformat
with nogil:
ierr = ncmpi_inq_default_format(&curformat)
_check_err(ierr)
Expand Down
3 changes: 2 additions & 1 deletion test/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ check_PROGRAMS = tst_atts.py \
tst_var_string.py \
tst_var_type.py \
tst_version.py \
tst_wait.py
tst_wait.py \
tst_libver.py

TESTS_ENVIRONMENT = export check_PROGRAMS="${check_PROGRAMS}";
OUTPUT_DIR = _tmp_output
Expand Down
6 changes: 5 additions & 1 deletion test/parallel_run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ fi
for prog in ${check_PROGRAMS} ; do
printf '%-60s' "Testing $prog "

CMD="mpiexec -n $NPROC python $prog $OUT_DIR"
if test $prog = "tst_libver.py" ; then
CMD="mpiexec -n $NPROC python $prog -q"
else
CMD="mpiexec -n $NPROC python $prog $OUT_DIR"
fi
$CMD
status=$?
if [ $status -ne 0 ]; then
Expand Down
66 changes: 66 additions & 0 deletions test/tst_libver.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#
# Copyright (C) 2024, Northwestern University and Argonne National Laboratory
# See COPYRIGHT notice in top-level directory.
#

"""
This program test the followings:
pnetcdf class member: __version__ a string of PnetCDF-Python version
pnetcdf method: libver() a function call to get the version
pnetcdf method: inq_clibvers() a string of PnetCDF-C library version
"""

import sys, argparse
from mpi4py import MPI
import pnetcdf

def parse_help():
help_flag = "-h" in sys.argv or "--help" in sys.argv
if help_flag and rank == 0:
help_text = (
"Usage: {} [-h | -q]\n"
" [-h] Print help\n"
" [-q] Quiet mode (reports when fail)\n"
).format(sys.argv[0])
print(help_text)
return help_flag

if __name__ == "__main__":

rank = MPI.COMM_WORLD.Get_rank()

if parse_help():
MPI.Finalize()
sys.exit(1)

# Get command-line arguments
args = None
parser = argparse.ArgumentParser()
parser.add_argument("-q", help="Quiet mode (reports when fail)", action="store_true")
args = parser.parse_args()

verbose = False if args.q else True

if verbose and rank == 0:
print("test pnetcdf.libver() and pnetcdf.inq_clibvers()")

# Run tests
try:
mlibver = pnetcdf.__version__
if verbose and rank == 0:
print("Test python class member, pnetcdf.__version__ = ", mlibver)

plibver = pnetcdf.libver()
if verbose and rank == 0:
print("test pnetcdf.libver(), PnetCDF Python version : ", plibver)

clibvers = pnetcdf.inq_clibvers()
if verbose and rank == 0:
print("test pnetcdf_python_clibvers(), PnetCDF C library version: ", clibvers)

except BaseException as err:
print("Error: type:", type(err), str(err))
raise

MPI.Finalize()

0 comments on commit a005a80

Please sign in to comment.