GEOS
wrapperHelpers.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 TotalEnergies
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 
20 #ifndef GEOS_DATAREPOSITORY_WRAPPERHELPERS_HPP_
21 #define GEOS_DATAREPOSITORY_WRAPPERHELPERS_HPP_
22 
23 
25 #define RESTART_TYPE_LOGGING 0
26 
27 // Source includes
28 #include "BufferOps.hpp"
29 #include "BufferOpsDevice.hpp"
30 #include "DefaultValue.hpp"
31 #include "ConduitRestart.hpp"
32 #include "common/DataTypes.hpp"
33 #include "common/GeosxMacros.hpp"
34 #include "common/Span.hpp"
35 #include "codingUtilities/traits.hpp"
36 #include "LvArray/src/system.hpp"
37 
38 #if defined(GEOS_USE_PYGEOSX)
39 #include "LvArray/src/python/python.hpp"
40 #endif
41 
42 // TPL includes
43 #include <conduit.hpp>
44 
45 // System includes
46 #include <cstring>
47 
48 #if RESTART_TYPE_LOGGING
49 #include <unordered_set>
50 #endif
51 
52 namespace geos
53 {
54 namespace dataRepository
55 {
56 namespace wrapperHelpers
57 {
58 namespace internal
59 {
60 
61 inline void logOutputType( string const & typeString, string const & msg )
62 {
63 #if RESTART_TYPE_LOGGING
64  static std::unordered_set< string > m_types;
65 
66  if( !m_types.count( typeString ) )
67  {
68  m_types.insert( typeString );
69  GEOS_LOG( msg << typeString );
70  }
71 #else
72  GEOS_DEBUG_VAR( typeString );
73  GEOS_DEBUG_VAR( msg );
74 #endif
75 }
76 
77 template< typename T, typename ... INDICES >
78 string getIndicesToComponent( T const &, int const component, INDICES const ... existingIndices )
79 {
80  GEOS_ERROR_IF_NE( component, 0 );
81  return LvArray::indexing::getIndexString( existingIndices ... );
82 }
83 
84 template< typename ... INDICES >
85 string getIndicesToComponent( R1Tensor const &, int const component, INDICES const ... existingIndices )
86 { return LvArray::indexing::getIndexString( existingIndices ..., component ); }
87 
88 template< typename T >
89 T const * getPointerToComponent( T const & var, int const component )
90 {
91  GEOS_ERROR_IF_NE( component, 0 );
92  return &var;
93 }
94 
95 inline
96 real64 const * getPointerToComponent( R1Tensor const & var, int const component )
97 {
98  GEOS_ERROR_IF_GE( component, 3 );
99  return &var[ component ];
100 }
101 
102 } // namespace internal
103 
104 template< typename T >
105 class ArrayDimLabels
106 {
107 public:
108 
109  void set( integer const, Span< string const > )
110  {
111  GEOS_ERROR( "Dimension labels are only available in Array wrappers" );
112  }
113 
114  Span< string const > get( integer const ) const
115  {
116  GEOS_ERROR( "Dimension labels are only available in Array wrappers" );
117  return {};
118  }
119 };
120 
121 template< typename T, int NDIM, typename PERM >
122 class ArrayDimLabels< Array< T, NDIM, PERM > >
123 {
124 public:
125 
126  void set( integer const dim, Span< string const > labels )
127  {
128  GEOS_ERROR_IF_LT( dim, 0 );
129  GEOS_ERROR_IF_GE( dim, NDIM );
130  m_values[dim].resize( labels.size() );
131  std::copy( labels.begin(), labels.end(), m_values[dim].begin() );
132  }
133 
134  Span< string const > get( integer const dim ) const
135  {
136  GEOS_ERROR_IF_LT( dim, 0 );
137  GEOS_ERROR_IF_GE( dim, NDIM );
138  return { m_values[dim].begin(), m_values[dim].end() };
139  }
140 
141 private:
142 
143  string_array m_values[NDIM]{};
144 };
145 
146 template< typename T >
147 inline std::enable_if_t< traits::HasMemberFunction_size< T >, size_t >
148 size( T const & value )
149 { return value.size(); }
150 
151 template< typename T >
152 inline std::enable_if_t< !traits::HasMemberFunction_size< T >, size_t >
153 size( T const & GEOS_UNUSED_PARAM( value ) )
154 { return 1; }
155 
156 
157 inline char *
158 dataPtr( string & var )
159 { return const_cast< char * >( var.data() ); }
160 
161 inline char *
162 dataPtr( Path & var )
163 { return const_cast< char * >( var.data() ); }
164 
165 template< typename T >
166 inline std::enable_if_t< traits::HasMemberFunction_data< T >, typename traits::Pointer< T > >
167 dataPtr( T & value )
168 { return value.data(); }
169 
170 template< typename T >
171 inline std::enable_if_t< !traits::HasMemberFunction_data< T >, typename traits::Pointer< T > >
172 dataPtr( T & value )
173 { return &value; }
174 
175 template< class T >
176 inline typename traits::ConstPointer< T >
177 dataPtr( T const & value )
178 { return dataPtr( const_cast< T & >( value ) ); }
179 
180 
181 template< typename T >
182 inline std::enable_if_t< traits::HasMemberFunction_resize< T > >
183 resize( T & value, localIndex const newSize )
184 { value.resize( newSize ); }
185 
186 template< typename T >
187 inline std::enable_if_t< !traits::HasMemberFunction_resize< T > >
188 resize( T & GEOS_UNUSED_PARAM( value ),
189  localIndex const GEOS_UNUSED_PARAM( newSize ) )
190 {}
191 
192 template< typename T >
193 inline void
194 resizeDefault( T & value,
195  localIndex const newSize,
196  DefaultValue< T > const & defaultValue,
197  string const & )
198 { value.resizeDefault( newSize, defaultValue.value ); }
199 
200 
201 
202 template< typename T, int NDIM, typename PERMUTATION >
203 inline void
204 resizeDimensions( Array< T, NDIM, PERMUTATION > & value, int num_dims, localIndex const * const dims )
205 { value.resize( num_dims, dims ); }
206 
207 template< typename T >
208 inline void
209 resizeDimensions( T & value, int num_dims, localIndex const * const dims )
210 {
211  if( num_dims != 1 )
212  {
213  GEOS_ERROR( "Data is not multidimensional" );
214  return;
215  }
216  resize( value, dims[ 0 ] );
217 }
218 
219 
220 template< typename T >
221 inline localIndex
222 byteSizeOfElement()
223 { return sizeof( *dataPtr( std::declval< T >() ) ); }
224 
225 
226 template< typename T >
227 inline size_t
228 byteSize( T const & value )
229 { return wrapperHelpers::size( value ) * byteSizeOfElement< T >(); }
230 
231 
232 template< typename T >
233 inline localIndex
234 numElementsFromByteSize( localIndex const byteSize )
235 {
236  GEOS_ERROR_IF_NE( byteSize % byteSizeOfElement< T >(), 0 );
237  return byteSize / byteSizeOfElement< T >();
238 }
239 
240 
241 template< typename T >
242 std::enable_if_t< traits::HasMemberFunction_reserve< T > >
243 reserve( T & value, localIndex const newCapacity )
244 {
245  value.reserve( newCapacity );
246  if constexpr ( traits::HasMemberFunction_reserveValues< T const > )
247  {
248  localIndex const oldCapacity = value.size();
249  double const oldValueCapacity = value.valueCapacity();
250  localIndex const newValueCapacity = oldValueCapacity * newCapacity / oldCapacity;
251  value.reserveValues( newValueCapacity );
252  }
253 }
254 
255 template< typename T,
256  int NDIM,
257  typename PERMUTATION >
258 void reserve( Array< T, NDIM, PERMUTATION > & value, localIndex newCapacity )
259 {
260  localIndex const * const dims = value.dims();
261  for( int i=1; i<NDIM; ++i )
262  {
263  newCapacity *= dims[ i ];
264  }
265  value.reserve( newCapacity );
266 }
267 
268 template< typename T >
269 std::enable_if_t< !traits::HasMemberFunction_reserve< T > >
270 reserve( T & GEOS_UNUSED_PARAM( value ), localIndex const GEOS_UNUSED_PARAM( newCapacity ) )
271 {}
272 
273 
274 template< typename T >
275 std::enable_if_t< traits::HasMemberFunction_capacity< T const >, localIndex >
276 capacity( T const & value )
277 { return value.capacity(); }
278 
279 template< typename T >
280 std::enable_if_t< !traits::HasMemberFunction_capacity< T const >, localIndex >
281 capacity( T const & value )
282 { return wrapperHelpers::size( value ); }
283 
284 
285 
286 template< typename T >
287 std::enable_if_t< traits::HasMemberFunction_setName< T > >
288 setName( T & value, string const & name )
289 { value.setName( name ); }
290 
291 template< typename T >
292 std::enable_if_t< !traits::HasMemberFunction_setName< T > >
293 setName( T & GEOS_UNUSED_PARAM( value ), string const & GEOS_UNUSED_PARAM( name ) )
294 {}
295 
296 template< typename T >
297 std::enable_if_t< traits::HasMemberFunction_move< T > >
298 move( T & value, LvArray::MemorySpace const space, bool const touch )
299 { value.move( space, touch ); }
300 
301 template< typename T >
302 std::enable_if_t< !traits::HasMemberFunction_move< T > >
303 move( T & GEOS_UNUSED_PARAM( value ),
304  LvArray::MemorySpace const GEOS_UNUSED_PARAM( space ),
305  bool const GEOS_UNUSED_PARAM( touch ) )
306 {}
307 
308 // This is for an object that needs to be packed.
309 template< typename T >
310 std::enable_if_t< !bufferOps::can_memcpy< typename traits::Pointer< T > > >
311 pushDataToConduitNode( T const & var, conduit::Node & node )
312 {
313  internal::logOutputType( LvArray::system::demangleType( var ), "Packing for output: " );
314 
315  // Get the number of bytes in the packed object.
316  localIndex const byteSize = bufferOps::PackSize( var );
317 
318  // Create a conduit data type that describes the array.
319  conduit::DataType const dtype( conduitTypeInfo< buffer_unit_type >::id, byteSize );
320 
321  // Allocate the array in the "__values__" child.
322  conduit::Node & valuesNode = node[ "__values__" ];
323  valuesNode.set( dtype );
324 
325  // Get the pointer to the array and pack the object into it.
326  buffer_unit_type * buffer = valuesNode.value();
327  bufferOps::Pack< true >( buffer, var );
328 }
329 
330 // This is for an object that needs to be packed.
331 template< typename T >
332 std::enable_if_t< !bufferOps::can_memcpy< typename traits::Pointer< T > > >
333 pullDataFromConduitNode( T & var, conduit::Node const & node )
334 {
335  conduit::Node const & valuesNode = node.fetch_existing( "__values__" );
336 
337  // Get the number of bytes in the array and a pointer to the array.
338  localIndex const byteSize = valuesNode.dtype().number_of_elements();
339  buffer_unit_type const * buffer = valuesNode.value();
340 
341  // Unpack the object from the array.
342  localIndex const bytesRead = bufferOps::Unpack( buffer, var );
343  GEOS_ERROR_IF_NE( bytesRead, byteSize );
344 }
345 
346 // This is for an string since the type of char is different on different platforms :(.
347 inline
348 void
349 pushDataToConduitNode( string const & var, conduit::Node & node )
350 {
351  internal::logOutputType( LvArray::system::demangleType( var ), "Output via external pointer: " );
352 
353  constexpr int conduitTypeID = conduitTypeInfo< signed char >::id;
354  conduit::DataType const dtype( conduitTypeID, var.size() );
355 
356  signed char * const ptr = const_cast< signed char * >( reinterpret_cast< signed char const * >( var.data() ) );
357  node[ "__values__" ].set_external( dtype, ptr );
358 }
359 
360 // This is for Path since it derives from string. See overload for string.
361 inline
362 void
363 pushDataToConduitNode( Path const & var, conduit::Node & node )
364 {
365  pushDataToConduitNode( static_cast< string const & >(var), node );
366 }
367 
368 // This is for an object that doesn't need to be packed but isn't an LvArray.
369 template< typename T >
370 std::enable_if_t< bufferOps::can_memcpy< typename traits::Pointer< T > > >
371 pushDataToConduitNode( T const & var, conduit::Node & node )
372 {
373  internal::logOutputType( LvArray::system::demangleType( var ), "Output via external pointer: " );
374 
375  constexpr int conduitTypeID = conduitTypeInfo< typename traits::Pointer< T > >::id;
376  constexpr int sizeofConduitType = conduitTypeInfo< typename traits::Pointer< T > >::sizeOfConduitType;
377  localIndex const numBytes = byteSize( var );
378  conduit::DataType const dtype( conduitTypeID, numBytes / sizeofConduitType );
379 
380  void * const ptr = const_cast< void * >( static_cast< void const * >( dataPtr( var ) ) );
381  node[ "__values__" ].set_external( dtype, ptr );
382 }
383 
384 // This is for an object that doesn't need to be packed but isn't an LvArray or a SortedArray.
385 template< typename T >
386 std::enable_if_t< bufferOps::can_memcpy< typename traits::Pointer< T > > >
387 pullDataFromConduitNode( T & var, conduit::Node const & node )
388 {
389  conduit::Node const & valuesNode = node.fetch_existing( "__values__" );
390 
391  localIndex const byteSize = LvArray::integerConversion< localIndex >( valuesNode.dtype().strided_bytes() );
392  localIndex const numElements = numElementsFromByteSize< T >( byteSize );
393 
394  resize( var, numElements );
395 
396  std::memcpy( dataPtr( var ), valuesNode.data_ptr(), byteSize );
397 }
398 
399 // This is for a SortedArray that doesn't need to be packed.
400 template< typename T >
401 std::enable_if_t< bufferOps::can_memcpy< T > >
402 pullDataFromConduitNode( SortedArray< T > & var, conduit::Node const & node )
403 {
404  conduit::Node const & valuesNode = node.fetch_existing( "__values__" );
405 
406  localIndex const byteSize = LvArray::integerConversion< localIndex >( valuesNode.dtype().strided_bytes() );
407  localIndex const numElements = numElementsFromByteSize< T >( byteSize );
408 
409  T const * const values = reinterpret_cast< T const * >( valuesNode.data_ptr() );
410  var.insert( values, values + numElements );
411 }
412 
413 
414 // This is an LvArray that doesn't need to be packed.
415 template< typename T, int NDIM, typename PERMUTATION >
416 std::enable_if_t< bufferOps::can_memcpy< T > >
417 pushDataToConduitNode( Array< T, NDIM, PERMUTATION > const & var,
418  conduit::Node & node )
419 {
420  internal::logOutputType( LvArray::system::demangleType( var ), "Output array via external pointer: " );
421 
422  // Push the data into conduit
423  constexpr int conduitTypeID = conduitTypeInfo< T >::id;
424  constexpr int sizeofConduitType = conduitTypeInfo< T >::sizeOfConduitType;
425  conduit::DataType const dtype( conduitTypeID, var.size() * sizeof( T ) / sizeofConduitType );
426  void * const ptr = const_cast< void * >( static_cast< void const * >( var.data() ) );
427  node[ "__values__" ].set_external( dtype, ptr );
428 
429  // Create a copy of the dimensions
430  camp::idx_t temp[ NDIM + 1 ];
431  for( int i = 0; i < NDIM; ++i )
432  {
433  temp[ i ] = var.size( i );
434  }
435 
436  // If T is something like a Tensor than there is an extra implicit dimension.
437  constexpr int const implicitDimensionLength = conduitTypeInfo< T >::numConduitValues;
438  constexpr bool const hasImplicitDimension = implicitDimensionLength != 1;
439  constexpr int totalNumDimensions = NDIM + hasImplicitDimension;
440  if( hasImplicitDimension )
441  {
442  temp[ NDIM ] = implicitDimensionLength;
443  }
444 
445  // push the dimensions into the node
446  conduit::DataType const dimensionType( conduitTypeInfo< camp::idx_t >::id, totalNumDimensions );
447  node[ "__dimensions__" ].set( dimensionType, temp );
448 
449  // Create a copy of the permutation
450  constexpr std::array< camp::idx_t, NDIM > const perm = to_stdArray( RAJA::as_array< PERMUTATION >::get());
451  for( int i = 0; i < NDIM; ++i )
452  {
453  temp[ i ] = perm[ i ];
454  }
455 
456  if( hasImplicitDimension )
457  {
458  temp[ NDIM ] = NDIM;
459  }
460 
461  node[ "__permutation__" ].set( dimensionType, temp );
462 }
463 
464 // This is an LvArray that doesn't need to be packed.
465 template< typename T, int NDIM, typename PERMUTATION >
466 std::enable_if_t< bufferOps::can_memcpy< T > >
467 pullDataFromConduitNode( Array< T, NDIM, PERMUTATION > & var,
468  conduit::Node const & node )
469 {
470  // Get the number of dimensions written out, accounting for an implicit dimension and the permutation.
471  constexpr int const implicitDimensionLength = conduitTypeInfo< T >::numConduitValues;
472  constexpr bool const hasImplicitDimension = implicitDimensionLength != 1;
473  constexpr int totalNumDimensions = NDIM + hasImplicitDimension;
474 
475  // Check that the permutations match.
476  conduit::Node const & permutationNode = node.fetch_existing( "__permutation__" );
477  GEOS_ERROR_IF_NE( permutationNode.dtype().number_of_elements(), totalNumDimensions );
478 
479  constexpr std::array< camp::idx_t, NDIM > const perm = to_stdArray( RAJA::as_array< PERMUTATION >::get());
480  camp::idx_t const * const permFromConduit = permutationNode.value();
481  for( int i = 0; i < NDIM; ++i )
482  {
483  GEOS_ERROR_IF_NE_MSG( permFromConduit[ i ], perm[ i ],
484  "The permutation of the data in conduit and the provided Array don't match." );
485  }
486 
487  if( hasImplicitDimension )
488  {
489  GEOS_ERROR_IF_NE_MSG( permFromConduit[ NDIM ], NDIM,
490  "The permutation of the data in conduit and the provided Array don't match." );
491  }
492 
493  // Now pull out the dimensions and resize the array.
494  conduit::Node const & dimensionNode = node.fetch_existing( "__dimensions__" );
495  GEOS_ERROR_IF_NE( dimensionNode.dtype().number_of_elements(), totalNumDimensions );
496  camp::idx_t const * const dims = dimensionNode.value();
497 
498  if( hasImplicitDimension )
499  {
500  GEOS_ERROR_IF_NE( dims[ NDIM ], implicitDimensionLength );
501  }
502 
503  var.resize( NDIM, dims );
504 
505  // Finally memcpy
506  conduit::Node const & valuesNode = node.fetch_existing( "__values__" );
507  localIndex numBytesFromArray = var.size() * sizeof( T );
508  GEOS_ERROR_IF_NE( numBytesFromArray, valuesNode.dtype().strided_bytes() );
509  std::memcpy( var.data(), valuesNode.data_ptr(), numBytesFromArray );
510 }
511 
512 
513 
514 template< typename T, typename INDEX_TYPE >
515 std::enable_if_t< bufferOps::can_memcpy< T > >
516 pushDataToConduitNode( ArrayOfArrays< T, INDEX_TYPE > const & var2,
517  conduit::Node & node )
518 {
519  ArrayOfArraysView< T const, INDEX_TYPE > const & var = var2.toViewConst();
520  internal::logOutputType( LvArray::system::demangleType( var ), "Output array via external pointer: " );
521 
522  // ArrayOfArrays::m_numArrays
523  INDEX_TYPE const numArrays = var.size();
524  conduit::DataType const numArraysType( conduitTypeInfo< INDEX_TYPE >::id, 1 );
525  node[ "__numberOfArrays__" ].set( numArraysType, const_cast< void * >( static_cast< void const * >(&numArrays) ) );
526 
527  // ArrayOfArrays::m_offsets
528  INDEX_TYPE const * const offsets = var.getOffsets();
529  conduit::DataType const offsetsType( conduitTypeInfo< INDEX_TYPE >::id, numArrays+1 );
530  node[ "__offsets__" ].set_external( offsetsType, const_cast< void * >( static_cast< void const * >( offsets ) ) );
531 
532  // ArrayOfArrays::m_sizes
533  INDEX_TYPE const * const sizes = var.getSizes();
534  conduit::DataType const sizesType( conduitTypeInfo< INDEX_TYPE >::id, numArrays );
535  node[ "__sizes__" ].set_external( sizesType, const_cast< void * >( static_cast< void const * >( sizes ) ) );
536 
537  // **** WARNING: alters the uninitialized values in the ArrayOfArrays ****
538  T * const values = const_cast< T * >(var.getValues());
539  for( INDEX_TYPE i = 0; i < numArrays; ++i )
540  {
541  INDEX_TYPE const curOffset = offsets[ i ];
542  INDEX_TYPE const nextOffset = offsets[ i + 1 ];
543  for( INDEX_TYPE j = curOffset + var.sizeOfArray( i ); j < nextOffset; ++j )
544  {
545  if constexpr ( std::is_arithmetic< T >::value )
546  {
547  values[ j ] = 0;
548  }
549  else
550  {
551  values[ j ] = T();
552  }
553  }
554  }
555 
556  constexpr int conduitTypeID = conduitTypeInfo< T >::id;
557  constexpr int sizeofConduitType = conduitTypeInfo< T >::sizeOfConduitType;
558  conduit::DataType const dtype( conduitTypeID, offsets[numArrays] * sizeof( T ) / sizeofConduitType );
559 
560  // Push the data into conduit
561  node[ "__values__" ].set_external( dtype, values );
562 }
563 
564 template< typename T, typename INDEX_TYPE >
565 std::enable_if_t< bufferOps::can_memcpy< T > >
566 pullDataFromConduitNode( ArrayOfArrays< T, INDEX_TYPE > & var,
567  conduit::Node const & node )
568 {
569 
570  // numArrays node
571  conduit::Node const & numArraysNode = node.fetch_existing( "__numberOfArrays__" );
572  INDEX_TYPE const * const numArrays = numArraysNode.value();
573 
574  // offsets node
575  conduit::Node const & offsetsNode = node.fetch_existing( "__offsets__" );
576  conduit::DataType const & offsetsDataType = offsetsNode.dtype();
577  INDEX_TYPE const * const offsets = offsetsNode.value();
578  INDEX_TYPE const sizeOffsets = offsetsDataType.number_of_elements();
579 
580  // sizes node
581  conduit::Node const & sizesNode = node.fetch_existing( "__sizes__" );
582  conduit::DataType const & sizesDataType = sizesNode.dtype();
583  INDEX_TYPE const * const sizes = sizesNode.value();
584  INDEX_TYPE const sizeSizes = sizesDataType.number_of_elements();
585 
586  // Check that the numArrays, sizes and offsets are consistent.
587  GEOS_ERROR_IF_NE( *numArrays, sizeSizes );
588  GEOS_ERROR_IF_NE( *numArrays+1, sizeOffsets );
589 
590  // values node
591  conduit::Node const & valuesNode = node.fetch_existing( "__values__" );
592  conduit::DataType const & valuesDataType = valuesNode.dtype();
593  const INDEX_TYPE valuesSize = valuesDataType.number_of_elements();
594 
595  // should preallocate var.m_values with estimated sizes
596  INDEX_TYPE const arraySizeEstimate = (*numArrays)==0 ? 0 : valuesSize / (*numArrays);
597  var.resize( *numArrays, arraySizeEstimate );
598  var.reserveValues( valuesSize );
599 
600  // correctly set the sizes and capacities of each sub-array
601  localIndex allocatedSize = 0;
602  for( INDEX_TYPE i = 0; i < *numArrays; ++i )
603  {
604  INDEX_TYPE const arrayAllocation = offsets[i+1] - offsets[i];
605  var.setCapacityOfArray( i, arrayAllocation );
606  var.resizeArray( i, sizes[ i ] );
607  allocatedSize += arrayAllocation;
608  }
609 
610  // make sure that the allocated size is the same as the number of values read
611  GEOS_ERROR_IF_NE( valuesSize, allocatedSize );
612 
613  // make sure the allocatedSize is consistent wit the last offset
614  GEOS_ERROR_IF_NE( allocatedSize, offsets[sizeOffsets-1] );
615 
616  // get a view because the ArrayOfArraysView data accessors are protected
617  ArrayOfArraysView< T const, INDEX_TYPE > const & varView = var.toViewConst();
618  INDEX_TYPE const * const varOffsets = varView.getOffsets();
619  INDEX_TYPE const * const varSizes = varView.getSizes();
620 
621  // check that the offsets that are read are the same as the ones that were allocated
622  GEOS_ERROR_IF_NE( varOffsets[0], offsets[0] );
623 
624  // check each subarray has the identical capacity and size
625  for( INDEX_TYPE i = 0; i<*numArrays; ++i )
626  {
627  GEOS_ERROR_IF_NE( varOffsets[i+1], offsets[i+1] );
628  GEOS_ERROR_IF_NE( varSizes[i], sizes[i] );
629  }
630 
631  // copy the values
632  localIndex numBytesFromArray = allocatedSize * sizeof( T );
633  GEOS_ERROR_IF_NE( numBytesFromArray, valuesDataType.strided_bytes() );
634  std::memcpy( const_cast< T * >(varView.getValues()), valuesNode.data_ptr(), numBytesFromArray );
635 }
636 
637 
638 
639 template< typename T >
640 void pushDataToConduitNode( InterObjectRelation< T > const & var,
641  conduit::Node & node )
642 {return pushDataToConduitNode( var.base(), node ); }
643 
644 template< typename T >
645 void pullDataFromConduitNode( InterObjectRelation< T > & var,
646  conduit::Node const & node )
647 { return pullDataFromConduitNode( var.base(), node ); }
648 
649 
651 template< typename T, int NDIM, int USD >
652 std::enable_if_t< std::is_arithmetic< T >::value || traits::is_tensorT< T > >
653 addBlueprintField( ArrayView< T const, NDIM, USD > const & var,
654  conduit::Node & fields,
655  string const & fieldName,
656  string const & topology,
657  stdVector< string > const & componentNames )
658 {
659  GEOS_ERROR_IF_LE( var.size(), 0 );
660 
661  using ConduitType = typename conduitTypeInfo< T >::type;
662  constexpr int conduitTypeID = conduitTypeInfo< T >::id;
663  constexpr int numComponentsPerValue = conduitTypeInfo< T >::numConduitValues;
664 
665  localIndex const totalNumberOfComponents = numComponentsPerValue * var.size() / var.size( 0 );
666  if( !componentNames.empty() )
667  {
668  GEOS_ERROR_IF_NE( localIndex( componentNames.size() ), totalNumberOfComponents );
669  }
670 
671  var.move( hostMemorySpace, false );
672 
673  conduit::DataType dtype( conduitTypeID, var.size( 0 ) );
674  dtype.set_stride( sizeof( ConduitType ) * numComponentsPerValue * var.strides()[ 0 ] );
675 
676  localIndex curComponent = 0;
677  LvArray::forValuesInSliceWithIndices( var[ 0 ], [&fields, &fieldName, &topology, &componentNames, totalNumberOfComponents, &dtype, &curComponent]
678  ( T const & val, auto const ... indices )
679  {
680  for( int i = 0; i < numComponentsPerValue; ++i )
681  {
682  string name;
683  if( totalNumberOfComponents == 1 )
684  {
685  name = fieldName;
686  }
687  else if( componentNames.empty() )
688  {
689  string indexString = internal::getIndicesToComponent( val, i, indices ... );
690  indexString.erase( indexString.begin() );
691  indexString.pop_back();
692  indexString.pop_back();
693  name = fieldName + indexString;
694  }
695  else
696  {
697  name = componentNames[ curComponent++ ];
698  }
699 
700  conduit::Node & field = fields[ name ];
701  field[ "association" ] = "element";
702  field[ "volume_dependent" ] = "false";
703  field[ "topology" ] = topology;
704 
705  void const * pointer = internal::getPointerToComponent( val, i );
706  field[ "values" ].set_external( dtype, const_cast< void * >( pointer ) );
707  }
708  } );
709 }
710 
711 template< typename T >
712 void addBlueprintField( T const &,
713  conduit::Node & fields,
714  string const &,
715  string const &,
716  stdVector< string > const & )
717 {
718  GEOS_ERROR( "Cannot create a mcarray out of " << LvArray::system::demangleType< T >() <<
719  "\nWas trying to write it to " << fields.path() );
720  GEOS_UNUSED_VAR( fields );
721 }
722 
723 template< typename T, int NDIM, int USD >
724 std::enable_if_t< std::is_arithmetic< T >::value || traits::is_tensorT< T > >
725 populateMCArray( ArrayView< T const, NDIM, USD > const & var,
726  conduit::Node & node,
727  stdVector< string > const & componentNames )
728 {
729  GEOS_ERROR_IF_LE( var.size(), 0 );
730 
731  using ConduitType = typename conduitTypeInfo< T >::type;
732  constexpr int conduitTypeID = conduitTypeInfo< T >::id;
733  constexpr int numComponentsPerValue = conduitTypeInfo< T >::numConduitValues;
734 
735  if( !componentNames.empty() )
736  {
737  GEOS_ERROR_IF_NE( localIndex( componentNames.size() ), numComponentsPerValue * var.size() / var.size( 0 ) );
738  }
739 
740  var.move( hostMemorySpace, false );
741 
742  conduit::DataType dtype( conduitTypeID, var.size( 0 ) );
743  dtype.set_stride( sizeof( ConduitType ) * numComponentsPerValue * var.strides()[ 0 ] );
744 
745  localIndex curComponent = 0;
746  LvArray::forValuesInSliceWithIndices( var[ 0 ], [&componentNames, &node, &dtype, &curComponent]
747  ( T const & val, auto const ... indices )
748  {
749  for( int i = 0; i < numComponentsPerValue; ++i )
750  {
751  string const name = componentNames.empty() ? internal::getIndicesToComponent( val, i, indices ... ) :
752  componentNames[ curComponent++ ];
753 
754  void const * pointer = internal::getPointerToComponent( val, i );
755  node[ name ].set_external( dtype, const_cast< void * >( pointer ) );
756  }
757  } );
758 }
759 
760 template< typename T >
761 void populateMCArray( T const &,
762  conduit::Node & node,
763  stdVector< string > const & )
764 {
765  GEOS_ERROR( "Cannot create a mcarray out of " << LvArray::system::demangleType< T >() <<
766  "\nWas trying to write it to " << node.path() );
767  GEOS_UNUSED_VAR( node );
768 }
769 
770 template< typename T >
771 std::enable_if_t< std::is_arithmetic< T >::value, std::unique_ptr< Array< T, 1 > > >
772 averageOverSecondDim( ArrayView< T const, 1, 0 > const & var )
773 {
774  std::unique_ptr< Array< T, 1 > > ret = std::make_unique< Array< T, 1 > >();
775 
776  ret->resize( var.size() );
777  ret->template setValues< serialPolicy >( var );
778 
779  return ret;
780 }
781 
782 template< typename T, int NDIM, int USD >
783 std::enable_if_t< std::is_arithmetic< T >::value, std::unique_ptr< Array< T, NDIM - 1 > > >
784 averageOverSecondDim( ArrayView< T const, NDIM, USD > const & var )
785 {
786  std::unique_ptr< Array< T, NDIM - 1 > > ret = std::make_unique< Array< T, NDIM - 1 > >();
787 
788  localIndex newDims[ NDIM - 1 ];
789  newDims[ 0 ] = var.size( 0 );
790  for( int i = 2; i < NDIM; ++i )
791  {
792  newDims[ i - 1 ] = var.size( i );
793  }
794 
795  ret->resize( NDIM - 1, newDims );
796 
797  ArrayView< T, NDIM - 1 > const & output = *ret;
798 
799  localIndex const numSamples = var.size( 1 );
800  forAll< serialPolicy >( var.size( 0 ), [var, numSamples, &output] ( localIndex const i )
801  {
802  LvArray::sumOverFirstDimension( var[ i ], output[ i ] );
803 
804  LvArray::forValuesInSlice( output[ i ], [numSamples] ( T & val )
805  {
806  val /= numSamples;
807  } );
808  } );
809 
810  return ret;
811 }
812 
813 template< typename T >
814 std::unique_ptr< int > averageOverSecondDim( T const & )
815 {
816  GEOS_ERROR( "Cannot average over the second dimension of " << LvArray::system::demangleType< T >() );
817  return std::unique_ptr< int >( nullptr );
818 }
819 
820 template< typename T, int NDIM, int USD >
821 int numArrayDims( ArrayView< T const, NDIM, USD > const & GEOS_UNUSED_PARAM( var ) )
822 {
823  return NDIM;
824 }
825 
826 template< typename T >
827 int numArrayDims( stdVector< T > const & GEOS_UNUSED_PARAM( var ) )
828 {
829  return 1;
830 }
831 
832 template< typename T >
833 int numArrayDims( T const & GEOS_UNUSED_PARAM( var ) )
834 {
835  return 0;
836 }
837 
838 template< typename T, int NDIM, int USD >
839 localIndex numArrayComp( ArrayView< T const, NDIM, USD > const & var )
840 {
841  return LvArray::indexing::multiplyAll< NDIM - 1 >( var.dims() + 1 );
842 }
843 
844 template< typename T >
845 localIndex numArrayComp( ArrayView< T const, 1, 0 > const & GEOS_UNUSED_PARAM( var ) )
846 {
847  return 1;
848 }
849 
850 template< typename T >
851 localIndex numArrayComp( T const & GEOS_UNUSED_PARAM( var ) )
852 {
853  return 0;
854 }
855 
856 template< bool DO_PACKING, typename T, typename IDX >
857 inline std::enable_if_t< bufferOps::is_packable_by_index< T >, localIndex >
858 PackByIndex( buffer_unit_type * & buffer, T & var, IDX & idx )
859 { return bufferOps::PackByIndex< DO_PACKING >( buffer, var, idx ); }
860 
861 template< bool DO_PACKING, typename T, typename IDX >
862 inline std::enable_if_t< !bufferOps::is_packable_by_index< T >, localIndex >
863 PackByIndex( buffer_unit_type * &, T &, IDX & )
864 {
865  GEOS_ERROR( "Trying to pack data type (" << LvArray::system::demangleType< T >() << ") by index. Operation not supported." );
866  return 0;
867 }
868 
869 template< typename T, typename IDX >
870 inline std::enable_if_t< bufferOps::is_packable_by_index< T >, localIndex >
871 UnpackByIndex( buffer_unit_type const * & buffer, T & var, IDX & idx )
872 { return bufferOps::UnpackByIndex( buffer, var, idx ); }
873 
874 template< typename T, typename IDX >
875 inline std::enable_if_t< !bufferOps::is_packable_by_index< T >, localIndex >
876 UnpackByIndex( buffer_unit_type const * &, T &, IDX & )
877 {
878  GEOS_ERROR( "Trying to unpack data type (" << LvArray::system::demangleType< T >() << ") by index. Operation not supported." );
879  return 0;
880 }
881 
882 
883 template< bool DO_PACKING, typename T >
884 inline std::enable_if_t< bufferOps::is_container< T > || bufferOps::can_memcpy< T >, localIndex >
885 PackDevice( buffer_unit_type * & buffer, T const & var, parallelDeviceEvents & events )
886 { return bufferOps::PackDevice< DO_PACKING >( buffer, var, events ); }
887 
888 
889 template< bool DO_PACKING, typename T >
890 inline std::enable_if_t< !bufferOps::is_container< T > && !bufferOps::can_memcpy< T >, localIndex >
891 PackDevice( buffer_unit_type * &, T const &, parallelDeviceEvents & )
892 {
893  GEOS_ERROR( "Trying to pack data type (" << LvArray::system::demangleType< T >() << ") on device. Operation not supported." );
894  return 0;
895 }
896 
897 template< bool DO_PACKING, typename T, typename IDX >
898 inline std::enable_if_t< bufferOps::is_container< T >, localIndex >
899 PackByIndexDevice( buffer_unit_type * & buffer, T const & var, IDX & idx, parallelDeviceEvents & events )
900 { return bufferOps::PackByIndexDevice< DO_PACKING >( buffer, var, idx, events ); }
901 
902 template< bool DO_PACKING, typename T, typename IDX >
903 inline std::enable_if_t< !bufferOps::is_container< T >, localIndex >
904 PackByIndexDevice( buffer_unit_type * &, T const &, IDX &, parallelDeviceEvents & )
905 {
906  GEOS_ERROR( "Trying to pack data type (" << LvArray::system::demangleType< T >() << ") by index on device. Operation not supported." );
907  return 0;
908 }
909 
910 template< typename T >
911 inline std::enable_if_t< bufferOps::is_container< T >, localIndex >
912 UnpackDevice( buffer_unit_type const * & buffer, T const & var, parallelDeviceEvents & events )
913 { return bufferOps::UnpackDevice( buffer, var, events ); }
914 
915 template< typename T >
916 inline std::enable_if_t< !bufferOps::is_container< T >, localIndex >
917 UnpackDevice( buffer_unit_type const * &, T const &, parallelDeviceEvents & )
918 {
919  GEOS_ERROR( "Trying to unpack data type (" << LvArray::system::demangleType< T >() << ") on device. Operation not supported." );
920  return 0;
921 }
922 
923 template< typename T, typename IDX >
924 inline std::enable_if_t< bufferOps::is_container< T >, localIndex >
925 UnpackByIndexDevice( buffer_unit_type const * & buffer, T const & var, IDX & idx, parallelDeviceEvents & events, MPI_Op op=MPI_REPLACE )
926 { return bufferOps::UnpackByIndexDevice( buffer, var, idx, events, op ); }
927 
928 template< typename T, typename IDX >
929 inline std::enable_if_t< !bufferOps::is_container< T >, localIndex >
930 UnpackByIndexDevice( buffer_unit_type const * &, T &, IDX &, parallelDeviceEvents &, MPI_Op )
931 {
932  GEOS_ERROR( "Trying to unpack data type (" << LvArray::system::demangleType< T >() << ") by index on device. Operation not supported." );
933  return 0;
934 }
935 
936 
937 template< bool DO_PACKING, typename T >
939 PackDataDevice( buffer_unit_type * & buffer, T const & var, parallelDeviceEvents & events )
940 { return bufferOps::PackDataDevice< DO_PACKING >( buffer, var, events ); }
941 
942 template< bool DO_PACKING, typename T, typename IDX >
943 inline std::enable_if_t< bufferOps::is_container< T >, localIndex >
944 PackDataByIndexDevice( buffer_unit_type * & buffer, T const & var, IDX & idx, parallelDeviceEvents & events )
945 { return bufferOps::PackDataByIndexDevice< DO_PACKING >( buffer, var, idx, events ); }
946 
947 template< bool DO_PACKING, typename T, typename IDX >
948 inline std::enable_if_t< !bufferOps::is_container< T >, localIndex >
949 PackDataByIndexDevice( buffer_unit_type * &, T const &, IDX &, parallelDeviceEvents & )
950 {
951  GEOS_ERROR( "Trying to pack data type (" << LvArray::system::demangleType< T >() << ") by index on device. Operation not supported." );
952  return 0;
953 }
954 
955 template< typename T >
956 inline std::enable_if_t< bufferOps::is_container< T >, localIndex >
957 UnpackDataDevice( buffer_unit_type const * & buffer, T const & var, parallelDeviceEvents & events )
958 { return bufferOps::UnpackDataDevice( buffer, var, events ); }
959 
960 template< typename T >
961 inline std::enable_if_t< !bufferOps::is_container< T >, localIndex >
962 UnpackDataDevice( buffer_unit_type const * &, T const &, parallelDeviceEvents & )
963 {
964  GEOS_ERROR( "Trying to unpack data type (" << LvArray::system::demangleType< T >() << ") on device. Operation not supported." );
965  return 0;
966 }
967 
968 template< typename T, typename IDX >
969 inline std::enable_if_t< bufferOps::is_container< T >, localIndex >
970 UnpackDataByIndexDevice( buffer_unit_type const * & buffer, T const & var, IDX & idx, parallelDeviceEvents & events, MPI_Op op )
971 { return bufferOps::UnpackDataByIndexDevice( buffer, var, idx, events, op ); }
972 
973 template< typename T, typename IDX >
974 inline std::enable_if_t< !bufferOps::is_container< T >, localIndex >
975 UnpackDataByIndexDevice( buffer_unit_type const * &, T const &, IDX &, parallelDeviceEvents &, MPI_Op )
976 {
977  GEOS_ERROR( "Trying to unpack data type (" << LvArray::system::demangleType< T >() << ") by index on device. Operation not supported." );
978  return 0;
979 }
980 
981 #if defined(GEOS_USE_PYGEOSX)
982 
983 template< typename T >
984 inline std::enable_if_t< LvArray::python::CanCreate< T >, PyObject * >
985 createPythonObject( T & object )
986 { return LvArray::python::create( object ); }
987 
988 template< typename T >
989 inline std::enable_if_t< !LvArray::python::CanCreate< T >, PyObject * >
990 createPythonObject( T & )
991 { return nullptr; }
992 
993 #endif
994 
995 } // namespace wrapperHelpers
996 } // namespace dataRepository
997 } // namespace geos
998 
999 #undef RESTART_TYPE_LOGGING
1000 
1001 #endif // GEOS_DATAREPOSITORY_WRAPPERHELPERS_HPP_
#define GEOS_UNUSED_VAR(...)
Mark an unused variable and silence compiler warnings.
Definition: GeosxMacros.hpp:84
#define GEOS_DEBUG_VAR(...)
Mark a debug variable and silence compiler warnings.
Definition: GeosxMacros.hpp:87
#define GEOS_UNUSED_PARAM(X)
Mark an unused argument and silence compiler warnings.
Definition: GeosxMacros.hpp:72
#define GEOS_ERROR_IF_LE(lhs, rhs)
Raise a hard error if one value compares less than or equal to the other.
Definition: Logger.hpp:525
#define GEOS_ERROR(...)
Raise a hard error and terminate the program.
Definition: Logger.hpp:222
#define GEOS_ERROR_IF_LT(lhs, rhs)
Raise a hard error if one value compares less than the other.
Definition: Logger.hpp:507
#define GEOS_ERROR_IF_GE(lhs, rhs)
Raise a hard error if one value compares greater than or equal to the other.
Definition: Logger.hpp:490
#define GEOS_LOG(...)
Log a message on screen.
Definition: Logger.hpp:41
#define GEOS_ERROR_IF_NE_MSG(lhs, rhs,...)
Raise a hard error if two values are not equal.
Definition: Logger.hpp:449
#define GEOS_ERROR_IF_NE(lhs, rhs)
Raise a hard error if two values are not equal.
Definition: Logger.hpp:456
stdVector< string > string_array
A 1-dimensional array of geos::string types.
Definition: DataTypes.hpp:361
LvArray::Array< T, NDIM, PERMUTATION, localIndex, LvArray::ChaiBuffer > Array
Multidimensional array type. See LvArray:Array for details.
Definition: DataTypes.hpp:141
std::set< T > set
A set of local indices.
Definition: DataTypes.hpp:262
double real64
64-bit floating point type.
Definition: DataTypes.hpp:98
GEOS_LOCALINDEX_TYPE localIndex
Local index type (for indexing objects within an MPI partition).
Definition: DataTypes.hpp:84
constexpr stdArray< T, N > to_stdArray(std::array< T, N > const &arr)
Convert an std::array to an stdArray.
Tensor< real64, 3 > R1Tensor
Alias for a local (stack-based) rank-1 tensor type.
Definition: DataTypes.hpp:165
signed char buffer_unit_type
Type stored in communication buffers.
Definition: DataTypes.hpp:108
int integer
Signed integer type.
Definition: DataTypes.hpp:81
LvArray::ArrayView< T, NDIM, USD, localIndex, LvArray::ChaiBuffer > ArrayView
Multidimensional array view type. See LvArray:ArrayView for details.
Definition: DataTypes.hpp:147