1
0
mirror of https://github.com/TREX-CoE/trexio.git synced 2024-12-22 20:35:44 +01:00

Merge pull request #78 from TREX-CoE/add-inquire-functionality

Add trexio_info function
This commit is contained in:
Evgeny Posenitskiy 2022-01-24 18:21:47 +01:00 committed by GitHub
commit d46d4adab8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 142 additions and 21 deletions

View File

@ -32,8 +32,25 @@ if(EXISTS "${CMAKE_SOURCE_DIR}/.git/config")
else() else()
message(FATAL_ERROR "EMACS not found. It is required to produce TREXIO source code from org-mode files.") message(FATAL_ERROR "EMACS not found. It is required to produce TREXIO source code from org-mode files.")
endif() endif()
# in case Git is not available, we default to "unknown"
set(GIT_HASH "unknown")
# find Git and if available set GIT_HASH variable
find_package(Git QUIET)
if(GIT_FOUND)
execute_process(
COMMAND ${GIT_EXECUTABLE} --no-pager show -s --pretty=format:%H -n 1
OUTPUT_VARIABLE GIT_HASH
OUTPUT_STRIP_TRAILING_WHITESPACE
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
ERROR_QUIET)
endif()
# get the user name from the ${USER} env variable
set(TREXIO_USER_NAME $ENV{USER})
# replace placeholders in the templace config.h.in file to produce config.h
# config.h is needed to insert TREXIO_PACKAGE_VERSION and TREXIO_GIT_HASH into trexio.h
configure_file(${CMAKE_SOURCE_DIR}/include/cmake_config.h.in configure_file(${CMAKE_SOURCE_DIR}/include/cmake_config.h.in
${CMAKE_SOURCE_DIR}/include/config.h) ${CMAKE_SOURCE_DIR}/include/config.h
@ONLY)
endif() endif()
# Set directories to be included at build time. # Set directories to be included at build time.

View File

@ -3,3 +3,5 @@
#define VERSION_MAJOR @PROJECT_VERSION_MAJOR@ #define VERSION_MAJOR @PROJECT_VERSION_MAJOR@
#define VERSION_MINOR @PROJECT_VERSION_MINOR@ #define VERSION_MINOR @PROJECT_VERSION_MINOR@
#define VERSION_PATCH @PROJECT_VERSION_PATCH@ #define VERSION_PATCH @PROJECT_VERSION_PATCH@
#define GIT_HASH "@GIT_HASH@"
#define TREXIO_USER_NAME "@TREXIO_USER_NAME@"

View File

@ -38,6 +38,8 @@ except:
#============ WRITE THE DATA IN THE TEST FILE ============# #============ WRITE THE DATA IN THE TEST FILE ============#
#=========================================================# #=========================================================#
trexio.info()
# create TREXIO file and open it for writing # create TREXIO file and open it for writing
test_file = trexio.File(output_filename, mode='w', back_end=TEST_TREXIO_BACKEND) test_file = trexio.File(output_filename, mode='w', back_end=TEST_TREXIO_BACKEND)
assert test_file.exists assert test_file.exists

View File

@ -1,4 +1,17 @@
# ============= THREADS SUPPORT ==============
# the two options below add preference for the pthread library over other system-wide threads implementations
set(CMAKE_THREAD_PREFER_PTHREAD TRUE)
set(THREADS_PREFER_PTHREAD_FLAG TRUE)
# find thread library
find_package(Threads REQUIRED)
# ============= STDINT SUPPORT ===============
include(CheckIncludeFile)
check_include_file(stdint.h HAVE_STDINT_H)
# ========= DEFINE TREXIO C LIBRARY ========= # ========= DEFINE TREXIO C LIBRARY =========
# Set a list of TREXIO source and header files that are always compiled. # Set a list of TREXIO source and header files that are always compiled.

View File

