-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cu
137 lines (103 loc) · 3.01 KB
/
main.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
125
126
127
128
129
130
131
132
133
134
135
136
137
#ifndef ONLY_CPU
#include "cuda_runtime.h"
#define EIGEN_USE_GPU
#endif
#include <iostream>
#include <cmath>
#include <Eigen/Core>
#include <Eigen/Dense>
#include <unsupported/Eigen/CXX11/Tensor>
template <typename T>
using Vec = Eigen::TensorMap<Eigen::Tensor<T, 1, Eigen::RowMajor, Eigen::DenseIndex>,
Eigen::Aligned>;
template <typename T>
using Matrix = Eigen::TensorMap<Eigen::Tensor<T, 2, Eigen::RowMajor, Eigen::DenseIndex>,
Eigen::Aligned>;
template <typename T>
void print(T* input, int size) {
for (int i = 0; i < size; i++) {
std::cout << input[i] << " ";
}
std::cout << std::endl;
}
void test_add(int size, float* t_a, float* t_b, float* t_c) {
#ifndef ONLY_CPU
float* d_a;
float* d_b;
float* d_c;
cudaMalloc((void**)&d_a, size * sizeof(float));
cudaMalloc((void**)&d_b, size * sizeof(float));
cudaMalloc((void**)&d_c, size * sizeof(float));
cudaMemcpy(d_a, t_a, size * sizeof(float), cudaMemcpyHostToDevice);
cudaMemcpy(d_b, t_b, size * sizeof(float), cudaMemcpyHostToDevice);
Vec<float> a(d_a, size);
Vec<float> b(d_b, size);
Vec<float> c(d_c, size);
Eigen::CudaStreamDevice sd;
Eigen::GpuDevice dd(&sd);
c.device(dd) = a + b;
cudaMemcpy(t_c, d_c, size * sizeof(float), cudaMemcpyDeviceToHost);
print<float>(t_c, size);
cudaFree(d_a);
cudaFree(d_b);
cudaFree(d_c);
#else
Vec<float> a(t_a, size);
Vec<float> b(t_b, size);
Vec<float> c(t_c, size);
Eigen::DefaultDevice dd;
c.device(dd) = a + b;
print<float>(t_c, size);
#endif
}
void test_mul(int size, float* t_a, float* t_b, float* t_c) {
int width = sqrt(size);
int height = width;
Eigen::array<Eigen::IndexPair<Eigen::DenseIndex>, 1> dim_pair;
dim_pair[0].first = 1;
dim_pair[0].second = 0;
#ifndef ONLY_CPU
float* d_a;
float* d_b;
float* d_c;
cudaMalloc((void**)&d_a, size * sizeof(float));
cudaMalloc((void**)&d_b, size * sizeof(float));
cudaMalloc((void**)&d_c, size * sizeof(float));
cudaMemcpy(d_a, t_a, size * sizeof(float), cudaMemcpyHostToDevice);
cudaMemcpy(d_b, t_b, size * sizeof(float), cudaMemcpyHostToDevice);
Matrix<float> a(d_a, height, width);
Matrix<float> b(d_b, height, width);
Matrix<float> c(d_c, height, width);
Eigen::CudaStreamDevice sd;
Eigen::GpuDevice dd(&sd);
c.device(dd) = a.contract(b, dim_pair);
cudaMemcpy(t_c, d_c, size * sizeof(float), cudaMemcpyDeviceToHost);
print<float>(t_c, size);
cudaFree(d_a);
cudaFree(d_b);
cudaFree(d_c);
#else
Matrix<float> a(t_a, height, width);
Matrix<float> b(t_b, height, width);
Matrix<float> c(t_c, height, width);
Eigen::DefaultDevice dd;
c.device(dd) = a.contract(b, dim_pair);
print<float>(t_c, size);
#endif
}
int main() {
int size = 4;
float* t_a = (float*)malloc(size * sizeof(float));
float* t_b = (float*)malloc(size * sizeof(float));
float* t_c = (float*)malloc(size * sizeof(float));
for (int i = 0; i < size; i++) {
t_a[i] = i;
t_b[i] = i;
}
test_add(size, t_a, t_b, t_c);
test_mul(size, t_a, t_b, t_c);
free(t_a);
free(t_b);
free(t_c);
return 0;
}