-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathexample2.cu
More file actions
104 lines (83 loc) · 2.84 KB
/
example2.cu
File metadata and controls
104 lines (83 loc) · 2.84 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
#include <iostream>
#include <vector>
#include <device_matrix.h>
/*
* Add these two headers to support:
* (1) Basic File I/O for thrust::device_vector
* (2) Matrix - Vector multiplication
* But you need "nvcc" compiler to compile these two headers
*/
#include <device_arithmetic.h>
#include <device_math.h>
using namespace ext;
using namespace std;
typedef device_matrix<float> mat;
typedef thrust::device_vector<float> vec;
void randomInit(vec& v);
void randomInit(mat& m);
template <typename T>
struct square {
__host__ __device__ T operator()(const T& x) const { return x * x; }
};
int main (int argc, char* argv[]) {
mat A(16, 8);
randomInit(A);
vec x(16), y(8), z(8);
randomInit(x);
randomInit(y);
randomInit(z);
// ==========================================================
// ===== Converion between std::vector & thrust::vector =====
// ==========================================================
std::vector<float> h_x = ext::toStlVector(x);
x = thrust::device_vector<float>(h_x);
printf("x = \n"); print(x);
// =============================
// ===== Utility Functions =====
// =============================
printf("norm(x) = %.7f \n", norm(x)); // L2-norm
printf("sum(x) = %.7f \n\n", sum(x)); // sum
// ===========================
// ===== Vector - Scalar =====
// ===========================
printf("x + 1.23 = "); print(x + 1.23);
printf("x - 1.23 = "); print(x - 1.23);
printf("x * 1.23 = "); print(x * 1.23);
printf("x / 1.23 = "); print(x / 1.23);
printf("1.23 + x = "); print(1.23 + x);
printf("1.23 - x = "); print(1.23 - x);
printf("1.23 * x = "); print(1.23 * x);
printf("1.23 / x = "); print(1.23 / x);
printf("x += 1.23 = "); print(x += 1.23);
printf("x -= 1.23 = "); print(x -= 1.23);
printf("x *= 1.23 = "); print(x *= 1.23);
printf("x /= 1.23 = "); print(x /= 1.23);
// ===========================
// ===== Vector - Vector =====
// ===========================
printf("Element-Wise Multiplication: \n");
printf("y & z = "); print(y & z);
printf("Multiply to vector to get a matrix: (16 x 1) x (1 x 8) => 16 x 8 \n");
printf("x * y = \n"); (x * y).print();
// ===============================
// ===== Matrix - Vector (1) =====
// ===============================
printf("x * A = "); (x * A).print();
printf("A * y = \n"); (A * y).print();
printf("x * A * y = "); (x * A * y).print();
return 0;
}
void randomInit(mat& m) {
float* h_data = new float [m.size()];
for (size_t i=0; i<m.size(); ++i)
h_data[i] = rand() / (float) RAND_MAX;
cudaMemcpy(m.getData(), h_data, m.size() * sizeof(float), cudaMemcpyHostToDevice);
delete [] h_data;
}
void randomInit(vec& v) {
float* h_data = new float [v.size()];
for (size_t i=0; i<v.size(); ++i)
h_data[i] = rand() / (float) RAND_MAX;
v = vec(h_data, h_data + v.size());
delete [] h_data;
}