-
Notifications
You must be signed in to change notification settings - Fork 0
/
parallel_random.h
41 lines (34 loc) · 914 Bytes
/
parallel_random.h
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
#ifndef __PARALLEL_RANDOM_H
#define __PARALLEL_RANDOM_H
#include <iostream>
#include <omp.h>
#include <random>
#include "defs.h"
#include "parallel_array.h"
/**
* INTERFACE:
* RandomSequence(uint32_t size,
uint32_t NUM_THREADS) - constructs a random sequence of given size using NUM_THREADS
* uint32_t operator[i] const - ith number
*/
struct RandomSequence {
const u32 NUM_THREADS;
ParallelArray<u32> arr;
RandomSequence(u32 size, u32 NUM_THREADS = omp_get_max_threads()) : NUM_THREADS(NUM_THREADS), arr(size) {
#pragma omp parallel shared(arr)
{
std::mt19937 gen(std::random_device{}());
#pragma omp for
for (u32 i = 0; i < size; ++i) {
arr[i] = gen();
}
}
}
u32 size() const {
return arr.size();
}
u32 operator[](u32 id) {
return arr[id];
}
};
#endif