Skip to content

Commit a005a80

Browse files
authored
Merge pull request #54 from wkliao/libver
Add pnetcdf.libver() and pnetcdf.inq_clibvers()
2 parents dd3c0bf + a91a976 commit a005a80

File tree

10 files changed

+113
-13
lines changed

10 files changed

+113
-13
lines changed

docs/source/api/file_api.rst

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ relations among data fields stored in a netCDF file.
1515
detach_buff, set_fill, inq_buff_usage, inq_buff_size, inq_num_rec_vars,
1616
inq_num_fix_vars, inq_striping, inq_recsize, inq_version, inq_info,
1717
inq_header_size, inq_put_size, inq_header_extent, inq_nreqs
18-
:exclude-members: dimensions, variables, file_format, libver, indep_mode,
19-
path
18+
:exclude-members: dimensions, variables, file_format, indep_mode, path
2019

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

4544
**Type:** `str`
4645

47-
.. attribute:: libver
48-
49-
The PnetCDF-Python version string.
50-
51-
**Type:** `str`
52-

docs/source/api/function_api.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22
Utility Functions
33
================
44

5+
.. autofunction:: pnetcdf::libver
56
.. autofunction:: pnetcdf::strerror
67
.. autofunction:: pnetcdf::strerrno
78
.. autofunction:: pnetcdf::chartostring
89
.. autofunction:: pnetcdf::stringtochar
910
.. autofunction:: pnetcdf::set_default_format
1011
.. autofunction:: pnetcdf::inq_default_format
1112
.. autofunction:: pnetcdf::inq_file_format
13+
.. autofunction:: pnetcdf::inq_clibvers

include/PnetCDF.pxi

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ cdef extern from "pnetcdf.h":
2121
ctypedef int MPI_Datatype
2222
const_char_ptr ncmpi_strerror(int err);
2323
const_char_ptr ncmpi_strerrno(int err);
24+
const_char_ptr ncmpi_inq_libvers();
2425

2526

2627
cdef const int NC_BYTE_C "NC_BYTE"

setup.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,21 @@
1414
import mpi4py
1515
import json
1616
from packaging import version
17+
import shutil
1718

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

21+
if 'CC' not in os.environ:
22+
# environment variable CC is not set
23+
# check if command 'mpicc' is available in PATH
24+
path = shutil.which("mpicc")
25+
if path is None:
26+
raise RuntimeError("Error: could not find an mpicc, please set it in environment variable CC")
27+
os.environ["CC"] = path
28+
2029
PnetCDF_dir = os.environ.get('PNETCDF_DIR')
2130
pnc_config = None
2231

23-
2432
try:
2533
if pnc_config is None:
2634
if PnetCDF_dir is not None:

src/pnetcdf/__init__.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,13 @@
1212
from ._Dimension import *
1313
from ._Variable import *
1414
from ._utils import *
15+
16+
def libver():
17+
"""
18+
libver()
19+
20+
:return: The PnetCDF-Python version string, for example "1.0.0".
21+
:rtype: str
22+
"""
23+
return __version__
24+

src/pnetcdf/_utils.pxd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,4 @@ cpdef inq_default_format()
2626
cpdef inq_file_format(str file_name)
2727
cpdef inq_malloc_max_size()
2828
cpdef inq_malloc_size()
29+
cpdef inq_clibvers()

src/pnetcdf/_utils.pyx

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -791,6 +791,21 @@ cdef _get_format(int ncid):
791791

792792

793793
# external C functions.
794+
cpdef inq_clibvers():
795+
"""
796+
inq_clibvers()
797+
798+
This function returns a string describing the version of the PnetCDF-C
799+
library used to build this PnetCDF-Python module, and when the PnetCDF-C
800+
library was built.
801+
802+
:return: A string about PnetCDF-C library, for example, "1.13.0 of March 29, 2024".
803+
804+
:rtype: str
805+
"""
806+
ver_str = (<char *>ncmpi_inq_libvers()).decode('ascii')
807+
return ver_str
808+
794809
cpdef strerror(err_code):
795810
"""
796811
strerror(err_code)
@@ -807,7 +822,6 @@ cpdef strerror(err_code):
807822
"""
808823
cdef int ierr
809824
ierr = err_code
810-
811825
err_str = (<char *>ncmpi_strerror(ierr)).decode('ascii')
812826
return err_str
813827

