FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

FEAT: filter in convolutions is cast to the accum type · arrayfire/arrayfire@b1e87b3 · GitHub

Repository navigation

Commit b1e87b3

Browse files
committed
FEAT: filter in convolutions is cast to the accum type
1 parent cd56e3c commit b1e87b3

18 files changed

Lines changed: 146 additions & 215 deletions

‎src/api/c/convolve.cpp‎

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,15 @@ using namespace detail;
2323
template<typename T, typename accT, dim_type baseDim, bool expand>
2424
inline static af_array convolve(const af_array &s, const af_array &f, ConvolveBatchKind kind)
2525
{
26-
return getHandle(convolve<T, accT, baseDim, expand>(getArray<T>(s), getArray<T>(f), kind));
26+
return getHandle(convolve<T, accT, baseDim, expand>(getArray<T>(s), castArray<accT>(f), kind));
2727
}
2828

2929
template<typename T, typename accT, bool expand>
3030
inline static af_array convolve2(const af_array &s, const af_array &c_f, const af_array &r_f)
3131
{
32-
return getHandle(convolve2<T, accT, expand>(getArray<T>(s), getArray<T>(c_f), getArray<T>(r_f)));
32+
return getHandle(convolve2<T, accT, expand>(getArray<T>(s),
33+
castArray<accT>(c_f),
34+
castArray<accT>(r_f)));
3335
}
3436

3537
template<dim_type baseDim>
@@ -54,9 +56,6 @@ af_err convolve(af_array *out, af_array signal, af_array filter)
5456
ArrayInfo fInfo = getInfo(filter);
5557

5658
af_dtype stype = sInfo.getType();
57-
af_dtype ftype = fInfo.getType();
58-
59-
ARG_ASSERT(1, (stype==ftype));
6059

