-
Notifications
You must be signed in to change notification settings - Fork 0
/
culo_ipc_copy_dma_buf.cpp
176 lines (135 loc) · 5.01 KB
/
culo_ipc_copy_dma_buf.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
164
165
166
167
168
169
170
171
172
173
174
175
176
#include <iostream>
#include <libgen.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <cuda.h>
#define CHILDPROCESSES 1
int sv[CHILDPROCESSES][2];
size_t allocSize = 4096;
#define Elog(fmt,...) \
do { \
fprintf(stderr, "%s:%d: " fmt "\n", \
__FUNCTION__, __LINE__, ##__VA_ARGS__); \
exit(1); \
} while(0)
static int sendmsg(int pipefd, CUipcMemHandle ipc_mhandle) {
if (write(pipefd, &ipc_mhandle, sizeof(ipc_mhandle)) != sizeof(ipc_mhandle))
Elog("failed on write(&ipc_mhandle): %m");
return true;
}
static int reivmsg(int pipefd, CUipcMemHandle *ipc_mhandle) {
if (read(pipefd, ipc_mhandle, sizeof(*ipc_mhandle)) != sizeof(*ipc_mhandle))
Elog("failed on read(&ipc_mhandle): %m");
return true;
}
static const char * cudaErrorName(CUresult rc) {
const char *result;
if (cuGetErrorName(rc, &result) != CUDA_SUCCESS)
return "unknown error";
return result;
}
inline void initializeProcess(CUdeviceptr &cuda_devptr){
CUresult rc;
CUdevice cuda_device;
CUcontext cuda_context;
CUipcMemHandle ipc_mhandle;
/* init CUDA context */
rc = cuInit(0);
if (rc != CUDA_SUCCESS)
Elog("failed on cuInit: %s", cudaErrorName(rc));
rc = cuDeviceGet(&cuda_device, 0);
if (rc != CUDA_SUCCESS)
Elog("failed on cuDeviceGet: %s", cudaErrorName(rc));
rc = cuCtxCreate(&cuda_context, 0, cuda_device);
if (rc != CUDA_SUCCESS)
Elog("failed on cuCtxCreate: %s", cudaErrorName(rc));
}
void run_client(int commPipe, uint32_t clientId) {
std::cout << "Client " << clientId << ", process ID: " << std::dec << getpid() << "\n";
CUresult rc;
CUdeviceptr cuda_devptr;
CUipcMemHandle ipc_mhandle;
initializeProcess(cuda_devptr);
char *heapBuffer = new char[allocSize];
for (size_t i = 0; i < allocSize; ++i) {
heapBuffer[i] = static_cast<char>(i + 1);
}
close(sv[clientId][1]);
reivmsg(commPipe, &ipc_mhandle);
close(sv[clientId][0]);
rc = cuIpcOpenMemHandle(&cuda_devptr, ipc_mhandle,
CU_IPC_MEM_LAZY_ENABLE_PEER_ACCESS);
if (rc != CUDA_SUCCESS)
Elog("Failed on cuIpcOpenMemHandle: %s", cudaErrorName(rc));
rc = cuMemcpyHtoD(cuda_devptr, heapBuffer, allocSize);
if (rc != CUDA_SUCCESS)
Elog("Failed on cuMemcpyHtoD: %s", cudaErrorName(rc));
rc = cuIpcCloseMemHandle(cuda_devptr);
if (rc != CUDA_SUCCESS)
Elog("Failed on cuIpcCloseMemHandle: %s", cudaErrorName(rc));
}
void run_server(bool &validRet) {
std::cout << "Server process ID " << std::dec << getpid() << "\n";
CUresult rc;
CUdeviceptr cuda_devptr;
CUipcMemHandle ipc_mhandle;
initializeProcess(cuda_devptr);
/* allocation and export */
rc = cuMemAlloc(&cuda_devptr, allocSize);
if (rc != CUDA_SUCCESS)
Elog("failed on cuMemAlloc: %s", cudaErrorName(rc));
for (uint32_t i = 0; i < CHILDPROCESSES; i++) {
// Initialize the IPC buffer
int value = 3;
rc =cuMemsetD32(cuda_devptr, value, allocSize/sizeof(value));
if (rc != CUDA_SUCCESS)
Elog("failed on cuMemsetD32: %s", cudaErrorName(rc));
rc = cuIpcGetMemHandle(&ipc_mhandle, cuda_devptr);
if (rc != CUDA_SUCCESS)
Elog("failed on cuIpcGetMemHandle: %s", cudaErrorName(rc));
close(sv[i][0]);
sendmsg(sv[i][1], ipc_mhandle);
close(sv[i][1]);
char *heapBuffer = new char[allocSize];
for (size_t i = 0; i < allocSize; ++i) {
heapBuffer[i] = static_cast<char>(i + 1);
}
// Wait for child to exit
int child_status;
pid_t clientPId = wait(&child_status);
if (clientPId <= 0) {
std::cerr << "Client terminated abruptly with error code " << strerror(errno) << "\n";
std::terminate();
}
void *validateBuffer = new char[allocSize];;
rc = cuMemcpyDtoH (validateBuffer, cuda_devptr, allocSize);
if (rc != CUDA_SUCCESS)
Elog("failed on cuMemcpyDtoH: %s", cudaErrorName(rc));
validRet = (0 == memcmp(heapBuffer, validateBuffer, allocSize));
}
}
int main(int argc, char *argv[]) {
bool outputValidationSuccessful;
pid_t childPids[CHILDPROCESSES];
for (uint32_t i = 0; i < CHILDPROCESSES; i++) {
if (pipe(sv[i])!=0) {
perror("pipe");
exit(1);
}
childPids[i] = fork();
if (childPids[i] < 0) {
perror("fork");
exit(1);
} else if (childPids[i] == 0) {
run_client(sv[i][0], i);
exit(0);
}
}
run_server(outputValidationSuccessful);
std::cout << "\nZello IPC Results validation "
<< (outputValidationSuccessful ? "PASSED" : "FAILED")
<< std::endl;
}