atrstm

Load and structure a combustion gas mixture
git clone git://git.meso-star.fr/atrstm.git
Log | Files | Refs | README | LICENSE

commit 2786fe4d3b9f1a86838d1af3f5d8f11d68cd06d0
parent 3948ebf21e6381ee121b99556e82e574da40f368
Author: Vincent Forest <vincent.forest@meso-star.com>
Date:   Wed, 21 Oct 2020 16:03:09 +0200

Implement the device API

Diffstat:
Acmake/CMakeLists.txt | 109+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/atrgm.c | 115+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/atrgm.h | 84+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/atrgm_c.h | 37+++++++++++++++++++++++++++++++++++++
Asrc/atrgm_log.c | 124+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/atrgm_log.h | 68++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
6 files changed, 537 insertions(+), 0 deletions(-)

diff --git a/cmake/CMakeLists.txt b/cmake/CMakeLists.txt @@ -0,0 +1,109 @@ +# Copyright (C) 2020 CNRS +# +# 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 2.8) +project(atrgm C) +enable_testing() + +set(ATRGM_SOURCE_DIR ${PROJECT_SOURCE_DIR}/../src) +option(NO_TEST "Do not build tests" OFF) + +################################################################################ +# Check dependencies +################################################################################ +find_package(OpenMP 1.2 REQUIRED) +find_package(RCMake 0.4 REQUIRED) +find_package(RSys 0.10 REQUIRED) +find_package(StarTetraHedra 0.0 REQUIRED) +find_package(StarUVM 0.0 REQUIRED) + +set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${RCMAKE_SOURCE_DIR}) +include(rcmake) +include(rcmake_runtime) + +include_directories( + ${RSys_INCLUDE_DIR} + ${StarTetraHedra_INCLUDE_DIR} + ${StarUVM_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(ATRGM_FILES_SRC + atrgm.c + atrgm_log.c) +set(ATRGM_FILES_INC + atrgm_c.h + atrgm_log.h) +set(ATRGM_FILES_INC_API + atrgm.h) + +set(ATRGM_FILES_DOC COPYING README.md) + +# Prepend each file in the `ATRGM_FILES_<SRC|INC>' list by `ATRGM_SOURCE_DIR' +rcmake_prepend_path(ATRGM_FILES_SRC ${ATRGM_SOURCE_DIR}) +rcmake_prepend_path(ATRGM_FILES_INC ${ATRGM_SOURCE_DIR}) +rcmake_prepend_path(ATRGM_FILES_INC_API ${ATRGM_SOURCE_DIR}) +rcmake_prepend_path(ATRGM_FILES_DOC ${PROJECT_SOURCE_DIR}/../) + +add_library(atrgm SHARED ${ATRGM_FILES_SRC} ${ATRGM_FILES_INC} ${ATRGM_FILES_INC_API}) +target_link_libraries(atrgm RSys StarTetraHedra StarUVM) + +if(CMAKE_COMPILER_IS_GNUCC) + set_target_properties(atrgm PROPERTIES LINK_FLAGS "${OpenMP_C_FLAGS}") +endif() + +set_target_properties(atrgm PROPERTIES + COMPILE_FLAGS ${OpenMP_C_FLAGS} + DEFINE_SYMBOL ATRGM_SHARED_BUILD + VERSION ${VERSION} + SOVERSION ${VERSION_MAJOR}) + +rcmake_setup_devel(atrgm AtrGM ${VERSION} astoria/atrgm.h) + +################################################################################ +# Add tests +################################################################################ +if(NOT NO_TEST) + # function(build_test _name) + # add_executable(${_name} + # ${ATRGM_SOURCE_DIR}/${_name}.c + # ${ATRGM_SOURCE_DIR}/test_atrgm_utils.h) + # target_link_libraries(${_name} atrgm RSys ${ARGN}) + # endfunction() + # + # function(new_test _name) + # build_test(${_name} ${ARGN}) + # add_test(${_name} ${_name}) + # endfunction() + # + # new_test(test_atrgm) +endif() + +################################################################################ +# Define output & install directories +################################################################################ +install(TARGETS atrgm + ARCHIVE DESTINATION bin + LIBRARY DESTINATION lib + RUNTIME DESTINATION bin) +install(FILES ${ATRGM_FILES_INC_API} DESTINATION include/astoria) +install(FILES ${ATRGM_FILES_DOC} DESTINATION share/doc/atrgm) + diff --git a/src/atrgm.c b/src/atrgm.c @@ -0,0 +1,115 @@ +/* Copyright (C) 2020 CNRS + * + * 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 "atrgm.h" +#include "atrgm_c.h" +#include "atrgm_log.h" + +#include <rsys/mem_allocator.h> + +#include <omp.h> + +/******************************************************************************* + * Helper functions + ******************************************************************************/ +static void +release_atrgm(ref_T* ref) +{ + struct atrgm* atrgm; + ASSERT(ref); + atrgm = CONTAINER_OF(ref, struct atrgm, ref); + if(atrgm->logger == &atrgm->logger__) logger_release(&atrgm->logger__); + str_release(&atrgm->name); + MEM_RM(atrgm->allocator, atrgm); +} + +/******************************************************************************* + * Exported functions + ******************************************************************************/ +res_T +atrgm_create + (struct logger* logger, /* NULL <=> use default logger */ + struct mem_allocator* mem_allocator, /* NULL <=> use default allocator */ + const struct atrgm_args* args, + struct atrgm** out_atrgm) +{ + struct atrgm* atrgm = NULL; + struct mem_allocator* allocator = NULL; + int nthreads_max; + res_T res = RES_OK; + + if(!out_atrgm) { + res = RES_BAD_ARG; + goto error; + } + + allocator = mem_allocator ? mem_allocator : &mem_default_allocator; + atrgm = MEM_CALLOC(allocator, 1, sizeof(*atrgm)); + if(!atrgm) { + if(args->verbose) { + #define ERR_STR "Could not allocate the AtrGM data structure.\n" + if(logger) { + logger_print(logger, LOG_ERROR, ERR_STR); + } else { + fprintf(stderr, MSG_ERROR_PREFIX ERR_STR); + } + #undef ERR_STR + } + res = RES_MEM_ERR; + goto error; + } + nthreads_max = MMAX(omp_get_max_threads(), omp_get_num_procs()); + ref_init(&atrgm->ref); + atrgm->allocator = allocator; + atrgm->verbose = args->verbose; + atrgm->nthreads = MMIN(args->nthreads, (unsigned)nthreads_max); + + if(logger) { + atrgm->logger = logger; + } else { + setup_log_default(atrgm); + } + + res = str_set(&atrgm->name, args->name); + if(res != RES_OK) { + log_err(atrgm, "Cannot setup the gas mixture name to `%s'.\n", args->name); + goto error; + } + + /* TODO load data */ + +exit: + if(out_atrgm) *out_atrgm = atrgm; + return res; +error: + if(atrgm) { ATRGM(ref_put(atrgm)); atrgm = NULL; } + goto exit; +} + +res_T +atrgm_ref_get(struct atrgm* atrgm) +{ + if(!atrgm) return RES_BAD_ARG; + ref_get(&atrgm->ref); + return RES_OK; +} + +res_T +atrgm_ref_put(struct atrgm* atrgm) +{ + if(!atrgm) return RES_BAD_ARG; + ref_put(&atrgm->ref, release_atrgm); + return RES_OK; +} diff --git a/src/atrgm.h b/src/atrgm.h @@ -0,0 +1,84 @@ +/* Copyright (C) 2020 CNRS + * + * 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 ATRGM_H +#define ATRGM_H + +#include <rsys/rsys.h> + +/* Library symbol management */ +#if defined(ATRGM_SHARED_BUILD) /* Build shared library */ + #define ATRGM_API extern EXPORT_SYM +#elif defined(ATRGM_STATIC) /* Use/build static library */ + #define ATRGM_API extern LOCAL_SYM +#else /* Use shared library */ + #define ATRGM_API extern IMPORT_SYM +#endif + +/* Helper macro that asserts if the invocation of the atrgm function `Func' + * returns an error. One should use this macro on suvm function calls for + * which no explicit error checking is performed */ +#ifndef NDEBUG + #define ATRGM(Func) ASSERT(atrgm_ ## Func == RES_OK) +#else + #define ATRGM(Func) atrgm_ ## Func +#endif + +struct atrgm_args { + const char* sth_filename; /* Filename of the Star-TetraHedra mesh */ + const char* name; /* Name of the gas mixture */ + unsigned nthreads; /* Hint on the number of threads to use */ + int verbose; /* Verbosity level */ +}; + +#define ATRGM_ARGS_DEFAULT__ { \ + NULL, /* sth_filename */ \ + "gas mixture", /* Name */ \ + (unsigned)~0, /* #threads */ \ + 0 /* Verbosity level */ \ +} +static const struct atrgm_args ATRGM_ARGS_DEFAULT = ATRGM_ARGS_DEFAULT__; + +/* Forward declaration of extern data types */ +struct logger; +struct mem_allocator; + +/* Forward declaration of opaque data type */ +struct atrgm; + +BEGIN_DECLS + +/******************************************************************************* + * AtgGM API + ******************************************************************************/ +ATRGM_API res_T +atrgm_create + (struct logger* logger, /* NULL <=> use default logger */ + struct mem_allocator* allocator, /* NULL <=> use default allocator */ + const struct atrgm_args* args, + struct atrgm** atrgm); + +ATRGM_API res_T +atrgm_ref_get + (struct atrgm* atrgm); + + +ATRGM_API res_T +atrgm_ref_put + (struct atrgm* atrgm); + +END_DECLS + +#endif /* ATRGM_H */ diff --git a/src/atrgm_c.h b/src/atrgm_c.h @@ -0,0 +1,37 @@ +/* Copyright (C) 2020 CNRS + * + * 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 ATRGM_C_H +#define ATRGM_C_H + +#include <rsys/logger.h> +#include <rsys/ref_count.h> +#include <rsys/str.h> + +struct mem_allocator; + +struct atrgm { + unsigned nthreads; /* #nthreads */ + + struct str name; /* Name of the gas mixture */ + + struct mem_allocator* allocator; + struct logger* logger; + struct logger logger__; /* Default logger */ + int verbose; + ref_T ref; +}; + +#endif /* ATRGM_C_H */ diff --git a/src/atrgm_log.c b/src/atrgm_log.c @@ -0,0 +1,124 @@ +/* Copyright (C) 2020 CNRS + * + * 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 "atrgm_c.h" +#include "atrgm_log.h" + +#include <rsys/cstr.h> +#include <rsys/logger.h> + +#include <stdarg.h> + +/******************************************************************************* + * Helper functions + ******************************************************************************/ +static INLINE void +log_msg + (const struct atrgm* atrgm, + const enum log_type stream, + const char* msg, + va_list vargs) +{ + ASSERT(atrgm && msg); + if(atrgm->verbose) { + res_T res; (void)res; + res = logger_vprint(atrgm->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 atrgm* atrgm) +{ + res_T res = RES_OK; + ASSERT(atrgm); + + res = logger_init(atrgm->allocator, &atrgm->logger__); + if(res != RES_OK) { + if(atrgm->verbose) { + fprintf(stderr, + MSG_ERROR_PREFIX + "Could not setup the AtrGM default logger -- %s.\n", + res_to_cstr(res)); + } + goto error; + } + logger_set_stream(&atrgm->logger__, LOG_OUTPUT, print_info, NULL); + logger_set_stream(&atrgm->logger__, LOG_ERROR, print_err, NULL); + logger_set_stream(&atrgm->logger__, LOG_WARNING, print_warn, NULL); + atrgm->logger = &atrgm->logger__; + +exit: + return res; +error: + goto exit; +} + +void +log_info(const struct atrgm* atrgm, const char* msg, ...) +{ + va_list vargs_list; + ASSERT(atrgm && msg); + + va_start(vargs_list, msg); + log_msg(atrgm, LOG_OUTPUT, msg, vargs_list); + va_end(vargs_list); +} + +void +log_err(const struct atrgm* atrgm, const char* msg, ...) +{ + va_list vargs_list; + ASSERT(atrgm && msg); + + va_start(vargs_list, msg); + log_msg(atrgm, LOG_ERROR, msg, vargs_list); + va_end(vargs_list); +} + +void +log_warn(const struct atrgm* atrgm, const char* msg, ...) +{ + va_list vargs_list; + ASSERT(atrgm && msg); + + va_start(vargs_list, msg); + log_msg(atrgm, LOG_WARNING, msg, vargs_list); + va_end(vargs_list); +} diff --git a/src/atrgm_log.h b/src/atrgm_log.h @@ -0,0 +1,68 @@ +/* Copyright (C) 2020 CNRS + * + * 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 ATRGM_LOG_H +#define ATRGM_LOG_H + +#include <rsys/rsys.h> + +#define MSG_INFO_PREFIX "AtrGM:\x1b[1m\x1b[32minfo\x1b[0m: " +#define MSG_ERROR_PREFIX "AtrGM:\x1b[1m\x1b[31merror\x1b[0m: " +#define MSG_WARNING_PREFIX "AtrGM:\x1b[1m\x1b[33mwarning\x1b[0m: " + +struct atrgm; +struct logger; + +extern LOCAL_SYM res_T +setup_log_default + (struct atrgm* atrgm); + +/* Conditionally log a message on the LOG_OUTPUT stream of the atrgm logger, + * with respect to its verbose flag */ +extern LOCAL_SYM void +log_info + (const struct atrgm* atrgm, + const char* msg, + ...) +#ifdef COMPILER_GCC + __attribute((format(printf, 2, 3))) +#endif +; + +/* Conditionally log a message on the LOG_ERROR stream of the atrgm logger, + * with respect to its verbose flag */ +extern LOCAL_SYM void +log_err + (const struct atrgm* atrgm, + const char* msg, + ...) +#ifdef COMPILER_GCC + __attribute((format(printf, 2, 3))) +#endif +; + +/* Conditionally log a message on the LOG_WARNING stream of the atrgm logger, + * with respect to its verbose flag */ +extern LOCAL_SYM void +log_warn + (const struct atrgm* atrgm, + const char* msg, + ...) +#ifdef COMPILER_GCC + __attribute((format(printf, 2, 3))) +#endif +; + +#endif /* ATRGM_LOG_H */