-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSwitch.c
984 lines (749 loc) · 23.8 KB
/
Switch.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
#include "Switch.h"
#include "../include/rdtsc.h"
int producer_pid = 0;
int switch_pid = 0;
int switch_io_pid = 0;
int switch_io_port_pid = 0;
long long packet_id = 0;
int packets_sent = 0;
int packets_received = 0;
struct switch_t **__switch;
eventcount **switch_ec;
int main(void){
//user must initialize DESim
KnightSim_init();
switch_init();
switch_producer_init();
/*starts simulation and won't return until simulation
is complete or all contexts complete*/
printf("Start switch simulation %d network packets\n", NUMPACKETS * NUMSWITCHES);
simulate();
printf("End simulation, packets sent %d packets received %d cycles %llu\n",
packets_sent, packets_received, P_TIME);
return 1;
}
packet *switch_io_ctrl_get_packet(struct switch_t *switches, enum port_name port, enum switch_io_lane_map current_io_lane){
packet *net_packet = NULL;
switch(current_io_lane)
{
case io_request:
net_packet = (packet *)KnightSim_list_get(switches->tx_request_queues[port], 0);
break;
case io_reply:
net_packet = (packet *)KnightSim_list_get(switches->tx_reply_queues[port], 0);
break;
case io_invalid_lane:
default:
fatal("get_next_queue() Invalid port name\n");
break;
}
return net_packet;
}
list *switch_io_ctrl_get_tx_queue(struct switch_t *switches, enum port_name port, enum switch_io_lane_map current_io_lane){
list *tx_queue = NULL;
switch(current_io_lane)
{
case io_request:
tx_queue = switches->tx_request_queues[port];
break;
case io_reply:
tx_queue = switches->tx_reply_queues[port];
break;
case io_invalid_lane:
default:
fatal("get_next_queue() Invalid port name\n");
break;
}
return tx_queue;
}
enum switch_io_lane_map get_next_io_lane_rb(enum switch_io_lane_map current_io_lane){
enum switch_io_lane_map next_lane = io_invalid_lane;
switch(current_io_lane)
{
case io_request:
next_lane = io_reply;
break;
case io_reply:
next_lane = io_request;
break;
case io_invalid_lane:
default:
fatal("get_next_io_lane_rb() Invalid port name\n");
break;
}
assert(next_lane != io_invalid_lane);
return next_lane;
}
list *switch_io_get_next_rx_queue(struct switch_t *switches, enum port_name port, enum switch_io_lane_map current_io_lane){
list * next_rx_queue = NULL;
enum port_name next_queue;
next_queue = get_next_queue_reverse(port);
switch(next_queue)
{
case west_queue:
switch(current_io_lane)
{
case io_request:
next_rx_queue = switches->next_west_rx_request_queue;
break;
case io_reply:
next_rx_queue = switches->next_west_rx_reply_queue;
break;
default:
fatal("get_next_queue() Invalid port name\n");
break;
}
break;
case east_queue:
switch(current_io_lane)
{
case io_request:
next_rx_queue = switches->next_east_rx_request_queue;
break;
case io_reply:
next_rx_queue = switches->next_east_rx_reply_queue;
break;
default:
fatal("get_next_queue() Invalid port name\n");
break;
}
break;
case north_queue:
case south_queue:
case front_queue:
case back_queue:
case invalid_queue:
default:
fatal("get_next_queue() Invalid port name\n");
break;
}
assert(next_rx_queue);
return next_rx_queue;
}
void switch_io_ctrl(context * my_ctx){
int total_ports = NUMPORTS * NUMSWITCHES;
int switch_pid = switch_io_pid % NUMSWITCHES; //should give 0 - 31
int port_pid = switch_io_port_pid;
if(!((total_ports - 1 - switch_io_pid) % NUMSWITCHES))
switch_io_port_pid++;
switch_io_pid++;
//printf("s pid %d p pid %d\n", switch_pid, port_pid);
assert(switch_pid >= 0 && switch_pid < NUMSWITCHES);
assert(port_pid >= 0 && port_pid < NUMPORTS);
count_t step = 1;
//int i = 0;
//int num_packets = 0;
packet *net_packet = NULL;
int transfer_time = 0;
enum switch_io_lane_map current_lane = io_request;
//printf("switch_io_ctrl sw %d port %d init\n", switch_pid, port_pid);
context_init_halt(my_ctx);
while(1)
{
await(__switch[switch_pid]->switch_io_ec[port_pid], step, my_ctx);
//switch has been advanced
P_PAUSE(1, my_ctx);
net_packet = switch_io_ctrl_get_packet(__switch[switch_pid], (enum port_name)port_pid, current_lane);
if(!net_packet) //try the next lane...
{
current_lane = get_next_io_lane_rb(current_lane);
continue;
}
printf("switch_io_ctrl sw %d port %d advanced packet id %llu\n", switch_pid, port_pid, net_packet->id);
transfer_time = (net_packet->size/__switch[switch_pid]->bus_width);
if(transfer_time == 0)
transfer_time = 1;
/*north_queue
south_queue
front_queue
back_queue*/
if(port_pid == east_queue || port_pid == west_queue)
{
if(KnightSim_list_count(switch_io_get_next_rx_queue(__switch[switch_pid], (enum port_name)port_pid, current_lane)) >= MAXQUEUEDEPTH)
{
P_PAUSE(1, my_ctx);
}
else
{
step++;
P_PAUSE(transfer_time, my_ctx);
net_packet = (packet *)KnightSim_list_remove(switch_io_ctrl_get_tx_queue(__switch[switch_pid], (enum port_name)port_pid, current_lane), net_packet);
KnightSim_list_enqueue(switch_io_get_next_rx_queue(__switch[switch_pid], (enum port_name)port_pid, current_lane), net_packet);
if(port_pid == east_queue)
advance(switch_ec[__switch[switch_pid]->next_west], my_ctx);
else
advance(switch_ec[__switch[switch_pid]->next_east], my_ctx);
}
}
else
{
step++;
P_PAUSE(transfer_time, my_ctx);
packets_received++;
//destroy the packet
net_packet = (packet *)KnightSim_list_remove(switch_io_ctrl_get_tx_queue(__switch[switch_pid], (enum port_name)port_pid, current_lane), net_packet);
network_packet_destroy(net_packet);
}
current_lane = get_next_io_lane_rb(current_lane);
}
fatal("switch_io_ctrl(): exiting\n");
return;
}
void switch_ctrl(context * my_ctx){
int my_pid = switch_pid++;
count_t step = 1;
int i = 0;
packet *net_packet = NULL;
list *out_queue = NULL;
//packet *packet_ptr = NULL;
//printf("switch_ctrl %d init\n", my_pid);
context_init_halt(my_ctx);
while(1)
{
await(switch_ec[my_pid], step, my_ctx);
/*printf("ec count %llu step %llu\n", switch_ec->count, step);*/
/*wait and enter the subclock domain
to see if anyone else advance the switch this cycle*/
ENTER_SUB_CLOCK(my_ctx)
/*models a cross bar link as many inputs to outputs
as possible in a given cycle*/
switch_crossbar_link(__switch[my_pid]);
EXIT_SUB_CLOCK(my_ctx)
//charge latency
P_PAUSE(__switch[my_pid]->latency, my_ctx);
for(i = 0; i < __switch[my_pid]->crossbar->num_ports; i++)
{
if(crossbar_get_port_link_status(__switch[my_pid]) != invalid_queue)
{
/*the out queue is linked to an input queue and the output queue isn't full
move the packet from the input queue to the correct output queue*/
//careful here, the next line is for the INPUT queue...
net_packet = (packet *)KnightSim_list_remove_at(switch_get_rx_queue(__switch[my_pid], crossbar_get_port_link_status(__switch[my_pid])), 0);
assert(net_packet);
/*enum port_name{
north_queue = 0,
east_queue,
south_queue,
west_queue,
front_queue,
back_queue,
invalid_queue,
port_num
};*/
printf("switch %d routing packet id %llu dest %d dest port %d cycle %llu\n",
__switch[my_pid]->id, net_packet->id, net_packet->dest[0], net_packet->dest[1], P_TIME);
//get a pointer to the output queue
out_queue = switch_get_tx_queue(__switch[my_pid], __switch[my_pid]->crossbar->current_port);
KnightSim_list_enqueue(out_queue, net_packet);
advance(__switch[my_pid]->switch_io_ec[__switch[my_pid]->crossbar->current_port], my_ctx);
net_packet = NULL;
}
__switch[my_pid]->crossbar->current_port = get_next_queue_rb(__switch[my_pid]->crossbar->current_port);
}
step += __switch[my_pid]->crossbar->num_links;
//printf("formed links %d\n", __switch[my_pid]->crossbar->num_links);
//getchar();
//clear the current cross bar state
switch_crossbar_clear_state(__switch[my_pid]);
//set the next direction and lane to process
__switch[my_pid]->next_crossbar_lane = switch_get_next_crossbar_lane(__switch[my_pid]->next_crossbar_lane);
__switch[my_pid]->crossbar->current_port = get_next_queue_rb(__switch[my_pid]->crossbar->current_port);
__switch[my_pid]->current_queue = get_next_queue_rb(__switch[my_pid]->current_queue);
//set message_paket null
net_packet = NULL;
}
fatal("switch_ctrl(): outside of while loop\n");
return;
}
list *switch_get_tx_queue(struct switch_t *__switch, enum port_name queue){
struct list_t *switch_queue = NULL;
switch(__switch->next_crossbar_lane)
{
case crossbar_request:
switch_queue = __switch->tx_request_queues[queue];
break;
case crossbar_reply:
switch_queue = __switch->tx_reply_queues[queue];
break;
case crossbar_invalid_lane:
default:
fatal("switch_get_rx_packet() Invalid port name\n");
break;
}
assert(switch_queue != NULL);
return switch_queue;
}
list *switch_get_rx_queue(struct switch_t *__switch, enum port_name queue){
list *switch_queue = NULL;
switch(__switch->next_crossbar_lane)
{
case crossbar_request:
switch_queue = __switch->rx_request_queues[queue];
break;
case crossbar_reply:
switch_queue = __switch->rx_reply_queues[queue];
break;
case crossbar_invalid_lane:
default:
fatal("switch_get_rx_packet() Invalid port name\n");
break;
}
assert(switch_queue != NULL);
return switch_queue;
}
enum port_name crossbar_get_port_link_status(struct switch_t *__switch){
enum port_name port_status = invalid_queue;
/*check output link status*/
port_status = __switch->crossbar->out_port_linked_queues[__switch->crossbar->current_port];
return port_status;
}
enum switch_crossbar_lane_map switch_get_next_crossbar_lane(enum switch_crossbar_lane_map current_crossbar_lane){
enum switch_crossbar_lane_map next_lane = crossbar_invalid_lane;
switch(current_crossbar_lane)
{
case crossbar_request:
next_lane = crossbar_reply;
break;
case crossbar_reply:
next_lane = crossbar_request;
break;
case crossbar_invalid_lane:
default:
fatal("get_next_io_lane_rb() Invalid port name\n");
break;
}
assert(next_lane != crossbar_invalid_lane);
return next_lane;
}
void switch_crossbar_link(struct switch_t *__switch){
packet *net_packet = NULL;
enum port_name tx_port = invalid_queue;
int i = 0;
if(__switch->arb_style == round_robin)
{
/*printf("start_2 queue %d\n", switches->queue);*/
for(i = 0; i < __switch->crossbar->num_ports; i++)
{
//check for a waiting packet...
net_packet = switch_get_rx_packet(__switch);
//found a new packet, try to link it...
if(net_packet)
{
tx_port = switch_get_packet_route(__switch, net_packet);
//try to assign the link
switch_set_link(__switch, tx_port);
}
assert(__switch->current_queue >= 0 && __switch->current_queue < __switch->crossbar->num_ports);
//advance the queue pointer.
__switch->current_queue = get_next_queue_rb(__switch->current_queue);
}
}
else
{
fatal("switch_crossbar_link(): Invalid ARB set\n");
}
/*its possible to not be successful in a given cycle,
but should have no more than the number of ports on the switch.*/
assert((__switch->crossbar->num_links >= 0) && (__switch->crossbar->num_links <= __switch->crossbar->num_ports));
return;
}
crossbar *switch_crossbar_create(struct switch_t *__switch){
int i = 0;
crossbar *crossbar_ptr;
crossbar_ptr = (crossbar *) malloc(sizeof(crossbar));
//set up the cross bar variables
crossbar_ptr->num_ports = __switch->num_ports;
crossbar_ptr->num_links = 0;
crossbar_ptr->out_port_linked_queues = (enum port_name *) calloc(crossbar_ptr->num_ports, sizeof(enum port_name));
for(i = 0 ; i < crossbar_ptr->num_ports; i++)
{
crossbar_ptr->out_port_linked_queues[i] = invalid_queue;
}
return crossbar_ptr;
}
void switch_crossbar_clear_state(struct switch_t *__switch){
int i = 0;
//clear the cross bar state
__switch->crossbar->num_links = 0;
for(i = 0 ; i < __switch->crossbar->num_ports; i++)
{
__switch->crossbar->out_port_linked_queues[i] = invalid_queue;
}
return;
}
enum port_name get_next_queue_reverse(enum port_name queue){
enum port_name next_queue = invalid_queue;
switch(queue)
{
case west_queue:
next_queue = east_queue;
break;
case north_queue:
next_queue = south_queue;
break;
case east_queue:
next_queue = west_queue;
break;
case south_queue:
next_queue = north_queue;
break;
case front_queue:
next_queue = back_queue;
break;
case back_queue:
next_queue = front_queue;
break;
case invalid_queue:
default:
fatal("get_next_queue() Invalid port name\n");
break;
}
assert(next_queue != invalid_queue);
return next_queue;
}
enum port_name get_next_queue_rb(enum port_name queue){
enum port_name next_queue = invalid_queue;
switch(queue)
{
case west_queue:
next_queue = north_queue;
break;
case north_queue:
next_queue = east_queue;
break;
case east_queue:
next_queue = south_queue;
break;
case south_queue:
next_queue = front_queue;
break;
case front_queue:
next_queue = back_queue;
break;
case back_queue:
next_queue = west_queue;
break;
case invalid_queue:
default:
fatal("get_next_queue() Invalid port name\n");
break;
}
assert(next_queue != invalid_queue);
return next_queue;
}
void switch_set_link(struct switch_t *__switch, enum port_name tx_port){
/*we have the in port with switches->current_queue
* and the out port with tx_port try to link them...*/
/*we still a valid input port number switches->current_queue*/
//destination...
switch(__switch->next_crossbar_lane)
{
case crossbar_request: //if the dest port queue isn't full and it hasn't previously been linked then link it.
if((KnightSim_list_count(__switch->tx_request_queues[tx_port]) < MAXQUEUEDEPTH) && (__switch->crossbar->out_port_linked_queues[tx_port] == invalid_queue))
{
//links input port to output port
__switch->crossbar->out_port_linked_queues[tx_port] = __switch->current_queue;
__switch->crossbar->num_links++;
//printf("switch %d formed link\n", __switch->id);
}
break;
case crossbar_reply:
if((KnightSim_list_count(__switch->tx_reply_queues[tx_port]) < MAXQUEUEDEPTH) && (__switch->crossbar->out_port_linked_queues[tx_port] == invalid_queue))
{
__switch->crossbar->out_port_linked_queues[tx_port] = __switch->current_queue;
__switch->crossbar->num_links++;
//printf("switch %d formed link\n", __switch->id);
}
break;
case crossbar_invalid_lane:
default:
fatal("switch_get_rx_packet() Invalid port name\n");
break;
}
return;
}
//a real switch simulation would model a much more complicated routing algorithm here
enum port_name switch_get_packet_route(struct switch_t *__switch, packet *net_packet){
enum port_name tx_port = invalid_queue;
int x = 0;
int y = 0;
//determine if this is the dest switch
if(__switch->id == net_packet->dest[0])
{
assert(net_packet->dest[0] >= 0 && net_packet->dest[0] < NUMSWITCHES);
assert(net_packet->dest[1] != east_queue && net_packet->dest[1] != west_queue);
tx_port = (enum port_name)net_packet->dest[1];
}
else
{
//packet need to go to another switch
if(net_packet->direction)
{
/*if packet is already heading in one direction
send it along in the same direction*/
tx_port = (enum port_name)net_packet->direction;
}
else
{
/*determine the distance to the destination*/
if(net_packet->dest[0] > __switch->id)
{
x = net_packet->dest[0] - __switch->id;
y = NUMSWITCHES - x;
tx_port = east_queue;
net_packet->direction = east_queue;
assert(x + y == NUMSWITCHES);
}
else if(net_packet->dest[0] < __switch->id)
{
x = __switch->id - net_packet->dest[0];
y = NUMSWITCHES - x;
tx_port = west_queue;
net_packet->direction = west_queue;
assert(x + y == NUMSWITCHES);
}
/*printf("x + y %d\n", x + y);*/
//if the opposite direction is faster go that way
if(x < y)
{
if(tx_port == east_queue)
{
tx_port = west_queue;
net_packet->direction = west_queue;
}
else if(tx_port == west_queue)
{
tx_port = east_queue;
net_packet->direction = east_queue;
}
}
}
}
assert(tx_port >= 0 && tx_port < __switch->num_ports);
return tx_port;
}
packet *switch_get_rx_packet(struct switch_t *__switch){
packet *net_packet = NULL;
//sanity check
assert(__switch->current_queue >= 0 && __switch->current_queue < __switch->num_ports);
switch(__switch->next_crossbar_lane)
{
case crossbar_request:
net_packet = (packet *)KnightSim_list_get(__switch->rx_request_queues[__switch->current_queue], 0);
break;
case crossbar_reply:
net_packet = (packet *)KnightSim_list_get(__switch->rx_reply_queues[__switch->current_queue], 0);
break;
case crossbar_invalid_lane:
default:
fatal("switch_get_rx_packet() Invalid port name\n");
break;
}
return net_packet;
}
void switch_producer_ctrl(context * my_ctx){
int my_pid = producer_pid++;
int i = 0;
int type = 0;
int dest[2] = {0,0};
packet *packet_ptr = NULL;
context_init_halt(my_ctx);
//printf("producer_ctrl %d init\n", my_pid);
while(i < NUMPACKETS)
{
//create packet, keep trying to place into swith rx queue
type = rand() % 2; //random request or reply packet
dest[0] = rand() % NUMSWITCHES; //random destination
dest[1] = rand() % NUMPORTS;
packet_ptr = network_packet_create(type, dest); //create the packet and give it a size
//requests
if(packet_ptr->type == request)
{
while(KnightSim_list_count(__switch[my_pid]->rx_request_queues[north_queue]) >= MAXQUEUEDEPTH)
{
printf("producer_ctrl %d: stalling rx request queue size %d cycle %llu\n",
my_pid, KnightSim_list_count(__switch[my_pid]->rx_request_queues[north_queue]), P_TIME);
P_PAUSE(1, my_ctx);
}
//success clear the packet pointer
KnightSim_list_enqueue(__switch[my_pid]->rx_request_queues[north_queue], packet_ptr);
}
else if(packet_ptr->type == reply)
{
while(KnightSim_list_count(__switch[my_pid]->rx_reply_queues[north_queue]) >= MAXQUEUEDEPTH)
{
printf("producer_ctrl %d: stalling rx reply queue size %d cycle %llu\n",
my_pid, KnightSim_list_count(__switch[my_pid]->rx_reply_queues[north_queue]), P_TIME);
P_PAUSE(1, my_ctx);
}
//success clear the packet pointer
KnightSim_list_enqueue(__switch[my_pid]->rx_reply_queues[north_queue], packet_ptr);
}
packets_sent++;
printf("producer_ctrl %d: success queue size %d cycle %llu\n",
my_pid, KnightSim_list_count(__switch[my_pid]->rx_request_queues[north_queue]), P_TIME);
advance(switch_ec[my_pid], my_ctx); //wait for entire messages to transfer before signaling the switch
P_PAUSE(packet_ptr->size / 8, my_ctx); //transfer time
packet_ptr = NULL;
i++;
}
//This context finished its work, destroy
printf("producer terminating cycle %llu\n", P_TIME);
//getchar();
context_terminate(my_ctx);
return;
}
void network_packet_destroy(packet *net_packet){
free(net_packet);
return;
}
packet *network_packet_create(int type, int *dest){
packet *new_packet = NULL;
new_packet = (packet *) calloc((1), sizeof(packet));
assert(new_packet);
new_packet->id = packet_id++;
//if 1 make packet a request packet
new_packet->type = (enum type_name)type;
new_packet->dest[0] = dest[0];
new_packet->dest[1] = dest[1];
//if its east or west change it for example purposes
if(new_packet->dest[1] == east_queue || new_packet->dest[1] == west_queue)
new_packet->dest[1]++;
new_packet->direction = 0;
if(type == request)
new_packet->size = 8;
else
new_packet->size = 64;
return new_packet;
}
void switch_producer_init(void){
int i = 0;
char buff[100];
//for(i = 0; i < 1; i++)
for(i = 0; i < NUMSWITCHES; i++)
{
memset(buff,'\0' , 100);
snprintf(buff, 100, "producer_%d", i);
context_create(switch_producer_ctrl, DEFAULT_STACK_SIZE, strdup(buff), i);
}
srand(time(NULL));
return;
}
void switch_init(void){
switch_create();
switch_create_tasks();
switch_create_io_tasks();
return;
}
void switch_create_tasks(void){
char buff[100];
int i = 0;
//create the user defined eventcounts
switch_ec = (eventcount**) calloc(NUMSWITCHES, sizeof(eventcount*));
for(i = 0; i < NUMSWITCHES; i++)
{
memset(buff,'\0' , 100);
snprintf(buff, 100, "switch_ec_%d", i);
switch_ec[i] = eventcount_create(strdup(buff));
}
for(i = 0; i < NUMSWITCHES; i++)
{
memset(buff,'\0' , 100);
snprintf(buff, 100, "switch_ctrl_%d", i);
context_create(switch_ctrl, DEFAULT_STACK_SIZE, strdup(buff), i);
}
return;
}
void switch_create_io_tasks(void){
char buff[100];
int i = 0;
int j = 0;
//create the IO controllers for each switch
for(i = 0; i < NUMSWITCHES; i++)
{
__switch[i]->switch_io_ec = (eventcount**) calloc(NUMPORTS, sizeof(eventcount*));
for(j = 0; j < NUMPORTS; j++)
{
memset(buff,'\0' , 100);
snprintf(buff, 100, "switch_%d_io_ec_%d", __switch[i]->id, j);
__switch[i]->switch_io_ec[j] = eventcount_create(strdup(buff));
}
for(j = 0; j < NUMPORTS; j++)
{
memset(buff,'\0' , 100);
snprintf(buff, 100, "switch_%d_io_ctrl_%d", __switch[i]->id, j);
context_create(switch_io_ctrl, DEFAULT_STACK_SIZE, strdup(buff), j);
}
}
return;
}
void switch_create(void){
char buff[100];
int i = 0;
int j = 0;
//create the switch
__switch = (struct switch_t **) calloc(NUMSWITCHES, sizeof(struct switch_t*));
for(i = 0; i < NUMSWITCHES; i++)
{
__switch[i] = (struct switch_t *) malloc(sizeof(struct switch_t));
memset (buff,'\0' , 100);
snprintf(buff, 100, "switch[%d]", i);
__switch[i]->name = strdup(buff);
__switch[i]->id = i;
__switch[i]->num_ports = NUMPORTS;
__switch[i]->latency = SWITCHLATENCY;
__switch[i]->arb_style = round_robin;
__switch[i]->bus_width = BUSWIDTH;
//create switch virtual queues...
__switch[i]->rx_request_queues = (list **) calloc((__switch[i]->num_ports), sizeof(list *));
__switch[i]->rx_reply_queues = (list **) calloc((__switch[i]->num_ports), sizeof(list *));
__switch[i]->tx_request_queues = (list **) calloc((__switch[i]->num_ports), sizeof(list *));
__switch[i]->tx_reply_queues = (list **) calloc((__switch[i]->num_ports), sizeof(list *));
//allocate the queues...
for(j = 0; j < __switch[i]->num_ports; j++)
{
__switch[i]->rx_request_queues[j] = KnightSim_list_create(MAXQUEUEDEPTH);
__switch[i]->rx_reply_queues[j] = KnightSim_list_create(MAXQUEUEDEPTH);
__switch[i]->tx_request_queues[j] = KnightSim_list_create(MAXQUEUEDEPTH);
__switch[i]->tx_reply_queues[j] = KnightSim_list_create(MAXQUEUEDEPTH);
}
//create the crossbar
__switch[i]->crossbar = switch_crossbar_create(__switch[i]);
//init switch and crossbar pointers
__switch[i]->current_queue = west_queue;
__switch[i]->next_crossbar_lane = crossbar_request;
__switch[i]->north_next_io_lane = io_request;
__switch[i]->east_next_io_lane = io_request;
__switch[i]->south_next_io_lane = io_request;
__switch[i]->west_next_io_lane = io_request;
__switch[i]->crossbar->current_port = west_queue;
pthread_mutex_init(&__switch[i]->mutex, NULL);
}
//configure the ring bus
for (i = 0; i < NUMSWITCHES; i++)
{
__switch[i]->node_number = __switch[i]->id;
if(__switch[i]->id == 0)
{
//west queues
__switch[i]->next_west = NUMSWITCHES - 1;
__switch[i]->next_east = __switch[i]->id + 1;
}
else if(__switch[i]->id > 0 && __switch[i]->id < (NUMSWITCHES - 1))
{
__switch[i]->next_west = __switch[i]->id - 1;
__switch[i]->next_east = __switch[i]->id + 1;
}
else
{
assert(__switch[i]->id == (NUMSWITCHES - 1));
__switch[i]->next_west = __switch[i]->id - 1;
__switch[i]->next_east = 0;
}
__switch[i]->next_west_rx_request_queue = __switch[__switch[i]->next_west]->rx_request_queues[east_queue];
__switch[i]->next_west_rx_reply_queue = __switch[__switch[i]->next_west]->rx_reply_queues[east_queue];
__switch[i]->next_east_rx_request_queue = __switch[__switch[i]->next_east]->rx_request_queues[west_queue];
__switch[i]->next_east_rx_reply_queue = __switch[__switch[i]->next_east]->rx_reply_queues[west_queue];
}
return;
}