-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtdacc.cpp
More file actions
495 lines (424 loc) · 17 KB
/
tdacc.cpp
File metadata and controls
495 lines (424 loc) · 17 KB
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
#include "bitrange.h"
#include "tdb.h"
#include <algorithm>
#include <array>
#include <cstdint>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <mpi.h>
#include <omp.h>
#include <optional>
#include <queue>
#include <utility>
#define GALAXY_GRAPH_A_BLOCK_SIZE 8192
#define GALAXY_GRAPH_JA_BLOCK_SIZE GALAXY_GRAPH_A_BLOCK_SIZE
#define GALAXY_GRAPH_IA_BLOCK_SIZE 8192
#define SQ(x) ((x) * (x))
struct GalaxyGraph {
std::vector<System> A; // values
std::vector<uint32_t> IA; // accumulated non-sparse counts
std::vector<uint32_t> JA; // columns of values in A
};
struct Solution {
int64_t srcSystemIndex;
int64_t srcStationIndex;
int64_t dstSystemIndex; // might be redundant
int64_t dstStationIndex;
int64_t totalProfit;
};
int64_t computeMaxProfitWithStationIndices(int64_t srcStationIndex,
int64_t dstStationIndex,
const MarketInfo &marketInfo,
int64_t space) {
int64_t total = 0;
int64_t profit = 0;
thread_local std::vector<std::pair<int64_t, int64_t>> pq(1024);
int profitsIndex = 0;
for (int64_t i = 0, j = 0;
i < marketInfo.stations[srcStationIndex].nListings &&
j < marketInfo.stations[dstStationIndex].nListings;) {
const ItemPricing &srcPricing =
marketInfo.listings
[marketInfo.stations[srcStationIndex].listingStartIndex + i];
const ItemPricing &dstPricing =
marketInfo.listings
[marketInfo.stations[dstStationIndex].listingStartIndex + j];
if (srcPricing.itemID == dstPricing.itemID) {
int64_t profitPer = dstPricing.demandPrice - srcPricing.supplyPrice;
if (profitPer > 0) {
pq[profitsIndex].first = profitPer;
pq[profitsIndex].second = srcPricing.supplyQuantity;
profitsIndex++;
}
i++;
j++;
} else if (srcPricing.itemID < dstPricing.itemID) {
i++;
} else {
j++;
}
}
for (int i = 0; i < profitsIndex && total < space; i++) {
bool swapped = false;
for (int j = 0; j < profitsIndex - i - 1; j++) {
if (pq[j].first > pq[j + 1].first) {
std::swap(pq[j], pq[j + 1]);
swapped = true;
}
if (!swapped) {
break;
}
}
auto [profitPer, nAvailable] = pq[profitsIndex - i - 1];
nAvailable = std::min(nAvailable, space - total);
total += nAvailable;
profit += nAvailable * profitPer;
}
return profit;
};
int traverse(const MarketInfo &marketInfo, const GalaxyGraph &graph,
std::optional<const std::vector<Solution> *> previousSolutions,
std::vector<Solution> &solutions,
std::vector<omp_lock_t> &solutionLocks, BitRange &visitedSet,
int64_t startSystemIndex, int64_t jumps, int capacity) {
// BitRange visitedSet(marketInfo.systems.size());
struct Node {
int64_t index;
int64_t depth;
};
std::queue<Node> queue;
queue.push({startSystemIndex, 0});
visitedSet.set(startSystemIndex);
while (!queue.empty()) {
Node curr = queue.front();
queue.pop();
uint32_t startSize = graph.IA[curr.index];
uint32_t endSize = graph.IA[curr.index + 1];
uint32_t nAdjacent = endSize - startSize;
int64_t srcEndStationIndex =
marketInfo.systems[startSystemIndex].stationStartIndex +
marketInfo.systems[startSystemIndex].nStations;
int64_t dstEndStationIndex =
marketInfo.systems[curr.index].stationStartIndex +
marketInfo.systems[curr.index].nStations;
for (int64_t srcStationIndex =
marketInfo.systems[startSystemIndex].stationStartIndex;
srcStationIndex < srcEndStationIndex; srcStationIndex++) {
for (int64_t dstStationIndex =
marketInfo.systems[curr.index].stationStartIndex;
dstStationIndex < dstEndStationIndex; dstStationIndex++) {
int64_t profit = computeMaxProfitWithStationIndices(
srcStationIndex, dstStationIndex, marketInfo, capacity);
if (previousSolutions) {
profit += (*(previousSolutions.value()))[srcStationIndex]
.totalProfit;
}
// begin critical section
omp_set_lock(&solutionLocks[dstStationIndex]);
if (profit > solutions[dstStationIndex].totalProfit) {
solutions[dstStationIndex] = Solution{
.srcSystemIndex = startSystemIndex,
.srcStationIndex = srcStationIndex,
.dstSystemIndex = curr.index,
.dstStationIndex = dstStationIndex,
.totalProfit = profit,
};
}
omp_unset_lock(&solutionLocks[dstStationIndex]);
// end critical section
}
}
if (curr.depth < jumps) {
for (uint32_t i = 0; i < nAdjacent; i++) {
const System &system = graph.A[startSize + i];
if (!visitedSet.get(system.index)) {
queue.push({
.index = system.index,
.depth = curr.depth + 1,
});
visitedSet.set(system.index);
}
}
}
}
return 0;
}
int main(int argc, char **argv) {
if (argc < 5) {
fprintf(stderr, "please enter arguments in the following order: "
"jump_range, n_jumps, n_hops, capacity\n");
return 0;
}
double maxJump = std::stod(argv[1]);
int nJumps = std::stoi(argv[2]);
int nHops = std::stoi(argv[3]);
int capacity = std::stoi(argv[4]);
MPI_Init(&argc, &argv);
int worldSize;
MPI_Comm_size(MPI_COMM_WORLD, &worldSize);
int rank;
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
if (rank == 0) {
std::cout << "jump range: " << maxJump << std::endl;
std::cout << "jumps: " << nJumps << std::endl;
std::cout << "hops: " << nHops << std::endl;
std::cout << "capacity: " << capacity << std::endl;
}
// std::vector<System> systems = tdb.loadSystems();
MarketInfo marketInfo;
double loadStart = omp_get_wtime();
if (rank == 0) {
TDB tdb("data/TradeDangerous.db");
if (tdb.loadMarketInfo(&marketInfo)) {
exit(EXIT_FAILURE);
}
}
std::cout << "load wtime: " << omp_get_wtime() - loadStart << std::endl;
if (worldSize > 1) {
int stationsLen = marketInfo.stations.size();
int systemsLen = marketInfo.systems.size();
int listingsLen = marketInfo.listings.size();
MPI_Bcast(&stationsLen, 1, MPI_INT, 0, MPI_COMM_WORLD);
MPI_Bcast(&systemsLen, 1, MPI_INT, 0, MPI_COMM_WORLD);
MPI_Bcast(&listingsLen, 1, MPI_INT, 0, MPI_COMM_WORLD);
if (rank != 0) {
marketInfo.stations.resize(stationsLen);
marketInfo.systems.resize(systemsLen);
marketInfo.listings.resize(listingsLen);
}
MPI_Bcast(marketInfo.stations.data(),
marketInfo.stations.size() * sizeof(Station), MPI_BYTE, 0,
MPI_COMM_WORLD);
MPI_Bcast(marketInfo.systems.data(),
marketInfo.systems.size() * sizeof(System), MPI_BYTE, 0,
MPI_COMM_WORLD);
MPI_Bcast(marketInfo.listings.data(),
marketInfo.listings.size() * sizeof(ItemPricing), MPI_BYTE, 0,
MPI_COMM_WORLD);
}
if (rank == 0) {
std::cout << "system count: " << marketInfo.systems.size() << std::endl;
std::cout << "station count: " << marketInfo.stations.size()
<< std::endl;
std::cout << "listing count: " << marketInfo.listings.size()
<< std::endl;
}
GalaxyGraph graph;
graph.IA.reserve(GALAXY_GRAPH_IA_BLOCK_SIZE);
graph.A.reserve(GALAXY_GRAPH_A_BLOCK_SIZE);
graph.JA.reserve(GALAXY_GRAPH_JA_BLOCK_SIZE);
if (rank == 0) {
graph.IA.push_back(0);
uint32_t nnz = 0;
for (int i = 0; i < marketInfo.systems.size(); i++) {
for (int j = 0; j < marketInfo.systems.size(); j++) {
if (i == j)
continue;
double sqDist =
SQ(marketInfo.systems[i].x - marketInfo.systems[j].x) +
SQ(marketInfo.systems[i].y - marketInfo.systems[j].y) +
SQ(marketInfo.systems[i].z - marketInfo.systems[j].z);
if (sqDist <= SQ(maxJump)) {
graph.A.push_back(marketInfo.systems[j]);
graph.JA.push_back(j);
nnz++;
}
}
graph.IA.push_back(nnz);
}
}
if (worldSize > 1) {
int iaLen = graph.IA.size();
int jaLen = graph.JA.size();
int aLen = graph.A.size();
MPI_Bcast(&iaLen, 1, MPI_INT32_T, 0, MPI_COMM_WORLD);
MPI_Bcast(&jaLen, 1, MPI_INT32_T, 0, MPI_COMM_WORLD);
MPI_Bcast(&aLen, 1, MPI_INT32_T, 0, MPI_COMM_WORLD);
if (rank != 0) {
graph.IA.resize(iaLen);
graph.JA.resize(jaLen);
graph.A.resize(aLen);
}
MPI_Bcast(graph.IA.data(), graph.IA.size() * sizeof(uint32_t), MPI_BYTE,
0, MPI_COMM_WORLD);
MPI_Bcast(graph.JA.data(), graph.JA.size() * sizeof(uint32_t), MPI_BYTE,
0, MPI_COMM_WORLD);
MPI_Bcast(graph.A.data(), graph.A.size() * sizeof(System), MPI_BYTE, 0,
MPI_COMM_WORLD);
}
int32_t solIndex = -1;
for (int32_t i = 0; i < marketInfo.systems.size(); i++) {
if (marketInfo.systems[i].id == 10477373803) {
solIndex = i;
break;
}
}
if (solIndex < 0) {
fprintf(stderr, "failed to find sol!\n");
return 1;
} else {
fprintf(stderr, "found sol at index %d\n", solIndex);
}
std::vector<Solution> solutions(marketInfo.stations.size());
std::vector<omp_lock_t> solutionLocks(solutions.size());
for (omp_lock_t &lock : solutionLocks) {
omp_init_lock(&lock);
}
std::cout << "begin initial traversal..." << std::endl;
BitRange visitedSet(marketInfo.systems.size());
traverse(marketInfo, graph, std::nullopt, solutions, solutionLocks,
visitedSet, solIndex, nJumps, capacity);
Solution optimalSolution = solutions[0];
for (Solution solution : solutions) {
if (solution.totalProfit > optimalSolution.totalProfit) {
optimalSolution = solution;
}
}
size_t originCount = 0;
for (size_t i = 0; i < visitedSet.size; i++) {
if (visitedSet.get(i)) {
originCount++;
}
}
std::vector<std::vector<Solution>> previousSolutions = {solutions};
BitRange previousVisitedSet(visitedSet.size);
for (size_t i = 0; i < visitedSet.size; i++) {
if (visitedSet.get(i)) {
previousVisitedSet.set(i);
}
}
for (int hopNum = 1; hopNum < nHops; hopNum++) {
int startIndex = 0;
int endIndex = previousVisitedSet.size;
// compute start and end indices if we're using MPI
if (worldSize > 1) {
int count = 0;
int originsPerRank = originCount / worldSize;
int currentRank = 0;
for (int32_t i = 0; i < previousVisitedSet.size; i++) {
if (previousVisitedSet.get(i)) {
count++;
}
if (count == (originsPerRank * rank)) {
startIndex = i;
endIndex = startIndex + originsPerRank;
}
if (count == (originsPerRank * (rank + 1))) {
endIndex = i;
if (rank == worldSize - 1) {
endIndex = previousVisitedSet.size;
}
}
}
}
std::vector<Solution> hopSolutions(marketInfo.stations.size());
BitRange hopVisitedSet(marketInfo.systems.size());
int32_t nIterations = 0;
#pragma omp parallel for schedule(dynamic)
for (int32_t i = startIndex; i < endIndex; i++) {
BitRange hopVisitedSetTraverse(marketInfo.systems.size());
if (previousVisitedSet.get(i)) {
traverse(marketInfo, graph,
&previousSolutions[previousSolutions.size() - 1],
hopSolutions, solutionLocks, hopVisitedSetTraverse, i,
nJumps, capacity);
nIterations++;
if (nIterations % 100 == 0) {
printf("%d/%zu\n", nIterations, originCount);
}
}
for (int32_t j = 0; j < hopVisitedSetTraverse.size; j++) {
if (hopVisitedSetTraverse.get(j))
hopVisitedSet.set(j);
}
}
if (worldSize > 1) {
if (rank == 0) {
BitRange tempRange(visitedSet.size);
std::vector<Solution> tempHopSolutions(
marketInfo.stations.size());
for (int i = 1; i < worldSize; i++) {
MPI_Recv(tempRange.buf, tempRange.nmemb, MPI_UINT64_T, i, 0,
MPI_COMM_WORLD, MPI_STATUS_IGNORE);
MPI_Recv((void *)&tempHopSolutions[0],
tempHopSolutions.size() * sizeof(Solution),
MPI_BYTE, i, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
// combine visited set
for (int32_t j = 0; j < tempRange.size; j++) {
if (tempRange.get(j))
hopVisitedSet.set(j);
}
// combine solutions (I.E max of the two)
for (int32_t j = 0; j < tempHopSolutions.size(); j++) {
if (tempHopSolutions[j].totalProfit >
hopSolutions[j].totalProfit) {
hopSolutions[j] = tempHopSolutions[j];
}
}
}
} else {
MPI_Send(hopVisitedSet.buf, hopVisitedSet.nmemb, MPI_UINT64_T,
0, 0, MPI_COMM_WORLD);
MPI_Send(hopSolutions.data(),
hopSolutions.size() * sizeof(Solution), MPI_BYTE, 0, 0,
MPI_COMM_WORLD);
}
// sync
MPI_Bcast(hopVisitedSet.buf, hopVisitedSet.nmemb, MPI_UINT64_T, 0,
MPI_COMM_WORLD);
MPI_Bcast(hopSolutions.data(),
hopSolutions.size() * sizeof(Solution), MPI_BYTE, 0,
MPI_COMM_WORLD);
}
originCount = 0;
for (size_t i = 0; i < hopVisitedSet.size; i++) {
if (hopVisitedSet.get(i)) {
originCount++;
}
}
for (size_t i = 0; i < hopVisitedSet.size; i++) {
if (hopVisitedSet.get(i)) {
previousVisitedSet.set(i);
}
}
previousSolutions.push_back(hopSolutions);
}
// for (size_t i = previousSolutions.size() - 1; i >= 0; i--) {
if (rank != 0) {
return 0;
}
int i = previousSolutions.size() - 1;
optimalSolution = previousSolutions[i][0];
for (int64_t j = 0; j < previousSolutions[i].size(); j++) {
Solution &solution = previousSolutions[i][j];
if (solution.totalProfit > optimalSolution.totalProfit) {
optimalSolution = solution;
}
}
printf("hop %d:\n", nHops);
printf("\toptimal solution to system id %ld and station id %ld from system "
"id %ld and station id %ld with profit "
"%ld\n",
marketInfo.systems[optimalSolution.dstSystemIndex].id,
marketInfo.stations[optimalSolution.dstStationIndex].id,
marketInfo.systems[optimalSolution.srcSystemIndex].id,
marketInfo.stations[optimalSolution.srcStationIndex].id,
optimalSolution.totalProfit);
for (int j = i - 1; j >= 0; j--) {
optimalSolution = previousSolutions[j][optimalSolution.srcStationIndex];
printf("hop %d:\n", j + 1);
printf("\toptimal solution to system id %ld and station id %ld from "
"system "
"id %ld and station id %ld with profit "
"%ld\n",
marketInfo.systems[optimalSolution.dstSystemIndex].id,
marketInfo.stations[optimalSolution.dstStationIndex].id,
marketInfo.systems[optimalSolution.srcSystemIndex].id,
marketInfo.stations[optimalSolution.srcStationIndex].id,
optimalSolution.totalProfit);
}
// }
return 0;
}