-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmkl_gaussian_parallel_generator.cpp
54 lines (44 loc) · 1.54 KB
/
mkl_gaussian_parallel_generator.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include "mkl_gaussian_parallel_generator.h"
#include <stdlib.h>
#include <stdexcept>
#include <omp.h>
MklGaussianParallelGenerator::MklGaussianParallelGenerator(double mean, double stDeviation, std::size_t bufferSize, unsigned threadNum)
:_mean{ mean }, _stDeviation{ stDeviation }, _bufferSize{bufferSize},_threadNum{threadNum}
{
///////////////// If reproducibility from launch to launch is required seed is const, eslse seed must be random
MKL_UINT seed = 777;
/////////////////
for (unsigned i = 0; i < threadNum; i++) {
_streamWrappers.emplace_back(VSL_BRNG_MT2203 + i, seed);
}
_nPerThread = _bufferSize / _threadNum;
if (_bufferSize != _nPerThread * _threadNum) {
throw std::logic_error{ "buffsize must be multiple of number of threads" };
}
_buffer.resize(_bufferSize);
}
void MklGaussianParallelGenerator::generateNumbers()
{
//// Strange that it works without shared! check this out.
#pragma omp parallel num_threads(_threadNum) default(none)
{
const std::size_t threadId=omp_get_thread_num();
const std::size_t begin = _nPerThread * threadId;
const auto generationResult = vdRngGaussian(
VSL_RNG_METHOD_GAUSSIAN_ICDF, _streamWrappers.at(threadId).get(),
_nPerThread, _buffer.data() + begin,
_mean, _stDeviation
);
if (generationResult != VSL_STATUS_OK) {
throw std::runtime_error{ "can't generate numbers" };
}
}
}
const double * MklGaussianParallelGenerator::getNumbersBuffer() const
{
return _buffer.data();
}
std::size_t MklGaussianParallelGenerator::getNumbersBufferSize() const
{
return _bufferSize;
}