atrstm

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

atrstm_partition.h (3705B)


      1 /* Copyright (C) 2022, 2023 |Méso|Star> (contact@meso-star.com)
      2  * Copyright (C) 2020, 2021 Centre National de la Recherche Scientifique
      3  *
      4  * This program is free software: you can redistribute it and/or modify
      5  * it under the terms of the GNU General Public License as published by
      6  * the Free Software Foundation, either version 3 of the License, or
      7  * (at your option) any later version.
      8  *
      9  * This program is distributed in the hope that it will be useful,
     10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
     12  * GNU General Public License for more details.
     13  *
     14  * You should have received a copy of the GNU General Public License
     15  * along with this program. If not, see <http://www.gnu.org/licenses/>. */
     16 
     17 #ifndef ATRSTM_PARTITION_H
     18 #define ATRSTM_PARTITION_H
     19 
     20 #include "atrstm_svx.h"
     21 #include <rsys/list.h>
     22 #include <string.h>
     23 
     24 /* Definition of a partition along the 3 axis */
     25 #define LOG2_PARTITION_DEFINITION 5
     26 #define PARTITION_DEFINITION BIT(LOG2_PARTITION_DEFINITION) /*32*/
     27 #define PARTITION_NVOXELS                                                      \
     28   ( PARTITION_DEFINITION                                                       \
     29   * PARTITION_DEFINITION                                                       \
     30   * PARTITION_DEFINITION)
     31 
     32 /* Forward declarations */
     33 struct cond;
     34 struct mem_allocator;
     35 struct mutex;
     36 
     37 struct part {
     38   /* Morton ordered list of voxels */
     39   float voxels[PARTITION_NVOXELS * NFLOATS_PER_VOXEL];
     40   struct list_node node;
     41   size_t id; /* Unique identifier of the partition */
     42 };
     43 
     44 struct pool {
     45   /* Linked list of pre-allocated partitions */
     46   struct list_node parts_free;
     47 
     48   /* List of available partition sorted in ascending order wrt partition id */
     49   struct list_node parts_full;
     50 
     51   struct mutex* mutex;
     52   struct cond* cond_new;
     53   struct cond* cond_fetch;
     54 
     55   size_t next_part_id; /* Indentifier of the next partition */
     56 
     57   struct mem_allocator* allocator;
     58 
     59   ATOMIC error;
     60 };
     61 
     62 static INLINE void
     63 part_init(struct part* part)
     64 {
     65   ASSERT(part);
     66   list_init(&part->node);
     67   part->id = SIZE_MAX;
     68 }
     69 
     70 static INLINE void
     71 part_release(struct part* part)
     72 {
     73   ASSERT(part && is_list_empty(&part->node));
     74   (void)part; /* Do nothing */
     75 }
     76 
     77 static FINLINE float*
     78 part_get_voxel
     79   (struct part* part,
     80    const size_t ivoxel) /* Morton code of the voxel */
     81 {
     82   ASSERT(part);
     83   ASSERT(ivoxel < PARTITION_NVOXELS);
     84   return part->voxels + ivoxel * NFLOATS_PER_VOXEL;
     85 }
     86 
     87 static FINLINE void
     88 part_clear_voxels(struct part* part)
     89 {
     90   size_t ivox;
     91   FOR_EACH(ivox, 0, PARTITION_NVOXELS) {
     92     vox_clear(part_get_voxel(part, ivox));
     93   }
     94 }
     95 
     96 extern LOCAL_SYM res_T
     97 pool_init
     98   (struct mem_allocator* allocator, /* May be NULL <=> default allocator */
     99    const size_t npartitions, /* #partitions managed by the pool */
    100    struct pool* pool);
    101 
    102 extern LOCAL_SYM void
    103 pool_release
    104   (struct pool* pool);
    105 
    106 /* Return a free partition. Wait until a free partition is available. Return
    107  * NULL on error. */
    108 extern LOCAL_SYM struct part*
    109 pool_next_partition
    110   (struct pool* pool);
    111 
    112 /* Register the partition as a free partition against the pool */
    113 extern LOCAL_SYM void
    114 pool_free_partition
    115   (struct pool* pool,
    116    struct part* part);
    117 
    118 /* Register the partition as a valid partition that can be fetched */
    119 extern LOCAL_SYM void
    120 pool_commit_partition
    121   (struct pool* pool,
    122    struct part* part);
    123 
    124 /* Return the partition whose id is `part_id`. Wait until the expected
    125  * partition is available. Return NULL on error. */
    126 extern LOCAL_SYM struct part*
    127 pool_fetch_partition
    128   (struct pool* pool,
    129    const size_t part_id);
    130 
    131 extern LOCAL_SYM void
    132 pool_invalidate
    133   (struct pool* pool);
    134 
    135 #endif /* ATRSTM_PARTITION_H */