-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKnapsack.cu
460 lines (368 loc) · 19.2 KB
/
Knapsack.cu
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
#include "Knapsack.cuh"
void checkInputs(int argc, char* argv[]){
if (argc != 2) {
cerr << "INVALID USAGE: ./knapsack <input file>" << endl;
exit(1);
}
inputFile.open(argv[1]);
if (!inputFile.is_open()) {
cerr << "File does not exist or could not be opened" << endl;
exit(1);
}
}
void readFile(ifstream *inputFile) {
// Read the first two things in the file as the number of items and capacity
*inputFile >> NUM_ITEMS >> capacity;
// Read each item (value, then weight) into the itemChoices vector
for (int i = 0; i < NUM_ITEMS; i++) {
Item item;
*inputFile >> item.value >> item.weight; // Switch to value first, then weight
itemChoices.push_back(item);
}
// Try reading the optimal solution if it's there
if (*inputFile >> std::ws) { // Check if there's more data
optimalSolution.clear(); // Clear previous optimal solution if any
for (int i = 0; i < NUM_ITEMS; i++) {
int bit;
*inputFile >> bit;
optimalSolution.push_back(static_cast<char>(bit));
}
}
cout << "Read " << NUM_ITEMS << " items with capacity " << capacity << endl;
}
void initializeBestPerUnitWeights(){
// using this as a float * rather than vector makes passing it to the GPU easier later.
bestPerWeightValues = (float *)malloc(NUM_ITEMS * sizeof(float) + 1);
// if we iterate back to front, we can do this in linear time rather than having to search the entire rest of the array each time
for (int i = NUM_ITEMS - 1; i >= 0; i--) {
float thisRatio = (float)itemChoices[i].value / itemChoices[i].weight;
// if it's the first item we compute, it's just that value, else it's the max of this value and the last value
bestPerWeightValues[i] = (i == NUM_ITEMS - 1) ? thisRatio : max(bestPerWeightValues[i + 1], thisRatio);
}
bestPerWeightValues[NUM_ITEMS] = 0;
}
__global__ void greedySearch(int *weights, int *values, char *solutions, int *solutionValues, int NUM_ITEMS, int capacity) {
int startItem = blockIdx.x * blockDim.x + threadIdx.x;
// if we're in range, we copy over the data at our slot.
if (startItem >= NUM_ITEMS) return;
// fetch our weight and value for the first item which we are taking no matter what.
int weight = weights[startItem];
int value = (weight > capacity) ? -1 : values[startItem];
// mark this item used
solutions[startItem * NUM_ITEMS + startItem] = 1;
// Perform greedy search
while (weight < capacity) {
int bestItem = -1;
int bestItemValue = -1;
int bestItemWeight = -1;
// iterate through every item, each time around.
for (int i = 0; i < NUM_ITEMS; i++) {
// we need to have a better price than the current best, be available, and not be exceeding capacity
if ((values[i] > bestItemValue) && (solutions[startItem * NUM_ITEMS + i] == '0') && (weight + weights[i] <= capacity)) {
bestItem = i;
bestItemValue = values[i];
bestItemWeight = weights[i];
}
}
// if we didn't find a suitor, break
if (bestItem == -1)
break;
// mark the best item as used
solutions[startItem * NUM_ITEMS + bestItem] = '1';
// update our local variables that track our progress
weight += bestItemWeight;
value += bestItemValue;
}
// Store the solution value in shared memory and later copy to global memory
solutionValues[startItem] = value;
}
void launchGreedySearch() {
// Allocate CPU memory for item weights and values
int *hostItemWeights = (int *)malloc(NUM_ITEMS * sizeof(int));
int *hostItemValues = (int *)malloc(NUM_ITEMS * sizeof(int));
// Populate host arrays with item data
for (int i = 0; i < NUM_ITEMS; i++) {
hostItemWeights[i] = itemChoices[i].weight;
hostItemValues[i] = itemChoices[i].value;
}
// Allocate device memory for our four arrays
int *deviceItemWeights, *deviceItemValues, *deviceSolutionValues;
char *deviceSolutions;
// Allocate device memory
cudaMalloc(&deviceItemWeights, NUM_ITEMS * sizeof(int));
cudaMalloc(&deviceItemValues, NUM_ITEMS * sizeof(int));
cudaMalloc(&deviceSolutions, NUM_ITEMS * NUM_ITEMS * sizeof(char));
cudaMalloc(&deviceSolutionValues, NUM_ITEMS * sizeof(int));
// Copy data to device for those bad boys
cudaMemcpy(deviceItemWeights, hostItemWeights, NUM_ITEMS * sizeof(int), cudaMemcpyHostToDevice);
cudaMemcpy(deviceItemValues, hostItemValues, NUM_ITEMS * sizeof(int), cudaMemcpyHostToDevice);
cudaMemset(deviceSolutions, '0', NUM_ITEMS * NUM_ITEMS * sizeof(char)); // it makes more sense to use global memory here for the solutions than local thread memory, since if our N gets big that is an issue.
cudaMemset(deviceSolutionValues, 0, NUM_ITEMS * sizeof(int));
int numThreadsPerBlock = 1024;
int numBlocks = (NUM_ITEMS + numThreadsPerBlock - 1) / numThreadsPerBlock;
// Launch the kernel
greedySearch<<<numBlocks, numThreadsPerBlock>>>(deviceItemWeights, deviceItemValues, deviceSolutions, deviceSolutionValues, NUM_ITEMS, capacity);
cudaDeviceSynchronize();
// Copy the solution values back to the host
int *hostSolutionValues = (int *)malloc(NUM_ITEMS * sizeof(int));
char *hostSolutions = (char *)malloc(NUM_ITEMS * NUM_ITEMS * sizeof(char));
cudaMemcpy(hostSolutionValues, deviceSolutionValues, NUM_ITEMS * sizeof(int), cudaMemcpyDeviceToHost);
cudaMemcpy(hostSolutions, deviceSolutions, NUM_ITEMS * NUM_ITEMS * sizeof(char), cudaMemcpyDeviceToHost);
// update our global best when we find improvements
for (int i = 0; i < NUM_ITEMS; i++) {
if (hostSolutionValues[i] > globalBest){
globalBest = hostSolutionValues[i];
memcpy(globalBestChoices, &hostSolutions[i * NUM_ITEMS], NUM_ITEMS * sizeof(char));
}
}
// Clean up memory
free(hostItemWeights);
free(hostItemValues);
free(hostSolutionValues);
free(hostSolutions);
cudaFree(deviceItemWeights);
cudaFree(deviceItemValues);
cudaFree(deviceSolutions);
cudaFree(deviceSolutionValues);
}
__global__ void computeNextItems(int *returningValues, int *returningWeights, int *returningUpperBounds, int *tableOfValues, int *tableOfWeights, float nextRatio, int numToCompute, int maxBranches, int capacity, int currentWeight, int currentValue) {
extern __shared__ int memory[];
int iD = blockIdx.x * blockDim.x + threadIdx.x;
// get these pointers so that we can access our values in shared memory easily.
int *itemValues = &memory[0];
int *itemWeights = &memory[numToCompute];
// if you're in the range of the amount of items we need to compute, you're getting put to work copying stuff to shared memory
if (threadIdx.x < numToCompute) {
itemValues[threadIdx.x] = tableOfValues[threadIdx.x];
itemWeights[threadIdx.x] = tableOfWeights[threadIdx.x];
}
if (iD >= maxBranches) {
return;
}
// if we are over the number of branches we could possible solve, but under the default max, we just put -1's in because we're invalid, but our slots of the array are still allocated.
if (iD >= 1 << numToCompute) {
returningValues[iD] = -1;
returningWeights[iD] = -1;
returningUpperBounds[iD] = -1;
return;
}
// sync all the threads
__syncthreads();
// now we compute the weight and value of choosing the combination which corresponds to the threadIDs
int weight = currentWeight;
int value = currentValue;
// take those items which correspond to our threadID.
for (int i = 0; i < numToCompute; i++) {
if (iD & (1 << i)) {
weight += itemWeights[i];
value += itemValues[i];
}
}
// put our values back where they belong in their array, if weight is over, we put everything as -1
if (weight <= capacity) {
returningValues[iD] = value;
returningWeights[iD] = weight;
returningUpperBounds[iD] = value + (int)(nextRatio * (capacity - weight)); // our upper bound is the room we have left in the bag times the best possible value per pound remaining.
}
else {
returningValues[iD] = -1;
returningWeights[iD] = -1;
returningUpperBounds[iD] = -1;
}
}
void launchBranchAndBound(){
// TODO: find a way to track our solution char *'s as well.
deque<Solution *> solutionQueue;
deque<Solution *> usedSolutions;
time_t startTime = time(NULL);
// make and initialize all our streams
cudaStream_t* streams = new cudaStream_t[MAX_KERNEL_COUNT]; // Dynamically allocate array
for (int i = 0; i < MAX_KERNEL_COUNT; i++) {
cudaStreamCreate(&streams[i]); // Initialize each stream
}
// initialize our base solution
char *startingSolution = (char *)malloc(NUM_ITEMS * sizeof(char));
memset(startingSolution, '0', NUM_ITEMS * sizeof(char));
// create our initial solution
Solution *s = (Solution *)malloc(sizeof(Solution));
s->currentValue = 0;
s->currentWeight = 0;
s->upperBound = (int)(bestPerWeightValues[0] * capacity);
s->currentItem = 0;
s->itemChoices = (char *)malloc(NUM_ITEMS * sizeof(char));
memcpy(s->itemChoices, startingSolution, NUM_ITEMS * sizeof(char));
free(startingSolution);
// add it to the queue
solutionQueue.push_back(s);
// this is how many slots are in every return array how many solutions to do at once
int numBranches = pow(2, NUM_ITEMS_AT_ONCE);
// now we initialize all our memory.
// 3 arrays for the returning values on host and device. FOR THE AMOUNT OF KERNELS WE HAVE, so MAX_NUM_KERNELS * 6 arrays, 3 and 3 host/device
int **d_currentValues, **d_currentWeights, **d_upperBounds, **h_currentValues, **h_currentWeights, **h_upperBounds;
// these two are the lookup tables for the items
int **d_itemWeights, **d_itemValues;
// Allocate memory for the arrays of pointers themselves
d_currentValues = (int **)malloc(MAX_KERNEL_COUNT * sizeof(int *));
d_currentWeights = (int **)malloc(MAX_KERNEL_COUNT * sizeof(int *));
d_upperBounds = (int **)malloc(MAX_KERNEL_COUNT * sizeof(int *));
h_currentValues = (int **)malloc(MAX_KERNEL_COUNT * sizeof(int *));
h_currentWeights = (int **)malloc(MAX_KERNEL_COUNT * sizeof(int *));
h_upperBounds = (int **)malloc(MAX_KERNEL_COUNT * sizeof(int *));
d_itemWeights = (int **)malloc(MAX_KERNEL_COUNT * sizeof(int *));
d_itemValues = (int **)malloc(MAX_KERNEL_COUNT * sizeof(int *));
// allocate all our stuff
for(int i = 0; i < MAX_KERNEL_COUNT; i++){
// the returning values from the computations
cudaMalloc(&d_currentValues[i], numBranches * sizeof(int));
cudaMalloc(&d_currentWeights[i], numBranches * sizeof(int));
cudaMalloc(&d_upperBounds[i], numBranches * sizeof(int));
// these are the lookup tables for the items we are currently selecting
cudaMalloc(&d_itemWeights[i], NUM_ITEMS_AT_ONCE * sizeof(int));
cudaMalloc(&d_itemValues[i], NUM_ITEMS_AT_ONCE * sizeof(int));
// houses for the returning values
h_currentValues[i] = (int *)malloc(numBranches * sizeof(int));
h_currentWeights[i] = (int *)malloc(numBranches * sizeof(int));
h_upperBounds[i] = (int *)malloc(numBranches * sizeof(int));
}
// now, each time we simply go through, blast off a bunch of kernels, join them back and see the results.
// we stop once the queue is empty after we've generated all solutions
// use a long for this because this may get hairy
long int totalKernelLaunches = 0;
while(solutionQueue.size() > 0){
// make up to MAX_KERNEL_COUNT kernels
int numKernels = min(MAX_KERNEL_COUNT, solutionQueue.size());
// launch all our kernels
int launchedKernels = 0;
while(launchedKernels < numKernels && solutionQueue.size() > 0){
Solution *s = solutionQueue.front();
solutionQueue.pop_front();
if (s->currentItem >= NUM_ITEMS){
free(s->itemChoices);
free(s);
continue;
}
int itemsToCompute = min(NUM_ITEMS - s->currentItem, NUM_ITEMS_AT_ONCE);
int h_itemWeights[itemsToCompute];
int h_itemValues[itemsToCompute];
for (int i = 0; i < itemsToCompute; i++){
h_itemWeights[i] = itemChoices[s->currentItem + i].weight;
h_itemValues[i] = itemChoices[s->currentItem + i].value;
}
// copy the items to the device
cudaMemcpy(d_itemWeights[launchedKernels], h_itemWeights, itemsToCompute * sizeof(int), cudaMemcpyHostToDevice);
cudaMemcpy(d_itemValues[launchedKernels], h_itemValues, itemsToCompute * sizeof(int), cudaMemcpyHostToDevice);
int numThreadsPerBlock = 1024;
int numBlocks = (numBranches/numThreadsPerBlock) + 1;// this allows us to launch 1024 threads per block, and launch many blocks
// the shared memory size just needs to be the two lookup tables. this will be faster than using the global arrays, apparently by like 100x according to nvidia website.
size_t sharedMemSize = (NUM_ITEMS_AT_ONCE * 2 * sizeof(int));
// now we launch our kernel, using the corresponding stream
computeNextItems<<<numBlocks, numThreadsPerBlock, sharedMemSize, streams[launchedKernels]>>>(d_currentValues[launchedKernels], d_currentWeights[launchedKernels], d_upperBounds[launchedKernels], d_itemValues[launchedKernels], d_itemWeights[launchedKernels], bestPerWeightValues[s->currentItem + itemsToCompute], itemsToCompute, numBranches, capacity, s->currentWeight, s->currentValue);
launchedKernels++;
totalKernelLaunches++;
// put it in the used solutions list, now we can look back at this solution later in processing.
usedSolutions.push_back(s);
}
// get their results
for(int i = 0; i < launchedKernels; i++){
// copy the results back to the host
cudaStreamSynchronize(streams[i]);
cudaMemcpyAsync(h_currentValues[i], d_currentValues[i], numBranches * sizeof(int), cudaMemcpyDeviceToHost, streams[i]);
cudaMemcpyAsync(h_currentWeights[i], d_currentWeights[i], numBranches * sizeof(int), cudaMemcpyDeviceToHost, streams[i]);
cudaMemcpyAsync(h_upperBounds[i], d_upperBounds[i], numBranches * sizeof(int), cudaMemcpyDeviceToHost, streams[i]);
}
// put it all back together into solutions
for(int i = 0; i < launchedKernels; i++){
// synchronize our stream, make sure it's done copying over stuff
cudaStreamSynchronize(streams[i]);
// get the solution which launched this kernel
Solution *seedSolution = usedSolutions.front();
usedSolutions.pop_front();
// now go through every branch, each result of this kernel
for(int j = 0; j < numBranches; j++){
// if we're invalid for any reason, we just continue
if (h_currentValues[i][j] == -1 || h_upperBounds[i][j] < globalBest || h_currentWeights[i][j] > capacity){
continue;
}
// now we look at all the solutions that seedSolution made, and append their information to seeds basically
Solution *s = (Solution *)malloc(sizeof(Solution));
// set up the char array representing our current partial solution.
// copy over the parent solution, and then use bitwise operation based on the iD of this branch to get the selection this one represents
s->itemChoices = (char *)malloc(NUM_ITEMS * sizeof(char));
memcpy(s->itemChoices, seedSolution->itemChoices, NUM_ITEMS * sizeof(char));
// j represents the iD which this solution came from, so the bitwise representation represents all those decisions of this given branch
for (int k = 0; k < min(NUM_ITEMS - seedSolution->currentItem, NUM_ITEMS_AT_ONCE); k++) {
s->itemChoices[k + seedSolution->currentItem] = ((j & (1 << k)) ? '1' : '0');
}
// copy the rest of the stuff into our new solution
s->currentValue = h_currentValues[i][j];
s->currentWeight = h_currentWeights[i][j];
s->upperBound = h_upperBounds[i][j];
s->currentItem = seedSolution->currentItem + NUM_ITEMS_AT_ONCE;
// if we have a new best value, update it
if (s->currentValue > globalBest){
globalBest = s->currentValue;
memcpy(globalBestChoices, s->itemChoices, NUM_ITEMS * sizeof(char));
}
// put it into the solutionQueue
solutionQueue.push_back(s);
}
// free our parent Solution
free(seedSolution->itemChoices);
free(seedSolution);
}
}
for(int i = 0; i < MAX_KERNEL_COUNT; i++){
cudaFree(d_currentValues[i]);
cudaFree(d_currentWeights[i]);
cudaFree(d_upperBounds[i]);
cudaFree(d_itemWeights[i]);
cudaFree(d_itemValues[i]);
free(h_currentValues[i]);
free(h_currentWeights[i]);
free(h_upperBounds[i]);
}
free(d_currentValues);
free(d_currentWeights);
free(d_upperBounds);
free(d_itemWeights);
free(d_itemValues);
free(h_currentValues);
free(h_currentWeights);
free(h_upperBounds);
// free all the streams NOW
for (int i = 0; i < MAX_KERNEL_COUNT; i++){
cudaStreamDestroy(streams[i]);
}
delete[] streams;
cout << "\nFinished branch and bound!\n";
cout <<"\nBranch and bound stats:\n";
time_t totalTime = time(NULL) - startTime;
cout << "\tTotal Kernel Launches: " << totalKernelLaunches << endl;
cout << "\tTotal Time (Seconds): " << totalTime << endl;
}
int main(int argc, char* argv[]) {
checkInputs(argc, argv);
readFile(&inputFile); // Passing in our ifstream
// this is useful in the branching and bounding phase to compute upper bounds without iterating through the entire array.
// its a lookup table for the item we are at, and the best value per weight after it.
initializeBestPerUnitWeights();
globalBest = 0;
globalBestChoices = (char *)malloc(NUM_ITEMS * sizeof(char));
memset(globalBestChoices, '0', NUM_ITEMS * sizeof(char));
// run the greedy search to find a good initial solution
launchGreedySearch();
cout << "Finished greedy search and found best value of " << globalBest << endl;
cout << "\nStarting branch and bound...\n";
// run the branch and bound algorithm now.
launchBranchAndBound();
cout << "\nGLOBAL BEST VALUE: " << globalBest << endl;
// print out the best solution
cout << "\nMost Optimal Solution: ";
for (int i = 0; i < NUM_ITEMS; i++) {
printf("%c ", globalBestChoices[i]);
}
printf("\n");
free(bestPerWeightValues);
free(globalBestChoices);
return 0;
}