#include "kollib.h" #include "message.h" #include <iostream> #include <cmath> #include <queue> #include <set> #include <vector> #include <map> #include <unordered_set> using namespace std; typedef long long LL; bool isManager = false; int nodeId = 0; int nodesCount = 0; int queriesCount = 0; LL studentsCount = 0; LL packetSize = 0; struct JobRequest { int start; int start_next; }; bool operator<(const JobRequest& l, const JobRequest& r) { return (l.start<r.start || (l.start==r.start && l.start_next<r.start_next)); } struct JobResult { int start; int start_next; int stop_previous; int stop; int time; }; enum MessageTypes { MessageFinished = 0, MessageDoWork = 1, MessageWorkDone = 2 }; struct Message { public: int nodeId; int type; JobRequest jobRequest; JobResult jobResult; }; Message receiveMessage(int raw_id) { int id = Receive(raw_id); Message m; m.nodeId = GetInt(id); m.type = GetInt(id); if (m.type == MessageWorkDone) { m.jobResult.start = GetInt(id); m.jobResult.start_next = GetInt(id); m.jobResult.stop_previous = GetInt(id); m.jobResult.stop = GetInt(id); m.jobResult.time = GetInt(id); } else if (m.type == MessageDoWork) { m.jobRequest.start = GetInt(id); m.jobRequest.start_next = GetInt(id); } fprintf(stderr, "[%d<-%d] Received: %d\n", nodeId, id, m.type); return m; } void sendMessage(int id, const Message& m) { PutInt(id, nodeId); PutInt(id, m.type); if (m.type == MessageWorkDone) { PutInt(id, m.jobResult.start); PutInt(id, m.jobResult.start_next); PutInt(id, m.jobResult.stop_previous); PutInt(id, m.jobResult.stop); PutInt(id, m.jobResult.time); } else if (m.type == MessageDoWork) { PutInt(id, m.jobRequest.start); PutInt(id, m.jobRequest.start_next); } Send(id); fprintf(stderr, "[%d->%d] Sending: %d\n", nodeId, id, m.type); // fprintf(stderr, "[%d->%d] Sending: %d, qid: %d, from: %d, to: %d, time: %d\n", nodeId, id, m.type, m.job.queryId, m.job.from, m.job.to, m.job.time); } JobResult doTheWork(JobRequest request, unordered_set<int>& points) { int counter = 0; int previous = request.start; int current = request.start_next; while (true) { ++counter; if (points.count(current) > 0) { // work done JobResult result; result.start = request.start; result.start_next = request.start_next; result.stop_previous = previous; result.stop = current; result.time = counter; return result; } int n = FirstNeighbor(current); if (n == previous) { n = SecondNeighbor(current); } previous = current; current = n; } } void slave() { unordered_set<int> points; for (int i = 1; i <= queriesCount; ++i) { int from = QueryFrom(i); int to = QueryTo(i); points.insert(from); points.insert(to); } fprintf(stderr, "[%d] Entering message loop...\n", nodeId); while (true) { Message request = receiveMessage(0); if (request.type == MessageFinished) return; // do work Message response; response.nodeId = nodeId; response.type = MessageWorkDone; response.jobResult = doTheWork(request.jobRequest, points); sendMessage(0, response); } } void initializeJobs(set<JobRequest>& jobs, map<int, int>& results, set<int>& remaining) { for (int i = 1; i <= queriesCount; ++i) { int from = QueryFrom(i); int to = QueryTo(i); if (from == to) { results[i] = 0; remaining.erase(i); } JobRequest job; job.start = from; job.start_next = FirstNeighbor(from); jobs.insert(job); job.start = from; job.start_next = SecondNeighbor(from); jobs.insert(job); job.start = to; job.start_next = FirstNeighbor(to); jobs.insert(job); job.start = to; job.start_next = SecondNeighbor(to); jobs.insert(job); } } typedef set<int>::iterator siit; void update_fragments(map<JobRequest, JobResult>& fragments, JobResult result) { JobRequest jr; jr.start = result.start; jr.start_next = result.start_next; fragments[jr] = result; JobResult result2; result2.start = result.stop; result2.start_next = result.stop_previous; result2.stop_previous = result.start_next; result2.stop = result.start; result2.time = result.time; jr.start = result2.start; jr.start_next = result2.start_next; fragments[jr] = result2; } void check_paths(int queryId, map<JobRequest, JobResult>& fragments, JobRequest jr, int to, map<int, int>& results, set<int>& found) { int count = 0; map<JobRequest, JobResult>::iterator itFragments; while ((itFragments = fragments.find(jr)) != fragments.end()) { JobResult result = itFragments->second; // fprintf(stderr, "fragment %d, %d, %d, %d; %d->%d\n", result.start, result.start_next, result.stop_previous, result.stop, from, to); count += result.time; if (result.stop == to) { // fprintf(stderr, "fragment found %d->%d\n", queryId, count); results[queryId] = min((LL)count, (LL)studentsCount - count); found.insert(queryId); break; } jr.start = result.stop; jr.start_next = FirstNeighbor(jr.start); if (jr.start_next == result.stop_previous) { jr.start_next = SecondNeighbor(jr.start); } } } void master() { set<JobRequest> jobs; map<int, int> results; set<int> remaining; set<int> availableNodes; set<int> workingNodes; for (int i = 1; i < nodesCount; ++i) { availableNodes.insert(i); } for(int i = 1; i <= queriesCount; ++i) { remaining.insert(i); } initializeJobs(jobs, results, remaining); map<JobRequest, JobResult> fragments; // in loop: // - send job to all children // - do some work too // - receive results from all children fprintf(stderr, "[%d] Entering main loop...\n", nodeId); while (jobs.size() > 0 || workingNodes.size() > 0) // while (remaining.size() > 0) { // allocate unprocessed jobs to available nodes while (availableNodes.size() > 0) { // fprintf(stderr, "[%d] job size %lu\n", nodeId, jobs.size()); set<JobRequest>::iterator itJob = jobs.begin(); if (itJob == jobs.end()) { break; } JobRequest job = *itJob; jobs.erase(itJob); if (fragments.find(job) == fragments.end()) { siit itNode = availableNodes.begin(); int id = *itNode; availableNodes.erase(id); workingNodes.insert(id); Message m; m.type = MessageDoWork; m.jobRequest = job; sendMessage(id, m); } } if (workingNodes.size() > 0) { Message m = receiveMessage(-1); workingNodes.erase(m.nodeId); availableNodes.insert(m.nodeId); update_fragments(fragments, m.jobResult); // fprintf(stderr, "fragments count %lu, remaining %lu\n", fragments.size(), remaining.size()); // check if any path is full set<int> found; for(siit itQuery = remaining.begin(); itQuery != remaining.end(); ++itQuery) { int queryId = *itQuery; int from = QueryFrom(queryId); int to = QueryTo(queryId); JobRequest jr; jr.start = from; jr.start_next = FirstNeighbor(from); check_paths(queryId, fragments, jr, to, results, found); jr.start_next = SecondNeighbor(from); check_paths(queryId, fragments, jr, to, results, found); } for(siit itQuery = found.begin(); itQuery != found.end(); ++itQuery) { remaining.erase(*itQuery); } } } fprintf(stderr, "killing workers...\n"); for(siit itNode = availableNodes.begin(); itNode != availableNodes.end(); ++itNode) { Message m; m.type = MessageFinished; sendMessage(*itNode, m); } // fprintf(stderr, "result size %lu\n", results.size()); for(int i = 1; i <= queriesCount; ++i) { // fprintf(stderr, "answer %d -> %d\n", i, results[i]); printf("%d\n", results[i]); } } int main() { nodeId = MyNodeId(); nodesCount = NumberOfNodes(); studentsCount = NumberOfStudents(); queriesCount = NumberOfQueries(); packetSize = max(10000LL, studentsCount * queriesCount / (100 * nodesCount)); fprintf(stderr, "NodeId: %d, Nodes: %d, Students: %lld, Queries: %d, packet: %lld\n", nodeId, nodesCount, studentsCount, queriesCount, packetSize); if (nodeId == 0) master(); else slave(); 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 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 | #include "kollib.h" #include "message.h" #include <iostream> #include <cmath> #include <queue> #include <set> #include <vector> #include <map> #include <unordered_set> using namespace std; typedef long long LL; bool isManager = false; int nodeId = 0; int nodesCount = 0; int queriesCount = 0; LL studentsCount = 0; LL packetSize = 0; struct JobRequest { int start; int start_next; }; bool operator<(const JobRequest& l, const JobRequest& r) { return (l.start<r.start || (l.start==r.start && l.start_next<r.start_next)); } struct JobResult { int start; int start_next; int stop_previous; int stop; int time; }; enum MessageTypes { MessageFinished = 0, MessageDoWork = 1, MessageWorkDone = 2 }; struct Message { public: int nodeId; int type; JobRequest jobRequest; JobResult jobResult; }; Message receiveMessage(int raw_id) { int id = Receive(raw_id); Message m; m.nodeId = GetInt(id); m.type = GetInt(id); if (m.type == MessageWorkDone) { m.jobResult.start = GetInt(id); m.jobResult.start_next = GetInt(id); m.jobResult.stop_previous = GetInt(id); m.jobResult.stop = GetInt(id); m.jobResult.time = GetInt(id); } else if (m.type == MessageDoWork) { m.jobRequest.start = GetInt(id); m.jobRequest.start_next = GetInt(id); } fprintf(stderr, "[%d<-%d] Received: %d\n", nodeId, id, m.type); return m; } void sendMessage(int id, const Message& m) { PutInt(id, nodeId); PutInt(id, m.type); if (m.type == MessageWorkDone) { PutInt(id, m.jobResult.start); PutInt(id, m.jobResult.start_next); PutInt(id, m.jobResult.stop_previous); PutInt(id, m.jobResult.stop); PutInt(id, m.jobResult.time); } else if (m.type == MessageDoWork) { PutInt(id, m.jobRequest.start); PutInt(id, m.jobRequest.start_next); } Send(id); fprintf(stderr, "[%d->%d] Sending: %d\n", nodeId, id, m.type); // fprintf(stderr, "[%d->%d] Sending: %d, qid: %d, from: %d, to: %d, time: %d\n", nodeId, id, m.type, m.job.queryId, m.job.from, m.job.to, m.job.time); } JobResult doTheWork(JobRequest request, unordered_set<int>& points) { int counter = 0; int previous = request.start; int current = request.start_next; while (true) { ++counter; if (points.count(current) > 0) { // work done JobResult result; result.start = request.start; result.start_next = request.start_next; result.stop_previous = previous; result.stop = current; result.time = counter; return result; } int n = FirstNeighbor(current); if (n == previous) { n = SecondNeighbor(current); } previous = current; current = n; } } void slave() { unordered_set<int> points; for (int i = 1; i <= queriesCount; ++i) { int from = QueryFrom(i); int to = QueryTo(i); points.insert(from); points.insert(to); } fprintf(stderr, "[%d] Entering message loop...\n", nodeId); while (true) { Message request = receiveMessage(0); if (request.type == MessageFinished) return; // do work Message response; response.nodeId = nodeId; response.type = MessageWorkDone; response.jobResult = doTheWork(request.jobRequest, points); sendMessage(0, response); } } void initializeJobs(set<JobRequest>& jobs, map<int, int>& results, set<int>& remaining) { for (int i = 1; i <= queriesCount; ++i) { int from = QueryFrom(i); int to = QueryTo(i); if (from == to) { results[i] = 0; remaining.erase(i); } JobRequest job; job.start = from; job.start_next = FirstNeighbor(from); jobs.insert(job); job.start = from; job.start_next = SecondNeighbor(from); jobs.insert(job); job.start = to; job.start_next = FirstNeighbor(to); jobs.insert(job); job.start = to; job.start_next = SecondNeighbor(to); jobs.insert(job); } } typedef set<int>::iterator siit; void update_fragments(map<JobRequest, JobResult>& fragments, JobResult result) { JobRequest jr; jr.start = result.start; jr.start_next = result.start_next; fragments[jr] = result; JobResult result2; result2.start = result.stop; result2.start_next = result.stop_previous; result2.stop_previous = result.start_next; result2.stop = result.start; result2.time = result.time; jr.start = result2.start; jr.start_next = result2.start_next; fragments[jr] = result2; } void check_paths(int queryId, map<JobRequest, JobResult>& fragments, JobRequest jr, int to, map<int, int>& results, set<int>& found) { int count = 0; map<JobRequest, JobResult>::iterator itFragments; while ((itFragments = fragments.find(jr)) != fragments.end()) { JobResult result = itFragments->second; // fprintf(stderr, "fragment %d, %d, %d, %d; %d->%d\n", result.start, result.start_next, result.stop_previous, result.stop, from, to); count += result.time; if (result.stop == to) { // fprintf(stderr, "fragment found %d->%d\n", queryId, count); results[queryId] = min((LL)count, (LL)studentsCount - count); found.insert(queryId); break; } jr.start = result.stop; jr.start_next = FirstNeighbor(jr.start); if (jr.start_next == result.stop_previous) { jr.start_next = SecondNeighbor(jr.start); } } } void master() { set<JobRequest> jobs; map<int, int> results; set<int> remaining; set<int> availableNodes; set<int> workingNodes; for (int i = 1; i < nodesCount; ++i) { availableNodes.insert(i); } for(int i = 1; i <= queriesCount; ++i) { remaining.insert(i); } initializeJobs(jobs, results, remaining); map<JobRequest, JobResult> fragments; // in loop: // - send job to all children // - do some work too // - receive results from all children fprintf(stderr, "[%d] Entering main loop...\n", nodeId); while (jobs.size() > 0 || workingNodes.size() > 0) // while (remaining.size() > 0) { // allocate unprocessed jobs to available nodes while (availableNodes.size() > 0) { // fprintf(stderr, "[%d] job size %lu\n", nodeId, jobs.size()); set<JobRequest>::iterator itJob = jobs.begin(); if (itJob == jobs.end()) { break; } JobRequest job = *itJob; jobs.erase(itJob); if (fragments.find(job) == fragments.end()) { siit itNode = availableNodes.begin(); int id = *itNode; availableNodes.erase(id); workingNodes.insert(id); Message m; m.type = MessageDoWork; m.jobRequest = job; sendMessage(id, m); } } if (workingNodes.size() > 0) { Message m = receiveMessage(-1); workingNodes.erase(m.nodeId); availableNodes.insert(m.nodeId); update_fragments(fragments, m.jobResult); // fprintf(stderr, "fragments count %lu, remaining %lu\n", fragments.size(), remaining.size()); // check if any path is full set<int> found; for(siit itQuery = remaining.begin(); itQuery != remaining.end(); ++itQuery) { int queryId = *itQuery; int from = QueryFrom(queryId); int to = QueryTo(queryId); JobRequest jr; jr.start = from; jr.start_next = FirstNeighbor(from); check_paths(queryId, fragments, jr, to, results, found); jr.start_next = SecondNeighbor(from); check_paths(queryId, fragments, jr, to, results, found); } for(siit itQuery = found.begin(); itQuery != found.end(); ++itQuery) { remaining.erase(*itQuery); } } } fprintf(stderr, "killing workers...\n"); for(siit itNode = availableNodes.begin(); itNode != availableNodes.end(); ++itNode) { Message m; m.type = MessageFinished; sendMessage(*itNode, m); } // fprintf(stderr, "result size %lu\n", results.size()); for(int i = 1; i <= queriesCount; ++i) { // fprintf(stderr, "answer %d -> %d\n", i, results[i]); printf("%d\n", results[i]); } } int main() { nodeId = MyNodeId(); nodesCount = NumberOfNodes(); studentsCount = NumberOfStudents(); queriesCount = NumberOfQueries(); packetSize = max(10000LL, studentsCount * queriesCount / (100 * nodesCount)); fprintf(stderr, "NodeId: %d, Nodes: %d, Students: %lld, Queries: %d, packet: %lld\n", nodeId, nodesCount, studentsCount, queriesCount, packetSize); if (nodeId == 0) master(); else slave(); return 0; } |