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