-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhello_world_util.h
306 lines (256 loc) · 7.67 KB
/
hello_world_util.h
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/**
* Copyright (c) NVIDIA CORPORATION & AFFILIATES, 2001-2016. ALL RIGHTS RESERVED.
*
* See file LICENSE for terms.
*/
#ifndef UCX_HELLO_WORLD_H
#define UCX_HELLO_WORLD_H
#include <ucs/memory/memory_type.h>
#include <sys/poll.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <netdb.h>
#ifdef HAVE_CUDA
# include <cuda.h>
# include <cuda_runtime.h>
#endif
#define CHKERR_ACTION(_cond, _msg, _action) \
do { \
if (_cond) { \
fprintf(stderr, "Failed to %s\n", _msg); \
_action; \
} \
} while (0)
#define CHKERR_JUMP(_cond, _msg, _label) \
CHKERR_ACTION(_cond, _msg, goto _label)
#define CHKERR_JUMP_RETVAL(_cond, _msg, _label, _retval) \
do { \
if (_cond) { \
fprintf(stderr, "Failed to %s, return value %d\n", _msg, _retval); \
goto _label; \
} \
} while (0)
static ucs_memory_type_t test_mem_type = UCS_MEMORY_TYPE_HOST;
#define CUDA_FUNC(_func) \
do { \
cudaError_t _result = (_func); \
if (cudaSuccess != _result) { \
fprintf(stderr, "%s failed: %s\n", \
#_func, cudaGetErrorString(_result)); \
} \
} while(0)
void print_common_help(void);
void *mem_type_malloc(size_t length)
{
void *ptr;
switch (test_mem_type) {
case UCS_MEMORY_TYPE_HOST:
ptr = malloc(length);
break;
#ifdef HAVE_CUDA
case UCS_MEMORY_TYPE_CUDA:
CUDA_FUNC(cudaMalloc(&ptr, length));
break;
case UCS_MEMORY_TYPE_CUDA_MANAGED:
CUDA_FUNC(cudaMallocManaged(&ptr, length, cudaMemAttachGlobal));
break;
#endif
default:
fprintf(stderr, "Unsupported memory type: %d\n", test_mem_type);
ptr = NULL;
break;
}
return ptr;
}
void mem_type_free(void *address)
{
switch (test_mem_type) {
case UCS_MEMORY_TYPE_HOST:
free(address);
break;
#ifdef HAVE_CUDA
case UCS_MEMORY_TYPE_CUDA:
case UCS_MEMORY_TYPE_CUDA_MANAGED:
CUDA_FUNC(cudaFree(address));
break;
#endif
default:
fprintf(stderr, "Unsupported memory type: %d\n", test_mem_type);
break;
}
}
void *mem_type_memcpy(void *dst, const void *src, size_t count)
{
switch (test_mem_type) {
case UCS_MEMORY_TYPE_HOST:
memcpy(dst, src, count);
break;
#ifdef HAVE_CUDA
case UCS_MEMORY_TYPE_CUDA:
case UCS_MEMORY_TYPE_CUDA_MANAGED:
CUDA_FUNC(cudaMemcpy(dst, src, count, cudaMemcpyDefault));
break;
#endif
default:
fprintf(stderr, "Unsupported memory type: %d\n", test_mem_type);
break;
}
return dst;
}
void *mem_type_memset(void *dst, int value, size_t count)
{
switch (test_mem_type) {
case UCS_MEMORY_TYPE_HOST:
memset(dst, value, count);
break;
#ifdef HAVE_CUDA
case UCS_MEMORY_TYPE_CUDA:
case UCS_MEMORY_TYPE_CUDA_MANAGED:
CUDA_FUNC(cudaMemset(dst, value, count));
break;
#endif
default:
fprintf(stderr, "Unsupported memory type: %d", test_mem_type);
break;
}
return dst;
}
int check_mem_type_support(ucs_memory_type_t mem_type)
{
switch (test_mem_type) {
case UCS_MEMORY_TYPE_HOST:
return 1;
case UCS_MEMORY_TYPE_CUDA:
case UCS_MEMORY_TYPE_CUDA_MANAGED:
#ifdef HAVE_CUDA
return 1;
#else
return 0;
#endif
default:
fprintf(stderr, "Unsupported memory type: %d", test_mem_type);
break;
}
return 0;
}
ucs_memory_type_t parse_mem_type(const char *opt_arg)
{
if (!strcmp(opt_arg, "host")) {
return UCS_MEMORY_TYPE_HOST;
} else if (!strcmp(opt_arg, "cuda") &&
check_mem_type_support(UCS_MEMORY_TYPE_CUDA)) {
return UCS_MEMORY_TYPE_CUDA;
} else if (!strcmp(opt_arg, "cuda-managed") &&
check_mem_type_support(UCS_MEMORY_TYPE_CUDA_MANAGED)) {
return UCS_MEMORY_TYPE_CUDA_MANAGED;
} else {
fprintf(stderr, "Unsupported memory type: \"%s\".\n", opt_arg);
}
return UCS_MEMORY_TYPE_LAST;
}
void print_common_help()
{
fprintf(stderr, " -p <port> Set alternative server port (default:13337)\n");
fprintf(stderr, " -6 Use IPv6 address in data exchange\n");
fprintf(stderr, " -s <size> Set test string length (default:16)\n");
fprintf(stderr, " -m <mem type> Memory type of messages\n");
fprintf(stderr, " host - system memory (default)\n");
if (check_mem_type_support(UCS_MEMORY_TYPE_CUDA)) {
fprintf(stderr, " cuda - NVIDIA GPU memory\n");
}
if (check_mem_type_support(UCS_MEMORY_TYPE_CUDA_MANAGED)) {
fprintf(stderr, " cuda-managed - NVIDIA GPU managed/unified memory\n");
}
}
int connect_common(const char *server, uint16_t server_port, sa_family_t af)
{
int sockfd = -1;
int listenfd = -1;
int optval = 1;
char service[8];
struct addrinfo hints, *res, *t;
int ret;
snprintf(service, sizeof(service), "%u", server_port);
memset(&hints, 0, sizeof(hints));
hints.ai_flags = (server == NULL) ? AI_PASSIVE : 0;
hints.ai_family = af;
hints.ai_socktype = SOCK_STREAM;
ret = getaddrinfo(server, service, &hints, &res);
CHKERR_JUMP(ret < 0, "getaddrinfo() failed", out);
for (t = res; t != NULL; t = t->ai_next) {
sockfd = socket(t->ai_family, t->ai_socktype, t->ai_protocol);
if (sockfd < 0) {
continue;
}
if (server != NULL) {
if (connect(sockfd, t->ai_addr, t->ai_addrlen) == 0) {
break;
}
} else {
ret = setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &optval,
sizeof(optval));
CHKERR_JUMP(ret < 0, "server setsockopt()", err_close_sockfd);
if (bind(sockfd, t->ai_addr, t->ai_addrlen) == 0) {
ret = listen(sockfd, 0);
CHKERR_JUMP(ret < 0, "listen server", err_close_sockfd);
/* Accept next connection */
fprintf(stdout, "Waiting for connection...\n");
listenfd = sockfd;
sockfd = accept(listenfd, NULL, NULL);
close(listenfd);
break;
}
}
close(sockfd);
sockfd = -1;
}
CHKERR_ACTION(sockfd < 0,
(server) ? "open client socket" : "open server socket",
(void)sockfd /* no action */);
out_free_res:
freeaddrinfo(res);
out:
return sockfd;
err_close_sockfd:
close(sockfd);
sockfd = -1;
goto out_free_res;
}
static inline int
barrier(int oob_sock, void (*progress_cb)(void *arg), void *arg)
{
struct pollfd pfd;
int dummy = 0;
ssize_t res;
res = send(oob_sock, &dummy, sizeof(dummy), 0);
if (res < 0) {
return res;
}
pfd.fd = oob_sock;
pfd.events = POLLIN;
pfd.revents = 0;
do {
res = poll(&pfd, 1, 1);
progress_cb(arg);
} while (res != 1);
res = recv(oob_sock, &dummy, sizeof(dummy), MSG_WAITALL);
/* number of received bytes should be the same as sent */
return !(res == sizeof(dummy));
}
static inline int generate_test_string(char *str, int size)
{
char *tmp_str;
int i;
tmp_str = (char *)calloc(1, size);
CHKERR_ACTION(tmp_str == NULL, "allocate memory\n", return -1);
for (i = 0; i < (size - 1); ++i) {
tmp_str[i] = 'A' + (i % 26);
}
mem_type_memcpy(str, tmp_str, size);
free(tmp_str);
return 0;
}
#endif /* UCX_HELLO_WORLD_H */