-
Notifications
You must be signed in to change notification settings - Fork 549
Expand file tree
/
Copy pathvar.cpp
More file actions
125 lines (110 loc) · 4.34 KB
/
var.cpp
File metadata and controls
125 lines (110 loc) · 4.34 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
/*******************************************************
* Copyright (c) 2014, ArrayFire
* All rights reserved.
*
* This file is distributed under 3-clause BSD license.
* The complete license agreement can be obtained at:
* http://arrayfire.com/licenses/BSD-3-Clause
********************************************************/
#include <af/array.h>
#include <af/dim4.hpp>
#include <af/statistics.h>
#include "common.hpp"
#include "error.hpp"
#include "half.hpp"
#ifdef AF_CUDA
#include <cuda_fp16.h>
#include <traits.hpp>
#endif
namespace af {
array var(const array& in, const bool isbiased, const dim_t dim) {
const af_var_bias bias =
(isbiased ? AF_VARIANCE_SAMPLE : AF_VARIANCE_POPULATION);
return var(in, bias, dim);
}
array var(const array& in, const af_var_bias bias, const dim_t dim) {
af_array temp = 0;
AF_THROW(af_var_v2(&temp, in.get(), bias, getFNSD(dim, in.dims())));
return array(temp);
}
array var(const array& in, const array& weights, const dim_t dim) {
af_array temp = 0;
AF_THROW(af_var_weighted(&temp, in.get(), weights.get(),
getFNSD(dim, in.dims())));
return array(temp);
}
#define INSTANTIATE_VAR(T) \
template<> \
AFAPI T var(const array& in, const af_var_bias bias) { \
double ret_val; \
AF_THROW(af_var_all_v2(&ret_val, NULL, in.get(), bias)); \
return cast<T>(ret_val); \
} \
template<> \
AFAPI T var(const array& in, const bool isbiased) { \
const af_var_bias bias = \
(isbiased ? AF_VARIANCE_SAMPLE : AF_VARIANCE_POPULATION); \
return var<T>(in, bias); \
} \
\
template<> \
AFAPI T var(const array& in, const array& weights) { \
double ret_val; \
AF_THROW( \
af_var_all_weighted(&ret_val, NULL, in.get(), weights.get())); \
return cast<T>(ret_val); \
}
template<>
AFAPI af_cfloat var(const array& in, const af_var_bias bias) {
double real, imag;
AF_THROW(af_var_all_v2(&real, &imag, in.get(), bias));
return {static_cast<float>(real), static_cast<float>(imag)};
}
template<>
AFAPI af_cdouble var(const array& in, const af_var_bias bias) {
double real, imag;
AF_THROW(af_var_all_v2(&real, &imag, in.get(), bias));
return {real, imag};
}
template<>
AFAPI af_cfloat var(const array& in, const bool isbiased) {
const af_var_bias bias =
(isbiased ? AF_VARIANCE_SAMPLE : AF_VARIANCE_POPULATION);
return var<af_cfloat>(in, bias);
}
template<>
AFAPI af_cdouble var(const array& in, const bool isbiased) {
const af_var_bias bias =
(isbiased ? AF_VARIANCE_SAMPLE : AF_VARIANCE_POPULATION);
return var<af_cdouble>(in, bias);
}
template<>
AFAPI af_cfloat var(const array& in, const array& weights) {
double real, imag;
AF_THROW(af_var_all_weighted(&real, &imag, in.get(), weights.get()));
return {static_cast<float>(real), static_cast<float>(imag)};
}
template<>
AFAPI af_cdouble var(const array& in, const array& weights) {
double real, imag;
AF_THROW(af_var_all_weighted(&real, &imag, in.get(), weights.get()));
return {real, imag};
}
INSTANTIATE_VAR(float);
INSTANTIATE_VAR(double);
INSTANTIATE_VAR(int);
INSTANTIATE_VAR(unsigned int);
INSTANTIATE_VAR(long long);
INSTANTIATE_VAR(unsigned long long);
INSTANTIATE_VAR(short);
INSTANTIATE_VAR(unsigned short);
INSTANTIATE_VAR(char);
INSTANTIATE_VAR(signed char);
INSTANTIATE_VAR(unsigned char);
INSTANTIATE_VAR(af_half);
INSTANTIATE_VAR(half_float::half);
#ifdef AF_CUDA
INSTANTIATE_VAR(__half);
#endif
#undef INSTANTIATE_VAR
} // namespace af