IOSS 2.0
Loading...
Searching...
No Matches
Ioss_GroupingEntity.h
Go to the documentation of this file.
1// Copyright(C) 1999-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_export.h"
10
11#include "Ioss_CodeTypes.h" // for Complex
12#include "Ioss_DatabaseIO.h" // for DatabaseIO
13#include "Ioss_EntityType.h" // for EntityType
14#include "Ioss_Field.h" // for Field, Field::RoleType, etc
15#include "Ioss_FieldManager.h" // for FieldManager, NameList
16#include "Ioss_Property.h" // for Property
17#include "Ioss_PropertyManager.h" // for PropertyManager
18#include "Ioss_State.h" // for State
19#include "Ioss_VariableType.h" // for component_count()
20#include <cstddef> // for size_t, nullptr
21#include <cstdint> // for int64_t
22#include <string> // for string
23#include <vector> // for vector
24
25#ifdef SEACAS_HAVE_KOKKOS
26#include <Kokkos_Core.hpp> // for Kokkos::View
27#endif
28
29namespace Ioss {
30
31 /** \brief Base class for all 'grouping' entities.
32 * The following derived classes are typical:
33 *
34 * -- NodeSet -- grouping of nodes (0d topology)
35 *
36 * -- EdgeSet -- grouping of edges (1d topology)
37 *
38 * -- FaceSet -- grouping of faces (2d topology) [Surface]
39 *
40 * Similarly, there is:
41 *
42 * -- NodeBlock -- grouping of 'similar' nodes (same degree of freedom, ...)
43 *
44 * -- ElementBlock -- grouping of 'similar' elements (same element topology,
45 * attributes, ...)
46 * 0d, 1d, 2d, 3d topology possible -- e.g., sphere, bar, quad, hex
47 *
48 * A Region is also a grouping entity, except that its list of subentites
49 * are other GroupingEntities. That is, it maintains a list of NodeBlocks,
50 * ElementBlocks, NodeLists, CommLists and Surfaces. [Similar to the
51 * "Composite Pattern" in Design Patterns] All interface to GroupingEntities
52 * is through the Region class; clients of the IO subsystem have no direct
53 * access to the underlying GroupingEntities (other than the Region).
54 *
55 * Each GroupingEntity contains:
56 *
57 * -- name
58 *
59 * -- MeshEntities of the specified topological dimension
60 *
61 * -- Optional attributes, either global (applied to the groupingentity), or
62 * unique value(s) to be applied to each subentity.
63 *
64 * -- Data items
65 */
66 class IOSS_EXPORT GroupingEntity
67 {
68 public:
69 friend class Property;
70
71 GroupingEntity() = default;
72 GroupingEntity(DatabaseIO *io_database, const std::string &my_name, int64_t entity_count);
73 GroupingEntity(const GroupingEntity &other);
75
76 virtual ~GroupingEntity();
77
78 IOSS_NODISCARD State get_state() const;
79
80 IOSS_NODISCARD DatabaseIO *get_database() const;
81 void set_database(DatabaseIO *io_database);
82 void reset_database(DatabaseIO *io_database);
83 virtual void delete_database();
84
85 /** Return the GroupingEntity pointer of the "object" that this
86 * entity is contained in. For example, a SideBlock would
87 * return the SideSet that "owns" the SideBlock.
88 * Most GroupingEntities would return the containing Region
89 * A region would return itself(?)
90 * A NodeBlock containing the subset of nodes in a StructuredBlock
91 * would return that StructuredBlock.
92 */
93 IOSS_NODISCARD virtual const GroupingEntity *contained_in() const;
94
95 /** \brief Get name of entity.
96 *
97 * This short-circuits the process of getting the name via the property.
98 * \returns The same information as: entity->get_property("name").get_string()
99 */
100 IOSS_NODISCARD const std::string &name() const { return entityName; }
101
102 /** \brief Set the name of the entity.
103 *
104 * \param[in] new_name The new name of the entity.
105 */
106 void set_name(const std::string &new_name) { entityName = new_name; }
107
108 /** \brief Get a generated name based on the type of the entity and the id.
109 *
110 * For example, element block 10 would return "block_10"
111 * This is the default name if no name is assigned in the mesh database.
112 * \returns The generic name.
113 */
114 IOSS_NODISCARD std::string generic_name() const;
115
116 /** Determine whether a name is an alias for this entity.
117 *
118 * \param[in] my_name Determine whether this name is an alias for this entity.
119 * \returns True if input name is an alias for this entity.
120 */
121 IOSS_NODISCARD bool is_alias(const std::string &my_name) const;
122
123 /** \brief Get list of blocks that the entities in this GroupingEntity "touch".
124 *
125 * For a SideSet, returns a list of the element blocks that the
126 * elements in the set belong to.
127 * For others, it returns an empty vector.
128 * Entries are pushed onto the "block_members" vector, so it will be
129 * appended to if it is not empty at entry to the function.
130 */
131 virtual void block_membership(Ioss::NameList & /* block_members */) {}
132
133 IOSS_NODISCARD std::string get_filename() const;
134
135 /** \brief Get the name of the particular type of entity.
136 *
137 * \returns The name of the particular type of entity.
138 */
139 IOSS_NODISCARD virtual std::string type_string() const = 0;
140
141 /** \brief Get a short name of the particular type of entity.
142 *
143 * \returns The short name of the particular type of entity.
144 */
145 IOSS_NODISCARD virtual std::string short_type_string() const = 0;
146
147 /** \brief What does this entity contain
148 *
149 * \returns The name of the thing this entity contains.
150 */
151 IOSS_NODISCARD virtual std::string contains_string() const = 0;
152
153 /** \brief Get the EntityType, which indicates the particular type of GroupingEntity this is.
154 *
155 * \returns The particular EntityType of this GroupingEntity.
156 */
157 IOSS_NODISCARD virtual EntityType type() const = 0;
158
159 // ========================================================================
160 // PROPERTIES
161 // ========================================================================
162 // Property-related information....
163 // Just forward it through to the property manager...
164 void property_add(const Property &new_prop);
165 void property_erase(const std::string &property_name);
166 IOSS_NODISCARD bool property_exists(const std::string &property_name) const;
167 IOSS_NODISCARD Property get_property(const std::string &property_name) const;
168 IOSS_NODISCARD int64_t get_optional_property(const std::string &property,
169 int64_t optional_value) const;
170 IOSS_NODISCARD std::string get_optional_property(const std::string &property_name,
171 const std::string &optional_value) const;
172 IOSS_NODISCARD NameList property_describe() const;
173 int property_describe(NameList *names) const;
174 IOSS_NODISCARD NameList property_describe(Ioss::Property::Origin origin) const;
175 int property_describe(Ioss::Property::Origin origin, NameList *names) const;
176 IOSS_NODISCARD size_t property_count() const;
177 /** Add a property, or change its value if it already exists with
178 a different value */
179 void property_update(const std::string &property, int64_t value) const;
180 void property_update(const std::string &property, const std::string &value) const;
181
182 // ========================================================================
183 // FIELDS
184 // ========================================================================
185 // Just forward these through to the field manager...
186 void field_add(Field new_field);
187 void field_erase(const std::string &field_name);
188 void field_erase(Field::RoleType role);
189 IOSS_NODISCARD bool field_exists(const std::string &field_name) const;
190 IOSS_NODISCARD Field get_field(const std::string &field_name) const;
191 IOSS_NODISCARD const Field &get_fieldref(const std::string &field_name) const;
192 int field_describe(NameList *names) const;
193 IOSS_NODISCARD NameList field_describe() const;
194 int field_describe(Field::RoleType role, NameList *names) const;
195 IOSS_NODISCARD NameList field_describe(Field::RoleType role) const;
196 IOSS_NODISCARD size_t field_count() const;
197 IOSS_NODISCARD size_t field_count(Field::RoleType role) const;
198
199 IOSS_NODISCARD bool check_for_duplicate(const Ioss::Field &new_field) const;
200
201 // Put this fields data into 'data'.
202
203 // Returns number of entities for which the field was read.
204 // Assumes 'data' is large enough to hold all values.
205 int64_t get_field_data(const std::string &field_name, void *data, size_t data_size) const;
206
207 int64_t put_field_data(const std::string &field_name, void *data, size_t data_size) const;
208
209 // Zero-copy API. *IF* a field is zero-copyable, then this function will set the `data`
210 // pointer to point to a chunk of memory of size `data_size` bytes containing the field
211 // data for the specified field. If the field is not zero-copyable, then the `data`
212 // pointer will point to `nullptr` and `data_size` will be 0.
213 int64_t get_field_data(const std::string &field_name, void **data, size_t *data_size) const;
214
215 // Put this fields data into the specified std::vector space.
216 // Returns number of entities for which the field was read.
217 // Resizes 'data' to size needed to hold all values.
218 template <typename T>
219 int64_t get_field_data(const std::string &field_name, std::vector<T> &data) const;
220
221 template <typename T>
222 int64_t put_field_data(const std::string &field_name, const std::vector<T> &data) const;
223 template <typename T>
224 int64_t put_field_data(const std::string &field_name, std::vector<T> &data) const;
225
226#ifdef SEACAS_HAVE_KOKKOS
227 // Get and put this field's data into the specified Kokkos::View.
228 // Returns the number of entities for which the field was read.
229 // Resizes 'data' to size needed to hold all values;
230 // however, any Views that were previously created referencing the same
231 // underlying memory allocation as 'data' will remain the original size.
232 template <typename T, typename... Args>
233 int64_t get_field_data(const std::string &field_name, Kokkos::View<T *, Args...> &data) const;
234
235 template <typename T, typename... Args>
236 int64_t get_field_data(const std::string &field_name, Kokkos::View<T **, Args...> &data) const;
237
238 template <typename T, typename... Args>
239 int64_t put_field_data(const std::string &field_name, Kokkos::View<T *, Args...> &data) const;
240
241 template <typename T, typename... Args>
242 int64_t put_field_data(const std::string &field_name, Kokkos::View<T **, Args...> &data) const;
243#endif
244
245 /** Get the number of bytes used to store the INT data type
246 *
247 * \returns The number of bytes.
248 */
250 {
251 if (database_ == nullptr || get_database()->int_byte_size_api() == 4) {
252 return Ioss::Field::INT32;
253 }
254
255 return Ioss::Field::INT64;
256 }
257
258 IOSS_NODISCARD unsigned int hash() const { return hash_; }
259
260 IOSS_NODISCARD int64_t entity_count() const { return get_property("entity_count").get_int(); }
261
262 // COMPARE GroupingEntities
263 IOSS_NODISCARD bool operator!=(const GroupingEntity &rhs) const;
264 IOSS_NODISCARD bool operator==(const GroupingEntity &rhs) const;
265 IOSS_NODISCARD bool equal(const GroupingEntity &rhs) const;
266
267 protected:
268 void count_attributes() const;
269
270 bool set_state(State new_state)
271 {
272 entityState = new_state;
273 return true;
274 }
275
276 // Protected to give access to Region which is the only
277 // class that should delete the database. May have to make
278 // private and provide friend...
279 void really_delete_database();
280
281 // Handle implicit properties -- These are calculated from data stored
282 // in the grouping entity instead of having an explicit value assigned.
283 // An example would be 'element_block_count' for a region.
284 // Note that even though this is a pure virtual function, an implementation
285 // is provided to return properties that are common to all grouping entities.
286 // Derived classes should call 'GroupingEntity::get_implicit_property'
287 // if the requested property is not specific to their type.
288 IOSS_NODISCARD virtual Property get_implicit_property(const std::string &my_name) const = 0;
289
292
293 virtual int64_t internal_get_field_data(const Field &field, void *data,
294 size_t data_size = 0) const = 0;
295 virtual int64_t internal_put_field_data(const Field &field, void *data,
296 size_t data_size = 0) const = 0;
297
298 virtual int64_t internal_get_zc_field_data(const Field &field, void **data,
299 size_t *data_size) const = 0;
300
301 int64_t entityCount = 0;
302
303#if defined(IOSS_THREADSAFE)
304 mutable std::mutex m_;
305#endif
306
307 IOSS_NODISCARD bool equal_(const GroupingEntity &rhs, bool quiet) const;
308
309 private:
310 void verify_field_exists(const std::string &field_name, const std::string &inout) const;
311
312 std::string entityName{};
313
314 DatabaseIO *database_ = nullptr;
315
316 mutable int64_t attributeCount = 0;
317 State entityState = STATE_CLOSED;
318 unsigned int hash_ = 0;
319 };
320} // namespace Ioss
321
322/** \brief Add a property to the entity's property manager.
323 *
324 * \param[in] new_prop The property to add.
325 */
327{
328 properties.add(new_prop);
329}
330
331/** \brief Remove a property from the entity's property manager.
332 *
333 * Assumes that the property with the given name already exists in the property manager.
334 *
335 * \param[in] property_name The name of the property to remove.
336 *
337 */
338inline void Ioss::GroupingEntity::property_erase(const std::string &property_name)
339{
340 properties.erase(property_name);
341}
342
343/** \brief Checks if a property exists in the entity's property manager.
344 *
345 * \param[in] property_name The property to check
346 * \returns True if the property exists.
347 */
348inline bool Ioss::GroupingEntity::property_exists(const std::string &property_name) const
349{
350 return properties.exists(property_name);
351}
352
353/** \brief Get the Property from the property manager associated with the entity.
354 *
355 * \param[in] property_name The name of the property to get
356 * \returns The property
357 *
358 */
359inline Ioss::Property Ioss::GroupingEntity::get_property(const std::string &property_name) const
360{
361 return properties.get(property_name);
362}
363
364inline int64_t Ioss::GroupingEntity::get_optional_property(const std::string &property_name,
365 int64_t optional_value) const
366{
367 return properties.get_optional(property_name, optional_value);
368}
369
370inline std::string
371Ioss::GroupingEntity::get_optional_property(const std::string &property_name,
372 const std::string &optional_value) const
373{
374 return properties.get_optional(property_name, optional_value);
375}
376
377/** \brief Get the names of all properties in the property manager for this entity.
378 *
379 * \returns The property names in the property manager.
380 */
382{
383 return properties.describe();
384}
385
386/** \brief Get the names of all properties in the property manager for this entity.
387 *
388 * \param[out] names All the property names in the property manager.
389 * \returns The number of properties extracted from the property manager.
390 */
392{
393 return properties.describe(names);
394}
395
397{
398 return properties.describe(origin);
399}
400
402 NameList *names) const
403{
404 return properties.describe(origin, names);
405}
406
407/** \brief Get the number of properties defined in the property manager for this entity.
408 *
409 * \returns The number of properties.
410 */
411inline size_t Ioss::GroupingEntity::property_count() const { return properties.count(); }
412
413// ------------------------------------------------------------------------
414
415/** \brief Remove all fields of type `role` from the entity's field manager.
416 *
417 * \param[in] role Remove all fields (if any) of type `role`
418 */
419inline void Ioss::GroupingEntity::field_erase(Ioss::Field::RoleType role) { fields.erase(role); }
420
421/** \brief Remove a field from the entity's field manager.
422 *
423 * Assumes that a field with the given name exists in the field manager.
424 *
425 * \param[in] field_name The name of the field to remove.
426 */
427inline void Ioss::GroupingEntity::field_erase(const std::string &field_name)
428{
429 fields.erase(field_name);
430}
431
432/** \brief Checks if a field with a given name exists in the entity's field manager.
433 *
434 * \param[in] field_name The name of the field to check for.
435 * \returns True if the field exists in the entity's field manager.
436 *
437 */
438inline bool Ioss::GroupingEntity::field_exists(const std::string &field_name) const
439{
440 return fields.exists(field_name);
441}
442
443/** \brief Get a field from the entity's field manager.
444 *
445 * \param[in] field_name The name of the field to get.
446 * \returns The field object.
447 *
448 */
449inline Ioss::Field Ioss::GroupingEntity::get_field(const std::string &field_name) const
450{
451 return fields.get(field_name);
452}
453
454/** \brief Get a reference to a field from the entity's field manager.
455 *
456 * \param[in] field_name The name of the field to get.
457 * \returns A reference to the field object.
458 *
459 */
460inline const Ioss::Field &Ioss::GroupingEntity::get_fieldref(const std::string &field_name) const
461{
462 return fields.getref(field_name);
463}
464
465/** \brief Get the names of all fields in the entity's field manager.
466 *
467 * \returns All field names in the entity's field manager.
468 *
469 */
470inline Ioss::NameList Ioss::GroupingEntity::field_describe() const { return fields.describe(); }
471
472/** \brief Get the names of all fields in the entity's field manager.
473 *
474 * \param[out] names All field names in the entity's field manager.
475 * \returns The number of fields extracted from the entity's field manager.
476 *
477 */
479{
480 return fields.describe(names);
481}
482
483/** \brief Get the names of all fields of a specified RoleType in the entity's field manager.
484 *
485 * \param[in] role The role type (MESH, ATTRIBUTE, TRANSIENT, REDUCTION, etc.)
486 * \returns All field names of the specified RoleType in the entity's field manager.
487 *
488 */
490{
491 return fields.describe(role);
492}
493
494/** \brief Get the names of all fields of a specified RoleType in the entity's field manager.
495 *
496 * \param[in] role The role type (MESH, ATTRIBUTE, TRANSIENT, REDUCTION, etc.)
497 * \param[out] names All field names of the specified RoleType in the entity's field manager.
498 * \returns The number of fields extracted from the entity's field manager.
499 *
500 */
502{
503 return fields.describe(role, names);
504}
505
506/** \brief Get the number of fields in the entity's field manager.
507 *
508 * \returns The number of fields in the entity's field manager.
509 */
510inline size_t Ioss::GroupingEntity::field_count() const { return fields.count(); }
511
512/** \brief Read type 'T' field data from the database file into memory using a std::vector.
513 *
514 * \param[in] field_name The name of the field to read.
515 * \param[out] data The data.
516 * \returns The number of values read.
517 *
518 */
519template <typename T>
520int64_t Ioss::GroupingEntity::get_field_data(const std::string &field_name,
521 std::vector<T> &data) const
522{
523 verify_field_exists(field_name, "input");
524
525 Ioss::Field field = get_field(field_name);
526 field.check_type(Ioss::Field::get_field_type(static_cast<T>(0)));
527
528 data.resize(field.raw_count() * field.raw_storage()->component_count());
529 size_t data_size = data.size() * sizeof(T);
530 auto retval = internal_get_field_data(field, Data(data), data_size);
531
532 // At this point, transform the field if specified...
533 if (retval >= 0) {
534 field.transform(Data(data));
535 }
536
537 return retval;
538}
539
540/** \brief Write type 'T' field data from memory into the database file using a std::vector.
541 *
542 * \param[in] field_name The name of the field to write.
543 * \param[in] data The data.
544 * \returns The number of values written.
545 *
546 */
547template <typename T>
548int64_t Ioss::GroupingEntity::put_field_data(const std::string &field_name,
549 const std::vector<T> &data) const
550{
551 verify_field_exists(field_name, "output");
552
553 Ioss::Field field = get_field(field_name);
555 size_t data_size = data.size() * sizeof(T);
556 if (field.has_transform()) {
557 // Need non-const data since the transform will change the users data.
558 std::vector<T> nc_data(data);
559 field.transform(Data(nc_data));
560 return internal_put_field_data(field, Data(nc_data), data_size);
561 }
562
563 T *my_data = const_cast<T *>(Data(data));
564 return internal_put_field_data(field, my_data, data_size);
565}
566
567template <typename T>
568int64_t Ioss::GroupingEntity::put_field_data(const std::string &field_name,
569 std::vector<T> &data) const
570{
571 verify_field_exists(field_name, "output");
572
573 Ioss::Field field = get_field(field_name);
574 field.check_type(Ioss::Field::get_field_type(static_cast<T>(0)));
575 size_t data_size = data.size() * sizeof(T);
576 T *my_data = const_cast<T *>(Data(data));
577 field.transform(my_data);
578 return internal_put_field_data(field, my_data, data_size);
579}
580
581#ifdef SEACAS_HAVE_KOKKOS
582
583/** \brief Read field data from the database file into memory using a 1-D Kokkos:::View.
584 *
585 * \tparam T The data type.
586 * \tparam Args The other template arguments for data.
587 * \param[in] field_name The name of the field to read.
588 * \param[out] data The data.
589 * \returns The number of values read.
590 *
591 */
592template <typename T, typename... Args>
593int64_t Ioss::GroupingEntity::get_field_data(const std::string &field_name,
594 Kokkos::View<T *, Args...> &data) const
595{
596 typedef Kokkos::View<T *, Args...> ViewType;
597
598 verify_field_exists(field_name, "input");
599
600 Ioss::Field field = get_field(field_name);
601
602 // Resize the view
603 auto new_view_size = field.raw_count() * field.raw_storage()->component_count();
604 Kokkos::resize(data, new_view_size);
605 size_t data_size = new_view_size * sizeof(T);
606
607 // Create a host mirror view. (No memory allocation if data is in HostSpace.)
608 typename ViewType::HostMirror host_data = Kokkos::create_mirror_view(data);
609
610 // Extract a pointer to the underlying allocated memory of the host view.
611 T *host_data_ptr = host_data.data();
612
613 // Extract the data from disk to the underlying memory pointed to by host_data_ptr.
614 auto retval = internal_get_field_data(field, host_data_ptr, data_size);
615
616 // At this point, transform the field if specified...
617 if (retval >= 0)
618 field.transform(host_data_ptr);
619
620 // Copy the data to the device. (No op if data is in HostSpace.)
621 Kokkos::deep_copy(data, host_data);
622
623 return retval;
624}
625
626/** \brief Read field data from the database file into memory using a 2-D Kokkos:::View.
627 *
628 * \tparam T The data type
629 * \tparam Args The other template arguments for data.
630 * \param[in] field_name The name of the field to read.
631 * \param[out] data The data.
632 * \returns The number of values read.
633 *
634 */
635template <typename T, typename... Args>
636int64_t Ioss::GroupingEntity::get_field_data(const std::string &field_name,
637 Kokkos::View<T **, Args...> &data) const
638{
639 typedef Kokkos::View<T **, Args...> ViewType;
640
641 verify_field_exists(field_name, "input");
642
643 Ioss::Field field = get_field(field_name);
644
645 // Resize the view
646 int new_view_size_left = field.raw_count();
647 int new_view_size_right = field.raw_storage()->component_count();
648 Kokkos::resize(data, new_view_size_left, new_view_size_right);
649 size_t data_size = new_view_size_left * new_view_size_right * sizeof(T);
650
651 // Create and allocate an array to hold the data temporarily.
652 // This is necessary to ensure the data is placed in the correct
653 // location in the 2-D array, avoiding incorrect placement due
654 // to Views with padded dimensions.
655 T *data_array = new T[data_size];
656
657 // Create a host mirror view. (No memory allocation if data is in HostSpace.)
658 typename ViewType::HostMirror host_data = Kokkos::create_mirror_view(data);
659
660 // Extract the data from disk to the underlying memory pointed to by host_data_ptr.
661 auto retval = internal_get_field_data(field, data_array, data_size);
662
663 // At this point, transform the field if specified...
664 if (retval >= 0)
665 field.transform(data_array);
666
667 // Copy the data to the host Mirror view.
668 // The host mirror view has the same layout as the device view.
669 // For CUDA, this will be LayoutLeft. In this case, the loop order
670 // chosen here will be slower than the reversed loop order.
671 // However, The time for this extra in-memory copy is small
672 // compared with the time to copy from disk into memory.
673 for (int i = 0; i < new_view_size_left; ++i) {
674 for (int j = 0; j < new_view_size_right; ++j) {
675 host_data(i, j) = data_array[new_view_size_right * i + j];
676 }
677 }
678
679 // Delete the temporary array
680 delete[] data_array;
681
682 // Copy the data to the device. (No op if data is in HostSpace.)
683 Kokkos::deep_copy(data, host_data);
684
685 return retval;
686}
687
688/** \brief Write field data from memory into the database file using a 1-D Kokkos::View.
689 *
690 * \tparam T The data type
691 * \tparam Args The other template arguments for data.
692 * \param[in] field_name The name of the field to write.
693 * \param[in] data The data.
694 * \returns The number of values written.
695 *
696 */
697template <typename T, typename... Args>
698int64_t Ioss::GroupingEntity::put_field_data(const std::string &field_name,
699 Kokkos::View<T *, Args...> &data) const
700{
701 typedef Kokkos::View<T *, Args...> ViewType;
702
703 verify_field_exists(field_name, "output");
704
705 Ioss::Field field = get_field(field_name);
706 size_t data_size = field.raw_count() * field.raw_storage()->component_count() * sizeof(T);
707
708 // Create a host mirror view. (No memory allocation if data is in HostSpace.)
709 typename ViewType::HostMirror host_data = Kokkos::create_mirror_view(data);
710
711 // Copy the data to the host. (No op if data is in HostSpace.)
712 Kokkos::deep_copy(host_data, data);
713
714 // Extract a pointer to the underlying allocated memory of the host view.
715 T *host_data_ptr = host_data.data();
716
717 // Transform the field
718 field.transform(host_data_ptr);
719
720 // Copy the data to disk from the underlying memory pointed to by host_data_ptr.
721 return internal_put_field_data(field, host_data_ptr, data_size);
722}
723
724/** \brief Write field data from memory into the database file using a 2-D Kokkos::View.
725 *
726 * \tparam T The data type
727 * \tparam Args The other template arguments for data.
728 * \param[in] field_name The name of the field to write.
729 * \param[in] data The data.
730 * \returns The number of values written.
731 *
732 */
733template <typename T, typename... Args>
734int64_t Ioss::GroupingEntity::put_field_data(const std::string &field_name,
735 Kokkos::View<T **, Args...> &data) const
736{
737 typedef Kokkos::View<T **, Args...> ViewType;
738
739 verify_field_exists(field_name, "output");
740
741 Ioss::Field field = get_field(field_name);
742
743 int view_size_left = data.extent(0);
744 int view_size_right = data.extent(1);
745 size_t data_size = field.raw_count() * field.raw_storage()->component_count() * sizeof(T);
746
747 if (view_size_left * view_size_right * sizeof(T) != data_size) {
748 std::ostringstream errmsg;
749 errmsg << "\nERROR: View dimensions are inconsistent with field raw count or raw storage "
750 "component count"
751 << "for field" << field_name << "\n\n";
752 IOSS_ERROR(errmsg);
753 }
754
755 // Create a host mirror view. (No memory allocation if data is in HostSpace.)
756 typename ViewType::HostMirror host_data = Kokkos::create_mirror_view(data);
757
758 // Copy the data to the host. (No op if data is in HostSpace.)
759 Kokkos::deep_copy(host_data, data);
760
761 // Create and allocate an array to hold the data temporarily.
762 // This is necessary to ensure the data is taken from the correct
763 // location in the 2-D array, avoiding incorrect location due
764 // to Views with padded dimensions.
765 T *data_array = new T[data_size];
766
767 // Copy the data from the host Mirror view.
768 // The host mirror view has the same layout as the device view.
769 // For CUDA, this will be LayoutLeft. In this case, the loop order
770 // chosen here will be slower than the reversed loop order.
771 // However, The time for this extra in-memory copy is small
772 // compared with the time to copy to disk from memory.
773 for (int i = 0; i < view_size_left; ++i) {
774 for (int j = 0; j < view_size_right; ++j) {
775 data_array[view_size_right * i + j] = host_data(i, j);
776 }
777 }
778
779 // Transform the field
780 field.transform(data_array);
781
782 // Copy the data to disk from the underlying memory pointed to by data_array.
783 auto retval = internal_put_field_data(field, data_array, data_size);
784
785 // Delete the temporary array
786 delete[] data_array;
787
788 return retval;
789}
790#endif
#define IOSS_NODISCARD
Definition Ioss_CodeTypes.h:55
IOSS_NODISCARD constexpr T * Data(std::vector< T > &vec)
Definition Ioss_Utils.h:56
void IOSS_ERROR(const std::ostringstream &errmsg)
Definition Ioss_Utils.h:38
void put_field_data(std::string field_name, int local_size, size_t component_count, Entity *e)
Definition Ut_ioad.C:276
An input or output Database.
Definition Ioss_DatabaseIO.h:63
A collection of Ioss::Field objects.
Definition Ioss_FieldManager.h:35
Holds metadata for bulk data associated with a GroupingEntity.
Definition Ioss_Field.h:25
IOSS_NODISCARD const VariableType * raw_storage() const
Definition Ioss_Field.h:168
IOSS_NODISCARD bool has_transform() const
Definition Ioss_Field.h:220
BasicType
The basic data type held in the field.
Definition Ioss_Field.h:29
@ INT64
Definition Ioss_Field.h:35
@ INT32
Definition Ioss_Field.h:34
IOSS_NODISCARD size_t raw_count() const
Definition Ioss_Field.h:171
RoleType
Definition Ioss_Field.h:69
bool transform(void *data)
Definition Ioss_Field.C:309
void check_type(BasicType the_type) const
Definition Ioss_Field.C:201
static IOSS_NODISCARD Ioss::Field::BasicType get_field_type(char)
Definition Ioss_Field.h:43
Base class for all 'grouping' entities. The following derived classes are typical:
Definition Ioss_GroupingEntity.h:67
virtual IOSS_NODISCARD std::string short_type_string() const =0
Get a short name of the particular type of entity.
virtual int64_t internal_get_field_data(const Field &field, void *data, size_t data_size=0) const =0
IOSS_NODISCARD Ioss::Field::BasicType field_int_type() const
Definition Ioss_GroupingEntity.h:249
virtual IOSS_NODISCARD EntityType type() const =0
Get the EntityType, which indicates the particular type of GroupingEntity this is.
FieldManager fields
Definition Ioss_GroupingEntity.h:291
virtual IOSS_NODISCARD std::string type_string() const =0
Get the name of the particular type of entity.
IOSS_NODISCARD int64_t get_optional_property(const std::string &property, int64_t optional_value) const
Definition Ioss_GroupingEntity.h:364
GroupingEntity()=default
IOSS_NODISCARD NameList property_describe() const
Get the names of all properties in the property manager for this entity.
Definition Ioss_GroupingEntity.h:381
IOSS_NODISCARD Property get_property(const std::string &property_name) const
Get the Property from the property manager associated with the entity.
Definition Ioss_GroupingEntity.h:359
GroupingEntity & operator=(const GroupingEntity &rhs)=delete
int64_t get_field_data(const std::string &field_name, void *data, size_t data_size) const
Read field data from the database file into memory using a pointer.
Definition Ioss_GroupingEntity.C:254
IOSS_NODISCARD Field get_field(const std::string &field_name) const
Get a field from the entity's field manager.
Definition Ioss_GroupingEntity.h:449
IOSS_NODISCARD bool field_exists(const std::string &field_name) const
Checks if a field with a given name exists in the entity's field manager.
Definition Ioss_GroupingEntity.h:438
IOSS_NODISCARD unsigned int hash() const
Definition Ioss_GroupingEntity.h:258
IOSS_NODISCARD int64_t entity_count() const
Definition Ioss_GroupingEntity.h:260
virtual int64_t internal_put_field_data(const Field &field, void *data, size_t data_size=0) const =0
void field_erase(const std::string &field_name)
Remove a field from the entity's field manager.
Definition Ioss_GroupingEntity.h:427
PropertyManager properties
Definition Ioss_GroupingEntity.h:290
int64_t put_field_data(const std::string &field_name, void *data, size_t data_size) const
Write field data from memory into the database file using a pointer.
Definition Ioss_GroupingEntity.C:302
IOSS_NODISCARD const Field & get_fieldref(const std::string &field_name) const
Get a reference to a field from the entity's field manager.
Definition Ioss_GroupingEntity.h:460
IOSS_NODISCARD const std::string & name() const
Get name of entity.
Definition Ioss_GroupingEntity.h:100
bool set_state(State new_state)
Definition Ioss_GroupingEntity.h:270
void property_add(const Property &new_prop)
Add a property to the entity's property manager.
Definition Ioss_GroupingEntity.h:326
void property_erase(const std::string &property_name)
Remove a property from the entity's property manager.
Definition Ioss_GroupingEntity.h:338
IOSS_NODISCARD size_t property_count() const
Get the number of properties defined in the property manager for this entity.
Definition Ioss_GroupingEntity.h:411
virtual void block_membership(Ioss::NameList &)
Get list of blocks that the entities in this GroupingEntity "touch".
Definition Ioss_GroupingEntity.h:131
IOSS_NODISCARD size_t field_count() const
Get the number of fields in the entity's field manager.
Definition Ioss_GroupingEntity.h:510
virtual IOSS_NODISCARD std::string contains_string() const =0
What does this entity contain.
void set_name(const std::string &new_name)
Set the name of the entity.
Definition Ioss_GroupingEntity.h:106
IOSS_NODISCARD bool property_exists(const std::string &property_name) const
Checks if a property exists in the entity's property manager.
Definition Ioss_GroupingEntity.h:348
virtual int64_t internal_get_zc_field_data(const Field &field, void **data, size_t *data_size) const =0
IOSS_NODISCARD NameList field_describe() const
Get the names of all fields in the entity's field manager.
Definition Ioss_GroupingEntity.h:470
A collection of Ioss::Property objects.
Definition Ioss_PropertyManager.h:36
void add(const Property &new_prop)
Add a property to the property manager.
Definition Ioss_PropertyManager.C:21
A named value that has a known type.
Definition Ioss_Property.h:27
Origin
Definition Ioss_Property.h:30
IOSS_NODISCARD int component_count() const
Definition Ioss_VariableType.h:154
size_t data_size(const Ioss::Field &f)
Definition Iofaodel_FieldSerialization.C:17
The main namespace for the Ioss library.
Definition Ioad_DatabaseIO.C:40
State
Access states for a database.
Definition Ioss_State.h:15
@ STATE_CLOSED
Definition Ioss_State.h:22
Ioss::NameList NameList
Definition Ioss_ChangeSetFactory.h:25
EntityType
The particular type of GroupingEntity.
Definition Ioss_EntityType.h:12
std::vector< char > data
Definition cth_pressure_map.C:46