-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathtest_matrix_ewisemult_inverted.cpp
More file actions
169 lines (132 loc) · 4.94 KB
/
test_matrix_ewisemult_inverted.cpp
File metadata and controls
169 lines (132 loc) · 4.94 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#include "cubool/cubool.h"
#include <algorithm>
#include <testing/testing.hpp>
using DataMatrix = std::vector<std::vector<int>>;
void testApplyNotMask(const DataMatrix& matrix_data, const DataMatrix& mask_data) {
cuBool_Index nrows, ncols;
nrows = matrix_data.size();
ncols = matrix_data[0].size();
testing::Matrix test_matrix = testing::Matrix::generatet(nrows, ncols,
[&matrix_data](cuBool_Index i, cuBool_Index j) { return matrix_data[i][j]; });
nrows = mask_data.size();
ncols = mask_data[0].size();
testing::Matrix test_mask = testing::Matrix::generatet(nrows, ncols,
[&mask_data](cuBool_Index i, cuBool_Index j) { return mask_data[i][j]; });
cuBool_Matrix matrix, mask, result;
ASSERT_EQ(cuBool_Matrix_New(&matrix, test_matrix.nrows, test_matrix.ncols), CUBOOL_STATUS_SUCCESS);
ASSERT_EQ(cuBool_Matrix_New(&mask, test_mask.nrows, test_mask.ncols), CUBOOL_STATUS_SUCCESS);
ASSERT_EQ(cuBool_Matrix_New(&result, test_matrix.nrows, test_matrix.ncols), CUBOOL_STATUS_SUCCESS);
ASSERT_EQ(cuBool_Matrix_Build(matrix, test_matrix.rowsIndex.data(), test_matrix.colsIndex.data(), test_matrix.nvals,
CUBOOL_HINT_VALUES_SORTED & CUBOOL_HINT_NO_DUPLICATES),
CUBOOL_STATUS_SUCCESS);
ASSERT_EQ(cuBool_Matrix_Build(mask, test_mask.rowsIndex.data(), test_mask.colsIndex.data(), test_mask.nvals,
CUBOOL_HINT_VALUES_SORTED & CUBOOL_HINT_NO_DUPLICATES),
CUBOOL_STATUS_SUCCESS);
cuBool_Matrix_EWiseMulInverted(result, matrix, mask, CUBOOL_HINT_NO);
// validate value of algorithm
cuBool_Index nvals;
cuBool_Matrix_Nvals(result, &nvals);
std::vector<cuBool_Index> rows(nvals), cols(nvals);
cuBool_Matrix_ExtractPairs(result, rows.data(), cols.data(), &nvals);
cuBool_Matrix_Free(matrix);
cuBool_Matrix_Free(mask);
cuBool_Matrix_Free(result);
auto mask_data_inverted = mask_data;
for (auto& row : mask_data_inverted) {
for (int& value : row) {
value = !value;
}
}
std::vector result_data(matrix_data.size(), std::vector(matrix_data[0].size(), 0));
for (int i = 0; i < nvals; i++) {
result_data[rows[i]][cols[i]] = 1;
}
for (int i = 0; i < matrix_data.size(); i++) {
for (int j = 0; j < matrix_data[0].size(); j++) {
ASSERT_EQ(matrix_data[i][j] * mask_data_inverted[i][j], result_data[i][j]);
}
}
}
TEST(cuBool_Matrix, ApplyMatrix) {
ASSERT_EQ(cuBool_Initialize(CUBOOL_HINT_NO), CUBOOL_STATUS_SUCCESS);
DataMatrix matrix{
{1, 0, 0},
{0, 0, 0},
{0, 1, 0},
};
DataMatrix mask{
{0, 1, 1},
{1, 0, 1},
{0, 1, 1},
};
// iverted is
// 1 0 0
// 0 1 0
// 1 0 0
// matrix & ~mask must have (0, 0)
testApplyNotMask(matrix, mask);
ASSERT_EQ(cuBool_Finalize(), CUBOOL_STATUS_SUCCESS);
}
TEST(cuBool_Matrix, ApplyMatrixRandom) {
ASSERT_EQ(cuBool_Initialize(CUBOOL_HINT_NO), CUBOOL_STATUS_SUCCESS);
for (int i = 0; i < 102; i++) {
int n = rand() % 10 + 1;
int m = rand() % 10 + 1;
DataMatrix matrix(n, std::vector(m, 0));
DataMatrix mask(n, std::vector(m, 0));
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
matrix[i][j] = rand() & 1;
mask[i][j] = rand() & 1;
}
}
testApplyNotMask(matrix, mask);
}
ASSERT_EQ(cuBool_Finalize(), CUBOOL_STATUS_SUCCESS);
}
TEST(cuBool_Matrix, ApplyMatrixEmptyMask) {
ASSERT_EQ(cuBool_Initialize(CUBOOL_HINT_NO), CUBOOL_STATUS_SUCCESS);
DataMatrix matrix{
{1, 0, 1},
{0, 1, 0},
{1, 1, 0},
};
DataMatrix mask{
{0, 0, 0},
{0, 0, 0},
{0, 0, 0},
};
testApplyNotMask(matrix, mask);
ASSERT_EQ(cuBool_Finalize(), CUBOOL_STATUS_SUCCESS);
}
TEST(cuBool_Matrix, ApplyMatrixEmptySource) {
ASSERT_EQ(cuBool_Initialize(CUBOOL_HINT_NO), CUBOOL_STATUS_SUCCESS);
DataMatrix matrix{
{0, 0, 0},
{0, 0, 0},
{0, 0, 0},
};
DataMatrix mask{
{1, 0, 1},
{0, 1, 0},
{1, 1, 0},
};
testApplyNotMask(matrix, mask);
ASSERT_EQ(cuBool_Finalize(), CUBOOL_STATUS_SUCCESS);
}
TEST(cuBool_Matrix, ApplyMatrixBothEmpty) {
ASSERT_EQ(cuBool_Initialize(CUBOOL_HINT_NO), CUBOOL_STATUS_SUCCESS);
DataMatrix matrix{
{0, 0, 0},
{0, 0, 0},
{0, 0, 0},
};
DataMatrix mask{
{0, 0, 0},
{0, 0, 0},
{0, 0, 0},
};
testApplyNotMask(matrix, mask);
ASSERT_EQ(cuBool_Finalize(), CUBOOL_STATUS_SUCCESS);
}
CUBOOL_GTEST_MAIN