25#include <boost/thread/condition_variable.hpp>
26#include <boost/thread/locks.hpp>
27#include <boost/thread/mutex.hpp>
28#include <boost/thread/thread.hpp>
62 template <
class Index>
77 template <
class BlockIndex,
class Index>
80 assert(i>=0 && i<=n && n>0);
93 template <
class Index>
96 assert(j>=0 && j<m && n>0 && m>0);
104 Index high = ((j+1)*n)/m;
151 boost::lock_guard<boost::mutex> lock(q.mutex);
152 queue = std::move(q.queue);
162 boost::lock_guard<boost::mutex> lock(q.mutex);
170 boost::lock_guard<boost::mutex> lock(mutex);
171 return queue.empty();
180 boost::lock_guard<boost::mutex> lock(mutex);
190 boost::lock_guard<boost::mutex> lock(mutex);
191 queue.push(std::move(t));
203 boost::unique_lock<boost::mutex> lock(mutex);
206 while (queue.empty())
211 T t = std::move(queue.front());
224 boost::lock_guard<boost::mutex> lock(mutex);
226 return runningWorkers;
235 return runningWorkers;
240 mutable boost::mutex mutex;
241 boost::condition_variable filled;
251 typedef std::packaged_task<void()>
Task;
317 static NumaThreadPool&
instance(
int maxThreads = std::numeric_limits<int>::max());
348 return globalQueue.running();
356 return cpuByNode[node].size();
364 return maxCpusPerNode;
463 NumaThreadPool(
int maxThreads);
469 int nCpu, nNode, maxCpusPerNode;
470 std::vector<ConcurrentQueue<Task>> nodeQueue;
472 boost::thread_group threads;
473 std::vector<int> nodeByCpu;
474 std::vector<std::vector<int>> cpuByNode;
475 std::map<void*,std::pair<size_t,int>> memBlocks;
476 std::vector<Kalloc> nodeMemory;
501 template <
class Func>
502 void parallelFor(Func
const& f,
int maxTasks = std::numeric_limits<int>::max())
514 int nTasks = std::min(4*pool.
cpus(),maxTasks);
516 std::vector<Ticket> tickets(nTasks);
517 for (
int i=0; i<nTasks; ++i)
518 tickets[i] = pool.
run(
Task([i,&f,nTasks] { f(i,nTasks); }));
520 for (
auto& ticket: tickets)
543 template <
class Func>
544 void parallelFor(
size_t first,
size_t last, Func
const& f,
size_t nTasks=std::numeric_limits<size_t>::max())
548 nTasks = std::min(last-first,std::min(nTasks,
size_t(pool.
cpus())));
550 std::vector<Ticket> tickets(nTasks);
556 for (
size_t i=0; i<nTasks; ++i)
558 tickets[i] = pool.
run(
Task([&f,&first,last,&mutex,i,&tt]
563 std::unique_lock<std::mutex> lock(mutex);
564 size_t myPos = first;
582 std::lock_guard<std::mutex> lock(mutex);
592 for (
auto& ticket: tickets)
596 std::ofstream out(
"timing.gnu");
616 template <
class Func>
620 int nTasks = std::min(pool.
nodes(),maxTasks);
621 std::vector<Ticket> tickets(nTasks);
622 for (
int i=0; i<nTasks; ++i)
623 tickets[i] = pool.
runOnNode(i,
Task([i,&f,&nTasks] { f(i,nTasks); }));
624 for (
auto& ticket: tickets)
633 namespace ThreadingDetail
635 class NumaAllocatorBase
638 NumaAllocatorBase(
int node);
640 size_t max_size()
const;
642 void* allocate(
size_t n);
644 void deallocate(
void* p,
size_t n);
718 return alloc.max_size() /
sizeof(T);
732 return static_cast<pointer>(alloc.allocate(n*
sizeof(T)));
740 alloc.deallocate(
static_cast<void*
>(p),n*
sizeof(T));
743 template<
class U,
class... Args >
746 ::new((
void*)p) U(std::forward<Args>(args)...);
773 ThreadingDetail::NumaAllocatorBase alloc;
811 boost::mutex&
get() {
return mutex; }
size_t size() const
Returns the number of tasks waiting.
ConcurrentQueue()
Constructs an empty queue.
int running(int n)
Change the number of running worker threads.
void push_back(T &&t)
Stores an element at the end of the queue.
ConcurrentQueue & operator=(ConcurrentQueue const &q)
Assignment.
T pop_front()
Retrieves the foremost element.
ConcurrentQueue(ConcurrentQueue &&q)
Moves a queue.
int running() const
Get the number of running worker threads.
A simple memory manager for NUMA systems.
boost::mutex & get()
provides access to the mutex to perform the locking.
Mutex & operator=(Mutex const &m)
Assignment.
Mutex(Mutex const &m)
Copy constructor.
Mutex()=default
Default constructor.
void construct(U *p, Args &&... args)
void deallocate(pointer p, size_type n)
NumaAllocator(int node)
Construct an allocator for allocating on the given NUMA node.
const_pointer address(const_reference x) const
pointer address(reference x) const
T const & const_reference
std::true_type propagate_on_container_copy_assignment
bool operator==(NumaAllocator< U > const &other) const
comparison for equality
bool operator!=(NumaAllocator< U > const &other) const
std::true_type propagate_on_container_swap
pointer allocate(size_type n, void *=0)
Allocates the requested amount of memory.
std::true_type propagate_on_container_move_assignment
size_type max_size() const
std::ptrdiff_t difference_type
Implementation of thread pools suitable for parallelization of (more or less) memory-bound algorithms...
int runningOnGlobalQueue() const
Reports how many worker threads are running to work on the global task queue.
static NumaThreadPool & instance(int maxThreads=std::numeric_limits< int >::max())
Returns a globally unique thread pool instance.
int nodes() const
Reports the number of NUMA nodes (i.e., memory interfaces/CPU sockets)
size_t alignment(int node) const
Reports the alignment size of allocator at given NUMA node.
void * allocate(size_t n, int node)
Allocates memory on a specific node.
int maxCpusOnNode() const
Reports the maximal number of CPUs on one node.
Ticket runOnNode(int node, Task &&task)
Schedules a task to be executed on a CPU belonging to the given NUMA node.
int cpus() const
Reports the total number of CPUs (usually a multiple of nodes).
void deallocate(void *p, size_t n, int node)
frees a chunk of memory previously allocated
void reserve(size_t n, size_t k, int node)
Tells the allocator to prepare for subsequent allocation of several memory blocks of same size.
bool isSequential() const
Returns true if tasks are executed sequentially. sequential execution can be enforced by calling Numa...
int cpus(int node) const
Reports the number of CPUs on the given node (usually the same for all nodes).
Ticket run(Task &&task)
Schedules a task to be executed on an arbitrary CPU.
Kalloc & allocator(int node)
Returns the allocator used for the given node.
A class that gathers data on task timing and provides gnuplot visualization.
void start(int task)
defines the start of given task.
void stop(int task)
defines the start of given task.
Index uniformWeightRangeStart(BlockIndex i, BlockIndex n, Index m)
Computes partitioning points of ranges for uniform weight distributions.
std::packaged_task< void()> Task
Abstract interface for tasks to be scheduled for concurrent execution.
void runInBackground(std::function< void()> &f)
Executes a function in a child process.
std::future< void > Ticket
Abstract waitable job ticket for submitted tasks.
void parallelForNodes(Func const &f, int maxTasks=std::numeric_limits< int >::max())
A parallel for loop that executes the given functor in parallel on different NUMA nodes.
void equalWeightRanges(std::vector< Index > &x, int n)
Computes partitioning points such that the sum of weights in each partition is roughly the same.
Index uniformWeightRange(Index j, Index n, Index m)
Computes the range in which an index is to be found when partitioned for uniform weights.
boost::mutex refElementMutex
A global lock for the Dune::GenericReferenceElement singletons, which are not thread-safe.
void parallelFor(Func const &f, int maxTasks=std::numeric_limits< int >::max())
A parallel for loop that executes the given functor in parallel on different CPUs.
std::mutex DuneQuadratureRulesMutex
A global lock for the Dune::QuadratureRules factory, which is not thread-safe as of 2015-01-01.