forked from arrayfire/arrayfire
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathireduce.cpp
More file actions
100 lines (94 loc) · 4.52 KB
/
ireduce.cpp
File metadata and controls
100 lines (94 loc) · 4.52 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
/*******************************************************
* 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 <gtest/gtest.h>
#include <af/array.h>
#include <af/arith.h>
#include <af/data.h>
#include <testHelpers.hpp>
using namespace std;
using namespace af;
#define MINMAXOP(fn, ty) \
TEST(IndexedMinMaxTests, Test_##fn##_##ty##_0) \
{ \
if (noDoubleTests<ty>()) return; \
dtype dty = (dtype)dtype_traits<ty>::af_type; \
const int nx = 10000; \
const int ny = 100; \
af::array in = randu(nx, ny, dty); \
af::array val, idx; \
fn(val, idx, in, 0); \
\
ty *h_in = in.host<ty>(); \
ty *h_in_st = h_in; \
ty *h_val = val.host<ty>(); \
uint *h_idx = idx.host<uint>(); \
for (int i = 0; i < ny; i++) { \
ty tmp = *fn##_element(h_in, h_in + nx); \
ASSERT_EQ(tmp, h_val[i]) \
<< "for index" << i; \
ASSERT_EQ(h_in[h_idx[i]], tmp) \
<< "for index" << i; \
h_in += nx; \
} \
delete[] h_in_st; \
delete[] h_val; \
delete[] h_idx; \
} \
TEST(IndexedMinMaxTests, Test_##fn##_##ty##_1) \
{ \
if (noDoubleTests<ty>()) return; \
dtype dty = (dtype)dtype_traits<ty>::af_type; \
const int nx = 100; \
const int ny = 100; \
af::array in = randu(nx, ny, dty); \
af::array val, idx; \
fn(val, idx, in, 1); \
\
ty *h_in = in.host<ty>(); \
ty *h_val = val.host<ty>(); \
uint *h_idx = idx.host<uint>(); \
for (int i = 0; i < nx; i++) { \
ty val = h_val[i]; \
for (int j= 0; j < ny; j++) { \
ty tmp = fn(val, h_in[j * nx + i]); \
ASSERT_EQ(tmp, val); \
} \
ASSERT_EQ(val, h_in[h_idx[i] * nx + i]); \
} \
delete[] h_in; \
delete[] h_val; \
delete[] h_idx; \
} \
TEST(IndexedMinMaxTests, Test_##fn##_##ty##_all) \
{ \
if (noDoubleTests<ty>()) return; \
dtype dty = (dtype)dtype_traits<ty>::af_type; \
const int num = 100000; \
af::array in = randu(num, dty); \
ty val; \
uint idx; \
fn<ty>(&val, &idx, in); \
ty *h_in = in.host<ty>(); \
ty tmp = *fn##_element(h_in, h_in + num); \
ASSERT_EQ(tmp, val); \
ASSERT_EQ(tmp, h_in[idx]); \
delete[] h_in; \
} \
MINMAXOP(min, float)
MINMAXOP(min, double)
MINMAXOP(min, int)
MINMAXOP(min, uint)
MINMAXOP(min, char)
MINMAXOP(min, uchar)
MINMAXOP(max, float)
MINMAXOP(max, double)
MINMAXOP(max, int)
MINMAXOP(max, uint)
MINMAXOP(max, char)
MINMAXOP(max, uchar)