-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathrocm_bridge.cpp
163 lines (137 loc) · 5.6 KB
/
rocm_bridge.cpp
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
#include <mlir_runner_utils.h>
#include <cstdint>
#include <iostream>
#include <map>
#define ENABLE_CPU_ROUTE (true)
void hip_alloc(void**, int);
void hip_free(void*);
void hip_fill(void*, float, int);
void hip_vecadd(void*, void*, void*, int);
void hip_memcpydtoh(void*, void*, int);
void rocblas_sgemm(float*, float*, float*, int, int, int, int, int, int);
void miopen_conv2d(float*, float*, float*, int, int, int, int, int, int, int,
int, int, int, int, int, int, int, int);
typedef std::map<void *, void *> HostDeviceMemoryMap;
static HostDeviceMemoryMap g_mmap;
extern "C" void gpu_alloc(StridedMemRefType<char, 1> *memref_host) {
void *gpu_ptr = nullptr;
hip_alloc(&gpu_ptr, memref_host->sizes[0]);
g_mmap[memref_host->data] = gpu_ptr;
}
extern "C" void gpu_dealloc(StridedMemRefType<char, 1> *memref_host) {
if (g_mmap.find(memref_host->data) != g_mmap.end()) {
hip_free(memref_host->data);
} else {
std::cout << "g_mmap doesn't contain specified host address!\n";
}
}
extern "C" float gpu_load(StridedMemRefType<float, 1> *memref_host, int64_t index) {
float result = 0.f;
if (g_mmap.find(memref_host->data) != g_mmap.end()) {
hip_memcpydtoh(memref_host->data, g_mmap[memref_host->data], memref_host->sizes[0] * sizeof(float));
result = memref_host->data[index];
} else {
std::cerr << "g_mmap doesn't contain specified host address!\n";
}
return result;
}
extern "C" float gpu_load2d(StridedMemRefType<float, 2> *memref_host, int64_t y, int64_t x) {
float result = 0.f;
if (g_mmap.find(memref_host->data) != g_mmap.end()) {
hip_memcpydtoh(memref_host->data, g_mmap[memref_host->data], memref_host->sizes[0] * memref_host->sizes[1] * sizeof(float));
result = memref_host->data[y * memref_host->strides[0] + x];
} else {
std::cerr << "g_mmap doesn't contain specified host address!\n";
}
return result;
}
extern "C" float gpu_load4d(StridedMemRefType<float, 4> *memref_host, int64_t d0, int64_t d1, int64_t d2, int64_t d3) {
float result = 0.f;
if (g_mmap.find(memref_host->data) != g_mmap.end()) {
hip_memcpydtoh(memref_host->data, g_mmap[memref_host->data], memref_host->sizes[0] * memref_host->sizes[1] * memref_host->sizes[2] * memref_host->sizes[3] * sizeof(float));
result = memref_host->data[d0 * memref_host->strides[0] +
d1 * memref_host->strides[1] +
d2 * memref_host->strides[2] +
d3 * memref_host->strides[3]];
} else {
std::cerr << "g_mmap doesn't contain specified host address!\n";
}
return result;
}
extern "C" void linalg_fill_viewsxf32_f32(StridedMemRefType<float, 1> *X,
float f) {
#if ENABLE_CPU_ROUTE
// Fill CPU memref.
for (unsigned i = 0; i < X->sizes[0]; ++i)
*(X->data + X->offset + i * X->strides[0]) = f;
#endif
// Fill GPU memref.
if (g_mmap.find(X->data) != g_mmap.end()) {
hip_fill(g_mmap[X->data], f, X->sizes[0]);
}
}
__attribute__((always_inline)) extern "C" void
external_func(
StridedMemRefType<float, 1> *A, StridedMemRefType<float, 1> *B,
StridedMemRefType<float, 1> *C) {
if ((g_mmap.find(A->data) != g_mmap.end()) &&
(g_mmap.find(B->data) != g_mmap.end()) &&
(g_mmap.find(C->data) != g_mmap.end())) {
hip_vecadd(g_mmap[A->data], g_mmap[B->data], g_mmap[C->data],
A->sizes[0]);
}
}
__attribute__((always_inline)) extern "C" void
linalg_matmul_viewsxsxf32_viewsxsxf32_viewsxsxf32(
StridedMemRefType<float, 2> *A, StridedMemRefType<float, 2> *B,
StridedMemRefType<float, 2> *C) {
// GEMM on GPU.
if ((g_mmap.find(A->data) != g_mmap.end()) &&
(g_mmap.find(B->data) != g_mmap.end()) &&
(g_mmap.find(C->data) != g_mmap.end())) {
rocblas_sgemm(static_cast<float*>(g_mmap[A->data]),
static_cast<float*>(g_mmap[B->data]),
static_cast<float*>(g_mmap[C->data]),
C->sizes[0], C->sizes[1], A->sizes[1],
A->strides[0], B->strides[0], C->strides[0]);
} else {
std::cerr << "g_mmap doesn't contain specified host address!\n";
}
}
__attribute__((always_inline)) extern "C" void
linalg_conv_viewsxsxsxsxf32_viewsxsxsxsxf32_viewsxsxsxsxf32(
StridedMemRefType<float, 4> *filter,
StridedMemRefType<float, 4> *input,
StridedMemRefType<float, 4> *output) {
//std::cout << std::endl;
//printMemRefMetaData(std::cerr, *filter);
//std::cout << std::endl;
//printMemRefMetaData(std::cerr, *input);
//std::cout << std::endl;
//printMemRefMetaData(std::cerr, *output);
//std::cout << std::endl;
// convolution on GPU.
if ((g_mmap.find(filter->data) != g_mmap.end()) &&
(g_mmap.find(input->data) != g_mmap.end()) &&
(g_mmap.find(output->data) != g_mmap.end())) {
miopen_conv2d(static_cast<float*>(g_mmap[filter->data]),
static_cast<float*>(g_mmap[input->data]),
static_cast<float*>(g_mmap[output->data]),
/* n */ input->sizes[0],
/* c */ input->sizes[1],
/* hi */ input->sizes[2],
/* wi */ input->sizes[3],
/* k */ filter->sizes[0],
/* y */ filter->sizes[2],
/* x */ filter->sizes[3],
/* ho */ output->sizes[2],
/* wo */ output->sizes[3],
// TBD figure out how to get these attributes from MLIR.
/* pad_h */ 0,
/* pad_w */ 0,
/* stride_h */ 1,
/* stride_w */ 1,
/* dilation_h */ 1,
/* dilation_w */ 1);
}
}