forked from arrayfire/arrayfire
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathiota.cpp
More file actions
148 lines (124 loc) · 4.51 KB
/
Copy pathiota.cpp
File metadata and controls
148 lines (124 loc) · 4.51 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
/*******************************************************
* 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 <arrayfire.h>
#include <af/dim4.hpp>
#include <af/defines.h>
#include <af/traits.hpp>
#include <vector>
#include <iostream>
#include <complex>
#include <string>
#include <testHelpers.hpp>
using std::vector;
using std::string;
using std::cout;
using std::endl;
using af::cfloat;
using af::cdouble;
template<typename T>
class Iota : public ::testing::Test
{
public:
virtual void SetUp() {
subMat0.push_back({0, 4, 1});
subMat0.push_back({2, 6, 1});
subMat0.push_back({0, 2, 1});
}
vector<af_seq> subMat0;
};
// create a list of types to be tested
typedef ::testing::Types<float, double, int, unsigned int, unsigned char> TestTypes;
// register the type list
TYPED_TEST_CASE(Iota, TestTypes);
template<typename T>
void iotaTest(const uint x, const uint y, const uint z, const uint w, const uint rep)
{
if (noDoubleTests<T>()) return;
af::dim4 idims(x, y, z, w);
af_array outArray = 0;
ASSERT_EQ(AF_SUCCESS, af_iota(&outArray, idims.ndims(), idims.get(), rep, (af_dtype) af::dtype_traits<T>::af_type));
// Get result
T* outData = new T[idims.elements()];
ASSERT_EQ(AF_SUCCESS, af_get_data_ptr((void*)outData, outArray));
// Compare result
unsigned mul1 = rep > 0;
unsigned mul2 = rep > 1;
unsigned mul3 = rep > 2;
for(int w = 0; w < idims[3]; w++) {
for(int z = 0; z < idims[2]; z++) {
for(int y = 0; y < idims[1]; y++) {
for(int x = 0; x < idims[0]; x++) {
T val = (mul3 * w * idims[0] * idims[1] * idims[2]) +
(mul2 * z * idims[0] * idims[1]) +
(mul1 * y * idims[0]) + x;
dim_type idx = (w * idims[0] * idims[1] * idims[2]) +
(z * idims[0] * idims[1]) +
(y * idims[0]) + x;
ASSERT_EQ(val, outData[idx]) << "at: " << idx << std::endl;
}
}
}
}
// Delete
delete[] outData;
if(outArray != 0) af_destroy_array(outArray);
}
#define IOTA_INIT(desc, x, y, z, w, rep) \
TYPED_TEST(Iota, desc) \
{ \
iotaTest<TypeParam>(x, y, z, w, rep); \
}
IOTA_INIT(Iota1D0, 100, 1, 1, 1, 0);
IOTA_INIT(Iota2D0, 10, 20, 1, 1, 0);
IOTA_INIT(Iota2D1, 100, 5, 1, 1, 1);
IOTA_INIT(Iota3D0, 20, 6, 3, 1, 0);
IOTA_INIT(Iota3D1, 10, 12, 5, 1, 1);
IOTA_INIT(Iota3D2, 25, 30, 2, 1, 2);
IOTA_INIT(Iota4D0, 20, 6, 3, 2, 0);
IOTA_INIT(Iota4D1, 10, 12, 5, 2, 1);
IOTA_INIT(Iota4D2, 25, 30, 2, 2, 2);
IOTA_INIT(Iota4D3, 25, 30, 2, 2, 3);
///////////////////////////////// CPP ////////////////////////////////////
//
TEST(Iota, CPP)
{
if (noDoubleTests<float>()) return;
const unsigned x = 23;
const unsigned y = 15;
const unsigned z = 4;
const unsigned w = 2;
const unsigned rep = 2;
af::dim4 idims(x, y, z, w);
af::array output = af::iota(x, y, z, w, rep, f32);
// Get result
float* outData = new float[idims.elements()];
output.host((void*)outData);
// Compare result
unsigned mul1 = rep > 0;
unsigned mul2 = rep > 1;
unsigned mul3 = rep > 2;
for(int w = 0; w < idims[3]; w++) {
for(int z = 0; z < idims[2]; z++) {
for(int y = 0; y < idims[1]; y++) {
for(int x = 0; x < idims[0]; x++) {
float val = (mul3 * w * idims[0] * idims[1] * idims[2]) +
(mul2 * z * idims[0] * idims[1]) +
(mul1 * y * idims[0]) + x;
dim_type idx = (w * idims[0] * idims[1] * idims[2]) +
(z * idims[0] * idims[1]) +
(y * idims[0]) + x;
ASSERT_EQ(val, outData[idx]) << "at: " << idx << std::endl;
}
}
}
}
// Delete
delete[] outData;
}