Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- MPI_Bcast(&lines, 1, MPI_INT, ROOT, MPI_COMM_WORLD);
- MPI_Bcast(&width, 1, MPI_INT, ROOT, MPI_COMM_WORLD);
- MPI_Bcast(&fillIn, 1, MPI_INT, ROOT, MPI_COMM_WORLD);
- sum = (lines*width);
- localLines = ((sum / width) / total);
- if (me == total-1) {
- localLines = (lines-(localLines*(total-1)));
- }
- int linesWidthSize = (localLines * width); // normale ChunkGroesse
- int absoluteStart = me * localLines; // absolute Startposition
- int absoluteEnd = absoluteStart + (linesWidthSize-1); // absolute Endposition
- labChunk = (int*) malloc(linesWidthSize *sizeof(int)); // Laenge des Labyrinth-Teils
- int i, j, k, l;
- printf("P%d: linesWidthSize = %d\n", me, linesWidthSize);
- printf("P%d: localLines = %d\n", me, localLines);
- // Labyrinth in total Teile aufteilen an die Prozesse
- MPI_Scatter(labLine, linesWidthSize, MPI_INT, labChunk, linesWidthSize, MPI_INT, ROOT, MPI_COMM_WORLD);
- MPI_Request request;
- MPI_Status status;
- MPI_Request request1;
- MPI_Status status1;
- MPI_Request request2;
- MPI_Status status2;
- if (me == ROOT) {
- //printf(">> labChunkMax Array, %d, INT, an %d\n", maxLabChunkSize, (total-1));
- MPI_Isend(labChunkMax, maxLabChunkSize, MPI_INT, (total-1), 33, MPI_COMM_WORLD, &request);
- }
- if (me == (total-1)) {
- //printf("<< labChunk Array, %d, INT, von %d\n", linesWidthSize, ROOT);
- MPI_Recv(labChunk, linesWidthSize, MPI_INT, ROOT, 33, MPI_COMM_WORLD, &status);
- }
- MPI_Barrier(MPI_COMM_WORLD);
- // Jeder Prozess gibt sein lokales Labyrinth aus
- if (1) {
- for (i=0; i<total; i++) {
- if (me == i) {
- printf("P%d>>>>\n", me);
- showEmptyLab(labChunk, 1);
- }
- MPI_Barrier(MPI_COMM_WORLD);
- }
- }
- printf("\n");
- int *myOuts = (int*) malloc(sizeof(int) * width);
- int *hisOuts = (int*) malloc(sizeof(int) * width);
- int *myIns = (int*) malloc(sizeof(int) * width);
- int *hisIns = (int*) malloc(sizeof(int) * width);
- // Jeder schickt seine Ausgaenge an den naechstne damit dieser weiss wo die Eingaenge wirklich sind
- for (j=0; j<width; j++) {
- int myOut, myIn;
- myOut = getValue((localLines-1), j);
- myIn = getValue(0, j);
- // Eingangsposition speichern
- if (myOut == fillIn) {
- //printf("P%d: Ausgang bei (%d, %d)\n", me, j, (localLines-1));
- myOuts[j] = fillIn;
- } else {
- myOuts[j] = 0;
- }
- //printf("P%d: bei (%d, %d) = [%d]\n", me, j, 0, myOut);
- if (myIn == fillIn) {
- //printf("P%d: Eingang bei (%d, %d)\n", me, j, 0);
- myIns[j] = fillIn;
- } else {
- myIns[j] = 0;
- }
- //printf("P%d: bei (%d, %d) = [%d]\n", me, j, 0, myIn);
- }
- // an den naechsten schicken
- if (me != (total-1)) {
- MPI_Isend(myOuts, width, MPI_INT, (me+1), 22, MPI_COMM_WORLD, &request1);
- }
- if (me != ROOT) {
- MPI_Isend(myIns, width, MPI_INT, (me-1), 11, MPI_COMM_WORLD, &request2);
- }
- if (me != ROOT) {
- MPI_Recv(hisOuts, width, MPI_INT, (me-1), 22, MPI_COMM_WORLD, &status1);
- //printf("P%d: ich habe etwas empfangen!\n", me);
- for (i=0; i<width; i++) {
- if (hisOuts[i] == 1) {
- //printf("hisOuts[%d] = %d\n", i, hisOuts[i]);
- }
- }
- }
- if (me != (total-1)) {
- MPI_Recv(hisIns, width, MPI_INT, (me+1), 11, MPI_COMM_WORLD, &status2);
- //printf("P%d: ich habe etwas empfangen!\n", me);
- for (i=0; i<width; i++) {
- if (hisOuts[i] == 1) {
- //printf("hisOuts[%d] = %d\n", i, hisOuts[i]);
- }
- }
- }
- MPI_Barrier(MPI_COMM_WORLD);
- // 1) labChunk (lokales Array) sichern
- // 2) Fuer jeden Eingang normal rekursiv durchsuchen, dabei Pfade abspeichern
- // 3) Wenn mehr als 1 Eingang existiert labChunk zurueckkopieren und weiter wie Schritt 2
- // Pfade zusammenfuegen ....
- // Teste getValue() Funktion
- if (0) {
- for (k=0; k<total; k++) {
- if (me == k) {
- for (i=0; i<localLines; i++) {
- for (j=0; j<width; j++) {
- int val = getValue(i,j);
- printf("%d", val);
- }
- printf("\n");
- }
- }
- MPI_Barrier(MPI_COMM_WORLD);
- }
- }
- // Eingaenge bestimmen
- realStart = (int*) calloc(width, sizeof(int));
- realEnd = (int*) calloc(width, sizeof(int));
- int tmp;
- for (j=0; j<width; j++) {
- // echte Eingaenge
- tmp = getValue(0, j);
- if (me != ROOT) {
- if (hisOuts[j] == fillIn && tmp == fillIn) {
- realStart[j] = 1;
- printf("P%d: >>>>>>> echter Eingang [%d]\n", me, j);
- }
- } else {
- // ROOT hat nur einen Eingang !
- if (tmp == fillIn) {
- realStart[j] = 1;
- printf("P%d: >>>>>>> GLOBALER Eingang [%d]\n", me, j);
- }
- }
- MPI_Barrier(MPI_COMM_WORLD);
- // echte Ausgaenge
- tmp = getValue((localLines-1), j);
- if (me != (total-1)) {
- if (hisIns[j] == fillIn && tmp == fillIn) {
- realEnd[j] = 1;
- printf("P%d: echter Ausgang [%d]\n", me, j);
- }
- } else {
- // P(last) hat nur einen Ausgang !
- if (tmp == fillIn) {
- realEnd[j] = 1;
- printf("P%d: >>>>>>> GLOBALER Ausgange [%d]\n", me, j);
- }
- }
- }
- if (me == 0) {
- // Sicherheitskopie
- //int *copy = copyArray(labChunk, linesWidthSize);
- int *copy = (int*) malloc(linesWidthSize * sizeof(int));
- memcpy(copy, labChunk, linesWidthSize * sizeof(int));
- // Pruefe fuer jeden Eingang den kuerzesten Weg
- for (i=0; i<width; i++) {
- if (realStart[i] == 1) {
- printf("Starte fuer (0,%d)\n", i);
- p1x = 0; p1y = i;
- shortestPath(labChunk, 0, 0, i);
- showLocalLab(labChunk, 20, 1);
- memcpy(labChunk, copy, linesWidthSize * sizeof(int));
- }
- }
- // Pruefe auf Abschnite, die nicht an den Startknoten dranhaengen
- for (i=0; i<width; i++) {
- if (realEnd[i] == 1) {
- int result = searchStartPointInPath((localLines-1), i);
- if (result == 0) {
- printf("Starte (R) fuer (%d,%d)\n", (localLines-1), i);
- p1x = (localLines-1); p1y = i;
- shortestPath(labChunk, 0, (localLines-1), i);
- showLocalLab(labChunk, 20, 1);
- memcpy(labChunk, copy, linesWidthSize * sizeof(int));
- }
- }
- }
- printf("PFADE folgen nun...\n");
- showPaths();
- }
- /* MPI Laufzeitsystem beenden */
- assert (MPI_Finalize() == MPI_SUCCESS);
- // Kann wie ein 2D Array behandelt werden mit Zeilen und Spalten Indizes
- return 0;
- }
- int shortestPath (int* lab, int dist, int x, int y) {
- dist += 1;
- int pos = ((x*width)+y);
- if (x>=0 && x<localLines && y>=0 && y<lines && lab[pos] > dist) {
- //printf("Jap: (%d, %d) = [%d]\t\tlab[%d] = %d> %d\n", x, y, dist, pos, lab[pos], dist);
- // pruefe in jede Richtung ob nicht ausserhalb des Labyrinths und ob der Weg laenger ist als jetzt
- // wenn ja dann rufe rekursiv auf:
- lab[pos] = dist;
- // oben
- if (x>0 && lab[pos-width] > dist) {
- shortestPath(lab, dist, (x-1), y);
- }
- // unten
- if (x<(width-1) && lab[pos+width] > dist) {
- shortestPath(lab, dist, (x+1), y);
- }
- // links
- if (y>0 && lab[pos-1] > dist) {
- shortestPath(lab, dist, x, (y-1));
- }
- // rechts
- if (y<(lines-1) && lab[pos+1] > dist) {
- shortestPath(lab, dist, x, (y+1));
- }
- // Pruefe ob aktuelles Feld in der letzten Zeile ist, also im Ziel und ob der Weg frei ist
- // Wenn ja, schreibe aktuelle Distanz rein und beende Rekursion
- if ((x == (localLines-1) || x == 0) && lab[pos] != 0) {
- //printf("Pfad: (%d, %d) => (%d, %d) = [%d]\n", p1x, p1y, x, y, dist);
- // Pfade bei folgenden Konstellationen nicht speichern
- int itsself = (x == p1x && y == p1y);
- int tmp1 = realStart[y];
- int tmp2 = realEnd[y];
- int firstLine = ((x == 0 && x == p1x && !tmp1)); // Wenn Element in der ersten Zeile nebeneinander liegen und kein echter Startpunkt ist
- int lastLine = ((x == (localLines-1) && p1x == 0 && !tmp2)); // Wenn Element in der letzten Zeile nebeneinander liegen und kein echter Endpunkt ist
- if (itsself || firstLine || lastLine) {
- //printf("wird nicht gespeichert: (%d, %d) ==> (%d, %d)\t\t itsself=%d firstLine=%d lastLine=%d 1[%d] 2[%d]\n", p1x, p1y, x, y, itsself, firstLine, lastLine, tmp1, tmp2);
- } else {
- savePath(p1x, p1y, x, y, dist);
- //printf("wird gespeichert: (%d, %d) ==> (%d, %d)\t\t itsself=%d firstLine=%d lastLine=%d\n", p1x, p1y, x, y, itsself, firstLine, lastLine);
- }
- }
- } else {
- return 0;
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment