forked from arrayfire/arrayfire
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmean.cpp
More file actions
161 lines (130 loc) · 4.91 KB
/
mean.cpp
File metadata and controls
161 lines (130 loc) · 4.91 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
/*******************************************************
* 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 <Array.hpp>
#include <common/half.hpp>
#include <kernel/mean.hpp>
#include <mean.hpp>
#include <platform.hpp>
#include <queue.hpp>
#include <types.hpp>
#include <af/dim4.hpp>
#include <complex>
using af::dim4;
using common::half;
namespace cpu {
template<typename Ti, typename Tw, typename To>
using mean_dim_func = std::function<void(
Param<To>, const dim_t, const CParam<Ti>, const dim_t, const int)>;
template<typename Ti, typename Tw, typename To>
Array<To> mean(const Array<Ti> &in, const int dim) {
dim4 odims = in.dims();
odims[dim] = 1;
Array<To> out = createEmptyArray<To>(odims);
static const mean_dim_func<Ti, Tw, To> mean_funcs[] = {
kernel::mean_dim<Ti, Tw, To, 1>(), kernel::mean_dim<Ti, Tw, To, 2>(),
kernel::mean_dim<Ti, Tw, To, 3>(), kernel::mean_dim<Ti, Tw, To, 4>()};
getQueue().enqueue(mean_funcs[in.ndims() - 1], out, 0, in, 0, dim);
return out;
}
template<typename T, typename Tw>
using mean_weighted_dim_func =
std::function<void(Param<T>, const dim_t, const CParam<T>, const dim_t,
const CParam<Tw>, const dim_t, const int)>;
template<typename T, typename Tw>
Array<T> mean(const Array<T> &in, const Array<Tw> &wt, const int dim) {
dim4 odims = in.dims();
odims[dim] = 1;
Array<T> out = createEmptyArray<T>(odims);
static const mean_weighted_dim_func<T, Tw> mean_funcs[] = {
kernel::mean_weighted_dim<T, Tw, 1>(),
kernel::mean_weighted_dim<T, Tw, 2>(),
kernel::mean_weighted_dim<T, Tw, 3>(),
kernel::mean_weighted_dim<T, Tw, 4>()};
getQueue().enqueue(mean_funcs[in.ndims() - 1], out, 0, in, 0, wt, 0, dim);
return out;
}
template<typename T, typename Tw>
T mean(const Array<T> &in, const Array<Tw> &wt) {
using MeanOpT = kernel::MeanOp<compute_t<T>, compute_t<T>, compute_t<Tw>>;
in.eval();
wt.eval();
getQueue().sync();
af::dim4 dims = in.dims();
af::dim4 strides = in.strides();
const T *inPtr = in.get();
const Tw *wtPtr = wt.get();
auto input = compute_t<T>(inPtr[0]);
auto weight = compute_t<Tw>(wtPtr[0]);
MeanOpT Op(input, weight);
for (dim_t l = 0; l < dims[3]; l++) {
dim_t off3 = l * strides[3];
for (dim_t k = 0; k < dims[2]; k++) {
dim_t off2 = k * strides[2];
for (dim_t j = 0; j < dims[1]; j++) {
dim_t off1 = j * strides[1];
for (dim_t i = 0; i < dims[0]; i++) {
dim_t idx = i + off1 + off2 + off3;
Op(compute_t<T>(inPtr[idx]), compute_t<Tw>(wtPtr[idx]));
}
}
}
}
return T(Op.runningMean);
}
template<typename Ti, typename Tw, typename To>
To mean(const Array<Ti> &in) {
using MeanOpT = kernel::MeanOp<compute_t<Ti>, compute_t<To>, compute_t<Tw>>;
in.eval();
getQueue().sync();
af::dim4 dims = in.dims();
af::dim4 strides = in.strides();
const Ti *inPtr = in.get();
MeanOpT Op(0, 0);
for (dim_t l = 0; l < dims[3]; l++) {
dim_t off3 = l * strides[3];
for (dim_t k = 0; k < dims[2]; k++) {
dim_t off2 = k * strides[2];
for (dim_t j = 0; j < dims[1]; j++) {
dim_t off1 = j * strides[1];
for (dim_t i = 0; i < dims[0]; i++) {
dim_t idx = i + off1 + off2 + off3;
Op(compute_t<Ti>(inPtr[idx]), 1);
}
}
}
}
return To(Op.runningMean);
}
#define INSTANTIATE(Ti, Tw, To) \
template To mean<Ti, Tw, To>(const Array<Ti> &in); \
template Array<To> mean<Ti, Tw, To>(const Array<Ti> &in, const int dim);
INSTANTIATE(double, double, double);
INSTANTIATE(float, float, float);
INSTANTIATE(int, float, float);
INSTANTIATE(unsigned, float, float);
INSTANTIATE(intl, double, double);
INSTANTIATE(uintl, double, double);
INSTANTIATE(short, float, float);
INSTANTIATE(ushort, float, float);
INSTANTIATE(uchar, float, float);
INSTANTIATE(char, float, float);
INSTANTIATE(cfloat, float, cfloat);
INSTANTIATE(cdouble, double, cdouble);
INSTANTIATE(half, float, half);
INSTANTIATE(half, float, float);
#define INSTANTIATE_WGT(T, Tw) \
template T mean<T, Tw>(const Array<T> &in, const Array<Tw> &wts); \
template Array<T> mean<T, Tw>(const Array<T> &in, const Array<Tw> &wts, \
const int dim);
INSTANTIATE_WGT(double, double);
INSTANTIATE_WGT(float, float);
INSTANTIATE_WGT(cfloat, float);
INSTANTIATE_WGT(cdouble, double);
INSTANTIATE_WGT(half, float);
} // namespace cpu