@@ -873,7 +887,7 @@ cpdef inq_default_format():
873887
874888
:Operational mode: This function is an independent subroutine.
875889
"""
876-
cdef int curformat
890+
cdef int ierr, curformat
877891
with nogil:
878892
ierr = ncmpi_inq_default_format(&curformat)
879893
_check_err(ierr)

test/Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ check_PROGRAMS = tst_atts.py \
4747
tst_var_string.py \
4848
tst_var_type.py \
4949
tst_version.py \
50-
tst_wait.py
50+
tst_wait.py \
51+
tst_libver.py
5152

5253
TESTS_ENVIRONMENT = export check_PROGRAMS="${check_PROGRAMS}";
5354
OUTPUT_DIR = _tmp_output

test/parallel_run.sh

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,11 @@ fi
3030
for prog in ${check_PROGRAMS} ; do
3131
printf '%-60s' "Testing $prog "
3232

33-
CMD="mpiexec -n $NPROC python $prog $OUT_DIR"
33+
if test $prog = "tst_libver.py" ; then
34+
CMD="mpiexec -n $NPROC python $prog -q"
35+
else
36+
CMD="mpiexec -n $NPROC python $prog $OUT_DIR"
37+
fi
3438
$CMD
3539
status=$?
3640
if [ $status -ne 0 ]; then

test/tst_libver.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#
2+
# Copyright (C) 2024, Northwestern University and Argonne National Laboratory
3+
# See COPYRIGHT notice in top-level directory.
4+
#
5+
6+
"""
7+
This program test the followings:
8+
pnetcdf class member: __version__ a string of PnetCDF-Python version
9+
pnetcdf method: libver() a function call to get the version
10+
pnetcdf method: inq_clibvers() a string of PnetCDF-C library version
11+
"""
12+
13+
import sys, argparse
14+
from mpi4py import MPI
15+
import pnetcdf
16+
17+
def parse_help():
18+
help_flag = "-h" in sys.argv or "--help" in sys.argv
19+
if help_flag and rank == 0:
20+
help_text = (
21+
"Usage: {} [-h | -q]\n"
22+
" [-h] Print help\n"
23+
" [-q] Quiet mode (reports when fail)\n"
24+
).format(sys.argv[0])
25+
print(help_text)
26+
return help_flag
27+
28+
if __name__ == "__main__":
29+
30+
rank = MPI.COMM_WORLD.Get_rank()
31+
32+
if parse_help():
33+
MPI.Finalize()
34+
sys.exit(1)
35+
36+
# Get command-line arguments
37+
args = None
38+
parser = argparse.ArgumentParser()
39+
parser.add_argument("-q", help="Quiet mode (reports when fail)", action="store_true")
40+
args = parser.parse_args()
41+
42+
verbose = False if args.q else True
43+
44+
if verbose and rank == 0:
45+
print("test pnetcdf.libver() and pnetcdf.inq_clibvers()")
46+
47+
# Run tests
48+
try:
49+
mlibver = pnetcdf.__version__
50+
if verbose and rank == 0:
51+
print("Test python class member, pnetcdf.__version__ = ", mlibver)
52+
53+
plibver = pnetcdf.libver()
54+
if verbose and rank == 0:
55+
print("test pnetcdf.libver(), PnetCDF Python version : ", plibver)
56+
57+
clibvers = pnetcdf.inq_clibvers()
58+
if verbose and rank == 0:
59+
print("test pnetcdf_python_clibvers(), PnetCDF C library version: ", clibvers)
60+
61+
except BaseException as err:
62+
print("Error: type:", type(err), str(err))
63+
raise
64+
65+
MPI.Finalize()
66+

0 commit comments

Comments
 (0)