proxygen
folly::detail::DigestBuilder< DigestT > Class Template Reference

#include <DigestBuilder.h>

Classes

struct  CpuLocalBuffer
 

Public Member Functions

 DigestBuilder (size_t bufferSize, size_t digestSize)
 
DigestT build ()
 
void append (double value)
 

Private Attributes

std::vector< CpuLocalBuffercpuLocalBuffers_
 
size_t bufferSize_
 
size_t digestSize_
 

Detailed Description

template<typename DigestT>
class folly::detail::DigestBuilder< DigestT >

Definition at line 40 of file DigestBuilder.h.

Constructor & Destructor Documentation

template<typename DigestT >
folly::detail::DigestBuilder< DigestT >::DigestBuilder ( size_t  bufferSize,
size_t  digestSize 
)
explicit

Definition at line 31 of file DigestBuilder-defs.h.

References folly::detail::DigestBuilder< DigestT >::cpuLocalBuffers_, and folly::CacheLocality::system().

32  : bufferSize_(bufferSize), digestSize_(digestSize) {
33  auto& cl = CacheLocality::system();
34  cpuLocalBuffers_.resize(cl.numCachesByLevel[0]);
35 }
static const CacheLocality & system()
std::vector< CpuLocalBuffer > cpuLocalBuffers_
Definition: DigestBuilder.h:63

Member Function Documentation

template<typename DigestT >
void folly::detail::DigestBuilder< DigestT >::append ( double  value)

Definition at line 74 of file DigestBuilder-defs.h.

References folly::detail::DigestBuilder< DigestT >::bufferSize_, folly::detail::DigestBuilder< DigestT >::cpuLocalBuffers_, folly::AccessSpreader< Atom >::current(), folly::detail::DigestBuilder< DigestT >::digestSize_, g(), and folly::detail::tls_lastCpuBufferSlot.

Referenced by TEST().

74  {
75  auto& which = tls_lastCpuBufferSlot;
76  auto cpuLocalBuf = &cpuLocalBuffers_[which];
77  std::unique_lock<SpinLock> g(cpuLocalBuf->mutex, std::try_to_lock_t());
78  if (!g.owns_lock()) {
80  cpuLocalBuf = &cpuLocalBuffers_[which];
81  g = std::unique_lock<SpinLock>(cpuLocalBuf->mutex);
82  }
83  cpuLocalBuf->buffer.push_back(value);
84  if (cpuLocalBuf->buffer.size() == bufferSize_) {
85  if (!cpuLocalBuf->digest) {
86  cpuLocalBuf->digest = std::make_unique<DigestT>(digestSize_);
87  }
88  *cpuLocalBuf->digest = cpuLocalBuf->digest->merge(cpuLocalBuf->buffer);
89  cpuLocalBuf->buffer.clear();
90  }
91 }
std::vector< CpuLocalBuffer > cpuLocalBuffers_
Definition: DigestBuilder.h:63
g_t g(f_t)
static size_t current(size_t numStripes)
uint64_t value(const typename LockFreeRingBuffer< T, Atom >::Cursor &rbcursor)
static FOLLY_TLS uint32_t tls_lastCpuBufferSlot
template<typename DigestT >
DigestT folly::detail::DigestBuilder< DigestT >::build ( )

Definition at line 38 of file DigestBuilder-defs.h.

References count, folly::detail::DigestBuilder< DigestT >::cpuLocalBuffers_, folly::detail::DigestBuilder< DigestT >::digestSize_, g(), merge(), folly::gen::move, and values().

Referenced by TEST().

38  {
39  std::vector<std::vector<double>> valuesVec;
40  std::vector<std::unique_ptr<DigestT>> digestPtrs;
41  valuesVec.reserve(cpuLocalBuffers_.size());
42  digestPtrs.reserve(cpuLocalBuffers_.size());
43 
44  for (auto& cpuLocalBuffer : cpuLocalBuffers_) {
45  SpinLockGuard g(cpuLocalBuffer.mutex);
46  valuesVec.push_back(std::move(cpuLocalBuffer.buffer));
47  if (cpuLocalBuffer.digest) {
48  digestPtrs.push_back(std::move(cpuLocalBuffer.digest));
49  }
50  }
51 
52  std::vector<DigestT> digests;
53  for (auto& digestPtr : digestPtrs) {
54  digests.push_back(std::move(*digestPtr));
55  }
56 
57  size_t count = 0;
58  for (const auto& vec : valuesVec) {
59  count += vec.size();
60  }
61  if (count) {
62  std::vector<double> values;
63  values.reserve(count);
64  for (const auto& vec : valuesVec) {
65  values.insert(values.end(), vec.begin(), vec.end());
66  }
67  DigestT digest(digestSize_);
68  digests.push_back(digest.merge(values));
69  }
70  return DigestT::merge(digests);
71 }
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
std::vector< CpuLocalBuffer > cpuLocalBuffers_
Definition: DigestBuilder.h:63
SpinLockGuardImpl< SpinLock > SpinLockGuard
Definition: SpinLock.h:79
Definition: Traits.h:588
int * count
g_t g(f_t)
void merge(unsigned int iters, size_t maxSize, size_t bufSize)
std::vector< int > values(1'000)

Member Data Documentation

template<typename DigestT>
size_t folly::detail::DigestBuilder< DigestT >::bufferSize_
private

Definition at line 64 of file DigestBuilder.h.

Referenced by folly::detail::DigestBuilder< DigestT >::append().

template<typename DigestT>
size_t folly::detail::DigestBuilder< DigestT >::digestSize_
private

The documentation for this class was generated from the following files: