PandA-2024.02
adi.c
Go to the documentation of this file.
1 
10 /* adi.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 "adi.h"
22 
23 
24 /* Array initialization. */
25 static
26 void init_array (int n,
27  DATA_TYPE POLYBENCH_2D(u,N,N,n,n))
28 {
29  int i, j;
30 
31  for (i = 0; i < n; i++)
32  for (j = 0; j < n; j++)
33  {
34  u[i][j] = (DATA_TYPE)(i + n-j) / n;
35  }
36 }
37 
38 
39 /* DCE code. Must scan the entire live-out data.
40  Can be used also to check the correctness of the output. */
41 static
42 void print_array(int n,
43  DATA_TYPE POLYBENCH_2D(u,N,N,n,n))
44 
45 {
46  int i, j;
47 
50  for (i = 0; i < n; i++)
51  for (j = 0; j < n; j++) {
52  if ((i * n + j) % 20 == 0) fprintf(POLYBENCH_DUMP_TARGET, "\n");
53  fprintf (POLYBENCH_DUMP_TARGET, DATA_PRINTF_MODIFIER, u[i][j]);
54  }
55  POLYBENCH_DUMP_END("u");
57 }
58 
59 
60 /* Main computational kernel. The whole function will be timed,
61  including the call and return. */
62 /* Based on a Fortran code fragment from Figure 5 of
63  * "Automatic Data and Computation Decomposition on Distributed Memory Parallel Computers"
64  * by Peizong Lee and Zvi Meir Kedem, TOPLAS, 2002
65  */
66 __attribute__((noinline))
67 void kernel_adi(int tsteps, int n,
68  DATA_TYPE POLYBENCH_2D(u,N,N,n,n),
69  DATA_TYPE POLYBENCH_2D(v,N,N,n,n),
70  DATA_TYPE POLYBENCH_2D(p,N,N,n,n),
71  DATA_TYPE POLYBENCH_2D(q,N,N,n,n))
72 {
73  int t, i, j;
74  DATA_TYPE DX, DY, DT;
75  DATA_TYPE B1, B2;
76  DATA_TYPE mul1, mul2;
77  DATA_TYPE a, b, c, d, e, f;
78 
79 #pragma scop
80 
81  DX = SCALAR_VAL(1.0)/(DATA_TYPE)_PB_N;
82  DY = SCALAR_VAL(1.0)/(DATA_TYPE)_PB_N;
83  DT = SCALAR_VAL(1.0)/(DATA_TYPE)_PB_TSTEPS;
84  B1 = SCALAR_VAL(2.0);
85  B2 = SCALAR_VAL(1.0);
86  mul1 = B1 * DT / (DX * DX);
87  mul2 = B2 * DT / (DY * DY);
88 
89  a = -mul1 / SCALAR_VAL(2.0);
90  b = SCALAR_VAL(1.0)+mul1;
91  c = a;
92  d = -mul2 / SCALAR_VAL(2.0);
93  e = SCALAR_VAL(1.0)+mul2;
94  f = d;
95 
96  for (t=1; t<=_PB_TSTEPS; t++) {
97  //Column Sweep
98  for (i=1; i<_PB_N-1; i++) {
99  v[0][i] = SCALAR_VAL(1.0);
100  p[i][0] = SCALAR_VAL(0.0);
101  q[i][0] = v[0][i];
102  for (j=1; j<_PB_N-1; j++) {
103  p[i][j] = -c / (a*p[i][j-1]+b);
104  q[i][j] = (-d*u[j][i-1]+(SCALAR_VAL(1.0)+SCALAR_VAL(2.0)*d)*u[j][i] - f*u[j][i+1]-a*q[i][j-1])/(a*p[i][j-1]+b);
105  }
106 
107  v[_PB_N-1][i] = SCALAR_VAL(1.0);
108  for (j=_PB_N-2; j>=1; j--) {
109  v[j][i] = p[i][j] * v[j+1][i] + q[i][j];
110  }
111  }
112  //Row Sweep
113  for (i=1; i<_PB_N-1; i++) {
114  u[i][0] = SCALAR_VAL(1.0);
115  p[i][0] = SCALAR_VAL(0.0);
116  q[i][0] = u[i][0];
117  for (j=1; j<_PB_N-1; j++) {
118  p[i][j] = -f / (d*p[i][j-1]+e);
119  q[i][j] = (-a*v[i-1][j]+(SCALAR_VAL(1.0)+SCALAR_VAL(2.0)*a)*v[i][j] - c*v[i+1][j]-d*q[i][j-1])/(d*p[i][j-1]+e);
120  }
121  u[i][_PB_N-1] = SCALAR_VAL(1.0);
122  for (j=_PB_N-2; j>=1; j--) {
123  u[i][j] = p[i][j] * u[i][j+1] + q[i][j];
124  }
125  }
126  }
127 #pragma endscop
128 }
129 
130 
131 int main(int argc, char** argv)
132 {
133  /* Retrieve problem size. */
134  int n = N;
135  int tsteps = TSTEPS;
136 
137  /* Variable declaration/allocation. */
138  POLYBENCH_2D_ARRAY_DECL(u, DATA_TYPE, N, N, n, n);
139  POLYBENCH_2D_ARRAY_DECL(v, DATA_TYPE, N, N, n, n);
140  POLYBENCH_2D_ARRAY_DECL(p, DATA_TYPE, N, N, n, n);
141  POLYBENCH_2D_ARRAY_DECL(q, DATA_TYPE, N, N, n, n);
142 
143 
144  /* Initialize array(s). */
145  init_array (n, POLYBENCH_ARRAY(u));
146 
147  /* Start timer. */
149 
150  /* Run kernel. */
151  kernel_adi (tsteps, n, POLYBENCH_ARRAY(u), POLYBENCH_ARRAY(v), POLYBENCH_ARRAY(p), POLYBENCH_ARRAY(q));
152 
153  /* Stop and print timer. */
156 
157  /* Prevent dead-code elimination. All live-out data must be printed
158  by the function call in argument. */
160 
161  /* Be clean. */
166 
167  return 0;
168 }
#define POLYBENCH_ARRAY(x)
Definition: polybench.h:84
#define POLYBENCH_DUMP_BEGIN(s)
Definition: polybench.h:167
#define POLYBENCH_FREE_ARRAY(x)
Definition: polybench.h:88
#define POLYBENCH_2D(var, dim1, dim2, ddim1, ddim2)
Definition: polybench.h:98
int main(int argc, char **argv)
Definition: adi.c:131
#define TSTEPS
Definition: adi.h:36
#define POLYBENCH_DUMP_START
Definition: polybench.h:165
#define N
Definition: dfdiv.c:60
#define POLYBENCH_2D_ARRAY_DECL(var, type, dim1, dim2, ddim1, ddim2)
Definition: polybench.h:131
__attribute__((noinline))
Convert the given fixedpt number to a decimal string.
Definition: adi.c:66
#define DATA_PRINTF_MODIFIER
Definition: correlation.h:73
#define polybench_prevent_dce(func)
Definition: polybench.h:170
#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
#define _PB_TSTEPS
Definition: adi.h:48
static void print_array(int n, DATA_TYPE POLYBENCH_2D(u, N, N, n, n))
Definition: adi.c:42
#define POLYBENCH_DUMP_FINISH
Definition: polybench.h:166
#define _PB_N
Definition: correlation.h:49
static void init_array(int n, DATA_TYPE POLYBENCH_2D(u, N, N, n, n))
This version is stamped on May 10, 2016.
Definition: adi.c:26
#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