stardis-main.c (4694B)
1 /* Copyright (C) 2018-2025 |Méso|Star> (contact@meso-star.com) 2 * 3 * This program is free software: you can redistribute it and/or modify 4 * it under the terms of the GNU General Public License as published by 5 * the Free Software Foundation, either version 3 of the License, or 6 * (at your option) any later version. 7 * 8 * This program is distributed in the hope that it will be useful, 9 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * GNU General Public License for more details. 12 * 13 * You should have received a copy of the GNU General Public License 14 * along with this program. If not, see <http://www.gnu.org/licenses/>. */ 15 16 #include "stardis-app.h" 17 #include "stardis-args.h" 18 #include "stardis-parsing.h" 19 #include "stardis-output.h" 20 #include "stardis-compute.h" 21 22 #include <rsys/rsys.h> 23 #include <rsys/mem_allocator.h> 24 #include <rsys/logger.h> 25 #include <rsys/clock_time.h> 26 27 #include <rsys/str.h> 28 #include <stdlib.h> 29 #include <stdio.h> 30 31 #ifdef STARDIS_ENABLE_MPI 32 #include <mpi.h> 33 #endif 34 35 int 36 main(int argc, char** argv) 37 { 38 struct args* args = NULL; 39 struct stardis stardis; 40 int err = EXIT_SUCCESS; 41 struct mem_allocator allocator; 42 struct logger logger; 43 int mode = 0; 44 struct time start; 45 FILE* f = NULL; 46 struct str name; 47 res_T res = RES_OK; 48 49 /* Initialisation statuses */ 50 int logger_initialized = 0; 51 int allocator_initialized = 0; 52 int args_initialized = 0; 53 int stardis_initialized = 0; 54 int name_initialized = 0; 55 56 time_current(&start); 57 58 #ifdef STARDIS_ENABLE_MPI 59 ERR(init_mpi(&argc, &argv, log_err_fn, log_warn_fn)); 60 #endif 61 62 ERR(mem_init_proxy_allocator(&allocator, &mem_default_allocator)); 63 allocator_initialized = 1; 64 65 ERR(logger_init(&allocator, &logger)); 66 logger_initialized = 1; 67 str_init(&allocator, &name); 68 name_initialized = 1; 69 /* Active loggin for args parsing */ 70 logger_set_stream(&logger, LOG_ERROR, log_err_fn, NULL); 71 logger_set_stream(&logger, LOG_WARNING, log_warn_fn, NULL); 72 logger_set_stream(&logger, LOG_OUTPUT, log_prt_fn, NULL); 73 74 ERR(init_args(&logger, &allocator, &args)); 75 args_initialized = 1; 76 ERR(parse_args(argc, argv, args, &allocator)); 77 mode = (int)args->mode; 78 79 if(mode & MODE_DUMP_HELP) { 80 usage(stdout); 81 goto exit; 82 } 83 else if(mode & MODE_DUMP_VERSION) { 84 print_version(stdout); 85 goto exit; 86 } 87 88 /* Deactivate some loggin according to the -V arg */ 89 if(args->verbose < 1) 90 logger_set_stream(&logger, LOG_ERROR, NULL, NULL); 91 if(args->verbose < 2) 92 logger_set_stream(&logger, LOG_WARNING, NULL, NULL); 93 if(args->verbose < 3) 94 logger_set_stream(&logger, LOG_OUTPUT, NULL, NULL); 95 96 ERR(stardis_init(args, &logger, &allocator, &stardis)); 97 stardis_initialized = 1; 98 release_args(args); 99 args_initialized = 0; 100 101 if(mode & MODE_DUMP_MODEL) { 102 ERR(str_copy(&name, &stardis.dump_model_filename)); 103 ERR(str_append(&name, ".vtk")); 104 f = fopen(str_cget(&name), "w"); 105 if(!f) { 106 logger_print(stardis.logger, LOG_ERROR, 107 "cannot open file '%s' for writing.\n", str_cget(&name)); 108 res = RES_IO_ERR; 109 goto error; 110 } 111 112 /* Dump all the app-independent information */ 113 ERR(sg3d_geometry_dump_as_vtk(stardis.geometry.sg3d, f)); 114 /* Dump boundaries 115 * Should we dump connections too? */ 116 ERR(dump_boundaries_at_the_end_of_vtk(&stardis, f)); 117 /* Dump the compute region if any */ 118 if(mode & REGION_COMPUTE_MODES) { 119 ERR(dump_compute_region_at_the_end_of_vtk(&stardis, f)); 120 } 121 ERR(dump_enclosure_related_stuff_at_the_end_of_vtk(&stardis, f)); 122 fclose(f); f = NULL; 123 124 /* If dump, exit after dump done */ 125 goto exit; 126 } 127 else if(mode & MODE_DUMP_C_CHUNKS) { 128 ERR(dump_model_as_c_chunks(&stardis, stdout)); 129 /* If dump, exit after dump done */ 130 goto exit; 131 } 132 133 ASSERT(mode & COMPUTE_MODES); 134 ERR(stardis_compute(&stardis, &start)); 135 136 exit: 137 if(name_initialized) str_release(&name); 138 if(f) fclose(f); 139 if(args_initialized) release_args(args); 140 if(stardis_initialized) stardis_release(&stardis); 141 if(logger_initialized) logger_release(&logger); 142 if(allocator_initialized) { 143 if(MEM_ALLOCATED_SIZE(&allocator) != 0) { 144 char dump[4096] = { '\0' }; 145 MEM_DUMP(&allocator, dump, sizeof(dump)); 146 fprintf(stderr, "%s\n", dump); 147 fprintf(stderr, "\nMemory leaks: %lu Bytes\n", 148 (unsigned long)MEM_ALLOCATED_SIZE(&allocator)); 149 } 150 mem_shutdown_proxy_allocator(&allocator); 151 } 152 #ifdef STARDIS_ENABLE_MPI 153 finalize_mpi(); 154 #endif 155 156 return err; 157 error: 158 if(mode & COMPUTE_MODES) 159 logger_print(&logger, LOG_ERROR, 160 "No computation possible.\n"); 161 err = EXIT_FAILURE; 162 goto exit; 163 }