IOSS 2.0
Loading...
Searching...
No Matches
Ioss_Map.h
Go to the documentation of this file.
1// Copyright(C) 1999-2020, 2022, 2023, 2024, 2025 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_SORTED_VECTOR
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
27namespace 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
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
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,
68 bool output_error = false) const;
69
70 template <typename INT>
71 bool set_map(INT *ids, size_t count, size_t offset, bool in_define_mode = true);
72
73 void set_default(size_t count, size_t offset = 0);
74
75 void build_reverse_map();
76 void build_reverse_map_no_lock();
77 void build_reverse_map(int64_t num_to_get, int64_t offset);
78
79 void release_memory(); //! Release memory for all maps.
80
81 void reverse_map_data(void *data, const Ioss::Field &field, size_t count) const;
82 void map_data(void *data, const Ioss::Field &field, size_t count) const;
83 void map_data(void *data, const Ioss::Field::BasicType type, size_t count) const;
84 void map_implicit_data(void *data, const Ioss::Field &field, size_t count, size_t offset) const;
85
86 template <typename T>
87 size_t map_field_to_db_scalar_order(T *variables, std::vector<double> &db_var,
88 size_t begin_offset, size_t count, size_t stride,
89 size_t offset);
90
91 IOSS_NODISCARD const MapContainer &map() const { return m_map; }
93
94 IOSS_NODISCARD bool defined() const { return m_defined; }
95 void set_defined(bool yes_no) { m_defined = yes_no; }
96
97 IOSS_NODISCARD bool reorders() const { return !m_reorder.empty(); }
98
99 private:
100 template <typename INT> void reverse_map_data(INT *data, size_t count) const;
101 template <typename INT> void map_data(INT *data, size_t count) const;
102 template <typename INT> void map_implicit_data(INT *data, size_t count, size_t offset) const;
103
104 int64_t global_to_local_nl(int64_t global, bool must_exist = true,
105 bool output_error = false) const;
106 void build_reorder_map_nl(int64_t start, int64_t count);
107 void build_reverse_map_nl(int64_t num_to_get, int64_t offset);
108#if defined MAP_USE_SORTED_VECTOR
109 void verify_no_duplicate_ids(std::vector<Ioss::IdPair> &reverse_map);
110#endif
111#if defined(IOSS_THREADSAFE)
112 mutable std::mutex m_;
113#endif
117 std::string m_entityType{"unknown"}; // node, element, edge, face
118 std::string m_filename{"undefined"}; // For error messages only.
119 mutable int64_t m_offset{-1}; // local to global offset if m_map is sequential.
120 int m_myProcessor{0}; // For error messages...
121 bool m_defined{false}; // For use by some clients; not all, so don't read too much into value...
122 };
123} // namespace Ioss
#define IOSS_NODISCARD
Definition Ioss_CodeTypes.h:56
Holds metadata for bulk data associated with a GroupingEntity.
Definition Ioss_Field.h:28
BasicType
The basic data type held in the field.
Definition Ioss_Field.h:32
bool m_defined
Definition Ioss_Map.h:121
IOSS_NODISCARD size_t size() const
Definition Ioss_Map.h:60
void set_defined(bool yes_no)
Definition Ioss_Map.h:95
Map(std::string entity_type, std::string file_name, int processor)
Definition Ioss_Map.h:49
Map()=default
ReverseMapContainer m_reverse
Definition Ioss_Map.h:116
int m_myProcessor
Definition Ioss_Map.h:120
std::string m_filename
Definition Ioss_Map.h:118
MapContainer m_map
Definition Ioss_Map.h:114
IOSS_NODISCARD MapContainer & map()
Definition Ioss_Map.h:92
MapContainer m_reorder
Definition Ioss_Map.h:115
void set_is_sequential(bool yesno)
Definition Ioss_Map.h:62
Map & operator=(const Map &from)=delete
IOSS_NODISCARD const MapContainer & map() const
Definition Ioss_Map.h:91
IOSS_NODISCARD bool defined() const
Definition Ioss_Map.h:94
Map(const Map &from)=delete
int64_t m_offset
Definition Ioss_Map.h:119
std::string m_entityType
Definition Ioss_Map.h:117
IOSS_NODISCARD bool reorders() const
Definition Ioss_Map.h:97
void set_rank(int processor)
Definition Ioss_Map.h:57
Definition bhopscotch_map.h:61
Definition robin_map.h:90
The main namespace for the Ioss library.
Definition Ioad_DatabaseIO.C:40
std::pair< int64_t, int64_t > IdPair
Definition Ioss_Map.h:31
std::vector< IdPair > ReverseMapContainer
Definition Ioss_Map.h:32
std::vector< int64_t > MapContainer
Definition Ioss_Map.h:29