IOSS 2.0
Loading...
Searching...
No Matches
Ioss_GroupingEntity.h
Go to the documentation of this file.
1// Copyright(C) 1999-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_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
79
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 /** \brief Get the name of the particular type of entity.
134 *
135 * \returns The name of the particular type of entity.
136 */
137 IOSS_NODISCARD virtual std::string type_string() const = 0;
138
139 /** \brief Get a short name of the particular type of entity.
140 *
141 * \returns The short name of the particular type of entity.
142 */
143 IOSS_NODISCARD virtual std::string short_type_string() const = 0;
144
145 /** \brief What does this entity contain
146 *
147 * \returns The name of the thing this entity contains.
148 */
149 IOSS_NODISCARD virtual std::string contains_string() const = 0;
150
151 /** \brief Get the EntityType, which indicates the particular type of GroupingEntity this is.
152 *
153 * \returns The particular EntityType of this GroupingEntity.
154 */
155 IOSS_NODISCARD virtual EntityType type() const = 0;
156
157 // ========================================================================
158 // PROPERTIES
159 // ========================================================================
160 // Property-related information....
161 // Just forward it through to the property manager...
162 void property_add(const Property &new_prop);
163 void property_erase(const std::string &property_name);
164 IOSS_NODISCARD bool property_exists(const std::string &property_name) const;
165 IOSS_NODISCARD Property get_property(const std::string &property_name) const;
166 IOSS_NODISCARD int64_t get_optional_property(const std::string &property,
167 int64_t optional_value) const;
168 IOSS_NODISCARD std::string get_optional_property(const std::string &property_name,
169 const std::string &optional_value) const;
171 int property_describe(NameList *names) const;
173 int property_describe(Ioss::Property::Origin origin, NameList *names) const;
174 IOSS_NODISCARD size_t property_count() const;
175 /** Add a property, or change its value if it already exists with
176 a different value */
177 void property_update(const std::string &property, int64_t value) const;
178 void property_update(const std::string &property, const std::string &value) const;
179
180 // ========================================================================
181 // FIELDS
182 // ========================================================================
183 // Just forward these through to the field manager...
184 void field_add(Field new_field);
185 void field_erase(const std::string &field_name);
186 void field_erase(Field::RoleType role);
187 IOSS_NODISCARD bool field_exists(const std::string &field_name) const;
188 IOSS_NODISCARD Field get_field(const std::string &field_name) const;
189 IOSS_NODISCARD const Field &get_fieldref(const std::string &field_name) const;
190 int field_describe(NameList *names) const;
192 int field_describe(Field::RoleType role, NameList *names) const;
194 IOSS_NODISCARD size_t field_count() const;
195 IOSS_NODISCARD size_t field_count(Field::RoleType role) const;
196
197 IOSS_NODISCARD bool check_for_duplicate(const Ioss::Field &new_field) const;
198
199 // Put this fields data into 'data'.
200
201 // Returns number of entities for which the field was read.
202 // Assumes 'data' is large enough to hold all values.
203 int64_t get_field_data(const std::string &field_name, void *data, size_t data_size) const;
204
205 int64_t put_field_data(const std::string &field_name, void *data, size_t data_size) const;
206
207 // Zero-copy API. *IF* a field is zero-copyable, then this function will set the `data`
208 // pointer to point to a chunk of memory of size `data_size` bytes containing the field
209 // data for the specified field. If the field is not zero-copyable, then the `data`
210 // pointer will point to `nullptr` and `data_size` will be 0.
211 int64_t get_field_data(const std::string &field_name, void **data, size_t *data_size) const;
212
213 // Put this fields data into the specified std::vector space.
214 // Returns number of entities for which the field was read.
215 // Resizes 'data' to size needed to hold all values.
216 template <typename T>
217 int64_t get_field_data(const std::string &field_name, std::vector<T> &data) const;
218
219 template <typename T>
220 int64_t put_field_data(const std::string &field_name, const std::vector<T> &data) const;
221 template <typename T>
222 int64_t put_field_data(const std::string &field_name, std::vector<T> &data) const;
223
224#ifdef SEACAS_HAVE_KOKKOS
225 // Get and put this field's data into the specified Kokkos::View.
226 // Returns the number of entities for which the field was read.
227 // Resizes 'data' to size needed to hold all values;
228 // however, any Views that were previously created referencing the same
229 // underlying memory allocation as 'data' will remain the original size.
230 template <typename T, typename... Args>
231 int64_t get_field_data(const std::string &field_name, Kokkos::View<T *, Args...> &data) const;
232
233 template <typename T, typename... Args>
234 int64_t get_field_data(const std::string &field_name, Kokkos::View<T **, Args...> &data) const;
235
236 template <typename T, typename... Args>
237 int64_t put_field_data(const std::string &field_name, Kokkos::View<T *, Args...> &data) const;
238
239 template <typename T, typename... Args>
240 int64_t put_field_data(const std::string &field_name, Kokkos::View<T **, Args...> &data) const;
241#endif
242
243 /** Get the number of bytes used to store the INT data type
244 *
245 * \returns The number of bytes.
246 */
248 {
249 if (database_ == nullptr || get_database()->int_byte_size_api() == 4) {
250 return Ioss::Field::INT32;
251 }
252
253 return Ioss::Field::INT64;
254 }
255
256 IOSS_NODISCARD unsigned int hash() const { return hash_; }
257
258 IOSS_NODISCARD int64_t entity_count() const;
259 int64_t reset_entity_count(int64_t new_entity_count) const;
260
261 // COMPARE GroupingEntities
262 IOSS_NODISCARD bool operator!=(const GroupingEntity &rhs) const;
263 IOSS_NODISCARD bool operator==(const GroupingEntity &rhs) const;
264 IOSS_NODISCARD bool equal(const GroupingEntity &rhs) const;
265
266 protected:
267 void count_attributes() const;
268
269 bool set_state(State new_state)
270 {
271 entityState = new_state;
272 return true;
273 }
274
275 // Protected to give access to Region which is the only
276 // class that should delete the database. May have to make
277 // private and provide friend...
278 void really_delete_database();
279
280 // Handle implicit properties -- These are calculated from data stored
281 // in the grouping entity instead of having an explicit value assigned.
282 // An example would be 'element_block_count' for a region.
283 // Note that even though this is a pure virtual function, an implementation
284 // is provided to return properties that are common to all grouping entities.
285 // Derived classes should call 'GroupingEntity::get_implicit_property'
286 // if the requested property is not specific to their type.
287 IOSS_NODISCARD virtual Property get_implicit_property(const std::string &my_name) const = 0;
288
291
292 virtual int64_t internal_get_field_data(const Field &field, void *data,
293 size_t data_size = 0) const = 0;
294 virtual int64_t internal_put_field_data(const Field &field, void *data,
295 size_t data_size = 0) const = 0;
296
297 virtual int64_t internal_get_zc_field_data(const Field &field, void **data,
298 size_t *data_size) const = 0;
299
300#if defined(IOSS_THREADSAFE)
301 mutable std::mutex m_;
302#endif
303
304 IOSS_NODISCARD bool equal_(const GroupingEntity &rhs, bool quiet) const;
305
306 private:
307 void verify_field_exists(const std::string &field_name, const std::string &inout) const;
308
309 std::string entityName{};
310
312
313 mutable int64_t entityCount = 0;
314 mutable int64_t attributeCount = 0;
316 unsigned int hash_ = 0;
317 };
318} // namespace Ioss
319
320/** \brief Add a property to the entity's property manager.
321 *
322 * \param[in] new_prop The property to add.
323 */
325{
326 properties.add(new_prop);
327}
328
329/** \brief Remove a property from the entity's property manager.
330 *
331 * Assumes that the property with the given name already exists in the property manager.
332 *
333 * \param[in] property_name The name of the property to remove.
334 *
335 */
336inline void Ioss::GroupingEntity::property_erase(const std::string &property_name)
337{
338 properties.erase(property_name);
339}
340
341/** \brief Checks if a property exists in the entity's property manager.
342 *
343 * \param[in] property_name The property to check
344 * \returns True if the property exists.
345 */
346inline bool Ioss::GroupingEntity::property_exists(const std::string &property_name) const
347{
348 return properties.exists(property_name);
349}
350
351/** \brief Get the Property from the property manager associated with the entity.
352 *
353 * \param[in] property_name The name of the property to get
354 * \returns The property
355 *
356 */
357inline Ioss::Property Ioss::GroupingEntity::get_property(const std::string &property_name) const
358{
359 return properties.get(property_name);
360}
361
362inline int64_t Ioss::GroupingEntity::get_optional_property(const std::string &property_name,
363 int64_t optional_value) const
364{
365 return properties.get_optional(property_name, optional_value);
366}
367
368inline std::string
369Ioss::GroupingEntity::get_optional_property(const std::string &property_name,
370 const std::string &optional_value) const
371{
372 return properties.get_optional(property_name, optional_value);
373}
374
375inline int64_t Ioss::GroupingEntity::entity_count() const { return entityCount; }
376
377inline int64_t Ioss::GroupingEntity::reset_entity_count(int64_t new_entity_count) const
378{
379 int64_t save_entity_count = entityCount;
380 entityCount = new_entity_count;
381 return save_entity_count;
382}
383
384/** \brief Get the names of all properties in the property manager for this entity.
385 *
386 * \returns The property names in the property manager.
387 */
389{
390 return properties.describe();
391}
392
393/** \brief Get the names of all properties in the property manager for this entity.
394 *
395 * \param[out] names All the property names in the property manager.
396 * \returns The number of properties extracted from the property manager.
397 */
399{
400 return properties.describe(names);
401}
402
404{
405 return properties.describe(origin);
406}
407
409 NameList *names) const
410{
411 return properties.describe(origin, names);
412}
413
414/** \brief Get the number of properties defined in the property manager for this entity.
415 *
416 * \returns The number of properties.
417 */
418inline size_t Ioss::GroupingEntity::property_count() const { return properties.count(); }
419
420// ------------------------------------------------------------------------
421
422/** \brief Remove all fields of type `role` from the entity's field manager.
423 *
424 * \param[in] role Remove all fields (if any) of type `role`
425 */
427
428/** \brief Remove a field from the entity's field manager.
429 *
430 * Assumes that a field with the given name exists in the field manager.
431 *
432 * \param[in] field_name The name of the field to remove.
433 */
434inline void Ioss::GroupingEntity::field_erase(const std::string &field_name)
435{
436 fields.erase(field_name);
437}
438
439/** \brief Checks if a field with a given name exists in the entity's field manager.
440 *
441 * \param[in] field_name The name of the field to check for.
442 * \returns True if the field exists in the entity's field manager.
443 *
444 */
445inline bool Ioss::GroupingEntity::field_exists(const std::string &field_name) const
446{
447 return fields.exists(field_name);
448}
449
450/** \brief Get a field from the entity's field manager.
451 *
452 * \param[in] field_name The name of the field to get.
453 * \returns The field object.
454 *
455 */
456inline Ioss::Field Ioss::GroupingEntity::get_field(const std::string &field_name) const
457{
458 return fields.get(field_name);
459}
460
461/** \brief Get a reference to a field from the entity's field manager.
462 *
463 * \param[in] field_name The name of the field to get.
464 * \returns A reference to the field object.
465 *
466 */
467inline const Ioss::Field &Ioss::GroupingEntity::get_fieldref(const std::string &field_name) const
468{
469 return fields.getref(field_name);
470}
471
472/** \brief Get the names of all fields in the entity's field manager.
473 *
474 * \returns All field names in the entity's field manager.
475 *
476 */
477inline Ioss::NameList Ioss::GroupingEntity::field_describe() const { return fields.describe(); }
478
479/** \brief Get the names of all fields in the entity's field manager.
480 *
481 * \param[out] names All field names in the entity's field manager.
482 * \returns The number of fields extracted from the entity's field manager.
483 *
484 */
486{
487 return fields.describe(names);
488}
489
490/** \brief Get the names of all fields of a specified RoleType in the entity's field manager.
491 *
492 * \param[in] role The role type (MESH, ATTRIBUTE, TRANSIENT, REDUCTION, etc.)
493 * \returns All field names of the specified RoleType in the entity's field manager.
494 *
495 */
497{
498 return fields.describe(role);
499}
500
501/** \brief Get the names of all fields of a specified RoleType in the entity's field manager.
502 *
503 * \param[in] role The role type (MESH, ATTRIBUTE, TRANSIENT, REDUCTION, etc.)
504 * \param[out] names All field names of the specified RoleType in the entity's field manager.
505 * \returns The number of fields extracted from the entity's field manager.
506 *
507 */
509{
510 return fields.describe(role, names);
511}
512
513/** \brief Get the number of fields in the entity's field manager.
514 *
515 * \returns The number of fields in the entity's field manager.
516 */
517inline size_t Ioss::GroupingEntity::field_count() const { return fields.count(); }
518
519/** \brief Read type 'T' field data from the database file into memory using a std::vector.
520 *
521 * \param[in] field_name The name of the field to read.
522 * \param[out] data The data.
523 * \returns The number of values read.
524 *
525 */
526template <typename T>
527int64_t Ioss::GroupingEntity::get_field_data(const std::string &field_name,
528 std::vector<T> &data) const
529{
530 verify_field_exists(field_name, "input");
531
532 Ioss::Field field = get_field(field_name);
533 field.check_type(Ioss::Field::get_field_type(static_cast<T>(0)));
534
535 data.resize(field.raw_count() * field.raw_storage()->component_count());
536 size_t data_size = data.size() * sizeof(T);
537 auto retval = internal_get_field_data(field, Data(data), data_size);
538
539 // At this point, transform the field if specified...
540 if (retval >= 0) {
541 field.transform(Data(data));
542 }
543
544 return retval;
545}
546
547/** \brief Write type 'T' field data from memory into the database file using a std::vector.
548 *
549 * \param[in] field_name The name of the field to write.
550 * \param[in] data The data.
551 * \returns The number of values written.
552 *
553 */
554template <typename T>
555int64_t Ioss::GroupingEntity::put_field_data(const std::string &field_name,
556 const std::vector<T> &data) const
557{
558 verify_field_exists(field_name, "output");
559
560 Ioss::Field field = get_field(field_name);
562 size_t data_size = data.size() * sizeof(T);
563 if (field.has_transform()) {
564 // Need non-const data since the transform will change the users data.
565 std::vector<T> nc_data(data);
566 field.transform(Data(nc_data));
567 return internal_put_field_data(field, Data(nc_data), data_size);
568 }
569
570 T *my_data = const_cast<T *>(Data(data));
571 return internal_put_field_data(field, my_data, data_size);
572}
573
574template <typename T>
575int64_t Ioss::GroupingEntity::put_field_data(const std::string &field_name,
576 std::vector<T> &data) const
577{
578 verify_field_exists(field_name, "output");
579
580 Ioss::Field field = get_field(field_name);
581 field.check_type(Ioss::Field::get_field_type(static_cast<T>(0)));
582 size_t data_size = data.size() * sizeof(T);
583 T *my_data = const_cast<T *>(Data(data));
584 field.transform(my_data);
585 return internal_put_field_data(field, my_data, data_size);
586}
587
588#ifdef SEACAS_HAVE_KOKKOS
589
590/** \brief Read field data from the database file into memory using a 1-D Kokkos:::View.
591 *
592 * \tparam T The data type.
593 * \tparam Args The other template arguments for data.
594 * \param[in] field_name The name of the field to read.
595 * \param[out] data The data.
596 * \returns The number of values read.
597 *
598 */
599template <typename T, typename... Args>
600int64_t Ioss::GroupingEntity::get_field_data(const std::string &field_name,
601 Kokkos::View<T *, Args...> &data) const
602{
603 typedef Kokkos::View<T *, Args...> ViewType;
604
605 verify_field_exists(field_name, "input");
606
607 Ioss::Field field = get_field(field_name);
608
609 // Resize the view
610 auto new_view_size = field.raw_count() * field.raw_storage()->component_count();
611 Kokkos::resize(data, new_view_size);
612 size_t data_size = new_view_size * sizeof(T);
613
614 // Create a host mirror view. (No memory allocation if data is in HostSpace.)
615 typename ViewType::HostMirror host_data = Kokkos::create_mirror_view(data);
616
617 // Extract a pointer to the underlying allocated memory of the host view.
618 T *host_data_ptr = host_data.data();
619
620 // Extract the data from disk to the underlying memory pointed to by host_data_ptr.
621 auto retval = internal_get_field_data(field, host_data_ptr, data_size);
622
623 // At this point, transform the field if specified...
624 if (retval >= 0)
625 field.transform(host_data_ptr);
626
627 // Copy the data to the device. (No op if data is in HostSpace.)
628 Kokkos::deep_copy(data, host_data);
629
630 return retval;
631}
632
633/** \brief Read field data from the database file into memory using a 2-D Kokkos:::View.
634 *
635 * \tparam T The data type
636 * \tparam Args The other template arguments for data.
637 * \param[in] field_name The name of the field to read.
638 * \param[out] data The data.
639 * \returns The number of values read.
640 *
641 */
642template <typename T, typename... Args>
643int64_t Ioss::GroupingEntity::get_field_data(const std::string &field_name,
644 Kokkos::View<T **, Args...> &data) const
645{
646 typedef Kokkos::View<T **, Args...> ViewType;
647
648 verify_field_exists(field_name, "input");
649
650 Ioss::Field field = get_field(field_name);
651
652 // Resize the view
653 int new_view_size_left = field.raw_count();
654 int new_view_size_right = field.raw_storage()->component_count();
655 Kokkos::resize(data, new_view_size_left, new_view_size_right);
656 size_t data_size = new_view_size_left * new_view_size_right * sizeof(T);
657
658 // Create and allocate an array to hold the data temporarily.
659 // This is necessary to ensure the data is placed in the correct
660 // location in the 2-D array, avoiding incorrect placement due
661 // to Views with padded dimensions.
662 T *data_array = new T[data_size];
663
664 // Create a host mirror view. (No memory allocation if data is in HostSpace.)
665 typename ViewType::HostMirror host_data = Kokkos::create_mirror_view(data);
666
667 // Extract the data from disk to the underlying memory pointed to by host_data_ptr.
668 auto retval = internal_get_field_data(field, data_array, data_size);
669
670 // At this point, transform the field if specified...
671 if (retval >= 0)
672 field.transform(data_array);
673
674 // Copy the data to the host Mirror view.
675 // The host mirror view has the same layout as the device view.
676 // For CUDA, this will be LayoutLeft. In this case, the loop order
677 // chosen here will be slower than the reversed loop order.
678 // However, The time for this extra in-memory copy is small
679 // compared with the time to copy from disk into memory.
680 for (int i = 0; i < new_view_size_left; ++i) {
681 for (int j = 0; j < new_view_size_right; ++j) {
682 host_data(i, j) = data_array[new_view_size_right * i + j];
683 }
684 }
685
686 // Delete the temporary array
687 delete[] data_array;
688
689 // Copy the data to the device. (No op if data is in HostSpace.)
690 Kokkos::deep_copy(data, host_data);
691
692 return retval;
693}
694
695/** \brief Write field data from memory into the database file using a 1-D Kokkos::View.
696 *
697 * \tparam T The data type
698 * \tparam Args The other template arguments for data.
699 * \param[in] field_name The name of the field to write.
700 * \param[in] data The data.
701 * \returns The number of values written.
702 *
703 */
704template <typename T, typename... Args>
705int64_t Ioss::GroupingEntity::put_field_data(const std::string &field_name,
706 Kokkos::View<T *, Args...> &data) const
707{
708 typedef Kokkos::View<T *, Args...> ViewType;
709
710 verify_field_exists(field_name, "output");
711
712 Ioss::Field field = get_field(field_name);
713 size_t data_size = field.raw_count() * field.raw_storage()->component_count() * sizeof(T);
714
715 // Create a host mirror view. (No memory allocation if data is in HostSpace.)
716 typename ViewType::HostMirror host_data = Kokkos::create_mirror_view(data);
717
718 // Copy the data to the host. (No op if data is in HostSpace.)
719 Kokkos::deep_copy(host_data, data);
720
721 // Extract a pointer to the underlying allocated memory of the host view.
722 T *host_data_ptr = host_data.data();
723
724 // Transform the field
725 field.transform(host_data_ptr);
726
727 // Copy the data to disk from the underlying memory pointed to by host_data_ptr.
728 return internal_put_field_data(field, host_data_ptr, data_size);
729}
730
731/** \brief Write field data from memory into the database file using a 2-D Kokkos::View.
732 *
733 * \tparam T The data type
734 * \tparam Args The other template arguments for data.
735 * \param[in] field_name The name of the field to write.
736 * \param[in] data The data.
737 * \returns The number of values written.
738 *
739 */
740template <typename T, typename... Args>
741int64_t Ioss::GroupingEntity::put_field_data(const std::string &field_name,
742 Kokkos::View<T **, Args...> &data) const
743{
744 typedef Kokkos::View<T **, Args...> ViewType;
745
746 verify_field_exists(field_name, "output");
747
748 Ioss::Field field = get_field(field_name);
749
750 int view_size_left = data.extent(0);
751 int view_size_right = data.extent(1);
752 size_t data_size = field.raw_count() * field.raw_storage()->component_count() * sizeof(T);
753
754 if (view_size_left * view_size_right * sizeof(T) != data_size) {
755 std::ostringstream errmsg;
756 errmsg << "\nERROR: View dimensions are inconsistent with field raw count or raw storage "
757 "component count"
758 << "for field" << field_name << "\n\n";
759 IOSS_ERROR(errmsg);
760 }
761
762 // Create a host mirror view. (No memory allocation if data is in HostSpace.)
763 typename ViewType::HostMirror host_data = Kokkos::create_mirror_view(data);
764
765 // Copy the data to the host. (No op if data is in HostSpace.)
766 Kokkos::deep_copy(host_data, data);
767
768 // Create and allocate an array to hold the data temporarily.
769 // This is necessary to ensure the data is taken from the correct
770 // location in the 2-D array, avoiding incorrect location due
771 // to Views with padded dimensions.
772 T *data_array = new T[data_size];
773
774 // Copy the data from the host Mirror view.
775 // The host mirror view has the same layout as the device view.
776 // For CUDA, this will be LayoutLeft. In this case, the loop order
777 // chosen here will be slower than the reversed loop order.
778 // However, The time for this extra in-memory copy is small
779 // compared with the time to copy to disk from memory.
780 for (int i = 0; i < view_size_left; ++i) {
781 for (int j = 0; j < view_size_right; ++j) {
782 data_array[view_size_right * i + j] = host_data(i, j);
783 }
784 }
785
786 // Transform the field
787 field.transform(data_array);
788
789 // Copy the data to disk from the underlying memory pointed to by data_array.
790 auto retval = internal_put_field_data(field, data_array, data_size);
791
792 // Delete the temporary array
793 delete[] data_array;
794
795 return retval;
796}
797#endif
#define IOSS_NODISCARD
Definition Ioss_CodeTypes.h:56
IOSS_NODISCARD constexpr T * Data(std::vector< T > &vec)
Definition Ioss_Utils.h:66
void IOSS_ERROR(const std::string &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:28
IOSS_NODISCARD const VariableType * raw_storage() const
Definition Ioss_Field.h:171
IOSS_NODISCARD bool has_transform() const
Definition Ioss_Field.h:223
BasicType
The basic data type held in the field.
Definition Ioss_Field.h:32
@ INT64
Definition Ioss_Field.h:38
@ INT32
Definition Ioss_Field.h:37
IOSS_NODISCARD size_t raw_count() const
Definition Ioss_Field.h:174
RoleType
Definition Ioss_Field.h:72
bool transform(void *data)
Definition Ioss_Field.C:312
void check_type(BasicType the_type) const
Definition Ioss_Field.C:204
static IOSS_NODISCARD Ioss::Field::BasicType get_field_type(char)
Definition Ioss_Field.h:46
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.
IOSS_NODISCARD bool equal_(const GroupingEntity &rhs, bool quiet) const
Definition Ioss_GroupingEntity.C:377
void field_add(Field new_field)
Add a field to the entity's field manager.
Definition Ioss_GroupingEntity.C:202
virtual int64_t internal_get_field_data(const Field &field, void *data, size_t data_size=0) const =0
int64_t attributeCount
Definition Ioss_GroupingEntity.h:314
IOSS_NODISCARD Ioss::Field::BasicType field_int_type() const
Definition Ioss_GroupingEntity.h:247
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:290
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:362
IOSS_NODISCARD DatabaseIO * get_database() const
Definition Ioss_GroupingEntity.C:95
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:388
friend class Property
Definition Ioss_GroupingEntity.h:69
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:357
IOSS_NODISCARD int64_t entity_count() const
Definition Ioss_GroupingEntity.h:375
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:242
IOSS_NODISCARD Field get_field(const std::string &field_name) const
Get a field from the entity's field manager.
Definition Ioss_GroupingEntity.h:456
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:445
std::string entityName
Definition Ioss_GroupingEntity.h:309
int64_t reset_entity_count(int64_t new_entity_count) const
Definition Ioss_GroupingEntity.h:377
IOSS_NODISCARD unsigned int hash() const
Definition Ioss_GroupingEntity.h:256
void verify_field_exists(const std::string &field_name, const std::string &inout) const
Definition Ioss_GroupingEntity.C:335
void reset_database(DatabaseIO *io_database)
Definition Ioss_GroupingEntity.C:108
virtual void delete_database()
Definition Ioss_GroupingEntity.C:67
IOSS_NODISCARD State get_state() const
Get the current Ioss::State of the entity.
Definition Ioss_GroupingEntity.C:131
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:434
int field_describe(NameList *names) const
Get the names of all fields in the entity's field manager.
Definition Ioss_GroupingEntity.h:485
PropertyManager properties
Definition Ioss_GroupingEntity.h:289
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:290
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:467
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:269
void property_update(const std::string &property, int64_t value) const
Definition Ioss_GroupingEntity.C:346
void property_add(const Property &new_prop)
Add a property to the entity's property manager.
Definition Ioss_GroupingEntity.h:324
IOSS_NODISCARD bool check_for_duplicate(const Ioss::Field &new_field) const
Definition Ioss_GroupingEntity.C:162
DatabaseIO * database_
Definition Ioss_GroupingEntity.h:311
void property_erase(const std::string &property_name)
Remove a property from the entity's property manager.
Definition Ioss_GroupingEntity.h:336
IOSS_NODISCARD size_t property_count() const
Get the number of properties defined in the property manager for this entity.
Definition Ioss_GroupingEntity.h:418
int64_t entityCount
Definition Ioss_GroupingEntity.h:313
unsigned int hash_
Definition Ioss_GroupingEntity.h:316
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:517
virtual IOSS_NODISCARD const GroupingEntity * contained_in() const
Definition Ioss_GroupingEntity.C:75
virtual IOSS_NODISCARD std::string contains_string() const =0
What does this entity contain.
void set_database(DatabaseIO *io_database)
Definition Ioss_GroupingEntity.C:101
State entityState
Definition Ioss_GroupingEntity.h:315
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:346
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:477
A collection of Ioss::Property objects.
Definition Ioss_PropertyManager.h:36
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