24 #ifndef _GEMA_APPEND_BUFFER_H_ 25 #define _GEMA_APPEND_BUFFER_H_ 30 #include <QAtomicInteger> 31 #include <QVarLengthArray> 38 #ifndef Q_ATOMIC_INT64_IS_SUPPORTED 39 #error No 64 bit support for atomic operations 47 static_assert(
sizeof(
size_t) == 8,
"Unexpected size_t size");
75 virtual void clear() = 0;
88 virtual void append(
const T& val) = 0;
94 virtual size_t size()
const = 0;
109 virtual T*
data() = 0;
181 Q_UNUSED(resizeFactor);
184 assert(initSize >= 0);
219 for(
int i = 0, nb =
_data.
size(); i < nb; i++)
249 size_t s = (bsize +
_nt-1)/
_nt;
251 for(
int i = 1; i <
_nt; i++)
257 virtual void append(
const T& val)
266 virtual void appendFromThread(
int tid,
const T& val)
276 virtual size_t size()
const 286 for(
int i = 0, nb =
_data.
size(); i < nb; i++)
314 for(
int i = 0, nb =
_data.
size(); i < nb; i++)
319 size_t n = buffer.
size();
336 virtual size_t usedMemory()
const 350 for(
int i = 0, nb =
_data.
size(); i < nb; i++)
352 return s *
sizeof(T);
420 assert(resizeFactor > 1.0);
421 assert(initSize >= 0);
463 _head =
new ControllData(bsize, NULL);
468 virtual void append(
const T& val)
482 ControllData* p =
_controll.loadAcquire();
499 qint64 index = pos - p->_offset;
501 if(index < (qint64)p->_size)
504 while(index < 0) { assert(p->_prev); p = p->_prev; index += p->_size; }
506 p->_data[index] = val;
526 size_t cursize = p->_offset + p->_size;
527 size_t newsize = qMax((
size_t)(cursize *
_resizeFactor), pos + 1);
531 ControllData* newc =
new ControllData(newsize - cursize, p);
533 assert(pos >= newc->_offset);
534 newc->_data[pos - newc->_offset] = val;
550 virtual void appendFromThread(
int tid,
const T& val)
594 ControllData* p =
_head;
600 assert(!p->_prev || p->_prev->_next == p);
601 assert(!p->_next || p->_next->_prev == p);
602 assert(p->_offset == o);
622 virtual size_t usedMemory()
const 631 return (p->_offset + p->_size) *
sizeof(T);
639 while((p =
_head) != NULL)
T * _data
This data buffer.
Definition: gmAppendBuffer.h:666
T * _dataBuffer
The single buffer after a call to data()
Definition: gmAppendBuffer.h:359
A simple spin lock implementation based on a loop using test and set over an atomic int to change its...
Definition: gmSpinLock.h:36
T fetchAndAddAcquire(T valueToAdd)
void * GmPmemcpy(void *dst, const void *src, size_t n, int nt=0, size_t min=10 *1024 *1024)
Parallel (thread enabled) version of memcpy using OpenMP.
Definition: gmMemory.h:167
bool _globalOnly
Flag set to true if the user passed zero as the number of threads for the constructor.
Definition: gmAppendBuffer.h:357
An implementation of the GmAppendBuffer interface based on a "per thread" growing buffer.
Definition: gmAppendBuffer.h:152
virtual void append(const T &val)=0
Appends val to the buffer in a thread safe way. Can grow the buffer if needed.
Aux controll structure storing a buffer.
Definition: gmAppendBuffer.h:648
Declaration of the GmTLS class.
virtual void appendFromThread(int tid, const T &val)=0
Appends val to the buffer in a thread safe way, using the given tid to access TLS storage....
ControllData * _prev
The previous buffer.
Definition: gmAppendBuffer.h:670
GmPerThreadAppendBuffer(size_t initSize, double resizeFactor=2.0, int numThreads=-1)
Buffer constructor. Can optionally pre allocate the buffer with initSize entries. If initSize is zero...
Definition: gmAppendBuffer.h:178
#define S_TRACE()
Macro for run time stack tracking at release build.
Definition: gmTrace.h:44
An implementation of the GmAppendBuffer interface based on synchoronized access to a shared buffer.
Definition: gmAppendBuffer.h:402
virtual T * data()=0
Returns a vector filled with the buffer data. After this call, NO calls to append() can be made witho...
A class that works together with GmThreadManager to provide thread local storage.
Definition: gmThreadLocalStorage.h:131
Declaration of the GmSpinLock class.
double _resizeFactor
The resize factor.
Definition: gmAppendBuffer.h:675
A virtual class representing a buffer of T objects that can be appended in a thread-safe way,...
Definition: gmArmadilloSolverMatrix.h:36
virtual void reserve(size_t bsize)
See comments on the base class. Should not be called if the size was given in the constructor.
Definition: gmAppendBuffer.h:456
bool tryLock()
Try to lock. Returns true if the lock was aquired, false otherwise.
Definition: gmSpinLock.h:46
Declaration of the GmThreadManager class.
virtual void reserve(size_t bsize)
Pre allocates buffer sizes. See basic description on the base class Should not be called if the size ...
Definition: gmAppendBuffer.h:232
QAtomicInteger< size_t > _nextIndex
The next free index in the global vector.
Definition: gmAppendBuffer.h:676
~ControllData()
Destructor.
Definition: gmAppendBuffer.h:664
size_t _size
The size of this data buffer.
Definition: gmAppendBuffer.h:667
T & localData(int tid)
Returns the given thread local data as a modifiable reference.
Definition: gmThreadLocalStorage.h:163
T * _dataBuffer
The single buffer after a call to data()
Definition: gmAppendBuffer.h:679
int GmOmpAdjustNumThreads(int nt)
Adjusts the given number of threads. If nt <= 0 or if nt > maximum number of omp threads,...
Definition: gmOmp.h:51
int _nt
Number of threads considered for buffer pre-allocation & parallel memcopy.
Definition: gmAppendBuffer.h:358
GmSingleAppendBuffer(size_t initSize, double resizeFactor=2.0, int numThreads=-1)
Buffer constructor. Can optionally pre allocate the buffer with initSize entries. If initSize is zero...
Definition: gmAppendBuffer.h:415
virtual void reserve(size_t size)=0
Informs the buffer of the expected number of entries that will be filled by (concurrent) calls to app...
virtual void clear()=0
Releases the memory used by the buffer, returning it to a recently constructed state....
virtual size_t usedMemory() const =0
Returns an estimative of the total memory used by the buffer in bytes.
virtual size_t size() const =0
Returns the number of entries in the buffer. Must be called from the main thread only.
size_t _dataSize
The size in _dataBuffer when _dataBuffer is not NULL.
Definition: gmAppendBuffer.h:360
ControllData * _next
The next buffer.
Definition: gmAppendBuffer.h:671
GmTLS< QVarLengthArray< T >, false > _data
Per thread buffers.
Definition: gmAppendBuffer.h:356
static bool inMainThread()
Is the current thread the main thread? Equivalent to comparing the currentId() with 0.
Definition: gmThreadManager.h:169
int size() const
Returns the number of values stored in the TLS object. Equal to the max number of threads + 1 (for th...
Definition: gmThreadLocalStorage.h:160
int _nt
Number of threads considered for parallel memcopy.
Definition: gmAppendBuffer.h:680
void unlock()
Releases the lock.
Definition: gmSpinLock.h:49
~GmPerThreadAppendBuffer()
Destructor.
Definition: gmAppendBuffer.h:199
~GmSingleAppendBuffer()
Destructor. Releases the allocated memory.
Definition: gmAppendBuffer.h:436
GmSpinLock _controllLock
The lock controlling changes to _controll.
Definition: gmAppendBuffer.h:678
virtual ~GmAppendBuffer()
Virtual destructor.
Definition: gmAppendBuffer.h:69
size_t _offset
The offset of the first entry in data in the global buffer reference.
Definition: gmAppendBuffer.h:668
QAtomicPointer< ControllData > _controll
The controll block pointing to the current buffer.
Definition: gmAppendBuffer.h:677
Auxiliary configuration file used to enable or disable compiling the GeMA tools with support for usin...
static int maxWorkerThreads()
Returns the maximum number of allowed working threads.
Definition: gmThreadManager.h:153
ControllData * _head
Pointer to the first allocated buffer.
Definition: gmAppendBuffer.h:674
ControllData(size_t size, ControllData *prev)
Constructor: Initializes the buffer with the given size. Allocation errors should be cought by the ca...
Definition: gmAppendBuffer.h:653