PandA-2024.02
fdtd-2d.c
Go to the documentation of this file.
1 
10 /* fdtd-2d.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 "fdtd-2d.h"
22 
23 
24 /* Array initialization. */
25 static
26 void init_array (int tmax,
27  int nx,
28  int ny,
29  DATA_TYPE POLYBENCH_2D(ex,NX,NY,nx,ny),
30  DATA_TYPE POLYBENCH_2D(ey,NX,NY,nx,ny),
31  DATA_TYPE POLYBENCH_2D(hz,NX,NY,nx,ny),
32  DATA_TYPE POLYBENCH_1D(_fict_,TMAX,tmax))
33 {
34  int i, j;
35 
36  for (i = 0; i < tmax; i++)
37  _fict_[i] = (DATA_TYPE) i;
38  for (i = 0; i < nx; i++)
39  for (j = 0; j < ny; j++)
40  {
41  ex[i][j] = ((DATA_TYPE) i*(j+1)) / nx;
42  ey[i][j] = ((DATA_TYPE) i*(j+2)) / ny;
43  hz[i][j] = ((DATA_TYPE) i*(j+3)) / nx;
44  }
45 }
46 
47 
48 /* DCE code. Must scan the entire live-out data.
49  Can be used also to check the correctness of the output. */
50 static
51 void print_array(int nx,
52  int ny,
53  DATA_TYPE POLYBENCH_2D(ex,NX,NY,nx,ny),
54  DATA_TYPE POLYBENCH_2D(ey,NX,NY,nx,ny),
55  DATA_TYPE POLYBENCH_2D(hz,NX,NY,nx,ny))
56 {
57  int i, j;
58 
61  for (i = 0; i < nx; i++)
62  for (j = 0; j < ny; j++) {
63  if ((i * nx + j) % 20 == 0) fprintf(POLYBENCH_DUMP_TARGET, "\n");
64  fprintf(POLYBENCH_DUMP_TARGET, DATA_PRINTF_MODIFIER, ex[i][j]);
65  }
66  POLYBENCH_DUMP_END("ex");
68 
70  for (i = 0; i < nx; i++)
71  for (j = 0; j < ny; j++) {
72  if ((i * nx + j) % 20 == 0) fprintf(POLYBENCH_DUMP_TARGET, "\n");
73  fprintf(POLYBENCH_DUMP_TARGET, DATA_PRINTF_MODIFIER, ey[i][j]);
74  }
75  POLYBENCH_DUMP_END("ey");
76 
78  for (i = 0; i < nx; i++)
79  for (j = 0; j < ny; j++) {
80  if ((i * nx + j) % 20 == 0) fprintf(POLYBENCH_DUMP_TARGET, "\n");
81  fprintf(POLYBENCH_DUMP_TARGET, DATA_PRINTF_MODIFIER, hz[i][j]);
82  }
83  POLYBENCH_DUMP_END("hz");
84 }
85 
86 
87 /* Main computational kernel. The whole function will be timed,
88  including the call and return. */
89 __attribute__((noinline))
90 void kernel_fdtd_2d(int tmax,
91  int nx,
92  int ny,
93  DATA_TYPE POLYBENCH_2D(ex,NX,NY,nx,ny),
94  DATA_TYPE POLYBENCH_2D(ey,NX,NY,nx,ny),
95  DATA_TYPE POLYBENCH_2D(hz,NX,NY,nx,ny),
96  DATA_TYPE POLYBENCH_1D(_fict_,TMAX,tmax))
97 {
98  int t, i, j;
99 
100 #pragma scop
101 
102  for(t = 0; t < _PB_TMAX; t++)
103  {
104  for (j = 0; j < _PB_NY; j++)
105  ey[0][j] = _fict_[t];
106  for (i = 1; i < _PB_NX; i++)
107  for (j = 0; j < _PB_NY; j++)
108  ey[i][j] = ey[i][j] - SCALAR_VAL(0.5)*(hz[i][j]-hz[i-1][j]);
109  for (i = 0; i < _PB_NX; i++)
110  for (j = 1; j < _PB_NY; j++)
111  ex[i][j] = ex[i][j] - SCALAR_VAL(0.5)*(hz[i][j]-hz[i][j-1]);
112  for (i = 0; i < _PB_NX - 1; i++)
113  for (j = 0; j < _PB_NY - 1; j++)
114  hz[i][j] = hz[i][j] - SCALAR_VAL(0.7)* (ex[i][j+1] - ex[i][j] +
115  ey[i+1][j] - ey[i][j]);
116  }
117 
118 #pragma endscop
119 }
120 
121 
122 int main(int argc, char** argv)
123 {
124  /* Retrieve problem size. */
125  int tmax = TMAX;
126  int nx = NX;
127  int ny = NY;
128 
129  /* Variable declaration/allocation. */
134 
135  /* Initialize array(s). */
136  init_array (tmax, nx, ny,
137  POLYBENCH_ARRAY(ex),
138  POLYBENCH_ARRAY(ey),
139  POLYBENCH_ARRAY(hz),
140  POLYBENCH_ARRAY(_fict_));
141 
142  /* Start timer. */
144 
145  /* Run kernel. */
146  kernel_fdtd_2d (tmax, nx, ny,
147  POLYBENCH_ARRAY(ex),
148  POLYBENCH_ARRAY(ey),
149  POLYBENCH_ARRAY(hz),
150  POLYBENCH_ARRAY(_fict_));
151 
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  POLYBENCH_ARRAY(ey),
161  POLYBENCH_ARRAY(hz)));
162 
163  /* Be clean. */
167  POLYBENCH_FREE_ARRAY(_fict_);
168 
169  return 0;
170 }
#define _PB_NY
Definition: fdtd-2d.h:55
#define POLYBENCH_ARRAY(x)
Definition: polybench.h:84
int main(int argc, char **argv)
Definition: fdtd-2d.c:122
#define POLYBENCH_DUMP_BEGIN(s)
Definition: polybench.h:167
#define TMAX
Definition: fdtd-2d.h:39
#define POLYBENCH_FREE_ARRAY(x)
Definition: polybench.h:88
#define NY
Definition: fdtd-2d.h:41
static void init_array(int tmax, int nx, int ny, DATA_TYPE POLYBENCH_2D(ex, NX, NY, nx, ny), DATA_TYPE POLYBENCH_2D(ey, NX, NY, nx, ny), DATA_TYPE POLYBENCH_2D(hz, NX, NY, nx, ny), DATA_TYPE POLYBENCH_1D(_fict_, TMAX, tmax))
This version is stamped on May 10, 2016.
Definition: fdtd-2d.c:26
#define POLYBENCH_2D(var, dim1, dim2, ddim1, ddim2)
Definition: polybench.h:98
__attribute__((noinline))
Convert the given fixedpt number to a decimal string.
Definition: fdtd-2d.c:89
#define NX
Definition: fdtd-2d.h:40
#define POLYBENCH_DUMP_START
Definition: polybench.h:165
#define POLYBENCH_2D_ARRAY_DECL(var, type, dim1, dim2, ddim1, ddim2)
Definition: polybench.h:131
#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
static void print_array(int nx, int ny, DATA_TYPE POLYBENCH_2D(ex, NX, NY, nx, ny), DATA_TYPE POLYBENCH_2D(ey, NX, NY, nx, ny), DATA_TYPE POLYBENCH_2D(hz, NX, NY, nx, ny))
Definition: fdtd-2d.c:51
#define POLYBENCH_1D(var, dim1, ddim1)
Definition: polybench.h:97
#define POLYBENCH_DUMP_FINISH
Definition: polybench.h:166
#define POLYBENCH_1D_ARRAY_DECL(var, type, dim1, ddim1)
Definition: polybench.h:128
#define _PB_NX
Definition: fdtd-2d.h:54
#define _PB_TMAX
Definition: fdtd-2d.h:53
#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