23 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
26 static CppadParallel& getInstance()
28 static CppadParallel instance;
32 static void initParallel(
size_t numThreads) { CppadParallel::getInstance().initParallelImpl(numThreads); }
33 static void resetParallel() { CppadParallel::getInstance().resetParallelImpl(); }
42 void initParallelImpl(
size_t numThreads)
45 numThreads_ = numThreads;
47 CppAD::thread_alloc::parallel_setup(numThreads_, in_parallel, thread_number);
49 CppAD::thread_alloc::hold_memory(
true);
50 CppAD::parallel_ad<double>();
51 isSequential_ =
false;
54 void resetParallelImpl()
58 CppAD::thread_alloc::parallel_setup(1, CPPAD_NULL, CPPAD_NULL);
59 CppAD::thread_alloc::hold_memory(
false);
60 CppAD::parallel_ad<double>();
64 CppadParallel() : isSequential_(false), numThreads_(100) {}
66 std::map<size_t, size_t> threadMap_;
68 std::mutex hashMutex_;
76 static bool in_parallel(
void) {
return !(CppadParallel::getInstance().isSequential_); }
83 static size_t thread_number(
void)
85 std::thread::id this_id = std::this_thread::get_id();
86 std::hash<std::thread::id> hasher;
87 size_t hashId = hasher(this_id);
89 CppadParallel& instance = CppadParallel::getInstance();
91 instance.hashMutex_.lock();
92 if (instance.threadMap_.size() < instance.numThreads_)
93 if (!instance.threadMap_.count(hashId))
94 instance.threadMap_.insert(std::make_pair(hashId, instance.threadMap_.size()));
96 size_t threadNum = instance.threadMap_[hashId];
97 instance.hashMutex_.unlock();
104 CppadParallel(CppadParallel
const&) =
delete;
105 void operator=(CppadParallel
const&) =
delete;