GEOS
LifoStorage.hpp
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 
16 #ifndef LIFOSTORAGE_HPP
17 #define LIFOSTORAGE_HPP
18 
19 #include <deque>
20 #include <future>
21 #include <mutex>
22 #include <condition_variable>
23 #include <camp/camp.hpp>
24 #include <sys/stat.h>
25 #include <fcntl.h>
26 #include <unistd.h>
27 #include <algorithm>
28 
29 #include "common/GEOS_RAJA_Interface.hpp"
30 #include "common/TimingMacros.hpp"
31 #include "common/LifoStorageCommon.hpp"
32 #include "common/LifoStorageHost.hpp"
33 #ifdef GEOS_USE_CUDA
34 #include "common/LifoStorageCuda.hpp"
35 #endif
36 
37 namespace geos
38 {
42 template< typename T, typename INDEX_TYPE >
44 {
45 
46 public:
47 
48 
61  LifoStorage( std::string name, size_t elemCnt, int numberOfBuffersToStoreOnDevice, int numberOfBuffersToStoreOnHost, int maxNumberOfBuffers ):
62  m_maxNumberOfBuffers( maxNumberOfBuffers ),
63  m_bufferSize( elemCnt*sizeof( T ) ),
64  m_bufferCount( 0 )
65  {
66  LIFO_LOG_RANK( " LIFO : maximum size "<< m_maxNumberOfBuffers << " buffers " );
67  LIFO_LOG_RANK( " LIFO : buffer size " << m_bufferSize / ( 1024.0 * 1024.0 ) << "MB" );
68  if( numberOfBuffersToStoreOnDevice < 0 )
69  {
70 #ifdef GEOS_USE_CUDA
71  numberOfBuffersToStoreOnDevice = LifoStorageCuda< T, INDEX_TYPE >::computeNumberOfBufferOnDevice( -numberOfBuffersToStoreOnDevice, m_bufferSize, m_maxNumberOfBuffers );
72 #else
73  numberOfBuffersToStoreOnDevice = 0;
74 #endif
75  }
76  if( numberOfBuffersToStoreOnHost < 0 )
77  {
78  numberOfBuffersToStoreOnHost =
79  LifoStorageCommon< T, INDEX_TYPE >::computeNumberOfBufferOnHost( -numberOfBuffersToStoreOnHost, m_bufferSize, m_maxNumberOfBuffers, numberOfBuffersToStoreOnDevice );
80  }
81  LIFO_LOG_RANK( " LIFO : allocating "<< numberOfBuffersToStoreOnHost <<" buffers on host" );
82  LIFO_LOG_RANK( " LIFO : allocating "<< numberOfBuffersToStoreOnDevice <<" buffers on device" );
83 #ifdef GEOS_USE_CUDA
84  if( numberOfBuffersToStoreOnDevice > 0 )
85  {
86  m_lifo = std::make_unique< LifoStorageCuda< T, INDEX_TYPE > >( name, elemCnt, numberOfBuffersToStoreOnDevice, numberOfBuffersToStoreOnHost, maxNumberOfBuffers );
87  }
88  else
89 #endif
90  {
91  m_lifo = std::make_unique< LifoStorageHost< T, INDEX_TYPE > >( name, elemCnt, numberOfBuffersToStoreOnHost, maxNumberOfBuffers );
92  }
93 
94  }
95 
105  LifoStorage( std::string name, arrayView1d< T > array, int numberOfBuffersToStoreOnDevice, int numberOfBuffersToStoreOnHost, int maxNumberOfBuffers ):
106  LifoStorage( name, array.size(), numberOfBuffersToStoreOnDevice, numberOfBuffersToStoreOnHost, maxNumberOfBuffers ) {}
107 
114  {
115  LIFO_MARK_FUNCTION;
116  m_lifo->pushAsync( array );
117  }
118 
122  void pushWait()
123  {
124  LIFO_MARK_FUNCTION;
125  m_lifo->pushWait();
126  }
127 
133  void push( arrayView1d< T > array )
134  {
135  LIFO_MARK_FUNCTION;
136  pushAsync( array );
137  pushWait();
138  }
139 
146  {
147  LIFO_MARK_FUNCTION;
148  m_lifo->popAsyncPrelude();
149  m_lifo->popAsync( array );
150  }
151 
155  void popWait()
156  {
157  LIFO_MARK_FUNCTION;
158  m_lifo->popWait();
159  }
160 
166  void pop( arrayView1d< T > array )
167  {
168  LIFO_MARK_FUNCTION;
169  popAsync( array );
170  popWait();
171  }
172 
178  bool empty()
179  {
180  return m_lifo->empty();
181  }
182 
183 private:
185  int m_maxNumberOfBuffers;
187  size_t m_bufferSize;
189  int m_bufferCount;
190 
192  std::unique_ptr< LifoStorageCommon< T, INDEX_TYPE > > m_lifo;
193 
194 };
195 }
196 #endif // LIFOSTORAGE_HPP
static int computeNumberOfBufferOnHost(int percent, size_t bufferSize, int maxNumberOfBuffers, int numberOfBuffersToStoreOnDevice)
static int computeNumberOfBufferOnDevice(int percent, size_t bufferSize, int maxNumberOfBuffers)
void push(arrayView1d< T > array)
void pushAsync(arrayView1d< T > array)
LifoStorage(std::string name, arrayView1d< T > array, int numberOfBuffersToStoreOnDevice, int numberOfBuffersToStoreOnHost, int maxNumberOfBuffers)
void pop(arrayView1d< T > array)
void popAsync(arrayView1d< T > array)
LifoStorage(std::string name, size_t elemCnt, int numberOfBuffersToStoreOnDevice, int numberOfBuffersToStoreOnHost, int maxNumberOfBuffers)
Definition: LifoStorage.hpp:61
ArrayView< T, 1 > arrayView1d
Alias for 1D array view.
Definition: DataTypes.hpp:188
std::string string
String type.
Definition: DataTypes.hpp:90