-
Notifications
You must be signed in to change notification settings - Fork 91
/
Copy pathexample2_assignment1.cu
124 lines (101 loc) · 3.98 KB
/
example2_assignment1.cu
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
#include <matx.h>
using namespace matx;
/**
* MatX training assignment 2. This training goes through tensor operations that
* were learned in the 02_operators notebook. Uncomment each verification block
* as you go to ensure your solutions are correct.
*/
int main() {
auto A = make_tensor<float>({2, 3});
auto B = make_tensor<float>({2, 3});
auto V = make_tensor<float>({3});
/****************************************************************************************************
* Initialize tensor A with increasing values from 0.5 to 3.0 in steps of 0.4,
*and tensor V from -1 to -3 in steps of -1.
****************************************************************************************************/
/*** End editing ***/
// Verify init is correct
float step = 0.5;
for (int row = 0; row < A.Size(0); row++) {
for (int col = 0; col < A.Size(1); col++) {
if (A(row, col) != step) {
printf("Mismatch in A init view! actual = %f, expected = %f\n",
A(row, col), step);
exit(-1);
}
step += 0.5;
}
}
for (int col = 0; col < V.Size(0); col++) {
if (V(col) != (-1 + col * -1)) {
printf("Mismatch in A init view! actual = %f, expected = %f\n", V(col),
(float)(-1 + col * -1));
exit(-1);
}
}
print(A);
print(V);
printf("Init verification passed!\n");
/****************************************************************************************************
* Add 5.0 to all elements of A and store the results back in A
****************************************************************************************************/
/*** End editing ***/
cudaStreamSynchronize(0);
step = 0.5;
for (int row = 0; row < A.Size(0); row++) {
for (int col = 0; col < A.Size(1); col++) {
if (A(row, col) != (5.0 + step)) {
printf("Mismatch in A sum view! actual = %f, expected = %f\n",
A(row, col), 5.0 + step);
exit(-1);
}
step += 0.5;
}
}
print(A);
printf("Sum verification passed!\n");
/****************************************************************************************************
* Clone V to match the dimensions of A, and subtract V from A. The results
* should be stored in A
*
* https://devtech-compute.gitlab-master-pages.nvidia.com/matx/quickstart.html#increasing-dimensionality
* https://devtech-compute.gitlab-master-pages.nvidia.com/matx/api/tensorview.html#_CPPv4I0_iEN4matx12tensor_tE
*
****************************************************************************************************/
/// auto tvs = ;
/*** End editing. ***/
// cudaStreamSynchronize(0);
// step = 0.5;
// for (int row = 0; row < A.Size(0); row++) {
// for (int col = 0; col < A.Size(1); col++) {
// if (A(row, col) != (5.0 + step - tvs(row, col))) {
// printf("Mismatch in A sub view! actual = %f, expected = %f\n", A(row,
// col), 5.0 + step - tvs(row, col)); exit(-1);
// }
// step += 0.5;
// }
// }
// print(A);
// print(tvs);
// printf("Clone verification passed!\n");
/****************************************************************************************************
* Raise the matrix A to the power of 2 and multiply the output by two. Next,
* subtract the vector V from each row. Store the result in tensor B.
*
* https://devtech-compute.gitlab-master-pages.nvidia.com/matx/api/tensorops.html#_CPPv4N4matx3powE2Op2Op
****************************************************************************************************/
/*** End editing ***/
cudaStreamSynchronize(0);
for (int row = 0; row < B.Size(0); row++) {
for (int col = 0; col < B.Size(1); col++) {
if (B(row, col) != powf(A(row, col), 2) * 2 - V(col)) {
printf("Mismatch in B init view! actual = %f, expected = %f\n",
B(row, col), powf(A(row, col), 2) * 2 - V(col));
exit(-1);
}
}
}
print(B);
printf("Mixed verification passed!\n");
return 0;
}