star-mc

Parallel estimation of Monte Carlo integrators
git clone git://git.meso-star.fr/star-mc.git
Log | Files | Refs | README | LICENSE

test_smc_device.c (3946B)


      1 /* Copyright (C) 2015-2018, 2021-2023 |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 "smc.h"
     17 #include "test_smc_utils.h"
     18 
     19 #include <rsys/logger.h>
     20 
     21 #include <star/ssp.h>
     22 
     23 #include <omp.h>
     24 
     25 static void
     26 log_stream(const char* msg, void* ctx)
     27 {
     28   ASSERT(msg);
     29   (void)msg, (void)ctx;
     30   printf("%s\n", msg);
     31 }
     32 
     33 int
     34 main(int argc, char** argv)
     35 {
     36   struct logger logger;
     37   struct mem_allocator allocator;
     38   enum ssp_rng_type type;
     39   struct smc_device_create_args args = SMC_DEVICE_CREATE_ARGS_DEFAULT;
     40   struct smc_device* dev;
     41   unsigned nthreads;
     42   (void)argc, (void)argv;
     43 
     44   CHK(smc_device_create(&args, NULL) == RES_BAD_ARG);
     45   CHK(smc_device_create(&args, &dev) == RES_OK);
     46 
     47   CHK(smc_device_get_threads_count(NULL, NULL) == RES_BAD_ARG);
     48   CHK(smc_device_get_threads_count(dev, NULL) == RES_BAD_ARG);
     49   CHK(smc_device_get_threads_count(NULL, &nthreads) == RES_BAD_ARG);
     50   CHK(smc_device_get_threads_count(dev, &nthreads) == RES_OK);
     51   CHK(nthreads == (unsigned)omp_get_num_procs());
     52 
     53   CHK(smc_device_ref_get(NULL) == RES_BAD_ARG);
     54   CHK(smc_device_ref_get(dev) == RES_OK);
     55   CHK(smc_device_ref_put(NULL) == RES_BAD_ARG);
     56   CHK(smc_device_ref_put(dev) == RES_OK);
     57   CHK(smc_device_ref_put(dev) == RES_OK);
     58 
     59   args.nthreads_hint = 0;
     60   CHK(smc_device_create(&args, &dev) == RES_BAD_ARG);
     61 
     62   mem_init_proxy_allocator(&allocator, &mem_default_allocator);
     63   CHK(MEM_ALLOCATED_SIZE(&allocator) == 0);
     64 
     65   args.nthreads_hint = 1;
     66   args.allocator = &allocator;
     67   CHK(smc_device_create(&args, NULL) == RES_BAD_ARG);
     68   CHK(smc_device_create(&args, &dev) == RES_OK);
     69   CHK(smc_device_ref_put(dev) == RES_OK);
     70   CHK(MEM_ALLOCATED_SIZE(&allocator) == 0);
     71 
     72   CHK(logger_init(&allocator, &logger) == RES_OK);
     73   logger_set_stream(&logger, LOG_OUTPUT, log_stream, NULL);
     74   logger_set_stream(&logger, LOG_ERROR, log_stream, NULL);
     75   logger_set_stream(&logger, LOG_WARNING, log_stream, NULL);
     76 
     77   args.logger = &logger;
     78   args.allocator = NULL;
     79   CHK(smc_device_create(&args, NULL) == RES_BAD_ARG);
     80   CHK(smc_device_create(&args, &dev) == RES_OK);
     81   CHK(smc_device_ref_put(dev) == RES_OK);
     82 
     83   args.logger = &logger;
     84   args.allocator = &allocator;
     85   args.nthreads_hint = SMC_NTHREADS_DEFAULT;
     86 
     87   CHK(smc_device_create(&args, NULL) == RES_BAD_ARG);
     88   CHK(smc_device_create(&args, &dev) == RES_OK);
     89 
     90   CHK(smc_device_set_rng_type(NULL, SSP_RNG_TYPE_NULL) == RES_BAD_ARG);
     91   CHK(smc_device_set_rng_type(dev, SSP_RNG_TYPE_NULL) == RES_BAD_ARG);
     92   CHK(smc_device_set_rng_type(NULL, SSP_RNG_RANLUX48) == RES_BAD_ARG);
     93   CHK(smc_device_set_rng_type(dev, SSP_RNG_RANLUX48) == RES_OK);
     94 
     95   CHK(smc_device_get_rng_type(NULL, NULL) == RES_BAD_ARG);
     96   CHK(smc_device_get_rng_type(dev, NULL) == RES_BAD_ARG);
     97   CHK(smc_device_get_rng_type(NULL, &type) == RES_BAD_ARG);
     98   CHK(smc_device_get_rng_type(dev, &type) == RES_OK);
     99   CHK(type == SSP_RNG_RANLUX48);
    100 
    101   CHK(smc_device_set_rng_type(dev, SSP_RNG_KISS) == RES_OK);
    102   CHK(smc_device_get_rng_type(dev, &type) == RES_OK);
    103   CHK(type == SSP_RNG_KISS);
    104 
    105   CHK(smc_device_ref_put(dev) == RES_OK);
    106 
    107   args.rng_type = SSP_RNG_RANLUX48;
    108   CHK(smc_device_create(&args, &dev) == RES_OK);
    109   CHK(smc_device_ref_put(dev) == RES_OK);
    110 
    111   logger_release(&logger);
    112   check_memory_allocator(&allocator);
    113   mem_shutdown_proxy_allocator(&allocator);
    114   CHK(mem_allocated_size() == 0);
    115   return 0;
    116 }