Caffe2 - C++ API
A deep learning, cross platform ML framework
pthreadpool_impl.cc
1 #include "caffe2/utils/threadpool/pthreadpool.h"
2 #include "caffe2/utils/threadpool/ThreadPool.h"
3 
4 
5 //
6 // External API
7 //
8 
9 void pthreadpool_compute_1d(
10  pthreadpool_t threadpool,
11  pthreadpool_function_1d_t function,
12  void* argument,
13  size_t range) {
14  if (threadpool == nullptr) {
15  /* No thread pool provided: execute function sequentially on the calling
16  * thread */
17  for (size_t i = 0; i < range; i++) {
18  function(argument, i);
19  }
20  return;
21  }
22  reinterpret_cast<caffe2::ThreadPool*>(threadpool)
23  ->run(
24  [function, argument](int threadId, size_t workId) {
25  function(argument, workId);
26  },
27  range);
28 }
29 
30 size_t pthreadpool_get_threads_count(pthreadpool_t threadpool) {
31  return reinterpret_cast<caffe2::ThreadPool*>(threadpool)->getNumThreads();
32 }
33 
34 pthreadpool_t pthreadpool_create(size_t threads_count) {
35  std::mutex thread_pool_creation_mutex_;
36  std::lock_guard<std::mutex> guard(thread_pool_creation_mutex_);
37 
38  return reinterpret_cast<pthreadpool_t>(new caffe2::ThreadPool(threads_count));
39 }
40 
41 void pthreadpool_destroy(pthreadpool_t pthreadpool) {
42  if (pthreadpool) {
43  caffe2::ThreadPool* threadpool =
44  reinterpret_cast<caffe2::ThreadPool*>(pthreadpool);
45  delete threadpool;
46  }
47 }