forked from daphne-project/daphne
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSample.h
More file actions
145 lines (126 loc) · 5.77 KB
/
Copy pathSample.h
File metadata and controls
145 lines (126 loc) · 5.77 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
* Copyright 2021 The DAPHNE Consortium
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef SRC_RUNTIME_LOCAL_KERNELS_SAMPLEOP_H
#define SRC_RUNTIME_LOCAL_KERNELS_SAMPLEOP_H
#include <runtime/local/context/DaphneContext.h>
#include <runtime/local/datastructures/DataObjectFactory.h>
#include <runtime/local/datastructures/DenseMatrix.h>
#include <algorithm>
#include <random>
#include <set>
#include <stdexcept>
#include <type_traits>
#include <cmath>
#include <cstddef>
#include <cstdint>
#include <chrono>
#include <unordered_set>
// ****************************************************************************
// Struct for partial template specialization
// ****************************************************************************
template<class DTRes, typename VTArg>
struct Sample {
static void apply(DTRes *& res, VTArg range, size_t size, bool withReplacement, int64_t seed, DCTX(ctx)) = delete;
};
// ****************************************************************************
// Convenience function
// ****************************************************************************
template<class DTRes, typename VTArg>
void sample(DTRes *& res, VTArg range, size_t size, bool withReplacement, int64_t seed, DCTX(ctx)) {
Sample<DTRes, VTArg>::apply(res, range, size, withReplacement, seed, ctx);
}
// ****************************************************************************
// (Partial) template specializations for different data/value types
// ****************************************************************************
// ----------------------------------------------------------------------------
// DenseMatrix
// ----------------------------------------------------------------------------
template<typename VT>
struct Sample<DenseMatrix<VT>, VT> {
static void apply(DenseMatrix<VT> *& res, VT range, int64_t size, bool withReplacement, int64_t seed, DCTX(ctx)) {
if (size <= 0)
throw std::runtime_error("size (rows) must be > 0");
if (range <= 0)
throw std::runtime_error("range must be > 0");
if (!withReplacement && !std::is_floating_point<VT>::value &&
range < static_cast<VT>(size)) {
throw std::runtime_error("if no duplicates are allowed, "
"then must be range >= size");
}
if(res == nullptr)
res = DataObjectFactory::create<DenseMatrix<VT>>(size, 1, false);
if (seed == -1) {
std::random_device rd;
std::uniform_int_distribution<int64_t> seedRnd;
seed = seedRnd(rd);
}
std::mt19937 genVal(seed);
if (!std::is_floating_point<VT>::value && !std::is_integral<VT>::value)
throw std::runtime_error(
"the value type must be either floating point or integral");
// TODO For std::uniform_real_distribution, the upper bound is not
// included in the interval of possible values, so when VT is a
// floating-point type, std::nextafter() is not required. However, we
// don't lose much by that, so it is fine for now.
typename std::conditional<
std::is_floating_point<VT>::value,
std::uniform_real_distribution<VT>,
std::uniform_int_distribution<VT>>::type distrVal(0, std::nextafter(range, 0));
if (withReplacement) {
VT *valuesRes = res->getValues();
for (int64_t c = 0; c < size; c++)
{
valuesRes[c] = distrVal(genVal);
}
}
else {
// If range is `double` we can simply store each number we
// generate and check if it already exists each time (doubles
// are rarely duplicate).
if (std::is_floating_point<VT>::value){
std::unordered_set<VT> contained;
VT *valuesRes = res->getValues();
for (int64_t c = 0; c < size; c++)
{
VT generatedValue = distrVal(genVal);
while (contained.find(generatedValue) != contained.end()){
generatedValue = distrVal(genVal);
}
valuesRes[c] = generatedValue;
contained.insert(generatedValue);
}
}
// Else if range is `int` the above method does not work efficiently.
// Ex. size = range, finding the correct number is increasingly
// harder as we fill the array. We must implement an efficient algorithm
// to create non-duplicate numbers (see Knuth's algorithm).
else {
VT *valuesRes = res->getValues();
VT iRange;
int64_t iSize;
iSize = 0;
for (iRange = 0; iRange < range && iSize < size; iRange++) {
size_t rRange = range - iRange;
size_t rSize = size - iSize;
if (fmod(distrVal(genVal), rRange) < rSize)
valuesRes[iSize++] = iRange;
}
std::shuffle(valuesRes, valuesRes + size, genVal);
}
}
}
};
#endif //SRC_RUNTIME_LOCAL_KERNELS_SAMPLEOP_H