-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathiarray_random.c
More file actions
221 lines (187 loc) · 6.86 KB
/
iarray_random.c
File metadata and controls
221 lines (187 loc) · 6.86 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/*
* Copyright ironArray SL 2021.
*
* All rights reserved.
*
* This software is the confidential and proprietary information of ironArray SL
* ("Confidential Information"). You shall not disclose such Confidential
* Information and shall use it only in accordance with the terms of the license agreement.
*
*/
#include <libiarray/iarray.h>
#include "iarray_constructor.h"
#include <mkl_vsl.h>
INA_API(ina_rc_t) iarray_random_ctx_new(iarray_context_t *ctx,
uint32_t seed,
iarray_random_rng_t rng,
iarray_random_ctx_t **rng_ctx)
{
INA_VERIFY_NOT_NULL(ctx);
INA_VERIFY_NOT_NULL(rng_ctx);
*rng_ctx = (iarray_random_ctx_t*)ina_mem_alloc(sizeof(iarray_random_ctx_t));
(*rng_ctx)->seed = seed;
(*rng_ctx)->rng = rng;
int mkl_rng;
switch (rng) {
case IARRAY_RANDOM_RNG_MRG32K3A:
mkl_rng = VSL_BRNG_MRG32K3A;
break;
default:
IARRAY_TRACE1(iarray.error, "Random generator not supported");
return IARRAY_ERR_INVALID_RNG_METHOD;
}
vslNewStream(&(*rng_ctx)->stream, mkl_rng, seed);
ina_mem_set((*rng_ctx)->params, 0, sizeof(double) * (IARRAY_RANDOM_DIST_PARAM_SENTINEL));
return INA_SUCCESS;
}
INA_API(void) iarray_random_ctx_free(iarray_context_t *ctx, iarray_random_ctx_t **rng_ctx)
{
INA_VERIFY_FREE(rng_ctx);
INA_UNUSED(ctx);
vslDeleteStream(&((*rng_ctx)->stream));
INA_MEM_FREE_SAFE(*rng_ctx);
}
INA_API(ina_rc_t) iarray_random_dist_set_param(iarray_random_ctx_t *ctx,
iarray_random_dist_parameter_t key,
double value)
{
INA_VERIFY_NOT_NULL(ctx);
ctx->params[key] = value;
return INA_SUCCESS;
}
INA_API(ina_rc_t) iarray_random_kstest(iarray_context_t *ctx,
iarray_container_t *container1,
iarray_container_t *container2,
bool *res)
{
if (container1->catarr->nitems != container2->catarr->nitems) {
return INA_ERROR(IARRAY_ERR_INVALID_SHAPE);
}
int64_t size = container1->catarr->nitems;
int nbins = 100;
double bins[100];
double hist1[100];
double hist2[100];
double max = -INFINITY;
double min = INFINITY;
iarray_iter_read_t *iter;
iarray_iter_read_value_t val;
IARRAY_RETURN_IF_FAILED(iarray_iter_read_new(ctx, &iter, container1, &val));
while (INA_SUCCEED(iarray_iter_read_has_next(iter))) {
IARRAY_RETURN_IF_FAILED(iarray_iter_read_next(iter));
double data;
switch(container1->dtshape->dtype){
case IARRAY_DATA_TYPE_DOUBLE:
data = ((double *) val.elem_pointer)[0];
break;
case IARRAY_DATA_TYPE_FLOAT:
data = ((float *) val.elem_pointer)[0];
break;
case IARRAY_DATA_TYPE_INT32:
data = ((int32_t *) val.elem_pointer)[0];
break;
default:
IARRAY_TRACE1(iarray.error, "The data type is invalid");
return (INA_ERROR(IARRAY_ERR_INVALID_DTYPE));
}
max = (data > max) ? data : max;
min = (data < min) ? data : min;
}
IARRAY_ITER_FINISH();
iarray_iter_read_free(&iter);
IARRAY_RETURN_IF_FAILED(iarray_iter_read_new(ctx, &iter, container2, &val));
while (INA_SUCCEED(iarray_iter_read_has_next(iter))) {
IARRAY_RETURN_IF_FAILED(iarray_iter_read_next(iter));
double data;
switch(container1->dtshape->dtype){
case IARRAY_DATA_TYPE_DOUBLE:
data = ((double *) val.elem_pointer)[0];
break;
case IARRAY_DATA_TYPE_FLOAT:
data = ((float *) val.elem_pointer)[0];
break;
case IARRAY_DATA_TYPE_INT32:
data = ((int32_t *) val.elem_pointer)[0];
break;
default:
IARRAY_TRACE1(iarray.error, "The data type is invalid");
return (INA_ERROR(IARRAY_ERR_INVALID_DTYPE));
}
max = (data > max) ? data : max;
min = (data < min) ? data : min;
}
iarray_iter_read_free(&iter);
IARRAY_ITER_FINISH();
for (int i = 0; i < nbins; ++i) {
bins[i] = min + (max-min)/nbins * (i+1);
hist1[i] = 0;
hist2[i] = 0;
}
IARRAY_RETURN_IF_FAILED(iarray_iter_read_new(ctx, &iter, container1, &val));
while (INA_SUCCEED(iarray_iter_read_has_next(iter))) {
IARRAY_RETURN_IF_FAILED(iarray_iter_read_next(iter));
double data;
switch(container1->dtshape->dtype){
case IARRAY_DATA_TYPE_DOUBLE:
data = ((double *) val.elem_pointer)[0];
break;
case IARRAY_DATA_TYPE_FLOAT:
data = ((float *) val.elem_pointer)[0];
break;
case IARRAY_DATA_TYPE_INT32:
data = ((int32_t *) val.elem_pointer)[0];
break;
default:
IARRAY_TRACE1(iarray.error, "The data type is invalid");
return (INA_ERROR(IARRAY_ERR_INVALID_DTYPE));
}
for (int i = 0; i < nbins; ++i) {
if (data <= bins[i]) {
hist1[i] += 1;
break;
}
}
}
iarray_iter_read_free(&iter);
IARRAY_ITER_FINISH();
IARRAY_RETURN_IF_FAILED(iarray_iter_read_new(ctx, &iter, container2, &val));
while (INA_SUCCEED(iarray_iter_read_has_next(iter))) {
IARRAY_RETURN_IF_FAILED(iarray_iter_read_next(iter));
double data;
switch(container1->dtshape->dtype){
case IARRAY_DATA_TYPE_DOUBLE:
data = ((double *) val.elem_pointer)[0];
break;
case IARRAY_DATA_TYPE_FLOAT:
data = ((float *) val.elem_pointer)[0];
break;
case IARRAY_DATA_TYPE_INT32:
data = ((int32_t *) val.elem_pointer)[0];
break;
default:
IARRAY_TRACE1(iarray.error, "The data type is invalid");
return (INA_ERROR(IARRAY_ERR_INVALID_DTYPE));
}
for (int i = 0; i < nbins; ++i) {
if (data <= bins[i]) {
hist2[i] += 1;
break;
}
}
}
iarray_iter_read_free(&iter);
IARRAY_ITER_FINISH();
for (int i = 1; i < nbins; ++i) {
hist1[i] += hist1[i-1];
hist2[i] += hist2[i-1];
}
double max_dif = -INFINITY;
for (int i = 0; i < nbins; ++i) {
max_dif = (fabs(hist1[i] - hist2[i]) / (double) size > max_dif) ? fabs(hist1[i] - hist2[i]) / (double) size : max_dif;
}
// The smaller the a, the less strict the threshold is
double a = 0.0001;
double threshold = sqrt(- log(a) / 2) * sqrt(2 * ((double) size) / (double) (size * size));
*res = (max_dif < threshold);
return INA_SUCCESS;
}