forked from arrayfire/arrayfire
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwrite.cpp
More file actions
101 lines (81 loc) · 2.32 KB
/
write.cpp
File metadata and controls
101 lines (81 loc) · 2.32 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
/*******************************************************
* 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/traits.hpp>
#include <vector>
#include <iostream>
#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 Write : public ::testing::Test
{
public:
virtual void SetUp() {
}
};
// create a list of types to be tested
typedef ::testing::Types<float, cfloat, double, cdouble, int, unsigned, char, unsigned char> TestTypes;
// register the type list
TYPED_TEST_CASE(Write, TestTypes);
template<typename T>
void writeTest(af::dim4 dims)
{
if (noDoubleTests<T>()) return;
af::array A = af::randu(dims, (af_dtype) af::dtype_traits<T>::af_type);
af::array B = af::randu(dims, (af_dtype) af::dtype_traits<T>::af_type);
af::array A_copy = A.copy();
af::array B_copy = B.copy();
T *a_host = A.host<T>();
T *b_dev = B.device<T>();
A.write(b_dev, dims.elements() * sizeof(T), afDevice);
B.write(a_host, dims.elements() * sizeof(T), afHost);
af::array check1 = A != B_copy; // False so check1 is all 0s
af::array check2 = B != A_copy; // False so check2 is all 0s
char *h_check1 = check1.host<char>();
char *h_check2 = check2.host<char>();
for(int i = 0; i < (int)dims.elements(); i++) {
ASSERT_EQ(h_check1[i], 0) << "at: " << i << std::endl;
ASSERT_EQ(h_check2[i], 0) << "at: " << i << std::endl;
}
delete [] a_host;
delete [] h_check1;
delete [] h_check2;
}
TYPED_TEST(Write, Vector0)
{
writeTest<TypeParam>(af::dim4(10));
}
TYPED_TEST(Write, Vector1)
{
writeTest<TypeParam>(af::dim4(1000));
}
TYPED_TEST(Write, Matrix0)
{
writeTest<TypeParam>(af::dim4(64, 8));
}
TYPED_TEST(Write, Matrix1)
{
writeTest<TypeParam>(af::dim4(256, 256));
}
TYPED_TEST(Write, Volume0)
{
writeTest<TypeParam>(af::dim4(10, 10, 10));
}
TYPED_TEST(Write, Volume1)
{
writeTest<TypeParam>(af::dim4(32, 64, 16));
}