-
Notifications
You must be signed in to change notification settings - Fork 3
/
listing12.html
executable file
·569 lines (482 loc) · 18.4 KB
/
listing12.html
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
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd">
<html>
<head>
<!-- BEGIN META TAG INFO -->
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<link rel="home" href="http://developer.apple.com/">
<link rel="find" href="http://developer.apple.com/search/">
<link rel="stylesheet" type="text/css" href="../../documentation/css/adcstyle.css" title="fonts">
<script language="JavaScript" src="../../documentation/js/adc.js" type="text/javascript"></script>
<!-- END META TAG INFO -->
<!-- BEGIN TITLE -->
<title>dist_fft - /sched.c</title>
<!-- END TITLE -->
<script language="JavaScript">
function JumpToNewPage() {
window.location=document.scpopupmenu.gotop.value;
return true;
}
</script>
</head>
<!-- BEGIN BODY OPEN -->
<body>
<!--END BODY OPEN -->
<!-- START CENTER OPEN -->
<center>
<!-- END CENTER OPEN -->
<!-- BEGIN LOGO AND SEARCH -->
<!--#include virtual="/includes/adcnavbar"-->
<!-- END LOGO AND SEARCH -->
<!-- START BREADCRUMB -->
<div id="breadcrumb">
<table width="680" border="0" cellpadding="0" cellspacing="0">
<tr>
<td scope="row"><img width="340" height="10" src="images/1dot.gif" alt=""></td>
<td><img width="340" height="10" src="images/1dot.gif" alt=""></td>
</tr>
<tr valign="middle">
<td align="left" colspan="2">
<a href="http://developer.apple.com/">ADC Home</a> > <a href="../../referencelibrary/index.html">Reference Library</a> > <a href="../../samplecode/index.html">Sample Code</a> > <a href="../../samplecode/Performance/index.html">Performance</a> > <a href="../../samplecode/Performance/idxVelocityEngine-date.html">Velocity Engine</a> > <A HREF="javascript:location.replace('index.html');">dist_fft</A> >
</td>
</tr>
<tr>
<td colspan="2" scope="row"><img width="680" height="35" src="images/1dot.gif" alt=""></td>
</tr>
</table>
</div>
<!-- END BREADCRUMB -->
<!-- START MAIN CONTENT -->
<!-- START TITLE GRAPHIC AND INTRO-->
<table width="680" border="0" cellpadding="0" cellspacing="0">
<tr align="left" valign="top">
<td><h1><div id="pagehead">dist_fft</div></h1></td>
</tr>
</table>
<!-- END TITLE GRAPHIC AND INTRO -->
<!-- START WIDE COLUMN -->
<table width="680" border="0" cellpadding="0" cellspacing="0">
<tr align="left" valign="top">
<td id="scdetails">
<h2>/sched.c</h2>
<form name="scpopupmenu" onSubmit="return false;" method=post>
<p><strong>View Source Code:</strong>
<select name="gotop" onChange="JumpToNewPage();" style="width:340px"><option selected value="ingnore">Select File</option>
<option value="listing1.html">/dist_fft.c</option>
<option value="listing2.html">/dist_fft.h</option>
<option value="listing3.html">/dist_fft_prefetch.c</option>
<option value="listing4.html">/dist_fft_prefetch.h</option>
<option value="listing5.html">/dist_fft_transpose.c</option>
<option value="listing6.html">/dist_fft_transpose.h</option>
<option value="listing7.html">/dist_fft_twiddle.c</option>
<option value="listing8.html">/dist_fft_twiddle.h</option>
<option value="listing9.html">/dist_fft_types.h</option>
<option value="listing10.html">/LnxMPI/LnxMPI_S.c</option>
<option value="listing11.html">/LnxMPI/mpi.h</option>
<option value="listing12.html">/sched.c</option>
<option value="listing13.html">/sched.h</option>
<option value="listing14.html">/test_main.c</option>
<option value="listing15.html">/test_main.h</option>
<option value="listing16.html">/TOMS_transpose.c</option>
<option value="listing17.html">/TOMS_transpose.h</option>
<option value="listing18.html">/transpose_mpi.c</option>
<option value="listing19.html">/transpose_mpi.h</option></select>
</p>
</form>
<p><strong><a href="dist_fft.zip">Download Sample</a></strong> (“dist_fft.zip”, 716.6K)<BR>
<strong><a href="dist_fft.dmg">Download Sample</a></strong> (“dist_fft.dmg”, 800.0K)</p>
<!--
<p><strong><a href="#">Download Sample</a></strong> (“filename.sit”, 500K)</p>
-->
</td>
</tr>
<tr>
<td scope="row"><img width="680" height="10" src="images/1dot.gif" alt=""><br>
<img height="1" width="680" src="images/1dot_919699.gif" alt=""><br>
<img width="680" height="20" src="images/1dot.gif" alt=""></td>
</tr>
<tr>
<td scope="row">
<!--googleon: index -->
<pre class="sourcecodebox">/*
* Copyright (c) 1997-1999, 2003 Massachusetts Institute of Technology
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#include <stdio.h>
#include <stdlib.h>
#include "transpose_mpi.h"
#include "sched.h"
/* This file contains routines to compute communications schedules for
all-to-all communications (complete exchanges) that are performed
in-place. (That is, the block that processor x sends to processor
y gets replaced on processor x by a block received from processor y.)
A schedule, int **sched, is a two-dimensional array where
sched[pe][i] is the processor that pe expects to exchange a message
with on the i-th step of the exchange. sched[pe][i] == -1 for the
i after the last exchange scheduled on pe.
Here, processors (pe's, for processing elements), are numbered from
0 to npes-1.
There are a couple of constraints that a schedule should satisfy
(besides the obvious one that every processor has to communicate
with every other processor exactly once).
* First, and most importantly, there must be no deadlocks.
* Second, we would like to overlap communications as much as possible,
so that all exchanges occur in parallel. It turns out that perfect
overlap is possible if npes is even, and only a single extra step is
required if npes is odd.
It turns out that this scheduling problem is actually well-studied,
and good solutions are known. The problem is known as a
"time-tabling" problem, and is specifically the problem of
scheduling a sports competition (where n teams must compete exactly
once with every other team). The problem is discussed and
algorithms are presented in:
[1] J. A. M. Schreuder, "Constructing Timetables for Sport
Competitions," Mathematical Programming Study 13, pp. 58-67 (1980).
[2] A. Schaerf, "Scheduling Sport Tournaments using Constraint
Logic Programming," Proc. of 12th Europ. Conf. on
Artif. Intell. (ECAI-96), pp. 634-639 (Budapest 1996).
http://hermes.dis.uniromal.it/~aschaerf/publications.html
(These people actually impose a lot of additional constraints that
we don't care about, so they are solving harder problems. [1] gives
a simple enough algorithm for our purposes, though.)
However, we have to do more: for a particular processor, the
communications schedule must be sorted in ascending or descending
order of processor index. (This is necessary so that the data
coming in for the transpose does not overwrite data that will be
sent later; for that processor the incoming and outgoing blocks are
of different non-zero sizes.)
Fortunately, it is possible to reorder the schedule to achieve any
permutation on a given processor while maintaining the two required
properties above. ...except, when npes is odd (when the schedule
already contains a stall), our reordering introduces an extra stall
due to the motion of the self-communication past a stall. We could
fix this if it were really important, but it turns out that the
extra stall is not introduced in the case that we care about (when
the sorted processor is the first or last processor). */
/* Create a new communications schedule for a given number of processors.
The schedule is initialized to a deadlock-free, maximum overlap
schedule. Returns NULL on an error (may print a message to
stderr if there is a program bug detected). */
int **make_comm_schedule(int npes)
{
int **sched;
int i;
sched = (int **) malloc(sizeof(int *) * npes);
if (!sched)
return NULL;
for (i = 0; i < npes; ++i)
sched[i] = NULL;
for (i = 0; i < npes; ++i) {
sched[i] = (int *) malloc(sizeof(int) * 10 * (npes + 1));
if (!sched[i]) {
free_comm_schedule(sched,npes);
return NULL;
}
}
empty_comm_schedule(sched,npes);
fill_comm_schedule(sched,npes);
if (!check_comm_schedule(sched,npes)) {
free_comm_schedule(sched,npes);
return NULL;
}
return sched;
}
void free_comm_schedule(int **sched, int npes)
{
if (sched) {
int i;
for (i = 0; i < npes; ++i)
free(sched[i]);
free(sched);
}
}
void empty_comm_schedule(int **sched, int npes)
{
int i;
for (i = 0; i < npes; ++i)
sched[i][0] = -1;
}
static void add_dest_to_comm_schedule(int **sched, int pe, int dest)
{
int i;
for (i = 0; sched[pe][i] != -1; ++i)
;
sched[pe][i] = dest;
sched[pe][i+1] = -1;
}
static void add_pair_to_comm_schedule(int **sched, int pe1, int pe2)
{
add_dest_to_comm_schedule(sched, pe1, pe2);
if (pe1 != pe2)
add_dest_to_comm_schedule(sched, pe2, pe1);
}
/* Simplification of algorithm presented in [1] (we have fewer
constraints). Produces a perfect schedule if npes is even;
otherwise contains one unavoidable extra step. */
void fill_comm_schedule(int **sched, int npes)
{
int pe, i, n;
for (pe = 0; pe < npes; ++pe)
add_pair_to_comm_schedule(sched,pe,pe);
if (npes % 2 == 0)
n = npes;
else
n = npes + 1;
for (pe = 0; pe < n - 1; ++pe) {
if (pe != npes - 1)
add_pair_to_comm_schedule(sched,pe,npes - 1);
for (i = 1; i < n/2; ++i) {
int pe_a, pe_b;
pe_a = pe - i;
if (pe_a < 0)
pe_a += n - 1;
pe_b = (pe + i) % (n - 1);
if (pe_a != npes - 1 && pe_b != npes - 1)
add_pair_to_comm_schedule(sched,pe_a,pe_b);
}
}
}
/* Below, we have various checks in case of bugs: */
/* check for deadlocks by simulating the schedule and looking for
cycles in the dependency list; returns 0 if there are deadlocks
(or other errors) */
static int check_schedule_deadlock(int **sched, int npes)
{
int *step, *depend, *visited, pe, pe2, period, done = 0;
int counter = 0;
/* step[pe] is the step in the schedule that a given pe is on */
step = (int *) malloc(sizeof(int) * npes);
/* depend[pe] is the pe' that pe is currently waiting for a message
from (-1 if none) */
depend = (int *) malloc(sizeof(int) * npes);
/* visited[pe] tells whether we have visited the current pe already
when we are looking for cycles. */
visited = (int *) malloc(sizeof(int) * npes);
if (!step || !depend || !visited) {
free(step); free(depend); free(visited);
return 0;
}
for (pe = 0; pe < npes; ++pe)
step[pe] = 0;
while (!done) {
++counter;
for (pe = 0; pe < npes; ++pe)
depend[pe] = sched[pe][step[pe]];
/* now look for cycles in the dependencies with period > 2: */
for (pe = 0; pe < npes; ++pe)
if (depend[pe] != -1) {
for (pe2 = 0; pe2 < npes; ++pe2)
visited[pe2] = 0;
period = 0;
pe2 = pe;
do {
visited[pe2] = period + 1;
pe2 = depend[pe2];
period++;
} while (pe2 != -1 && !visited[pe2]);
if (pe2 == -1) {
fprintf(stderr,
"BUG: unterminated cycle in schedule!\n");
free(step); free(depend);
free(visited);
return 0;
}
if (period - (visited[pe2] - 1) > 2) {
fprintf(stderr,"BUG: deadlock in schedule!\n");
free(step); free(depend);
free(visited);
return 0;
}
if (pe2 == pe)
step[pe]++;
}
done = 1;
for (pe = 0; pe < npes; ++pe)
if (sched[pe][step[pe]] != -1) {
done = 0;
break;
}
}
free(step); free(depend); free(visited);
return (counter > 0 ? counter : 1);
}
/* sanity checks; prints message and returns 0 on failure.
undocumented feature: the return value on success is actually the
number of steps required for the schedule to complete, counting
stalls. */
int check_comm_schedule(int **sched, int npes)
{
int pe, i, comm_pe;
for (pe = 0; pe < npes; ++pe) {
for (comm_pe = 0; comm_pe < npes; ++comm_pe) {
for (i = 0; sched[pe][i] != -1 && sched[pe][i] != comm_pe; ++i)
;
if (sched[pe][i] == -1) {
fprintf(stderr,"BUG: schedule never sends message from "
"%d to %d.\n",pe,comm_pe);
return 0; /* never send message to comm_pe */
}
}
for (i = 0; sched[pe][i] != -1; ++i)
;
if (i != npes) {
fprintf(stderr,"BUG: schedule sends too many messages from "
"%d\n",pe);
return 0;
}
}
return check_schedule_deadlock(sched,npes);
}
/* invert the order of all the schedules; this has no effect on
its required properties. */
void invert_comm_schedule(int **sched, int npes)
{
int pe, i;
for (pe = 0; pe < npes; ++pe)
for (i = 0; i < npes/2; ++i) {
int dummy = sched[pe][i];
sched[pe][i] = sched[pe][npes-1-i];
sched[pe][npes-1-i] = dummy;
}
}
/* Relabel pe1 <-> pe2 in all the schedules. The required schedule
properties are invariant under this operation. */
static void comm_schedule_swap(int **sched, int npes, int pe1, int pe2)
{
int pe, i, *dummy;
for (pe = 0; pe < npes; ++pe)
for (i = 0; sched[pe][i] != -1; ++i) {
if (sched[pe][i] == pe1)
sched[pe][i] = pe2;
else if (sched[pe][i] == pe2)
sched[pe][i] = pe1;
}
dummy = sched[pe1];
sched[pe1] = sched[pe2];
sched[pe2] = dummy;
}
/* Sort the schedule for sort_pe in ascending order of processor
index. Unfortunately, for odd npes (when schedule has a stall
to begin with) this will introduce an extra stall due to
the motion of the self-communication past a stall. We could
fix this if it were really important. Actually, we don't
get an extra stall when sort_pe == 0 or npes-1, which is sufficient
for our purposes. */
void sort_comm_schedule(int **sched, int npes, int sort_pe)
{
int i,j,pe;
/* Note that we can do this sort in O(npes) swaps because we know
that the numbers we are sorting are just 0...npes-1. */
/* find self-communication: */
for (i = 0; i < npes; ++i)
if (sched[sort_pe][i] == sort_pe)
break;
if (i == npes) {
fprintf(stderr,"BUG: missing self-communication for %d.",sort_pe);
exit(1);
}
/* Note that, to maintain communications
overlap, we perform the same shift
on the self-communication for all pe's.
The self-communication is always at the
same step for all pe's (this invariant
is maintained both by this operation
and by comm_schedule_swap). */
/* shift self-communications to the correct place:
-- this has to be done separately because we cannot perform
simple swaps of the self-communication elements */
for (pe = 0; pe < npes; ++pe) {
if (sched[pe][i] != pe) {
fprintf(stderr,
"BUG: self-communication isn't at fixed step.");
exit(1);
}
for (j = i; j+1 < npes; ++j)
sched[pe][j] = sched[pe][j+1];
for (j = npes - 2; j >= sort_pe; --j)
sched[pe][j+1] = sched[pe][j];
sched[pe][sort_pe] = pe;
}
/* Move the remaining items to their sorted positions: */
for (pe = 0; pe < npes; ++pe)
if (pe != sort_pe) {
for (j = 0; j < npes; ++j)
if (sched[sort_pe][j] == pe) break;
if (j == npes) {
fprintf(stderr,"BUG: missing %d in %d schedule.",
pe, sort_pe);
exit(1);
}
/* move communications with pe to correct position: */
comm_schedule_swap(sched,npes,
sched[sort_pe][pe],
sched[sort_pe][j]);
}
}
/* print the schedule (for debugging purposes) */
void print_comm_schedule(int **sched, int npes)
{
int pe, i, width;
if (npes < 10)
width = 1;
else if (npes < 100)
width = 2;
else
width = 3;
for (pe = 0; pe < npes; ++pe) {
printf("pe %*d schedule:", width, pe);
for (i = 0; sched[pe][i] != -1; ++i)
printf(" %*d",width,sched[pe][i]);
printf("\n");
}
}
</pre>
<!--googleoff: index -->
</td>
</tr>
</table>
<!-- END WIDE COLUMN -->
<!-- END MAIN CONTENT -->
<table width="680" border="0" cellpadding="0" cellspacing="0">
<tr>
<td><div style="width: 100%; height: 1px; background-color: #919699; margin-top: 5px; margin-bottom: 15px"></div></td>
</tr>
<tr>
<td align="center"><br/>
<table border="0" cellpadding="0" cellspacing="0" class="graybox">
<tr>
<th>Did this document help you?</th>
</tr>
<tr>
<td>
<div style="margin-bottom: 8px"><a href="http://developer.apple.com/feedback/?v=1&url=/samplecode/dist_fft/listing12.html%3Fid%3DDTS10003377-1.0&media=dvd" target=_new>Yes</a>: Tell us what works for you.</div>
<div style="margin-bottom: 8px"><a href="http://developer.apple.com/feedback/?v=2&url=/samplecode/dist_fft/listing12.html%3Fid%3DDTS10003377-1.0&media=dvd" target=_new>It’s good, but:</a> Report typos, inaccuracies, and so forth.</div>
<div><a href="http://developer.apple.com/feedback/?v=3&url=/samplecode/dist_fft/listing12.html%3Fid%3DDTS10003377-1.0&media=dvd" target=_new>It wasn’t helpful</a>: Tell us what would have helped.</div>
</td>
</tr>
</table>
</td>
</tr>
</table>
<!-- START BOTTOM APPLE NAVIGATION -->
<!--#include virtual="/includes/footer"-->
<!-- END BOTTOM APPLE NAVIGATION -->
<!-- START CENTER CLOSE -->
</center>
<!-- END CENTER CLOSE -->
</body>
</html>