-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpathfinder.cpp
executable file
·135 lines (115 loc) · 2.84 KB
/
pathfinder.cpp
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
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <assert.h>
#include <omp.h>
#include "timer.h"
void run(int argc, char** argv);
/* define timer macros */
#define pin_stats_reset() startTime()
#define pin_stats_pause(cycles) stopTime(cycles)
#define pin_stats_dump(cycles,s) printf("%s timer: %f\n",s, cycles)
#define BENCH_PRINT
int rows, cols;
int* data;
int** wall;
int* result;
#define M_SEED 9
void
init(int argc, char** argv)
{
if(argc==3){
cols = atoi(argv[1]);
rows = atoi(argv[2]);
}else{
printf("Usage: pathfiner width num_of_steps\n");
exit(0);
}
data = new int[rows*cols];
wall = new int*[rows];
for(int n=0; n<rows; n++)
wall[n]=data+cols*n;
result = new int[cols];
int seed = M_SEED;
srand(seed);
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
wall[i][j] = (i*j) %10;
}
}
for (int j = 0; j < cols; j++)
result[j] = wall[0][j];
#ifdef BENCH_PRINT
/* for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
printf("%d ",wall[i][j]) ;
}
printf("\n") ;
}*/
#endif
}
void
fatal(char *s)
{
fprintf(stderr, "error: %s\n", s);
}
#define IN_RANGE(x, min, max) ((x)>=(min) && (x)<=(max))
#define CLAMP_RANGE(x, min, max) x = (x<(min)) ? min : ((x>(max)) ? max : x )
#define MIN(a, b) ((a)<=(b) ? (a) : (b))
int main(int argc, char** argv)
{
run(argc,argv);
return EXIT_SUCCESS;
}
void run(int argc, char** argv)
{
double cycles;
pin_stats_reset();
init(argc, argv);
pin_stats_pause(cycles);
pin_stats_dump(cycles,"input processing ");
pin_stats_reset();
int *src, *dst, *temp;
int min;
dst = result;
src = new int[cols];
pin_stats_pause(cycles);
pin_stats_dump(cycles,"pre algorithm ");
omp_set_num_threads(1);
for (int t = 0; t < rows-1; t++) {
pin_stats_reset();
temp = src;
src = dst;
dst = temp;
#pragma omp parallel for private(min)
for(int n = 0; n < cols; n++){
min = src[n];
if (n > 0)
min = MIN(min, src[n-1]);
if (n < cols-1)
min = MIN(min, src[n+1]);
dst[n] = wall[t+1][n]+min;
}
pin_stats_pause(cycles);
pin_stats_dump(cycles,"openmp ");
}
pin_stats_reset();
#ifdef BENCH_PRINT
/* for (int i = 0; i < cols; i++)
printf("%d ",data[i]) ;
printf("\n") ;*/
for (int i = 0; i < cols; i++)
printf("%d\n",dst[i]) ;
printf("\n") ;
#endif
pin_stats_pause(cycles);
pin_stats_dump(cycles,"output ");
delete [] data;
delete [] wall;
delete [] dst;
delete [] src;
}