star-aerosol

Describe the radiative properties of aerosols
git clone git://git.meso-star.fr/star-aerosol.git
Log | Files | Refs | README | LICENSE

commit c0e460c8be6d1918f9f94add2200bbb661139b32
parent dc23cbec38fe2f895266b35db84d9e55d8621b91
Author: Vincent Forest <vincent.forest@meso-star.com>
Date:   Mon, 30 Oct 2023 11:49:16 +0100

Merge branch 'release_0.1'

Diffstat:
M.gitignore | 14+++++++-------
AMakefile | 150+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
MREADME.md | 49++++++++++++++++++++++++++++---------------------
Dcmake/CMakeLists.txt | 118-------------------------------------------------------------------------------
Aconfig.mk | 77+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Ddoc/sars.5.scd | 94-------------------------------------------------------------------------------
Amake.sh | 71+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asars.5 | 105+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asars.pc.in | 10++++++++++
Msrc/sars.c | 425++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-----------
Msrc/sars.h | 38+++++++++++++++++++++++++++++++++++---
Msrc/sars_c.h | 16++++++++++++++++
Msrc/test_sars_load.c | 100+++++++++++++++++++++++++++++++++++++++++++++++++++++--------------------------
13 files changed, 936 insertions(+), 331 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -1,12 +1,12 @@ .gitignore -CMakeCache.txt -CMakeFiles -Makefile -tmp [Bb]uild* *.sw[po] -*.[ao] -*.orig +*.[aod] +*.so *~ +test* +!test*.[ch] +.config +.test tags - +*.pc diff --git a/Makefile b/Makefile @@ -0,0 +1,150 @@ +# Copyright (C) 2022, 2023 |Méso|Star> (contact@meso-star.com) +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. + +.POSIX: +.SUFFIXES: # Clean up default inference rules + +include config.mk + +LIBNAME_STATIC = libsars.a +LIBNAME_SHARED = libsars.so +LIBNAME = $(LIBNAME_$(LIB_TYPE)) + +################################################################################ +# Library building +################################################################################ +SRC = src/sars.c src/sars_log.c +OBJ = $(SRC:.c=.o) +DEP = $(SRC:.c=.d) + +build_library: .config $(DEP) + @$(MAKE) -fMakefile $$(for i in $(DEP); do echo -f $${i}; done) \ + $$(if [ -n "$(LIBNAME)" ]; then \ + echo "$(LIBNAME)"; \ + else \ + echo "$(LIBNAME_SHARED)"; \ + fi) + +$(DEP) $(OBJ): config.mk + +$(LIBNAME_SHARED): $(OBJ) + $(CC) $(CFLAGS_SO) $(RSYS_CFLAGS) -o $@ $(OBJ) $(LDFLAGS_SO) $(RSYS_LIBS) + +$(LIBNAME_STATIC): libsars.o + $(AR) -rc $@ $? + $(RANLIB) $@ + +libsars.o: $(OBJ) + $(LD) -r $(OBJ) -o $@ + $(OBJCOPY) $(OCPFLAGS) $@ + +.config: config.mk + @if ! $(PKG_CONFIG) --atleast-version $(RSYS_VERSION) rsys; then \ + echo "rsys $(RSYS_VERSION) not found" >&2; exit 1; fi + @echo "config done" > $@ + +.SUFFIXES: .c .d .o +.c.d: + @$(CC) $(CFLAGS_SO) $(RSYS_CFLAGS) -MM -MT "$(@:.d=.o) $@" $< -MF $@ + +.c.o: + $(CC) $(CFLAGS_SO) $(RSYS_CFLAGS) -DSARS_SHARED_BUILD -c $< -o $@ + +################################################################################ +# Installation +################################################################################ +pkg: + sed -e 's#@PREFIX@#$(PREFIX)#g'\ + -e 's#@VERSION@#$(VERSION)#g'\ + -e 's#@RSYS_VERSION@#$(RSYS_VERSION)#g'\ + sars.pc.in > sars.pc + +sars-local.pc: sars.pc.in + sed -e '1d'\ + -e 's#^includedir=.*#includedir=./src/#'\ + -e 's#^libdir=.*#libdir=./#'\ + -e 's#@VERSION@#$(VERSION)#g'\ + -e 's#@RSYS_VERSION@#$(RSYS_VERSION)#g'\ + sars.pc.in > $@ + +install: build_library pkg + @$(SHELL) make.sh install "$(DESTDIR)$(PREFIX)/lib" $(LIBNAME) + @$(SHELL) make.sh install "$(DESTDIR)$(PREFIX)/lib/pkgconfig" sars.pc + @$(SHELL) make.sh install "$(DESTDIR)$(PREFIX)/include/star" src/sars.h + @$(SHELL) make.sh install "$(DESTDIR)$(PREFIX)/share/doc/star-aerosol" COPYING README.md + @$(SHELL) make.sh install "$(DESTDIR)$(PREFIX)/share/man/man5" sars.5 + +uninstall: + rm -f "$(DESTDIR)$(PREFIX)/lib/$(LIBNAME)" + rm -f "$(DESTDIR)$(PREFIX)/lib/pkgconfig/sars.pc" + rm -f "$(DESTDIR)$(PREFIX)/share/doc/star-aerosol/COPYING" + rm -f "$(DESTDIR)$(PREFIX)/share/doc/star-aerosol/README.md" + rm -f "$(DESTDIR)$(PREFIX)/include/star/sars.h" + rm -f "$(DESTDIR)$(PREFIX)/share/man/man5/sars.5" + +################################################################################ +# Miscellaneous targets +################################################################################ +all: build_library build_tests + +clean: clean_test + rm -f $(OBJ) $(TEST_OBJ) $(LIBNAME) + rm -f .config .test libsars.o sars.pc sars-local.pc + +distclean: clean + rm -f $(DEP) $(TEST_DEP) + +lint: + shellcheck -o all make.sh + mandoc -Tlint -Wall sars.5 || [ $$? -le 1 ] + +################################################################################ +# Tests +################################################################################ +TEST_SRC =\ + src/test_sars.c\ + src/test_sars_load.c +TEST_OBJ = $(TEST_SRC:.c=.o) +TEST_DEP = $(TEST_SRC:.c=.d) + +PKG_CONFIG_LOCAL = PKG_CONFIG_PATH="./:$${PKG_CONFIG_PATH}" $(PKG_CONFIG) +SARS_CFLAGS = $$($(PKG_CONFIG_LOCAL) $(PCFLAGS) --cflags sars-local.pc) +SARS_LIBS = $$($(PKG_CONFIG_LOCAL) $(PCFLAGS) --libs sars-local.pc) + +build_tests: build_library $(TEST_DEP) .test + @$(MAKE) -fMakefile -f.test $$(for i in $(TEST_DEP); do echo -f"$${i}"; done) test_bin + +test: build_tests + @$(SHELL) make.sh run_test $(TEST_SRC) + +.test: Makefile + @$(SHELL) make.sh config_test $(TEST_SRC) > $@ + +clean_test: + $(SHELL) make.sh clean_test $(TEST_SRC) + rm -f test_file.sars + +$(TEST_DEP): config.mk sars-local.pc + @$(CC) $(CFLAGS_EXE) $(SARS_CFLAGS) $(RSYS_CFLAGS) \ + -MM -MT "$(@:.d=.o) $@" $(@:.d=.c) -MF $@ + +$(TEST_OBJ): config.mk sars-local.pc + $(CC) $(CFLAGS_EXE) $(SARS_CFLAGS) $(RSYS_CFLAGS) -c $(@:.o=.c) -o $@ + +test_sars: config.mk sars-local.pc $(LIBNAME) + $(CC) $(CFLAGS_EXE) -o $@ src/$@.o $(LDFLAGS_EXE) $(SARS_LIBS) $(RSYS_LIBS) + +test_sars_load: config.mk sars-local.pc $(LIBNAME) + $(CC) $(CFLAGS_EXE) -o $@ src/$@.o $(LDFLAGS_EXE) $(SARS_LIBS) $(RSYS_LIBS) -lm diff --git a/README.md b/README.md @@ -1,31 +1,38 @@ -# Star-Aerosol +# Star Aerosol -This C library loads the radiative properties of an aerosol saved wrt the -Star-Aerosol file format. +This C library loads the radiative properties of an aerosol saved in +sars format. See `sars.5` for the format specification. -## How to build +## Requirements -This library is compatible with 64-bits POSIX systems. It relies the -[CMake](http://www.cmake.org) and the -[RCMake](https://gitlab.com/vaplv/rcmake/) packages to build. It also depends -on the [RSys](https://gitlab.com/vaplv/rsys/) library. It optionally depends on -[scdoc](https://sr.ht/~sircmpwn/scdoc/) which, if available, is used to -generate the man page of the Star-Aerosol file format. +- C compiler +- POSIX make +- pkg-config +- [RSys](https://gitlab.com/vaplv/rsys) +- [mandoc](https://mandoc.bsd.lv) -First ensure that CMake is installed on your system. Then install the RCMake -package as well as the aforementioned prerequisites. Finally generate the -project from the `cmake/CMakeLists.txt` file by appending to the -`CMAKE_PREFIX_PATH` variable the install directories of its dependencies. The -resulting project can be edited, built, tested and installed as any CMake -project. Refer to the [CMake documentation](https://cmake.org/documentation) -for further informations on CMake. +## Installation -## Copyright notice +Edit config.mk as needed, then run: -Copyright (C) 2022, 2023 [|Méso|Star>](https://www.meso-star.com) (<contact@meso-star.com>). + make clean install + +## Release notes + +### Version 0.1 + +- Write the man page directly in mdoc's roff macros, instead of using + the intermediate scdoc source. +- Replace CMake by Makefile as build system. +- Update compiler and linker flags to increase the security and + robustness of generated binaries. +- Provide a pkg-config file to link the library as an external + dependency. ## License -Star-Aerosol is free software released under the GPL v3+ license: GNU GPL -version 3 or later. You are welcome to redistribute it under certain +Copyright (C) 2022, 2023 |Méso|Star> (contact@meso-star.com) + +Star-Aerosol is free software released under the GPL v3+ license: GNU +GPL version 3 or later. You are welcome to redistribute it under certain conditions; refer to the COPYING file for details. diff --git a/cmake/CMakeLists.txt b/cmake/CMakeLists.txt @@ -1,118 +0,0 @@ -# Copyright (C) 2022, 2023 |Méso|Star> (contact@meso-star.com) -# -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see <http://www.gnu.org/licenses/>. - -cmake_minimum_required(VERSION 3.1) -project(sars C) -enable_testing() - -set(SARS_SOURCE_DIR ${PROJECT_SOURCE_DIR}/../src) -option(NO_TEST "Do not build tests" OFF) - -################################################################################ -# Check dependencies -################################################################################ -find_package(RCMake 0.4 REQUIRED) -find_package(RSys 0.13 REQUIRED) - -set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${RCMAKE_SOURCE_DIR}) -include(rcmake) -include(rcmake_runtime) - -include_directories(${RSys_INCLUDE_DIR}) - -################################################################################ -# Configure and define targets -################################################################################ -set(VERSION_MAJOR 0) -set(VERSION_MINOR 0) -set(VERSION_PATCH 0) -set(VERSION ${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH}) - -set(SARS_FILES_SRC - sars.c - sars_log.c) -set(SARS_FILES_INC - sars_c.h - sars_log.h) -set(SARS_FILES_INC_API - sars.h) - -set(SARS_FILES_DOC COPYING README.md) - -# Prepend each file in the `SARS_FILES_<SRC|INC>' list by `SARS_SOURCE_DIR' -rcmake_prepend_path(SARS_FILES_SRC ${SARS_SOURCE_DIR}) -rcmake_prepend_path(SARS_FILES_INC ${SARS_SOURCE_DIR}) -rcmake_prepend_path(SARS_FILES_INC_API ${SARS_SOURCE_DIR}) -rcmake_prepend_path(SARS_FILES_DOC ${PROJECT_SOURCE_DIR}/../) - -add_library(sars SHARED ${SARS_FILES_SRC} ${SARS_FILES_INC} ${SARS_FILES_INC_API}) -target_link_libraries(sars RSys) - -if(CMAKE_COMPILER_IS_GNUCC) - target_link_libraries(sars m) -endif() - -set_target_properties(sars PROPERTIES - DEFINE_SYMBOL SARS_SHARED_BUILD - VERSION ${VERSION} - SOVERSION ${VERSION_MAJOR}) - -rcmake_setup_devel(sars StarAerosol ${VERSION} star/sars_version.h) - -################################################################################ -# Add tests -################################################################################ -if(NOT NO_TEST) - function(new_test _name) - add_executable(${_name} ${SARS_SOURCE_DIR}/${_name}.c) - target_link_libraries(${_name} sars RSys ${ARGN}) - add_test(${_name} ${_name}) - endfunction() - - new_test(test_sars) - new_test(test_sars_load) -endif() - -################################################################################ -# Man page -############################################################################### -find_program(SCDOC NAMES scdoc) -if(NOT SCDOC) - message(WARNING - "The `scdoc' program is missing. " - "The Star-Aerosol man page cannot be generated.") -else() - set(_src ${PROJECT_SOURCE_DIR}/../doc/sars.5.scd) - add_custom_command( - OUTPUT sars.5 - COMMAND ${SCDOC} < ${_src} > sars.5 - DEPENDS ${_src} - WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} - COMMENT "Buid ROFF man page sars.5" - VERBATIM) - add_custom_target(man-roff ALL DEPENDS sars.5) - install(FILES ${CMAKE_CURRENT_BINARY_DIR}/sars.5 DESTINATION share/man/man5) -endif() - -################################################################################ -# Define output & install directories -################################################################################ -install(TARGETS sars - ARCHIVE DESTINATION bin - LIBRARY DESTINATION lib - RUNTIME DESTINATION bin) -install(FILES ${SARS_FILES_INC_API} DESTINATION include/star) -install(FILES ${SARS_FILES_DOC} DESTINATION share/doc/star-aerosol) - diff --git a/config.mk b/config.mk @@ -0,0 +1,77 @@ +VERSION = 0.1.0 +PREFIX = /usr/local + +LIB_TYPE = SHARED +#LIB_TYPE = STATIC + +BUILD_TYPE = RELEASE +#BUILD_TYPE = DEBUG + +################################################################################ +# Tools +################################################################################ +AR = ar +CC = cc +LD = ld +OBJCOPY = objcopy +PKG_CONFIG = pkg-config +RANLIB = ranlib + +################################################################################ +# Dependencies +################################################################################ +PCFLAGS_SHARED = +PCFLAGS_STATIC = --static +PCFLAGS = $(PCFLAGS_$(LIB_TYPE)) + +RSYS_VERSION = 0.14 +RSYS_CFLAGS = $$($(PKG_CONFIG) $(PCFLAGS) --cflags rsys) +RSYS_LIBS = $$($(PKG_CONFIG) $(PCFLAGS) --libs rsys) + +################################################################################ +# Compilation options +################################################################################ +WFLAGS =\ + -Wall\ + -Wcast-align\ + -Wconversion\ + -Wextra\ + -Wmissing-declarations\ + -Wmissing-prototypes\ + -Wshadow + +CFLAGS_HARDENED =\ + -D_FORTIFY_SOURCES=2\ + -fcf-protection=full\ + -fstack-clash-protection\ + -fstack-protector-strong + +CFLAGS_COMMON =\ + -std=c89\ + -pedantic\ + -fvisibility=hidden\ + -fstrict-aliasing\ + $(CFLAGS_HARDENED)\ + $(WFLAGS) + +CFLAGS_DEBUG = -g $(CFLAGS_COMMON) +CFLAGS_RELEASE = -O2 -DNDEBUG $(CFLAGS_COMMON) +CFLAGS = $(CFLAGS_$(BUILD_TYPE)) + +CFLAGS_SO = $(CFLAGS) -fPIC +CFLAGS_EXE = $(CFLAGS) -fPIE + +################################################################################ +# Linker options +################################################################################ +LDFLAGS_HARDENED = -Wl,-z,relro,-z,now +LDFLAGS_DEBUG = $(LDFLAGS_HARDENED) +LDFLAGS_RELEASE = -s $(LDFLAGS_HARDENED) +LDFLAGS = $(LDFLAGS_$(BUILD_TYPE)) + +LDFLAGS_SO = $(LDFLAGS) -shared -Wl,--no-undefined +LDFLAGS_EXE = $(LDFLAGS) -pie + +OCPFLAGS_DEBUG = --localize-hidden +OCPFLAGS_RELEASE = --localize-hidden --strip-unneeded +OCPFLAGS = $(OCPFLAGS_$(BUILD_TYPE)) diff --git a/doc/sars.5.scd b/doc/sars.5.scd @@ -1,94 +0,0 @@ -sars(5) - -; Copyright (C) 2022, 2023 |Méso|Star> (contact@meso-star.com) -; -; This program is free software: you can redistribute it and/or modify -; it under the terms of the GNU General Public License as published by -; the Free Software Foundation, either version 3 of the License, or -; (at your option) any later version. -; -; This program is distributed in the hope that it will be useful, -; but WITHOUT ANY WARRANTY; without even the implied warranty of -; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -; GNU General Public License for more details. -; -; You should have received a copy of the GNU General Public License -; along with this program. If not, see <http://www.gnu.org/licenses/>. - -# NAME - -sars - Star AeoRoSol file format - -# DESCRIPTION - -*sars* is a binary file format for storing the radiative properties of an -aerosol. The volumetric mesh to which the CKs are attached is _not_ described -there but must be defined in a separated file via, for example, the *smsh*(5) -file format. - -An *sars* file begins by a header that describes the layout of the data. Next -the spectral bands on which the radiative coefficients were defined are listed. -Finally, the entire list of the scattering coefficients and absorption -coefficients are stored. - -The header consists of 3 64-bit integers. The first integer is a power of two -(usually 4096) that defines the _pagesize_ in bytes on which the radiative -coefficients are aligned. The remaining two integers store the number of bands -and the number of nodes, that is, the number of radiative coefficients per band -which is actually the number of nodes in the volumetric mesh to which these -coefficients are attached. - -After the header comes the list of spectral bands sorted in ascending order -relative to their interval. Each spectral band is defined by 2 double-precision -floating-point numbers that represent the lower and upper limits of the band in -nanometers. - -Fill bytes follow the list of spectral bands to ensure alignment of the -radiative coefficients on _pagesize_. By aligning data on _pagesize_, and -depending on system requirements, memory mapping can be used to automatically -load/unload the radiative coefficients on demand (see *mmap*(2)). For each band, -the absorption coefficient and diffusion coefficient are listed per node. This -list is followed by a _padding_, which is a list of bytes that provides memory -alignment of the following data to _pagesize_. Bands are sorted according to the -order in which they were previously declared. - -# BINARY FILE FORMAT - -Data are encoded with respect to the little endian bytes ordering, i.e. least -significant bytes are stored first. - -``` -<sars> ::= <pagesize> <#bands> <#nodes> - <bands> - <padding> - <rad-coefs> - -<pagesize> ::= UINT64 -<#bands> ::= UINT64 -<#nodes> ::= UINT64 -<padding> ::= [ BYTE ... ] #ensure alignment on <pagesize> - ---- - -<band-list> ::= <band> [ <band> ... ] -<band> ::= <band-low> <band-upp> - -<band-id> ::= UINT64 -<band-low> ::= DOUBLE # In nm (inclusive) -<band-upp> ::= DOUBLE # In nm (exclusive) - ---- - -<rad-coefs> ::= <per-band-k> - [ <per-band-k> ... ] - -<per-band-k> ::= <ka-ks> [ <ka-ks> ... ] <padding> - -<ka-ks> ::= <ka> <ks> -<ka> ::= FLOAT # in m^-1 -<ks> ::= FLOAT # in m^-1 -``` - -# SEE ALSO - -*mmap*(2), *smsh*(5) diff --git a/make.sh b/make.sh @@ -0,0 +1,71 @@ +#!/bin/sh + +# Copyright (C) 2022, 2023 |Méso|Star> (contact@meso-star.com) +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. + +set -e + +config_test() +{ + for i in "$@"; do + test=$(basename "${i}" ".c") + test_list="${test_list} ${test}" + printf "%s: %s\n" "${test}" "src/${test}.o" + done + printf "test_bin: %s\n" "${test_list}" +} + +run_test() +{ + for i in "$@"; do + test=$(basename "${i}" ".c") + + printf "%s " "${test}" + if "./${test}" > /dev/null 2>&1; then + printf "\033[1;32mOK\033[m\n" + else + printf "\033[1;31mError\033[m\n" + fi + done 2> /dev/null +} + + +clean_test() +{ + for i in "$@"; do + rm -f "$(basename "${i}" ".c")" + done +} + +install() +{ + prefix=$1 + shift 1 + + mkdir -p "${prefix}" + + for i in "$@"; do + dst="${prefix}/${i##*/}" + + if cmp -s "${i}" "${dst}"; then + printf "Up to date %s\n" "${dst}" + else + printf "Installing %s\n" "${dst}" + cp "${i}" "${prefix}" + fi + done +} + +"$@" diff --git a/sars.5 b/sars.5 @@ -0,0 +1,105 @@ +.\" Copyright (C) 2022, 2023 |Méso|Star> (contact@meso-star.com) +.\" +.\" This program is free software: you can redistribute it and/or modify +.\" it under the terms of the GNU General Public License as published by +.\" the Free Software Foundation, either version 3 of the License, or +.\" (at your option) any later version. +.\" +.\" This program is distributed in the hope that it will be useful, +.\" but WITHOUT ANY WARRANTY; without even the implied warranty of +.\" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +.\" GNU General Public License for more details. +.\" +.\" You should have received a copy of the GNU General Public License +.\" along with this program. If not, see <http://www.gnu.org/licenses/>. +.Dd July 28, 2023 +.Dt SARS 5 +.Os +.Sh NAME +.Nm sars +.Nd Star AeRoSol file format +.Sh DESCRIPTION +.Nm +is a binary file format for storing the radiative properties of an +aerosol. +The volumetric mesh to which the CKs are attached is +.Em not +described there but must be defined in a separated file via, for example, the +.Xr smsh 5 +file format. +.Pp +A +.Nm +file begins by a header that describes the layout of the data. +Next the spectral bands on which the radiative coefficients were defined are +listed. +Finally, the entire list of the scattering coefficients and absorption +coefficients are stored. +.Pp +The header consists of 3 64-bit integers. +The first integer is a power of two +.Pq usually 4096 +that defines the +.Va pagesize +in bytes on which the radiative coefficients are aligned. +The remaining two integers store the number of bands and the number of nodes, +that is, the number of radiative coefficients per band which is actually the +number of nodes in the volumetric mesh to which these coefficients are +attached. +.Pp +After the header comes the list of spectral bands sorted in ascending order +relative to their interval. +Each spectral band is defined by 2 double-precision floating-point numbers that +represent the lower and upper limits of the band in nanometers. +.Pp +Fill bytes follow the list of spectral bands to ensure alignment of the +radiative coefficients on +.Va pagesize . +By aligning data on +.Va pagesize , +and depending on system requirements, memory mapping can be used to +automatically load/unload the radiative coefficients on demand +.Pq see Xr mmap 2 . +For each band, the absorption coefficient and diffusion coefficient are listed +per node. +This list is followed by a +.Va padding , +which is a list of bytes that provides memory alignment of the following data +to +.Va pagesize . +Bands are sorted according to the order in which they were previously declared. +.Pp +Data are encoded with respect to the little endian bytes ordering, i.e. least +significant bytes are stored first. +.Pp +The file format is as follows: +.Bl -column (per-band-k) (::=) () +.It Ao Va sars Ac Ta ::= Ta Ao Va pagesize Ac Ao Va #bands Ac Ao Va #nodes Ac +.It Ta Ta Aq Va bands +.It Ta Ta Aq Va padding +.It Ta Ta Aq Va rad-coefs +.It \ Ta Ta +.It Ao Va pagesize Ac Ta ::= Ta Vt uint64_t +.It Ao Va #bands Ac Ta ::= Ta Vt uint64_t +.It Ao Va #nodes Ac Ta ::= Ta Vt uint64_t +.It Ao Va padding Ac Ta ::= Ta Op Va int8_t ... +# Ensure alignment on +.Va pagesize +.It \ Ta Ta +.It Ao Va bands Ac Ta ::= Ta Ao Va band Ac Va ... +.It Ao Va band Ac Ta ::= Ta Ao Va band-low Ac Ao Va band-upp Ac +.It Ao Va band-low Ac Ta ::= Ta Vt double +# Inclusive bound in nm +.It Ao Va band-upp Ac Ta ::= Ta Vt double +# Excusive bound in nm +.It \ Ta Ta +.It Ao Va rad-coefs Ac Ta ::= Ta Ao Va per-band-k Ac Va ... +.It Ao Va ka-ks Ac Ta ::= Ta Ao Va ka Ac Ao Va ks Ac +.It Ao Va ka Ac Ta ::= Ta Vt float +# In m^-1 +.It Ao Va ks Ac Ta ::= Ta Vt float +# In m^-1 +.El +.Sh SEE ALSO +.Xr mmap 2 , +.Xr smsh 5 diff --git a/sars.pc.in b/sars.pc.in @@ -0,0 +1,10 @@ +prefix=@PREFIX@ +includedir=${prefix}/include +libdir=${prefix}/lib + +Requires: rsys >= @RSYS_VERSION@ +Name: Star-AeRoSol +Description: Star Aerosol library +Version: @VERSION@ +Libs: -L${libdir} -lsars +CFlags: -I${includedir} diff --git a/src/sars.c b/src/sars.c @@ -22,16 +22,29 @@ #include "sars_log.h" #include <rsys/algorithm.h> +#include <rsys/cstr.h> #include <rsys/hash.h> #include <unistd.h> /* sysconf support */ #include <errno.h> #include <sys/mman.h> /* mmap */ +#include <sys/stat.h> /* fstat */ /******************************************************************************* * Helper functions ******************************************************************************/ +static INLINE int +is_stdin(FILE* stream) +{ + struct stat stream_buf; + struct stat stdin_buf; + ASSERT(stream); + CHK(fstat(fileno(stream), &stream_buf) == 0); + CHK(fstat(STDIN_FILENO, &stdin_buf) == 0); + return stream_buf.st_dev == stdin_buf.st_dev; +} + static INLINE res_T check_sars_create_args(const struct sars_create_args* args) { @@ -39,6 +52,28 @@ check_sars_create_args(const struct sars_create_args* args) return args ? RES_OK : RES_BAD_ARG; } +static INLINE res_T +check_sars_load_args(const struct sars_load_args* args) +{ + if(!args || !args->path) return RES_BAD_ARG; + return RES_OK; +} + +static INLINE res_T +check_sars_load_stream_args + (struct sars* sars, + const struct sars_load_stream_args* args) +{ + if(!args || !args->stream || !args->name) return RES_BAD_ARG; + if(args->memory_mapping && is_stdin(args->stream)) { + log_err(sars, + "%s: unable to use memory mapping on data loaded from stdin\n", + args->name); + return RES_BAD_ARG; + } + return RES_OK; +} + static void reset_sars(struct sars* sars) { @@ -52,20 +87,20 @@ static res_T read_band (struct sars* sars, struct band* band, - FILE* stream, - const char* stream_name) + FILE* stream) { size_t iband; res_T res = RES_OK; - ASSERT(sars && band && stream_name); + ASSERT(sars && band); + band->sars = sars; iband = (size_t)(band - darray_band_cdata_get(&sars->bands)); /* Read band definition */ #define READ(Var, Name) { \ if(fread((Var), sizeof(*(Var)), 1, stream) != 1) { \ log_err(sars, "%s: band %lu: could not read the %s.\n", \ - stream_name, (unsigned long)iband, (Name)); \ + sars_get_name(sars), (unsigned long)iband, (Name)); \ res = RES_IO_ERR; \ goto error; \ } \ @@ -78,7 +113,7 @@ read_band if(band->low < 0 || band->low >= band->upp) { log_err(sars, "%s: band %lu: invalid band range [%g, %g[.\n", - stream_name, (unsigned long)iband, band->low, band->upp); + sars_get_name(sars), (unsigned long)iband, band->low, band->upp); res = RES_BAD_ARG; goto error; } @@ -89,23 +124,223 @@ error: goto exit; } +static res_T +map_data + (struct sars* sars, + const int fd, /* File descriptor */ + const size_t filesz, /* Overall filesize */ + const char* data_name, + const off_t offset, /* Offset of the data into file */ + const size_t map_len, + void** out_map) /* Lenght of the data to map */ +{ + void* map = NULL; + res_T res = RES_OK; + ASSERT(sars && filesz && data_name && map_len && out_map); + ASSERT(IS_ALIGNED((size_t)offset, (size_t)sars->pagesize)); + + if((size_t)offset + map_len > filesz) { + log_err(sars, "%s: the %s to map exceed the file size\n", + sars_get_name(sars), data_name); + res = RES_IO_ERR; + goto error; + } + + map = mmap(NULL, map_len, PROT_READ, MAP_PRIVATE|MAP_POPULATE, fd, offset); + if(map == MAP_FAILED) { + log_err(sars, "%s: could not map the %s -- %s\n", + sars_get_name(sars), data_name, strerror(errno)); + res = RES_IO_ERR; + goto error; + } + +exit: + *out_map = map; + return res; +error: + if(map == MAP_FAILED) map = NULL; + goto exit; +} static res_T -load_stream(struct sars* sars, FILE* stream, const char* stream_name) +map_file(struct sars* sars, FILE* stream) { + size_t filesz; size_t map_len; size_t iband; + size_t nbands; + off_t offset; + res_T res = RES_OK; + ASSERT(sars && stream); + + /* Compute the length in bytes of the k to map for each band/quadrature point */ + map_len = ALIGN_SIZE(sars->nnodes * sizeof(float)*2, sars->pagesize); + + /* Compute the offset toward the 1st list of radiative coefficients */ + offset = ftell(stream); + offset = (off_t)ALIGN_SIZE((uint64_t)offset, sars->pagesize); + + /* Retrieve the overall filesize */ + fseek(stream, 0, SEEK_END); + filesz = (size_t)ftell(stream); + + nbands = sars_get_bands_count(sars); + FOR_EACH(iband, 0, nbands) { + struct band* band = NULL; + + band = darray_band_data_get(&sars->bands) + iband; + band->map_len = map_len; + + /* Mapping per band radiative coefficients */ + res = map_data(sars, fileno(stream), filesz, "radiative coefficients", + offset, band->map_len, (void**)&band->k_list); + if(res != RES_OK) { + log_err(sars, + "%s: data mapping error for band %lu\n", + sars_get_name(sars), (unsigned long)iband); + res = RES_IO_ERR; + goto error; + } + + offset = (off_t)((size_t)offset + map_len); + } + +exit: + return res; +error: + goto exit; +} + +static res_T +read_padding(FILE* stream, const size_t padding) +{ + char chunk[1024]; + size_t remaining_nbytes = padding; + + while(remaining_nbytes) { + const size_t nbytes = MMIN(sizeof(chunk), remaining_nbytes); + if(fread(chunk, 1, nbytes, stream) != nbytes) return RES_IO_ERR; + remaining_nbytes -= nbytes; + } + return RES_OK; +} + +/* Return the size in bytes of the data layout and band descriptors */ +static INLINE size_t +compute_sizeof_header(struct sars* sars) +{ + size_t sizeof_header = 0; + ASSERT(sars); + + sizeof_header = + sizeof(uint64_t) /* pagesize */ + + sizeof(uint64_t) /* #bands */ + + sizeof(uint64_t) /* #nodes */ + + sizeof(double[2]) * sars_get_bands_count(sars); /* Bands */ + return sizeof_header; +} + +static res_T +load_data + (struct sars* sars, + FILE* stream, + const char* data_name, + float** out_data) +{ + float* data = NULL; + res_T res = RES_OK; + ASSERT(sars && stream && data_name && out_data); + + data = MEM_ALLOC(sars->allocator, sizeof(float[2]/*ka and ks*/)*sars->nnodes); + if(!data) { + res = RES_MEM_ERR; + log_err(sars, "%s: could not allocate the %s -- %s\n", + sars_get_name(sars), data_name, res_to_cstr(res)); + goto error; + } + + if(fread(data, sizeof(float[2]), sars->nnodes, stream) != sars->nnodes) { + res = RES_IO_ERR; + log_err(sars, "%s: could not read the %s -- %s\n", + sars_get_name(sars), data_name, res_to_cstr(res)); + goto error; + } + +exit: + *out_data = data; + return res; +error: + if(data) { MEM_RM(sars->allocator, data); data = NULL; } + goto exit; +} + +static res_T +load_file(struct sars* sars, FILE* stream) +{ + size_t sizeof_header; + size_t sizeof_k_list; + size_t padding_bytes; + size_t iband; + size_t nbands; + res_T res = RES_OK; + ASSERT(sars && stream); + + sizeof_header = compute_sizeof_header(sars); + sizeof_k_list = sizeof(float[2])*sars->nnodes; + + padding_bytes = ALIGN_SIZE(sizeof_header, sars->pagesize) - sizeof_header; + if((res = read_padding(stream, padding_bytes)) != RES_OK) goto error; + + /* Calculate the padding between the lists of radiative coefficients. Note + * that this padding is the same between each list */ + padding_bytes = ALIGN_SIZE(sizeof_k_list, sars->pagesize) - sizeof_k_list; + + nbands = sars_get_bands_count(sars); + FOR_EACH(iband, 0, nbands) { + struct band* band = NULL; + + band = darray_band_data_get(&sars->bands) + iband; + ASSERT(!band->k_list && band->sars == sars); + + /* Loading per band scattering coefficients */ + res = load_data(sars, stream, "radiative coefficients", &band->k_list); + if(res != RES_OK) { + log_err(sars, + "%s: data loading error for band %lu\n", + sars_get_name(sars), (unsigned long)iband); + goto error; + } + + if((res = read_padding(stream, padding_bytes)) != RES_OK) goto error; + } + +exit: + return res; +error: + goto exit; +} + +static res_T +load_stream(struct sars* sars, const struct sars_load_stream_args* args) +{ + size_t iband; uint64_t nbands; - off_t offset = 0; res_T res = RES_OK; - ASSERT(sars && stream && stream_name); + ASSERT(sars && check_sars_load_stream_args(sars, args) == RES_OK); reset_sars(sars); + res = str_set(&sars->name, args->name); + if(res != RES_OK) { + log_err(sars, "%s: unable to duplicate path to loaded data or stream name\n", + args->name); + goto error; + } + /* Read file header */ #define READ(Var, Name) { \ - if(fread((Var), sizeof(*(Var)), 1, stream) != 1) { \ - log_err(sars, "%s: could not read the %s.\n", stream_name, (Name)); \ + if(fread((Var), sizeof(*(Var)), 1, args->stream) != 1) { \ + log_err(sars, "%s: could not read the %s.\n", sars_get_name(sars), (Name));\ res = RES_IO_ERR; \ goto error; \ } \ @@ -120,7 +355,7 @@ load_stream(struct sars* sars, FILE* stream, const char* stream_name) log_err(sars, "%s: invalid page size %lu. The page size attribute must be aligned on " "the page size of the operating system (%lu).\n", - stream_name, + sars_get_name(sars), (unsigned long)sars->pagesize, (unsigned long)sars->pagesize_os); res = RES_BAD_ARG; @@ -128,13 +363,13 @@ load_stream(struct sars* sars, FILE* stream, const char* stream_name) } if(!nbands) { log_err(sars, "%s: invalid number of bands %lu.\n", - stream_name, (unsigned long)nbands); + sars_get_name(sars), (unsigned long)nbands); res = RES_BAD_ARG; goto error; } if(!sars->nnodes) { log_err(sars, "%s: invalid number of nodes %lu.\n", - stream_name, (unsigned long)sars->nnodes); + sars_get_name(sars), (unsigned long)sars->nnodes); res = RES_BAD_ARG; goto error; } @@ -143,20 +378,20 @@ load_stream(struct sars* sars, FILE* stream, const char* stream_name) res = darray_band_resize(&sars->bands, nbands); if(res != RES_OK) { log_err(sars, "%s: could not allocate the list of bands (#bands=%lu).\n", - stream_name, (unsigned long)nbands); + sars_get_name(sars), (unsigned long)nbands); goto error; } /* Read the band description */ FOR_EACH(iband, 0, nbands) { struct band* band = darray_band_data_get(&sars->bands) + iband; - res = read_band(sars, band, stream, stream_name); + res = read_band(sars, band, args->stream); if(res != RES_OK) goto error; if(iband > 0 && band[0].low < band[-1].upp) { log_err(sars, "%s: bands must be sorted in ascending order and must not " "overlap (band %lu in [%g, %g[ nm; band %lu in [%g, %g[ nm).\n", - stream_name, + sars_get_name(sars), (unsigned long)(iband-1), band[-1].low, band[-1].upp, (unsigned long)(iband), band[ 0].low, band[ 0].upp); res = RES_BAD_ARG; @@ -164,30 +399,12 @@ load_stream(struct sars* sars, FILE* stream, const char* stream_name) } } - /* Compute the length in bytes of the k to map for each band/quadrature point */ - map_len = ALIGN_SIZE(sars->nnodes * sizeof(float)*2, sars->pagesize); - - /* Compute the offset toward the 1st list of radiative coefficients */ - offset = ftell(stream); - offset = (off_t)ALIGN_SIZE((uint64_t)offset, sars->pagesize); - - FOR_EACH(iband, 0, nbands) { - struct band* band = NULL; - - band = darray_band_data_get(&sars->bands) + iband; - band->map_len = map_len; - - /* Map the per band scattering coefficient */ - band->k_list = mmap(NULL, band->map_len, PROT_READ, - MAP_PRIVATE|MAP_POPULATE, fileno(stream), offset); - if(band->k_list == MAP_FAILED) { - log_err(sars, - "%s: band %lu: could not map the radiative coefficients -- %s\n", - stream_name, (unsigned long)iband, strerror(errno)); - res = RES_IO_ERR; - goto error; - } - offset = (off_t)((size_t)offset + map_len); + if(args->memory_mapping) { + res = map_file(sars, args->stream); + if(res != RES_OK) goto error; + } else { + res = load_file(sars, args->stream); + if(res != RES_OK) goto error; } exit: @@ -233,6 +450,7 @@ release_sars(ref_T* ref) ASSERT(ref); sars = CONTAINER_OF(ref, struct sars, ref); if(sars->logger == &sars->logger__) logger_release(&sars->logger__); + str_release(&sars->name); darray_band_release(&sars->bands); MEM_RM(sars->allocator, sars); } @@ -273,6 +491,7 @@ sars_create sars->allocator = allocator; sars->verbose = args->verbose; sars->pagesize_os = (size_t)sysconf(_SC_PAGESIZE); + str_init(allocator, &sars->name); darray_band_init(allocator, &sars->bands); if(args->logger) { sars->logger = args->logger; @@ -305,24 +524,27 @@ sars_ref_put(struct sars* sars) } res_T -sars_load(struct sars* sars, const char* path) +sars_load(struct sars* sars, const struct sars_load_args* args) { + struct sars_load_stream_args stream_args = SARS_LOAD_STREAM_ARGS_NULL; FILE* file = NULL; res_T res = RES_OK; - if(!sars || !path) { - res = RES_BAD_ARG; - goto error; - } + if(!sars) { res = RES_BAD_ARG; goto error; } + res = check_sars_load_args(args); + if(res != RES_OK) goto error; - file = fopen(path, "r"); + file = fopen(args->path, "r"); if(!file) { - log_err(sars, "%s: error opening file `%s'.\n", FUNC_NAME, path); + log_err(sars, "%s: error opening file `%s'.\n", FUNC_NAME, args->path); res = RES_IO_ERR; goto error; } - res = load_stream(sars, file, path); + stream_args.stream = file; + stream_args.name = args->path; + stream_args.memory_mapping = args->memory_mapping; + res = load_stream(sars, &stream_args); if(res != RES_OK) goto error; exit: @@ -333,13 +555,59 @@ error: } res_T -sars_load_stream - (struct sars* sars, - FILE* stream, - const char* stream_name) +sars_load_stream(struct sars* sars, const struct sars_load_stream_args* args) { - if(!sars || !stream) return RES_BAD_ARG; - return load_stream(sars, stream, stream_name ? stream_name : "<stream>"); + res_T res = RES_OK; + if(!sars) return RES_BAD_ARG; + res = check_sars_load_stream_args(sars, args); + if(res != RES_OK) return res; + return load_stream(sars, args); +} + +res_T +sars_validate(const struct sars* sars) +{ + size_t iband; + size_t nbands; + if(!sars) return RES_BAD_ARG; + + nbands = sars_get_bands_count(sars); + FOR_EACH(iband, 0, nbands) { + struct sars_band band = SARS_BAND_NULL; + size_t inode; + size_t nnodes; + + SARS(get_band(sars, iband, &band)); + + /* Check band limits */ + if(band.lower != band.lower /* NaN? */ + || band.upper != band.upper) { /* NaN? */ + log_err(sars, + "%s: invalid limits for band %lu: [%g, %g[\n", + sars_get_name(sars), (unsigned long)iband, band.lower, band.upper); + return RES_BAD_ARG; + } + + /* Check radiative coefficients */ + nnodes = sars_get_nodes_count(sars); + FOR_EACH(inode, 0, nnodes) { + const float ka = sars_band_get_ka(&band, inode); + const float ks = sars_band_get_ks(&band, inode); + if(ka != ka /* NaN? */ || ka < 0) { + log_err(sars, + "%s: invalid absorption coefficient for band %lu at node %lu: %g\n", + sars_get_name(sars), (unsigned long)iband, (unsigned long)inode, ka); + return RES_BAD_ARG; + } + if(ks != ks /* NaN? */ || ks < 0) { + log_err(sars, + "%s: invalid scattering coefficient for band %lu at node %lu: %g\n", + sars_get_name(sars), (unsigned long)iband, (unsigned long)inode, ka); + return RES_BAD_ARG; + } + } + } + return RES_OK; } size_t @@ -513,6 +781,13 @@ error: goto exit; } +const char* +sars_get_name(const struct sars* sars) +{ + ASSERT(sars); + return str_cget(&sars->name); +} + /******************************************************************************* * Local functions ******************************************************************************/ @@ -520,7 +795,47 @@ void band_release(struct band* band) { ASSERT(band); - if(band->k_list && band->k_list != MAP_FAILED) { + if(!band->k_list) return; + + if(!band->map_len) { + MEM_RM(band->sars->allocator, band->k_list); + } else if(band->k_list != MAP_FAILED) { munmap(band->k_list, band->map_len); } } + +res_T +band_copy(struct band* dst, const struct band* src) +{ + ASSERT(dst && src); + + dst->sars = src->sars; + dst->low = src->low; + dst->upp = dst->upp; + dst->map_len = src->map_len; + dst->k_list = NULL; + + if(src->map_len) { + /* The k are mapped: copy the pointer */ + dst->k_list = src->k_list; + } else if(src->k_list != NULL) { + /* The k are loaded: duplicate thable contents */ + const size_t memsz = sizeof(*dst->k_list)*src->sars->nnodes*2/*ka & ks*/; + dst->k_list = MEM_ALLOC(src->sars->allocator, memsz); + if(!dst->k_list) return RES_MEM_ERR; + memcpy(dst->k_list, src->k_list, memsz); + } + return RES_OK; +} + +res_T +band_copy_and_release(struct band* dst, struct band* src) +{ + ASSERT(dst && src); + dst->sars = src->sars; + dst->low = src->low; + dst->upp = dst->upp; + dst->map_len = src->map_len; + dst->k_list = src->k_list; + return RES_OK; +} diff --git a/src/sars.h b/src/sars.h @@ -50,6 +50,24 @@ struct sars_create_args { static const struct sars_create_args SARS_CREATE_ARGS_DEFAULT = SARS_CREATE_ARGS_DEFAULT__; +struct sars_load_args { + const char* path; + int memory_mapping; /* Use memory mapping instead of normal loading */ +}; +#define SARS_LOAD_ARGS_NULL__ {NULL, 0} +static const struct sars_load_args SARS_LOAD_ARGS_NULL = SARS_LOAD_ARGS_NULL__; + +struct sars_load_stream_args { + FILE* stream; + const char* name; /* Stream name */ + /* Use memory mapping instead of normal loading. Note that memory mapping + * cannot be used on some stream like stdin */ + int memory_mapping; +}; +#define SARS_LOAD_STREAM_ARGS_NULL__ {NULL, "stream", 0} +static const struct sars_load_stream_args SARS_LOAD_STREAM_ARGS_NULL = + SARS_LOAD_STREAM_ARGS_NULL__; + struct sars_band { double lower; /* Lower band wavelength in nm (inclusive) */ double upper; /* Upper band wavelength in nm (exclusive) */ @@ -84,13 +102,23 @@ sars_ref_put SARS_API res_T sars_load (struct sars* sars, - const char* path); + const struct sars_load_args* args); SARS_API res_T sars_load_stream (struct sars* sars, - FILE* stream, - const char* stream_name); /* Can be NULL */ + const struct sars_load_stream_args* args); + +/* Validates radiative coefficients. Data checks have already been carried out + * during loading, notably on spectral bands, but this function performs longer + * and more thorough tests. It reviews all scattering and absorption + * coefficients to check their validity, i.e. whether they are positive or zero. + * Note that checking radiative coefficients is not mandatory, in order to speed + * up the loading step and avoid loading/unloading them when using memory + * mapping. */ +SARS_API res_T +sars_validate + (const struct sars* sars); SARS_API size_t sars_get_bands_count @@ -126,6 +154,10 @@ sars_compute_hash (const struct sars* sars, hash256_T hash); +SARS_API const char* +sars_get_name + (const struct sars* sars); + static INLINE float sars_band_get_ka(const struct sars_band* band, const size_t inode) { diff --git a/src/sars_c.h b/src/sars_c.h @@ -19,8 +19,10 @@ #include <rsys/dynamic_array.h> #include <rsys/logger.h> #include <rsys/ref_count.h> +#include <rsys/str.h> struct band { + struct sars* sars; double low; /* Lower bound in nm (inclusive) */ double upp; /* Upper bound in nm (exclusive) */ size_t map_len; @@ -32,6 +34,7 @@ band_init(struct mem_allocator* allocator, struct band* band) { ASSERT(band); (void)allocator; + band->sars = NULL; band->low = DBL_MAX; band->upp =-DBL_MAX; band->map_len = 0; @@ -42,11 +45,23 @@ extern LOCAL_SYM void band_release (struct band* band); +extern LOCAL_SYM res_T +band_copy + (struct band* dst, + const struct band* src); + +extern LOCAL_SYM res_T +band_copy_and_release + (struct band* dst, + struct band* src); + /* Generate the dynamic array of bands */ #define DARRAY_NAME band #define DARRAY_DATA struct band #define DARRAY_FUNCTOR_INIT band_init #define DARRAY_FUNCTOR_RELEASE band_release +#define DARRAY_FUNCTOR_COPY band_copy +#define DARRAY_FUNCTOR_COPY_AND_RELEASE band_copy_and_release #include <rsys/dynamic_array.h> struct mem_allocator; @@ -58,6 +73,7 @@ struct sars { struct darray_band bands; size_t pagesize_os; + struct str name; struct mem_allocator* allocator; struct logger* logger; diff --git a/src/test_sars_load.c b/src/test_sars_load.c @@ -19,6 +19,7 @@ #include <rsys/hash.h> #include <rsys/mem_allocator.h> #include <math.h> +#include <string.h> /******************************************************************************* * Helper functions @@ -114,6 +115,9 @@ test_load(struct sars* sars) hash256_T hash1; hash256_T band_hash0; hash256_T band_hash1; + struct sars_load_args args = SARS_LOAD_ARGS_NULL; + struct sars_load_stream_args stream_args = SARS_LOAD_STREAM_ARGS_NULL; + FILE* fp = NULL; const char* filename = "test_file.sars"; const uint64_t pagesize = 16384; @@ -124,13 +128,16 @@ test_load(struct sars* sars) write_sars(fp, pagesize, nbands, nnodes); rewind(fp); - CHK(sars_load_stream(NULL, fp, filename) == RES_BAD_ARG); - CHK(sars_load_stream(sars, NULL, filename) == RES_BAD_ARG); - CHK(sars_load_stream(sars, fp, NULL) == RES_OK); - check_sars_load(sars, nbands, nnodes); + stream_args.stream =fp; + stream_args.name = filename; + CHK(sars_load_stream(NULL, &stream_args) == RES_BAD_ARG); + CHK(sars_load_stream(sars, NULL) == RES_BAD_ARG); + stream_args.stream = NULL; + CHK(sars_load_stream(sars, &stream_args) == RES_BAD_ARG); + stream_args.stream = fp; + CHK(sars_load_stream(sars, &stream_args) == RES_OK); - rewind(fp); - CHK(sars_load_stream(sars, fp, filename) == RES_OK); + CHK(!strcmp(sars_get_name(sars), filename)); CHK(sars_compute_hash(NULL, hash0) == RES_BAD_ARG); CHK(sars_compute_hash(sars, NULL) == RES_BAD_ARG); @@ -145,10 +152,23 @@ test_load(struct sars* sars) CHK(sars_band_compute_hash(sars, 0, band_hash1) == RES_OK); CHK(hash256_eq(band_hash0, band_hash1)); - CHK(sars_load(NULL, filename) == RES_BAD_ARG); + check_sars_load(sars, nbands, nnodes); + rewind(fp); + + stream_args.name = NULL; + CHK(sars_load_stream(sars, &stream_args) == RES_BAD_ARG); + stream_args.name = SARS_LOAD_STREAM_ARGS_NULL.name; + stream_args.memory_mapping = 1; + CHK(sars_load_stream(sars, &stream_args) == RES_OK); + CHK(!strcmp(sars_get_name(sars), SARS_LOAD_STREAM_ARGS_NULL.name)); + check_sars_load(sars, nbands, nnodes); + + args.path = "nop"; + CHK(sars_load(NULL, &args) == RES_BAD_ARG); CHK(sars_load(sars, NULL) == RES_BAD_ARG); - CHK(sars_load(sars, "nop") == RES_IO_ERR); - CHK(sars_load(sars, filename) == RES_OK); + CHK(sars_load(sars, &args) == RES_IO_ERR); + args.path = filename; + CHK(sars_load(sars, &args) == RES_OK); check_sars_load(sars, nbands, nnodes); CHK(sars_compute_hash(sars, hash1) == RES_OK); @@ -158,7 +178,9 @@ test_load(struct sars* sars) write_sars(fp, pagesize, nbands+1, nnodes); rewind(fp); - CHK(sars_load_stream(sars, fp, filename) == RES_OK); + stream_args.stream = fp; + stream_args.name = filename; + CHK(sars_load_stream(sars, &stream_args) == RES_OK); CHK(sars_compute_hash(sars, hash1) == RES_OK); CHK(!hash256_eq(hash0, hash1)); @@ -171,6 +193,7 @@ test_load(struct sars* sars) static void test_load_fail(struct sars* sars) { + struct sars_load_stream_args stream_args = SARS_LOAD_STREAM_ARGS_NULL; FILE* fp = NULL; double low; double upp; @@ -179,28 +202,32 @@ test_load_fail(struct sars* sars) CHK(fp = tmpfile()); write_sars(fp, 2048, 1, 1); rewind(fp); - CHK(sars_load_stream(sars, fp, NULL) == RES_BAD_ARG); + stream_args.stream = fp; + CHK(sars_load_stream(sars, &stream_args) == RES_BAD_ARG); CHK(fclose(fp) == 0); /* The pagesize is not a power of two */ CHK(fp = tmpfile()); write_sars(fp, 4100, 1, 1); rewind(fp); - CHK(sars_load_stream(sars, fp, NULL) == RES_BAD_ARG); + stream_args.stream = fp; + CHK(sars_load_stream(sars, &stream_args) == RES_BAD_ARG); CHK(fclose(fp) == 0); /* Wrong #bands */ CHK(fp = tmpfile()); write_sars(fp, 4096, 0, 1); rewind(fp); - CHK(sars_load_stream(sars, fp, NULL) == RES_BAD_ARG); + stream_args.stream = fp; + CHK(sars_load_stream(sars, &stream_args) == RES_BAD_ARG); CHK(fclose(fp) == 0); /* Wrong #nodes */ CHK(fp = tmpfile()); write_sars(fp, 4096, 1, 0); rewind(fp); - CHK(sars_load_stream(sars, fp, NULL) == RES_BAD_ARG); + stream_args.stream = fp; + CHK(sars_load_stream(sars, &stream_args) == RES_BAD_ARG); CHK(fclose(fp) == 0); /* Wrong band boundaries */ @@ -212,7 +239,8 @@ test_load_fail(struct sars* sars) CHK(fwrite(&low, sizeof(low), 1, fp) == 1); CHK(fwrite(&upp, sizeof(upp), 1, fp) == 1); rewind(fp); - CHK(sars_load_stream(sars, fp, NULL) == RES_BAD_ARG); + stream_args.stream = fp; + CHK(sars_load_stream(sars, &stream_args) == RES_BAD_ARG); CHK(fclose(fp) == 0); /* Unsorted bands */ @@ -226,7 +254,8 @@ test_load_fail(struct sars* sars) CHK(fwrite(&low, sizeof(low), 1, fp) == 1); CHK(fwrite(&upp, sizeof(upp), 1, fp) == 1); rewind(fp); - CHK(sars_load_stream(sars, fp, NULL) == RES_BAD_ARG); + stream_args.stream = fp; + CHK(sars_load_stream(sars, &stream_args) == RES_BAD_ARG); CHK(fclose(fp) == 0); /* Bands overlap */ @@ -240,7 +269,8 @@ test_load_fail(struct sars* sars) CHK(fwrite(&low, sizeof(low), 1, fp) == 1); CHK(fwrite(&upp, sizeof(upp), 1, fp) == 1); rewind(fp); - CHK(sars_load_stream(sars, fp, NULL) == RES_BAD_ARG); + stream_args.stream = fp; + CHK(sars_load_stream(sars, &stream_args) == RES_BAD_ARG); CHK(fclose(fp) == 0); } @@ -255,9 +285,22 @@ test_load_files(struct sars* sars, int argc, char** argv) size_t nbands; size_t iband; - printf("Load %s\n", argv[1]); - - CHK(sars_load(sars, argv[i]) == RES_OK); + if(!strcmp(argv[i], "-")) { /* Read from stdin */ + struct sars_load_stream_args args = SARS_LOAD_STREAM_ARGS_NULL; + printf("Load from stdin\n"); + args.stream = stdin; + args.name = "stdin"; + args.memory_mapping = 1; + CHK(sars_load_stream(sars, &args) == RES_BAD_ARG); + args.memory_mapping = 0; + CHK(sars_load_stream(sars, &args) == RES_OK); + } else { + struct sars_load_args args = SARS_LOAD_ARGS_NULL; + printf("Load %s\n", argv[1]); + args.path = argv[i]; + args.memory_mapping = 1; + CHK(sars_load(sars, &args) == RES_OK); + } nbands = sars_get_bands_count(sars); nnodes = sars_get_nodes_count(sars); CHK(nbands); @@ -265,25 +308,14 @@ test_load_files(struct sars* sars, int argc, char** argv) FOR_EACH(iband, 0, nbands) { struct sars_band band = SARS_BAND_NULL; - size_t inode; CHK(sars_get_band(sars, iband, &band) == RES_OK); printf("band %lu in [%g, %g[ nm\n", (unsigned long)band.id, band.lower, band.upper); - - CHK(band.lower == band.lower); /* !NaN */ - CHK(band.upper == band.upper); /* !NaN */ - CHK(band.lower < band.upper); - - FOR_EACH(inode, 0, nnodes) { - const float ka = sars_band_get_ka(&band, inode); - const float ks = sars_band_get_ks(&band, inode); - CHK(ka == ka); /* !NaN */ - CHK(ks == ks); /* !NaN */ - } } + CHK(sars_validate(sars) == RES_OK); CHK(sars_compute_hash(sars, hash) == RES_OK); } } @@ -291,6 +323,7 @@ test_load_files(struct sars* sars, int argc, char** argv) static void test_find(struct sars* sars) { + struct sars_load_stream_args stream_args = SARS_LOAD_STREAM_ARGS_NULL; size_t ibands[2]; double range[2]; FILE* fp; @@ -298,7 +331,8 @@ test_find(struct sars* sars) CHK(fp = tmpfile()); write_sars(fp, 4096, 10, 1); rewind(fp); - CHK(sars_load_stream(sars, fp, NULL) == RES_OK); + stream_args.stream = fp; + CHK(sars_load_stream(sars, &stream_args) == RES_OK); range[0] = 0; range[1] = 10;