Skip to content

Commit b0e4992

Browse files
committed
FEAT Add CPU backend for unwrap function
* Empty functions for CUDA and OpenCL
1 parent 17db1c9 commit b0e4992

File tree

9 files changed

+340
-0
lines changed

9 files changed

+340
-0
lines changed

include/af/image.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,8 @@ AFAPI array rgb2hsv(const array& in);
461461
*/
462462
AFAPI array colorSpace(const array& image, const CSpace to, const CSpace from);
463463

464+
AFAPI array unwrap(const array& in, const dim_t wx, const dim_t wy, const dim_t sx, const dim_t sy);
465+
464466
}
465467
#endif
466468

@@ -903,6 +905,8 @@ extern "C" {
903905
*/
904906
AFAPI af_err af_color_space(af_array *out, const af_array image, const af_cspace_t to, const af_cspace_t from);
905907

908+
AFAPI af_err af_unwrap(af_array *out, const af_array in, const dim_t wx, const dim_t wy, const dim_t sx, const dim_t sy);
909+
906910
#ifdef __cplusplus
907911
}
908912
#endif

src/api/c/unwrap.cpp

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*******************************************************
2+
* Copyright (c) 2014, ArrayFire
3+
* All rights reserved.
4+
*
5+
* This file is distributed under 3-clause BSD license.
6+
* The complete license agreement can be obtained at:
7+
* http://arrayfire.com/licenses/BSD-3-Clause
8+
********************************************************/
9+
10+
#include <af/image.h>
11+
#include <af/defines.h>
12+
#include <err_common.hpp>
13+
#include <handle.hpp>
14+
#include <backend.hpp>
15+
#include <ArrayInfo.hpp>
16+
#include <unwrap.hpp>
17+
18+
using af::dim4;
19+
using namespace detail;
20+
21+
template<typename T>
22+
static inline af_array unwrap(const af_array in, const dim_t wx, const dim_t wy,
23+
const dim_t sx, const dim_t sy)
24+
{
25+
return getHandle(unwrap<T>(getArray<T>(in), wx, wy, sx, sy));
26+
}
27+
28+
af_err af_unwrap(af_array *out, const af_array in,
29+
const dim_t wx, const dim_t wy, const dim_t sx, const dim_t sy)
30+
{
31+
try {
32+
ArrayInfo info = getInfo(in);
33+
af_dtype type = info.getType();
34+
af::dim4 idims = info.dims();
35+
36+
DIM_ASSERT(1, idims[0] >= wx && idims[1] >= wy);
37+
ARG_ASSERT(4, sx > 0);
38+
ARG_ASSERT(5, sy > 0);
39+
40+
af_array output;
41+
42+
switch(type) {
43+
case f32: output = unwrap<float >(in, wx, wy, sx, sy); break;
44+
case f64: output = unwrap<double >(in, wx, wy, sx, sy); break;
45+
case c32: output = unwrap<cfloat >(in, wx, wy, sx, sy); break;
46+
case c64: output = unwrap<cdouble>(in, wx, wy, sx, sy); break;
47+
case s32: output = unwrap<int >(in, wx, wy, sx, sy); break;
48+
case u32: output = unwrap<uint >(in, wx, wy, sx, sy); break;
49+
case s64: output = unwrap<intl >(in, wx, wy, sx, sy); break;
50+
case u64: output = unwrap<uintl >(in, wx, wy, sx, sy); break;
51+
case u8: output = unwrap<uchar >(in, wx, wy, sx, sy); break;
52+
case b8: output = unwrap<char >(in, wx, wy, sx, sy); break;
53+
default: TYPE_ERROR(1, type);
54+
}
55+
std::swap(*out,output);
56+
}
57+
CATCHALL;
58+
59+
return AF_SUCCESS;
60+
}

src/api/cpp/unwrap.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*******************************************************
2+
* Copyright (c) 2014, ArrayFire
3+
* All rights reserved.
4+
*
5+
* This file is distributed under 3-clause BSD license.
6+
* The complete license agreement can be obtained at:
7+
* http://arrayfire.com/licenses/BSD-3-Clause
8+
********************************************************/
9+
10+
#include <af/array.h>
11+
#include <af/image.h>
12+
#include "error.hpp"
13+
14+
namespace af
15+
{
16+
array unwrap(const array& in, const dim_t wx, const dim_t wy, const dim_t sx, const dim_t sy)
17+
{
18+
af_array out = 0;
19+
AF_THROW(af_unwrap(&out, in.get(), wx, wy, sx, sy));
20+
return array(out);
21+
}
22+
}
23+

