souffle  2.0.2-371-g6315b36
Data Structures | Public Member Functions | Data Fields | Static Public Attributes
souffle::PiggyList< T > Class Template Reference

#include <PiggyList.h>

Collaboration diagram for souffle::PiggyList< T >:
Collaboration graph

Data Structures

class  iterator
 

Public Member Functions

size_t append (T element)
 
iterator begin ()
 
void clear ()
 Clear all elements from the PiggyList. More...
 
size_t createNode ()
 
iterator end ()
 
void freeList ()
 Free the arrays allocated within the linked list nodes. More...
 
T & get (size_t index) const
 Retrieve a reference to the stored value at index. More...
 
T * getBlock (size_t blocknum) const
 
PiggyListoperator= (const PiggyList &other)=delete
 copy assign ctor More...
 
 PiggyList ()
 
 PiggyList (const PiggyList &other)
 copy constructor More...
 
 PiggyList (PiggyList &&other)=delete
 move constructor More...
 
 PiggyList (size_t initialbitsize)
 
size_t size () const
 Well, returns the number of nodes exist within the list + number of nodes queued to be inserted The reason for this, is that there may be many nodes queued up that haven't had time to had containers created and updated. More...
 
 ~PiggyList ()
 

Data Fields

size_t allocsize = BLOCKSIZE
 
const size_t BLOCKBITS = 16ul
 
std::array< T *, max_contsblockLookupTable = {}
 
const size_t BLOCKSIZE = (1ul << BLOCKBITS)
 
std::atomic< size_t > container_size = 0
 
std::atomic< size_t > m_size = 0
 
std::atomic< size_t > num_containers = 0
 
SpinLock sl
 

Static Public Attributes

static constexpr size_t max_conts = 64
 

Detailed Description

template<class T>
class souffle::PiggyList< T >

Definition at line 143 of file PiggyList.h.

Constructor & Destructor Documentation

◆ PiggyList() [1/4]

template<class T >
souffle::PiggyList< T >::PiggyList ( )
inline

Definition at line 145 of file PiggyList.h.

145 : num_containers(0), container_size(0), m_size(0) {}

◆ PiggyList() [2/4]

template<class T >
souffle::PiggyList< T >::PiggyList ( size_t  initialbitsize)
inline

Definition at line 146 of file PiggyList.h.

147  : BLOCKBITS(initialbitsize), num_containers(0), container_size(0), m_size(0) {}

◆ PiggyList() [3/4]

template<class T >
souffle::PiggyList< T >::PiggyList ( const PiggyList< T > &  other)
inline

copy constructor

Definition at line 150 of file PiggyList.h.

150  : BLOCKBITS(other.BLOCKBITS) {
151  num_containers.store(other.num_containers.load());
152  container_size.store(other.container_size.load());
153  m_size.store(other.m_size.load());
154  // copy each chunk from other into this
155  // the size of the next container to allocate
156  size_t cSize = BLOCKSIZE;
157  for (size_t i = 0; i < other.num_containers; ++i) {
158  this->blockLookupTable[i] = new T[cSize];
159  std::memcpy(this->blockLookupTable[i], other.blockLookupTable[i], cSize * sizeof(T));
160  cSize <<= 1;
161  }
162  // if this isn't the case, uhh
163  assert((cSize >> 1) == container_size.load());
164  }

◆ PiggyList() [4/4]

template<class T >
souffle::PiggyList< T >::PiggyList ( PiggyList< T > &&  other)
delete

move constructor

◆ ~PiggyList()

template<class T >
souffle::PiggyList< T >::~PiggyList ( )
inline

Definition at line 171 of file PiggyList.h.

171  {
172  freeList();
173  }

Member Function Documentation

◆ append()

template<class T >
size_t souffle::PiggyList< T >::append ( element)
inline

Definition at line 189 of file PiggyList.h.

189  {
190  size_t new_index = m_size.fetch_add(1, std::memory_order_acquire);
191 
192  // will this not fit?
193  if (container_size < new_index + 1) {
194  sl.lock();
195  // check and add as many containers as required
196  while (container_size < new_index + 1) {
198  num_containers += 1;
200  // double the number elements that will be allocated next time
201  allocsize <<= 1;
202  }
203  sl.unlock();
204  }
205 
206  this->get(new_index) = element;
207  return new_index;
208  }

Referenced by souffle::test::TEST().

◆ begin()

template<class T >
iterator souffle::PiggyList< T >::begin ( )
inline

Definition at line 295 of file PiggyList.h.

295  {
296  return iterator(this);
297  }

◆ clear()

template<class T >
void souffle::PiggyList< T >::clear ( )
inline

Clear all elements from the PiggyList.

Definition at line 246 of file PiggyList.h.

246  {
247  freeList();
248  m_size = 0;
249  num_containers = 0;
250 
252  container_size = 0;
253  }

◆ createNode()

template<class T >
size_t souffle::PiggyList< T >::createNode ( )
inline

Definition at line 210 of file PiggyList.h.

210  {
211  size_t new_index = m_size.fetch_add(1, std::memory_order_acquire);
212 
213  // will this not fit?
214  if (container_size < new_index + 1) {
215  sl.lock();
216  // check and add as many containers as required
217  while (container_size < new_index + 1) {
219  num_containers += 1;
221  // double the number elements that will be allocated next time
222  allocsize <<= 1;
223  }
224  sl.unlock();
225  }
226 
227  return new_index;
228  }

◆ end()

template<class T >
iterator souffle::PiggyList< T >::end ( )
inline

Definition at line 298 of file PiggyList.h.