@ -4,18 +4,22 @@
cat prefix_front.c > trexio.c cat prefix_front.c > trexio.c
cat prefix_front.h > trexio.h cat prefix_front.h > trexio.h
# parse the config-defined version attributes to pass them to Fortran module file # parse the config-defined version attributes to pass them to the header files
VERSION_VAL=`grep "PACKAGE_VERSION" ../../include/config.h | cut -d " " -f 3` VERSION_VAL=`grep "PACKAGE_VERSION" ../../include/config.h | cut -d " " -f 3`
VERSION_MAJOR_VAL=`grep "VERSION_MAJOR" ../../include/config.h | cut -d " " -f 3` VERSION_MAJOR_VAL=`grep "VERSION_MAJOR" ../../include/config.h | cut -d " " -f 3`
VERSION_MINOR_VAL=`grep "VERSION_MINOR" ../../include/config.h | cut -d " " -f 3` VERSION_MINOR_VAL=`grep "VERSION_MINOR" ../../include/config.h | cut -d " " -f 3`
VERSION_PATCH_VAL=`grep "VERSION_PATCH" ../../include/config.h | cut -d " " -f 3` VERSION_PATCH_VAL=`grep "VERSION_PATCH" ../../include/config.h | cut -d " " -f 3`
# parse the config-defined GIT_HASH to pass them to the header files
GIT_HASH_STR=`grep "GIT_HASH" ../../include/config.h | cut -d " " -f 3`
# grep some usefull constants from the config.h # grep some usefull constants from the config.h
echo "" >> trexio.h echo "" >> trexio.h
echo "#define TREXIO_PACKAGE_VERSION ${VERSION_VAL:='0.0.0'}" >> trexio.h echo "#define TREXIO_PACKAGE_VERSION ${VERSION_VAL:='0.0.0'}" >> trexio.h
echo "#define TREXIO_VERSION_MAJOR ${VERSION_MAJOR_VAL:=0}" >> trexio.h echo "#define TREXIO_VERSION_MAJOR ${VERSION_MAJOR_VAL:=0}" >> trexio.h
echo "#define TREXIO_VERSION_MINOR ${VERSION_MINOR_VAL:=0}" >> trexio.h echo "#define TREXIO_VERSION_MINOR ${VERSION_MINOR_VAL:=0}" >> trexio.h
echo "#define TREXIO_VERSION_PATCH ${VERSION_PATCH_VAL:=0}" >> trexio.h echo "#define TREXIO_VERSION_PATCH ${VERSION_PATCH_VAL:=0}" >> trexio.h
echo "#define TREXIO_GIT_HASH ${GIT_HASH_STR:='0000'}" >> trexio.h
echo "" >> trexio.h echo "" >> trexio.h
cat prefix_s_front.h > trexio_s.h cat prefix_s_front.h > trexio_s.h
@ -24,10 +28,11 @@ cat prefix_python.py > trexio.py
# append version string and attributes to the Fortran module file # append version string and attributes to the Fortran module file
echo "" >> trexio_f.f90 echo "" >> trexio_f.f90
echo "character(len = 12) :: TREXIO_PACKAGE_VERSION = ${VERSION_VAL}" >> trexio_f.f90 echo "character(len = 12) :: TREXIO_PACKAGE_VERSION = ${VERSION_VAL:='0.0.0'}" >> trexio_f.f90
echo "integer :: TREXIO_VERSION_MAJOR = ${VERSION_MAJOR_VAL}" >> trexio_f.f90 echo "integer :: TREXIO_VERSION_MAJOR = ${VERSION_MAJOR_VAL}" >> trexio_f.f90
echo "integer :: TREXIO_VERSION_MINOR = ${VERSION_MINOR_VAL}" >> trexio_f.f90 echo "integer :: TREXIO_VERSION_MINOR = ${VERSION_MINOR_VAL}" >> trexio_f.f90
echo "integer :: TREXIO_VERSION_PATCH = ${VERSION_PATCH_VAL}" >> trexio_f.f90 echo "integer :: TREXIO_VERSION_PATCH = ${VERSION_PATCH_VAL}" >> trexio_f.f90
echo "character(len = 64) :: TREXIO_GIT_HASH = ${GIT_HASH_STR:='0000'}" >> trexio_f.f90
echo "" >> trexio_f.f90 echo "" >> trexio_f.f90
# c front end # c front end

View File

