pytorch/caffe2/utils/threadpool/pthreadpool_impl.cc
Hao Lu 58a7f2aed1 Add pthreadpool_create and pthreadpool_destroy (#15492)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/15492

Add pthreadpool_create and pthreadpool_destroy, which are used by NNPACK tests.

Reviewed By: Maratyszcza

Differential Revision: D13540997

fbshipit-source-id: 628c599df87b552ca1a3703854ec170243f04d2e
2018-12-21 20:28:18 -08:00

47 lines
1.2 KiB
C++

#include "caffe2/utils/threadpool/pthreadpool.h"
#include "caffe2/utils/threadpool/ThreadPool.h"
//
// External API
//
void pthreadpool_compute_1d(
pthreadpool_t threadpool,
pthreadpool_function_1d_t function,
void* argument,
size_t range) {
if (threadpool == nullptr) {
/* No thread pool provided: execute function sequentially on the calling
* thread */
for (size_t i = 0; i < range; i++) {
function(argument, i);
}
return;
}
reinterpret_cast<caffe2::ThreadPool*>(threadpool)
->run(
[function, argument](int threadId, size_t workId) {
function(argument, workId);
},
range);
}
size_t pthreadpool_get_threads_count(pthreadpool_t threadpool) {
return reinterpret_cast<caffe2::ThreadPool*>(threadpool)->getNumThreads();
}
pthreadpool_t pthreadpool_create(size_t threads_count) {
std::mutex thread_pool_creation_mutex_;
std::lock_guard<std::mutex> guard(thread_pool_creation_mutex_);
return reinterpret_cast<pthreadpool_t>(new caffe2::ThreadPool(threads_count));
}
void pthreadpool_destroy(pthreadpool_t pthreadpool) {
if (pthreadpool) {
caffe2::ThreadPool* threadpool =
reinterpret_cast<caffe2::ThreadPool*>(pthreadpool);
delete threadpool;
}
}