-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathknapsackDYN_omp.c
More file actions
97 lines (77 loc) · 2.19 KB
/
knapsackDYN_omp.c
File metadata and controls
97 lines (77 loc) · 2.19 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
// A Dynamic Programming based solution for 0-1 Knapsack problem
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <sys/time.h>
#include <string.h>
#include <omp.h>
// A utility function that returns maximum of two integers
int max(int a, int b) { return (a > b)? a : b; }
// Returns the maximum value that can be put in a knapsack of capacity W
static int knapSack(long int W, long int N, int wt[], int val[])
{
int *K = (int*) malloc((W+1)*sizeof(int));
int *Kp = (int*) malloc((W+1)*sizeof(int));
long int i, w;
for (i = 0; i <= N; i++)
{
#pragma omp parallel for
for (w = 0; w <= W; w++)
{
if (i==0 || w==0)
K[w] = 0;
else if (wt[i-1] <= w)
K[w] = max(val[i-1] + Kp[w-wt[i-1]], Kp[w]);
else
K[w] = Kp[w];
}
int *tmp = Kp;
Kp = K;
K = tmp;
}
int result = K[W];
free(K);
free(Kp);
return result;
}
int main(int argc, char **argv){
FILE *test_file;
int *val, *wt; // width and cost values
long int Nitems; // Number of items
long int Width; // Max. load to carry
long int cont; // counter
double tpivot1=0,tpivot2=0,tpivot3=0; //time counting
struct timeval tim;
if (argc < 2) {
printf("Usage: ./%s <test_file> [numer_of_threads]\n", argv[0]);
return 1;
}
if (argc == 3) {
omp_set_num_threads(atoi(argv[2]));
}
//Capture first token time - init execution
gettimeofday(&tim, NULL);
tpivot1 = tim.tv_sec+(tim.tv_usec/1000000.0);
if (!(test_file=fopen(argv[1],"r"))) {
printf("Error opening Value file: %s\n",argv[1]);
return 1;
}
//Reading number of items and Maximum width
fscanf(test_file,"%ld %ld\n",&Nitems, &Width);
val = (int *)malloc(Nitems*sizeof(int)); //values for each element
wt = (int *)malloc(Nitems*sizeof(int)); //width for each element
//Reading value and width for each element
for (cont=0;cont<Nitems;cont++){
fscanf(test_file,"%d,%d\n",&val[cont],&wt[cont]);
}
gettimeofday(&tim, NULL);
tpivot2 = (tim.tv_sec+(tim.tv_usec/1000000.0));
printf("%ld:%ld:%d", Width, Nitems, knapSack(Width,Nitems, wt, val));
gettimeofday(&tim, NULL);
tpivot3 = (tim.tv_sec+(tim.tv_usec/1000000.0));
printf(":%.6lf:%.6lf\n", tpivot3-tpivot2,tpivot3-tpivot1);
free(val);
free(wt);
fclose(test_file);
return 0;
}