6160
dim4 sdims = sInfo.dims();
6261
dim4 fdims = fInfo.dims();
@@ -95,11 +94,6 @@ af_err convolve2_sep(af_array *out, af_array col_filter, af_array row_filter, af
9594
ArrayInfo rfInfo= getInfo(row_filter);
9695

9796
af_dtype signalType = sInfo.getType();
98-
af_dtype colFiltType = cfInfo.getType();
99-
af_dtype rowFiltType = rfInfo.getType();
100-
101-
ARG_ASSERT(1, (signalType==colFiltType));
102-
ARG_ASSERT(2, (colFiltType==rowFiltType));
10397

10498
dim4 signalDims = sInfo.dims();
10599

‎src/backend/cpu/convolve.cpp‎

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -20,25 +20,25 @@ using af::dim4;
2020
namespace cpu
2121
{
2222

23-
template<typename T, typename accType, bool expand>
24-
void one2one_1d(T *optr, T const *iptr, T const *fptr, dim4 const &oDims,
23+
template<typename T, typename accT, bool expand>
24+
void one2one_1d(T *optr, T const *iptr, accT const *fptr, dim4 const &oDims,
2525
dim4 const &sDims, dim4 const &fDims, dim4 const &sStrides)
2626
{
2727
dim_type start = (expand ? 0 : fDims[0]/2);
2828
dim_type end = (expand ? oDims[0] : start + sDims[0]);
2929
for(dim_type i=start; i<end; ++i) {
30-
accType accum = 0.0;
30+
accT accum = 0.0;
3131
for(dim_type f=0; f<fDims[0]; ++f) {
3232
dim_type iIdx = i-f;
3333
T s_val = ((iIdx>=0 &&iIdx<sDims[0])? iptr[iIdx*sStrides[0]] : T(0));
34-
accum += accType(s_val * fptr[f]);
34+
accum += accT(s_val * fptr[f]);
3535
}
3636
optr[i-start] = T(accum);
3737
}
3838
}
3939

40-
template<typename T, typename accType, bool expand>
41-
void one2one_2d(T *optr, T const *iptr, T const *fptr, dim4 const &oDims,
40+
template<typename T, typename accT, bool expand>
41+
void one2one_2d(T *optr, T const *iptr, accT const *fptr, dim4 const &oDims,
4242
dim4 const &sDims, dim4 const &fDims, dim4 const &oStrides,
4343
dim4 const &sStrides, dim4 const &fStrides)
4444
{
@@ -52,7 +52,7 @@ void one2one_2d(T *optr, T const *iptr, T const *fptr, dim4 const &oDims,
5252

5353
for(dim_type i=iStart; i<iEnd; ++i) {
5454

55-
accType accum = accType(0);
55+
accT accum = accT(0);
5656
for(dim_type wj=0; wj<fDims[1]; ++wj) {
5757
dim_type jIdx = j-wj;
5858
dim_type w_joff = wj*fStrides[1];
@@ -67,16 +67,16 @@ void one2one_2d(T *optr, T const *iptr, T const *fptr, dim4 const &oDims,
6767
s_val = iptr[s_joff+iIdx*sStrides[0]];
6868
}
6969

70-
accum += accType(s_val * fptr[w_joff+wi*fStrides[0]]);
70+
accum += accT(s_val * fptr[w_joff+wi*fStrides[0]]);
7171
}
7272
}
7373
optr[joff+i-iStart] = T(accum);
7474
}
7575
}
7676
}
7777

78-
template<typename T, typename accType, bool expand>
79-
void one2one_3d(T *optr, T const *iptr, T const *fptr, dim4 const &oDims,
78+
template<typename T, typename accT, bool expand>
79+
void one2one_3d(T *optr, T const *iptr, accT const *fptr, dim4 const &oDims,
8080
dim4 const &sDims, dim4 const &fDims, dim4 const &oStrides,
8181
dim4 const &sStrides, dim4 const &fStrides)
8282
{
@@ -95,7 +95,7 @@ void one2one_3d(T *optr, T const *iptr, T const *fptr, dim4 const &oDims,
9595

9696
for(dim_type i=iStart; i<iEnd; ++i) {
9797

98-
accType accum = accType(0);
98+
accT accum = accT(0);
9999
for(dim_type wk=0; wk<fDims[2]; ++wk) {
100100
dim_type kIdx = k-wk;
101101
dim_type w_koff = wk*fStrides[2];
@@ -116,7 +116,7 @@ void one2one_3d(T *optr, T const *iptr, T const *fptr, dim4 const &oDims,
116116
s_val = iptr[s_koff+s_joff+iIdx*sStrides[0]];
117117
}
118118

119-
accum += accType(s_val * fptr[w_koff+w_joff+wi*fStrides[0]]);
119+
accum += accT(s_val * fptr[w_koff+w_joff+wi*fStrides[0]]);
120120
}
121121
}
122122
}
@@ -126,15 +126,15 @@ void one2one_3d(T *optr, T const *iptr, T const *fptr, dim4 const &oDims,
126126
} // k loop ends here
127127
}
128128

129-
template<typename T, typename accType, dim_type baseDim, bool expand>
130-
void convolve_nd(T *optr, T const *iptr, T const *fptr,
129+
template<typename T, typename accT, dim_type baseDim, bool expand>
130+
void convolve_nd(T *optr, T const *iptr, accT const *fptr,
131131
dim4 const &oDims, dim4 const &sDims, dim4 const &fDims,
132132
dim4 const &oStrides, dim4 const &sStrides, dim4 const &fStrides,
133133
ConvolveBatchKind kind)
134134
{
135135
T * out = optr;
136136
T const *in = iptr;
137-
T const *filt = fptr;
137+
accT const *filt = fptr;
138138

139139
dim_type out_step = 0, in_step = 0, filt_step = 0;
140140

@@ -161,9 +161,9 @@ void convolve_nd(T *optr, T const *iptr, T const *fptr,
161161

162162
for(dim_type b=0; b<bCount; ++b) {
163163
switch(baseDim) {
164-
case 1: one2one_1d<T, accType, expand>(out, in, filt, oDims, sDims, fDims, sStrides); break;
165-
case 2: one2one_2d<T, accType, expand>(out, in, filt, oDims, sDims, fDims, oStrides, sStrides, fStrides); break;
166-
case 3: one2one_3d<T, accType, expand>(out, in, filt, oDims, sDims, fDims, oStrides, sStrides, fStrides); break;
164+
case 1: one2one_1d<T, accT, expand>(out, in, filt, oDims, sDims, fDims, sStrides); break;
165+
case 2: one2one_2d<T, accT, expand>(out, in, filt, oDims, sDims, fDims, oStrides, sStrides, fStrides); break;
166+
case 3: one2one_3d<T, accT, expand>(out, in, filt, oDims, sDims, fDims, oStrides, sStrides, fStrides); break;
167167
}
168168
out += out_step;
169169
in += in_step;
@@ -172,7 +172,7 @@ void convolve_nd(T *optr, T const *iptr, T const *fptr,
172172
}
173173

174174
template<typename T, typename accT, dim_type baseDim, bool expand>
175-
Array<T> convolve(Array<T> const& signal, Array<T> const& filter, ConvolveBatchKind kind)
175+
Array<T> convolve(Array<T> const& signal, Array<accT> const& filter, ConvolveBatchKind kind)
176176
{
177177
auto sDims = signal.dims();
178178
auto fDims = filter.dims();
@@ -201,8 +201,8 @@ Array<T> convolve(Array<T> const& signal, Array<T> const& filter, ConvolveBatchK
201201
return out;
202202
}
203203

204-
template<typename T, typename accType, dim_type conv_dim, bool expand>
205-
void convolve2_separable(T *optr, T const *iptr, T const *fptr,
204+
template<typename T, typename accT, dim_type conv_dim, bool expand>
205+
void convolve2_separable(T *optr, T const *iptr, accT const *fptr,
206206
dim4 const &oDims, dim4 const &sDims, dim4 const &orgDims, dim_type fDim,
207207
dim4 const &oStrides, dim4 const &sStrides, dim_type fStride)
208208
{
@@ -216,7 +216,7 @@ void convolve2_separable(T *optr, T const *iptr, T const *fptr,
216216
dim_type iOff = i*oStrides[0];
217217
dim_type ci = i + (conv_dim==0)*(expand ? 0 : fDim>>1);
218218

219-
accType accum = scalar<accType>(0);
219+
accT accum = scalar<accT>(0);
220220

221221
for(dim_type f=0; f<fDim; ++f) {
222222
T f_val = fptr[f];
@@ -234,15 +234,15 @@ void convolve2_separable(T *optr, T const *iptr, T const *fptr,
234234
s_val = (isCJValid && isCIValid ? iptr[offj*sDims[0]+ci] : scalar<T>(0));
235235
}
236236

237-
accum += accType(s_val * f_val);
237+
accum += accT(s_val * f_val);
238238
}
239239
optr[iOff+jOff] = T(accum);
240240
}
241241
}
242242
}
243243

244244
template<typename T, typename accT, bool expand>
245-
Array<T> convolve2(Array<T> const& signal, Array<T> const& c_filter, Array<T> const& r_filter)
245+
Array<T> convolve2(Array<T> const& signal, Array<accT> const& c_filter, Array<accT> const& r_filter)
246246
{
247247
auto sDims = signal.dims();
248248
auto cfDims = c_filter.dims();
@@ -284,15 +284,15 @@ Array<T> convolve2(Array<T> const& signal, Array<T> const& c_filter, Array<T> co
284284
return out;
285285
}
286286

287-
#define INSTANTIATE(T, accT) \
288-
template Array<T> convolve <T, accT, 1, true >(Array<T> const& signal, Array<T> const& filter, ConvolveBatchKind kind); \
289-
template Array<T> convolve <T, accT, 1, false>(Array<T> const& signal, Array<T> const& filter, ConvolveBatchKind kind); \
290-
template Array<T> convolve <T, accT, 2, true >(Array<T> const& signal, Array<T> const& filter, ConvolveBatchKind kind); \
291-
template Array<T> convolve <T, accT, 2, false>(Array<T> const& signal, Array<T> const& filter, ConvolveBatchKind kind); \
292-
template Array<T> convolve <T, accT, 3, true >(Array<T> const& signal, Array<T> const& filter, ConvolveBatchKind kind); \
293-
template Array<T> convolve <T, accT, 3, false>(Array<T> const& signal, Array<T> const& filter, ConvolveBatchKind kind); \
294-
template Array<T> convolve2<T, accT, true >(Array<T> const& signal, Array<T> const& c_filter, Array<T> const& r_filter); \
295-
template Array<T> convolve2<T, accT, false>(Array<T> const& signal, Array<T> const& c_filter, Array<T> const& r_filter);
287+
#define INSTANTIATE(T, accT) \
288+
template Array<T> convolve <T, accT, 1, true >(Array<T> const& signal, Array<accT> const& filter, ConvolveBatchKind kind); \
289+
template Array<T> convolve <T, accT, 1, false>(Array<T> const& signal, Array<accT> const& filter, ConvolveBatchKind kind); \
290+
template Array<T> convolve <T, accT, 2, true >(Array<T> const& signal, Array<accT> const& filter, ConvolveBatchKind kind); \
291+
template Array<T> convolve <T, accT, 2, false>(Array<T> const& signal, Array<accT> const& filter, ConvolveBatchKind kind); \
292+
template Array<T> convolve <T, accT, 3, true >(Array<T> const& signal, Array<accT> const& filter, ConvolveBatchKind kind); \
293+
template Array<T> convolve <T, accT, 3, false>(Array<T> const& signal, Array<accT> const& filter, ConvolveBatchKind kind); \
294+
template Array<T> convolve2<T, accT, true >(Array<T> const& signal, Array<accT> const& c_filter, Array<accT> const& r_filter); \
295+
template Array<T> convolve2<T, accT, false>(Array<T> const& signal, Array<accT> const& c_filter, Array<accT> const& r_filter);
296296

297297
INSTANTIATE(cdouble, cdouble)
298298
INSTANTIATE(cfloat , cfloat)

‎src/backend/cpu/convolve.hpp‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ namespace cpu
1414
{
1515

1616
template<typename T, typename accT, dim_type baseDim, bool expand>
17-
Array<T> convolve(Array<T> const& signal, Array<T> const& filter, ConvolveBatchKind kind);
17+
Array<T> convolve(Array<T> const& signal, Array<accT> const& filter, ConvolveBatchKind kind);
1818

1919
template<typename T, typename accT, bool expand>
20-
Array<T> convolve2(Array<T> const& signal, Array<T> const& c_filter, Array<T> const& r_filter);
20+
Array<T> convolve2(Array<T> const& signal, Array<accT> const& c_filter, Array<accT> const& r_filter);
2121

2222
}

‎src/backend/cuda/Array.hpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@
66
* The complete license agreement can be obtained at:
77
* http://arrayfire.com/licenses/BSD-3-Clause
88
********************************************************/
9+
#pragma once
910

1011
// Workaround for BOOST_NOINLINE not being defined with nvcc / CUDA < 6.5
1112
#if CUDA_VERSION < 6050
1213
#define BOOST_NOINLINE __attribute__ ((noinline))
1314
#endif
1415

15-
#pragma once
1616
#include <af/array.h>
1717
#include <af/dim4.hpp>
1818
#include <ArrayInfo.hpp>

‎src/backend/cuda/convolve.cpp‎

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ namespace cuda
2121
{
2222

2323
template<typename T, typename accT, dim_type baseDim, bool expand>
24-
Array<T> convolve(Array<T> const& signal, Array<T> const& filter, ConvolveBatchKind kind)
24+
Array<T> convolve(Array<T> const& signal, Array<accT> const& filter, ConvolveBatchKind kind)
2525
{
2626
const dim4 sDims = signal.dims();
2727
const dim4 fDims = filter.dims();
@@ -48,7 +48,7 @@ Array<T> convolve(Array<T> const& signal, Array<T> const& filter, ConvolveBatchK
4848
}
4949

5050
template<typename T, typename accT, bool expand>
51-
Array<T> convolve2(Array<T> const& signal, Array<T> const& c_filter, Array<T> const& r_filter)
51+
Array<T> convolve2(Array<T> const& signal, Array<accT> const& c_filter, Array<accT> const& r_filter)
5252
{
5353
const dim4 cfDims = c_filter.dims();
5454
const dim4 rfDims = r_filter.dims();
@@ -75,15 +75,15 @@ Array<T> convolve2(Array<T> const& signal, Array<T> const& c_filter, Array<T> co
7575
return out;
7676
}
7777

78-
#define INSTANTIATE(T, accT) \
79-
template Array<T> convolve <T, accT, 1, true >(Array<T> const& signal, Array<T> const& filter, ConvolveBatchKind kind); \
80-
template Array<T> convolve <T, accT, 1, false>(Array<T> const& signal, Array<T> const& filter, ConvolveBatchKind kind); \
81-
template Array<T> convolve <T, accT, 2, true >(Array<T> const& signal, Array<T> const& filter, ConvolveBatchKind kind); \
82-
template Array<T> convolve <T, accT, 2, false>(Array<T> const& signal, Array<T> const& filter, ConvolveBatchKind kind); \
83-
template Array<T> convolve <T, accT, 3, true >(Array<T> const& signal, Array<T> const& filter, ConvolveBatchKind kind); \
84-
template Array<T> convolve <T, accT, 3, false>(Array<T> const& signal, Array<T> const& filter, ConvolveBatchKind kind); \
85-
template Array<T> convolve2<T, accT, true >(Array<T> const& signal, Array<T> const& c_filter, Array<T> const& r_filter); \
86-
template Array<T> convolve2<T, accT, false>(Array<T> const& signal, Array<T> const& c_filter, Array<T> const& r_filter);
78+
#define INSTANTIATE(T, accT) \
79+
template Array<T> convolve <T, accT, 1, true >(Array<T> const& signal, Array<accT> const& filter, ConvolveBatchKind kind); \
80+
template Array<T> convolve <T, accT, 1, false>(Array<T> const& signal, Array<accT> const& filter, ConvolveBatchKind kind); \
81+
template Array<T> convolve <T, accT, 2, true >(Array<T> const& signal, Array<accT> const& filter, ConvolveBatchKind kind); \
82+
template Array<T> convolve <T, accT, 2, false>(Array<T> const& signal, Array<accT> const& filter, ConvolveBatchKind kind); \
83+
template Array<T> convolve <T, accT, 3, true >(Array<T> const& signal, Array<accT> const& filter, ConvolveBatchKind kind); \
84+
template Array<T> convolve <T, accT, 3, false>(Array<T> const& signal, Array<accT> const& filter, ConvolveBatchKind kind); \
85+
template Array<T> convolve2<T, accT, true >(Array<T> const& signal, Array<accT> const& c_filter, Array<accT> const& r_filter); \
86+
template Array<T> convolve2<T, accT, false>(Array<T> const& signal, Array<accT> const& c_filter, Array<accT> const& r_filter);
8787

8888
INSTANTIATE(cdouble, cdouble)
8989
INSTANTIATE(cfloat , cfloat)

‎src/backend/cuda/convolve.hpp‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ namespace cuda
1414
{
1515

1616
template<typename T, typename accT, dim_type baseDim, bool expand>
17-
Array<T> convolve(Array<T> const& signal, Array<T> const& filter, ConvolveBatchKind kind);
17+
Array<T> convolve(Array<T> const& signal, Array<accT> const& filter, ConvolveBatchKind kind);
1818

1919
template<typename T, typename accT, bool expand>
20-
Array<T> convolve2(Array<T> const& signal, Array<T> const& c_filter, Array<T> const& r_filter);
20+
Array<T> convolve2(Array<T> const& signal, Array<accT> const& c_filter, Array<accT> const& r_filter);
2121

2222
}

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL