Chris@42: /* Chris@42: * Copyright (c) 2003, 2007-14 Matteo Frigo Chris@42: * Copyright (c) 1999-2003, 2007-8 Massachusetts Institute of Technology Chris@42: * Chris@42: * This program is free software; you can redistribute it and/or modify Chris@42: * it under the terms of the GNU General Public License as published by Chris@42: * the Free Software Foundation; either version 2 of the License, or Chris@42: * (at your option) any later version. Chris@42: * Chris@42: * This program is distributed in the hope that it will be useful, Chris@42: * but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@42: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@42: * GNU General Public License for more details. Chris@42: * Chris@42: * You should have received a copy of the GNU General Public License Chris@42: * along with this program; if not, write to the Free Software Chris@42: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA Chris@42: * Chris@42: */ Chris@42: Chris@42: /**********************************************************************/ Chris@42: /* This is a modified and combined version of the sched.c and Chris@42: test_sched.c files shipped with FFTW 2, written to implement and Chris@42: test various all-to-all communications scheduling patterns. Chris@42: Chris@42: It is not used in FFTW 3, but I keep it around in case we ever want Chris@42: to play with this again or to change algorithms. In particular, I Chris@42: used it to implement and test the fill1_comm_sched routine in Chris@42: transpose-pairwise.c, which allows us to create a schedule for one Chris@42: process at a time and is much more compact than the FFTW 2 code. Chris@42: Chris@42: Note that the scheduling algorithm is somewhat modified from that Chris@42: of FFTW 2. Originally, I thought that one "stall" in the schedule Chris@42: was unavoidable for odd numbers of processes, since this is the Chris@42: case for the soccer-timetabling problem. However, because of the Chris@42: self-communication step, we can use the self-communication to fill Chris@42: in the stalls. (Thanks to Ralf Wildenhues for pointing this out.) Chris@42: This greatly simplifies the process re-sorting algorithm. */ Chris@42: Chris@42: /**********************************************************************/ Chris@42: Chris@42: #include Chris@42: #include Chris@42: Chris@42: /* This file contains routines to compute communications schedules for Chris@42: all-to-all communications (complete exchanges) that are performed Chris@42: in-place. (That is, the block that processor x sends to processor Chris@42: y gets replaced on processor x by a block received from processor y.) Chris@42: Chris@42: A schedule, int **sched, is a two-dimensional array where Chris@42: sched[pe][i] is the processor that pe expects to exchange a message Chris@42: with on the i-th step of the exchange. sched[pe][i] == -1 for the Chris@42: i after the last exchange scheduled on pe. Chris@42: Chris@42: Here, processors (pe's, for processing elements), are numbered from Chris@42: 0 to npes-1. Chris@42: Chris@42: There are a couple of constraints that a schedule should satisfy Chris@42: (besides the obvious one that every processor has to communicate Chris@42: with every other processor exactly once). Chris@42: Chris@42: * First, and most importantly, there must be no deadlocks. Chris@42: Chris@42: * Second, we would like to overlap communications as much as possible, Chris@42: so that all exchanges occur in parallel. It turns out that perfect Chris@42: overlap is possible for all number of processes (npes). Chris@42: Chris@42: It turns out that this scheduling problem is actually well-studied, Chris@42: and good solutions are known. The problem is known as a Chris@42: "time-tabling" problem, and is specifically the problem of Chris@42: scheduling a sports competition (where n teams must compete exactly Chris@42: once with every other team). The problem is discussed and Chris@42: algorithms are presented in: Chris@42: Chris@42: [1] J. A. M. Schreuder, "Constructing Timetables for Sport Chris@42: Competitions," Mathematical Programming Study 13, pp. 58-67 (1980). Chris@42: Chris@42: [2] A. Schaerf, "Scheduling Sport Tournaments using Constraint Chris@42: Logic Programming," Proc. of 12th Europ. Conf. on Chris@42: Artif. Intell. (ECAI-96), pp. 634-639 (Budapest 1996). Chris@42: http://hermes.dis.uniromal.it/~aschaerf/publications.html Chris@42: Chris@42: (These people actually impose a lot of additional constraints that Chris@42: we don't care about, so they are solving harder problems. [1] gives Chris@42: a simple enough algorithm for our purposes, though.) Chris@42: Chris@42: In the timetabling problem, N teams can all play one another in N-1 Chris@42: steps if N is even, and N steps if N is odd. Here, however, Chris@42: there is a "self-communication" step (a team must also "play itself") Chris@42: and so we can always make an optimal N-step schedule regardless of N. Chris@42: Chris@42: However, we have to do more: for a particular processor, the Chris@42: communications schedule must be sorted in ascending or descending Chris@42: order of processor index. (This is necessary so that the data Chris@42: coming in for the transpose does not overwrite data that will be Chris@42: sent later; for that processor the incoming and outgoing blocks are Chris@42: of different non-zero sizes.) Fortunately, because the schedule Chris@42: is stall free, each parallel step of the schedule is independent Chris@42: of every other step, and we can reorder the steps arbitrarily Chris@42: to achieve any desired order on a particular process. Chris@42: */ Chris@42: Chris@42: void free_comm_schedule(int **sched, int npes) Chris@42: { Chris@42: if (sched) { Chris@42: int i; Chris@42: Chris@42: for (i = 0; i < npes; ++i) Chris@42: free(sched[i]); Chris@42: free(sched); Chris@42: } Chris@42: } Chris@42: Chris@42: void empty_comm_schedule(int **sched, int npes) Chris@42: { Chris@42: int i; Chris@42: for (i = 0; i < npes; ++i) Chris@42: sched[i][0] = -1; Chris@42: } Chris@42: Chris@42: extern void fill_comm_schedule(int **sched, int npes); Chris@42: Chris@42: /* Create a new communications schedule for a given number of processors. Chris@42: The schedule is initialized to a deadlock-free, maximum overlap Chris@42: schedule. Returns NULL on an error (may print a message to Chris@42: stderr if there is a program bug detected). */ Chris@42: int **make_comm_schedule(int npes) Chris@42: { Chris@42: int **sched; Chris@42: int i; Chris@42: Chris@42: sched = (int **) malloc(sizeof(int *) * npes); Chris@42: if (!sched) Chris@42: return NULL; Chris@42: Chris@42: for (i = 0; i < npes; ++i) Chris@42: sched[i] = NULL; Chris@42: Chris@42: for (i = 0; i < npes; ++i) { Chris@42: sched[i] = (int *) malloc(sizeof(int) * 10 * (npes + 1)); Chris@42: if (!sched[i]) { Chris@42: free_comm_schedule(sched,npes); Chris@42: return NULL; Chris@42: } Chris@42: } Chris@42: Chris@42: empty_comm_schedule(sched,npes); Chris@42: fill_comm_schedule(sched,npes); Chris@42: Chris@42: if (!check_comm_schedule(sched,npes)) { Chris@42: free_comm_schedule(sched,npes); Chris@42: return NULL; Chris@42: } Chris@42: Chris@42: return sched; Chris@42: } Chris@42: Chris@42: static void add_dest_to_comm_schedule(int **sched, int pe, int dest) Chris@42: { Chris@42: int i; Chris@42: Chris@42: for (i = 0; sched[pe][i] != -1; ++i) Chris@42: ; Chris@42: Chris@42: sched[pe][i] = dest; Chris@42: sched[pe][i+1] = -1; Chris@42: } Chris@42: Chris@42: static void add_pair_to_comm_schedule(int **sched, int pe1, int pe2) Chris@42: { Chris@42: add_dest_to_comm_schedule(sched, pe1, pe2); Chris@42: if (pe1 != pe2) Chris@42: add_dest_to_comm_schedule(sched, pe2, pe1); Chris@42: } Chris@42: Chris@42: /* Simplification of algorithm presented in [1] (we have fewer Chris@42: constraints). Produces a perfect schedule (npes steps). */ Chris@42: Chris@42: void fill_comm_schedule(int **sched, int npes) Chris@42: { Chris@42: int pe, i, n; Chris@42: Chris@42: if (npes % 2 == 0) { Chris@42: n = npes; Chris@42: for (pe = 0; pe < npes; ++pe) Chris@42: add_pair_to_comm_schedule(sched,pe,pe); Chris@42: } Chris@42: else Chris@42: n = npes + 1; Chris@42: Chris@42: for (pe = 0; pe < n - 1; ++pe) { Chris@42: add_pair_to_comm_schedule(sched, pe, npes % 2 == 0 ? npes - 1 : pe); Chris@42: Chris@42: for (i = 1; i < n/2; ++i) { Chris@42: int pe_a, pe_b; Chris@42: Chris@42: pe_a = pe - i; Chris@42: if (pe_a < 0) Chris@42: pe_a += n - 1; Chris@42: Chris@42: pe_b = (pe + i) % (n - 1); Chris@42: Chris@42: add_pair_to_comm_schedule(sched,pe_a,pe_b); Chris@42: } Chris@42: } Chris@42: } Chris@42: Chris@42: /* given an array sched[npes], fills it with the communications Chris@42: schedule for process pe. */ Chris@42: void fill1_comm_sched(int *sched, int which_pe, int npes) Chris@42: { Chris@42: int pe, i, n, s = 0; Chris@42: if (npes % 2 == 0) { Chris@42: n = npes; Chris@42: sched[s++] = which_pe; Chris@42: } Chris@42: else Chris@42: n = npes + 1; Chris@42: for (pe = 0; pe < n - 1; ++pe) { Chris@42: if (npes % 2 == 0) { Chris@42: if (pe == which_pe) sched[s++] = npes - 1; Chris@42: else if (npes - 1 == which_pe) sched[s++] = pe; Chris@42: } Chris@42: else if (pe == which_pe) sched[s++] = pe; Chris@42: Chris@42: if (pe != which_pe && which_pe < n - 1) { Chris@42: i = (pe - which_pe + (n - 1)) % (n - 1); Chris@42: if (i < n/2) Chris@42: sched[s++] = (pe + i) % (n - 1); Chris@42: Chris@42: i = (which_pe - pe + (n - 1)) % (n - 1); Chris@42: if (i < n/2) Chris@42: sched[s++] = (pe - i + (n - 1)) % (n - 1); Chris@42: } Chris@42: } Chris@42: if (s != npes) { Chris@42: fprintf(stderr, "bug in fill1_com_schedule (%d, %d/%d)\n", Chris@42: s, which_pe, npes); Chris@42: exit(EXIT_FAILURE); Chris@42: } Chris@42: } Chris@42: Chris@42: /* sort the communication schedule sched for npes so that the schedule Chris@42: on process sortpe is ascending or descending (!ascending). */ Chris@42: static void sort1_comm_sched(int *sched, int npes, int sortpe, int ascending) Chris@42: { Chris@42: int *sortsched, i; Chris@42: sortsched = (int *) malloc(npes * sizeof(int) * 2); Chris@42: fill1_comm_sched(sortsched, sortpe, npes); Chris@42: if (ascending) Chris@42: for (i = 0; i < npes; ++i) Chris@42: sortsched[npes + sortsched[i]] = sched[i]; Chris@42: else Chris@42: for (i = 0; i < npes; ++i) Chris@42: sortsched[2*npes - 1 - sortsched[i]] = sched[i]; Chris@42: for (i = 0; i < npes; ++i) Chris@42: sched[i] = sortsched[npes + i]; Chris@42: free(sortsched); Chris@42: } Chris@42: Chris@42: /* Below, we have various checks in case of bugs: */ Chris@42: Chris@42: /* check for deadlocks by simulating the schedule and looking for Chris@42: cycles in the dependency list; returns 0 if there are deadlocks Chris@42: (or other errors) */ Chris@42: static int check_schedule_deadlock(int **sched, int npes) Chris@42: { Chris@42: int *step, *depend, *visited, pe, pe2, period, done = 0; Chris@42: int counter = 0; Chris@42: Chris@42: /* step[pe] is the step in the schedule that a given pe is on */ Chris@42: step = (int *) malloc(sizeof(int) * npes); Chris@42: Chris@42: /* depend[pe] is the pe' that pe is currently waiting for a message Chris@42: from (-1 if none) */ Chris@42: depend = (int *) malloc(sizeof(int) * npes); Chris@42: Chris@42: /* visited[pe] tells whether we have visited the current pe already Chris@42: when we are looking for cycles. */ Chris@42: visited = (int *) malloc(sizeof(int) * npes); Chris@42: Chris@42: if (!step || !depend || !visited) { Chris@42: free(step); free(depend); free(visited); Chris@42: return 0; Chris@42: } Chris@42: Chris@42: for (pe = 0; pe < npes; ++pe) Chris@42: step[pe] = 0; Chris@42: Chris@42: while (!done) { Chris@42: ++counter; Chris@42: Chris@42: for (pe = 0; pe < npes; ++pe) Chris@42: depend[pe] = sched[pe][step[pe]]; Chris@42: Chris@42: /* now look for cycles in the dependencies with period > 2: */ Chris@42: for (pe = 0; pe < npes; ++pe) Chris@42: if (depend[pe] != -1) { Chris@42: for (pe2 = 0; pe2 < npes; ++pe2) Chris@42: visited[pe2] = 0; Chris@42: Chris@42: period = 0; Chris@42: pe2 = pe; Chris@42: do { Chris@42: visited[pe2] = period + 1; Chris@42: pe2 = depend[pe2]; Chris@42: period++; Chris@42: } while (pe2 != -1 && !visited[pe2]); Chris@42: Chris@42: if (pe2 == -1) { Chris@42: fprintf(stderr, Chris@42: "BUG: unterminated cycle in schedule!\n"); Chris@42: free(step); free(depend); Chris@42: free(visited); Chris@42: return 0; Chris@42: } Chris@42: if (period - (visited[pe2] - 1) > 2) { Chris@42: fprintf(stderr,"BUG: deadlock in schedule!\n"); Chris@42: free(step); free(depend); Chris@42: free(visited); Chris@42: return 0; Chris@42: } Chris@42: Chris@42: if (pe2 == pe) Chris@42: step[pe]++; Chris@42: } Chris@42: Chris@42: done = 1; Chris@42: for (pe = 0; pe < npes; ++pe) Chris@42: if (sched[pe][step[pe]] != -1) { Chris@42: done = 0; Chris@42: break; Chris@42: } Chris@42: } Chris@42: Chris@42: free(step); free(depend); free(visited); Chris@42: return (counter > 0 ? counter : 1); Chris@42: } Chris@42: Chris@42: /* sanity checks; prints message and returns 0 on failure. Chris@42: undocumented feature: the return value on success is actually the Chris@42: number of steps required for the schedule to complete, counting Chris@42: stalls. */ Chris@42: int check_comm_schedule(int **sched, int npes) Chris@42: { Chris@42: int pe, i, comm_pe; Chris@42: Chris@42: for (pe = 0; pe < npes; ++pe) { Chris@42: for (comm_pe = 0; comm_pe < npes; ++comm_pe) { Chris@42: for (i = 0; sched[pe][i] != -1 && sched[pe][i] != comm_pe; ++i) Chris@42: ; Chris@42: if (sched[pe][i] == -1) { Chris@42: fprintf(stderr,"BUG: schedule never sends message from " Chris@42: "%d to %d.\n",pe,comm_pe); Chris@42: return 0; /* never send message to comm_pe */ Chris@42: } Chris@42: } Chris@42: for (i = 0; sched[pe][i] != -1; ++i) Chris@42: ; Chris@42: if (i != npes) { Chris@42: fprintf(stderr,"BUG: schedule sends too many messages from " Chris@42: "%d\n",pe); Chris@42: return 0; Chris@42: } Chris@42: } Chris@42: return check_schedule_deadlock(sched,npes); Chris@42: } Chris@42: Chris@42: /* invert the order of all the schedules; this has no effect on Chris@42: its required properties. */ Chris@42: void invert_comm_schedule(int **sched, int npes) Chris@42: { Chris@42: int pe, i; Chris@42: Chris@42: for (pe = 0; pe < npes; ++pe) Chris@42: for (i = 0; i < npes/2; ++i) { Chris@42: int dummy = sched[pe][i]; Chris@42: sched[pe][i] = sched[pe][npes-1-i]; Chris@42: sched[pe][npes-1-i] = dummy; Chris@42: } Chris@42: } Chris@42: Chris@42: /* Sort the schedule for sort_pe in ascending order of processor Chris@42: index. Unfortunately, for odd npes (when schedule has a stall Chris@42: to begin with) this will introduce an extra stall due to Chris@42: the motion of the self-communication past a stall. We could Chris@42: fix this if it were really important. Actually, we don't Chris@42: get an extra stall when sort_pe == 0 or npes-1, which is sufficient Chris@42: for our purposes. */ Chris@42: void sort_comm_schedule(int **sched, int npes, int sort_pe) Chris@42: { Chris@42: int i,j,pe; Chris@42: Chris@42: /* Note that we can do this sort in O(npes) swaps because we know Chris@42: that the numbers we are sorting are just 0...npes-1. But we'll Chris@42: just do a bubble sort for simplicity here. */ Chris@42: Chris@42: for (i = 0; i < npes - 1; ++i) Chris@42: for (j = i + 1; j < npes; ++j) Chris@42: if (sched[sort_pe][i] > sched[sort_pe][j]) { Chris@42: for (pe = 0; pe < npes; ++pe) { Chris@42: int s = sched[pe][i]; Chris@42: sched[pe][i] = sched[pe][j]; Chris@42: sched[pe][j] = s; Chris@42: } Chris@42: } Chris@42: } Chris@42: Chris@42: /* print the schedule (for debugging purposes) */ Chris@42: void print_comm_schedule(int **sched, int npes) Chris@42: { Chris@42: int pe, i, width; Chris@42: Chris@42: if (npes < 10) Chris@42: width = 1; Chris@42: else if (npes < 100) Chris@42: width = 2; Chris@42: else Chris@42: width = 3; Chris@42: Chris@42: for (pe = 0; pe < npes; ++pe) { Chris@42: printf("pe %*d schedule:", width, pe); Chris@42: for (i = 0; sched[pe][i] != -1; ++i) Chris@42: printf(" %*d",width,sched[pe][i]); Chris@42: printf("\n"); Chris@42: } Chris@42: } Chris@42: Chris@42: int main(int argc, char **argv) Chris@42: { Chris@42: int **sched; Chris@42: int npes = -1, sortpe = -1, steps, i; Chris@42: Chris@42: if (argc >= 2) { Chris@42: npes = atoi(argv[1]); Chris@42: if (npes <= 0) { Chris@42: fprintf(stderr,"npes must be positive!"); Chris@42: return 1; Chris@42: } Chris@42: } Chris@42: if (argc >= 3) { Chris@42: sortpe = atoi(argv[2]); Chris@42: if (sortpe < 0 || sortpe >= npes) { Chris@42: fprintf(stderr,"sortpe must be between 0 and npes-1.\n"); Chris@42: return 1; Chris@42: } Chris@42: } Chris@42: Chris@42: if (npes != -1) { Chris@42: printf("Computing schedule for npes = %d:\n",npes); Chris@42: sched = make_comm_schedule(npes); Chris@42: if (!sched) { Chris@42: fprintf(stderr,"Out of memory!"); Chris@42: return 6; Chris@42: } Chris@42: Chris@42: if (steps = check_comm_schedule(sched,npes)) Chris@42: printf("schedule OK (takes %d steps to complete).\n", steps); Chris@42: else Chris@42: printf("schedule not OK.\n"); Chris@42: Chris@42: print_comm_schedule(sched, npes); Chris@42: Chris@42: if (sortpe != -1) { Chris@42: printf("\nRe-creating schedule for pe = %d...\n", sortpe); Chris@42: int *sched1 = (int*) malloc(sizeof(int) * npes); Chris@42: for (i = 0; i < npes; ++i) sched1[i] = -1; Chris@42: fill1_comm_sched(sched1, sortpe, npes); Chris@42: printf(" ="); Chris@42: for (i = 0; i < npes; ++i) Chris@42: printf(" %*d", npes < 10 ? 1 : (npes < 100 ? 2 : 3), Chris@42: sched1[i]); Chris@42: printf("\n"); Chris@42: Chris@42: printf("\nSorting schedule for sortpe = %d...\n", sortpe); Chris@42: sort_comm_schedule(sched,npes,sortpe); Chris@42: Chris@42: if (steps = check_comm_schedule(sched,npes)) Chris@42: printf("schedule OK (takes %d steps to complete).\n", Chris@42: steps); Chris@42: else Chris@42: printf("schedule not OK.\n"); Chris@42: Chris@42: print_comm_schedule(sched, npes); Chris@42: Chris@42: printf("\nInverting schedule...\n"); Chris@42: invert_comm_schedule(sched,npes); Chris@42: Chris@42: if (steps = check_comm_schedule(sched,npes)) Chris@42: printf("schedule OK (takes %d steps to complete).\n", Chris@42: steps); Chris@42: else Chris@42: printf("schedule not OK.\n"); Chris@42: Chris@42: print_comm_schedule(sched, npes); Chris@42: Chris@42: free_comm_schedule(sched,npes); Chris@42: Chris@42: free(sched1); Chris@42: } Chris@42: } Chris@42: else { Chris@42: printf("Doing infinite tests...\n"); Chris@42: for (npes = 1; ; ++npes) { Chris@42: int *sched1 = (int*) malloc(sizeof(int) * npes); Chris@42: printf("npes = %d...",npes); Chris@42: sched = make_comm_schedule(npes); Chris@42: if (!sched) { Chris@42: fprintf(stderr,"Out of memory!\n"); Chris@42: return 5; Chris@42: } Chris@42: for (sortpe = 0; sortpe < npes; ++sortpe) { Chris@42: empty_comm_schedule(sched,npes); Chris@42: fill_comm_schedule(sched,npes); Chris@42: if (!check_comm_schedule(sched,npes)) { Chris@42: fprintf(stderr, Chris@42: "\n -- fill error for sortpe = %d!\n",sortpe); Chris@42: return 2; Chris@42: } Chris@42: Chris@42: for (i = 0; i < npes; ++i) sched1[i] = -1; Chris@42: fill1_comm_sched(sched1, sortpe, npes); Chris@42: for (i = 0; i < npes; ++i) Chris@42: if (sched1[i] != sched[sortpe][i]) Chris@42: fprintf(stderr, Chris@42: "\n -- fill1 error for pe = %d!\n", Chris@42: sortpe); Chris@42: Chris@42: sort_comm_schedule(sched,npes,sortpe); Chris@42: if (!check_comm_schedule(sched,npes)) { Chris@42: fprintf(stderr, Chris@42: "\n -- sort error for sortpe = %d!\n",sortpe); Chris@42: return 3; Chris@42: } Chris@42: invert_comm_schedule(sched,npes); Chris@42: if (!check_comm_schedule(sched,npes)) { Chris@42: fprintf(stderr, Chris@42: "\n -- invert error for sortpe = %d!\n", Chris@42: sortpe); Chris@42: return 4; Chris@42: } Chris@42: } Chris@42: free_comm_schedule(sched,npes); Chris@42: printf("OK\n"); Chris@42: if (npes % 50 == 0) Chris@42: printf("(...Hit Ctrl-C to stop...)\n"); Chris@42: free(sched1); Chris@42: } Chris@42: } Chris@42: Chris@42: return 0; Chris@42: }