Debian Codegraph @

packages/seacas/libraries/ioss/src/Ioss_Map.h

raw
1 // Copyright(C) 1999-2020, 2022, 2023, 2024 National Technology & Engineering Solutions
2 // of Sandia, LLC (NTESS). Under the terms of Contract DE-NA0003525 with
3 // NTESS, the U.S. Government retains certain rights in this software.
4 //
5 // See packages/seacas/LICENSE for details
6
7 #pragma once
8
9 #include "Ioss_CodeTypes.h"
10 #include "Ioss_Field.h"
11 #include <cstddef> // for size_t
12 #include <cstdint> // for int64_t
13 #include <string> // for string
14 #include <vector> // for vector
15
16 #include "ioss_export.h"
17
18 #define MAP_USE_STD
19 #if defined MAP_USE_STD
20 #include <unordered_map>
21 #elif defined MAP_USE_HOPSCOTCH
22 #include <bhopscotch_map.h>
23 #elif defined MAP_USE_ROBIN
24 #include <robin_map.h>
25 #endif
26
27 namespace Ioss {
28
29 using MapContainer = std::vector<int64_t>;
30 #if defined MAP_USE_SORTED_VECTOR
31 using IdPair = std::pair<int64_t, int64_t>;
32 using ReverseMapContainer = std::vector<IdPair>;
33 #elif defined MAP_USE_STD
34 using ReverseMapContainer = std::unordered_map<int64_t, int64_t>;
35 #elif defined MAP_USE_HOPSCOTCH
36 // The `b` variant requires less-than-comparable key, but is faster
37 using ReverseMapContainer = tsl::bhopscotch_map<int64_t, int64_t>;
38 // using ReverseMapContainer = tsl::hopscotch_map<int64_t, int64_t>;
39 // using ReverseMapContainer = tsl::hopscotch_pg_map<int64_t, int64_t>;
40 #elif defined MAP_USE_ROBIN
41 using ReverseMapContainer = tsl::robin_map<int64_t, int64_t>;
42 // using ReverseMapContainer = tsl::robin_pg_map<int64_t, int64_t>;
43 #endif
44
45 class IOSS_EXPORT Map
46 {
47 public:
48 Map() = default;
49 Map(std::string entity_type, std::string file_name, int processor)
50 : m_entityType(std::move(entity_type)), m_filename(std::move(file_name)),
51 m_myProcessor(processor)
52 {
53 }
54 Map(const Map &from) = delete;
55 Map &operator=(const Map &from) = delete;
56
57 void set_rank(int processor) { m_myProcessor = processor; }
58
59 void set_size(size_t entity_count);
60 IOSS_NODISCARD size_t size() const { return m_map.empty() ? 0 : m_map.size() - 1; }
61
62 void set_is_sequential(bool yesno) { m_map[0] = yesno ? -1 : 1; }
63
64 // Determines whether the input map is sequential (m_map[i] == i)
65 IOSS_NODISCARD bool is_sequential(bool check_all = false) const;
66
67 IOSS_NODISCARD int64_t global_to_local(int64_t global, bool must_exist = true) const;
68
69 template <typename INT>
70 bool set_map(INT *ids, size_t count, size_t offset, bool in_define_mode = true);
71
72 void set_default(size_t count, size_t offset = 0);
73
74 void build_reverse_map();
75 void build_reverse_map_no_lock();
76 void build_reverse_map(int64_t num_to_get, int64_t offset);
77
78 void release_memory(); //! Release memory for all maps.
79
80 void reverse_map_data(void *data, const Ioss::Field &field, size_t count) const;
81 void map_data(void *data, const Ioss::Field &field, size_t count) const;
82 void map_data(void *data, const Ioss::Field::BasicType type, size_t count) const;
83 void map_implicit_data(void *data, const Ioss::Field &field, size_t count, size_t offset) const;
84
85 template <typename T>
86 size_t map_field_to_db_scalar_order(T *variables, std::vector<double> &db_var,
87 size_t begin_offset, size_t count, size_t stride,
88 size_t offset);
89
90 IOSS_NODISCARD const MapContainer &map() const { return m_map; }
91 IOSS_NODISCARD MapContainer &map() { return m_map; }
92
93 IOSS_NODISCARD bool defined() const { return m_defined; }
94 void set_defined(bool yes_no) { m_defined = yes_no; }
95
96 IOSS_NODISCARD bool reorders() const { return !m_reorder.empty(); }
97
98 private:
99 template <typename INT> void reverse_map_data(INT *data, size_t count) const;
100 template <typename INT> void map_data(INT *data, size_t count) const;
101 template <typename INT> void map_implicit_data(INT *data, size_t count, size_t offset) const;
102
103 int64_t global_to_local_nl(int64_t global, bool must_exist = true) const;
104 void build_reorder_map_nl(int64_t start, int64_t count);
105 void build_reverse_map_nl(int64_t num_to_get, int64_t offset);
106 #if defined MAP_USE_SORTED_VECTOR
107 void verify_no_duplicate_ids(std::vector<Ioss::IdPair> &reverse_map);
108 #endif
109 #if defined(IOSS_THREADSAFE)
110 mutable std::mutex m_;
111 #endif
112 MapContainer m_map{};
113 MapContainer m_reorder{};
114 ReverseMapContainer m_reverse{};
115 std::string m_entityType{"unknown"}; // node, element, edge, face
116 std::string m_filename{"undefined"}; // For error messages only.
117 mutable int64_t m_offset{-1}; // local to global offset if m_map is sequential.
118 int m_myProcessor{0}; // For error messages...
119 bool m_defined{false}; // For use by some clients; not all, so don't read too much into value...
120 };
121 } // namespace Ioss