GEOSX
WrapperBase.hpp
Go to the documentation of this file.
1 /*
2  * ------------------------------------------------------------------------------------------------------------
3  * SPDX-License-Identifier: LGPL-2.1-only
4  *
5  * Copyright (c) 2018-2020 Lawrence Livermore National Security LLC
6  * Copyright (c) 2018-2020 The Board of Trustees of the Leland Stanford Junior University
7  * Copyright (c) 2018-2020 TotalEnergies
8  * Copyright (c) 2019- GEOSX Contributors
9  * All rights reserved
10  *
11  * See top level LICENSE, COPYRIGHT, CONTRIBUTORS, NOTICE, and ACKNOWLEDGEMENTS files for details.
12  * ------------------------------------------------------------------------------------------------------------
13  */
14 
17 #ifndef GEOS_DATAREPOSITORY_WRAPPERBASE_HPP_
18 #define GEOS_DATAREPOSITORY_WRAPPERBASE_HPP_
19 
20 #include "common/DataTypes.hpp"
21 #include "common/GEOS_RAJA_Interface.hpp"
22 #include "common/Span.hpp"
23 #include "InputFlags.hpp"
24 #include "xmlWrapper.hpp"
25 #include "RestartFlags.hpp"
26 #include "HistoryDataSpec.hpp"
27 #include "DataContext.hpp"
28 
29 #if defined(GEOSX_USE_PYGEOSX)
30 #include "LvArray/src/python/python.hpp"
31 #endif
32 
33 #include <string>
34 #include <memory>
35 #include <set>
36 
37 namespace conduit
38 {
39 class Node;
40 }
41 
42 
43 namespace geos
44 {
45 namespace dataRepository
46 {
47 
48 class Group;
49 
55 {
56 public:
57 
62 
69  explicit WrapperBase( string const & name,
70  Group & parent,
71  string const & rtTypeName );
72 
74  WrapperBase() = delete;
75  WrapperBase( WrapperBase const & ) = delete;
76  WrapperBase( WrapperBase && ) = delete;
77  WrapperBase & operator=( WrapperBase const & ) = delete;
78  WrapperBase & operator=( WrapperBase && ) = delete;
80 
84  virtual ~WrapperBase();
85 
87 
95 
100  virtual localIndex size() const = 0;
101 
105  virtual void const * voidPointer() const = 0;
106 
111  virtual localIndex elementByteSize() const = 0;
112 
116  virtual size_t bytesAllocated() const = 0;
117 
118 
124  virtual void resize( int num_dims, localIndex const * const dims ) = 0;
125 
130  virtual void reserve( localIndex const newCapacity ) = 0;
131 
135  virtual localIndex capacity() const = 0;
136 
141  virtual void resize( localIndex newsize ) = 0;
142 
146  void resize();
147 
153  virtual void copy( localIndex const sourceIndex, localIndex const destIndex ) = 0;
154 
159  virtual void erase( std::set< localIndex > const & indicesToErase ) = 0;
160 
166  virtual void move( LvArray::MemorySpace const space, bool const touch ) const = 0;
167 
171  virtual Regex const & getTypeRegex() const = 0;
172 
176  string const & getRTTypeName() const
177  { return m_rtTypeName; }
178 
186  {
187  m_rtTypeName = rtTypeName;
188  return *this;
189  }
190 
192 
197  virtual bool hasDefaultValue() const = 0;
198 
203  virtual string getDefaultValueString() const = 0;
204 
211  virtual bool processInputFile( xmlWrapper::xmlNode const & targetNode,
212  xmlWrapper::xmlNodePos const & nodePos ) = 0;
213 
222  virtual void addBlueprintField( conduit::Node & fields,
223  string const & name,
224  string const & topology,
225  std::vector< string > const & componentNames = {} ) const = 0;
226 
233  virtual void populateMCArray( conduit::Node & node, std::vector< string > const & componentNames = {} ) const = 0;
234 
243  virtual std::unique_ptr< WrapperBase > averageOverSecondDim( string const & name, Group & group ) const = 0;
244 
249 
253  virtual void registerToWrite() const = 0;
254 
258  virtual void finishWriting() const = 0;
259 
264  virtual bool loadFromConduit() = 0;
265 
267 
273  virtual HistoryMetadata getHistoryMetadata( localIndex const packCount ) const = 0;
274 
281 
287  virtual bool isPackable( bool onDevice ) const = 0;
288 
301  template< bool DO_PACKING >
303  bool withMetadata,
304  bool onDevice,
305  parallelDeviceEvents & events ) const
306  {
307  return DO_PACKING ? packPrivate( buffer, withMetadata, onDevice, events ) : packSizePrivate( withMetadata, onDevice, events );
308  }
309 
323  template< bool DO_PACKING >
325  arrayView1d< localIndex const > const & packList,
326  bool withMetadata,
327  bool onDevice,
328  parallelDeviceEvents & events ) const
329  {
330  return DO_PACKING ? packByIndexPrivate( buffer, packList, withMetadata, onDevice, events ) : packByIndexSizePrivate( packList, withMetadata, onDevice, events );
331  }
332 
344  virtual localIndex unpack( buffer_unit_type const * & buffer,
345  bool withMetadata,
346  bool onDevice,
347  parallelDeviceEvents & events ) = 0;
348 
362  virtual localIndex unpackByIndex( buffer_unit_type const * & buffer,
363  arrayView1d< localIndex const > const & unpackIndices,
364  bool withMetadata,
365  bool onDevice,
366  parallelDeviceEvents & events,
367  MPI_Op op=MPI_REPLACE ) = 0;
368 
370 
375 
380  int sizedFromParent() const
381  {
382  return m_sizedFromParent;
383  }
384 
391  {
392  m_sizedFromParent = val;
393  return *this;
394  }
395 
401 
408  {
409  m_restart_flags = flags;
410  return *this;
411  }
412 
417  PlotLevel getPlotLevel() const { return m_plotLevel; }
418 
425  {
426  m_plotLevel = flag;
427  return *this;
428  }
429 
434  string const & getName() const
435  {
436  return m_name;
437  }
438 
443  string getPath() const;
444 
449  DataContext const & getDataContext() const
450  { return *m_dataContext; }
451 
456  { return *m_parent; }
457 
461  Group const & getParent() const
462  { return *m_parent; }
463 
470  {
471  if( input == InputFlags::OPTIONAL || input == InputFlags::REQUIRED )
472  {
473  this->setSizedFromParent( 0 );
475  }
476  m_inputFlag = input;
477 
478  return *this;
479  }
480 
486  {
487  return m_inputFlag;
488  }
489 
496  {
498  }
499 
505  WrapperBase & setDescription( string const & description )
506  {
507  m_description = description;
508  return *this;
509  }
510 
515  string const & getDescription() const
516  {
517  return m_description;
518  }
519 
525  string dumpInputOptions( bool const outputHeader ) const;
526 
527 
532  std::set< string > const & getRegisteringObjects() const
533  {
534  return m_registeringObjects;
535  }
536 
542  WrapperBase & setRegisteringObjects( string const & objectName )
543  {
544  m_registeringObjects.insert( objectName );
545  return *this;
546  }
547 
549 
554 
559  virtual void copyWrapperAttributes( WrapperBase const & source );
560 
570  virtual std::unique_ptr< WrapperBase > clone( string const & name, Group & parent ) = 0;
571 
576  virtual void copyData( WrapperBase const & source ) = 0;
577 
582  virtual void copyWrapper( WrapperBase const & source ) = 0;
583 
588  virtual std::type_info const & getTypeId() const = 0;
589 
594  virtual int numArrayDims() const = 0;
595 
600  virtual localIndex numArrayComp() const = 0;
601 
615  virtual WrapperBase & setDimLabels( integer dim, Span< string const > labels ) = 0;
616 
624  virtual Span< string const > getDimLabels( integer dim ) const = 0;
625 
627 
628 #if defined(USE_TOTALVIEW_OUTPUT)
634  virtual string totalviewTypeName() const = 0;
635 
641  virtual int setTotalviewDisplay() const;
642 // static int TV_ttf_display_type( const WrapperBase * wrapper);
643 #endif
644 
645 #if defined(GEOSX_USE_PYGEOSX)
650  virtual PyObject * createPythonObject( ) = 0;
651 #endif
652 
653 protected:
654 
656 
657  conduit::Node & getConduitNode()
658  {
659  return m_conduitNode;
660  }
661 
663 
669  void createDataContext( xmlWrapper::xmlNode const & targetNode,
670  xmlWrapper::xmlNodePos const & nodePos );
671 
672 protected:
673 
675  string m_name;
676 
679 
682 
685 
688 
691 
694 
697 
699  string m_rtTypeName;
700 
702  std::set< string > m_registeringObjects;
703 
705  conduit::Node & m_conduitNode;
706 
708  std::unique_ptr< DataContext > m_dataContext;
709 
710 private:
711 
723  virtual localIndex packPrivate( buffer_unit_type * & buffer, bool withMetadata, bool onDevice, parallelDeviceEvents & events ) const = 0;
724 
736  virtual localIndex packSizePrivate( bool withMetadata, bool onDevice, parallelDeviceEvents & events ) const = 0;
737 
738 
751  virtual localIndex packByIndexPrivate( buffer_unit_type * & buffer,
752  arrayView1d< localIndex const > const & packList,
753  bool withMetadata,
754  bool onDevice,
755  parallelDeviceEvents & events ) const = 0;
756 
768  virtual localIndex packByIndexSizePrivate( arrayView1d< localIndex const > const & packList,
769  bool withMetadata,
770  bool onDevice,
771  parallelDeviceEvents & events ) const = 0;
772 };
773 
774 }
775 }
776 
777 #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:41
Base class for all wrappers containing common operations.
Definition: WrapperBase.hpp:55
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.
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:57
ArrayView< T, 1 > arrayView1d
Alias for 1D array view.
Definition: DataTypes.hpp:220
std::int32_t integer
Signed integer type.
Definition: DataTypes.hpp:122
@ Node
location is node (like displacements in finite elements)
signed char buffer_unit_type
Type stored in communication buffers.
Definition: DataTypes.hpp:149
GEOSX_LOCALINDEX_TYPE localIndex
Local index type (for indexing objects within an MPI partition).
Definition: DataTypes.hpp:125
std::string_view string_view
String type.
Definition: DataTypes.hpp:134
The regular expression data for validating inputs. Use rtTypes to get the regex of a type,...
Definition: DataTypes.hpp:513