PandA-2024.02
deriche.c
Go to the documentation of this file.
1 
10 /* deriche.c: this file is part of PolyBench/C */
11 
12 #include <stdio.h>
13 #include <unistd.h>
14 #include <string.h>
15 #include <math.h>
16 
17 /* Include polybench common header. */
18 #include <polybench.h>
19 
20 /* Include benchmark-specific header. */
21 #include "deriche.h"
22 
23 
24 /* Array initialization. */
25 static
26 void init_array (int w, int h, DATA_TYPE* alpha,
27  DATA_TYPE POLYBENCH_2D(imgIn,W,H,w,h),
28  DATA_TYPE POLYBENCH_2D(imgOut,W,H,w,h))
29 {
30  int i, j;
31 
32  *alpha=0.25; //parameter of the filter
33 
34  //input should be between 0 and 1 (grayscale image pixel)
35  for (i = 0; i < w; i++)
36  for (j = 0; j < h; j++)
37  imgIn[i][j] = (DATA_TYPE) ((313*i+991*j)%65536) / 65535.0f;
38 }
39 
40 
41 /* DCE code. Must scan the entire live-out data.
42  Can be used also to check the correctness of the output. */
43 static
44 void print_array(int w, int h,
45  DATA_TYPE POLYBENCH_2D(imgOut,W,H,w,h))
46 
47 {
48  int i, j;
49 
51  POLYBENCH_DUMP_BEGIN("imgOut");
52  for (i = 0; i < w; i++)
53  for (j = 0; j < h; j++) {
54  if ((i * h + j) % 20 == 0) fprintf(POLYBENCH_DUMP_TARGET, "\n");
55  fprintf(POLYBENCH_DUMP_TARGET, DATA_PRINTF_MODIFIER, imgOut[i][j]);
56  }
57  POLYBENCH_DUMP_END("imgOut");
59 }
60 
61 
62 
63 /* Main computational kernel. The whole function will be timed,
64  including the call and return. */
65 /* Original code provided by Gael Deest */
66 __attribute__((noinline))
67 void kernel_deriche(int w, int h, DATA_TYPE alpha,
68  DATA_TYPE POLYBENCH_2D(imgIn, W, H, w, h),
69  DATA_TYPE POLYBENCH_2D(imgOut, W, H, w, h),
70  DATA_TYPE POLYBENCH_2D(y1, W, H, w, h),
71  DATA_TYPE POLYBENCH_2D(y2, W, H, w, h)) {
72  int i,j;
73  DATA_TYPE xm1, tm1, ym1, ym2;
74  DATA_TYPE xp1, xp2;
75  DATA_TYPE tp1, tp2;
76  DATA_TYPE yp1, yp2;
77 
78  DATA_TYPE k;
79  DATA_TYPE a1, a2, a3, a4, a5, a6, a7, a8;
80  DATA_TYPE b1, b2, c1, c2;
81 
82 #pragma scop
83  k = (SCALAR_VAL(1.0)-EXP_FUN(-alpha))*(SCALAR_VAL(1.0)-EXP_FUN(-alpha))/(SCALAR_VAL(1.0)+SCALAR_VAL(2.0)*alpha*EXP_FUN(-alpha)-EXP_FUN(SCALAR_VAL(2.0)*alpha));
84  a1 = a5 = k;
85  a2 = a6 = k*EXP_FUN(-alpha)*(alpha-SCALAR_VAL(1.0));
86  a3 = a7 = k*EXP_FUN(-alpha)*(alpha+SCALAR_VAL(1.0));
87  a4 = a8 = -k*EXP_FUN(SCALAR_VAL(-2.0)*alpha);
88  b1 = POW_FUN(SCALAR_VAL(2.0),-alpha);
89  b2 = -EXP_FUN(SCALAR_VAL(-2.0)*alpha);
90  c1 = c2 = 1;
91 
92  for (i=0; i<_PB_W; i++) {
93  ym1 = SCALAR_VAL(0.0);
94  ym2 = SCALAR_VAL(0.0);
95  xm1 = SCALAR_VAL(0.0);
96  for (j=0; j<_PB_H; j++) {
97  y1[i][j] = a1*imgIn[i][j] + a2*xm1 + b1*ym1 + b2*ym2;
98  xm1 = imgIn[i][j];
99  ym2 = ym1;
100  ym1 = y1[i][j];
101  }
102  }
103 
104  for (i=0; i<_PB_W; i++) {
105  yp1 = SCALAR_VAL(0.0);
106  yp2 = SCALAR_VAL(0.0);
107  xp1 = SCALAR_VAL(0.0);
108  xp2 = SCALAR_VAL(0.0);
109  for (j=_PB_H-1; j>=0; j--) {
110  y2[i][j] = a3*xp1 + a4*xp2 + b1*yp1 + b2*yp2;
111  xp2 = xp1;
112  xp1 = imgIn[i][j];
113  yp2 = yp1;
114  yp1 = y2[i][j];
115  }
116  }
117 
118  for (i=0; i<_PB_W; i++)
119  for (j=0; j<_PB_H; j++) {
120  imgOut[i][j] = c1 * (y1[i][j] + y2[i][j]);
121  }
122 
123  for (j=0; j<_PB_H; j++) {
124  tm1 = SCALAR_VAL(0.0);
125  ym1 = SCALAR_VAL(0.0);
126  ym2 = SCALAR_VAL(0.0);
127  for (i=0; i<_PB_W; i++) {
128  y1[i][j] = a5*imgOut[i][j] + a6*tm1 + b1*ym1 + b2*ym2;
129  tm1 = imgOut[i][j];
130  ym2 = ym1;
131  ym1 = y1 [i][j];
132  }
133  }
134 
135 
136  for (j=0; j<_PB_H; j++) {
137  tp1 = SCALAR_VAL(0.0);
138  tp2 = SCALAR_VAL(0.0);
139  yp1 = SCALAR_VAL(0.0);
140  yp2 = SCALAR_VAL(0.0);
141  for (i=_PB_W-1; i>=0; i--) {
142  y2[i][j] = a7*tp1 + a8*tp2 + b1*yp1 + b2*yp2;
143  tp2 = tp1;
144  tp1 = imgOut[i][j];
145  yp2 = yp1;
146  yp1 = y2[i][j];
147  }
148  }
149 
150  for (i=0; i<_PB_W; i++)
151  for (j=0; j<_PB_H; j++)
152  imgOut[i][j] = c2*(y1[i][j] + y2[i][j]);
153 
154 #pragma endscop
155 }
156 
157 
158 int main(int argc, char** argv)
159 {
160  /* Retrieve problem size. */
161  int w = W;
162  int h = H;
163 
164  /* Variable declaration/allocation. */
165  DATA_TYPE alpha;
166  POLYBENCH_2D_ARRAY_DECL(imgIn, DATA_TYPE, W, H, w, h);
167  POLYBENCH_2D_ARRAY_DECL(imgOut, DATA_TYPE, W, H, w, h);
168  POLYBENCH_2D_ARRAY_DECL(y1, DATA_TYPE, W, H, w, h);
169  POLYBENCH_2D_ARRAY_DECL(y2, DATA_TYPE, W, H, w, h);
170 
171 
172  /* Initialize array(s). */
173  init_array (w, h, &alpha, POLYBENCH_ARRAY(imgIn), POLYBENCH_ARRAY(imgOut));
174 
175  /* Start timer. */
177 
178  /* Run kernel. */
179  kernel_deriche (w, h, alpha, POLYBENCH_ARRAY(imgIn), POLYBENCH_ARRAY(imgOut), POLYBENCH_ARRAY(y1), POLYBENCH_ARRAY(y2));
180 
181  /* Stop and print timer. */
184 
185  /* Prevent dead-code elimination. All live-out data must be printed
186  by the function call in argument. */
188 
189  /* Be clean. */
190  POLYBENCH_FREE_ARRAY(imgIn);
191  POLYBENCH_FREE_ARRAY(imgOut);
194 
195  return 0;
196 }
int main(int argc, char **argv)
Definition: deriche.c:158
#define _PB_W
Definition: deriche.h:48
#define POLYBENCH_ARRAY(x)
Definition: polybench.h:84
TVMArray c2[1]
#define POLYBENCH_DUMP_BEGIN(s)
Definition: polybench.h:167
static void init_array(int w, int h, DATA_TYPE *alpha, DATA_TYPE POLYBENCH_2D(imgIn, W, H, w, h), DATA_TYPE POLYBENCH_2D(imgOut, W, H, w, h))
This version is stamped on May 10, 2016.
Definition: deriche.c:26
TVMArray c1[1]
__attribute__((noinline))
Convert the given fixedpt number to a decimal string.
Definition: deriche.c:66
#define POLYBENCH_FREE_ARRAY(x)
Definition: polybench.h:88
#define POLYBENCH_2D(var, dim1, dim2, ddim1, ddim2)
Definition: polybench.h:98
#define _PB_H
Definition: deriche.h:49
static const uint32_t k[]
Definition: sha-256.c:22
const int h[24]
Definition: adpcm.c:87
#define POLYBENCH_DUMP_START
Definition: polybench.h:165
TVMArray a3[1]
#define POLYBENCH_2D_ARRAY_DECL(var, type, dim1, dim2, ddim1, ddim2)
Definition: polybench.h:131
#define DATA_PRINTF_MODIFIER
Definition: correlation.h:73
#define W
Definition: deriche.h:36
#define polybench_prevent_dce(func)
Definition: polybench.h:170
static void print_array(int w, int h, DATA_TYPE POLYBENCH_2D(imgOut, W, H, w, h))
Definition: deriche.c:44
#define SCALAR_VAL(x)
Definition: correlation.h:74
#define POLYBENCH_DUMP_TARGET
Definition: polybench.h:164
#define POLYBENCH_DUMP_END(s)
Definition: polybench.h:168
TVMArray b2[1]
TVMArray a2[1]
TVMArray a1[1]
#define H
Definition: deriche.h:37
#define EXP_FUN(x)
Definition: correlation.h:76
TVMArray b1[1]
#define POW_FUN(x, y)
Definition: correlation.h:77
#define POLYBENCH_DUMP_FINISH
Definition: polybench.h:166
#define polybench_stop_instruments
Definition: polybench.h:177
#define polybench_print_instruments
Definition: polybench.h:178
#define polybench_start_instruments
Definition: polybench.h:176
#define DATA_TYPE
Definition: correlation.h:72

Generated on Mon Feb 12 2024 13:02:50 for PandA-2024.02 by doxygen 1.8.13