GEOS
WrapperBase.hpp
Go to the documentation of this file.
1 /*
2  * ------------------------------------------------------------------------------------------------------------
3  * SPDX-License-Identifier: LGPL-2.1-only
4  *
5  * Copyright (c) 2016-2024 Lawrence Livermore National Security LLC
6  * Copyright (c) 2018-2024 Total, S.A
7  * Copyright (c) 2018-2024 The Board of Trustees of the Leland Stanford Junior University
8  * Copyright (c) 2023-2024 Chevron
9  * Copyright (c) 2019- GEOS/GEOSX Contributors
10  * All rights reserved
11  *
12  * See top level LICENSE, COPYRIGHT, CONTRIBUTORS, NOTICE, and ACKNOWLEDGEMENTS files for details.
13  * ------------------------------------------------------------------------------------------------------------
14  */
15 
18 #ifndef GEOS_DATAREPOSITORY_WRAPPERBASE_HPP_
19 #define GEOS_DATAREPOSITORY_WRAPPERBASE_HPP_
20 
21 #include "common/DataTypes.hpp"
22 #include "common/GEOS_RAJA_Interface.hpp"
23 #include "common/Span.hpp"
24 #include "InputFlags.hpp"
25 #include "xmlWrapper.hpp"
26 #include "RestartFlags.hpp"
27 #include "HistoryDataSpec.hpp"
28 #include "DataContext.hpp"
29 
30 #if defined(GEOS_USE_PYGEOSX)
31 #include "LvArray/src/python/python.hpp"
32 #endif
33 
34 #include <string>
35 #include <memory>
36 #include <set>
37 
38 namespace conduit
39 {
40 class Node;
41 }
42 
43 
44 namespace geos
45 {
46 namespace dataRepository
47 {
48 
49 class Group;
50 
56 {
57 public:
58 
63 
70  explicit WrapperBase( string const & name,
71  Group & parent,
72  string const & rtTypeName );
73 
75  WrapperBase() = delete;
76  WrapperBase( WrapperBase const & ) = delete;
77  WrapperBase( WrapperBase && ) = delete;
78  WrapperBase & operator=( WrapperBase const & ) = delete;
79  WrapperBase & operator=( WrapperBase && ) = delete;
81 
85  virtual ~WrapperBase();
86 
88 
96 
101  virtual localIndex size() const = 0;
102 
106  virtual void const * voidPointer() const = 0;
107 
112  virtual localIndex elementByteSize() const = 0;
113 
117  virtual size_t bytesAllocated() const = 0;
118 
119 
125  virtual void resize( int num_dims, localIndex const * const dims ) = 0;
126 
131  virtual void reserve( localIndex const newCapacity ) = 0;
132 
136  virtual localIndex capacity() const = 0;
137 
142  virtual void resize( localIndex newsize ) = 0;
143 
147  void resize();
148 
154  virtual void copy( localIndex const sourceIndex, localIndex const destIndex ) = 0;
155 
160  virtual void erase( std::set< localIndex > const & indicesToErase ) = 0;
161 
167  virtual void move( LvArray::MemorySpace const space, bool const touch ) const = 0;
168 
172  virtual Regex const & getTypeRegex() const = 0;
173 
177  string const & getRTTypeName() const
178  { return m_rtTypeName; }
179 
187  {
188  m_rtTypeName = rtTypeName;
189  return *this;
190  }
191 
193 
198  virtual bool hasDefaultValue() const = 0;
199 
204  virtual string getDefaultValueString() const = 0;
205 
212  virtual bool processInputFile( xmlWrapper::xmlNode const & targetNode,
213  xmlWrapper::xmlNodePos const & nodePos ) = 0;
214 
223  virtual void addBlueprintField( conduit::Node & fields,
224  string const & name,
225  string const & topology,
226  std::vector< string > const & componentNames = {} ) const = 0;
227 
234  virtual void populateMCArray( conduit::Node & node, std::vector< string > const & componentNames = {} ) const = 0;
235 
244  virtual std::unique_ptr< WrapperBase > averageOverSecondDim( string const & name, Group & group ) const = 0;
245 
250 
254  virtual void registerToWrite() const = 0;
255 
259  virtual void finishWriting() const = 0;
260 
265  virtual bool loadFromConduit() = 0;
266 
268 
274  virtual HistoryMetadata getHistoryMetadata( localIndex const packCount ) const = 0;
275 
282 
288  virtual bool isPackable( bool onDevice ) const = 0;
289 
302  template< bool DO_PACKING >
304  bool withMetadata,
305  bool onDevice,
306  parallelDeviceEvents & events ) const
307  {
308  return DO_PACKING ? packPrivate( buffer, withMetadata, onDevice, events ) : packSizePrivate( withMetadata, onDevice, events );
309  }
310 
324  template< bool DO_PACKING >
326  arrayView1d< localIndex const > const & packList,
327  bool withMetadata,
328  bool onDevice,
329  parallelDeviceEvents & events ) const
330  {
331  return DO_PACKING ? packByIndexPrivate( buffer, packList, withMetadata, onDevice, events ) : packByIndexSizePrivate( packList, withMetadata, onDevice, events );
332  }
333 
345  virtual localIndex unpack( buffer_unit_type const * & buffer,
346  bool withMetadata,
347  bool onDevice,
348  parallelDeviceEvents & events ) = 0;
349 
363  virtual localIndex unpackByIndex( buffer_unit_type const * & buffer,
364  arrayView1d< localIndex const > const & unpackIndices,
365  bool withMetadata,
366  bool onDevice,
367  parallelDeviceEvents & events,
368  MPI_Op op=MPI_REPLACE ) = 0;
369 
371 
376 
381  int sizedFromParent() const
382  {
383  return m_sizedFromParent;
384  }
385 
392  {
393  m_sizedFromParent = val;
394  return *this;
395  }
396 
402 
409  {
410  m_restart_flags = flags;
411  return *this;
412  }
413 
418  PlotLevel getPlotLevel() const { return m_plotLevel; }
419 
426  {
427  m_plotLevel = flag;
428  return *this;
429  }
430 
435  string const & getName() const
436  {
437  return m_name;
438  }
439 
444  string getPath() const;
445 
450  DataContext const & getDataContext() const
451  { return *m_dataContext; }
452 
457  { return *m_parent; }
458 
462  Group const & getParent() const
463  { return *m_parent; }
464 
471  {
472  if( input == InputFlags::OPTIONAL || input == InputFlags::REQUIRED )
473  {
474  this->setSizedFromParent( 0 );
476  }
477  m_inputFlag = input;
478 
479  return *this;
480  }
481 
487  {
488  return m_inputFlag;
489  }
490 
497  {
499  }
500 
506  WrapperBase & setDescription( string const & description )
507  {
508  m_description = description;
509  return *this;
510  }
511 
517  WrapperBase & appendDescription( string const & description )
518  {
519  m_description += description;
520  return *this;
521  }
522 
527  string const & getDescription() const
528  {
529  return m_description;
530  }
531 
537  string dumpInputOptions( bool const outputHeader ) const;
538 
539 
544  std::set< string > const & getRegisteringObjects() const
545  {
546  return m_registeringObjects;
547  }
548 
554  WrapperBase & setRegisteringObjects( string const & objectName )
555  {
556  m_registeringObjects.insert( objectName );
557  return *this;
558  }
559 
561 
566 
571  virtual void copyWrapperAttributes( WrapperBase const & source );
572 
582  virtual std::unique_ptr< WrapperBase > clone( string const & name, Group & parent ) = 0;
583 
588  virtual void copyData( WrapperBase const & source ) = 0;
589 
594  virtual void copyWrapper( WrapperBase const & source ) = 0;
595 
600  virtual std::type_info const & getTypeId() const = 0;
601 
606  virtual int numArrayDims() const = 0;
607 
612  virtual localIndex numArrayComp() const = 0;
613 
627  virtual WrapperBase & setDimLabels( integer dim, Span< string const > labels ) = 0;
628 
636  virtual Span< string const > getDimLabels( integer dim ) const = 0;
637 
639 
640 #if defined(USE_TOTALVIEW_OUTPUT)
646  virtual string totalviewTypeName() const = 0;
647 
653  virtual int setTotalviewDisplay() const;
654 // static int TV_ttf_display_type( const WrapperBase * wrapper);
655 #endif
656 
657 #if defined(GEOS_USE_PYGEOSX)
662  virtual PyObject * createPythonObject( ) = 0;
663 #endif
664 
665 protected:
666 
668 
669  conduit::Node & getConduitNode()
670  {
671  return m_conduitNode;
672  }
673 
675 
681  void createDataContext( xmlWrapper::xmlNode const & targetNode,
682  xmlWrapper::xmlNodePos const & nodePos );
683 
684 protected:
685 
687  string m_name;
688 
691 
694 
697 
700 
703 
706 
709 
711  string m_rtTypeName;
712 
714  std::set< string > m_registeringObjects;
715 
717  conduit::Node & m_conduitNode;
718 
720  std::unique_ptr< DataContext > m_dataContext;
721 
722 private:
723 
735  virtual localIndex packPrivate( buffer_unit_type * & buffer, bool withMetadata, bool onDevice, parallelDeviceEvents & events ) const = 0;
736 
748  virtual localIndex packSizePrivate( bool withMetadata, bool onDevice, parallelDeviceEvents & events ) const = 0;
749 
750 
763  virtual localIndex packByIndexPrivate( buffer_unit_type * & buffer,
764  arrayView1d< localIndex const > const & packList,
765  bool withMetadata,
766  bool onDevice,
767  parallelDeviceEvents & events ) const = 0;
768 
780  virtual localIndex packByIndexSizePrivate( arrayView1d< localIndex const > const & packList,
781  bool withMetadata,
782  bool onDevice,
783  parallelDeviceEvents & events ) const = 0;
784 };
785 
786 }
787 }
788 
789 #endif /* GEOS_DATAREPOSITORY_WRAPPERBASE_HPP_ */
A minimal class to specify information about time history information being collected and output.
Lightweight non-owning wrapper over a contiguous range of elements.
Definition: Span.hpp:42
Base class for all wrappers containing common operations.
Definition: WrapperBase.hpp:56
InputFlags getInputFlag() const
Get the InputFlag of the wrapper.
virtual localIndex unpack(buffer_unit_type const *&buffer, bool withMetadata, bool onDevice, parallelDeviceEvents &events)=0
Unpack the entire wrapped object from a buffer.
virtual bool hasDefaultValue() const =0
Return true iff this wrapper has a valid default value.
string const & getName() const
Get name of the wrapper.
string const & getDescription() const
Get the description string of the wrapper.
virtual localIndex size() const =0
Calls T::size()
virtual localIndex numArrayComp() const =0
Return the number of components in a multidimensional array.
localIndex packByIndex(buffer_unit_type *&buffer, arrayView1d< localIndex const > const &packList, bool withMetadata, bool onDevice, parallelDeviceEvents &events) const
Concrete implementation of the packing by index method.
PlotLevel m_plotLevel
Flag to store the plotLevel.
std::set< string > const & getRegisteringObjects() const
Get the list of names of groups that registered this wrapper.
string const & getRTTypeName() const
virtual void populateMCArray(conduit::Node &node, std::vector< string > const &componentNames={}) const =0
Push the data in the wrapper into a Conduit Blueprint mcarray.
virtual bool processInputFile(xmlWrapper::xmlNode const &targetNode, xmlWrapper::xmlNodePos const &nodePos)=0
Initialize the wrapper from the input xml node.
int sizedFromParent() const
Check whether this wrapper is resized when its parent is resized.
RestartFlags getRestartFlags() const
Get the RestartFlags of the wrapper.
WrapperBase & setInputFlag(InputFlags const input)
Set the InputFlag of the wrapper.
virtual void resize(localIndex newsize)=0
Calls T::resize(newsize) if it exists.
WrapperBase(string const &name, Group &parent, string const &rtTypeName)
Constructor.
conduit::Node & m_conduitNode
A reference to the corresponding conduit::Node.
virtual void move(LvArray::MemorySpace const space, bool const touch) const =0
Calls T::move(space, touch)
Group * m_parent
Pointer to Group that holds this WrapperBase.
virtual void erase(std::set< localIndex > const &indicesToErase)=0
Calls T::erase(indicesToErase)
virtual void finishWriting() const =0
Write the wrapped data into Conduit.
string dumpInputOptions(bool const outputHeader) const
int m_sizedFromParent
Integer to indicate whether or not this wrapped object should be resized when m_parent is resized.
DataContext const & getDataContext() const
WrapperBase & setDescription(string const &description)
Set the description string of the wrapper.
virtual void reserve(localIndex const newCapacity)=0
Calls T::reserve( newCapacity ) if it exists, otherwise a no-op.
virtual WrapperBase & setDimLabels(integer dim, Span< string const > labels)=0
Set dimension labels for an array.
virtual void copyData(WrapperBase const &source)=0
Copy the the data contained in another wrapper into this wrapper.
virtual bool loadFromConduit()=0
Read the wrapped data from Conduit.
WrapperBase & setSizedFromParent(int val)
Set whether this wrapper is resized when its parent is resized.
virtual void resize(int num_dims, localIndex const *const dims)=0
Calls T::resize( num_dims, dims )
virtual localIndex elementByteSize() const =0
virtual void copyWrapperAttributes(WrapperBase const &source)
Copy attributes from another wrapper.
WrapperBase & setRegisteringObjects(string const &objectName)
Add a new name to the list of groups that register this wrapper.
virtual void registerToWrite() const =0
Register the wrapper's data for writing with Conduit.
WrapperBase & setRestartFlags(RestartFlags flags)
Set the RestartFlags of the wrapper.
Group const & getParent() const
string m_rtTypeName
A string regex to validate the input values string to parse for the wrapped object.
WrapperBase & appendDescription(string const &description)
Add up more text to the existing description string of the wrapper.
virtual localIndex unpackByIndex(buffer_unit_type const *&buffer, arrayView1d< localIndex const > const &unpackIndices, bool withMetadata, bool onDevice, parallelDeviceEvents &events, MPI_Op op=MPI_REPLACE)=0
For indexable types, unpack selected indices of wrapped object from a buffer.
virtual localIndex capacity() const =0
std::unique_ptr< DataContext > m_dataContext
A DataContext object that can helps to contextualize this Group.
virtual std::unique_ptr< WrapperBase > averageOverSecondDim(string const &name, Group &group) const =0
Create a new Wrapper with values averaged over the second dimension.
void createDataContext(xmlWrapper::xmlNode const &targetNode, xmlWrapper::xmlNodePos const &nodePos)
Sets the m_dataContext to a DataFileContext by retrieving the attribute file line.
bool getSuccessfulReadFromInput() const
Returns flag that indicates whether the contents of the wrapper have been successfully read from the ...
virtual size_t bytesAllocated() const =0
string m_description
A string description of the wrapped object.
PlotLevel getPlotLevel() const
Get PlotLevel for this wrapper.
void resize()
Calls resize(newsize) where newsize is taken from the parent Group.
virtual ~WrapperBase()
Default destructor.
virtual void copy(localIndex const sourceIndex, localIndex const destIndex)=0
Calls T::copy(sourceIndex, destIndex)
bool m_successfulReadFromInput
Flag to indicate if wrapped object was successfully read from input.
virtual bool isPackable(bool onDevice) const =0
Check whether wrapped type is can be packed into a buffer on host or device.
std::set< string > m_registeringObjects
A vector of the names of the objects that created this Wrapper.
virtual Regex const & getTypeRegex() const =0
virtual string getDefaultValueString() const =0
Return a string representing the default value.
virtual std::type_info const & getTypeId() const =0
Get the typeid of T.
virtual void const * voidPointer() const =0
virtual void copyWrapper(WrapperBase const &source)=0
Copies the contents of a Wrapper into *this.
virtual int numArrayDims() const =0
Return the number of dimensions of the array.
WrapperBase & setRTTypeName(string_view rtTypeName)
override the rtType to use when parsing an input value to the wrapped object. It can be useful to cha...
virtual HistoryMetadata getHistoryMetadata(localIndex const packCount) const =0
Get a description of the wrapped data for time history collection/output.
virtual Span< string const > getDimLabels(integer dim) const =0
Get dimension labels of an array.
localIndex pack(buffer_unit_type *&buffer, bool withMetadata, bool onDevice, parallelDeviceEvents &events) const
Concrete implementation of the packing method.
InputFlags m_inputFlag
Flag to store if this wrapped object should be read from input.
string m_name
Name of the object that is being wrapped.
WrapperBase & setPlotLevel(PlotLevel const flag)
Set the PlotLevel of the wrapper.
virtual void addBlueprintField(conduit::Node &fields, string const &name, string const &topology, std::vector< string > const &componentNames={}) const =0
Push the data in the wrapper into a Conduit blueprint field.
RestartFlags m_restart_flags
Flag to determine the restart behavior for this wrapped object.
string getPath() const
Return the path to this Wrapper in the data repository.
virtual std::unique_ptr< WrapperBase > clone(string const &name, Group &parent)=0
Creates a clone of *this WrapperBase.
@ OPTIONAL
Optional in input.
@ REQUIRED
Required in input.
pugi::xml_node xmlNode
Definition: xmlWrapper.hpp:59
ArrayView< T, 1 > arrayView1d
Alias for 1D array view.
Definition: DataTypes.hpp:180
GEOS_LOCALINDEX_TYPE localIndex
Local index type (for indexing objects within an MPI partition).
Definition: DataTypes.hpp:85
std::int32_t integer
Signed integer type.
Definition: DataTypes.hpp:82
@ Node
location is node (like displacements in finite elements)
signed char buffer_unit_type
Type stored in communication buffers.
Definition: DataTypes.hpp:109
std::string_view string_view
String type.
Definition: DataTypes.hpp:94