stardis-green

Post-processing of green functions
git clone git://git.meso-star.fr/stardis-green.git
Log | Files | Refs | README | LICENSE

green-main.c (5549B)


      1 /* Copyright (C) 2020-2022, 2024 |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 "green-types.h"
     17 #include "green-args.h"
     18 #include "green-output.h"
     19 #include "green-compute.h"
     20 
     21 #include <rsys/rsys.h>
     22 #include <rsys/mem_allocator.h>
     23 #include <rsys/logger.h>
     24 #include <rsys/clock_time.h>
     25 
     26 #include <stdlib.h>
     27 #include <omp.h>
     28 
     29 int
     30 main
     31   (int argc,
     32    char** argv)
     33 {
     34   int err = EXIT_SUCCESS;
     35   res_T res = RES_OK;
     36   int logger_initialized = 0, green_initialized = 0, allocator_initialized = 0;
     37   struct mem_allocator allocator;
     38   struct logger logger;
     39   struct green green;
     40   struct args args;
     41   struct time init_start, tmp;
     42   char buf[128];
     43   const int flag = TIME_USEC | TIME_MSEC | TIME_SEC | TIME_MIN | TIME_HOUR ;
     44   FILE* stream = NULL;
     45 
     46   time_current(&init_start);
     47 
     48   ERR(mem_init_proxy_allocator(&allocator, &mem_default_allocator));
     49   allocator_initialized = 1;
     50 
     51   ERR(logger_init(&allocator, &logger));
     52   logger_initialized = 1;
     53   /* Active loggin for args pasing */
     54   logger_set_stream(&logger, LOG_OUTPUT, log_prt, NULL);
     55   logger_set_stream(&logger, LOG_ERROR, log_err, NULL);
     56   logger_set_stream(&logger, LOG_WARNING, log_warn, NULL);
     57 
     58   init_args(&logger, &allocator, &args);
     59   ERR(parse_args(argc, argv, &args));
     60 
     61   if(args.mode & MODE_HELP) {
     62     usage(stdout);
     63     goto exit;
     64   }
     65 
     66   if(args.mode & MODE_VERSION) {
     67     print_version(stdout);
     68     goto exit;
     69   }
     70   /* Deactivate some loggin according to the -V arg */
     71   if(args.verbose < 1)
     72     logger_set_stream(&logger, LOG_ERROR, NULL, NULL);
     73   if(args.verbose < 2)
     74     logger_set_stream(&logger, LOG_WARNING, NULL, NULL);
     75   if(args.verbose < 3)
     76     logger_set_stream(&logger, LOG_OUTPUT, NULL, NULL);
     77 
     78   green_init(&allocator, &logger, args.nthreads, &green);
     79   green_initialized = 1;
     80   #pragma omp parallel sections num_threads(args.nthreads)
     81   {
     82     /* If there are some settings to apply read the 2 files at the same time*/
     83     #pragma omp section
     84     if(args.green_filename) {
     85       stream = fopen(args.green_filename, "rb");
     86       if(!stream) {
     87         logger_print(&logger, LOG_ERROR,
     88           "Cannot open model file '%s'\n", args.green_filename);
     89         res = RES_IO_ERR;
     90       } else {
     91         res_T tmp_res;
     92         tmp_res = read_green_function(&green, stream);
     93         fclose(stream); stream = NULL;
     94         if(tmp_res == RES_MEM_ERR) {
     95           logger_print(&logger, LOG_ERROR, "Could not allocate memory.\n");
     96           res = tmp_res;
     97         }
     98         else if(tmp_res != RES_OK) {
     99           logger_print(&logger, LOG_ERROR,
    100             "Cannot read model file '%s' (file could be corrupted).\n",
    101             args.green_filename);
    102           res = tmp_res;
    103         }
    104       }
    105     }
    106     #pragma omp section
    107     if(args.mode & MODE_APPLY_GREEN) {
    108       res_T tmp_res;
    109       tmp_res = green_read_settings(&green, args.command_filename);
    110       if(tmp_res == RES_MEM_ERR) {
    111         logger_print(&logger, LOG_ERROR, "Could not allocate memory.\n");
    112         res = tmp_res;
    113       }
    114       else if(tmp_res != RES_OK) {
    115         logger_print(&logger, LOG_ERROR,
    116           "Cannot read settings file '%s' (file could be corrupted).\n",
    117           args.green_filename);
    118         res = tmp_res;
    119       }
    120     }
    121   } /* Implicit barrier */
    122   if(res != RES_OK) goto error;
    123   if(args.mode & MODE_EXTENTED_RESULTS) {
    124     struct time init_end;
    125     time_current(&init_end);
    126     time_sub(&tmp, &init_end, &init_start);
    127     time_dump(&tmp, flag, NULL, buf, sizeof(buf));
    128     logger_print(&logger, LOG_OUTPUT, "Initialisation time = %s\n", buf);
    129   }
    130 
    131   if(args.mode & MODE_HTML_SUMMARY) {
    132     ASSERT(args.info_filename);
    133     stream = fopen(args.info_filename, "w");
    134     if(!stream) {
    135       logger_print(&logger, LOG_ERROR,
    136         "Cannot open model file '%s'\n", args.info_filename);
    137       res = RES_IO_ERR;
    138       goto error;
    139     }
    140     dump_green_info(&green, stream);
    141     fclose(stream); stream = NULL;
    142   }
    143 
    144   if(args.mode & MODE_APPLY_GREEN) {
    145     struct time compute_start, compute_end;
    146     time_current(&compute_start);
    147     ERR(green_compute(&green, args.command_filename));
    148     if(args.mode & MODE_EXTENTED_RESULTS) {
    149       time_current(&compute_end);
    150       time_sub(&tmp, &compute_end, &compute_start);
    151       time_dump(&tmp, flag, NULL, buf, sizeof(buf));
    152       logger_print(&logger, LOG_OUTPUT, "Computation time = %s\n", buf);
    153     }
    154     green_print_result(&green, args.mode, stdout);
    155   }
    156 
    157 exit:
    158   if(green_initialized) green_release(&green);
    159   if(logger_initialized) logger_release(&logger);
    160   if(stream) fclose(stream);
    161   if(allocator_initialized) {
    162     if(MEM_ALLOCATED_SIZE(&allocator) != 0) {
    163       char dump[4096] = { '\0' };
    164       MEM_DUMP(&allocator, dump, sizeof(dump));
    165       fprintf(stderr, "%s\n", dump);
    166       fprintf(stderr, "\nMemory leaks: %lu Bytes\n",
    167         (unsigned long)MEM_ALLOCATED_SIZE(&allocator));
    168     }
    169     mem_shutdown_proxy_allocator(&allocator);
    170   }
    171   return err;
    172 error:
    173   err = EXIT_FAILURE;
    174   goto exit;
    175 }