GEOSX
Wrapper.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 
19 #ifndef GEOS_DATAREPOSITORY_WRAPPER_HPP_
20 #define GEOS_DATAREPOSITORY_WRAPPER_HPP_
21 
22 // Source inclues
23 #include "wrapperHelpers.hpp"
24 #include "KeyNames.hpp"
25 #include "LvArray/src/limits.hpp"
26 #include "common/DataTypes.hpp"
27 #include "codingUtilities/SFINAE_Macros.hpp"
28 #include "LvArray/src/Macros.hpp"
29 #include "BufferOps.hpp"
30 #include "BufferOpsDevice.hpp"
31 #include "RestartFlags.hpp"
32 #include "codingUtilities/traits.hpp"
33 #include "common/GeosxConfig.hpp"
34 #include "DefaultValue.hpp"
35 #include "LvArray/src/system.hpp"
36 #include "WrapperBase.hpp"
37 
38 // System includes
39 #include <type_traits>
40 #include <cstdlib>
41 #include <type_traits>
42 
43 namespace geos
44 {
45 
46 namespace dataRepository
47 {
48 //template< typename U >
49 //static void totalViewType( char * const dataType );
50 
55 template< typename T >
56 class Wrapper final : public WrapperBase
57 {
58 public:
59 
63  using TYPE = T;
64 
69 
75  explicit Wrapper( string const & name,
76  Group & parent ):
77  WrapperBase( name, parent, rtTypes::getTypeName( typeid( T ) ) ),
78  m_ownsData( true ),
79  m_isClone( false ),
80  m_data( new T() ),
81  m_default()
82  {
83  if( traits::is_tensorT< T > || std::is_arithmetic< T >::value || traits::is_string< T > )
84  {
85  setSizedFromParent( 0 );
86  }
87 
88  setName();
89  }
90 
97  explicit Wrapper( string const & name,
98  Group & parent,
99  std::unique_ptr< T > object ):
100  WrapperBase( name, parent, rtTypes::getTypeName( typeid( T ) ) ),
101  m_ownsData( true ),
102  m_isClone( false ),
103  m_data( object.release() ),
104  m_default()
105  {
106  if( traits::is_tensorT< T > || std::is_arithmetic< T >::value || traits::is_string< T > )
107  {
108  setSizedFromParent( 0 );
109  }
110 
111  setName();
112  }
113 
120  explicit Wrapper( string const & name,
121  Group & parent,
122  T * object ):
123  WrapperBase( name, parent, rtTypes::getTypeName( typeid( T ) ) ),
124  m_ownsData( false ),
125  m_isClone( false ),
126  m_data( object ),
127  m_default()
128  {
129  if( traits::is_tensorT< T > || std::is_arithmetic< T >::value || traits::is_string< T > )
130  {
131  setSizedFromParent( 0 );
132  }
133 
134  setName();
135  }
136 
142  virtual ~Wrapper() noexcept override
143  {
144  if( m_ownsData )
145  {
146  delete m_data;
147  }
148  //tvTemplateInstantiation();
149  }
150 
157  Wrapper & operator=( Wrapper const & source )
158  {
159  m_data = source.m_data;
160  return *this;
161  }
162 
168  Wrapper & operator=( Wrapper && source )
169  {
170  m_data = std::move( source.m_data );
171  return *this;
172  }
173 
175 
180 
182  virtual std::unique_ptr< WrapperBase > clone( string const & name,
183  Group & parent ) override
184  {
185  std::unique_ptr< Wrapper< T > > clonedWrapper = std::make_unique< Wrapper< T > >( name, parent, m_data );
186  clonedWrapper->copyWrapperAttributes( *this );
187  clonedWrapper->m_isClone = true;
188  return clonedWrapper;
189  }
190 
191  virtual void copyWrapper( WrapperBase const & source ) override
192  {
193  GEOS_ERROR_IF( source.getName() != m_name, "Tried to copy wrapper with a different name" );
194  copyWrapperAttributes( source );
195  copyData( source );
196  }
197 
199  virtual void copyWrapperAttributes( WrapperBase const & source ) override
200  {
202  Wrapper< T > const & castedSource = dynamicCast< Wrapper< T > const & >( source );
203  m_ownsData = castedSource.m_ownsData;
204  m_default = castedSource.m_default;
205  m_dimLabels = castedSource.m_dimLabels;
206  }
207 
209  virtual const std::type_info & getTypeId() const noexcept override
210  {
211  return typeid(T);
212  }
213 
220  static Wrapper & cast( WrapperBase & wrapper )
221  {
222  GEOS_ERROR_IF( wrapper.getTypeId() != typeid( T ),
223  "Invalid downcast to Wrapper< " << LvArray::system::demangleType< T >() << " >" );
224  return static_cast< Wrapper< T > & >( wrapper );
225  }
226 
233  static Wrapper< T > const & cast( WrapperBase const & wrapper )
234  {
235  GEOS_ERROR_IF( wrapper.getTypeId() != typeid( T ),
236  "Invalid downcast to Wrapper< " << LvArray::system::demangleType< T >() << " >" );
237  return static_cast< Wrapper< T > const & >( wrapper );
238  }
239 
241 
242  virtual int numArrayDims() const override
243  {
244  return wrapperHelpers::numArrayDims( reference() );
245  }
246 
247  virtual localIndex numArrayComp() const override
248  {
249  return wrapperHelpers::numArrayComp( reference() );
250  }
251 
252  virtual Wrapper & setDimLabels( integer const dim, Span< string const > const labels ) override
253  {
254  m_dimLabels.set( dim, labels );
255  return *this;
256  }
257 
258  virtual Span< string const > getDimLabels( integer const dim ) const override
259  {
260  return m_dimLabels.get( dim );
261  }
262 
264 
266  virtual
267  HistoryMetadata getHistoryMetadata( localIndex const packCount = -1 ) const override final
268  {
269  return geos::getHistoryMetadata( getName(), referenceAsView( ), numArrayComp(), packCount );
270  }
271 
276 
279  virtual
280  bool isPackable( bool onDevice ) const override
281  {
282  if( onDevice )
283  {
284  // this isn't accurate if array/arraview return false for this, which I think they do
285  return bufferOps::can_memcpy< T >;
286  }
287  else
288  {
289  return bufferOps::is_packable< T >;
290  }
291  }
292 
295  virtual
296  localIndex unpack( buffer_unit_type const * & buffer, bool withMetadata, bool onDevice, parallelDeviceEvents & events ) override final
297  {
298  localIndex unpackedSize = 0;
299  if( withMetadata )
300  {
301  string name;
302  unpackedSize += bufferOps::Unpack( buffer, name );
303  GEOS_ERROR_IF( name != getName(), "buffer unpack leads to wrapper names that don't match" );
304  }
305  if( onDevice )
306  {
307  if( withMetadata )
308  {
309  unpackedSize += wrapperHelpers::UnpackDevice( buffer, referenceAsView(), events );
310  }
311  else
312  {
313  unpackedSize += wrapperHelpers::UnpackDataDevice( buffer, referenceAsView(), events );
314  }
315  }
316  else
317  {
318  unpackedSize += bufferOps::Unpack( buffer, *m_data );
319  }
320  return unpackedSize;
321  }
322 
325  virtual
327  arrayView1d< localIndex const > const & unpackIndices,
328  bool withMetadata,
329  bool onDevice,
330  parallelDeviceEvents & events,
331  MPI_Op op ) override final
332  {
333  localIndex unpackedSize = 0;
334 
335  if( withMetadata )
336  {
337  string name;
338  unpackedSize += bufferOps::Unpack( buffer, name );
339  GEOS_ERROR_IF( name != getName(), "buffer unpack leads to wrapper names that don't match" );
340  }
341  if( onDevice )
342  {
343  if( withMetadata )
344  {
345  unpackedSize += wrapperHelpers::UnpackByIndexDevice( buffer, referenceAsView(), unpackIndices, events, op );
346  }
347  else
348  {
349  unpackedSize += wrapperHelpers::UnpackDataByIndexDevice( buffer, referenceAsView(), unpackIndices, events, op );
350  }
351  }
352  else
353  {
354  unpackedSize += wrapperHelpers::UnpackByIndex( buffer, *m_data, unpackIndices );
355  }
356 
357  return unpackedSize;
358  }
359 
361 
363  void const * voidPointer() const override
364  { return wrapperHelpers::dataPtr( *m_data ); }
365 
367  virtual localIndex elementByteSize() const override
368  { return wrapperHelpers::byteSizeOfElement< T >(); }
369 
370  virtual size_t bytesAllocated() const override final
371  {
372  return m_isClone ? 0 : wrapperHelpers::byteSize< T >( *m_data );
373  }
374 
375 
383 
385  virtual localIndex size() const override
386  { return wrapperHelpers::size( *m_data ); }
387 
389  virtual void resize( int ndims, localIndex const * const dims ) override
390  {
391  wrapperHelpers::move( *m_data, hostMemorySpace, true );
392  wrapperHelpers::resizeDimensions( *m_data, ndims, dims );
393  }
394 
396  virtual void reserve( localIndex const newCapacity ) override
397  {
398  wrapperHelpers::move( *m_data, hostMemorySpace, true );
399  wrapperHelpers::reserve( reference(), newCapacity );
400  }
401 
403  virtual localIndex capacity() const override
404  {
405  // We don't use reference() here because that would return an ArrayView which has no capacity method.
406  return wrapperHelpers::capacity( *m_data );
407  }
408 
410  virtual void resize( localIndex const newSize ) override
411  {
412  wrapperHelpers::move( *m_data, hostMemorySpace, true );
413  wrapperHelpers::resizeDefault( reference(), newSize, m_default );
414  }
415 
417  struct copy_wrapper
418  {
419  template< typename U, int NDIM, typename PERMUTATION >
420  static void copy( Array< U, NDIM, PERMUTATION > const & array, localIndex const sourceIndex, localIndex const destIndex )
421  {
422  LvArray::forValuesInSliceWithIndices( array[ sourceIndex ],
423  [destIndex, &array]( U const & sourceVal, auto const ... indicesToErase )
424  {
425  array( destIndex, indicesToErase ... ) = sourceVal;
426  } );
427  }
428 
429  template< typename U >
430  static void copy( U const &, localIndex const, localIndex const )
431  {}
432 
433  template< typename U=T >
434  static std::enable_if_t< traits::hasCopyAssignmentOp< U > >
435  copyData( U & destinationData, U const & sourceData )
436  {
437  destinationData = sourceData;
438  }
439 
440  template< typename U=T >
441  static std::enable_if_t< !traits::hasCopyAssignmentOp< U > >
442  copyData( U &, U const & )
443  {}
444  };
446 
448  virtual void copy( localIndex const sourceIndex, localIndex const destIndex ) override
449  {
450  copy_wrapper::copy( reference(), sourceIndex, destIndex );
451  }
452 
453 
454 
455  virtual void copyData( WrapperBase const & source ) override
456  {
457  Wrapper< T > const & castedSource = dynamicCast< Wrapper< T > const & >( source );
458  copy_wrapper::copyData( *m_data, *castedSource.m_data );
459  }
460 
461 
463  struct erase_wrapper // This should probably be in LvArray?
464  {
465  template< typename TYPE >
466  static void erase( TYPE &, std::set< localIndex > const & )
467  {}
468 
469  template< typename TYPE >
470  static void erase( array1d< TYPE > & array, std::set< localIndex > const & indicesToErase )
471  {
472  int oldSize = array.size( 0 );
473  int numToErase = indicesToErase.size();
474  int newSize = oldSize - numToErase;
475  std::set< localIndex >::iterator it = indicesToErase.begin();
476  int offset = 0;
477  for( localIndex i=*it+1; i<oldSize; i++ )
478  {
479  if( i == *it + 1 )
480  {
481  offset++;
482  if( offset < numToErase )
483  {
484  it++;
485  }
486  }
487  array[i-offset] = array[i];
488  }
489  array.resize( newSize );
490  }
491 
492  template< typename TYPE >
493  static void erase( array2d< TYPE > & array, std::set< localIndex > const & indicesToErase )
494  {
495  int oldSize = array.size( 0 );
496  int numToErase = indicesToErase.size();
497  int newSize = oldSize - numToErase;
498  int dim1 = array.size( 1 );
499  std::set< localIndex >::iterator it = indicesToErase.begin();
500  int offset = 0;
501  for( localIndex i=*it+1; i<oldSize; i++ )
502  {
503  if( i == *it + 1 )
504  {
505  offset++;
506  if( offset < numToErase )
507  {
508  it++;
509  }
510  }
511  for( int j=0; j<dim1; j++ )
512  {
513  array[i-offset][j] = array[i][j];
514  }
515  }
516  array.resize( newSize );
517  }
518 
519  template< typename TYPE >
520  static void erase( array3d< TYPE > & array, std::set< localIndex > const & indicesToErase )
521  {
522  int oldSize = array.size( 0 );
523  int numToErase = indicesToErase.size();
524  int newSize = oldSize - numToErase;
525  int dim1 = array.size( 1 );
526  int dim2 = array.size( 2 );
527  std::set< localIndex >::iterator it = indicesToErase.begin();
528  int offset = 0;
529  for( localIndex i=*it+1; i<oldSize; i++ )
530  {
531  if( i == *it + 1 )
532  {
533  offset++;
534  if( offset < numToErase )
535  {
536  it++;
537  }
538  }
539  for( int j=0; j<dim1; j++ )
540  {
541  for( int k=0; k<dim2; k++ )
542  {
543  array[i-offset][j][k] = array[i][j][k];
544  }
545  }
546  }
547  array.resize( newSize );
548  }
549  };
551 
552 
554  void erase( std::set< localIndex > const & indicesToErase ) override
555  {
556  GEOS_ERROR_IF( indicesToErase.size() == 0, "Wrapper::erase() can only be called on a populated set of indices!" );
557  erase_wrapper::erase( reference(), indicesToErase );
558  }
559 
560 
562  virtual void move( LvArray::MemorySpace const space, bool const touch ) const override
563  { return wrapperHelpers::move( *m_data, space, touch ); }
564 
566  virtual Regex const & getTypeRegex() const override
567  { return rtTypes::getTypeRegex< T >( m_rtTypeName ); }
568 
570 
575 
580  T & reference()
581  { return *m_data; }
582 
589  { return referenceAsView(); }
590 
599  template< typename _T=T, typename=std::enable_if_t< traits::HasMemberFunction_toView< _T > > >
601  { return m_data->toView(); }
602 
606  template< typename _T=T, typename=std::enable_if_t< !traits::HasMemberFunction_toView< _T > > >
608  { return *m_data; }
609 
613  template< typename _T=T, typename=std::enable_if_t< traits::HasMemberFunction_toView< _T > > >
615  { return m_data->toViewConst(); }
616 
620  template< typename _T=T, typename=std::enable_if_t< !traits::HasMemberFunction_toView< _T > > >
621  T const & referenceAsView() const
622  { return *m_data; }
623 
625 
630 
634  virtual bool hasDefaultValue() const final override
635  {
636  return m_default.has_default_value;
637  }
638 
643  template< typename U=T >
644  DefaultValue< T > const &
646  {
647  return m_default;
648  }
649 
650 
655  template< typename U=T >
656  std::enable_if_t< DefaultValue< U >::has_default_value, typename DefaultValue< U >::value_type const & >
658  {
659  return m_default.value;
660  }
661 
667  template< typename U=T >
668  std::enable_if_t< DefaultValue< U >::has_default_value, Wrapper< T > & >
669  setDefaultValue( typename DefaultValue< U >::value_type const & defaultVal )
670  {
671  m_default.value = defaultVal;
672  return *this;
673  }
674 
680  template< typename U=T >
681  std::enable_if_t< !traits::is_array< U > && DefaultValue< U >::has_default_value, Wrapper< T > & >
682  setApplyDefaultValue( typename DefaultValue< U >::value_type const & defaultVal )
683  {
684  m_default.value = defaultVal;
685  *m_data = m_default.value;
686  return *this;
687  }
688 
694  template< typename U=T >
695  std::enable_if_t< traits::is_array< U > && DefaultValue< U >::has_default_value, Wrapper< T > & >
696  setApplyDefaultValue( typename DefaultValue< U >::value_type const & defaultVal )
697  {
698  m_default.value = defaultVal;
699  m_data->template setValues< serialPolicy >( m_default.value );
700  return *this;
701  }
702 
706  virtual string getDefaultValueString() const override
707  {
708  std::ostringstream ss;
709  ss << std::string( numArrayDims(), '{' ) << m_default << std::string( numArrayDims(), '}' );
710  return ss.str();
711  }
712 
713  virtual bool processInputFile( xmlWrapper::xmlNode const & targetNode,
714  xmlWrapper::xmlNodePos const & nodePos ) override
715  {
716  InputFlags const inputFlag = getInputFlag();
717  if( inputFlag >= InputFlags::OPTIONAL )
718  {
719  try
720  {
721  if( inputFlag == InputFlags::REQUIRED || !hasDefaultValue() )
722  {
724  getName(),
725  rtTypes::getTypeRegex< T >( getRTTypeName() ),
726  targetNode,
727  inputFlag == InputFlags::REQUIRED );
729  GEOS_FMT( "XML Node {} ({}) with name={} is missing required attribute '{}'."
730  "Available options are:\n {}\n For more details, please refer to documentation at:\n"
731  "http://geosx-geosx.readthedocs-hosted.com/en/latest/docs/sphinx/userGuide/Index.html",
732  targetNode.name(), nodePos.toString(), targetNode.attribute( "name" ).value(),
733  getName(), dumpInputOptions( true ) ),
734  InputError );
735  }
736  else
737  {
739  getName(),
740  rtTypes::getTypeRegex< T >( getRTTypeName() ),
741  targetNode,
743  }
744  }
745  catch( std::exception const & ex )
746  {
747  xmlWrapper::processInputException( ex, getName(), targetNode, nodePos );
748  }
749 
751  createDataContext( targetNode, nodePos );
752 
753  return true;
754  }
755 
756  return false;
757  }
758 
760 
766  void setName()
767  { wrapperHelpers::setName( reference(), m_conduitNode.path() ); }
768 
769 
771  void addBlueprintField( conduit::Node & fields,
772  string const & name,
773  string const & topology,
774  std::vector< string > const & componentNames = {} ) const override
775  { wrapperHelpers::addBlueprintField( reference(), fields, name, topology, componentNames ); }
776 
778  void populateMCArray( conduit::Node & node, std::vector< string > const & componentNames = {} ) const override
779  { wrapperHelpers::populateMCArray( reference(), node, componentNames ); }
780 
782  std::unique_ptr< WrapperBase > averageOverSecondDim( string const & name, Group & group ) const override
783  {
784  auto ptr = wrapperHelpers::averageOverSecondDim( reference() );
785  using U = typename decltype( ptr )::element_type;
786 
787  GEOS_ERROR_IF( ptr == nullptr, "Failed to average over the second dimension of." );
788 
789  auto ret = std::make_unique< Wrapper< U > >( name, group, std::move( ptr ) );
790  for( integer dim = 2; dim < numArrayDims(); ++dim )
791  {
792  ret->setDimLabels( dim - 1, getDimLabels( dim ) );
793  }
794 
795  return ret;
796  }
797 
799  void registerToWrite() const override
800  {
801  m_conduitNode.reset();
802 
804  {
805  return;
806  }
807 
808  move( hostMemorySpace, false );
809 
810  m_conduitNode[ "__sizedFromParent__" ].set( sizedFromParent() );
811 
812  wrapperHelpers::pushDataToConduitNode( *m_data, m_conduitNode );
813  }
814 
816  void finishWriting() const override
817  { m_conduitNode.reset(); }
818 
819 
821  bool loadFromConduit() override
822  {
824  {
825  m_conduitNode.reset();
826  return false;
827  }
828 
829  setSizedFromParent( m_conduitNode[ "__sizedFromParent__" ].value() );
830 
831  wrapperHelpers::pullDataFromConduitNode( *m_data, m_conduitNode );
832 
833  m_conduitNode.reset();
834 
835  return true;
836  }
837 
844 
845  /*
846  * @brief Set whether this wrapper is resized when its parent is resized.
847  * @param val an int that is converted into a bool
848  * @return a pointer to this wrapper
849  */
850 
855  {
857  return *this;
858  }
859 
864  {
866  return *this;
867  }
868 
873  {
875  return *this;
876  }
877 
882  {
883  WrapperBase::setInputFlag( input );
884  return *this;
885  }
886 
890  Wrapper< T > & setDescription( string const & description )
891  {
892  WrapperBase::setDescription( description );
893  return *this;
894  }
895 
899  Wrapper< T > & setRegisteringObjects( string const & objectName )
900  {
902  return *this;
903  }
904 
909  {
910  WrapperBase::setRTTypeName( rtTypeName );
911  return *this;
912  }
913 
915 
916 #if defined(USE_TOTALVIEW_OUTPUT)
917  virtual string totalviewTypeName() const override
918  {
919  return LvArray::system::demangle( typeid( Wrapper< T > ).name() );
920  }
921 
922  virtual int setTotalviewDisplay() const override
923  {
924  //std::cout<<"executing Wrapper::setTotalviewDisplay()"<<std::endl;
925  WrapperBase::setTotalviewDisplay();
926  TV_ttf_add_row( "m_ownsData", "bool", &m_ownsData );
927  TV_ttf_add_row( "m_data", LvArray::system::demangle< T >().c_str(), m_data );
928  TV_ttf_add_row( "m_default", LvArray::system::demangle< DefaultValue< T > >().c_str(), &m_default );
929  return 0;
930  }
931 // void tvTemplateInstantiation();
932 #endif
933 
934 #if defined(GEOSX_USE_PYGEOSX)
935  virtual PyObject * createPythonObject( ) override
936  { return wrapperHelpers::createPythonObject( reference() ); }
937 #endif
938 
939 private:
940 
954  template< bool DO_PACKING >
955  localIndex packImpl( buffer_unit_type * & buffer,
956  bool withMetadata,
957  bool onDevice,
958  parallelDeviceEvents & events ) const
959  {
960  localIndex packedSize = 0;
961 
962  if( withMetadata )
963  { packedSize += bufferOps::Pack< DO_PACKING >( buffer, getName() ); }
964  if( onDevice )
965  {
966  if( withMetadata )
967  {
968  packedSize += wrapperHelpers::PackDevice< DO_PACKING >( buffer, reference(), events );
969  }
970  else
971  {
972  packedSize += wrapperHelpers::PackDataDevice< DO_PACKING >( buffer, reference(), events );
973  }
974  }
975  else
976  {
977  packedSize += bufferOps::Pack< DO_PACKING >( buffer, *m_data );
978  }
979 
980  return packedSize;
981  }
982 
996  template< bool DO_PACKING >
997  localIndex packByIndexImpl( buffer_unit_type * & buffer,
998  arrayView1d< localIndex const > const & packList,
999  bool withMetadata,
1000  bool onDevice,
1001  parallelDeviceEvents & events ) const
1002  {
1003  localIndex packedSize = 0;
1004 
1005  if( withMetadata )
1006  { packedSize += bufferOps::Pack< DO_PACKING >( buffer, getName() ); }
1007  if( onDevice )
1008  {
1009  if( withMetadata )
1010  {
1011  packedSize += wrapperHelpers::PackByIndexDevice< DO_PACKING >( buffer, reference(), packList, events );
1012  }
1013  else
1014  {
1015  packedSize += wrapperHelpers::PackDataByIndexDevice< DO_PACKING >( buffer, reference(), packList, events );
1016  }
1017  }
1018  else
1019  {
1020  packedSize += wrapperHelpers::PackByIndex< DO_PACKING >( buffer, *m_data, packList );
1021  }
1022 
1023  return packedSize;
1024  }
1025 
1029  localIndex packPrivate( buffer_unit_type * & buffer,
1030  bool withMetadata,
1031  bool onDevice,
1032  parallelDeviceEvents & events ) const override final
1033  {
1034  return this->packImpl< true >( buffer, withMetadata, onDevice, events );
1035  }
1036 
1040  localIndex packByIndexPrivate( buffer_unit_type * & buffer,
1041  arrayView1d< localIndex const > const & packList,
1042  bool withMetadata,
1043  bool onDevice,
1044  parallelDeviceEvents & events ) const override final
1045  {
1046  return this->packByIndexImpl< true >( buffer, packList, withMetadata, onDevice, events );
1047  }
1048 
1052  localIndex packSizePrivate( bool withMetadata,
1053  bool onDevice,
1054  parallelDeviceEvents & events ) const override final
1055  {
1056  buffer_unit_type * dummy;
1057  return this->packImpl< false >( dummy, withMetadata, onDevice, events );
1058  }
1059 
1063  localIndex packByIndexSizePrivate( arrayView1d< localIndex const > const & packList,
1064  bool withMetadata,
1065  bool onDevice,
1066  parallelDeviceEvents & events ) const override final
1067  {
1068  buffer_unit_type * dummy;
1069  return this->packByIndexImpl< false >( dummy, packList, withMetadata, onDevice, events );
1070  }
1071 
1074  bool m_ownsData;
1075 
1076  bool m_isClone;
1077 
1079  T * m_data;
1080 
1082  DefaultValue< T > m_default;
1083 
1085  wrapperHelpers::ArrayDimLabels< T > m_dimLabels;
1086 };
1087 
1088 }
1089 
1090 } // end of namespace geos
1091 
1092 // Do not remove the following commented code since it's used for debugging with TotalView.
1093 //template< typename T >
1094 //int TV_ttf_display_type( geos::dataRepository::Wrapper<T> const * wrapper)
1095 //{
1096 // std::cout<<"Executing "<<wrapper->totalviewTypeName()<<"::TV_ttf_display_type()"<<std::endl;
1097 // return TV_ttf_format_raw;
1098 //}
1099 //
1100 //template int TV_ttf_display_type( geos::dataRepository::Wrapper<int> const * wrapper );
1101 //
1102 //template< typename T >
1103 //void geos::dataRepository::Wrapper<T>::tvTemplateInstantiation()
1104 //{
1105 // TV_ttf_display_type<T>(this);
1106 //}
1107 
1108 #endif /* GEOS_DATAREPOSITORY_WRAPPER_HPP_ */
#define GEOS_DECLTYPE_AUTO_RETURN
Doxygen can't parse a decltype( auto ) return type, using this gets around that.
#define GEOS_ERROR_IF(EXP, msg)
Conditionally raise a hard error and terminate the program.
Definition: Logger.hpp:107
#define GEOS_THROW_IF(EXP, msg, TYPE)
Conditionally throw an exception.
Definition: Logger.hpp:116
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.
string const & getName() const
Get name of the wrapper.
string const & getRTTypeName() const
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.
conduit::Node & m_conduitNode
A reference to the corresponding conduit::Node.
string dumpInputOptions(bool const outputHeader) const
WrapperBase & setDescription(string const &description)
Set the description string of the wrapper.
WrapperBase & setSizedFromParent(int val)
Set whether this wrapper is resized when its parent is resized.
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.
WrapperBase & setRestartFlags(RestartFlags flags)
Set the RestartFlags of the wrapper.
string m_rtTypeName
A string regex to validate the input values string to parse for the wrapped object.
void createDataContext(xmlWrapper::xmlNode const &targetNode, xmlWrapper::xmlNodePos const &nodePos)
Sets the m_dataContext to a DataFileContext by retrieving the attribute file line.
bool m_successfulReadFromInput
Flag to indicate if wrapped object was successfully read from input.
virtual std::type_info const & getTypeId() const =0
Get the typeid of T.
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...
string m_name
Name of the object that is being wrapped.
WrapperBase & setPlotLevel(PlotLevel const flag)
Set the PlotLevel of the wrapper.
void addBlueprintField(conduit::Node &fields, string const &name, string const &topology, std::vector< string > const &componentNames={}) const override
Push the data in the wrapper into a Conduit blueprint field.
Definition: Wrapper.hpp:771
virtual Wrapper & setDimLabels(integer const dim, Span< string const > const labels) override
Set dimension labels for an array.
Definition: Wrapper.hpp:252
Wrapper< T > & setRTTypeName(string_view rtTypeName)
override the rtType to use when parsing an input value to the wrapped object. It can be useful to cha...
Definition: Wrapper.hpp:908
Wrapper< T > & setRestartFlags(RestartFlags flags)
Set the RestartFlags of the wrapper.
Definition: Wrapper.hpp:863
virtual const std::type_info & getTypeId() const noexcept override
Get the typeid of T.
Definition: Wrapper.hpp:209
Wrapper & operator=(Wrapper &&source)
Move Assignment Operator.
Definition: Wrapper.hpp:168
virtual void copyWrapperAttributes(WrapperBase const &source) override
Copy attributes from another wrapper.
Definition: Wrapper.hpp:199
Wrapper< T > & setInputFlag(InputFlags const input)
Set the InputFlag of the wrapper.
Definition: Wrapper.hpp:881
T TYPE
Alias for the wrapped type T.
Definition: Wrapper.hpp:63
void registerToWrite() const override
Register the wrapper's data for writing with Conduit.
Definition: Wrapper.hpp:799
std::enable_if_t< !traits::is_array< U > &&DefaultValue< U >::has_default_value, Wrapper< T > & > setApplyDefaultValue(typename DefaultValue< U >::value_type const &defaultVal)
Set and apply for default value.
Definition: Wrapper.hpp:682
virtual void resize(localIndex const newSize) override
Calls T::resize(newsize) if it exists.
Definition: Wrapper.hpp:410
virtual void reserve(localIndex const newCapacity) override
Calls T::reserve( newCapacity ) if it exists, otherwise a no-op.
Definition: Wrapper.hpp:396
virtual bool isPackable(bool onDevice) const override
Check whether wrapped type is can be packed into a buffer on host or device.
Definition: Wrapper.hpp:280
Wrapper< T > & setRegisteringObjects(string const &objectName)
Add a new name to the list of groups that register this wrapper.
Definition: Wrapper.hpp:899
static Wrapper< T > const & cast(WrapperBase const &wrapper)
Downcast base to a const typed wrapper.
Definition: Wrapper.hpp:233
GEOS_DECLTYPE_AUTO_RETURN referenceAsView() const
Provide access to wrapped object converted to a view, if possible.
Definition: Wrapper.hpp:614
void setName()
DO_NOT_DOCUMENT.
Definition: Wrapper.hpp:766
T & referenceAsView()
Provide access to wrapped object converted to a view, if possible.
Definition: Wrapper.hpp:607
virtual ~Wrapper() noexcept override
Default destructor.
Definition: Wrapper.hpp:142
Wrapper(string const &name, Group &parent, std::unique_ptr< T > object)
Constructor that takes ownership of an existing object.
Definition: Wrapper.hpp:97
std::enable_if_t< traits::is_array< U > &&DefaultValue< U >::has_default_value, Wrapper< T > & > setApplyDefaultValue(typename DefaultValue< U >::value_type const &defaultVal)
Set and apply for default value.
Definition: Wrapper.hpp:696
virtual std::unique_ptr< WrapperBase > clone(string const &name, Group &parent) override
Creates a clone of *this WrapperBase.
Definition: Wrapper.hpp:182
Wrapper< T > & setPlotLevel(PlotLevel const flag)
Set the PlotLevel of the wrapper.
Definition: Wrapper.hpp:872
virtual void copy(localIndex const sourceIndex, localIndex const destIndex) override
Calls T::copy(sourceIndex, destIndex)
Definition: Wrapper.hpp:448
virtual localIndex capacity() const override
Definition: Wrapper.hpp:403
GEOS_DECLTYPE_AUTO_RETURN referenceAsView()
Provide access to wrapped object converted to a view, if possible.
Definition: Wrapper.hpp:600
GEOS_DECLTYPE_AUTO_RETURN reference() const
const Accessor for m_data
Definition: Wrapper.hpp:588
void populateMCArray(conduit::Node &node, std::vector< string > const &componentNames={}) const override
Push the data in the wrapper into a Conduit Blueprint mcarray.
Definition: Wrapper.hpp:778
virtual size_t bytesAllocated() const override final
Definition: Wrapper.hpp:370
virtual localIndex unpack(buffer_unit_type const *&buffer, bool withMetadata, bool onDevice, parallelDeviceEvents &events) override final
Unpack the entire wrapped object from a buffer.
Definition: Wrapper.hpp:296
virtual HistoryMetadata getHistoryMetadata(localIndex const packCount=-1) const override final
Get a description of the wrapped data for time history collection/output.
Definition: Wrapper.hpp:267
Wrapper(string const &name, Group &parent, T *object)
Constructor that does not take ownership of an existing object.
Definition: Wrapper.hpp:120
Wrapper< T > & setDescription(string const &description)
Set the description string of the wrapper.
Definition: Wrapper.hpp:890
virtual localIndex numArrayComp() const override
Return the number of components in a multidimensional array.
Definition: Wrapper.hpp:247
Wrapper(string const &name, Group &parent)
Constructor that creates a new instance of wrapped type.
Definition: Wrapper.hpp:75
virtual bool processInputFile(xmlWrapper::xmlNode const &targetNode, xmlWrapper::xmlNodePos const &nodePos) override
Initialize the wrapper from the input xml node.
Definition: Wrapper.hpp:713
virtual void resize(int ndims, localIndex const *const dims) override
Calls T::resize( num_dims, dims )
Definition: Wrapper.hpp:389
void finishWriting() const override
Write the wrapped data into Conduit.
Definition: Wrapper.hpp:816
virtual localIndex size() const override
Calls T::size()
Definition: Wrapper.hpp:385
virtual localIndex unpackByIndex(buffer_unit_type const *&buffer, arrayView1d< localIndex const > const &unpackIndices, bool withMetadata, bool onDevice, parallelDeviceEvents &events, MPI_Op op) override final
For indexable types, unpack selected indices of wrapped object from a buffer.
Definition: Wrapper.hpp:326
T const & referenceAsView() const
Provide access to wrapped object converted to a view, if possible.
Definition: Wrapper.hpp:621
virtual string getDefaultValueString() const override
Return a string representing the default value.
Definition: Wrapper.hpp:706
bool loadFromConduit() override
Read the wrapped data from Conduit.
Definition: Wrapper.hpp:821
DefaultValue< T > const & getDefaultValueStruct() const
Accessor for m_default.
Definition: Wrapper.hpp:645
virtual int numArrayDims() const override
Return the number of dimensions of the array.
Definition: Wrapper.hpp:242
void const * voidPointer() const override
Definition: Wrapper.hpp:363
std::enable_if_t< DefaultValue< U >::has_default_value, Wrapper< T > & > setDefaultValue(typename DefaultValue< U >::value_type const &defaultVal)
Setter for default value.
Definition: Wrapper.hpp:669
Wrapper< T > & setSizedFromParent(int val)
Set whether this wrapper is resized when its parent is resized.
Definition: Wrapper.hpp:854
virtual bool hasDefaultValue() const final override
Return true iff this wrapper has a valid default value.
Definition: Wrapper.hpp:634
std::enable_if_t< DefaultValue< U >::has_default_value, typename DefaultValue< U >::value_type const & > getDefaultValue() const
Accessor for default value.
Definition: Wrapper.hpp:657
Wrapper & operator=(Wrapper const &source)
Copy Assignment Operator.
Definition: Wrapper.hpp:157
virtual void copyWrapper(WrapperBase const &source) override
Copies the contents of a Wrapper into *this.
Definition: Wrapper.hpp:191
void erase(std::set< localIndex > const &indicesToErase) override
Calls T::erase(indicesToErase)
Definition: Wrapper.hpp:554
static Wrapper & cast(WrapperBase &wrapper)
Downcast base to a typed wrapper.
Definition: Wrapper.hpp:220
virtual Regex const & getTypeRegex() const override
Definition: Wrapper.hpp:566
virtual void copyData(WrapperBase const &source) override
Copy the the data contained in another wrapper into this wrapper.
Definition: Wrapper.hpp:455
T & reference()
Accessor for m_data.
Definition: Wrapper.hpp:580
virtual void move(LvArray::MemorySpace const space, bool const touch) const override
Calls T::move(space, touch)
Definition: Wrapper.hpp:562
std::unique_ptr< WrapperBase > averageOverSecondDim(string const &name, Group &group) const override
Create a new Wrapper with values averaged over the second dimension.
Definition: Wrapper.hpp:782
virtual localIndex elementByteSize() const override
Definition: Wrapper.hpp:367
virtual Span< string const > getDimLabels(integer const dim) const override
Get dimension labels of an array.
Definition: Wrapper.hpp:258
Static class to manage the type selection of types at runtime and obtain the regexes of these types....
Definition: DataTypes.hpp:559
@ OPTIONAL
Optional in input.
@ REQUIRED
Required in input.
internal::Helper< T > DefaultValue
A templated alias to hold default values.
@ WRITE_AND_READ
Write and read from restart.
@ NO_WRITE
Do not write into restart.
pugi::xml_node xmlNode
Definition: xmlWrapper.hpp:57
std::enable_if_t< !internal::canParseVariable< T >, bool > readAttributeAsType(T &, string const &name, Regex const &, xmlNode const &, U const &)
Extract attribute in an xml tree, and translate its value into a typed variable. This SFINAE implemen...
Definition: xmlWrapper.hpp:444
void processInputException(std::exception const &ex, string const &targetAttributeName, xmlWrapper::xmlNode const &targetNode, xmlWrapper::xmlNodePos const &nodePos)
Helper method to process an exception that has been thrown during xml parsing.
ArrayView< T, 1 > arrayView1d
Alias for 1D array view.
Definition: DataTypes.hpp:220
LvArray::Array< T, NDIM, PERMUTATION, localIndex, LvArray::ChaiBuffer > Array
Multidimensional array type. See LvArray:Array for details.
Definition: DataTypes.hpp:182
std::string string
String type.
Definition: DataTypes.hpp:131
std::int32_t integer
Signed integer type.
Definition: DataTypes.hpp:122
void erase(OrderedVariableToManyElementRelation &relation, localIndex const firstIndex, localIndex const er, localIndex const esr, localIndex const ei)
Remove an element relation from an object in the relation.
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
Array< T, 1 > array1d
Alias for 1D array.
Definition: DataTypes.hpp:216
std::enable_if< can_history_io< T >, HistoryMetadata >::type getHistoryMetadata(string const &name, ArrayView< T const, 1, 0 > const &arr, localIndex const numComps, localIndex sizeOverride=-1)
Produce a HistoryMetadata object for a supported one-dimensional array type.
std::string_view string_view
String type.
Definition: DataTypes.hpp:134
Exception class used to report errors in user input.
Definition: Logger.hpp:464
The regular expression data for validating inputs. Use rtTypes to get the regex of a type,...
Definition: DataTypes.hpp:513