#include <cstdlib> #include <iostream> #include "krazki.h" #include "message.h" int main() { int id = MyNodeId(); int nodes = NumberOfNodes(); int n = PipeHeight(); int discs = NumberOfDiscs(); if (n < nodes) nodes = n; if (id >= nodes) return 0; int pipeFrom = ((n-1)/nodes+1)*id+1; int pipeTo = ((n-1)/nodes+1)*(id+1); int nForNode = ((n-1)/nodes+1); long long int tMin[nForNode]; if (id > 0) { Receive(id-1); tMin[0] = HoleDiameter(pipeFrom); long long int minFromPreviousNode = GetLL(id-1); if (minFromPreviousNode < tMin[0]) tMin[0] = minFromPreviousNode; } else tMin[0] = HoleDiameter(pipeFrom); for (int i=pipeFrom+1; i<=pipeTo; i++) { long long int hole = HoleDiameter(i); if (hole < tMin[i-1 - pipeFrom]) tMin[i-pipeFrom] = hole; else tMin[i-pipeFrom] = tMin[i-1 - pipeFrom]; } if (id < nodes-1) { PutLL(id+1, tMin[pipeTo-pipeFrom]); Send(id+1); } int currentDisc = 1; if (id < nodes-1) { Receive(id+1); currentDisc = GetInt(id+1); } if (currentDisc > discs) //koniec, doszlismy do konca dyskow { if (id > 0) { PutInt(id-1, currentDisc); Send(id-1); } return 0; } int currentPipe = nForNode; for (int i=currentDisc; i<=discs; i++) { currentPipe--; long long int discWidth = DiscDiameter(i); while (currentPipe >= 0 && tMin[currentPipe] < discWidth) currentPipe--; if (currentPipe < 0) { if (id == 0) printf("0\n"); else { PutInt(id-1, i); Send(id-1); } return 0; //aktualny węzeł skończył swoją pracę } } printf("%d\n", currentPipe + pipeFrom); if (id > 0) //wysyłamy do pozostałych instancji, żeby zakończyły swoją pracę { PutInt(id-1, discs+1); Send(id-1); } return 0; }
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 | #include <cstdlib> #include <iostream> #include "krazki.h" #include "message.h" int main() { int id = MyNodeId(); int nodes = NumberOfNodes(); int n = PipeHeight(); int discs = NumberOfDiscs(); if (n < nodes) nodes = n; if (id >= nodes) return 0; int pipeFrom = ((n-1)/nodes+1)*id+1; int pipeTo = ((n-1)/nodes+1)*(id+1); int nForNode = ((n-1)/nodes+1); long long int tMin[nForNode]; if (id > 0) { Receive(id-1); tMin[0] = HoleDiameter(pipeFrom); long long int minFromPreviousNode = GetLL(id-1); if (minFromPreviousNode < tMin[0]) tMin[0] = minFromPreviousNode; } else tMin[0] = HoleDiameter(pipeFrom); for (int i=pipeFrom+1; i<=pipeTo; i++) { long long int hole = HoleDiameter(i); if (hole < tMin[i-1 - pipeFrom]) tMin[i-pipeFrom] = hole; else tMin[i-pipeFrom] = tMin[i-1 - pipeFrom]; } if (id < nodes-1) { PutLL(id+1, tMin[pipeTo-pipeFrom]); Send(id+1); } int currentDisc = 1; if (id < nodes-1) { Receive(id+1); currentDisc = GetInt(id+1); } if (currentDisc > discs) //koniec, doszlismy do konca dyskow { if (id > 0) { PutInt(id-1, currentDisc); Send(id-1); } return 0; } int currentPipe = nForNode; for (int i=currentDisc; i<=discs; i++) { currentPipe--; long long int discWidth = DiscDiameter(i); while (currentPipe >= 0 && tMin[currentPipe] < discWidth) currentPipe--; if (currentPipe < 0) { if (id == 0) printf("0\n"); else { PutInt(id-1, i); Send(id-1); } return 0; //aktualny węzeł skończył swoją pracę } } printf("%d\n", currentPipe + pipeFrom); if (id > 0) //wysyłamy do pozostałych instancji, żeby zakończyły swoją pracę { PutInt(id-1, discs+1); Send(id-1); } return 0; } |