src/backend/cpu/unwrap.cpp

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/*******************************************************
2+
* Copyright (c) 2014, ArrayFire
3+
* All rights reserved.
4+
*
5+
* This file is distributed under 3-clause BSD license.
6+
* The complete license agreement can be obtained at:
7+
* http://arrayfire.com/licenses/BSD-3-Clause
8+
********************************************************/
9+
10+
#include <Array.hpp>
11+
#include <unwrap.hpp>
12+
#include <stdexcept>
13+
#include <err_cpu.hpp>
14+
15+
namespace cpu
16+
{
17+
template<typename T>
18+
void unwrap_(T *outPtr, const T *inPtr, const af::dim4 &odims, const af::dim4 &idims,
19+
const af::dim4 &ostrides, const af::dim4 &istrides,
20+
const dim_t wx, const dim_t wy, const dim_t sx, const dim_t sy)
21+
{
22+
dim_t nx = (idims[0] - wx) / sx + 1;
23+
dim_t ny = (idims[1] - wy) / sy + 1;
24+
25+
for(dim_t w = 0; w < odims[3]; w++) {
26+
for(dim_t z = 0; z < odims[2]; z++) {
27+
dim_t cOut = w * ostrides[3] + z * ostrides[2];
28+
dim_t cIn = w * istrides[3] + z * istrides[2];
29+
for(dim_t col = 0; col < odims[1]; col++) {
30+
// Calculate input window index
31+
dim_t winy = (col / ny);
32+
dim_t winx = (col % ny);
33+
34+
dim_t startx = winx * sx;
35+
dim_t starty = winy * sy;
36+
37+
T* optr = outPtr + cOut + col * ostrides[1];
38+
const T* iptr = inPtr + cIn + starty * istrides[1] + startx;
39+
40+
for(dim_t y = 0; y < wy; y++) {
41+
for(dim_t x = 0; x < wx; x++) {
42+
dim_t oloc = (y * wy + x) * ostrides[0];
43+
dim_t iloc = (y * istrides[1] + x * istrides[0]);
44+
optr[oloc] = iptr[iloc];
45+
}
46+
}
47+
}
48+
}
49+
}
50+
}
51+
52+
template<typename T>
53+
Array<T> unwrap(const Array<T> &in, const dim_t wx, const dim_t wy,
54+
const dim_t sx, const dim_t sy)
55+
{
56+
af::dim4 idims = in.dims();
57+
58+
dim_t nx = (idims[0] - wx) / sx + 1;
59+
dim_t ny = (idims[1] - wy) / sy + 1;
60+
61+
af::dim4 odims(wx * wy, nx * ny, idims[2], idims[3]);
62+
63+
// Create output placeholder
64+
Array<T> outArray = createEmptyArray<T>(odims);
65+
66+
// Get pointers to raw data
67+
const T *inPtr = in.get();
68+
T *outPtr = outArray.get();
69+
70+
af::dim4 ostrides = outArray.strides();
71+
af::dim4 istrides = in.strides();
72+
73+
unwrap_(outPtr, inPtr, odims, idims, ostrides, istrides, wx, wy, sx, sy);
74+
75+
return outArray;
76+
}
77+
78+
79+
#define INSTANTIATE(T) \
80+
template Array<T> unwrap<T> (const Array<T> &in, const dim_t wx, const dim_t wy, \
81+
const dim_t sx, const dim_t sy);
82+
83+
84+
INSTANTIATE(float)
85+
INSTANTIATE(double)
86+
INSTANTIATE(cfloat)
87+
INSTANTIATE(cdouble)
88+
INSTANTIATE(int)
89+
INSTANTIATE(uint)
90+
INSTANTIATE(intl)
91+
INSTANTIATE(uintl)
92+
INSTANTIATE(uchar)
93+
INSTANTIATE(char)
94+
}
95+

src/backend/cpu/unwrap.hpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*******************************************************
2+
* Copyright (c) 2014, ArrayFire
3+
* All rights reserved.
4+
*
5+
* This file is distributed under 3-clause BSD license.
6+
* The complete license agreement can be obtained at:
7+
* http://arrayfire.com/licenses/BSD-3-Clause
8+
********************************************************/
9+
10+
#include <Array.hpp>
11+
12+
namespace cpu
13+
{
14+
template<typename T>
15+
Array<T> unwrap(const Array<T> &in, const dim_t wx, const dim_t wy,
16+
const dim_t sx, const dim_t sy);
17+
}
18+