298  {
299  return iterator(this, size());
300  }

◆ freeList()

template<class T >
void souffle::PiggyList< T >::freeList ( )
inline

Free the arrays allocated within the linked list nodes.

Definition at line 320 of file PiggyList.h.

320  {
321  sl.lock();
322  // we don't know which ones are taken up!
323  for (size_t i = 0; i < num_containers; ++i) {
324  delete[] blockLookupTable[i];
325  }
326  sl.unlock();
327  }

Referenced by souffle::PiggyList< std::atomic< block_t > >::clear(), and souffle::PiggyList< std::atomic< block_t > >::~PiggyList().

◆ get()

template<class T >
T& souffle::PiggyList< T >::get ( size_t  index) const
inline

Retrieve a reference to the stored value at index.

Parameters
indexposition to search
Returns
the value at index

Definition at line 235 of file PiggyList.h.

235  {
236  // supa fast 2^16 size first block
237  size_t nindex = index + BLOCKSIZE;
238  size_t blockNum = (63 - __builtin_clzll(nindex));
239  size_t blockInd = (nindex) & ((1 << blockNum) - 1);
240  return this->getBlock(blockNum - BLOCKBITS)[blockInd];
241  }

Referenced by souffle::PiggyList< std::atomic< block_t > >::append(), souffle::EquivalenceRelation< Arity >::insert(), souffle::EquivalenceRelation< TupleType >::iterator::iterator(), souffle::PiggyList< T >::iterator::operator*(), and souffle::DisjointSet::size().

◆ getBlock()

template<class T >
T* souffle::PiggyList< T >::getBlock ( size_t  blocknum) const
inline

Definition at line 185 of file PiggyList.h.

185  {
186  return this->blockLookupTable[blocknum];
187  }

Referenced by souffle::PiggyList< std::atomic< block_t > >::get().

◆ operator=()

template<class T >
PiggyList& souffle::PiggyList< T >::operator= ( const PiggyList< T > &  other)
delete

copy assign ctor

◆ size()

template<class T >
size_t souffle::PiggyList< T >::size ( ) const
inline

Well, returns the number of nodes exist within the list + number of nodes queued to be inserted The reason for this, is that there may be many nodes queued up that haven't had time to had containers created and updated.

Returns
the number of nodes exist within the list + number of nodes queued to be inserted

Definition at line 181 of file PiggyList.h.

181  {
182  return m_size.load();
183  };

Referenced by souffle::PiggyList< std::atomic< block_t > >::end(), souffle::EquivalenceRelation< Arity >::insert(), and souffle::EquivalenceRelation< TupleType >::iterator::iterator().

Field Documentation

◆ allocsize

template<class T >
size_t souffle::PiggyList< T >::allocsize = BLOCKSIZE

◆ BLOCKBITS

template<class T >
const size_t souffle::PiggyList< T >::BLOCKBITS = 16ul

Definition at line 301 of file PiggyList.h.

Referenced by souffle::PiggyList< std::atomic< block_t > >::get().

◆ blockLookupTable

template<class T >
std::array<T*, max_conts> souffle::PiggyList< T >::blockLookupTable = {}

◆ BLOCKSIZE

template<class T >
const size_t souffle::PiggyList< T >::BLOCKSIZE = (1ul << BLOCKBITS)

◆ container_size

template<class T >
std::atomic<size_t> souffle::PiggyList< T >::container_size = 0

◆ m_size

template<class T >
std::atomic<size_t> souffle::PiggyList< T >::m_size = 0

◆ max_conts

template<class T >
constexpr size_t souffle::PiggyList< T >::max_conts = 64
staticconstexpr

Definition at line 311 of file PiggyList.h.

◆ num_containers

template<class T >
std::atomic<size_t> souffle::PiggyList< T >::num_containers = 0

◆ sl

template<class T >
SpinLock souffle::PiggyList< T >::sl
mutable

The documentation for this class was generated from the following file:
souffle::PiggyList::get
T & get(size_t index) const
Retrieve a reference to the stored value at index.
Definition: PiggyList.h:235
souffle::PiggyList::size
size_t size() const
Well, returns the number of nodes exist within the list + number of nodes queued to be inserted The r...
Definition: PiggyList.h:181
souffle::PiggyList::allocsize
size_t allocsize
Definition: PiggyList.h:306
souffle::PiggyList::getBlock
T * getBlock(size_t blocknum) const
Definition: PiggyList.h:185
souffle::PiggyList::BLOCKBITS
const size_t BLOCKBITS
Definition: PiggyList.h:301
souffle::PiggyList::num_containers
std::atomic< size_t > num_containers
Definition: PiggyList.h:305
i
size_t i
Definition: json11.h:663
souffle::PiggyList::sl
SpinLock sl
Definition: PiggyList.h:315
souffle::PiggyList::blockLookupTable
std::array< T *, max_conts > blockLookupTable
Definition: PiggyList.h:312
souffle::PiggyList::m_size
std::atomic< size_t > m_size
Definition: PiggyList.h:308
souffle::PiggyList::container_size
std::atomic< size_t > container_size
Definition: PiggyList.h:307
souffle::SpinLock::lock
void lock()
Definition: ParallelUtil.h:491
souffle::PiggyList::BLOCKSIZE
const size_t BLOCKSIZE
Definition: PiggyList.h:302
souffle::PiggyList::freeList
void freeList()
Free the arrays allocated within the linked list nodes.
Definition: PiggyList.h:320
souffle::SpinLock::unlock
void unlock()
Definition: ParallelUtil.h:497