aboutsummaryrefslogtreecommitdiffstats
path: root/benchmarks/polybench-syn-div/stencils/fdtd-2d.c
blob: 2c5cdb25f0cfb77c84ca57a295b3e9b941b9b066 (plain)
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
/**
 * This version is stamped on May 10, 2016
 *
 * Contact:
 *   Louis-Noel Pouchet <pouchet.ohio-state.edu>
 *   Tomofumi Yuki <tomofumi.yuki.fr>
 *
 * Web address: http://polybench.sourceforge.net
 */
/* fdtd-2d.c: this file is part of PolyBench/C */

#ifndef SYNTHESIS
#include <stdio.h>
#endif

#define plus(i) i = i + ONE
static
void init_array (int tmax,
   int nx,
   int ny,
   int ex[ 20 + 0][30 + 0],
   int ey[ 20 + 0][30 + 0],
   int hz[ 20 + 0][30 + 0],
   int _fict_[ 20 + 0])
{
  int i, j;
  int ONE = 1;

  for (i = 0; i < tmax; plus(i))
    _fict_[i] = (int) i;
  for (i = 0; i < nx; plus(i))
    for (j = 0; j < ny; plus(j))
    {
      ex[i][j] = ((i*(j+1)) / nx);
      ey[i][j] = ((i*(j+2)) / ny);
      hz[i][j] = ((i*(j+3)) / nx);
    }
      
}




static
int print_array(int nx,
   int ny,
   int ex[ 20 + 0][30 + 0],
   int ey[ 20 + 0][30 + 0],
   int hz[ 20 + 0][30 + 0])
{
  int i, j;
  int res = 0;
  int ONE = 1;

  for (i = 0; i < nx; plus(i))
    for (j = 0; j < ny; plus(j)) {
      res ^= ex[i][j];
    }
  for (i = 0; i < nx; plus(i))
    for (j = 0; j < ny; plus(j)) {
      res ^= ey[i][j];
    }
  for (i = 0; i < nx; plus(i))
    for (j = 0; j < ny; plus(j)) {
      res ^= hz[i][j];
    }

#ifndef SYNTHESIS
  printf("finished: %u\n", res);
#endif

  return res;
}


static
void kernel_fdtd_2d(int tmax,
      int nx,
      int ny,
      int ex[ 20 + 0][30 + 0],
      int ey[ 20 + 0][30 + 0],
      int hz[ 20 + 0][30 + 0],
      int _fict_[ 20 + 0])
{
  int t, i, j;
  int ONE = 1;

 for(t = 0; t < tmax; t=t+ONE)
    {
      for (j = 0; j < ny; plus(j))
 ey[0][j] = _fict_[t];
      for (i = 1; i < nx; plus(i))
 for (j = 0; j < ny; plus(j))
   ey[i][j] = ey[i][j] - ((hz[i][j]-((hz[i-ONE][j])>>1)));
      for (i = 0; i < nx; plus(i))
 for (j = 1; j < ny; plus(j))
   ex[i][j] = ex[i][j] - ((hz[i][j]-((hz[i][j-ONE])>>1)));
      for (i = 0; i < nx - ONE; plus(i))
 for (j = 0; j < ny - ONE; plus(j)){
   int tmp = (ex[i][j+ONE] - ex[i][j] +
           ey[i+ONE][j] - ey[i][j]);
   hz[i][j] = hz[i][j] - (tmp >> 1) - (tmp >> 2);
           }
    }

}


int main()
{

  int tmax = 20;
  int nx = 20;
  int ny = 30;


  int ex[20 + 0][30 + 0]; 
  int ey[20 + 0][30 + 0]; 
  int hz[20 + 0][30 + 0]; 
  int _fict_[20 + 0];     

  init_array (tmax, nx, ny,
       ex,
       ey,
       hz,
       _fict_);
  kernel_fdtd_2d (tmax, nx, ny,
    ex,
    ey,
    hz,
    _fict_);

  return print_array(nx, ny, ex, ey, hz);
}