From 5a45fe11a89adf688b83e80c2d0d0db749413cb1 Mon Sep 17 00:00:00 2001 From: jpekkila Date: Tue, 1 Oct 2019 15:23:39 +0300 Subject: [PATCH] Added the utility library itself (linked with target astaroth_utils) --- src/utils/CMakeLists.txt | 10 +++ src/utils/config_loader.c | 143 ++++++++++++++++++++++++++++++++++++++ src/utils/config_loader.h | 37 ++++++++++ src/utils/memory.c | 64 +++++++++++++++++ src/utils/memory.h | 44 ++++++++++++ 5 files changed, 298 insertions(+) create mode 100644 src/utils/CMakeLists.txt create mode 100644 src/utils/config_loader.c create mode 100644 src/utils/config_loader.h create mode 100644 src/utils/memory.c create mode 100644 src/utils/memory.h diff --git a/src/utils/CMakeLists.txt b/src/utils/CMakeLists.txt new file mode 100644 index 0000000..9200b39 --- /dev/null +++ b/src/utils/CMakeLists.txt @@ -0,0 +1,10 @@ +## C++ standard +set(CMAKE_C_STANDARD 11) +set(CMAKE_C_STANDARD_REQUIRED ON) + +## Compilation flags +add_compile_options(-Wall -Wextra -Werror -Wdouble-promotion -Wfloat-conversion -Wshadow) + +## Compile +add_library(astaroth_utils STATIC config_loader.c memory.c) +target_link_libraries(astaroth_utils PRIVATE astaroth_core) diff --git a/src/utils/config_loader.c b/src/utils/config_loader.c new file mode 100644 index 0000000..2b22f73 --- /dev/null +++ b/src/utils/config_loader.c @@ -0,0 +1,143 @@ +/* + Copyright (C) 2014-2019, Johannes Pekkilae, Miikka Vaeisalae. + + This file is part of Astaroth. + + Astaroth 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. + + Astaroth 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 Astaroth. If not, see . +*/ + +/** + * @file + * \brief Brief info. + * + * Detailed info. + * + */ +#include "config_loader.h" + +#include +#include // UINT_MAX +#include +#include // uint8_t, uint32_t +#include // print +#include // memset +//#include "src/core/math_utils.h" + +/** + \brief Find the index of the keyword in names + \return Index in range 0...n if the keyword is in names. -1 if the keyword was + not found. + */ +static int +find_str(const char keyword[], const char* names[], const int n) +{ + for (int i = 0; i < n; ++i) + if (!strcmp(keyword, names[i])) + return i; + + return -1; +} + +static void +parse_config(const char* path, AcMeshInfo* config) +{ + FILE* fp; + fp = fopen(path, "r"); + // For knowing which .conf file will be used + printf("Config file path: \n %s \n ", path); + assert(fp != NULL); + + const size_t BUF_SIZE = 128; + char keyword[BUF_SIZE]; + char value[BUF_SIZE]; + int items_matched; + while ((items_matched = fscanf(fp, "%s = %s", keyword, value)) != EOF) { + + if (items_matched < 2) + continue; + + int idx = -1; + if ((idx = find_str(keyword, intparam_names, NUM_INT_PARAMS)) >= 0) + config->int_params[idx] = atoi(value); + else if ((idx = find_str(keyword, realparam_names, NUM_REAL_PARAMS)) >= 0) + config->real_params[idx] = (AcReal)(atof(value)); + } + + fclose(fp); +} + +void +update_config(AcMeshInfo* config) +{ + config->int_params[AC_mx] = config->int_params[AC_nx] + STENCIL_ORDER; + ///////////// PAD TEST + // config->int_params[AC_mx] = config->int_params[AC_nx] + STENCIL_ORDER + PAD_SIZE; + ///////////// PAD TEST + config->int_params[AC_my] = config->int_params[AC_ny] + STENCIL_ORDER; + config->int_params[AC_mz] = config->int_params[AC_nz] + STENCIL_ORDER; + + // Bounds for the computational domain, i.e. nx_min <= i < nx_max + config->int_params[AC_nx_min] = STENCIL_ORDER / 2; + config->int_params[AC_nx_max] = config->int_params[AC_nx_min] + config->int_params[AC_nx]; + config->int_params[AC_ny_min] = STENCIL_ORDER / 2; + config->int_params[AC_ny_max] = config->int_params[AC_ny] + STENCIL_ORDER / 2; + config->int_params[AC_nz_min] = STENCIL_ORDER / 2; + config->int_params[AC_nz_max] = config->int_params[AC_nz] + STENCIL_ORDER / 2; + + // Spacing + config->real_params[AC_inv_dsx] = (AcReal)(1.) / config->real_params[AC_dsx]; + config->real_params[AC_inv_dsy] = (AcReal)(1.) / config->real_params[AC_dsy]; + config->real_params[AC_inv_dsz] = (AcReal)(1.) / config->real_params[AC_dsz]; + + /* Additional helper params */ + // Int helpers + config->int_params[AC_mxy] = config->int_params[AC_mx] * config->int_params[AC_my]; + config->int_params[AC_nxy] = config->int_params[AC_nx] * config->int_params[AC_ny]; + config->int_params[AC_nxyz] = config->int_params[AC_nxy] * config->int_params[AC_nz]; +} + +/** +\brief Loads data from astaroth.conf into a config struct. +\return 0 on success, -1 if there are potentially uninitialized values. +*/ +int +acLoadConfig(const char* config_path, AcMeshInfo* config) +{ + int retval = 0; + assert(config_path); + + // memset reads the second parameter as a byte even though it says int in + // the function declaration + memset(config, (uint8_t)0xFF, sizeof(*config)); + + parse_config(config_path, config); + update_config(config); +#if VERBOSE_PRINTING // Defined in astaroth.h + printf("###############################################################\n"); + printf("Config dimensions recalculated:\n"); + acPrintMeshInfo(*config); + printf("###############################################################\n"); +#endif + + // sizeof(config) must be a multiple of 4 bytes for this to work + assert(sizeof(*config) % sizeof(uint32_t) == 0); + for (size_t i = 0; i < sizeof(*config) / sizeof(uint32_t); ++i) { + if (((uint32_t*)config)[i] == (uint32_t)0xFFFFFFFF) { + fprintf(stderr, "Some config values may be uninitialized. " + "See that all are defined in astaroth.conf\n"); + retval = -1; + } + } + return retval; +} diff --git a/src/utils/config_loader.h b/src/utils/config_loader.h new file mode 100644 index 0000000..4eb2c50 --- /dev/null +++ b/src/utils/config_loader.h @@ -0,0 +1,37 @@ +/* + Copyright (C) 2014-2019, Johannes Pekkilae, Miikka Vaeisalae. + + This file is part of Astaroth. + + Astaroth 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. + + Astaroth 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 Astaroth. If not, see . +*/ + +/** + * @file + * \brief Functions for loading and updating AcMeshInfo. + * + */ +#pragma once +#include "astaroth.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** Loads data from the config file */ +int acLoadConfig(const char* config_path, AcMeshInfo* config); + +#ifdef __cplusplus +} // extern "C" +#endif diff --git a/src/utils/memory.c b/src/utils/memory.c new file mode 100644 index 0000000..9fb26ce --- /dev/null +++ b/src/utils/memory.c @@ -0,0 +1,64 @@ +/* + Copyright (C) 2014-2019, Johannes Pekkilae, Miikka Vaeisalae. + + This file is part of Astaroth. + + Astaroth 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. + + Astaroth 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 Astaroth. If not, see . +*/ +#include "memory.h" + +#include +#include + +#include "src/core/errchk.h" + +AcResult +acMeshCreate(const AcMeshInfo info, AcMesh* mesh) +{ + mesh->info = info; + + const size_t bytes = acVertexBufferSizeBytes(mesh->info); + for (int w = 0; w < NUM_VTXBUF_HANDLES; ++w) { + mesh->vertex_buffer[w] = malloc(bytes); + ERRCHK_ALWAYS(mesh->vertex_buffer[w]); + } + + return AC_SUCCESS; +} + +AcResult +acMeshDestroy(AcMesh* mesh) +{ + for (int w = 0; w < NUM_VTXBUF_HANDLES; ++w) + free(mesh->vertex_buffer[w]); + + return AC_SUCCESS; +} + +AcResult +acMeshSet(const AcReal value, AcMesh* mesh) +{ + const int n = acVertexBufferSize(mesh->info); + for (int w = 0; w < NUM_VTXBUF_HANDLES; ++w) + for (int i = 0; i < n; ++i) + mesh->vertex_buffer[w][i] = value; + + return AC_SUCCESS; +} + +AcResult +acMeshClear(AcMesh* mesh) +{ + return acMeshSet(0, mesh); +} diff --git a/src/utils/memory.h b/src/utils/memory.h new file mode 100644 index 0000000..5300c48 --- /dev/null +++ b/src/utils/memory.h @@ -0,0 +1,44 @@ +/* + Copyright (C) 2014-2019, Johannes Pekkilae, Miikka Vaeisalae. + + This file is part of Astaroth. + + Astaroth 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. + + Astaroth 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 Astaroth. If not, see . +*/ + +/** + * @file + * \brief Brief info. + * + * Detailed info. + * + */ +#pragma once +#include "astaroth.h" + +#ifdef __cplusplus +extern "C" { +#endif + +AcResult acMeshCreate(const AcMeshInfo mesh_info, AcMesh* mesh); + +AcResult acMeshDestroy(AcMesh* mesh); + +AcResult acMeshSet(const AcReal value, AcMesh* mesh); + +AcResult acMeshClear(AcMesh* mesh); + +#ifdef __cplusplus +} // extern "C" +#endif