src/backend/cuda/unwrap.cu

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*******************************************************
2+
* Copyright (c) 2014, ArrayFire
3+
* All rights reserved.
4+
*
5+
* This file is distributed under 3-clause BSD license.
6+
* The complete license agreement can be obtained at:
7+
* http://arrayfire.com/licenses/BSD-3-Clause
8+
********************************************************/
9+
10+
#include <Array.hpp>
11+
#include <unwrap.hpp>
12+
//#include <kernel/unwrap.hpp>
13+
#include <stdexcept>
14+
#include <err_cuda.hpp>
15+
16+
namespace cuda
17+
{
18+
template<typename T>
19+
Array<T> unwrap(const Array<T> &in, const dim_t wx, const dim_t wy,
20+
const dim_t sx, const dim_t sy)
21+
{
22+
af::dim4 idims = in.dims();
23+
24+
dim_t nx = (idims[0] - wx) / sx + 1;
25+
dim_t ny = (idims[1] - wy) / sy + 1;
26+
27+
af::dim4 odims(wx * wy, nx * ny, idims[2], idims[3]);
28+
29+
// Create output placeholder
30+
Array<T> outArray = createEmptyArray<T>(odims);
31+
32+
return outArray;
33+
}
34+
35+
36+
#define INSTANTIATE(T) \
37+
template Array<T> unwrap<T> (const Array<T> &in, const dim_t wx, const dim_t wy, \
38+
const dim_t sx, const dim_t sy);
39+
40+
41+
INSTANTIATE(float)
42+
INSTANTIATE(double)
43+
INSTANTIATE(cfloat)
44+
INSTANTIATE(cdouble)
45+
INSTANTIATE(int)
46+
INSTANTIATE(uint)
47+
INSTANTIATE(intl)
48+
INSTANTIATE(uintl)
49+
INSTANTIATE(uchar)
50+
INSTANTIATE(char)
51+
}
52+

src/backend/cuda/unwrap.hpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*******************************************************
2+
* Copyright (c) 2014, ArrayFire
3+
* All rights reserved.
4+
*
5+
* This file is distributed under 3-clause BSD license.
6+
* The complete license agreement can be obtained at:
7+
* http://arrayfire.com/licenses/BSD-3-Clause
8+
********************************************************/
9+
10+
#include <Array.hpp>
11+
12+
namespace cuda
13+
{
14+
template<typename T>
15+
Array<T> unwrap(const Array<T> &in, const dim_t wx, const dim_t wy,
16+
const dim_t sx, const dim_t sy);
17+
}
18+

src/backend/opencl/unwrap.cpp

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*******************************************************
2+
* Copyright (c) 2014, ArrayFire
3+
* All rights reserved.
4+
*
5+
* This file is distributed under 3-clause BSD license.
6+
* The complete license agreement can be obtained at:
7+
* http://arrayfire.com/licenses/BSD-3-Clause
8+
********************************************************/
9+
10+
#include <Array.hpp>
11+
#include <unwrap.hpp>
12+
//#include <kernel/unwrap.hpp>
13+
#include <stdexcept>
14+
#include <err_opencl.hpp>
15+
16+
namespace opencl
17+
{
18+
template<typename T>
19+
Array<T> unwrap(const Array<T> &in, const dim_t wx, const dim_t wy,
20+
const dim_t sx, const dim_t sy)
21+
{
22+
af::dim4 idims = in.dims();
23+
24+
dim_t nx = (idims[0] - wx) / sx + 1;
25+
dim_t ny = (idims[1] - wy) / sy + 1;
26+
27+
af::dim4 odims(wx * wy, nx * ny, idims[2], idims[3]);
28+
29+
// Create output placeholder
30+
Array<T> outArray = createEmptyArray<T>(odims);
31+
32+
return outArray;
33+
}
34+
35+
36+
#define INSTANTIATE(T) \
37+
template Array<T> unwrap<T> (const Array<T> &in, const dim_t wx, const dim_t wy, \
38+
const dim_t sx, const dim_t sy);
39+
40+
41+
INSTANTIATE(float)
42+
INSTANTIATE(double)
43+
INSTANTIATE(cfloat)
44+
INSTANTIATE(cdouble)
45+
INSTANTIATE(int)
46+
INSTANTIATE(uint)
47+
INSTANTIATE(intl)
48+
INSTANTIATE(uintl)
49+
INSTANTIATE(uchar)
50+
INSTANTIATE(char)
51+
}
52+

src/backend/opencl/unwrap.hpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*******************************************************
2+
* Copyright (c) 2014, ArrayFire
3+
* All rights reserved.
4+
*
5+
* This file is distributed under 3-clause BSD license.
6+
* The complete license agreement can be obtained at:
7+
* http://arrayfire.com/licenses/BSD-3-Clause
8+
********************************************************/
9+
10+
#include <Array.hpp>
11+
12+
namespace opencl
13+
{
14+
template<typename T>
15+
Array<T> unwrap(const Array<T> &in, const dim_t wx, const dim_t wy,
16+
const dim_t sx, const dim_t sy);
17+
}
18+

0 commit comments

Comments
 (0)