@ -1201,6 +1201,18 @@ def _close(trexio_file):
output: output:
~trexio_exit_code~ exit code. ~trexio_exit_code~ exit code.
It returns:
- ~TREXIO_SUCCESS~ if the ~file_name~ exists and if it correspond to a valid TREXIO file (i.e. a directory OR an HDF5 file)
- ~TREXIO_FILE_ERROR~ if ~file_name~ exists but it is not a TREXIO file
- ~TREXIO_FAILURE~ otherwise
Also, there is an implicit way to have the aforementioned functionality.
Attempt to open a non-existing file with ~trexio_open~ function will result in a ~TREXIO_OPEN_ERROR~ exit code
(see the modified value that has been passed as a 4-th argument to ~trexio_open~).
You can see examples of both functionalities in =test_f.f90= (search for calls with ~_void~ suffix).
*** C *** C
#+begin_src c :tangle prefix_front.h :exports none #+begin_src c :tangle prefix_front.h :exports none
@ -1387,6 +1399,7 @@ trexio_exit_code
trexio_read_$group_num$_64 (trexio_t* const file, $group_num_dtype_double$* const num) trexio_read_$group_num$_64 (trexio_t* const file, $group_num_dtype_double$* const num)
{ {
if (file == NULL) return TREXIO_INVALID_ARG_1; if (file == NULL) return TREXIO_INVALID_ARG_1;
if (num == NULL) return TREXIO_INVALID_ARG_2;
if (trexio_has_$group_num$(file) != TREXIO_SUCCESS) return TREXIO_ATTR_MISSING; if (trexio_has_$group_num$(file) != TREXIO_SUCCESS) return TREXIO_ATTR_MISSING;
switch (file->back_end) { switch (file->back_end) {
@ -1446,6 +1459,7 @@ trexio_exit_code
trexio_read_$group_num$_32 (trexio_t* const file, $group_num_dtype_single$* const num) trexio_read_$group_num$_32 (trexio_t* const file, $group_num_dtype_single$* const num)
{ {
if (file == NULL) return TREXIO_INVALID_ARG_1; if (file == NULL) return TREXIO_INVALID_ARG_1;
if (num == NULL) return TREXIO_INVALID_ARG_2;
if (trexio_has_$group_num$(file) != TREXIO_SUCCESS) return TREXIO_ATTR_MISSING; if (trexio_has_$group_num$(file) != TREXIO_SUCCESS) return TREXIO_ATTR_MISSING;
$group_num_dtype_double$ num_64 = 0; $group_num_dtype_double$ num_64 = 0;
@ -2670,6 +2684,7 @@ trexio_exit_code
trexio_read_$group_dset$_size(trexio_t* const file, int64_t* const size_max) trexio_read_$group_dset$_size(trexio_t* const file, int64_t* const size_max)
{ {
if (file == NULL) return TREXIO_INVALID_ARG_1; if (file == NULL) return TREXIO_INVALID_ARG_1;
if (size_max == NULL) return TREXIO_INVALID_ARG_2;
if (trexio_has_$group_dset$(file) != TREXIO_SUCCESS) return TREXIO_DSET_MISSING; if (trexio_has_$group_dset$(file) != TREXIO_SUCCESS) return TREXIO_DSET_MISSING;
switch (file->back_end) { switch (file->back_end) {
@ -3894,6 +3909,70 @@ def has_$group_str$(trexio_file) -> bool:
return False return False
#+end_src #+end_src
* General helper functions
This section contains general helper functions like ~trexio_info~.
~trexio_info~ prints information about the TREXIO configuration (see =config.h= file).
In particular:
1) ~TREXIO_PACKAGE_VERSION~ [string]
2) ~HAVE_HDF5~ [bool]
3) ~HDF5_VERSION~ [string] (optional, only if ~HAVE_HDF5~ is ~true~)
4) ~TREXIO_GIT_HASH~ [string]
** C
#+begin_src c :tangle prefix_front.h :exports none
trexio_exit_code trexio_info(void);
#+end_src
#+begin_src c :tangle prefix_front.c
trexio_exit_code
trexio_info (void)
{
printf("TREXIO_PACKAGE_VERSION : %s\n", TREXIO_PACKAGE_VERSION);
#ifdef TREXIO_GIT_HASH
printf("TREXIO_GIT_HASH : %s\n", TREXIO_GIT_HASH);
#else
printf("GIT_HASH is stored in the config.h file, which is missing.");
#endif
#ifdef HAVE_HDF5
printf("HAVE_HDF5 : true\n");
printf("%s\n", H5_VERS_INFO);
#else
printf("HAVE_HDF5 : false\n");
printf("TREXIO configured without the HDF5 library\n");
#endif
return TREXIO_SUCCESS;
}
#+end_src
** Fortran
#+begin_src f90 :tangle prefix_fortran.f90
interface
integer function trexio_info () bind(C)
use, intrinsic :: iso_c_binding
end function trexio_info
end interface
#+end_src
** Python
#+begin_src python :tangle basic_python.py
def info():
"""Print the info about the installed TREXIO library.
"""
rc = pytr.trexio_info()
if rc != TREXIO_SUCCESS:
raise Error(rc)
#+end_src
* Fortran helper/wrapper functions * Fortran helper/wrapper functions
The function below adapts the original C-based ~trexio_open~ for Fortran. The function below adapts the original C-based ~trexio_open~ for Fortran.

View File

@ -14,6 +14,8 @@ int main() {
int rc; int rc;
trexio_info();
bool have_hdf5 = trexio_has_backend(TREXIO_HDF5); bool have_hdf5 = trexio_has_backend(TREXIO_HDF5);
if(have_hdf5) { if(have_hdf5) {
rc = system("rm -f -- test_all.h5"); rc = system("rm -f -- test_all.h5");
@ -223,5 +225,3 @@ int test_read(const char* file_name, const back_end_t backend) {
return 0; return 0;
} }

View File

@ -3,6 +3,7 @@ program test_trexio
use, intrinsic :: iso_c_binding use, intrinsic :: iso_c_binding
implicit none implicit none
integer :: rc
logical :: have_hdf5 logical :: have_hdf5
print * , "============================================" print * , "============================================"
@ -11,6 +12,8 @@ program test_trexio
print'(a,i3)', " TREXIO MINOR VERSION : ", TREXIO_VERSION_MINOR print'(a,i3)', " TREXIO MINOR VERSION : ", TREXIO_VERSION_MINOR
print * , "============================================" print * , "============================================"
rc = trexio_info()
call system('rm -rf -- test_write_f.dir') call system('rm -rf -- test_write_f.dir')
print *, 'call test_write(''test_write_f.dir'', TREXIO_TEXT)' print *, 'call test_write(''test_write_f.dir'', TREXIO_TEXT)'
call test_write('test_write_f.dir', TREXIO_TEXT) call test_write('test_write_f.dir', TREXIO_TEXT)