-
Notifications
You must be signed in to change notification settings - Fork 0
/
pathfinder.cpp
111 lines (90 loc) · 2.32 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
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <assert.h>
#include "timer.h"
void run(int argc, char** argv);
/* define timer macros */
#define pin_stats_reset() startCycle()
#define pin_stats_pause(cycles) stopCycle(cycles)
#define pin_stats_dump(cycles) printf("timer: %Lu\n", 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 j = 0; j < cols; j++){
for (int i = 0; i < rows; i++){
wall[i][j] = rand() % 10;
}
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){
init(argc, argv);
unsigned long long cycles;
int *src, *dst, *temp;
int min;
dst = result;
src = new int[cols];
pin_stats_reset();
for (int t = 0; t < rows-1; t++) {
temp = src;
src = dst;
dst = temp;
min = src[0];
for(int n = 0; n < cols-1; n++){
min = MIN(min, src[n+1]);
dst[n] = wall[t+1][n]+min;
}
dst[cols] = wall[t+1][cols]+min;
}
pin_stats_pause(cycles);
pin_stats_dump(cycles);
#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 ",dst[i]) ;
printf("\n") ;
#endif
delete [] data;
delete [] wall;
delete [] dst;
delete [] src;
}