-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathINTRO.txt
854 lines (768 loc) · 35.4 KB
/
INTRO.txt
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
ADLB - The Asynchronous, Dynamic Load-Balancing Library
Many parallel programs consist of a large number of relatively independent
work units to be done by a (usually much smaller) number of processes.
In these cases there is little or no communication among the processes of
the sort that is found in tightly coupled applications like linear
algebra or solving partial differential equations, where high volumes of
communication between short intervals of computation limit scalability.
Sequence matching in bioinformatics and Monte Carlo calculations are
some sources of such applications, but the model is general and there
are many others. In the extreme case, these computations are run as
collections of completely separate jobs.
When the units of work are of different sizes, a load balancing problem
is introduced into the parallelism in order that all processors can be
kept busy (some perhaps doing several work units while others are busy
with single work units.) When it is difficult to know ahead of time
exactly how long each unit of work will take, the load balancing
(assignment of work units to worker processes) needs to be dynamic:
that is, occurring during the run, not worked out ahead of time.
Fortunately this is easily and frequently done with a straightforward
master-slave algorithm, in which a single master process assigns work to
slaves, and treats each completion message from a slave as a request for
another work unit. This process continues until all work is done. This
scheme keeps all processes busy in the presence of work units of widely
different sizes, until the end of the computation. It allows completion
of one work unit to cause the master to add new work to the work queue
during the run. Conceptually, there is a single queue of work units
managed by a single master process.
Unfortunately, this simple scheme does not scale; as more processes are
added, or work units shrink in size while increasing in number (which
may happen precisely because more processes are added) the load on the
master process increases until it cannot keep up with the message
traffic to and from the slaves, and slave processes become idle, waiting
for new work assignments. A variety of schemes have been proposed and
successfully applied for extending the scalability of this scheme for
specific applications, but they all (must) eventually fail for large
enough numbers of processors.
The idea behind ADLB is to distribute the management of the conceptually
shared work queue among a set of processes (or threads), which can be as
large or small a fraction of the total number of processors devoted to
the job as necessary. Management of the work queue and communication
among the processes that do it occurs entirely separately from the
application, which will communicate with ADLB through an extremely
simple function-call interface. Any process will be allowed to insert
work units into the global queue and remove them. Parameters
controlling the type and priority of work units provide an application
with enough control to implement application-level algorithms for
ordering the computation. In some cases MPI communication calls can be
eliminated altogether from the application, and all MPI communication
occurs within ADLB.
The interface to the library is straightforward. Basically,
applications processes (called clients) do "put" and "get" operations to
the library. The "get" operation is in two parts, a "reserve" by which
a work unit is assigned to the client and made unavailable to other
clients, and a separate "get-reserved" which copies the work unit into
the client's address space so that the client can work on it. This
two-step process allows the client to find out how big the work unit it
has reserved is and allocate memory for it before actually acquiring it.
The "reserve" has two forms, one that blocks until the request has been
satisfied or until the job is finished, and a non-blocking version that
returns control even if the requested work unit type is unavailable.
The "put" sets a type and priority for the work unit; when work is
returned, high priority work is favored by the library.
The API has some features in common with the MPI interface. The C calls
are functions that return a return code. The Fortran calls are
subroutines that return a return code in their last argument.
Note: The application is responsible for calling MPI_Init before
calling ADLB_Init. If the application makes any MPI calls, it must use
the application communicator returned by MPI_Init.
The API:
int ADLB_Init(int nservers, int ntypes, int type_vect[], int *am_server,
MPI_Comm *app_comm)
ADLB_INIT(nservers, ntypes, type_vect, am_server, app_comm, ierr)
Executed by all processes before any other ADLB functions.
nservers indicates how many processes should become ADLB servers.
am_server will be set to 1 if the calling process should become
a server, else 0.
The program must provide to ADLB an array of all the types of
work that it will put into the work queue.
type_vect contains an entry for each type.
ntypes provides the length of that vector.
app_comm is a new MPI communicator that processes should use for
communications instead of MPI_COMM_WORLD. Many programs do not
perform communications other than making calls to ADLB.
Nonetheless, app_comm can be used to do MPI calls such MPI_Comm_rank.
int ADLB_Server()
ADLB_SERVER(ierr)
Processes that are to become ADLB servers must invoke this
function.
When ADLB_SERVER returns, ADLB_FINALIZE should be called (see
below)
int ADLB_Begin_batch_put()
ADLB_BEGIN_BATCH_PUT( ierr )
int ADLB_End_batch_put()
ADLB_END_BATCH_PUT( ierr )
These two functions can enhance performance if a user calls them
before and after a collection of Put operations that are all
performed at about the same time.
int ADLB_Put(void *work_buf, int work_len, int reserve_rank, int
answer_rank, int work_type, int work_prio)
ADLB_PUT(work_buf, work_len, target_rank, answer_rank,
work_type, work_prio, ierr)
Put work into the queue.
work_buf points to the work itself.
work_len is the length of the work in bytes.
target_rank can be -1 (wild card) if the work is not reserved
for any particular rank. If >= 0 the work can only be reserved
by the specified target_rank.
answer_rank is used by some applications to indicate which rank
is interested in the results of computations from this work.
That is, the answer can be returned by calling ADLB_Put with
reserve_rank specified as the answer_rank obtained from an
ADLB_Reserve.
work_type is set by the app but must be one of those registered
at Init.
work_prio is determined by the app. ADLB attempts to retrieve
the highest priority work (algebraically largest work_prio) later.
int ADLB_Reserve(int *req_types, int *work_type, int *work_prio, int
*work_handle, int *work_len, int *answer_rank)
ADLB_RESERVE(req_types, work_type, work_prio, work_handle,
work_len, answer_rank, ierr)
Reserves work but does not retrieve it. This is useful for
finding out how big a piece of work is and allocating buffer space
before the actual retrieval. This call hangs until either work
is retrieved or ADLB_NO_MORE_WORK is returned as the return code.
req_types is a vector of up to 4 types (currently). ADLB
will return the highest priority work of one of those types that
it can find.
work_type is set to the type of work actually reserved.
work_prio is set to the priority of the work reserved.
work_handle is set to a value that is used to retrieve the work
later.
work_len is the size in bytes of the work package. It can be
used to allocate buffer space before doing a Get operation.
answer_rank tells which rank to send answers to (or reserve them
for); not necessarily used by all apps.
int ADLB_Ireserve(int *req_types, int *work_type, int *work_prio, int
*work_handle, int *work_len, int *answer_rank)
ADLB_IRESERVE(req_types, work_type, work_prio, work_handle,
work_len, answer_rank, ierr)
Same as Reserve, but does not hang. If return code is < 0, no
work was available.
int ADLB_Get_reserved(void *work_buf, int work_handle)
ADLB_GET_RESERVED(work_buf, work_handle, ierr)
Use a handle obtained during a previous Reserve/Ireserve to
retrieve the work.
int ADLB_Set_no_more_work()
ADLB_Set_no_more_work( ierr )
Once this is called, ADLB starts returning ADLB_NO_MORE_WORK
return codes for Reserve, etc.
int ADLB_Finalize()
ADLB_FINALIZE(ierr)
Should be called to finalize operations. After this
MPI_FINALIZE should be called.
int ADLB_Info_get(int key, double *val)
ADLB_INFO_GET(ke,val,ierr)
This routine is used to retrieve internal stats info from the
adlb library. It is called with a key and produces a val.
The val is a double. The valid keys are defined in adlb.h
and adlbf.h. The set of valid keys may change often. It is
assumed that this routine will not be used in typical programs.
---------------------------------------------------------------------
The following example programs are abstractions of a real application
---------------------------------------------------------------------
Example Fortran program:
implicit real*8 ( a-z )
include "mpif.h"
include "adlb/adlb.h"
integer, parameter :: A_EPOCH = 2,
& CS_PER_B = 8,
& MASTER_RANK = 0
integer, parameter :: TAG_B_ANSWER = 1,
& TAG_C_ANSWER = 2
integer, parameter :: TYPE_A = 1,
& TYPE_B = 2,
& TYPE_C = 3
integer, parameter :: NUM_TYPES = 3
integer, parameter :: TYPE_VECT(3) = (/1,2,3/)
integer :: REQ_TYPES(4) = (/-1,-1,-1,-1/)
integer :: status(MPI_STATUS_SIZE)
integer :: ierr, num_delay, num_time_units_per_A, num_servers,
& i, num_world_nodes, my_world_rank, num_As, num_Bs,
& am_server, app_comm, num_app_nodes, my_app_rank, sum,
& num_slaves, iv, j, time_unit_this_A, num_As_this_node,
& priority_A, priority_B, priority_C,
& work_type, work_prio, orig_rank, work_handle,
& work_len, answer_rank, num_C_answers, msg_available
real*8 :: work_A(20), work_B(10), work_C(20)
real*8 :: malloc_hwm, avg_time_on_rq
character*64 chararg
write(6,*) 'BEFORE HANDLING CMD_LINE ARGS'
call flush(6)
num_delay = 10000000
num_time_units_per_A = A_EPOCH * 2
num_servers = 1
num_As = 4
! do i = 1, iargc(), 1
! call getarg( i, chararg )
! if ( trim(chararg) == '-nunits' ) then
! call getarg( i+1, chararg )
! read(chararg,*) num_time_units_per_A
! elseif ( trim(chararg) == '-nas' ) then
! call getarg( i+1, chararg )
! read(chararg,*) num_As
! elseif ( trim(chararg) == '-nservers' ) then
! call getarg( i+1, chararg )
! read(chararg,*) num_servers
! endif
! enddo
write(6,*) 'PAST HANDLING CMD_LINE ARGS'
call flush(6)
priority_A = 0
priority_B = -2
priority_C = -1
call MPI_Init( ierr )
if ( ierr /= MPI_SUCCESS ) then
write(6,*) 'MPI_Init failed with rc=', ierr
endif
call MPI_COMM_RANK( MPI_COMM_WORLD, my_world_rank, ierr )
call MPI_COMM_SIZE( MPI_COMM_WORLD, num_world_nodes, ierr )
write (6,*) 'starting', my_world_rank, num_world_nodes
call flush(6)
call ADLB_INIT( num_servers, NUM_TYPES, TYPE_VECT, am_server,
& app_comm, ierr )
write(6,*) 'num_servers and am_server', num_servers, am_server
call flush(6)
if ( am_server /= 1 ) then
call MPI_COMM_SIZE( app_comm, num_app_nodes, ierr )
write(6,*) 'num_app_nodes', num_app_nodes
call MPI_COMM_RANK( app_comm, my_app_rank, ierr )
write(6,*) 'num_app_nodes and my_app_rank',
& num_app_nodes, my_app_rank
call flush(6)
num_slaves = num_app_nodes - 1
endif
call MPI_BARRIER( MPI_COMM_WORLD, ierr )
start_time = MPI_WTIME()
if ( am_server == 1 ) then
call ADLB_SERVER( ierr )
call ADLB_INFO_GET(ADLB_INFO_MALLOC_HWM,malloc_hwm,ierr);
write(6,*) 'IERR=', ierr, 'MALLOC_HWM=', malloc_hwm
call flush(6)
elseif (my_app_rank == MASTER_RANK) then
sum = 0
!! one B per A per time_units_per_A/A_EPOCH
num_Bs = num_As * (num_time_units_per_A / A_EPOCH)
do i = 1, num_Bs, 1
write(6,*) 'waiting for mpi_recv; i=', i
! call flush(6)
call MPI_RECV(iv,1,MPI_INTEGER,MPI_ANY_SOURCE,MPI_ANY_TAG,
& app_comm, status, ierr)
if ( status(MPI_TAG) .eq. TAG_B_ANSWER) then
sum = sum + iv
write (6,*) 'past mpi_recv; i iv sum', i, iv, sum
else ! may need elseif here later for other msg types
write (6,*) '** invalid tag received', status(MPI_TAG)
! call flush(6)
endif
enddo
write (6,*) '****** SETTING NO MORE WORK ********'
! call flush(6)
call ADLB_SET_NO_MORE_WORK( ierr )
write (6,*) 'expected sum =',
& num_As*(num_time_units_per_A/A_EPOCH)*CS_PER_B
write (6,*) 'done: sum =', sum
! call flush(6)
else !! slave
if ( num_As >= num_slaves ) then
num_As_this_node = num_As / num_slaves
j = num_As - (num_As_this_node * num_slaves)
if ( j > 0 ) then
if ( my_app_rank < j ) then
num_As_this_node = num_As_this_node + 1
endif
endif
else
if ( my_app_rank >= 1 .and. my_app_rank <= num_As ) then
num_As_this_node = 1
else
num_As_this_node = 0
endif
endif
write (6,*) 'NUM_AS_THIS_NODE', num_As_this_node
! call flush(6)
call ADLB_BEGIN_BATCH_PUT( ierr )
do i = 1, num_As_this_node, 1
work_A(1) = my_world_rank
work_A(2) = i
work_A(3) = 1.0 ! starting time unit for this A
write (6,*) 'PT0'
! call flush(6)
call ADLB_PUT( work_A, 20*8, -1, my_app_rank,
& TYPE_A, priority_A, ierr)
write (6,*) 'PUT A VAL', work_A(1), work_A(2)
! call flush(6)
enddo
call ADLB_END_BATCH_PUT( ierr )
do while ( .true. )
REQ_TYPES(1) = -1
call ADLB_RESERVE( REQ_TYPES,
& work_type, work_prio, work_handle,
& work_len, answer_rank, ierr )
write (6,*) 'RESERVE IERR', ierr
! call flush(6)
if ( ierr == ADLB_NO_MORE_WORK ) then
write (6,*) 'GOT NO_MORE_WORK x1'
! call flush(6)
go to 10 !! fake break
endif
if ( ierr == 0 ) then
write (6,*) '******** RESERVE FAILED ********'
! call flush(6)
exit
endif
if ( work_type == TYPE_A ) then
call ADLB_GET_RESERVED( work_A, work_handle, ierr )
if ( ierr == ADLB_NO_MORE_WORK ) then
write (6,*) 'GOT NO_MORE_WORK x2'
! call flush(6)
go to 10 !! fake break
endif
tempval = work_A(1)
do i = 1, num_delay, 1
tempval = sqrt(tempval + 5000000.0) + 1
enddo
time_unit_this_A = work_A(3) + 1
write (6,*) 'HANDLING A VAL', work_A(1), work_A(2)
! call flush(6)
if ( (mod(time_unit_this_A,A_EPOCH)) == 0 .and.
& time_unit_this_A <= num_time_units_per_A ) then
work_B(1) = work_A(1)
work_B(2) = work_A(2)
call ADLB_PUT( work_B, 10*8, -1, my_app_rank,
& TYPE_B, priority_B, ierr)
write (6,*) 'PUT B VAL', work_B(1), work_B(2)
! call flush(6)
priority_B = priority_A - 2
endif
if ( time_unit_this_A <= num_time_units_per_A ) then
work_A(3) = time_unit_this_A + 1
priority_A = priority_A - 3
call ADLB_PUT( work_A, 20*8, -1, my_app_rank,
& TYPE_A, priority_A, ierr )
write (6,*) 'PUT A VAL', work_A(1), work_A(2)
! call flush(6)
endif
write (6,*) 'HANDLED A VAL', work_A(1), work_A(2)
elseif ( work_type == TYPE_B ) then
call ADLB_GET_RESERVED( work_B, work_handle, ierr )
if ( ierr == ADLB_NO_MORE_WORK ) then
write (6,*) 'GOT NO_MORE_WORK x3'
! call flush(6)
go to 10 !! fake break
endif
write (6,*) 'HANDLING B VAL', work_B(1), work_B(2)
call ADLB_BEGIN_BATCH_PUT( ierr )
do i = 1, CS_PER_B, 1
work_C(1) = work_B(1)
work_C(2) = work_B(2)
call ADLB_PUT(work_C, 20*8, -1, my_app_rank,
& TYPE_C, priority_C, ierr )
write (6,*) 'PUT C VAL', work_C(1), work_C(2)
! call flush(6)
priority_C = priority_B + 1
enddo
call ADLB_END_BATCH_PUT( ierr )
sum = 0
num_C_answers = 0
do while ( num_C_answers < CS_PER_B )
write (6,*) 'PT1'
! call flush(6)
call MPI_IPROBE( MPI_ANY_SOURCE, TAG_C_ANSWER,
& app_comm, msg_available,
& status, ierr )
write (6,*) 'PT2', msg_available
! call flush(6)
if ( msg_available > 0 ) then
call MPI_RECV( iv, 1, MPI_INTEGER,
& MPI_ANY_SOURCE,
& TAG_C_ANSWER, app_comm,
& status, ierr )
sum = sum + iv
num_C_answers = num_C_answers + 1
else
REQ_TYPES(1) = TYPE_C
call ADLB_IRESERVE( REQ_TYPES,
& work_type, work_prio,
& work_handle, work_len,
& answer_rank, ierr )
if ( ierr == ADLB_NO_MORE_WORK ) then
write (6,*) 'GOT NO_MORE_WORK x4'
! call flush(6)
go to 10 !! fake break
endif
if ( ierr > 0 ) then
write (6,*) 'PT3'
! call flush(6)
call ADLB_GET_RESERVED( work_C,
& work_handle,
& ierr )
if ( ierr == ADLB_NO_MORE_WORK ) then
write (6,*) 'GOT NO_MORE_WORK x5'
! call flush(6)
go to 10 !! fake break
endif
write (6,*) 'HANDLING C VAL',
& work_C(1), work_C(2)
! call flush(6)
tempval = work_C(1)
do i = 1, num_delay, 1
tempval = sqrt(tempval + 5000000.0) + 1
enddo
if ( answer_rank .eq. my_world_rank ) then
sum = sum + 1
num_C_answers = num_C_answers + 1
else
j = 1
call MPI_SEND( j, 1, MPI_INTEGER,
& answer_rank,
& TAG_C_ANSWER,
& app_comm, ierr )
endif
write (6,*) 'bHANDLED C VAL',
& work_C(1), work_C(2)
else
call MPI_RECV( iv, 1, MPI_INTEGER,
& MPI_ANY_SOURCE,
& TAG_C_ANSWER, app_comm,
& status, ierr )
sum = sum + iv
num_C_answers = num_C_answers + 1
endif
endif
enddo !! recving C answers
if ( ierr == ADLB_NO_MORE_WORK ) then
go to 10 !! fake break
endif
call MPI_SEND( sum, 1, MPI_INTEGER, MASTER_RANK,
& TAG_B_ANSWER, app_comm, ierr )
write (6,*) 'HANDLED B VAL', work_B(1), work_B(2)
elseif ( work_type == TYPE_C ) then
call ADLB_GET_RESERVED( work_C, work_handle, ierr )
if ( ierr == ADLB_NO_MORE_WORK ) then
write (6,*) 'GOT NO_MORE_WORK x6'
! call flush(6)
go to 10 !! fake break
endif
write (6,*) 'bHANDLING C VAL', work_C(1), work_C(2)
tempval = work_C(1)
do i = 1, num_delay, 1
tempval = sqrt(tempval + 5000000.0) + 1
enddo
j = 1
call MPI_SEND( j, 1, MPI_INTEGER, answer_rank,
& TAG_C_ANSWER, app_comm, ierr )
write (6,*) 'bHANDLED C VAL', work_C(1), work_C(2)
endif !! if TYPE_C
enddo !! infinite loop
10 continue !! destination of fake break
endif !! slave
if ( my_world_rank == MASTER_RANK ) then
end_time = MPI_WTIME()
write (6,*) 'total time =', end_time - start_time
endif
write (6,*) 'AT ADLB_FINALIZE'
! call flush(6)
call ADLB_FINALIZE( ierr )
write (6,*) 'AT MPI_FINALIZE'
! call flush(6)
call MPI_FINALIZE( ierr )
write (6,*) 'DONE'
! call flush(6)
stop
end
------------------------------------------------------------------------
Example C program:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <adlb/adlb.h>
#define A_EPOCH 2
#define CS_PER_B 8
#define MASTER_RANK 0
#define TAG_B_ANSWER 1
#define TAG_C_ANSWER 2
#define TAG_B_CHANGE 3
#define TYPE_A 1
#define TYPE_B 2
#define TYPE_C 3
int num_types = 3;
int type_vect[3] = { TYPE_A, TYPE_B, TYPE_C };
double start_time, end_time;
int main(int argc, char *argv[])
{
int i, j, rc, iv, sum, am_server;
int num_time_units_per_A, num_servers, num_slaves, num_world_nodes, my_world_rank;
int my_app_rank, num_app_nodes;
int num_As, num_Bs, num_Cs, num_As_this_node;
int priority_A = 0, priority_B = -2, priority_C = -1;
int req_types[4], work_type, answer_rank, work_len, num_delay, time_unit_this_A;
int work_prio, orig_rank, work_handle, num_C_answers, msg_available;
int work_A[20], work_B[10], work_C[20];
double tempval;
double malloc_hwm, avg_time_on_rq;
MPI_Comm app_comm;
MPI_Status status;
num_delay = 10000000;
num_time_units_per_A = A_EPOCH * 2;
num_servers = 3;
num_As = 4;
for (i=1; i < argc; i++)
{
if (strcmp(argv[i],"-nunits") == 0)
num_time_units_per_A = atoi(argv[i+1]);
else if (strcmp(argv[i],"-nas") == 0)
num_As = atoi(argv[i+1]);
else if (strcmp(argv[i],"-nservers") == 0)
num_servers = atoi(argv[i+1]);
}
rc = MPI_Init(&argc,&argv);
MPI_Comm_size(MPI_COMM_WORLD,&num_world_nodes);
MPI_Comm_rank(MPI_COMM_WORLD,&my_world_rank);
rc = ADLB_Init(num_servers,num_types,type_vect,&am_server,&app_comm);
if ( ! am_server)
{
MPI_Comm_size(app_comm,&num_app_nodes);
MPI_Comm_rank(app_comm,&my_app_rank);
num_slaves = num_app_nodes - 1;
}
rc = MPI_Barrier(MPI_COMM_WORLD);
start_time = MPI_Wtime();
if (am_server)
{
ADLB_Server();
rc = ADLB_Info_get(ADLB_INFO_MALLOC_HWM,&malloc_hwm);
rc = ADLB_Info_get(ADLB_INFO_AVG_TIME_ON_RQ,&avg_time_on_rq);
printf("MALLOC_HWM %.0f AVG_TIME_ON_RQ %f\n",malloc_hwm,avg_time_on_rq);
}
else if (my_app_rank == MASTER_RANK) /* if master */
{
sum = 0;
num_Bs = num_As * (num_time_units_per_A / A_EPOCH); // one B per A per time_units_per_A/A_EPOCH
for (i=0; i < num_Bs; i++)
{
aprintf(1,"waiting for mpi_recv; i=%d\n",i);
rc = MPI_Recv(&iv,1,MPI_INT,MPI_ANY_SOURCE,MPI_ANY_TAG,app_comm,&status);
if (status.MPI_TAG == TAG_B_ANSWER)
{
sum += iv;
aprintf(1,"past mpi_recv; i=%d iv=%d sum=%d\n",i,iv,sum);
}
else if (status.MPI_TAG == TAG_B_CHANGE)
{
// the number of Bs chgd
// do I run a new A thru all 120 steps or just go from curr time_unit?
// iv should tell me how many more/fewer TAG_B_ANSWERs to expect
}
else
{
aprintf(1,"**** unexpected msg received: type %d from wrank %d\n",
status.MPI_TAG,status.MPI_SOURCE);
}
}
aprintf(1,"********** SETTING NO MORE WORK ***********************************\n");
ADLB_Set_no_more_work();
aprintf(1,"expected sum = %d\n",num_As*(num_time_units_per_A/A_EPOCH)*CS_PER_B);
aprintf(1,"done: sum = %d\n",sum);
}
else /* slave */
{
/* put "my" As in the pool with priority_A */
if (num_As >= num_slaves)
{
num_As_this_node = (num_As / num_slaves);
j = num_As - (num_As_this_node * num_slaves);
if (j)
if (my_app_rank <= j)
num_As_this_node++;
}
else
{
if (my_app_rank >= 1 && my_app_rank <= num_As)
num_As_this_node = 1;
else
num_As_this_node = 0;
}
aprintf(1,"NUM_AS_THIS_NODE=%d\n",num_As_this_node);
ADLB_Begin_batch_put();
for (i=0; i < num_As_this_node; i++)
{
memset(work_A,0,20*sizeof(int)); // an A is 20 doubles
work_A[0] = (int)(my_world_rank); // id of A is orig rank plus
work_A[1] = (int)(i+1); // a unique Aid at that rank
work_A[2] = (int)1; // starting time_unit for this A
rc = ADLB_Put(work_A,20*sizeof(int),-1,my_app_rank,TYPE_A,priority_A);
aprintf(1,"PUT A VAL %d %d\n",(int)work_A[0],(int)work_A[1]);
}
ADLB_End_batch_put();
while (1)
{
req_types[0] = -1;
req_types[1] = req_types[2] = req_types[3] = -1;
aprintf(1111,"AT RESERVE\n");
rc = ADLB_Reserve(req_types,&work_type,&work_prio,&work_handle,
&work_len,&answer_rank);
aprintf(1111,"PAST RESERVE rc %d\n",rc);
if (rc == ADLB_NO_MORE_WORK)
{
aprintf(1,"GOT NO_MORE_WORK x1\n");
break;
}
if (rc < 0)
{
aprintf(1,"**** RESERVE FAILED rc %d\n",rc);
exit(-1);
}
if (work_type == TYPE_A)
{
rc = ADLB_Get_reserved(work_A,work_handle);
if (rc == ADLB_NO_MORE_WORK)
{
aprintf(1,"GOT NO_MORE_WORK x2\n");
break;
}
tempval = (double)work_A[0];
for (i=0; i < num_delay; i++)
tempval = sqrt(tempval + 5000000.0) + 1;
time_unit_this_A = (int)work_A[2];
aprintf(1,"HANDLING A VAL %d %d TIME %d\n",(int)work_A[0],(int)work_A[1],time_unit_this_A);
if (time_unit_this_A % A_EPOCH == 0 && time_unit_this_A <= num_time_units_per_A)
{
memset(work_B,0,10*sizeof(int)); // a B is 10 doubles
work_B[0] = work_A[0]; // a B is identified by the A whence it comes
work_B[1] = work_A[1]; // including both rank and Aid
rc = ADLB_Put(work_B,10*sizeof(int),-1,my_app_rank,TYPE_B,priority_B);
aprintf(1,"PUT B VAL %d %d\n",(int)work_B[0],(int)work_B[1]);
priority_B = priority_A - 2; // priority_B -= 10; // ?? priority of A -2
}
if (time_unit_this_A < num_time_units_per_A)
{
work_A[2] = (int) (time_unit_this_A + 1);
priority_A -= 3; // -= 10;
rc = ADLB_Put(work_A,20*sizeof(int),-1,my_app_rank,TYPE_A,priority_A);
aprintf(1,"PUT A VAL %d %d\n",(int)work_A[0],(int)work_A[1]);
}
aprintf(1,"HANDLED A VAL %d %d TIME %d\n",(int)work_A[0],(int)work_A[1],time_unit_this_A);
}
else if (work_type == TYPE_B)
{
rc = ADLB_Get_reserved(work_B,work_handle);
if (rc == ADLB_NO_MORE_WORK)
{
aprintf(1,"GOT NO_MORE_WORK x3\n");
break;
}
aprintf(1,"HANDLING B VAL %d %d\n",(int)work_B[0],(int)work_B[1]);
ADLB_Begin_batch_put();
for (i=0; i < CS_PER_B; i++)
{
memset(work_C,0,20*sizeof(int)); // an C is 20 doubles
work_C[0] = work_B[0]; // a C is identified by the B (A) whence it comes
work_C[1] = work_B[1]; // including both rank and Aid
rc = ADLB_Put(work_C,20*sizeof(int),-1,my_app_rank,TYPE_C,priority_C);
aprintf(1,"PUT C VAL %d %d\n",(int)work_C[0],(int)work_C[1]);
priority_C = priority_B + 1; // -= 10; // ?? priority_B + 1 ??
}
ADLB_End_batch_put();
sum = 0;
num_C_answers = 0;
while (num_C_answers < CS_PER_B)
{
rc = MPI_Iprobe(MPI_ANY_SOURCE,TAG_C_ANSWER,app_comm,&msg_available,&status);
if (msg_available)
{
rc = MPI_Recv(&iv,1,MPI_INT,MPI_ANY_SOURCE,TAG_C_ANSWER,app_comm,&status);
sum += iv;
num_C_answers++;
aprintf(1111,"PTA numCanswers %d\n",num_C_answers);
}
else
{
req_types[0] = TYPE_C;
req_types[1] = req_types[2] = req_types[3] = -1;
rc = ADLB_Ireserve(req_types,&work_type,&work_prio,&work_handle,
&work_len,&answer_rank);
aprintf(1111,"RMB: NOHANG GOT RC %d\n",rc);
if (rc == ADLB_NO_MORE_WORK)
{
aprintf(1,"GOT NO_MORE_WORK x4\n");
break;
}
if (rc > 0)
{
rc = ADLB_Get_reserved(work_C,work_handle);
if (rc == ADLB_NO_MORE_WORK)
{
aprintf(1,"GOT NO_MORE_WORK x5\n");
break;
}
aprintf(1,"aHANDLING C VAL %d %d\n",(int)work_C[0],(int)work_C[1]);
tempval = (double)work_C[0];
for (i=0; i < num_delay; i++)
tempval = sqrt(tempval + 5000000.0) + 1;
if (answer_rank == my_world_rank)
{
sum += 1;
num_C_answers++;
}
else
{
j = 1;
rc = MPI_Send(&j,1,MPI_INT,answer_rank,TAG_C_ANSWER,app_comm);
}
aprintf(1111,"PTB numCanswers %d\n",num_C_answers);
// aprintf(1,"aHANDLED C VAL %d %d\n",(int)work_C[0],(int)work_C[1]);
}
else
{
rc = MPI_Recv(&iv,1,MPI_INT,MPI_ANY_SOURCE,TAG_C_ANSWER,app_comm,&status);
sum += iv;
num_C_answers++;
aprintf(1111,"PTC numCanswers %d\n",num_C_answers);
}
}
}
if (rc == ADLB_NO_MORE_WORK)
break;
rc = MPI_Send(&sum,1,MPI_INT,MASTER_RANK,TAG_B_ANSWER,app_comm);
aprintf(1,"HANDLED B VAL %d %d\n",(int)work_B[0],(int)work_B[1]);
}
else if (work_type == TYPE_C)
{
rc = ADLB_Get_reserved(work_C,work_handle);
if (rc == ADLB_NO_MORE_WORK)
{
aprintf(1,"GOT NO_MORE_WORK x6\n");
break;
}
aprintf(1,"bHANDLING C VAL %d %d ar=%d\n",(int)work_C[0],(int)work_C[1],answer_rank);
tempval = (double)work_C[0];
for (i=0; i < num_delay; i++)
tempval = sqrt(tempval + 5000000.0) + 1;
if (answer_rank == my_world_rank)
{
sum += iv;
num_C_answers++;
}
else
{
j = 1;
rc = MPI_Send(&j,1,MPI_INT,answer_rank,TAG_C_ANSWER,app_comm);
}
aprintf(1,"bHANDLED C VAL %d %d\n",(int)work_C[0],(int)work_C[1]);
}
}
} /* slave */
if (my_world_rank == MASTER_RANK) /* if master */
{
end_time = MPI_Wtime();
aprintf(1,"total time = %10.2f\n",end_time-start_time);
}
aprintf(1,"AT ADLB_FINALIZE\n");
ADLB_Finalize();
aprintf(1,"AT MPI_FINALIZE\n");
MPI_Finalize();
aprintf(1,"DONE\n");
return 0;
}