star-line

Structure for accelerating line importance sampling
git clone git://git.meso-star.fr/star-line.git
Log | Files | Refs | README | LICENSE

commit ba4fc327cdb8e4a7d0b2dc0aaa33b830fdeb5ded
parent 6989e39beb6326cfe06c5afdcae00aca8b87abe6
Author: Vincent Forest <vincent.forest@meso-star.com>
Date:   Tue,  8 Mar 2022 15:15:24 +0100

Implement the API of the sln_device data structure

Diffstat:
Acmake/CMakeLists.txt | 99+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/sln.h | 75+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/sln_device.c | 101+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/sln_device_c.h | 35+++++++++++++++++++++++++++++++++++
Asrc/sln_log.c | 127+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/sln_log.h | 71+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
6 files changed, 508 insertions(+), 0 deletions(-)

diff --git a/cmake/CMakeLists.txt b/cmake/CMakeLists.txt @@ -0,0 +1,99 @@ +# Copyright (C) 2022 CNRS - LMD +# Copyright (C) 2022 |Meso|Star> (contact@meso-star.com) +# Copyright (C) 2022 Université Paul Sabatier - IRIT +# Copyright (C) 2022 Université Paul Sabatier - Laplace +# +# 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(sln C) +enable_testing() + +set(SLN_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.12.1 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(SLN_FILES_SRC + sln_device.c + sln_log.c) +set(SLN_FILES_INC + sln_device_c.h + sln_log.h) +set(SLN_FILES_INC_API + sln.h) + +set(SLN_FILES_DOC COPYING README.md) + +# Prepend each file in the `SLN_FILES_<SRC|INC>' list by `SLN_SOURCE_DIR' +rcmake_prepend_path(SLN_FILES_SRC ${SLN_SOURCE_DIR}) +rcmake_prepend_path(SLN_FILES_INC ${SLN_SOURCE_DIR}) +rcmake_prepend_path(SLN_FILES_INC_API ${SLN_SOURCE_DIR}) +rcmake_prepend_path(SLN_FILES_DOC ${PROJECT_SOURCE_DIR}/../) + +add_library(sln SHARED + ${SLN_FILES_SRC} ${SLN_FILES_INC} ${SLN_FILES_INC_API}) +target_link_libraries(sln RSys) + +set_target_properties(sln PROPERTIES + DEFINE_SYMBOL SLN_SHARED_BUILD + VERSION ${VERSION} + SOVERSION ${VERSION_MAJOR}) + +rcmake_setup_devel(sln StarLine ${VERSION} star/sln_version.h) + +################################################################################ +# Add tests +################################################################################ +if(NOT NO_TEST) + function(build_test _name) + add_executable(${_name} ${SLN_SOURCE_DIR}/${_name}.c) + target_link_libraries(${_name} sln RSys ${ARGN}) + endfunction() + + function(new_test _name) + build_test(${_name} ${ARGN}) + add_test(${_name} ${_name}) + endfunction() + + #new_test(test_sln_device) +endif() + +################################################################################ +# Define output & install directories +################################################################################ +install(TARGETS sln + ARCHIVE DESTINATION bin + LIBRARY DESTINATION lib + RUNTIME DESTINATION bin) +install(FILES ${SLN_FILES_INC_API} DESTINATION include/star) +install(FILES ${SLN_FILES_DOC} DESTINATION share/doc/star-line) diff --git a/src/sln.h b/src/sln.h @@ -0,0 +1,75 @@ +/* Copyright (C) 2022 CNRS - LMD + * Copyright (C) 2022 |Meso|Star> (contact@meso-star.com) + * Copyright (C) 2022 Université Paul Sabatier - IRIT + * Copyright (C) 2022 Université Paul Sabatier - Laplace + * + * 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/>. */ + +#ifndef SLN_H +#define SLN_H + +#include <rsys/rsys.h> + +/* Library symbol management */ +#if defined(SLN_SHARED_BUILD) /* Build shared library */ + #define SLN_API extern EXPORT_SYM +#elif defined(SLN_STATIC) /* Use/build static library */ + #define SLN_API extern LOCAL_SYM +#else + #define SLN_API extern IMPORT_SYM +#endif + +/* Helper macro that asserts if the invocation of the sln function `Func' + * returns an error. One should use this macro on sln calls for which no + * explicit error checking is performed */ +#ifndef NDEBUG + #define SLN(Func) ASSERT(sln_ ## Func == RES_OK) +#else + #define SLN(Func) sln_ ## Func +#endif + +/* Forwar declaration of external data structures */ +struct logger; +struct mem_allocator; + +struct sln_device_create_args { + struct logger* logger; /* May be NULL <=> default logger */ + struct mem_allocator* allocator; /* NULL <=> use default allocator */ + int verbose; /* Verbosity level */ +}; +#define SLN_DEVICE_CREATE_ARGS_DEFAULT__ {NULL, NULL, 0} +static const struct sln_device_create_args SLN_DEVICE_CREATE_ARGS_DEFAULT = + SLN_DEVICE_CREATE_ARGS_DEFAULT__; + +/* Forward declarations of opaque data structures */ +struct sln_device; +struct sln_tree; + +/******************************************************************************* + * Device API + ******************************************************************************/ +SLN_API res_T +sln_device_create + (const struct sln_device_create_args* args, + struct sln_device** sln); + +SLN_API res_T +sln_device_ref_get + (struct sln_device* sln); + +SLN_API res_T +sln_device_ref_put + (struct sln_device* sln); + +#endif /* SLN_H */ diff --git a/src/sln_device.c b/src/sln_device.c @@ -0,0 +1,101 @@ +/* Copyright (C) 2022 CNRS - LMD + * Copyright (C) 2022 |Meso|Star> (contact@meso-star.com) + * Copyright (C) 2022 Université Paul Sabatier - IRIT + * Copyright (C) 2022 Université Paul Sabatier - Laplace + * + * 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/>. */ + +#include "sln.h" +#include "sln_device_c.h" +#include "sln_log.h" + +/******************************************************************************* + * Helper functions + ******************************************************************************/ +static INLINE res_T +check_sln_device_create_args(const struct sln_device_create_args* args) +{ + return args ? RES_OK : RES_BAD_ARG; +} + +static void +release_sln_device(ref_T* ref) +{ + struct sln_device* sln = CONTAINER_OF(ref, struct sln_device, ref); + ASSERT(ref); + if(sln->logger == &sln->logger__) logger_release(&sln->logger__); + MEM_RM(sln->allocator, sln); +} + +/******************************************************************************* + * Exported functions + ******************************************************************************/ +res_T +sln_device_create + (const struct sln_device_create_args* args, + struct sln_device** out_sln) +{ + struct mem_allocator* allocator = NULL; + struct sln_device* sln = NULL; + res_T res = RES_OK; + + if(!out_sln) { res = RES_BAD_ARG; goto error; } + res = check_sln_device_create_args(args); + if(res != RES_OK) goto error; + + allocator = args->allocator ? args->allocator : &mem_default_allocator; + sln = MEM_CALLOC(allocator, 1, sizeof(*sln)); + if(!sln) { + #define ERR_STR "Could not allocate the Star-Line data structure.\n" + if(args->logger) { + logger_print(args->logger, LOG_ERROR, ERR_STR); + } else { + fprintf(stderr, MSG_ERROR_PREFIX ERR_STR); + } + #undef ERR_STR + res = RES_MEM_ERR; + goto error; + } + ref_init(&sln->ref); + sln->allocator = allocator; + sln->verbose = args->verbose; + if(args->logger) { + sln->logger = args->logger; + } else { + setup_log_default(sln); + } + +exit: + if(out_sln) *out_sln = sln; + return res; +error: + if(sln) { SLN(device_ref_put(sln)); sln = NULL; } + goto exit; +} + +res_T +sln_device_ref_get(struct sln_device* sln) +{ + if(!sln) return RES_BAD_ARG; + ref_get(&sln->ref); + return RES_OK; +} + +res_T +sln_device_ref_put(struct sln_device* sln) +{ + if(!sln) return RES_BAD_ARG; + ref_put(&sln->ref, release_sln_device); + return RES_OK; +} diff --git a/src/sln_device_c.h b/src/sln_device_c.h @@ -0,0 +1,35 @@ +/* Copyright (C) 2022 CNRS - LMD + * Copyright (C) 2022 |Meso|Star> (contact@meso-star.com) + * Copyright (C) 2022 Université Paul Sabatier - IRIT + * Copyright (C) 2022 Université Paul Sabatier - Laplace + * + * 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/>. */ + +#ifndef SLN_DEVICE_C_H +#define SLN_DEVICE_C_H + +#include <rsys/logger.h> +#include <rsys/ref_count.h> + +struct mem_allocator; + +struct sln_device { + struct mem_allocator* allocator; + struct logger* logger; + struct logger logger__; /* Default logger */ + int verbose; + ref_T ref; +}; + +#endif /* SLN_DEVICE_C_H */ diff --git a/src/sln_log.c b/src/sln_log.c @@ -0,0 +1,127 @@ +/* Copyright (C) 2022 CNRS - LMD + * Copyright (C) 2022 |Meso|Star> (contact@meso-star.com) + * Copyright (C) 2022 Université Paul Sabatier - IRIT + * Copyright (C) 2022 Université Paul Sabatier - Laplace + * + * 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/>. */ + +#include "sln_device_c.h" +#include "sln_log.h" + +#include <rsys/cstr.h> +#include <rsys/logger.h> + +#include <stdarg.h> + +/******************************************************************************* + * Helper functions + ******************************************************************************/ +static INLINE void +log_msg + (const struct sln_device* sln, + const enum log_type stream, + const char* msg, + va_list vargs) +{ + ASSERT(sln && msg); + if(sln->verbose) { + res_T res; (void)res; + res = logger_vprint(sln->logger, stream, msg, vargs); + ASSERT(res == RES_OK); + } +} + +static void +print_info(const char* msg, void* ctx) +{ + (void)ctx; + fprintf(stderr, MSG_INFO_PREFIX"%s", msg); +} + +static void +print_err(const char* msg, void* ctx) +{ + (void)ctx; + fprintf(stderr, MSG_ERROR_PREFIX"%s", msg); +} + +static void +print_warn(const char* msg, void* ctx) +{ + (void)ctx; + fprintf(stderr, MSG_WARNING_PREFIX"%s", msg); +} + +/******************************************************************************* + * Local functions + ******************************************************************************/ +res_T +setup_log_default(struct sln_device* sln) +{ + res_T res = RES_OK; + ASSERT(sln); + + res = logger_init(sln->allocator, &sln->logger__); + if(res != RES_OK) { + if(sln->verbose) { + fprintf(stderr, + MSG_ERROR_PREFIX + "Could not setup the default logger for the Star-Line library -- %s.\n", + res_to_cstr(res)); + } + goto error; + } + logger_set_stream(&sln->logger__, LOG_OUTPUT, print_info, NULL); + logger_set_stream(&sln->logger__, LOG_ERROR, print_err, NULL); + logger_set_stream(&sln->logger__, LOG_WARNING, print_warn, NULL); + sln->logger = &sln->logger__; + +exit: + return res; +error: + goto exit; +} + +void +log_info(const struct sln_device* sln, const char* msg, ...) +{ + va_list vargs_list; + ASSERT(sln && msg); + + va_start(vargs_list, msg); + log_msg(sln, LOG_OUTPUT, msg, vargs_list); + va_end(vargs_list); +} + +void +log_err(const struct sln_device* sln, const char* msg, ...) +{ + va_list vargs_list; + ASSERT(sln && msg); + + va_start(vargs_list, msg); + log_msg(sln, LOG_ERROR, msg, vargs_list); + va_end(vargs_list); +} + +void +log_warn(const struct sln_device* sln, const char* msg, ...) +{ + va_list vargs_list; + ASSERT(sln && msg); + + va_start(vargs_list, msg); + log_msg(sln, LOG_WARNING, msg, vargs_list); + va_end(vargs_list); +} diff --git a/src/sln_log.h b/src/sln_log.h @@ -0,0 +1,71 @@ +/* Copyright (C) 2022 CNRS - LMD + * Copyright (C) 2022 |Meso|Star> (contact@meso-star.com) + * Copyright (C) 2022 Université Paul Sabatier - IRIT + * Copyright (C) 2022 Université Paul Sabatier - Laplace + * + * 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/>. */ + +#ifndef SLN_LOG_H +#define SLN_LOG_H + +#include <rsys/rsys.h> + +#define MSG_INFO_PREFIX "Star-HITRAN:\x1b[1m\x1b[32minfo\x1b[0m: " +#define MSG_ERROR_PREFIX "Star-HITRAN:\x1b[1m\x1b[31merror\x1b[0m: " +#define MSG_WARNING_PREFIX "Star-HITRAN:\x1b[1m\x1b[33mwarning\x1b[0m: " + +struct sln_device; +struct logger; + +extern LOCAL_SYM res_T +setup_log_default + (struct sln_device* sln); + +/* Conditionally log a message on the LOG_OUTPUT stream of the sln logger, + * with respect to its verbose flag */ +extern LOCAL_SYM void +log_info + (const struct sln_device* sln, + const char* msg, + ...) +#ifdef COMPILER_GCC + __attribute((format(printf, 2, 3))) +#endif +; + +/* Conditionally log a message on the LOG_ERROR stream of the sln logger, + * with respect to its verbose flag */ +extern LOCAL_SYM void +log_err + (const struct sln_device* sln, + const char* msg, + ...) +#ifdef COMPILER_GCC + __attribute((format(printf, 2, 3))) +#endif +; + +/* Conditionally log a message on the LOG_WARNING stream of the sln logger, + * with respect to its verbose flag */ +extern LOCAL_SYM void +log_warn + (const struct sln_device* sln, + const char* msg, + ...) +#ifdef COMPILER_GCC + __attribute((format(printf, 2, 3))) +#endif +; + +#endif /* SLN_LOG_H */