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 Total, S.A
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  double bufferSize = ( ( double ) m_bufferSize ) / ( 1024.0 * 1024.0 );
68  LIFO_LOG_RANK( " LIFO : buffer size "<< bufferSize << "MB" );
69  if( numberOfBuffersToStoreOnDevice < 0 )
70  {
71 #ifdef GEOS_USE_CUDA
72  numberOfBuffersToStoreOnDevice = LifoStorageCuda< T, INDEX_TYPE >::computeNumberOfBufferOnDevice( -numberOfBuffersToStoreOnDevice, m_bufferSize, m_maxNumberOfBuffers );
73 #else
74  numberOfBuffersToStoreOnDevice = 0;
75 #endif
76  }
77  if( numberOfBuffersToStoreOnHost < 0 )
78  {
79  numberOfBuffersToStoreOnHost =
80  LifoStorageCommon< T, INDEX_TYPE >::computeNumberOfBufferOnHost( -numberOfBuffersToStoreOnHost, m_bufferSize, m_maxNumberOfBuffers, numberOfBuffersToStoreOnDevice );
81  }
82  LIFO_LOG_RANK( " LIFO : allocating "<< numberOfBuffersToStoreOnHost <<" buffers on host" );
83  LIFO_LOG_RANK( " LIFO : allocating "<< numberOfBuffersToStoreOnDevice <<" buffers on device" );
84 #ifdef GEOS_USE_CUDA
85  if( numberOfBuffersToStoreOnDevice > 0 )
86  {
87  m_lifo = std::make_unique< LifoStorageCuda< T, INDEX_TYPE > >( name, elemCnt, numberOfBuffersToStoreOnDevice, numberOfBuffersToStoreOnHost, maxNumberOfBuffers );
88  }
89  else
90 #endif
91  {
92  m_lifo = std::make_unique< LifoStorageHost< T, INDEX_TYPE > >( name, elemCnt, numberOfBuffersToStoreOnHost, maxNumberOfBuffers );
93  }
94 
95  }
96 
106  LifoStorage( std::string name, arrayView1d< T > array, int numberOfBuffersToStoreOnDevice, int numberOfBuffersToStoreOnHost, int maxNumberOfBuffers ):
107  LifoStorage( name, array.size(), numberOfBuffersToStoreOnDevice, numberOfBuffersToStoreOnHost, maxNumberOfBuffers ) {}
108 
115  {
116  LIFO_MARK_FUNCTION;
117  m_lifo->pushAsync( array );
118  }
119 
123  void pushWait()
124  {
125  LIFO_MARK_FUNCTION;
126  m_lifo->pushWait();
127  }
128 
134  void push( arrayView1d< T > array )
135  {
136  LIFO_MARK_FUNCTION;
137  pushAsync( array );
138  pushWait();
139  }
140 
147  {
148  LIFO_MARK_FUNCTION;
149  m_lifo->popAsyncPrelude();
150  m_lifo->popAsync( array );
151  }
152 
156  void popWait()
157  {
158  LIFO_MARK_FUNCTION;
159  m_lifo->popWait();
160  }
161 
167  void pop( arrayView1d< T > array )
168  {
169  LIFO_MARK_FUNCTION;
170  popAsync( array );
171  popWait();
172  }
173 
179  bool empty()
180  {
181  return m_lifo->empty();
182  }
183 
184 private:
186  int m_maxNumberOfBuffers;
188  size_t m_bufferSize;
190  int m_bufferCount;
191 
193  std::unique_ptr< LifoStorageCommon< T, INDEX_TYPE > > m_lifo;
194 
195 };
196 }
197 #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:180
std::string string
String type.
Definition: DataTypes.hpp:91