-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit.c
345 lines (290 loc) · 7.48 KB
/
init.c
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
#define _GNU_SOURCE
#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <inttypes.h>
#include <string.h>
#include <limits.h>
#include <ifaddrs.h>
#include <net/if.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <errno.h>
#include <rdma/fi_rma.h>
#include "init.h"
#include "buffer.h"
#include "connect.h"
#include "logging.h"
#define MAX_RETRIES 1
int _test_myrank;
int _test_nproc;
//static tatas_lock_t op_lock;
static int __initialized = 0;
static fi_cnct_ctx fi_ctx = {
.thread_safe = 0,
.node = NULL,
.service = NULL,
.domain = NULL,
.provider = NULL,
.num_cq = 1,
.rdma_put_align = TEST_FI_PUT_ALIGN,
.rdma_get_align = TEST_FI_GET_ALIGN
};
static void cq_readerr(struct fid_cq *cq, char *cq_str)
{
struct fi_cq_err_entry cq_err;
const char *err_str;
int ret;
ret = fi_cq_readerr(cq, &cq_err, 0);
if (ret < 0) {
dbg_err("Could not read error from CQ");
} else {
err_str = fi_cq_strerror(cq, cq_err.prov_errno, cq_err.err_data, NULL, 0);
log_err("%s: %d %s", cq_str, cq_err.err, fi_strerror(cq_err.err));
log_err("%s: prov_err: %s (%d)", cq_str, err_str, cq_err.prov_errno);
}
}
void *pfi_init(int rank, int nproc) {
__initialized = -1;
_test_myrank = rank;
_test_nproc = nproc;
fi_ctx.num_cq = 1;
fi_ctx.use_rcq = 1;
fi_ctx.eth_dev = "eth0";
fi_ctx.node = NULL;
fi_ctx.service = NULL;
fi_ctx.domain = NULL;
fi_ctx.provider = "sockets";
if (fi_ctx.eth_dev) {
struct ifaddrs *ifaddr, *ifa;
if (getifaddrs(&ifaddr) == -1) {
dbg_err("Cannot get interface addrs");
goto error_exit;
}
for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
if (ifa->ifa_addr == NULL)
continue;
if (!strcmp(ifa->ifa_name, fi_ctx.eth_dev) &&
ifa->ifa_addr->sa_family == AF_INET) {
fi_ctx.node = inet_ntoa(((struct sockaddr_in*)(ifa->ifa_addr))->sin_addr);
dbg_info("Found matching ethernet device: %s (%s)",
ifa->ifa_name, fi_ctx.node);
break;
}
}
}
fi_ctx.hints = fi_allocinfo();
if (!fi_ctx.hints) {
log_err("Could not allocate space for fi hints");
goto error_exit;
}
fi_ctx.hints->domain_attr->name = strdup(fi_ctx.provider);
fi_ctx.hints->domain_attr->mr_mode = FI_MR_BASIC;
fi_ctx.hints->ep_attr->type = FI_EP_RDM;
fi_ctx.hints->caps = FI_MSG | FI_RMA | FI_RMA_EVENT;
fi_ctx.hints->mode = FI_CONTEXT | FI_LOCAL_MR | FI_RX_CQ_DATA;
if(__fi_init_context(&fi_ctx)) {
log_err("Could not initialize libfabric context");
goto error_exit;
}
fi_freeinfo(fi_ctx.hints);
__initialized = 1;
return &fi_ctx;
error_exit:
return NULL;
}
int pfi_finalize() {
return TEST_OK;
}
int pfi_rdma_put(int proc, uintptr_t laddr, uintptr_t raddr, uint64_t size,
void *ldesc, uint64_t rkey, uint64_t id,
uint64_t imm_data, int flags) {
uint64_t *lid = malloc(sizeof(uint64_t));
int rc;
memcpy(lid, &id, sizeof(*lid));
//(!fi_ctx.thread_safe) ? sync_tatas_acquire(&op_lock):NULL;
{
if (fi_ctx.use_rcq && (flags & TEST_FLAG_WITH_IMM)) {
rc = fi_writedata(fi_ctx.eps[_test_myrank], (void*)laddr, size,
fi_mr_desc(ldesc), imm_data, fi_ctx.addrs[proc],
raddr, rkey, lid);
}
else {
rc = fi_write(fi_ctx.eps[_test_myrank], (void*)laddr, size,
fi_mr_desc(ldesc), fi_ctx.addrs[proc], raddr,
rkey, lid);
}
if (rc) {
log_err("Could not PUT to %p, size %lu: %s", (void*)raddr, size,
fi_strerror(-rc));
goto error_exit;
}
}
//(!fi_ctx.thread_safe) ? sync_tatas_release(&op_lock):NULL;
return TEST_OK;
error_exit:
free(lid);
return TEST_ERROR;
}
int pfi_rdma_get(int proc, uintptr_t laddr, uintptr_t raddr, uint64_t size,
void *ldesc, uint64_t rkey, uint64_t id, int flags) {
uint64_t *lid = malloc(sizeof(uint64_t));
int rc;
memcpy(lid, &id, sizeof(*lid));
//(!fi_ctx.thread_safe) ? sync_tatas_acquire(&op_lock):NULL;
{
rc = fi_read(fi_ctx.eps[_test_myrank], (void*)laddr, size,
fi_mr_desc(ldesc), fi_ctx.addrs[proc], raddr,
rkey, lid);
if (rc) {
log_err("Could not GET from %p, size %lu: %s", (void*)raddr, size,
fi_strerror(-rc));
goto error_exit;
}
}
//(!fi_ctx.thread_safe) ? sync_tatas_release(&op_lock):NULL;
return TEST_OK;
error_exit:
free(lid);
return TEST_ERROR;
}
int pfi_tx_size_left(int proc) {
int c;
//(!fi_ctx.thread_safe) ? sync_tatas_acquire(&op_lock):NULL;
{
c = (int)fi_tx_size_left(fi_ctx.eps[_test_myrank]);
}
//(!fi_ctx.thread_safe) ? sync_tatas_release(&op_lock):NULL;
return c;
}
int pfi_rx_size_left(int proc) {
int c;
//(!fi_ctx.thread_safe) ? sync_tatas_acquire(&op_lock):NULL;
{
c = (int)fi_rx_size_left(fi_ctx.eps[_test_myrank]);
}
//(!fi_ctx.thread_safe) ? sync_tatas_release(&op_lock):NULL;
return c;
}
int pfi_get_event(int proc, int max, uint64_t *ids, int *n) {
int i, j, ne, comp;
int start, end;
int retries;
struct fi_cq_data_entry entries[MAX_CQ_POLL];
*n = 0;
comp = 0;
if (fi_ctx.num_cq == 1) {
start = 0;
end = 1;
}
else if (proc == TEST_ANY_SOURCE) {
start = 0;
end = fi_ctx.num_cq;
}
else {
start = TEST_GET_CQ_IND(fi_ctx.num_cq, proc);
end = start+1;
}
//(!fi_ctx.thread_safe) ? sync_tatas_acquire(&op_lock):NULL;
{
for (i=start; i<end && comp<max; i++) {
retries = MAX_RETRIES;
do {
ne = fi_cq_read(fi_ctx.lcq[i], entries, max);
if (ne < 0) {
if (ne == -FI_EAGAIN) {
ne = 0;
continue;
}
else if (ne == -FI_EAVAIL) {
cq_readerr(fi_ctx.lcq[i], "local CQ");
goto error_exit;
}
else {
log_err("fi_cq_read() failed: %s", fi_strerror(-ne));
goto error_exit;
}
}
}
while ((ne < 1) && --retries);
for (j=0; j<ne && j<MAX_CQ_POLL; j++) {
ids[j+comp] = *((uint64_t*)entries[j].op_context);
free(entries[j].op_context);
}
comp += ne;
}
*n = comp;
}
//(!fi_ctx.thread_safe) ? sync_tatas_release(&op_lock):NULL;
// CQs are empty
if (comp == 0) {
return TEST_EVENT_NONE;
}
return TEST_EVENT_OK;
error_exit:
return TEST_EVENT_ERROR;
}
int pfi_get_revent(int proc, int max, uint64_t *ids, uint64_t *imms, int *n) {
int i, j, ne, comp;
int start, end;
int retries;
struct fi_cq_data_entry entries[MAX_CQ_POLL];
if (!fi_ctx.use_rcq) {
return TEST_EVENT_NOTIMPL;
}
*n = 0;
comp = 0;
if (fi_ctx.num_cq == 1) {
start = 0;
end = 1;
}
else if (proc == TEST_ANY_SOURCE) {
start = 0;
end = fi_ctx.num_cq;
}
else {
start = TEST_GET_CQ_IND(fi_ctx.num_cq, proc);
end = start+1;
}
//(!fi_ctx.thread_safe) ? sync_tatas_acquire(&op_lock):NULL;
{
for (i=start; i<end && comp<max; i++) {
retries = MAX_RETRIES;
do {
ne = fi_cq_read(fi_ctx.rcq[i], entries, max);
if (ne < 0) {
if (ne == -FI_EAGAIN) {
ne = 0;
continue;
}
else if (ne == -FI_EAVAIL) {
cq_readerr(fi_ctx.rcq[i], "remote CQ");
goto error_exit;
}
else {
log_err("fi_cq_read() failed: %s", fi_strerror(-ne));
goto error_exit;
}
}
}
while ((ne < 1) && --retries);
for (j=0; j<ne && j<MAX_CQ_POLL; j++) {
// there are no revent IDs associated with this context
ids[j+comp] = 0x0;
imms[j+comp] = entries[j].data;
}
comp += ne;
}
*n = comp;
}
//(!fi_ctx.thread_safe) ? sync_tatas_release(&op_lock):NULL;
// CQs are empty
if (comp == 0) {
return TEST_EVENT_NONE;
}
return TEST_EVENT_OK;
error_exit:
return TEST_EVENT_ERROR;
}