Path: blob/main/src/t8_cmesh/t8_cmesh_internal/t8_cmesh_stash.h
921 views
/*1This file is part of t8code.2t8code is a C library to manage a collection (a forest) of multiple3connected adaptive space-trees of general element classes in parallel.45Copyright (C) 2015 the developers67t8code is free software; you can redistribute it and/or modify8it under the terms of the GNU General Public License as published by9the Free Software Foundation; either version 2 of the License, or10(at your option) any later version.1112t8code is distributed in the hope that it will be useful,13but WITHOUT ANY WARRANTY; without even the implied warranty of14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the15GNU General Public License for more details.1617You should have received a copy of the GNU General Public License18along with t8code; if not, write to the Free Software Foundation, Inc.,1951 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.20*/2122/** \file t8_cmesh_stash.h23* We define the data structures and routines for temporary storage before commit24*/2526#ifndef T8_CMESH_STASH_H27#define T8_CMESH_STASH_H2829#include <t8.h>30#include <t8_eclass.h>3132/**33* Opaque pointer to a stash data structure.34*/35typedef struct t8_stash *t8_stash_t;3637/* TODO: could store class information in an offset array instead of38* for each single tree. Especially for non-hybrid meshes this39* would massively reduce our memory footprint */4041/** The eclass information that is stored before a cmesh is committed.42*/43typedef struct t8_stash_class44{45t8_gloidx_t id; /**< The global tree id */46t8_eclass_t eclass; /**< The eclass of that tree */47} t8_stash_class_struct_t;4849/** The face-connection information that is stored before a cmesh is committed.50*/51typedef struct t8_stash_joinface52{53t8_gloidx_t id1; /**< The global tree id of the first tree in the connection. */54t8_gloidx_t id2; /**< The global tree id of the second tree. We ensure id1<=id2. */55int face1; /**< The face number of the first of the connected faces. */56int face2; /**< The face number of the second face. */57int orientation; /**< The orientation of the face connection. \see t8_cmesh_types.h. */58} t8_stash_joinface_struct_t;5960/** The attribute information that is stored before a cmesh is committed.61* The pair (package_id, key) serves as a lookup key to identify the62* data.63*/64typedef struct t8_stash_attribute65{66t8_gloidx_t id; /**< The global tree id */67size_t attr_size; /**< The size (in bytes) of this attribute */68void *attr_data; /**< Array of \a size bytes storing the attributes data. */69int is_owned; /**< True if the data was copied, false if the data is still owned by user. */70int package_id; /**< The id of the package that set this attribute. */71int key; /**< The key used by the package to identify this attribute. */72} t8_stash_attribute_struct_t;7374/** The stash data structure is used to store information about the cmesh75* before it is committed. In particular we store the eclasses of the trees,76* the face-connections and the tree attributes.77* Using the stash structure allows us to have a very flexible interface. When constructing a new mesh, the78* user can specify all these mesh entities in arbitrary order.79* As soon as the cmesh is committed the information is copied from the stash80* to the cmesh in an order mannered.81*/82typedef struct t8_stash83{84sc_array_t classes; /**< Stores the eclasses of the trees. \see t8_stash_class */85sc_array_t joinfaces; /**< Stores the face-connections. \see t8_stash_joinface */86sc_array_t attributes; /**< Stores the attributes. \see t8_stash_attribute */87} t8_stash_struct_t;8889T8_EXTERN_C_BEGIN ();9091/** Initialize a stash data structure.92* \param [in,out] pstash A pointer to the stash to be initialized.93*/94void95t8_stash_init (t8_stash_t *pstash);9697/** Free all memory associated in a stash structure.98* \param [in,out] pstash A pointer to the stash to be destroyed.99* The pointer is set to NULL after the function call.100*/101void102t8_stash_destroy (t8_stash_t *pstash);103104/** Set the eclass of a tree.105* \param [in, out] stash The stash to be updated.106* \param [in] id The global id of the tree whose eclass should be set.107* \param [in] eclass The eclass of tree with id \a id.108*/109void110t8_stash_add_class (t8_stash_t stash, t8_gloidx_t id, t8_eclass_t eclass);111112/** Add a face connection to a stash.113* \param [in, out] stash The stash to be updated.114* \param [in] gid1 The global id of the first tree.115* \param [in] gid2 The global id of the second tree,116* \param [in] face1 The face number of the face of the first tree.117* \param [in] face2 The face number of the face of the second tree.118* \param [in] orientation The orientation of the faces to each other.119*/120void121t8_stash_add_facejoin (t8_stash_t stash, t8_gloidx_t gid1, t8_gloidx_t gid2, int face1, int face2, int orientation);122123/** Sort the entries in the class array by the order given in124* the enum definition of t8_eclass.125* \param [in,out] stash The stash whose class array is sorted.126*/127void128t8_stash_class_sort (t8_stash_t stash);129130/** Search for an entry with a given tree index in the class-stash.131* The stash must be sorted beforehand.132* \param [in] stash The stash to be searched for.133* \param [in] tree_id The global tree id.134* \return The index of an element in the classes array135* of \a stash corresponding to \a tree_id.136* -1 if not found.137*/138ssize_t139t8_stash_class_bsearch (t8_stash_t stash, t8_gloidx_t tree_id);140141/** Sort then entries in the facejoin array in order of the first treeid.142* \param [in,out] stash The stash whose facejoin array is sorted.143*/144void145t8_stash_joinface_sort (t8_stash_t stash);146147/** Add an attribute to a tree.148* \param [in] stash The stash structure to be modified.149* \param [in] id The global index of the tree to which the attribute is added.150* \param [in] package_id The unique id of the current package.151* \param [in] key An integer value used to identify this attribute.152* \param [in] size The size (in bytes) of the attribute.153* \param [in] attr Points to \a size bytes of memory that should be stored as the attribute.154* \param [in] copy If true the attribute data is copied from \a attr to an internal storage.155* If false only the pointer \a attr is stored and the data is only copied156* if the cmesh is committed. (More memory efficient).157*/158void159t8_stash_add_attribute (t8_stash_t stash, t8_gloidx_t id, int package_id, int key, size_t size, void *const attr,160int copy);161162/** Return the size (in bytes) of an attribute in the stash.163* \param [in] stash The stash to be considered.164* \param [in] index The index of the attribute in the attribute array of \a stash.165* \return The size in bytes of the attribute.166*/167size_t168t8_stash_get_attribute_size (t8_stash_t stash, size_t index);169170/** Return the pointer to an attribute in the stash.171* \param [in] stash The stash to be considered.172* \param [in] index The index of the attribute in the attribute array of \a stash.173* \return A void pointer to the memory region where the attribute is stored.174*/175void *176t8_stash_get_attribute (t8_stash_t stash, size_t index);177178/** Return the id of the tree a given attribute belongs to.179* \param [in] stash The stash to be considered.180* \param [in] index The index of the attribute in the attribute array of \a stash.181* \return The tree id.182*/183t8_gloidx_t184t8_stash_get_attribute_tree_id (t8_stash_t stash, size_t index);185186/** Return the key of a given attribute.187* \param [in] stash The stash to be considered.188* \param [in] index The index of the attribute in the attribute array of \a stash.189* \return The attribute's key.190*/191int192t8_stash_get_attribute_key (t8_stash_t stash, size_t index);193194/** Return the package_id of a given attribute.195* \param [in] stash The stash to be considered.196* \param [in] index The index of the attribute in the attribute array of \a stash.197* \return The attribute's package_id.198*/199int200t8_stash_get_attribute_id (t8_stash_t stash, size_t index);201202/** Return true if an attribute in the stash is owned by the stash, that is,203* it was copied in the call to \a t8_stash_add_attribute.204* Returns false if the attribute is not owned by the stash.205* \param [in] stash The stash to be considered.206* \param [in] index The index of the attribute in the attribute array of \a stash.207* \return True of false.208*/209int210t8_stash_attribute_is_owned (t8_stash_t stash, size_t index);211212/** Sort the attributes array of a stash in the order213* (treeid, packageid, key) *214* \param [in,out] stash The stash to be considered.215*/216void217t8_stash_attribute_sort (t8_stash_t stash);218219/** Broadcast a stash on the root process to all processes in a communicator.220* The number of entries in the classes, joinfaces and attributes arrays must221* be known on the receiving processes before calling this function.222* \param [in,out] stash On root the stash that is to be broadcasted.223* On the other process an initialized stash. Its entries will224* get overwritten by the entries in the root stash.225* \param [in] root The mpirank of the root process.226* \param [in] comm The mpi communicator which is used for broadcast.227* \param [in] elem_counts An array with three entries giving the number of228* elements in the classes, joinfaces and attributes arrays.229*/230t8_stash_t231t8_stash_bcast (t8_stash_t stash, int root, sc_MPI_Comm comm, const size_t elem_counts[3]);232233/* TODO: specify equivalence relation. is a different order of data allowed? */234/** Check two stashes for equal content and return true if so.235* \param [in] stash_a The first stash to be considered.236* \param [in] stash_b The first stash to be considered.237* \return True if both stashes hold copies of the same data.238* False otherwise.239*/240int241t8_stash_is_equal (t8_stash_t stash_a, t8_stash_t stash_b);242243T8_EXTERN_C_END ();244245#endif /* !T8_CMESH_STASH_H */246247248