-
Notifications
You must be signed in to change notification settings - Fork 978
Expand file tree
/
Copy pathbtl_usnic_module.c
More file actions
2317 lines (2010 loc) · 88.3 KB
/
Copy pathbtl_usnic_module.c
File metadata and controls
2317 lines (2010 loc) · 88.3 KB
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil -*- */
/*
* Copyright (c) 2004-2005 The Trustees of Indiana University and Indiana
* University Research and Technology
* Corporation. All rights reserved.
* Copyright (c) 2004-2008 The University of Tennessee and The University
* of Tennessee Research Foundation. All rights
* reserved.
* Copyright (c) 2004-2005 High Performance Computing Center Stuttgart,
* University of Stuttgart. All rights reserved.
* Copyright (c) 2004-2005 The Regents of the University of California.
* All rights reserved.
* Copyright (c) 2006 Sandia National Laboratories. All rights
* reserved.
* Copyright (c) 2009-2019 Cisco Systems, Inc. All rights reserved
* Copyright (c) 2014-2016 Los Alamos National Security, LLC. All rights
* reserved.
* Copyright (c) 2014-2020 Intel, Inc. All rights reserved.
* Copyright (c) 2018 Amazon.com, Inc. or its affiliates. All Rights reserved.
* $COPYRIGHT$
*
* Additional copyrights may follow
*
* $HEADER$
*/
#include "opal_config.h"
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include "opal/class/opal_bitmap.h"
#include "opal/datatype/opal_convertor.h"
#include "opal/mca/memchecker/base/base.h"
#include "opal/prefetch.h"
#include "opal/util/output.h"
#include "opal/util/printf.h"
#include "opal/util/show_help.h"
#include "opal_stdint.h"
#include "opal/mca/btl/base/btl_base_error.h"
#include "opal/mca/btl/btl.h"
#include "opal/mca/mpool/base/base.h"
#include "opal/mca/mpool/mpool.h"
#include "opal/mca/rcache/base/base.h"
#include "opal/mca/rcache/rcache.h"
#include "btl_usnic.h"
#include "btl_usnic_ack.h"
#include "btl_usnic_compat.h"
#include "btl_usnic_connectivity.h"
#include "btl_usnic_endpoint.h"
#include "btl_usnic_frag.h"
#include "btl_usnic_hwloc.h"
#include "btl_usnic_module.h"
#include "btl_usnic_proc.h"
#include "btl_usnic_send.h"
#include "btl_usnic_stats.h"
#include "btl_usnic_util.h"
static void finalize_one_channel(opal_btl_usnic_module_t *module,
struct opal_btl_usnic_channel_t *channel);
static int channel_addr2str(opal_btl_usnic_module_t *module, int channel, char *str,
size_t len_param)
{
size_t len;
len = len_param;
fi_av_straddr(module->av, module->mod_channels[channel].info->src_addr, str, &len);
if (len > len_param) {
opal_show_help("help-mpi-btl-usnic.txt", "libfabric API failed", true,
opal_process_info.nodename, module->linux_device_name, "fi_av_straddr",
__FILE__, __LINE__, FI_ENODATA,
"Failed to convert address to string: buffer too short");
return OPAL_ERR_OUT_OF_RESOURCE;
}
return OPAL_SUCCESS;
}
/*
* Loop over a block of procs sent to us in add_procs and see if we
* want to add a proc/endpoint for them.
*/
static int add_procs_block_create_endpoints(opal_btl_usnic_module_t *module, size_t block_offset,
size_t block_len, opal_proc_t **procs,
mca_btl_base_endpoint_t **endpoints)
{
int rc;
opal_proc_t *my_proc;
size_t num_created = 0;
char *errhost;
/* get pointer to my proc structure */
my_proc = opal_proc_local_get();
if (NULL == my_proc) {
return OPAL_ERR_OUT_OF_RESOURCE;
}
/* Loop over a block in the procs we were given */
for (size_t i = block_offset; i < (block_offset + block_len); i++) {
struct opal_proc_t *opal_proc = procs[i];
opal_btl_usnic_proc_t *usnic_proc;
mca_btl_base_endpoint_t *usnic_endpoint;
endpoints[i] = NULL;
/* Do not create loopback usnic connections */
if (opal_proc == my_proc) {
opal_output_verbose(75, USNIC_OUT, "btl:usnic:add_procs:%s: not connecting to self",
module->linux_device_name);
continue;
}
/* usNIC does not support loopback to the same machine */
if (OPAL_PROC_ON_LOCAL_NODE(opal_proc->proc_flags)) {
opal_output_verbose(75, USNIC_OUT,
"btl:usnic:add_procs:%s: not connecting to %s on same server",
module->linux_device_name,
usnic_compat_proc_name_print(&opal_proc->proc_name));
continue;
}
/* Find (or create if it doesn't exist) this peer's proc.
This will receive the modex info for that proc. Note that
the proc is shared by all usnic modules that are trying
to reach this destination. */
usnic_proc = NULL;
rc = opal_btl_usnic_proc_match(opal_proc, module, &usnic_proc);
if (OPAL_ERR_UNREACH == rc) {
/* If the peer doesn't have usnic modex info, then we just
skip it */
if (74 < opal_output_get_verbosity(USNIC_OUT)) {
errhost = opal_get_proc_hostname(opal_proc);
opal_output(0,
"btl:usnic:add_procs:%s: peer %s on %s does not have usnic modex info; "
"skipping",
module->linux_device_name,
usnic_compat_proc_name_print(&opal_proc->proc_name), errhost);
free(errhost);
}
continue;
} else if (OPAL_SUCCESS != rc) {
return OPAL_ERR_OUT_OF_RESOURCE;
}
/* Create the endpoint for this proc/module combination. If we cannot
* reach this proc via this module, move on to the next proc. */
usnic_endpoint = NULL;
rc = opal_btl_usnic_create_endpoint(module, usnic_proc, &usnic_endpoint);
if (OPAL_SUCCESS != rc) {
if (4 < opal_output_get_verbosity(USNIC_OUT)) {
errhost = opal_get_proc_hostname(opal_proc);
opal_output(0, "btl:usnic:add_procs:%s: unable to create endpoint to peer %s on %s",
module->linux_device_name,
usnic_compat_proc_name_print(&opal_proc->proc_name), errhost);
free(errhost);
}
OBJ_RELEASE(usnic_proc);
continue;
}
/* We like this new endpoint; save it */
opal_pointer_array_add(&module->all_procs, usnic_proc);
char str[IPV4STRADDRLEN];
struct opal_btl_usnic_modex_t *modex = &usnic_endpoint->endpoint_remote_modex;
opal_btl_usnic_snprintf_ipv4_addr(str, sizeof(str), modex->ipv4_addr, modex->netmask);
char local_pri_addr[64] = {0};
rc = channel_addr2str(module, USNIC_PRIORITY_CHANNEL, local_pri_addr,
sizeof(local_pri_addr));
if (OPAL_SUCCESS != rc) {
OBJ_RELEASE(usnic_proc);
continue;
}
char local_data_addr[64] = {0};
rc = channel_addr2str(module, USNIC_DATA_CHANNEL, local_data_addr, sizeof(local_data_addr));
if (OPAL_SUCCESS != rc) {
OBJ_RELEASE(usnic_proc);
continue;
}
opal_output_verbose(5, USNIC_OUT,
"btl:usnic:add_procs:%s: new usnic peer endpoint: pri=%s:%d, "
"data=%s:%d (local: pri=%s, data=%s)",
module->linux_device_name, str, modex->ports[USNIC_PRIORITY_CHANNEL],
str, modex->ports[USNIC_DATA_CHANNEL], local_pri_addr, local_data_addr);
endpoints[i] = usnic_endpoint;
++num_created;
}
opal_output_verbose(5, USNIC_OUT, "btl:usnic: made %" PRIsize_t " endpoints", num_created);
return OPAL_SUCCESS;
}
/*
* Print a warning about how the remote peer was unreachable.
*
* This is a separate helper function simply because it's somewhat
* bulky to put inline.
*/
static void add_procs_warn_unreachable(opal_btl_usnic_module_t *module,
opal_btl_usnic_endpoint_t *endpoint)
{
char *errhost;
/* Only show the warning if it is enabled */
if (!mca_btl_usnic_component.show_route_failures) {
return;
}
char remote[IPV4STRADDRLEN];
opal_btl_usnic_snprintf_ipv4_addr(remote, sizeof(remote),
endpoint->endpoint_remote_modex.ipv4_addr,
endpoint->endpoint_remote_modex.netmask);
opal_output_verbose(15, USNIC_OUT, "btl:usnic: %s (which is %s) couldn't reach peer %s",
module->linux_device_name, module->if_ipv4_addr_str, remote);
errhost = opal_get_proc_hostname(endpoint->endpoint_proc->proc_opal);
opal_show_help("help-mpi-btl-usnic.txt", "unreachable peer IP", true,
opal_process_info.nodename, module->if_ipv4_addr_str, module->linux_device_name,
errhost, remote);
free(errhost);
}
/* A bunch of calls to fi_av_insert() were previously
* invoked. Go reap them all.
*/
static int add_procs_block_reap_fi_av_inserts(opal_btl_usnic_module_t *module, size_t block_offset,
size_t block_len,
struct mca_btl_base_endpoint_t **endpoints)
{
int ret = OPAL_SUCCESS;
int num_left;
size_t i, channel;
uint32_t event;
struct fi_eq_entry entry;
struct fi_eq_err_entry err_entry;
bool error_occurred = false;
/* compute num fi_av_insert completions we are waiting for */
num_left = 0;
for (i = block_offset; i < (block_offset + block_len); ++i) {
if (NULL != endpoints[i]) {
num_left += USNIC_NUM_CHANNELS;
}
}
/* Loop polling for fi_av_insert completion (they were
individually started in btl_usnic_proc.c) */
while (num_left > 0) {
opal_btl_usnic_addr_context_t *context;
ret = fi_eq_sread(module->av_eq, &event, &entry, sizeof(entry), -1, 0);
/* fi_eq_sread() will return ret==sizeof(entry) if there are
no entries on the error queue and the event read completes
successfully. We'll get a non-error event back for every
fi_av_insert(), even if that insert errors out. */
if (sizeof(entry) == ret) {
context = entry.context;
free(context);
--num_left;
ret = 0;
}
/* fi_eq_sread() will return -FI_EAVAIL if there's something
on the error queue.
Note that if an fi_av_insert() fails, it will *first*
return an entry on the error queue (i.e., have
fi_eq_sread() return -FI_EVAIL), and *second* it will
return an entry on the normal queue. Meaning: the failed
fi_av_insert() context will show up twice. So don't free
the context (or anything associated with it) here in this
error case, because the same context will show up in the
non-error case (above). */
else if (-FI_EAVAIL == ret) {
ret = fi_eq_readerr(module->av_eq, &err_entry, 0);
if (sizeof(err_entry) == ret) {
context = err_entry.context;
/* Got some kind of address failure. This usually means
that we couldn't find a route to that peer (e.g., the
networking is hosed between us). So just mark that we
can't reach this peer, and print a pretty warning. */
if (EADDRNOTAVAIL == err_entry.err || EHOSTUNREACH == err_entry.err) {
/* Note that endpoint was passed in a context in
USNIC_NUM_CHANNELS fi_av_insert() calls.
Meaning: if that address fails to resolve,
we'll get USNIC_NUM_CHANNELS error events back
with a context containing that endpoint.
We therefore only want to print a pretty
warning about (and OBJ_RELEASE) that endpoint
the *first* time it is reported. */
for (i = block_offset; i < (block_offset + block_len); ++i) {
if (endpoints[i] == context->endpoint) {
add_procs_warn_unreachable(module, context->endpoint);
OBJ_RELEASE(context->endpoint);
endpoints[i] = NULL;
break;
}
}
ret = 0;
}
/* Got some other kind of error -- give up on this
interface. */
else {
opal_show_help("help-mpi-btl-usnic.txt", "libfabric API failed", true,
opal_process_info.nodename, module->linux_device_name,
"async insertion result", __FILE__, __LINE__, err_entry.err,
"Failed to insert address to AV");
ret = OPAL_ERR_OUT_OF_RESOURCE;
error_occurred = true;
/* we can't break here, need to finish reaping all
inserts */
}
/* Don't free the context or the endpoint -- events
that come in as errors will *also* come in as real
events */
} else {
/* If we get here, it means fi_eq_readerr() failed
badly, which means something has gone tremendously
wrong. Probably the only safe thing to do here is
exit. */
opal_show_help("help-mpi-btl-usnic.txt", "internal error during init", true,
opal_process_info.nodename, module->linux_device_name,
"fi_eq_readerr()", __FILE__, __LINE__, ret,
"Returned != sizeof(err_entry)");
ret = OPAL_ERR_OUT_OF_RESOURCE;
error_occurred = true;
/* Per above, there's really nothing sane left to do
but exit */
opal_btl_usnic_exit(module);
}
} else {
/* If we get here, it means fi_eq_readerr() failed badly,
which means something has gone tremendously wrong.
Given that we're potentially not even all the way
through MPI_INIT yet, the only sane thing to do here is
exit. */
opal_show_help("help-mpi-btl-usnic.txt", "internal error during init", true,
opal_process_info.nodename, module->linux_device_name, "fi_eq_sread()",
__FILE__, __LINE__, ret, "Returned != (sizeof(entry) or -FI_EAVAIL)");
ret = OPAL_ERR_OUT_OF_RESOURCE;
error_occurred = true;
/* Per above, there's really nothing sane left to do but
exit */
opal_btl_usnic_exit(module);
}
}
/* Look through the list:
- If something went wrong above, free all endpoints.
- If an otherwise-valid endpoint has no dest, that means we timed
out trying to resolve it, so just release that endpoint. */
size_t num_endpoints_created = 0;
for (i = block_offset; i < (block_offset + block_len); i++) {
if (NULL != endpoints[i]) {
bool happy;
happy = true;
if (error_occurred) {
happy = false;
} else {
for (channel = 0; channel < USNIC_NUM_CHANNELS; ++channel) {
if (FI_ADDR_NOTAVAIL == endpoints[i]->endpoint_remote_addrs[channel]) {
happy = false;
break;
}
}
}
if (happy) {
++num_endpoints_created;
} else {
OBJ_RELEASE(endpoints[i]);
endpoints[i] = NULL;
}
}
}
/* All done */
opal_output_verbose(5, USNIC_OUT,
"btl:usnic: created destinations for %" PRIsize_t " endpoints",
num_endpoints_created);
return ret;
}
/*
* Create endpoints for the procs we were given in add_procs.
*/
static int add_procs_create_endpoints(struct opal_btl_usnic_module_t *module, size_t nprocs,
struct opal_proc_t **procs,
struct mca_btl_base_endpoint_t **endpoints)
{
/* We need to ensure that we don't overrun the libfabric AV EQ.
Divide up all the peer address resolutions we need to do into a
series of blocks; insert and complete each block before moving
to the next (note: if performance mandates it, we can move to a
sliding window style of AV inserts to get better concurrency of
AV resolution). */
/* Leave a few empty slots in the AV EQ, just for good measure */
if (module->av_eq_size < 8) {
opal_show_help("help-mpi-btl-usnic.txt", "fi_av_eq too small", true,
opal_process_info.nodename, module->av_eq_size, 8);
return OPAL_ERR_OUT_OF_RESOURCE;
}
size_t eq_size = module->av_eq_size - 8;
size_t block_len = eq_size;
size_t num_av_inserts = nprocs * USNIC_NUM_CHANNELS;
size_t num_blocks = num_av_inserts / block_len;
if (num_av_inserts % block_len != 0) {
++num_blocks;
}
/* Per above, the blocks are expressed in terms of number of AV
inserts. Convert them to be expressed in terms of number of
procs. */
block_len /= USNIC_NUM_CHANNELS;
/* Per above, loop over creating the endpoints so that we do not
overrun the libfabric AV EQ. */
int rc;
for (size_t block_offset = 0, block = 0; block < num_blocks;
block_offset += block_len, ++block) {
/* Adjust for the last block */
if (block_len > (nprocs - block_offset)) {
block_len = nprocs - block_offset;
}
/* First, create endpoints (and procs, if they're not already
created) for the usnic-reachable procs we were given. */
rc = add_procs_block_create_endpoints(module, block_offset, block_len, procs, endpoints);
if (OPAL_SUCCESS != rc) {
return rc;
}
/* For each endpoint that was created, we initiated the
process to create NUM_CHANNELS fi_addrs. Go finish all of
those. This will be the final determination of whether we
can use the endpoint or not because we'll find out if each
endpoint is reachable or not. */
rc = add_procs_block_reap_fi_av_inserts(module, block_offset, block_len, endpoints);
if (OPAL_SUCCESS != rc) {
return rc;
}
}
return OPAL_SUCCESS;
}
/*
* Add procs to this BTL module, receiving endpoint information from
* the modex. This is done in 2 phases:
*
* 1. Find (or create) the remote proc, and create the associated
* endpoint.
* 2. Resolve the address handles for all remote endpoints.
*
* The second part is a separate loop from the first part to allow the
* address lookups to be done in parallel. This comes at a cost,
* however: we may determine during the 2nd part that we should tear
* down some or all the endpoints that we created in the 1st part.
* For example, fi_av_insert() may fail in a fatal way (i.e., we
* should fail the entire add_procs()), or it may fail for one or more
* peers (i.e., we should just mark those peers as unreachable and not
* add a proc or endpoint for them).
*/
static int usnic_add_procs(struct mca_btl_base_module_t *base_module, size_t nprocs,
struct opal_proc_t **procs, struct mca_btl_base_endpoint_t **endpoints,
opal_bitmap_t *reachable)
{
opal_btl_usnic_module_t *module = (opal_btl_usnic_module_t *) base_module;
int rc;
/* Go create the endpoints (including all relevant address
resolution) */
rc = add_procs_create_endpoints(module, nprocs, procs, endpoints);
if (OPAL_SUCCESS != rc) {
goto fail;
}
/* Find all the endpoints with a complete set of USD destinations
and mark them as reachable */
for (size_t i = 0; NULL != reachable && i < nprocs; ++i) {
if (NULL != endpoints[i]) {
bool happy = true;
for (int channel = 0; channel < USNIC_NUM_CHANNELS; ++channel) {
if (FI_ADDR_NOTAVAIL == endpoints[i]->endpoint_remote_addrs[channel]) {
happy = false;
break;
}
}
if (happy) {
opal_bitmap_set_bit(reachable, i);
}
}
}
/* This is fairly gross, but we need to output the connectivity
map after add_procs() has been called on all existing usnic
modules. The only way I can think to do that is to count each
time add_procs() is called, and when we're at a multiple of
component.num_modules (i.e., add_procs() has been called on
each module -- both during MPI_INIT and dynamic process cases),
call the function to output the map. */
static int num_times_add_procs_called = 0;
++num_times_add_procs_called;
if (0 == (num_times_add_procs_called % mca_btl_usnic_component.num_modules)) {
opal_btl_usnic_connectivity_map();
}
return OPAL_SUCCESS;
fail:
/* If we get here, it means something went terribly wrong. Scorch
the earth: destroy all endpoints and say that nothing was
reachable. */
for (size_t i = 0; i < nprocs; ++i) {
if (NULL != endpoints[i]) {
OBJ_RELEASE(endpoints[i]);
endpoints[i] = NULL;
}
}
return rc;
}
/*
* Delete the proc as reachable from this module. If there are
* multiple usnic modules in a process, we'll come through here
* multiple times to remove each proc. The OBJ reference counts
* will make all the details work out.
*/
static int usnic_del_procs(struct mca_btl_base_module_t *base_module, size_t nprocs,
struct opal_proc_t **procs, struct mca_btl_base_endpoint_t **peers)
{
size_t i, j;
opal_btl_usnic_module_t *module;
opal_btl_usnic_endpoint_t *endpoint;
int index;
module = (struct opal_btl_usnic_module_t *) base_module;
for (i = 0; i < nprocs; i++) {
opal_btl_usnic_proc_t *proc = opal_btl_usnic_proc_lookup_ompi(procs[i]);
if (NULL != proc) {
/* find endpoint for this module */
for (j = 0; j < proc->proc_endpoint_count; ++j) {
endpoint = proc->proc_endpoints[j];
if (NULL != endpoint && endpoint->endpoint_module == module) {
/* This call to usnic_del_procs is actually an
* implicit ACK of every packet we have ever sent
* ***because it is only ever invoked after an
* OOB/grpcomm barrier (in MPI_COMM_DISCONNECT and
* MPI_FINALIZE)***, so call handle_ack (via
* flush_endpoint) to do all the ACK processing
* and release all the data that needs
* releasing. */
if (!ENDPOINT_DRAINED(endpoint)) {
opal_btl_usnic_flush_endpoint(endpoint);
}
/* We're all done with this endpoint */
OBJ_RELEASE(endpoint);
break; /* done once we found match */
}
}
/* remove proc from this module, and decrement its refcount */
for (index = 0; index < module->all_procs.size; ++index) {
if (opal_pointer_array_get_item(&module->all_procs, index) == proc) {
OBJ_RELEASE(proc);
opal_pointer_array_set_item(&module->all_procs, index, NULL);
break;
}
}
}
}
return OPAL_SUCCESS;
}
/*
* Let the PML register a callback function with me
*/
static int usnic_register_pml_err_cb(struct mca_btl_base_module_t *btl,
mca_btl_base_module_error_cb_fn_t cbfunc)
{
opal_btl_usnic_module_t *module = (opal_btl_usnic_module_t *) btl;
module->pml_error_callback = cbfunc;
return OPAL_SUCCESS;
}
/**
* Allocate control messages or eager frags if BTL does not have
* INPLACE flag. To be clear: max it will ever alloc is eager_limit.
* THEREFORE: eager_limit is the max that ALLOC must always be able to
* alloc.
* --> Contraction in the btl.h documentation.
*/
static mca_btl_base_descriptor_t *usnic_alloc(struct mca_btl_base_module_t *btl,
struct mca_btl_base_endpoint_t *endpoint,
uint8_t order, size_t size, uint32_t flags)
{
opal_btl_usnic_send_frag_t *frag;
opal_btl_usnic_module_t *module = (opal_btl_usnic_module_t *) btl;
mca_btl_base_descriptor_t *desc;
/* small is easy, just allocate a small segment */
if (OPAL_LIKELY(size <= module->max_frag_payload)) {
opal_btl_usnic_small_send_frag_t *sfrag;
sfrag = opal_btl_usnic_small_send_frag_alloc(module);
if (NULL == sfrag) {
return NULL;
}
frag = &sfrag->ssf_base;
/* between MTU and eager limit, we need to allocate a buffer
* which can hold the data. We will allocate a
* large fragment, and attach the buffer to it.
*/
} else {
opal_btl_usnic_large_send_frag_t *lfrag;
/* truncate to eager_limit */
if (OPAL_UNLIKELY(size > module->super.btl_eager_limit)) {
size = module->super.btl_eager_limit;
}
lfrag = opal_btl_usnic_large_send_frag_alloc(module);
if (OPAL_UNLIKELY(NULL == lfrag)) {
return NULL;
}
frag = &lfrag->lsf_base;
assert(size > 0);
lfrag->lsf_buffer = malloc(size);
if (OPAL_UNLIKELY(NULL == lfrag->lsf_buffer)) {
opal_btl_usnic_frag_return(module, &lfrag->lsf_base.sf_base);
return NULL;
}
/* pointer to buffer for caller */
frag->sf_base.uf_base.USNIC_SEND_LOCAL[0].seg_addr.pval = lfrag->lsf_buffer;
MSGDEBUG1_OUT("usnic_alloc: packing frag %p on the fly", (void *) frag);
lfrag->lsf_pack_on_the_fly = true;
}
#if MSGDEBUG2
opal_output(0, "usnic_alloc: %s frag=%p, size=%d, flags=0x%x\n",
(size <= module->max_frag_payload) ? "small" : "large", (void *) frag, (int) size,
flags);
#endif
/* set endpoint */
frag->sf_endpoint = endpoint;
/* set up descriptor */
desc = &frag->sf_base.uf_base;
desc->des_flags = flags;
desc->USNIC_SEND_LOCAL[0].seg_len = size;
desc->USNIC_SEND_LOCAL_COUNT = 1;
return desc;
}
/**
* Return an allocated fragment
*
* Return the send fragment to the appropriate list
*/
static int usnic_free(struct mca_btl_base_module_t *btl, mca_btl_base_descriptor_t *des)
{
opal_btl_usnic_frag_t *frag = (opal_btl_usnic_frag_t *) des;
#if MSGDEBUG2
opal_output(0, "usnic_free: %p (%s)\n", (void *) frag, usnic_frag_type(frag->uf_type));
#endif
/* calling free routine gives us ownership - we need to make sure
* the flag is set for lower layers.
*/
frag->uf_base.des_flags |= MCA_BTL_DES_FLAGS_BTL_OWNERSHIP;
opal_btl_usnic_frag_return_cond((struct opal_btl_usnic_module_t *) btl, frag);
return OPAL_SUCCESS;
}
/* Packs data from the given large send frag into single new segment and
* returns a pointer to it. The packed data comes first from SG[0] (PML
* header) and then second from either SG[1] (if seg_addr is non-NULL) or from
* the convertor contained in the frag.
*
* The frag's bookkeeping data will be updated appropriately. */
static opal_btl_usnic_chunk_segment_t *
pack_chunk_seg_from_frag(struct opal_btl_usnic_module_t *module,
opal_btl_usnic_large_send_frag_t *lfrag)
{
opal_btl_usnic_chunk_segment_t *seg;
uint8_t *copyptr;
size_t copylen;
size_t seg_space;
size_t max_data;
assert(NULL != lfrag);
/* never should be attempting to pack if we've already packed everything */
assert(lfrag->lsf_pack_bytes_left > 0);
seg = opal_btl_usnic_chunk_segment_alloc(module);
if (OPAL_UNLIKELY(NULL == seg)) {
/* TODO look at ways to deal with this case more gracefully, possibly as
* part of capping the overall BTL memory consumption. Watch out for
* possible MPI-layer deadlock. */
opal_btl_usnic_util_abort("chunk segment allocation error", __FILE__, __LINE__);
}
seg_space = module->max_chunk_payload;
copyptr = seg->ss_base.us_payload.raw;
/* Keep copying in as long as we have space, there is data to be copied, and
* we aren't using a convertor (SG[1] will be NULL if we have a convertor).
*/
while (seg_space > 0 && lfrag->lsf_pack_bytes_left > 0 && NULL != lfrag->lsf_cur_ptr) {
if (seg_space > lfrag->lsf_bytes_left_in_sge) {
copylen = lfrag->lsf_bytes_left_in_sge;
} else {
copylen = seg_space;
}
memcpy(copyptr, lfrag->lsf_cur_ptr, copylen);
seg_space -= copylen;
copyptr += copylen;
lfrag->lsf_bytes_left_in_sge -= copylen;
lfrag->lsf_pack_bytes_left -= copylen;
if (lfrag->lsf_bytes_left_in_sge > 0) {
lfrag->lsf_cur_ptr += copylen;
} else {
++lfrag->lsf_cur_sge;
lfrag->lsf_cur_ptr = lfrag->lsf_des_src[lfrag->lsf_cur_sge].seg_addr.pval;
lfrag->lsf_bytes_left_in_sge = lfrag->lsf_des_src[lfrag->lsf_cur_sge].seg_len;
}
}
if (seg_space > 0 && lfrag->lsf_pack_bytes_left > 0) {
/* the remaining bytes come from a convertor; pack using it */
assert(NULL == lfrag->lsf_cur_ptr);
assert(1 == lfrag->lsf_cur_sge);
copylen = lfrag->lsf_pack_bytes_left;
if (copylen > seg_space) {
copylen = seg_space;
}
usnic_convertor_pack_simple(&lfrag->lsf_base.sf_convertor, copyptr, copylen, &max_data);
seg_space -= max_data;
lfrag->lsf_bytes_left_in_sge -= max_data;
lfrag->lsf_pack_bytes_left -= max_data;
}
MSGDEBUG1_OUT("%s: packed seg=%p, frag=%p, payload=%zd\n", __func__, (void *) seg,
(void *) lfrag, (module->max_chunk_payload - seg_space));
assert(lfrag->lsf_cur_sge <= 2);
assert(seg_space < module->max_chunk_payload); /* must make progress */
seg->ss_parent_frag = &lfrag->lsf_base;
seg->ss_len = module->max_chunk_payload - seg_space;
return seg;
}
static int usnic_finalize(struct mca_btl_base_module_t *btl)
{
opal_btl_usnic_module_t *module = (opal_btl_usnic_module_t *) btl;
if (module->device_async_event_active) {
opal_event_del(&(module->device_async_event));
module->device_async_event_active = false;
}
opal_btl_usnic_connectivity_unlisten(module);
finalize_one_channel(module, &module->mod_channels[USNIC_DATA_CHANNEL]);
finalize_one_channel(module, &module->mod_channels[USNIC_PRIORITY_CHANNEL]);
/* Shutdown the stats on this module */
opal_btl_usnic_stats_finalize(module);
/* Note that usnic_del_procs will have been called for *all* procs
by this point, so the module->all_endpoints list will be empty.
Destruct it. */
opal_mutex_lock(&module->all_endpoints_lock);
OBJ_DESTRUCT(&(module->all_endpoints));
module->all_endpoints_constructed = false;
opal_mutex_unlock(&module->all_endpoints_lock);
/* _flush_endpoint should have emptied this list */
assert(opal_list_is_empty(&(module->pending_resend_segs)));
OBJ_DESTRUCT(&module->pending_resend_segs);
/* Similarly, empty the endpoints_that_need_acks list so that
endpoints don't still have an endpoint_ack_li item still in
use */
while (!opal_list_is_empty(&(module->endpoints_that_need_acks))) {
(void) opal_list_remove_first(&(module->endpoints_that_need_acks));
}
OBJ_DESTRUCT(&module->endpoints_that_need_acks);
/* Note that usnic_del_procs will have been called for *all* procs
by this point, so the module->all_procs list will be empty.
Destruct it. */
OBJ_DESTRUCT(&module->all_procs);
for (int i = module->first_pool; i <= module->last_pool; ++i) {
OBJ_DESTRUCT(&module->module_recv_buffers[i]);
}
free(module->module_recv_buffers);
OBJ_DESTRUCT(&module->ack_segs);
OBJ_DESTRUCT(&module->endpoints_with_sends);
OBJ_DESTRUCT(&module->small_send_frags);
OBJ_DESTRUCT(&module->large_send_frags);
OBJ_DESTRUCT(&module->put_dest_frags);
OBJ_DESTRUCT(&module->chunk_segs);
OBJ_DESTRUCT(&module->senders);
mca_rcache_base_module_destroy(module->rcache);
if (NULL != module->av) {
fi_close(&module->av->fid);
}
if (NULL != module->av_eq) {
fi_close(&module->av_eq->fid);
}
if (NULL != module->dom_eq) {
fi_close(&module->dom_eq->fid);
}
fi_close(&module->domain->fid);
fi_close(&module->fabric->fid);
free(module->linux_device_name);
return OPAL_SUCCESS;
}
static inline unsigned get_send_credits(struct opal_btl_usnic_channel_t *chan)
{
return chan->credits;
}
static void usnic_do_resends(opal_btl_usnic_module_t *module)
{
opal_btl_usnic_send_segment_t *sseg;
opal_btl_usnic_endpoint_t *endpoint;
struct opal_btl_usnic_channel_t *data_channel;
int ret, count;
data_channel = &module->mod_channels[USNIC_DATA_CHANNEL];
count = mca_btl_usnic_component.max_resends_per_iteration;
while (count > 0 && (get_send_credits(data_channel) > 1)
&& !opal_list_is_empty(&module->pending_resend_segs)) {
/*
* If a segment is on the re-send list, it will not
* be in the retransmit hotel. Post the segment, then check it in.
*/
sseg = (opal_btl_usnic_send_segment_t *) opal_list_remove_first(
&module->pending_resend_segs);
endpoint = sseg->ss_parent_frag->sf_endpoint;
/* clobber any stale piggy-backed ACK */
sseg->ss_base.us_btl_header->ack_present = 0;
/* Only post this segment if not already posted */
if (sseg->ss_send_posted == 0) {
/* resends are always standard segments */
sseg->ss_channel = USNIC_DATA_CHANNEL;
/* re-send the segment (we have a send credit available) */
opal_btl_usnic_post_segment(module, endpoint, sseg);
/* consume a send credit for this endpoint. May send us
* negative, oh well... This is because the completion routine
* always increments send credits, and we must balance.
* Alternative is to mark this as a retrans segment and check in
* completion, but this ugly way avoids extra checks in the
* critical path. And, really, respects the concept of send
* credits more.
*/
--endpoint->endpoint_send_credits;
++module->stats.num_resends;
}
/* restart the retrans timer */
ret = opal_hotel_checkin(&endpoint->endpoint_hotel, sseg, &sseg->ss_hotel_room);
if (OPAL_UNLIKELY(OPAL_SUCCESS != ret)) {
opal_btl_usnic_util_abort("hotel checkin failed\n", __FILE__, __LINE__);
}
--count;
}
}
/* Given a large send frag (which is at the head of the given endpoint's send
* queue), generate a new segment, fill it with data, and
* endpoint_send_segment() it. Takes care of subsequent frag
* cleanup/bookkeeping (dequeue, descriptor callback, etc.) if this frag was
* completed by this segment.
*
* ASSUMES THAT THE CALLER HAS ALREADY CHECKED TO SEE IF WE HAVE
* A SEND CREDIT!
*/
static void usnic_handle_large_send(opal_btl_usnic_module_t *module,
opal_btl_usnic_endpoint_t *endpoint,
opal_btl_usnic_send_frag_t *frag)
{
opal_btl_usnic_large_send_frag_t *lfrag;
opal_btl_usnic_btl_chunk_header_t *chp;
opal_btl_usnic_send_segment_t *sseg;
size_t payload_len;
assert(frag->sf_base.uf_type == OPAL_BTL_USNIC_FRAG_LARGE_SEND);
lfrag = (opal_btl_usnic_large_send_frag_t *) frag;
if (lfrag->lsf_cur_offset == 0) {
/* assign a fragment ID */
do {
lfrag->lsf_frag_id = endpoint->endpoint_next_frag_id++;
} while (lfrag->lsf_frag_id == 0);
}
if (lfrag->lsf_pack_on_the_fly) {
assert(opal_list_is_empty(&lfrag->lsf_seg_chain));
/* just pack a single chunk segment and put it on the list */
sseg = pack_chunk_seg_from_frag(module, lfrag);
} else {
/* data was pre-packed in prepare_src */
sseg = (opal_btl_usnic_send_segment_t *) opal_list_remove_first(&lfrag->lsf_seg_chain);
}
assert(NULL != sseg);
payload_len = sseg->ss_len;
assert(payload_len > 0); /* must have made progress */
assert(payload_len <= module->max_chunk_payload);
assert(lfrag->lsf_bytes_left >= payload_len);
/* set actual packet length */
sseg->ss_len = sizeof(opal_btl_usnic_btl_chunk_header_t) + payload_len;
lfrag->lsf_bytes_left -= payload_len;
/* fill in the chunk's BTL header with frag info */
chp = sseg->ss_base.us_btl_chunk_header;
chp->ch_frag_id = lfrag->lsf_frag_id;
chp->ch_frag_size = lfrag->lsf_base.sf_size;
chp->ch_frag_offset = lfrag->lsf_cur_offset;
chp->ch_hdr.tag = lfrag->lsf_tag;
/* payload length into the header*/
sseg->ss_base.us_btl_header->payload_len = payload_len;
// We assume that the caller has checked to see that we have a
// send credit, so do the send.
opal_btl_usnic_endpoint_send_segment(module, sseg);
/* do fragment bookkeeping */
lfrag->lsf_cur_offset += payload_len;
#if MSGDEBUG1
opal_output(0, "%s: payload_len=%zd, bytes_left=%zd on_the_fly=%s\n", __func__, payload_len,
lfrag->lsf_bytes_left, lfrag->lsf_pack_on_the_fly ? "true" : "false");