-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrdma_gpu.cu
217 lines (182 loc) · 4.89 KB
/
rdma_gpu.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
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#include <cuda.h>
#include <cuda_runtime.h>
#include "rdma_common.h"
struct ibv_mr *mr = NULL;
int totalsize = TOTALSIZE;
int buffsize = BUFFSIZE;
void *addr, *addrserver;
/**
* @brief All of the functions with an address is run from the transmitter, the rest is ran from the receiver and uses the global *addr.
*
*/
/* This function registers RDMA memory region on GPU */
extern "C" struct ibv_mr* rdma_gpubuffer_alloc(struct ibv_pd *pd, uint32_t length,
enum ibv_access_flags permission)
{
if (!pd) {
rdma_error("Protection domain is NULL \n");
return NULL;
}
cudaMalloc((void**)&addr, length);
if (!addr) {
rdma_error("failed to allocate buffer, -ENOMEM\n");
return NULL;
}
printf("Allocating gpu memory\n");
debug("GPU Buffer allocated: %p , len: %u \n", addr, length);
if (!pd) {
rdma_error("Protection domain is NULL, ignoring \n");
return NULL;
}
debug("GPU pointer address : %p\n", addr);
mr = ibv_reg_mr(pd, addr, length, permission);
debug("mr: %p ", mr);
if (!mr) {
rdma_error("Failed to create mr on buffer, errno: %s \n", strerror(errno));
cudaFree(addr);
}
debug("Registered: %p , len: %u , stag: 0x%x \n",
mr->addr,
(unsigned int) mr->length,
mr->lkey);
return mr;
}
extern "C" struct ibv_mr* rdma_gpubuffer_alloc_adress(struct ibv_pd *pd, void* addr, uint32_t length,
enum ibv_access_flags permission)
{
if (!pd) {
rdma_error("Protection domain is NULL \n");
return NULL;
}
//debug("GPU Buffer from server: %p , len: %u \n", addr, length);
mr = ibv_reg_mr(pd, addr, length, permission);
debug("mr: %p ", mr);
if (!mr) {
rdma_error("Failed to create mr on buffers, errno: %s \n", strerror(errno));
cudaFree(addr);
}
debug("Registered: %p , len: %u , stag: 0x%x \n",
mr->addr,
(unsigned int) mr->length,
mr->lkey);
return mr;
}
extern "C" int cuAlloc(void** addr, size_t length){
void *cudmem;
cudaError_t err = cudaMallocHost((void**)&cudmem, length);
if (err != cudaSuccess) {
rdma_error("Failed to allocate gpumemory, -ENOMEM\n");
return err;
}
debug("GPU Buffer from server: %p , len: %lu \n", cudmem, (unsigned long)length);
*addr = cudmem;
return (int)err;
}
extern "C" int cuFree(void* addr){
cudaError_t err = cudaFree(addr);
if (err != cudaSuccess) {
rdma_error("Failed to allocate gpumemory, -ENOMEM\n");
return -1;
}
//printf("Cuda Error? :%d",err);
return (int)err;
}
/* This function releases RDMA memory region on GPU */
extern "C" void rdma_gpubuffer_free()
{
if (!mr) {
rdma_error("Passed memory region is NULL, ignoring\n");
return ;
}
void *to_free = mr->addr;
debug("Deregistered: %p , len: %u , stag : 0x%x \n",
mr->addr,
(unsigned int) mr->length,
mr->lkey);
ibv_dereg_mr(mr);
debug("Buffer %p free'ed\n", to_free);
cudaFree(to_free);
}
extern "C" void rdma_gpubuffer_free_addr(struct ibv_mr* mr)
{
if (!mr) {
rdma_error("Passed memory region is NULL, ignoring\n");
return ;
}
void *to_free = mr->addr;
debug("Deregistered: %p , len: %u , stag : 0x%x \n",
mr->addr,
(unsigned int) mr->length,
mr->lkey);
ibv_dereg_mr(mr);
debug("Buffer %p free'ed\n", to_free);
cudaFree(to_free);
}
/*
Function reads the value of the first address. If the memory is equal to idx we know that the transmitter has written the first sequence.
We then write a -1 in that memory location to signal we've read the written memory.
If the idx reaches the amount of packages(idx) specified, we break.
*/
__global__ void kernel(void *addr, int totalsize, int buffsize)
{
volatile int* memory = (int*)addr;
int idx = 1;
printf("Clientside GPU \n");
while(1){
if(memory[0] == idx){
printf("Read full buffer of msg #: %d\n", memory[0]);
//Print out entire written memory buffer
//for (size_t i = 0; i < buffsize/sizeof(int); i++)
//{
//printf(" %d", (memory[i]));
//}
memory[0] = -1;
idx++;
}
if(idx == 20000)
{
memory[0] = -1;
printf("Finished transfer, sent %d packages.\n", idx);
break;
}
}
}
__global__ void kernel_addr(void *addr, int totalsize, int buffsize)
{
int* mem = (int*)addr;
printf("Serverside GPU Array:");
for (size_t i = 0; i < buffsize/4; i++)
{
printf(" %d", (mem[i]));
}
printf("\n");
}
extern "C" void kernel_start()
{
kernel<<<1, 1>>>(addr, totalsize, buffsize);
}
extern "C" int cuCopy(void* dst, void* src, size_t size)
{
cudaError_t err = cudaMemcpy(dst, src, size, cudaMemcpyDeviceToDevice);
if (err != cudaSuccess) {
rdma_error("Failed Memcpy: %d\n", err);
}
return err;
}
/**
* @brief Copy a buffer to GPU memory.
*
*/
extern "C" void kernel_start_addr(void *addr)
{
int test[buffsize] = {0};
for (int i = 0; i < buffsize; i++)
{
test[i] = i;
}
debug("buffsize: %d\n",buffsize);
cudaError_t err = cudaMemcpy(addr, (void*)&test, buffsize, cudaMemcpyDefault);
if (err != cudaSuccess) {
rdma_error("Failed Memcpy\n");
}
}