atrstm

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

commit e8496c11f2f2f13bd419a4125e21c176502010c8
parent e7ea8944a16f7b0c3e5fe57f81a145ebf3f2c2fc
Author: Vincent Forest <vincent.forest@meso-star.com>
Date:   Wed, 20 Jan 2021 14:46:44 +0100

Small refactoring

Diffstat:
Msrc/atrstm_cache.c | 40++++++++++++++++++++++++++++------------
Msrc/atrstm_cache.h | 6+++++-
Msrc/atrstm_setup_octrees.c | 45++++++++++++++++++++++++++++++++-------------
3 files changed, 65 insertions(+), 26 deletions(-)

diff --git a/src/atrstm_cache.c b/src/atrstm_cache.c @@ -24,6 +24,7 @@ #include <rsys/hash.h> #include <rsys/mem_allocator.h> #include <rsys/ref_count.h> +#include <rsys/str.h> #include <star/suvm.h> @@ -36,6 +37,7 @@ struct cache { FILE* stream; struct atrstm* atrstm; + struct str name; int empty; ref_T ref; }; @@ -144,7 +146,7 @@ error: /* Setup the cache header, i.e. data that uniquely identify the cache regarding * the input data */ static res_T -write_cache_header(struct cache* cache, const char* filename) +write_cache_header(struct cache* cache) { struct hash hash; res_T res = RES_OK; @@ -153,7 +155,7 @@ write_cache_header(struct cache* cache, const char* filename) #define WRITE(Var, N) { \ if(fwrite((Var), sizeof(*(Var)), (N), cache->stream) != (N)) { \ log_err(cache->atrstm, \ - "%s: could not write the cache header.\n", filename); \ + "%s: could not write the cache header.\n", cache_get_name(cache)); \ res = RES_IO_ERR; \ goto error; \ } \ @@ -179,7 +181,7 @@ error: } static res_T -read_cache_header(struct cache* cache, const char* filename) +read_cache_header(struct cache* cache) { struct hash cached_hash; struct hash current_hash; @@ -204,7 +206,7 @@ read_cache_header(struct cache* cache, const char* filename) res = RES_UNKNOWN_ERR; \ } \ log_err(cache->atrstm, "%s: could not read the cache header -- %s.\n", \ - filename, res_to_cstr(res)); \ + cache_get_name(cache), res_to_cstr(res)); \ goto error; \ } \ } (void)0 @@ -213,7 +215,7 @@ read_cache_header(struct cache* cache, const char* filename) if(cache_version != CACHE_VERSION) { log_err(cache->atrstm, "%s: invalid cache in version %d. Expecting a cache in version %d.\n", - filename, cache_version, CACHE_VERSION); + cache_get_name(cache), cache_version, CACHE_VERSION); res = RES_BAD_ARG; goto error; } @@ -221,8 +223,8 @@ read_cache_header(struct cache* cache, const char* filename) if((CachedVal) != (CurrentVal)) { \ log_err(cache->atrstm, \ "%s: invalid cache regarding the "Name". " \ - "Cached value: "Fmt", Current Value: "Fmt".\n", \ - filename, (CachedVal), (CurrentVal)); \ + "Cached value: "Fmt". Current Value: "Fmt".\n", \ + cache_get_name(cache), (CachedVal), (CurrentVal)); \ res = RES_BAD_ARG; \ goto error; \ } \ @@ -269,7 +271,7 @@ read_cache_header(struct cache* cache, const char* filename) if(!hash256_eq((CachedHash), (CurrentHash))) { \ log_err(cache->atrstm, \ "%s: invalid cache regarding the submitted "Name".\n", \ - filename); \ + cache_get_name(cache)); \ res = RES_BAD_ARG; \ goto error; \ } \ @@ -297,8 +299,8 @@ release_cache(ref_T* ref) cache = CONTAINER_OF(ref, struct cache, ref); atrstm = cache->atrstm; if(cache->stream) CHK(fclose(cache->stream) == 0); + str_release(&cache->name); MEM_RM(atrstm->allocator, cache); - ATRSTM(ref_put(atrstm)); } /******************************************************************************* @@ -323,9 +325,16 @@ cache_create goto error; } ref_init(&cache->ref); - ATRSTM(ref_get(atrstm)); + str_init(atrstm->allocator, &cache->name); cache->atrstm = atrstm; + res = str_set(&cache->name, filename); + if(res != RES_OK) { + log_err(atrstm, "Could not copy the cache filename -- %s.\n", + res_to_cstr(res)); + goto error; + } + fd = open(filename, O_CREAT|O_RDWR, S_IRUSR|S_IWUSR); if(fd < 0) { log_err(atrstm, "Could not open the cache file `%s' -- %s.\n", @@ -353,11 +362,11 @@ cache_create if(cache_stat.st_size == 0) { /* Empty cache */ cache->empty = 1; - res = write_cache_header(cache, filename); + res = write_cache_header(cache); if(res != RES_OK) goto error; } else { /* Cache already exists */ cache->empty = 0; - res = read_cache_header(cache, filename); + res = read_cache_header(cache); if(res != RES_OK) goto error; } @@ -397,3 +406,10 @@ cache_is_empty(const struct cache* cache) ASSERT(cache); return cache->empty; } + +const char* +cache_get_name(const struct cache* cache) +{ + ASSERT(cache); + return str_cget(&cache->name); +} diff --git a/src/atrstm_cache.h b/src/atrstm_cache.h @@ -19,7 +19,7 @@ #include <rsys/rsys.h> #include <stdio.h> -/* Cache of the acceleration data structures used to partitionned the optical +/* Cache of the acceleration data structures used to partition the optical * properties of the semi transparent medium */ struct cache; @@ -45,4 +45,8 @@ extern LOCAL_SYM int cache_is_empty (const struct cache* cache); +extern LOCAL_SYM const char* +cache_get_name + (const struct cache* cache); + #endif /* ATRSTM_CACHE_H */ diff --git a/src/atrstm_setup_octrees.c b/src/atrstm_setup_octrees.c @@ -559,9 +559,7 @@ error: static res_T build_octrees(struct atrstm* atrstm) { - char buf[128]; struct atrri_refractive_index refract_id = ATRRI_REFRACTIVE_INDEX_NULL; - struct time t0, t1; struct pool* pools = NULL; size_t i; double wlen; @@ -593,7 +591,6 @@ build_octrees(struct atrstm* atrstm) log_info(atrstm, "Evaluating and partitionning the field of optical properties.\n"); - time_current(&t0); omp_set_nested(1); /* Enable nested threads for voxelize_volumetric_mesh */ #pragma omp parallel sections num_threads(2) @@ -629,12 +626,12 @@ build_octrees(struct atrstm* atrstm) } if(res != RES_OK) goto error; - time_sub(&t0, time_current(&t1), &t0); - time_dump(&t0, TIME_ALL, NULL, buf, sizeof(buf)); - log_info(atrstm, "Setup the partitionning data structures in %s\n", buf); - if(atrstm->cache) { ASSERT(cache_is_empty(atrstm->cache)); + + log_info(atrstm, "Write acceleration data structure to %s.\n", + cache_get_name(atrstm->cache)); + res = svx_tree_write(atrstm->octree, cache_get_stream(atrstm->cache)); if(res != RES_OK) { log_err(atrstm, "Could not write the octrees to the cache -- %s.\n", @@ -643,10 +640,6 @@ build_octrees(struct atrstm* atrstm) } } - i = MEM_ALLOCATED_SIZE(&atrstm->svx_allocator); - dump_memory_size(i, NULL, buf, sizeof(buf)); - log_info(atrstm, "Star-VoXel memory usage: %s\n", buf); - exit: if(pools) { FOR_EACH(i, 0, atrstm->nthreads) pool_release(pools+i); @@ -663,8 +656,12 @@ load_octrees(struct atrstm* atrstm) FILE* stream; ASSERT(atrstm && atrstm->cache && !cache_is_empty(atrstm->cache)); + log_info(atrstm, "Load the acceleration data structures of the optical " + "properties from '%s'.\n", cache_get_name(atrstm->cache)); + stream = cache_get_stream(atrstm->cache); return svx_tree_create_from_stream(atrstm->svx, stream, &atrstm->octree); + } /******************************************************************************* @@ -673,12 +670,34 @@ load_octrees(struct atrstm* atrstm) res_T setup_octrees(struct atrstm* atrstm) { + char buf[128]; + struct time t0, t1; + size_t sz; + res_T res = RES_OK; ASSERT(atrstm); + + time_current(&t0); + if(atrstm->cache && !cache_is_empty(atrstm->cache)) { - return load_octrees(atrstm);; + res = load_octrees(atrstm);; + if(res != RES_OK) goto error; } else { - return build_octrees(atrstm); + res = build_octrees(atrstm); + if(res != RES_OK) goto error; } + + time_sub(&t0, time_current(&t1), &t0); + time_dump(&t0, TIME_ALL, NULL, buf, sizeof(buf)); + log_info(atrstm, "Setup the partitionning data structures in %s\n", buf); + + sz = MEM_ALLOCATED_SIZE(&atrstm->svx_allocator); + dump_memory_size(sz, NULL, buf, sizeof(buf)); + log_info(atrstm, "Star-VoXel memory usage: %s\n", buf); + +exit: + return res; +error: + goto exit; } void