-
Notifications
You must be signed in to change notification settings - Fork 31
/
unifyfs_group_rpc.c
2093 lines (1876 loc) · 76.5 KB
/
unifyfs_group_rpc.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
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
/*
* Copyright (c) 2020, Lawrence Livermore National Security, LLC.
* Produced at the Lawrence Livermore National Laboratory.
*
* Copyright 2020, UT-Battelle, LLC.
*
* LLNL-CODE-741539
* All rights reserved.
*
* This is the license for UnifyFS.
* For details, see https://github.com/LLNL/UnifyFS.
* Please read https://github.com/LLNL/UnifyFS/LICENSE for full license text.
*/
#include "unifyfs_group_rpc.h"
#include "unifyfs_p2p_rpc.h"
#include "unifyfs_rpc_util.h"
#ifndef UNIFYFS_BCAST_K_ARY
# define UNIFYFS_BCAST_K_ARY 2
#endif
/* helper method to initialize collective request rpc handle for child peer */
static int get_child_request_handle(hg_id_t request_hgid,
int peer_rank,
hg_handle_t* chdl)
{
int ret = UNIFYFS_SUCCESS;
/* get address for specified server rank */
hg_addr_t addr = get_margo_server_address(peer_rank);
if (HG_ADDR_NULL == addr) {
LOGERR("missing margo address for rank=%d", peer_rank);
ret = UNIFYFS_ERROR_MARGO;
} else {
/* get handle to rpc function */
hg_return_t hret = margo_create(unifyfsd_rpc_context->svr_mid, addr,
request_hgid, chdl);
if (hret != HG_SUCCESS) {
LOGERR("failed to get handle for child request to server %d - %s",
peer_rank, HG_Error_to_string(hret));
ret = UNIFYFS_ERROR_MARGO;
}
}
return ret;
}
/* helper method to forward collective rpc request to one child */
static int forward_child_request(void* input_ptr,
hg_handle_t chdl,
margo_request* creq)
{
int ret = UNIFYFS_SUCCESS;
/* call rpc function */
double timeout_ms = margo_server_server_timeout_msec;
hg_return_t hret = margo_iforward_timed(chdl, input_ptr, timeout_ms, creq);
if (hret != HG_SUCCESS) {
LOGERR("failed to forward request(%p) - %s", creq,
HG_Error_to_string(hret));
ret = UNIFYFS_ERROR_MARGO;
}
return ret;
}
/* Helper function for the UNIFYFS_SERVER_BCAST_RPC_METAGET case in
* get_child_response(). Handles merging the output from a child
* with that from the parent. */
static int merge_metaget_all_bcast_outputs(
metaget_all_bcast_out_t* p_out,
metaget_all_bcast_out_t* c_out,
hg_handle_t p_hdl,
hg_handle_t c_hdl)
{
int ret = UNIFYFS_SUCCESS;
hg_return_t hret = HG_SUCCESS;
hg_return_t bulk_create_hret = HG_SUCCESS;
int32_t parent_num_files = p_out->num_files;
int32_t child_num_files = c_out->num_files;
hg_size_t child_buf_size = child_num_files * sizeof(unifyfs_file_attr_t);
/* Quick optimization: If there's no files for the child or
* parent, we can exit now. (In fact, trying to perform a bulk transfer
* of size 0 will fail, so if we don't bail now, we'd just have to have
* more checks for this case down below.) */
if ((0 == parent_num_files) && (0 == child_num_files)) {
return UNIFYFS_SUCCESS;
}
// Some variables we'll need for the bulk transfer(s)
unifyfs_file_attr_t* child_attr_list = NULL;
hg_bulk_t local_bulk;
const struct hg_info* info = NULL;
hg_addr_t server_addr;
margo_instance_id mid;
// If the number of child files is 0, don't bother with the bulk
// transfer - it will just fail with HG_INVALID_ARG
if (child_num_files) {
// Pull the bulk data (the list of file_attr structs) over
child_attr_list = calloc(child_num_files, sizeof(unifyfs_file_attr_t));
if (!child_attr_list) {
return ENOMEM;
}
// Figure out some margo-specific info that we need for the transfer
info = margo_get_info(c_hdl);
server_addr = info->addr;
mid = margo_hg_handle_get_instance(c_hdl);
hg_size_t segment_sizes[1] = { child_buf_size };
void* segment_ptrs[1] = { (void*)child_attr_list };
bulk_create_hret =
margo_bulk_create(mid, 1, segment_ptrs, segment_sizes,
HG_BULK_WRITE_ONLY, &local_bulk);
if (HG_SUCCESS != bulk_create_hret) {
LOGERR("margo_bulk_create() failed - %s",
HG_Error_to_string(bulk_create_hret));
free(child_attr_list);
return UNIFYFS_ERROR_MARGO;
}
hret = margo_bulk_transfer(mid, HG_BULK_PULL, server_addr,
c_out->file_meta, 0, local_bulk, 0,
child_buf_size);
if (HG_SUCCESS != hret) {
LOGERR("margo_bulk_transfer() failed - %s",
HG_Error_to_string(hret));
margo_bulk_free(local_bulk);
free(child_attr_list);
return UNIFYFS_ERROR_MARGO;
}
margo_bulk_free(local_bulk);
}
/* OK, file attrs from the child (assuming there were any files) are now
* stored in child_attr_list. And if there were 0 files, then
* child_attr_list is NULL. */
// Now get the bulk data from the parent (assuming there is any)
unifyfs_file_attr_t* parent_attr_list = NULL;
if (parent_num_files + child_num_files > 0) {
parent_attr_list = calloc(parent_num_files + child_num_files,
sizeof(unifyfs_file_attr_t));
/* Note: Deliberately allocating enough space for the child file attrs,
* since we're going to be copying them in anyway.
* Also, we had to check to see if there actually was any need to
* allocate memory because if you pass 0 into calloc(), you'll likely
* get a NULL back, and that would confuse the error checking on the
* next lines. */
if (!parent_attr_list) {
free(child_attr_list);
return ENOMEM;
}
}
if (parent_num_files) {
hg_size_t parent_buf_size =
parent_num_files * sizeof(unifyfs_file_attr_t);
// Figure out some margo-specific info that we need for the transfer
info = margo_get_info(p_hdl);
server_addr = info->addr;
// address of the bulk data on the server side
mid = margo_hg_handle_get_instance(p_hdl);
hg_size_t segment_sizes[1] = { parent_buf_size };
void* segment_ptrs[1] = { (void*)parent_attr_list };
bulk_create_hret =
margo_bulk_create(mid, 1, segment_ptrs, segment_sizes,
HG_BULK_WRITE_ONLY, &local_bulk);
if (HG_SUCCESS != bulk_create_hret) {
LOGERR("margo_bulk_create() failed - %s",
HG_Error_to_string(bulk_create_hret));
free(parent_attr_list);
free(child_attr_list);
return UNIFYFS_ERROR_MARGO;
}
/* It would be nice if we didn't have to actually do a margo transfer
* here. The data we need exists in our current address space
* somewhere. Unfortunately, we don't know where because that's
* hidden from us by Margo. The best we can do is hope that Margo is
* optimized for this case and this transfer ends up just being a
* mem copy. */
hret = margo_bulk_transfer(mid, HG_BULK_PULL, server_addr,
p_out->file_meta, 0, local_bulk, 0,
parent_buf_size);
if (HG_SUCCESS != hret) {
LOGERR("margo_bulk_transfer() failed - %s",
HG_Error_to_string(hret));
margo_bulk_free(local_bulk);
free(parent_attr_list);
free(child_attr_list);
return UNIFYFS_ERROR_MARGO;
}
margo_bulk_free(local_bulk);
}
/* OK, file attrs from the parent (assuming there were any files) are now
* stored in parent_attr_list. And parent_attr_list is actually big
* enough to hold all the file attrs from the parent and child.
*
* The next step is to append the child filenames string to the parent's,
* and update the string offsets stored in the child file attrs' filename
* members. */
uint64_t parent_filenames_len =
p_out->filenames ? strlen(p_out->filenames) : 0;
uint64_t child_filenames_len =
c_out->filenames ? strlen(c_out->filenames) : 0;
char* new_filenames = calloc(parent_filenames_len+child_filenames_len+1,
sizeof(char));
if (!new_filenames) {
free(parent_attr_list);
free(child_attr_list);
return ENOMEM;
}
if (p_out->filenames) {
strcpy(new_filenames, p_out->filenames);
}
if (c_out->filenames) {
strcat(new_filenames, c_out->filenames);
}
free(p_out->filenames);
p_out->filenames = new_filenames;
// Now update all the offset values in the child_attr_list
for (unsigned int i = 0; i < child_num_files; i++) {
uint64_t new_offset = (uint64_t)child_attr_list[i].filename +
parent_filenames_len;
child_attr_list[i].filename = (char*)new_offset;
}
/* Now we need to append the child file attrs to the parent file attrs,
* create a new hg_bulk and replace the old parent bulk with the new one.
*/
memcpy(&parent_attr_list[parent_num_files], child_attr_list,
child_buf_size);
free(child_attr_list);
size_t parent_buf_size =
(parent_num_files + child_num_files) * sizeof(unifyfs_file_attr_t);
hg_size_t segment_sizes[1] = { parent_buf_size };
void* segment_ptrs[1] = { (void*)parent_attr_list };
// Save the parent's old bulk so that we can restore it if the
// bulk create fails, or free it if the create succeeds
hg_bulk_t parent_old_bulk = p_out->file_meta;
hret = margo_bulk_create(unifyfsd_rpc_context->svr_mid, 1,
segment_ptrs, segment_sizes,
HG_BULK_READ_ONLY, &p_out->file_meta);
if (hret != HG_SUCCESS) {
LOGERR("margo_bulk_create() failed - %s", HG_Error_to_string(hret));
p_out->file_meta = parent_old_bulk;
free(parent_attr_list);
return UNIFYFS_ERROR_MARGO;
}
margo_bulk_free(parent_old_bulk);
/* Lastly, update the num_files value */
p_out->num_files += child_num_files;
return ret;
}
static int get_child_response(coll_request* coll_req,
hg_handle_t chdl)
{
int ret = UNIFYFS_SUCCESS;
void* out = calloc(1, coll_req->output_sz);
if (NULL == out) {
ret = ENOMEM;
} else {
hg_return_t hret = margo_get_output(chdl, out);
if (hret != HG_SUCCESS) {
LOGERR("margo_get_output() failed - %s", HG_Error_to_string(hret));
ret = UNIFYFS_ERROR_MARGO;
} else {
/* update collective return value using child response */
int child_ret = UNIFYFS_SUCCESS;
void* output = coll_req->output;
switch (coll_req->req_type) {
case UNIFYFS_SERVER_BCAST_RPC_BOOTSTRAP: {
bootstrap_complete_bcast_out_t* cbbo =
(bootstrap_complete_bcast_out_t*) out;
bootstrap_complete_bcast_out_t* bbo =
(bootstrap_complete_bcast_out_t*) output;
child_ret = cbbo->ret;
if ((NULL != bbo) && (child_ret != UNIFYFS_SUCCESS)) {
bbo->ret = child_ret;
}
break;
}
case UNIFYFS_SERVER_BCAST_RPC_EXTENTS: {
extent_bcast_out_t* cebo = (extent_bcast_out_t*) out;
extent_bcast_out_t* ebo = (extent_bcast_out_t*) output;
child_ret = cebo->ret;
if ((NULL != ebo) && (child_ret != UNIFYFS_SUCCESS)) {
ebo->ret = child_ret;
}
break;
}
case UNIFYFS_SERVER_BCAST_RPC_FILEATTR: {
fileattr_bcast_out_t* cfbo = (fileattr_bcast_out_t*) out;
fileattr_bcast_out_t* fbo = (fileattr_bcast_out_t*) output;
child_ret = cfbo->ret;
if ((NULL != fbo) && (child_ret != UNIFYFS_SUCCESS)) {
fbo->ret = child_ret;
}
break;
}
case UNIFYFS_SERVER_BCAST_RPC_LAMINATE: {
laminate_bcast_out_t* clbo = (laminate_bcast_out_t*) out;
laminate_bcast_out_t* lbo = (laminate_bcast_out_t*) output;
child_ret = clbo->ret;
if ((NULL != lbo) && (child_ret != UNIFYFS_SUCCESS)) {
lbo->ret = child_ret;
}
break;
}
case UNIFYFS_SERVER_BCAST_RPC_TRANSFER: {
transfer_bcast_out_t* ctbo = (transfer_bcast_out_t*) out;
transfer_bcast_out_t* tbo = (transfer_bcast_out_t*) output;
child_ret = ctbo->ret;
if ((NULL != tbo) && (child_ret != UNIFYFS_SUCCESS)) {
tbo->ret = child_ret;
}
break;
}
case UNIFYFS_SERVER_BCAST_RPC_TRUNCATE: {
truncate_bcast_out_t* ctbo = (truncate_bcast_out_t*) out;
truncate_bcast_out_t* tbo = (truncate_bcast_out_t*) output;
child_ret = ctbo->ret;
if ((NULL != tbo) && (child_ret != UNIFYFS_SUCCESS)) {
tbo->ret = child_ret;
}
break;
}
case UNIFYFS_SERVER_BCAST_RPC_UNLINK: {
unlink_bcast_out_t* cubo = (unlink_bcast_out_t*) out;
unlink_bcast_out_t* ubo = (unlink_bcast_out_t*) output;
child_ret = cubo->ret;
if ((NULL != ubo) && (child_ret != UNIFYFS_SUCCESS)) {
ubo->ret = child_ret;
}
break;
}
case UNIFYFS_SERVER_BCAST_RPC_METAGET: {
// NOTE: This case is different. It's currently the only
// case that actually returns more than just a single error
// code up the tree.
metaget_all_bcast_out_t* cmabo =
(metaget_all_bcast_out_t*) out;
metaget_all_bcast_out_t* mabo =
(metaget_all_bcast_out_t*) output;
child_ret = cmabo->ret;
if ((NULL != mabo) && (child_ret != UNIFYFS_SUCCESS)) {
mabo->ret = child_ret;
}
if ((NULL != cmabo) && (NULL != mabo)) {
merge_metaget_all_bcast_outputs(
mabo, cmabo, coll_req->progress_hdl, chdl);
} else {
/* One or both of the output structures is missing.
* (This shouldn't ever happen.) */
LOGERR(
"Missing required output structs when handling "
"UNIFYFS_SERVER_BCAST_RPC_METAGET child responses!");
LOGERR("Parent output struct: 0x%lX", (uint64_t)mabo);
LOGERR("Child output struct: 0x%lX", (uint64_t)cmabo);
mabo->ret = UNIFYFS_ERROR_BADCONFIG;
}
break;
}
default:
child_ret = UNIFYFS_FAILURE;
LOGERR("invalid collective request type %d",
coll_req->req_type);
break;
}
ret = child_ret;
margo_free_output(chdl, out);
}
free(out);
}
return ret;
}
static int wait_for_all_child_requests(coll_request* coll_req,
int n_children)
{
if (NULL == coll_req) {
return EINVAL;
}
if (n_children == 0) {
return UNIFYFS_SUCCESS;
} else if (NULL == coll_req->child_reqs) {
LOGERR("collective(%p) has %d children, but NULL child_reqs array",
coll_req, n_children);
return EINVAL;
}
int ret = UNIFYFS_SUCCESS;
int n_complete = 0;
/* use margo_wait_any() until all requests completed/errored */
do {
size_t complete_ndx;
hg_return_t hret = margo_wait_any((size_t)n_children,
coll_req->child_reqs,
&complete_ndx);
if (HG_SUCCESS == hret) {
n_complete++;
hg_handle_t* chdl = coll_req->child_hdls + complete_ndx;
margo_request* creq = coll_req->child_reqs + complete_ndx;
/* get the output of the rpc */
int child_ret = get_child_response(coll_req, *chdl);
LOGDBG("BCAST_RPC: collective(%p) child[%zu] resp=%d",
coll_req, complete_ndx, child_ret);
if (child_ret != UNIFYFS_SUCCESS) {
ret = child_ret;
}
/* set request to MARGO_REQUEST_NULL so that the next call to
* margo_wait_any() will ignore it */
*creq = MARGO_REQUEST_NULL;
/* release the handle for the completed request */
margo_destroy(*chdl);
*chdl = HG_HANDLE_NULL;
} else {
LOGERR("margo_wait_any() failed with error code=%s",
HG_Error_to_string(hret));
ret = UNIFYFS_ERROR_MARGO;
for (int i = 0; i < n_children; i++) {
hg_handle_t* chdl = coll_req->child_hdls + i;
if (HG_HANDLE_NULL != *chdl) {
margo_destroy(*chdl);
*chdl = HG_HANDLE_NULL;
}
}
break; /* out of do/while loop */
}
} while (n_complete < n_children);
return ret;
}
static coll_request* collective_create(server_rpc_e req_type,
hg_handle_t handle,
hg_id_t op_hgid,
int tree_root_rank,
void* input_struct,
void* output_struct,
size_t output_size,
hg_bulk_t bulk_in,
hg_bulk_t bulk_forward,
void* bulk_buf)
{
coll_request* coll_req = calloc(1, sizeof(*coll_req));
if (NULL != coll_req) {
LOGDBG("BCAST_RPC: collective(%p) create (type=%d, root=%d)",
coll_req, req_type, tree_root_rank);
coll_req->resp_hdl = handle;
coll_req->req_type = req_type;
coll_req->output = output_struct;
coll_req->input = input_struct;
coll_req->bulk_in = bulk_in;
coll_req->output_sz = output_size;
coll_req->bulk_buf = bulk_buf;
coll_req->bulk_forward = bulk_forward;
coll_req->progress_req = MARGO_REQUEST_NULL;
coll_req->progress_hdl = HG_HANDLE_NULL;
coll_req->app_id = -1;
coll_req->client_id = -1;
coll_req->client_req_id = -1;
coll_req->auto_cleanup = 1;
/* Default behavior is for bcast_progress_rpc() to automatically
* call collective_cleanup() on the instance. In cases where such
* behavior is incorrect - such as when results need to be returned
* from the collective's children - this variable can be changed
* before calling bcast_progress_rpc(). */
int rc = ABT_mutex_create(&coll_req->resp_valid_sync);
if (ABT_SUCCESS != rc) {
LOGERR("ABT_mutex_create failed");
free(coll_req);
return NULL;
}
rc = ABT_cond_create(&coll_req->resp_valid_cond);
if (ABT_SUCCESS != rc) {
LOGERR("ABT_cond_create failed");
ABT_mutex_free(&coll_req->resp_valid_sync);
free(coll_req);
return NULL;
}
rc = unifyfs_tree_init(glb_pmi_rank, glb_pmi_size, tree_root_rank,
UNIFYFS_BCAST_K_ARY, &(coll_req->tree));
if (rc) {
LOGERR("unifyfs_tree_init() failed");
ABT_mutex_free(&coll_req->resp_valid_sync);
ABT_cond_free(&coll_req->resp_valid_cond);
free(coll_req);
return NULL;
}
size_t n_children = (size_t) coll_req->tree.child_count;
if (n_children) {
coll_req->child_hdls = calloc(n_children, sizeof(hg_handle_t));
coll_req->child_reqs = calloc(n_children, sizeof(margo_request));
if ((NULL == coll_req->child_hdls) ||
(NULL == coll_req->child_reqs)) {
LOGERR("allocation of children state failed");
free(coll_req->child_hdls);
free(coll_req->child_reqs);
/* Note: calling free() on NULL is explicitly allowed */
ABT_mutex_free(&coll_req->resp_valid_sync);
ABT_cond_free(&coll_req->resp_valid_cond);
free(coll_req);
return NULL;
}
int* ranks = coll_req->tree.child_ranks;
for (int i = 0; i < coll_req->tree.child_count; i++) {
/* allocate child request handle */
LOGDBG("collective(%p) - child[%d] is rank=%d",
coll_req, i, ranks[i]);
hg_handle_t* chdl = coll_req->child_hdls + i;
int rc = get_child_request_handle(op_hgid, ranks[i], chdl);
if (rc != UNIFYFS_SUCCESS) {
LOGERR("failed to get child request handle");
*chdl = HG_HANDLE_NULL;
}
}
}
}
return coll_req;
}
/* reset collective input bulk handle to original value */
static void coll_restore_input_bulk(coll_request* coll_req)
{
void* input = coll_req->input;
if ((NULL == input) || (HG_BULK_NULL == coll_req->bulk_in)
|| (HG_BULK_NULL == coll_req->bulk_forward)) {
return;
}
/* update input structure bulk handle using stored value */
switch (coll_req->req_type) {
case UNIFYFS_SERVER_BCAST_RPC_EXTENTS: {
extent_bcast_in_t* ebi = (extent_bcast_in_t*) input;
ebi->extents = coll_req->bulk_in;
break;
}
case UNIFYFS_SERVER_BCAST_RPC_LAMINATE: {
laminate_bcast_in_t* lbi = (laminate_bcast_in_t*) input;
lbi->extents = coll_req->bulk_in;
break;
}
default:
LOGERR("invalid collective request type %d", coll_req->req_type);
break;
}
}
void collective_cleanup(coll_request* coll_req)
{
if (NULL == coll_req) {
return;
}
LOGDBG("BCAST_RPC: collective(%p) cleanup", coll_req);
/* release margo resources */
if (HG_HANDLE_NULL != coll_req->progress_hdl) {
if (MARGO_REQUEST_NULL != coll_req->progress_req) {
margo_wait(coll_req->progress_req);
}
margo_destroy(coll_req->progress_hdl);
}
if (HG_HANDLE_NULL != coll_req->resp_hdl) {
if (NULL != coll_req->input) {
coll_restore_input_bulk(coll_req);
margo_free_input(coll_req->resp_hdl, coll_req->input);
}
margo_destroy(coll_req->resp_hdl);
}
if (HG_BULK_NULL != coll_req->bulk_forward) {
margo_bulk_free(coll_req->bulk_forward);
}
/* Release the Argobots mutex and condition variable */
ABT_cond_free(&coll_req->resp_valid_cond);
ABT_mutex_free(&coll_req->resp_valid_sync);
/* free allocated memory */
if (NULL != coll_req->input) {
free(coll_req->input);
}
if (NULL != coll_req->output) {
free(coll_req->output);
}
if (NULL != coll_req->child_hdls) {
free(coll_req->child_hdls);
}
if (NULL != coll_req->child_reqs) {
free(coll_req->child_reqs);
}
if (NULL != coll_req->bulk_buf) {
free(coll_req->bulk_buf);
}
/* release communication tree resources */
unifyfs_tree_free(&(coll_req->tree));
memset(coll_req, 0, sizeof(*coll_req));
free(coll_req);
}
/* Forward the collective request to any children */
static int collective_forward(coll_request* coll_req)
{
/* get info for tree */
int child_count = coll_req->tree.child_count;
if (0 == child_count) {
return UNIFYFS_SUCCESS;
}
LOGDBG("BCAST_RPC: collective(%p) forward", coll_req);
/* forward request down the tree */
int ret = UNIFYFS_SUCCESS;
for (int i = 0; i < child_count; i++) {
/* invoke bcast request rpc on child */
margo_request* creq = coll_req->child_reqs + i;
hg_handle_t* chdl = coll_req->child_hdls + i;
LOGDBG("BCAST_RPC: collective(%p) forwarding to child[%d]",
coll_req, i);
int rc = forward_child_request(coll_req->input, *chdl, creq);
if (rc != UNIFYFS_SUCCESS) {
LOGERR("forward to child[%d] failed", i);
ret = rc;
}
}
return ret;
}
/* set collective output return value to local result value */
void collective_set_local_retval(coll_request* coll_req, int val)
{
/* update collective return value using local op return value */
void* output = coll_req->output;
if (NULL == output) {
return;
}
switch (coll_req->req_type) {
case UNIFYFS_SERVER_BCAST_RPC_BOOTSTRAP: {
bootstrap_complete_bcast_out_t* bbo =
(bootstrap_complete_bcast_out_t*) output;
bbo->ret = val;
break;
}
case UNIFYFS_SERVER_BCAST_RPC_EXTENTS: {
extent_bcast_out_t* ebo = (extent_bcast_out_t*) output;
ebo->ret = val;
break;
}
case UNIFYFS_SERVER_BCAST_RPC_FILEATTR: {
fileattr_bcast_out_t* fbo = (fileattr_bcast_out_t*) output;
fbo->ret = val;
break;
}
case UNIFYFS_SERVER_BCAST_RPC_LAMINATE: {
laminate_bcast_out_t* lbo = (laminate_bcast_out_t*) output;
lbo->ret = val;
break;
}
case UNIFYFS_SERVER_BCAST_RPC_TRANSFER: {
transfer_bcast_out_t* tbo = (transfer_bcast_out_t*) output;
tbo->ret = val;
break;
}
case UNIFYFS_SERVER_BCAST_RPC_TRUNCATE: {
truncate_bcast_out_t* tbo = (truncate_bcast_out_t*) output;
tbo->ret = val;
break;
}
case UNIFYFS_SERVER_BCAST_RPC_UNLINK: {
unlink_bcast_out_t* ubo = (unlink_bcast_out_t*) output;
ubo->ret = val;
break;
}
case UNIFYFS_SERVER_BCAST_RPC_METAGET: {
metaget_all_bcast_out_t* mabo = (metaget_all_bcast_out_t*) output;
mabo->ret = val;
}
default:
LOGERR("invalid collective request type %d", coll_req->req_type);
break;
}
}
/* finish collective process by waiting for any child responses and
* sending parent response (if applicable) */
int collective_finish(coll_request* coll_req)
{
int ret = UNIFYFS_SUCCESS;
LOGDBG("BCAST_RPC: collective(%p) finish", coll_req);
/* wait for responses from children */
int child_count = coll_req->tree.child_count;
int rc = wait_for_all_child_requests(coll_req, child_count);
if (rc != UNIFYFS_SUCCESS) {
ret = rc;
}
/* If there's output data AND there's a caller to send it back to,
* then send the output back to the caller. If we're at the root
* of the tree, though, there might be output data, but no place
* to send it. */
if ((NULL != coll_req->output) && (HG_HANDLE_NULL != coll_req->resp_hdl)) {
hg_return_t hret = margo_respond(coll_req->resp_hdl, coll_req->output);
if (hret != HG_SUCCESS) {
LOGERR("margo_respond() failed - %s", HG_Error_to_string(hret));
}
LOGDBG("BCAST_RPC: collective(%p, op=%d) responded",
coll_req, (int)(coll_req->req_type));
}
/* Signal the condition variable in case there are other threads
* waiting for the child responses */
ABT_mutex_lock(coll_req->resp_valid_sync);
ABT_cond_signal(coll_req->resp_valid_cond);
/* There should only be a single thread waiting on the CV, so we don't
* need to use ABT_cond_broadcast() */
ABT_mutex_unlock(coll_req->resp_valid_sync);
/* Locking the mutex before signaling is required in order to ensure
* that the waiting thread has had a chance to actually call
* ABT_cond_timedwait() before this thread signals the CV. */
return ret;
}
/*************************************************************************
* Broadcast progress via ULT
*************************************************************************/
int invoke_bcast_progress_rpc(coll_request* coll_req)
{
int ret = UNIFYFS_SUCCESS;
/* get address for local server rank */
hg_addr_t addr = get_margo_server_address(glb_pmi_rank);
if (HG_ADDR_NULL == addr) {
LOGERR("missing local margo address");
return UNIFYFS_ERROR_MARGO;
}
/* get handle to local rpc function */
hg_id_t hgid = unifyfsd_rpc_context->rpcs.bcast_progress_id;
hg_return_t hret = margo_create(unifyfsd_rpc_context->svr_mid, addr,
hgid, &(coll_req->progress_hdl));
if (hret != HG_SUCCESS) {
LOGERR("failed to get handle for bcast progress - %s",
HG_Error_to_string(hret));
ret = UNIFYFS_ERROR_MARGO;
} else {
/* call local rpc function, which allows progress to be handled
* by a ULT */
bcast_progress_in_t in;
in.coll_req = (hg_ptr_t) coll_req;
hret = margo_iforward(coll_req->progress_hdl, &in,
&(coll_req->progress_req));
if (hret != HG_SUCCESS) {
LOGERR("failed to forward bcast progress for coll(%p) - %s",
HG_Error_to_string(hret), coll_req);
ret = UNIFYFS_ERROR_MARGO;
}
}
return ret;
}
/* generic broadcast rpc progression handler */
static void bcast_progress_rpc(hg_handle_t handle)
{
/* assume we'll succeed */
int ret = UNIFYFS_SUCCESS;
coll_request* coll = NULL;
bool cleanup_collective = false;
bcast_progress_in_t in;
hg_return_t hret = margo_get_input(handle, &in);
if (hret != HG_SUCCESS) {
LOGERR("margo_get_input() failed - %s", HG_Error_to_string(hret));
ret = UNIFYFS_ERROR_MARGO;
} else {
coll = (coll_request*) in.coll_req;
margo_free_input(handle, &in);
cleanup_collective = ((NULL != coll) && (coll->auto_cleanup));
/* We have to check the auto_cleanup variable now because in the case
* where auto_cleanup is false, another thread will be freeing the
* collective. And once the memory is freed, we can't read the
* auto_cleanup variable.
*
* There's a condition variable that's signaled by collective_finish(),
* and the memory won't be freed until some time after that happens, so
* it's safe to check the variable up here. */
/* call collective_finish() to progress bcast operation */
LOGDBG("BCAST_RPC: bcast progress collective(%p)", coll);
ret = collective_finish(coll);
if (ret != UNIFYFS_SUCCESS) {
LOGERR("collective_finish() failed for collective(%p) (rc=%d)",
coll, ret);
}
}
/* finish rpc */
bcast_progress_out_t out;
out.ret = (int32_t) ret;
hret = margo_respond(handle, &out);
if (hret != HG_SUCCESS) {
LOGERR("margo_respond() failed - %s", HG_Error_to_string(hret));
}
if (cleanup_collective) {
collective_cleanup(coll);
}
/* free margo resources */
margo_destroy(handle);
}
DEFINE_MARGO_RPC_HANDLER(bcast_progress_rpc)
/***************************************************
* Broadcast server bootstrap completion
***************************************************/
/* bootstrap complete broadcast rpc handler */
static void bootstrap_complete_bcast_rpc(hg_handle_t handle)
{
LOGDBG("BCAST_RPC: bootstrap handler");
/* assume we'll succeed */
int ret = UNIFYFS_SUCCESS;
coll_request* coll = NULL;
server_rpc_req_t* req = calloc(1, sizeof(*req));
bootstrap_complete_bcast_in_t* in = calloc(1, sizeof(*in));
bootstrap_complete_bcast_out_t* out = calloc(1, sizeof(*out));
if ((NULL == req) || (NULL == in) || (NULL == out)) {
ret = ENOMEM;
} else {
/* get input params */
hg_return_t hret = margo_get_input(handle, in);
if (hret != HG_SUCCESS) {
LOGERR("margo_get_input() failed - %s", HG_Error_to_string(hret));
ret = UNIFYFS_ERROR_MARGO;
} else {
hg_id_t op_hgid =
unifyfsd_rpc_context->rpcs.bootstrap_complete_bcast_id;
server_rpc_e rpc = UNIFYFS_SERVER_BCAST_RPC_BOOTSTRAP;
coll = collective_create(rpc, handle, op_hgid, (int)(in->root),
(void*)in, (void*)out, sizeof(*out),
HG_BULK_NULL, HG_BULK_NULL, NULL);
if (NULL == coll) {
ret = ENOMEM;
} else {
ret = collective_forward(coll);
if (ret == UNIFYFS_SUCCESS) {
req->req_type = rpc;
req->coll = coll;
req->handle = handle;
req->input = (void*) in;
ret = sm_submit_service_request(req);
if (ret != UNIFYFS_SUCCESS) {
LOGERR("failed to submit coll request to svcmgr");
}
}
}
}
}
if (ret != UNIFYFS_SUCCESS) {
/* report failure back to caller */
bootstrap_complete_bcast_out_t bbo;
bbo.ret = (int32_t)ret;
hg_return_t hret = margo_respond(handle, &bbo);
if (hret != HG_SUCCESS) {
LOGERR("margo_respond() failed - %s", HG_Error_to_string(hret));
}
if (NULL != coll) {
collective_cleanup(coll);
} else {
margo_destroy(handle);
}
}
}
DEFINE_MARGO_RPC_HANDLER(bootstrap_complete_bcast_rpc)
/* Execute broadcast tree for 'bootstrap complete' notification */
int unifyfs_invoke_broadcast_bootstrap_complete(void)
{
/* assuming success */
int ret = UNIFYFS_SUCCESS;
LOGDBG("BCAST_RPC: starting bootstrap complete");
coll_request* coll = NULL;
bootstrap_complete_bcast_in_t* in = calloc(1, sizeof(*in));
if (NULL == in) {
ret = ENOMEM;
} else {
/* set input params */
in->root = (int32_t) glb_pmi_rank;
hg_id_t op_hgid =
unifyfsd_rpc_context->rpcs.bootstrap_complete_bcast_id;
server_rpc_e rpc = UNIFYFS_SERVER_BCAST_RPC_BOOTSTRAP;
coll = collective_create(rpc, HG_HANDLE_NULL, op_hgid,
glb_pmi_rank, (void*)in,
NULL, sizeof(bootstrap_complete_bcast_out_t),
HG_BULK_NULL, HG_BULK_NULL, NULL);
if (NULL == coll) {
ret = ENOMEM;
} else {
ret = collective_forward(coll);
if (ret == UNIFYFS_SUCCESS) {
/* avoid cleanup by the progress rpc */
coll->auto_cleanup = 0;
ABT_mutex_lock(coll->resp_valid_sync);
ret = invoke_bcast_progress_rpc(coll);
if (ret == UNIFYFS_SUCCESS) {
/* wait for all the child responses to come back */
struct timespec timeout;
clock_gettime(CLOCK_REALTIME, &timeout);
timeout.tv_sec += 5; /* 5 sec */
int rc = ABT_cond_timedwait(coll->resp_valid_cond,
coll->resp_valid_sync,
&timeout);
if (ABT_ERR_COND_TIMEDOUT == rc) {
LOGERR("timeout");
ret = UNIFYFS_ERROR_TIMEOUT;
} else if (rc) {
LOGERR("failed to wait on condition (err=%d)", rc);
ret = UNIFYFS_ERROR_MARGO;
} else if (NULL != coll->output) {
bootstrap_complete_bcast_out_t* out =
(bootstrap_complete_bcast_out_t*) coll->output;
ret = out->ret;
}
}
ABT_mutex_unlock(coll->resp_valid_sync);
} else {
LOGERR("collective(%p) forward failed - cleaning up", coll);
}
collective_cleanup(coll);
}
}
return ret;
}
/*************************************************************************
* Broadcast file extents metadata
*************************************************************************/
/* file extents metadata broadcast rpc handler */
static void extent_bcast_rpc(hg_handle_t handle)
{