-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmod_sci.c
More file actions
4046 lines (3525 loc) · 133 KB
/
mod_sci.c
File metadata and controls
4046 lines (3525 loc) · 133 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
/*
* Copyright (c) 2009 The Trustees of Indiana University and Indiana
* University Research and Technology
* Corporation. All rights reserved.
*
* Author(s): Torsten Hoefler <htor@cs.indiana.edu>
*
*/
#include "netgauge.h"
#if defined NG_SISCI && defined NG_MOD_SCI
#include "sisci_api.h"
#include <string.h>
/*
* Macro Definitions
*/
#define NO_FLAGS 0
#define NO_CALLBACK 0
#define NO_ARG 0
#define NO_OFFSET 0
#define NO_SUGGESTED_ADDRESS 0
#define ADAPTER_NO 0
#define SEG_ID 0
#define SEG_ID_DMA 1
#define NUM_LOCAL_SEGMENTS 2 // # of local SCI segments
#define DEFAULT_LOCAL_SEGMENT_SIZE 1*1024*1024 // 1 MB
#define TIMEOUT SCI_INFINITE_TIMEOUT
#define TRIES 4 // # attempts to connect to a remote segment
#define SCI_RELIABLE 1 // Reliable data transfer?
#define LOCAL_MEMCPY 1 // Copy local memory when necessary?
#define MAX_NUM_DMA_QUEUE_ENTRIES 4
#define MIN_NUM_DMA_OFFSET_ALIGNMENTS 3 // Minimum number of how many
// "private_data.info_dma_offset_alignment"s
// a mapped remote segment must consist of
/*
* Type Definitions
*/
/* Queue for data transfer requests */
typedef struct queue {
struct queue_element *head;
struct queue_element *tail;
struct queue *next;
} Queue;
/* Queue element */
typedef struct queue_element {
struct queue_element *next;
struct queue *queue;
/*
* Queue specific function to forward the request
* such a queue element stands for via DMA
* (Direct Memory Access)
*/
int (*forward_via_DMA)(struct queue_element *element);
/*
* Queue specific function to forward the request
* such a queue element stands for via PIO
* (Programmed I/O)
*/
int (*forward_via_PIO)(struct queue_element *element);
/* Yields if the request is finished or not */
int (*done)(struct queue_element *element);
/* Frees the request's memory (destroys it) */
void (*destroy)(struct queue_element *element);
/* Queue specific data element */
void *data;
} Queue_element;
/* Send queue data element */
typedef struct sendq_data_element {
int dst;
int size;
void *buffer;
int payload_transfer_size; // Size of the actual payload of next transfer
int DMA_transfer_size; // Size of next transfer
char DMA_buffer_sent; // Flag, indicating: DMA buffer was sent
char DMA_buffer_to_send; // Flag, indicating: DMA buffer needs to be sent
char done; // Flag, set if request done (for debugging)
} Sendq_data_element;
/* Receive queue data element */
typedef struct recvq_data_element {
int src;
int size;
void *buffer;
char done; // Flag, set if request done (for debugging)
} Recvq_data_element;
/* Node in the SCI network */
typedef struct node {
/* Handles to remote SCI resources */
sci_desc_t v_dev; // virtual SCI device to remote segment
sci_remote_segment_t remote_seg_res;
sci_sequence_t remote_seg_seq;
sci_map_t remote_map;
/* Handle to DMA queue */
sci_dma_queue_t dma_queue;
/* SCI node ID of the remote node */
int sci_id;
/* Pointers to local buffers of the protocol */
void *local_datab_addr;
void *local_idb_addr;
void *local_ackb_addr;
/* Pointer to local DMA buffer */
void *local_dmab_addr;
/* Pointers to remote buffers of the protocol */
void *remote_datab_addr;
void *remote_idb_addr;
void *remote_ackb_addr;
volatile void *remote_seg_addr; // 'volatile' according to SISCI API
/* Offset into the remote segment */
int remote_seg_offset;
/* Status information for the protocol */
u_int8_t next_id_to_send;
u_int8_t next_ack_to_recv;
u_int8_t next_id_to_recv;
Queue send_queue;
Queue recv_queue;
} Node;
/* Module private data */
typedef struct private_data {
/* Handles to local SCI resources */
sci_desc_t v_dev; // virtual SCI device to local segment
sci_local_segment_t local_seg_res;
sci_map_t local_map;
void *local_seg_addr;
/* Handles to local SCI DMA resources */
sci_desc_t v_dev_dma; // virtual SCI device to local segment
// used for DMA
sci_local_segment_t local_seg_res_dma;
sci_map_t local_map_dma;
void *local_seg_addr_dma;
/* Info about the underlying SCI System */
int info_dma_size_alignment;
int info_dma_offset_alignment;
int info_dma_mtu;
int info_suggested_min_dma_size;
int info_suggested_min_block_size;
/* Size of a remote segment mapped into addressable space */
int remote_seg_mapped_size;
/*
* Pointer to array with information for
* communicating with every node in the SCI network
*/
Node *node;
/* Information about MPI_COMM_WORLD */
int worldrank;
int worldsize;
/* Command line options */
int opt_local_seg_size;
/*
* This pointer is assigned to the
* "forward_via_PIO" variable in
* send request structures and determines
* the type of PIO data transfers
*/
int (*opt_pio_mode)(Queue_element *element);
/* The protocol's MTU */
int protocol_mtu;
} Private_data;
/*
* Function Declarations
*/
static inline int my_getopt(int argc, char **argv, struct ng_options *global_opts);
static inline int my_init(struct ng_options *global_opts);
static inline void my_shutdown(struct ng_options *global_opts);
static inline void my_usage(void);
/* Blocking sends/recv */
static inline int sendto_via_SCIMemCpy(int dst, void *buffer, int size);
static inline int sendto_via_SCIMemWrite(int dst, void *buffer, int size);
static inline int sendto_via_SCITransferBlock(int dst, void *buffer, int size);
static inline int sendto_via_memcpy(int dst, void *buffer, int size);
static inline int my_recvfrom(int src, void *buffer, int size);
/* Non-blocking send/recv */
static inline int my_isendto(int dst, void *buffer, int size, NG_Request *req);
static inline int my_irecvfrom(int src, void *buffer, int size, NG_Request *req);
static inline int test_via_DMA(NG_Request *req);
static inline int test_via_PIO(NG_Request *req);
/* Additional functions */
/*
* ONLY apply forward_sendto/recvfrom*() to the first request in a queue
* because otherwise a request could be finished
* or send data out of order
*/
static inline int forward_sendto_via_DMA(Queue_element *element);
static inline int forward_sendto_via_SCIMemCpy(Queue_element *element);
static inline int forward_sendto_via_SCIMemWrite(Queue_element *element);
static inline int forward_sendto_via_SCITransferBlock(Queue_element *element);
static inline int forward_sendto_via_memcpy(Queue_element *element);
static inline int forward_recvfrom(Queue_element *element);
static inline void forward_any_via_PIO(Queue *queue);
static inline int assure_reliable_memcpy_transfer(int dst, void *dst_buf, void *src_buf, int size);
static inline int assure_reliable_SCIMemWrite_transfer(int dst, void *dst_buf, void *src_buf, int size);
static inline int assure_reliable_SCITransferBlock_transfer(int dst, void *dst_buf, void *src_buf, int size);
static inline Queue_element *get_next_non_empty_queues_head(Queue_element *element);
static inline Queue *get_next_non_empty_queue(Queue *queue);
static inline void enqueue(Queue *queue, Queue_element *element);
static inline void dequeue(Queue *queue);
static inline int done(Queue_element *element);
static inline void destroy(Queue_element *element);
static inline char *get_sci_error(int error);
/*
* Global Variables
*/
/* Module registration data structure */
static struct ng_module sci_module = {
.name = "sci",
.desc = "Mode sci uses the SISCI API for distributed shared memory "
"data transmission.",
.max_datasize = -1, /* Can send data of arbitrary size */
.headerlen = 0, /* No extra space needed for header */
.flags = NG_MOD_MEMREG | NG_MOD_RELIABLE | NG_MOD_CHANNEL,
.malloc = NULL, /* implement */
.getopt = my_getopt,
.init = my_init,
.shutdown = my_shutdown,
.usage = my_usage,
.sendto = NULL,
.recvfrom = my_recvfrom,
.isendto = my_isendto,
.irecvfrom = my_irecvfrom,
.test = test_via_DMA,
};
/* Module's private data */
static Private_data private_data = {0};
/*
* Function Definitions
*/
static inline int my_getopt (int argc, char **argv, struct ng_options *global_opts) {
int c;
char *optstring = NG_OPTION_STRING "M:P:"; // Additional module options
extern char *optarg;
extern int optind, opterr, optopt;
int failure = 0;
int memsize_available_set = 0;
int pio_mode_set = 0;
/*
* SCI can only be used with MPI support enabled.
* MPI is used, among others, to communicate the SCI node IDs.
*/
if (!global_opts->mpi) {
ng_error("Mode %s requires the global option '-p' (execution via MPI)",
global_opts->mode);
failure = 1;
}
while (!failure && ((c = getopt(argc, argv, optstring)) >= 0)) {
switch (c) {
case '?':
/*
* Unrecognized or badly used option
*/
// Unrecognized option
if (!strchr(optstring, optopt)) {
continue;
}
// Badly used option
ng_error("Option '-%c' requires an argument", optopt);
failure = 1;
break;
case 'M':
/*
* Amount of memory available for
* creating local SCI segments
*/
// Size of one local segment
private_data.opt_local_seg_size = atoi(optarg) / NUM_LOCAL_SEGMENTS;
memsize_available_set = 1;
break;
case 'P':
/*
* PIO (Programmed I/O) mode
* for PIO data transmissions
*/
// SCIMemCpy
if (!strcmp("SCIMemCpy", optarg)) {
sci_module.sendto = sendto_via_SCIMemCpy;
private_data.opt_pio_mode = forward_sendto_via_SCIMemCpy;
pio_mode_set = 1;
}
// SCIMemWrite
else if (!strcmp("SCIMemWrite", optarg)) {
sci_module.sendto = sendto_via_SCIMemWrite;
private_data.opt_pio_mode = forward_sendto_via_SCIMemWrite;
pio_mode_set = 1;
}
// SCITransferBlock
else if (!strcmp("SCITransferBlock", optarg)) {
sci_module.sendto = sendto_via_SCITransferBlock;
private_data.opt_pio_mode = forward_sendto_via_SCITransferBlock;
pio_mode_set = 1;
}
// memcpy
else if (!strcmp("memcpy", optarg)) {
sci_module.sendto = sendto_via_memcpy;
private_data.opt_pio_mode = forward_sendto_via_memcpy;
pio_mode_set = 1;
}
else {
ng_error("Only 'SCIMemCpy', 'SCIMemWrite', 'SCITranferBlock'");
ng_error("or 'memcpy' are possible arguments for option '-P'");
failure = 1;
}
break;
} // End switch
} // End while
/*
* Set default values where necessary
*/
if (!failure) {
if (!memsize_available_set) {
private_data.opt_local_seg_size = DEFAULT_LOCAL_SEGMENT_SIZE;
ng_info(NG_VLEV1, "SCI INFO: Amount of memory available for creating local SCI");
ng_info(NG_VLEV1, "SCI INFO: segments not specified - defaulting to: %u B",
DEFAULT_LOCAL_SEGMENT_SIZE * NUM_LOCAL_SEGMENTS);
}
if (!pio_mode_set) {
sci_module.sendto = sendto_via_SCIMemCpy;
private_data.opt_pio_mode = forward_sendto_via_SCIMemCpy;
ng_info(NG_VLEV1, "SCI INFO: PIO (Programmed I/O) mode for PIO data transmissions");
ng_info(NG_VLEV1, "SCI INFO: not specified - defaulting to 'SCIMemCpy'");
}
}
/* Report success or failure */
return failure;
}
static inline int my_init (struct ng_options *global_opts) {
sci_error_t error;
sci_query_adapter_t query;
int recvcounts[global_opts->mpi_opts->worldsize];
int displs[global_opts->mpi_opts->worldsize];
int i, num1, num2, tmp;
/* Get info about MPI_COMM_WORLD */
private_data.worldrank = global_opts->mpi_opts->worldrank;
private_data.worldsize = global_opts->mpi_opts->worldsize;
/* Allocate memory for array with information for communicating with every SCI node */
private_data.node = (Node *) calloc (private_data.worldsize, sizeof (Node));
if (private_data.node == NULL) {
ng_error ("Could not allocate %d B for node array",
private_data.worldsize * sizeof (Node));
return 1;
}
/*
* Init some data in the array allocated above
*/
for (i = 0; i < private_data.worldsize; i++) {
/* Info specific to the protocol implemented */
private_data.node[i].next_id_to_send = 1;
private_data.node[i].next_id_to_recv = 1;
private_data.node[i].next_ack_to_recv = 0;
/* Build the ring of send and receive queues */
// Not my node id?
if (i != private_data.worldrank) {
/*
* Pointer from send to receive
* queue of the same node
*/
private_data.node[i].send_queue.next =
&private_data.node[i].recv_queue;
if ((i+1) % private_data.worldsize ==
private_data.worldrank) {
tmp = (i+2) % private_data.worldsize;
}
else {
tmp = (i+1) % private_data.worldsize;
}
/*
* Pointer from receive queue to the
* next valid send queue
*/
private_data.node[i].recv_queue.next =
&private_data.node[tmp].send_queue;
}
}
/* Init the SCI environment */
SCIInitialize (NO_FLAGS, &error);
if (error != SCI_ERR_OK) {
ng_error ("SCIInitialize() failed with error: %s",
get_sci_error (error));
return 1;
}
// Open virtual SCI device for local segment
SCIOpen (&private_data.v_dev, NO_FLAGS, &error);
if (error != SCI_ERR_OK) {
ng_error ("SCIOpen() for local device failed with error: %s",
get_sci_error (error));
return 1;
}
// Open virtual SCI device for local DMA segment
SCIOpen (&private_data.v_dev_dma, NO_FLAGS, &error);
if (error != SCI_ERR_OK) {
ng_error ("SCIOpen() for local DMA device failed with error: %s",
get_sci_error (error));
return 1;
}
// Open virtual SCI devices for remote segments from all SCI nodes
for (i = 0; i < private_data.worldsize; i++) {
// Not my node id?
if (i != private_data.worldrank) {
SCIOpen (&private_data.node[i].v_dev, NO_FLAGS, &error);
if (error != SCI_ERR_OK) {
ng_error("SCIOpen() for remote device failed with error: %s",
get_sci_error (error));
return 1;
}
}
}
/* Get info about the underlying SCI System */
query.localAdapterNo = ADAPTER_NO;
query.subcommand = SCI_Q_ADAPTER_NODEID;
query.data = &private_data.node[private_data.worldrank].sci_id;
// Get my SCI node ID
query.subcommand = SCI_Q_ADAPTER_NODEID;
SCIQuery (SCI_Q_ADAPTER, &query, NO_FLAGS, &error);
if (error != SCI_ERR_OK) {
ng_error ("SCIQuery() for SCI node id failed with error: %s",
get_sci_error (error));
return 1;
}
// Get all SCI node IDs
for (i = 0; i < private_data.worldsize; i++) {
displs[i] = &private_data.node[i].sci_id - &private_data.node[0].sci_id;
recvcounts[i] = 1;
}
if (MPI_Allgatherv(&private_data.node[private_data.worldrank].sci_id, 1, MPI_INT,
&private_data.node[0].sci_id, recvcounts, displs, MPI_INT,
MPI_COMM_WORLD) != MPI_SUCCESS) {
ng_error("Could not send/receive my SCI node ID/the remaining SCI node IDs");
return 1;
}
// Get alignment requirement for a segment size
query.subcommand = SCI_Q_ADAPTER_DMA_SIZE_ALIGNMENT;
query.data = &private_data.info_dma_size_alignment;
SCIQuery (SCI_Q_ADAPTER, &query, NO_FLAGS, &error);
if (error != SCI_ERR_OK) {
ng_error("SCIQuery() for segment size alignment failed with error: %s",
get_sci_error (error));
return 1;
}
// Get alignment requirement for a segment offset
query.subcommand = SCI_Q_ADAPTER_DMA_OFFSET_ALIGNMENT;
query.data = &private_data.info_dma_offset_alignment;
SCIQuery (SCI_Q_ADAPTER, &query, NO_FLAGS, &error);
if (error != SCI_ERR_OK) {
ng_error("SCIQuery() for segment offset alignment failed with error: %s",
get_sci_error (error));
return 1;
}
// Get maximum unit size in bytes for a DMA transfer
query.subcommand = SCI_Q_ADAPTER_DMA_MTU;
query.data = &private_data.info_dma_mtu;
SCIQuery (SCI_Q_ADAPTER, &query, NO_FLAGS, &error);
if (error != SCI_ERR_OK) {
ng_error ("SCIQuery() for DMA MTU failed with error: %s",
get_sci_error (error));
return 1;
}
// Get suggested minimum size for a DMA transfer
query.subcommand = SCI_Q_ADAPTER_SUGGESTED_MIN_DMA_SIZE;
query.data = &private_data.info_suggested_min_dma_size;
SCIQuery (SCI_Q_ADAPTER, &query, NO_FLAGS, &error);
if (error != SCI_ERR_OK) {
ng_error ("SCIQuery() for suggested min. size of DMA transfer "
"failed with error: %s", get_sci_error (error));
return 1;
}
// Get suggested minimum size for a block transfer
query.subcommand = SCI_Q_ADAPTER_SUGGESTED_MIN_BLOCK_SIZE;
query.data = &private_data.info_suggested_min_block_size;
SCIQuery (SCI_Q_ADAPTER, &query, NO_FLAGS, &error);
if (error != SCI_ERR_OK) {
ng_error ("SCIQuery() for suggested min. size of block transfer "
"failed with error: %s", get_sci_error (error));
return 1;
}
/*
* Compute the protocol's characteristics (e.g. payload size)
* based on the SCI environment (e.g. alignment requirements,
* #nodes to communicate with, ...) and check if these
* characteristics meet the protocol's actual requirements
*/
// To be on the safe side regarding alignment constraints
if (private_data.info_dma_size_alignment !=
private_data.info_dma_offset_alignment) {
ng_error ("DMA size alignment != DMA offset alignment, "
"but the protocol implemented expects them to be equal.");
return 1;
}
//
// Compute the size of how much of a remote segment, depending on the
// local segment size and the number of nodes, is mapped into addressable space:
//
// 1. How often does "private_data.info_dma_offset_alignment" fit
// into the size of the local segment? (=num1)
// 2. How often does (#nodes - 1) fit into "num1"? (=num2)
// 3. The result is "num2" * "private_data.info_dma_offset_alignment"
// (=private_data.remote_seg_mapped_size)
//
num1 = private_data.opt_local_seg_size / private_data.info_dma_offset_alignment;
num2 = num1 / (private_data.worldsize - 1);
private_data.remote_seg_mapped_size = num2 * private_data.info_dma_offset_alignment;
//
// Check if the mapped remote segment size ("private_data.remote_seg_mapped_size")
// satisfies the protocol's minimum space requirements
//
if (num2 < MIN_NUM_DMA_OFFSET_ALIGNMENTS) {
ng_error ("Not enough space in the local segment to run the protocol implemented.");
ng_error ("Possible Solutions:");
ng_error ("-------------------");
ng_error ("1. Increase the local segment size (command line option '-M').");
ng_error ("2. Decrease the number of nodes taking part in the benchmark.");
return 1;
}
// Everything is all right
ng_info (NG_VLEV1, "SCI INFO: Size of a mapped remote segment: %d B",
private_data.remote_seg_mapped_size);
for (i = 0; i < private_data.worldsize; i++) {
// Not my node id?
if (i != private_data.worldrank) {
/* Compute the offset into each remote segment */
// My node id < node id to connect to?
if (private_data.worldrank < i) {
private_data.node[i].remote_seg_offset =
private_data.worldrank *
private_data.remote_seg_mapped_size;
}
// My node id > node id to connect to!
else {
private_data.node[i].remote_seg_offset =
(private_data.worldrank - 1) *
private_data.remote_seg_mapped_size;
}
}
}
//
// Compute the protocol's MTU:
// 2 stands for the following protocol buffers: id, ack
//
private_data.protocol_mtu = private_data.remote_seg_mapped_size -
2 * private_data.info_dma_offset_alignment;
#if 0
/* Print SCI node IDs */
for (i = 0; i < private_data.worldsize; i++) {
ng_info (NG_VNORM /* | NG_VPALL */ , "sci_id[%d]: %u",
i, private_data.node[i].sci_id);
}
#endif
/* Create local segment */
SCICreateSegment(private_data.v_dev, &private_data.local_seg_res, SEG_ID,
private_data.opt_local_seg_size, NO_CALLBACK, NO_ARG,
NO_FLAGS, &error);
if (error != SCI_ERR_OK) {
ng_error ("SCICreateSegment() failed with error: %s",
get_sci_error (error));
return 1;
}
/* Create local DMA segment */
SCICreateSegment(private_data.v_dev_dma, &private_data.local_seg_res_dma, SEG_ID_DMA,
private_data.opt_local_seg_size, NO_CALLBACK, NO_ARG,
NO_FLAGS, &error);
if (error != SCI_ERR_OK) {
ng_error ("SCICreateSegment() for local DMA segment failed with error: %s",
get_sci_error (error));
return 1;
}
/* Map local segment into addressable space */
private_data.local_seg_addr =
SCIMapLocalSegment(private_data.local_seg_res, &private_data.local_map,
NO_OFFSET, private_data.opt_local_seg_size,
NO_SUGGESTED_ADDRESS, NO_FLAGS, &error);
if (error != SCI_ERR_OK) {
ng_error ("SCIMapLocalSegment() failed with error: %s",
get_sci_error (error));
return 1;
}
/* Map local DMA segment into addressable space */
private_data.local_seg_addr_dma =
SCIMapLocalSegment(private_data.local_seg_res_dma, &private_data.local_map_dma,
NO_OFFSET, private_data.opt_local_seg_size,
NO_SUGGESTED_ADDRESS, NO_FLAGS, &error);
if (error != SCI_ERR_OK) {
ng_error ("SCIMapLocalSegment() for local DMA segment failed with error: %s",
get_sci_error (error));
return 1;
}
/* Init local segment appropriately */
memset (private_data.local_seg_addr, 0, private_data.opt_local_seg_size);
/* Init local DMA segment appropriately */
memset (private_data.local_seg_addr_dma, 0, private_data.opt_local_seg_size);
/* Set pointers according to the protocol to buffers in the local segment */
for (i = 0; i < private_data.worldsize; i++) {
// Not my node id?
if (i != private_data.worldrank) {
// Node id connecting to me < my node id?
if (i < private_data.worldrank) {
private_data.node[i].local_datab_addr =
(u_int8_t *) private_data.local_seg_addr +
(i * private_data.remote_seg_mapped_size);
}
// Node id connecting to me > my node id!
else {
private_data.node[i].local_datab_addr =
(u_int8_t *) private_data.local_seg_addr +
((i - 1) * private_data.remote_seg_mapped_size);
}
private_data.node[i].local_ackb_addr =
(u_int8_t *) private_data.node[i].local_datab_addr +
(private_data.remote_seg_mapped_size -
private_data.info_dma_offset_alignment);
private_data.node[i].local_idb_addr =
(u_int8_t *) private_data.node[i].local_ackb_addr -
private_data.info_dma_offset_alignment;
}
}
/* Set pointers to buffers in the local DMA segment */
for (i = 0; i < private_data.worldsize; i++) {
// Not my node id?
if (i != private_data.worldrank) {
// Node id connecting to me < my node id?
if (i < private_data.worldrank) {
private_data.node[i].local_dmab_addr =
(u_int8_t *) private_data.local_seg_addr_dma +
(i * private_data.remote_seg_mapped_size);
}
// Node id connecting to me > my node id!
else {
private_data.node[i].local_dmab_addr =
(u_int8_t *) private_data.local_seg_addr_dma +
((i - 1) * private_data.remote_seg_mapped_size);
}
}
}
/* Make local segment available to the SCI network */
SCIPrepareSegment(private_data.local_seg_res, ADAPTER_NO, NO_FLAGS,
&error);
if (error != SCI_ERR_OK) {
ng_error ("SCIPrepareSegment() failed with error: %s",
get_sci_error (error));
return 1;
}
SCISetSegmentAvailable(private_data.local_seg_res, ADAPTER_NO, NO_FLAGS,
&error);
if (error != SCI_ERR_OK) {
ng_error ("SCISetSegmentAvailable() failed with error: %s",
get_sci_error (error));
return 1;
}
/* Prepare local DMA segment for being available to the SCI adapter */
SCIPrepareSegment(private_data.local_seg_res_dma, ADAPTER_NO, NO_FLAGS,
&error);
if (error != SCI_ERR_OK) {
ng_error ("SCIPrepareSegment() for local DMA segment failed with error: %s",
get_sci_error (error));
return 1;
}
/* Create DMA queues to all nodes */
for (i = 0; i < private_data.worldsize; i++) {
// Not my node id?
if (i != private_data.worldrank) {
SCICreateDMAQueue(private_data.v_dev_dma,
&private_data.node[i].dma_queue,
ADAPTER_NO,
MAX_NUM_DMA_QUEUE_ENTRIES,
NO_FLAGS,
&error);
if (error != SCI_ERR_OK) {
ng_error("SCICreateDMAQueue() to node %d failed with error: %s",
i, get_sci_error (error));
return 1;
}
}
}
/* Wait until all nodes have made their segments available */
MPI_Barrier (MPI_COMM_WORLD);
return my_setup_channels();
}
static inline int my_setup_channels (void) {
sci_error_t error;
sci_sequence_status_t status;
int num_errors = 0;
int i, j;
/* Connect to all remote segments in the SCI network */
for (i = 0; i < private_data.worldsize; i++) {
// Not my node id?
if (i != private_data.worldrank) {
// Return an error after TRIES unsuccessful tries to connect
for (j = 0; j < TRIES; j++) {
sleep (j);
SCIConnectSegment(private_data.node[i].v_dev,
&private_data.node[i].remote_seg_res,
private_data.node[i].sci_id,
SEG_ID, ADAPTER_NO, NO_CALLBACK,
NO_ARG, TIMEOUT, NO_FLAGS, &error);
if (error == SCI_ERR_OK) {
// Leave this loop
break;
}
}
// Not connected?
if (error != SCI_ERR_OK) {
ng_error("SCIConnectSegment() to node %d failed with error: %s",
i, get_sci_error (error));
num_errors++;
}
else {
ng_info (NG_VLEV1 | NG_VPALL,
"SCI INFO: SCIGetRemoteSegmentSize() of node %d: %d B", i,
SCIGetRemoteSegmentSize (private_data.node[i].remote_seg_res));
}
}
}
// Any remote segments I could not connect to?
if (num_errors != 0) {
return 1;
}
/*
* Map only those parts of the remote segments
* into addressable space which are important to me
*/
for (i = 0; i < private_data.worldsize; i++) {
// Not my node id?
if (i != private_data.worldrank) {
private_data.node[i].remote_seg_addr =
SCIMapRemoteSegment(private_data.node[i].remote_seg_res,
&private_data.node[i].remote_map,
private_data.node[i].remote_seg_offset,
private_data.remote_seg_mapped_size,
NO_SUGGESTED_ADDRESS,
NO_FLAGS,
&error);
if (error != SCI_ERR_OK) {
ng_error ("SCIMapRemoteSegment() to node %d "
"failed with error: %s", i, get_sci_error (error));
return 1;
}
}
}
/* Set pointers according to the protocol to buffers in the remote segments */
for (i = 0; i < private_data.worldsize; i++) {
// Not my node id?
if (i != private_data.worldrank) {
private_data.node[i].remote_datab_addr =
(u_int8_t *) private_data.node[i].remote_seg_addr;
private_data.node[i].remote_ackb_addr =
(u_int8_t *) private_data.node[i].remote_datab_addr +
(private_data.remote_seg_mapped_size -
private_data.info_dma_offset_alignment);
private_data.node[i].remote_idb_addr =
(u_int8_t *) private_data.node[i].remote_ackb_addr -
private_data.info_dma_offset_alignment;
}
}
/*
* Create sequences for all remote segments
* NOTE: Remote segments must be mapped into addressable space, first.
*/
for (i = 0; i < private_data.worldsize; i++) {
// Not my node id?
if (i != private_data.worldrank) {
SCICreateMapSequence(private_data.node[i].remote_map,
&private_data.node[i].remote_seg_seq,
NO_FLAGS, &error);
if (error != SCI_ERR_OK) {
ng_error ("SCICreateMapSequence() to node %d "
"failed with error: %s", i, get_sci_error (error));
return 1;
}
}
}
/* Start sequences for all remote segments */
for (i = 0; i < private_data.worldsize; i++) {
// Not my node id?
if (i != private_data.worldrank) {
do {
status = SCIStartSequence (private_data.node[i].remote_seg_seq,
NO_FLAGS, &error);
if (error != SCI_ERR_OK) {
ng_error ("SCIStartSequence() to node %d "
"failed with error: %s",
i, get_sci_error (error));
}
} while (status != SCI_ERR_OK);
}
}
return 0;
}
static inline int sendto_via_SCIMemCpy(int dst, void *buffer, int size) {
int size_old = size;
int size_to_send = 0;
unsigned int alignment_displ, padding;
Queue_element *queue_element_ptr = NULL;
Sendq_data_element *data_ptr = NULL;
sci_error_t error;
void *src_buffer;
/* Send queue empty and allowed to send? */
if (!private_data.node[dst].send_queue.head &&
*((volatile u_int8_t *) private_data.node[dst].local_ackb_addr) ==
private_data.node[dst].next_ack_to_recv) {
/* Size of useful data to be sent */
size_to_send = min(size, private_data.protocol_mtu);
/* Check "size_to_send" for SCI size alignment requirements */
padding = 0;
alignment_displ = size_to_send % private_data.info_dma_size_alignment;
if (alignment_displ) {
padding = private_data.info_dma_size_alignment - alignment_displ;
}
/* Check for SCI size and offset alignment requirements */
if (padding ||
(unsigned long) buffer % (unsigned long) private_data.info_dma_offset_alignment) {
#if LOCAL_MEMCPY
/* Copy data to be sent to DMA buffer */
memcpy(private_data.node[dst].local_dmab_addr, buffer, size_to_send);
#endif
/* Buffer from which to copy is DMA buffer */
src_buffer = private_data.node[dst].local_dmab_addr;
}
else {
/* Buffer from which to copy is user buffer */
src_buffer = buffer;
}
/* Update next ACK to receive */
if (private_data.node[dst].next_ack_to_recv == 0)
private_data.node[dst].next_ack_to_recv = 1;
else
private_data.node[dst].next_ack_to_recv = 0;
/* Send data */
SCIMemCpy(private_data.node[dst].remote_seg_seq,
src_buffer,
private_data.node[dst].remote_map,
NO_OFFSET,
size_to_send + padding,
SCI_FLAG_ERROR_CHECK,
&error);
#if SCI_RELIABLE
/* Make sure that the DATA sent arrives */
while (error == SCI_ERR_TRANSFER_FAILED) {
SCIMemCpy(private_data.node[dst].remote_seg_seq,
src_buffer,
private_data.node[dst].remote_map,
NO_OFFSET,
size_to_send + padding,
SCI_FLAG_ERROR_CHECK,
&error);
}
#endif
if (error != SCI_ERR_OK) {
ng_error("SCIMemCpy() failed with error: %s",
get_sci_error (error));
return -1;
}
/* Send ID, notifying the receiver that new data has arrived */
*((volatile u_int8_t *) private_data.node[dst].remote_idb_addr) =
private_data.node[dst].next_id_to_send;
#if SCI_RELIABLE
/* Make sure that the ID sent arrives */
assure_reliable_memcpy_transfer(dst,
private_data.node[dst].remote_idb_addr,
&private_data.node[dst].next_id_to_send,
1);
#endif
/* Update next ID to send */
if (private_data.node[dst].next_id_to_send == 0)
private_data.node[dst].next_id_to_send = 1;
else
private_data.node[dst].next_id_to_send = 0;