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