| /** |
| * fdtd-2d.c: This file is part of the PolyBench/C 3.2 test suite. |
| * |
| * |
| * Contact: Louis-Noel Pouchet <pouchet@cse.ohio-state.edu> |
| * Web address: http://polybench.sourceforge.net |
| */ |
| #include <stdio.h> |
| #include <unistd.h> |
| #include <string.h> |
| #include <math.h> |
| |
| /* Include polybench common header. */ |
| #include <polybench.h> |
| |
| /* Include benchmark-specific header. */ |
| /* Default data type is double, default size is 50x1000x1000. */ |
| #include "fdtd-2d.h" |
| |
| |
| /* Array initialization. */ |
| 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)) |
| { |
| #pragma STDC FP_CONTRACT OFF |
| int i, j; |
| |
| for (i = 0; i < tmax; i++) |
| _fict_[i] = (DATA_TYPE) i; |
| for (i = 0; i < nx; i++) |
| for (j = 0; j < ny; j++) |
| { |
| ex[i][j] = ((DATA_TYPE) i*(j+1)) / nx; |
| ey[i][j] = ((DATA_TYPE) i*(j+2)) / ny; |
| hz[i][j] = ((DATA_TYPE) i*(j+3)) / nx; |
| } |
| } |
| |
| |
| /* DCE code. Must scan the entire live-out data. |
| Can be used also to check the correctness of the output. */ |
| 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)) |
| { |
| int i, j; |
| char *printmat = malloc(ny*16 + 1); printmat[ny*16] = 0; |
| |
| for (i = 0; i < nx; i++) { |
| for (j = 0; j < ny; j++) |
| print_element(ex[i][j], j*16, printmat); |
| fputs(printmat, stderr); |
| for (j = 0; j < ny; j++) |
| print_element(ey[i][j], j*16, printmat); |
| fputs(printmat, stderr); |
| for (j = 0; j < ny; j++) |
| print_element(hz[i][j], j*16, printmat); |
| fputs(printmat, stderr); |
| } |
| free(printmat); |
| } |
| |
| |
| /* Main computational kernel. The whole function will be timed, |
| including the call and return. */ |
| static |
| void kernel_fdtd_2d(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)) |
| { |
| int t, i, j; |
| |
| #pragma scop |
| |
| for(t = 0; t < _PB_TMAX; t++) |
| { |
| for (j = 0; j < _PB_NY; j++) |
| ey[0][j] = _fict_[t]; |
| for (i = 1; i < _PB_NX; i++) |
| for (j = 0; j < _PB_NY; j++) |
| ey[i][j] = ey[i][j] - 0.5*(hz[i][j]-hz[i-1][j]); |
| for (i = 0; i < _PB_NX; i++) |
| for (j = 1; j < _PB_NY; j++) |
| ex[i][j] = ex[i][j] - 0.5*(hz[i][j]-hz[i][j-1]); |
| for (i = 0; i < _PB_NX - 1; i++) |
| for (j = 0; j < _PB_NY - 1; j++) |
| hz[i][j] = hz[i][j] - 0.7* (ex[i][j+1] - ex[i][j] + |
| ey[i+1][j] - ey[i][j]); |
| } |
| |
| #pragma endscop |
| } |
| |
| static void |
| kernel_fdtd_2d_StrictFP(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)) |
| { |
| #pragma STDC FP_CONTRACT OFF |
| int t, i, j; |
| |
| for(t = 0; t < _PB_TMAX; t++) |
| { |
| for (j = 0; j < _PB_NY; j++) |
| ey[0][j] = _fict_[t]; |
| for (i = 1; i < _PB_NX; i++) |
| for (j = 0; j < _PB_NY; j++) |
| ey[i][j] = ey[i][j] - 0.5*(hz[i][j]-hz[i-1][j]); |
| for (i = 0; i < _PB_NX; i++) |
| for (j = 1; j < _PB_NY; j++) |
| ex[i][j] = ex[i][j] - 0.5*(hz[i][j]-hz[i][j-1]); |
| for (i = 0; i < _PB_NX - 1; i++) |
| for (j = 0; j < _PB_NY - 1; j++) |
| hz[i][j] = hz[i][j] - 0.7* (ex[i][j+1] - ex[i][j] + |
| ey[i+1][j] - ey[i][j]); |
| } |
| } |
| |
| /* Return 0 when one of the elements of arrays A and B do not match within the |
| allowed FP_ABSTOLERANCE. Return 1 when all elements match. */ |
| static inline int |
| check_FP(int nx, int ny, |
| DATA_TYPE POLYBENCH_2D(A,NX,NY,nx,ny), |
| DATA_TYPE POLYBENCH_2D(B,NX,NY,nx,ny)) { |
| int i, j; |
| double AbsTolerance = FP_ABSTOLERANCE; |
| for (i = 0; i < _PB_NX; i++) |
| for (j = 0; j < _PB_NY; j++) |
| { |
| double V1 = A[i][j]; |
| double V2 = B[i][j]; |
| double Diff = fabs(V1 - V2); |
| if (Diff > AbsTolerance) { |
| fprintf(stderr, "A[%d][%d] = %lf and B[%d][%d] = %lf differ more than" |
| " FP_ABSTOLERANCE = %lf\n", i, j, V1, i, j, V2, AbsTolerance); |
| return 0; |
| } |
| } |
| |
| /* All elements are within the allowed FP_ABSTOLERANCE error margin. */ |
| return 1; |
| } |
| |
| int main(int argc, char** argv) |
| { |
| /* Retrieve problem size. */ |
| int tmax = TMAX; |
| int nx = NX; |
| int ny = NY; |
| |
| /* Variable declaration/allocation. */ |
| POLYBENCH_2D_ARRAY_DECL(ex,DATA_TYPE,NX,NY,nx,ny); |
| POLYBENCH_2D_ARRAY_DECL(ey,DATA_TYPE,NX,NY,nx,ny); |
| POLYBENCH_2D_ARRAY_DECL(hz,DATA_TYPE,NX,NY,nx,ny); |
| POLYBENCH_2D_ARRAY_DECL(ex_StrictFP,DATA_TYPE,NX,NY,nx,ny); |
| POLYBENCH_2D_ARRAY_DECL(ey_StrictFP,DATA_TYPE,NX,NY,nx,ny); |
| POLYBENCH_2D_ARRAY_DECL(hz_StrictFP,DATA_TYPE,NX,NY,nx,ny); |
| POLYBENCH_1D_ARRAY_DECL(_fict_,DATA_TYPE,TMAX,tmax); |
| |
| /* Initialize array(s). */ |
| init_array (tmax, nx, ny, |
| POLYBENCH_ARRAY(ex), |
| POLYBENCH_ARRAY(ey), |
| POLYBENCH_ARRAY(hz), |
| POLYBENCH_ARRAY(_fict_)); |
| |
| /* Start timer. */ |
| polybench_start_instruments; |
| |
| /* Run kernel. */ |
| kernel_fdtd_2d (tmax, nx, ny, |
| POLYBENCH_ARRAY(ex), |
| POLYBENCH_ARRAY(ey), |
| POLYBENCH_ARRAY(hz), |
| POLYBENCH_ARRAY(_fict_)); |
| |
| |
| /* Stop and print timer. */ |
| polybench_stop_instruments; |
| polybench_print_instruments; |
| |
| init_array (tmax, nx, ny, |
| POLYBENCH_ARRAY(ex_StrictFP), |
| POLYBENCH_ARRAY(ey_StrictFP), |
| POLYBENCH_ARRAY(hz_StrictFP), |
| POLYBENCH_ARRAY(_fict_)); |
| kernel_fdtd_2d_StrictFP(tmax, nx, ny, |
| POLYBENCH_ARRAY(ex_StrictFP), |
| POLYBENCH_ARRAY(ey_StrictFP), |
| POLYBENCH_ARRAY(hz_StrictFP), |
| POLYBENCH_ARRAY(_fict_)); |
| |
| /* Prevent dead-code elimination. All live-out data must be printed |
| by the function call in argument. */ |
| polybench_prevent_dce(print_array(nx, ny, POLYBENCH_ARRAY(ex_StrictFP), |
| POLYBENCH_ARRAY(ey_StrictFP), |
| POLYBENCH_ARRAY(hz_StrictFP))); |
| |
| /* Be clean. */ |
| POLYBENCH_FREE_ARRAY(ex); |
| POLYBENCH_FREE_ARRAY(ey); |
| POLYBENCH_FREE_ARRAY(hz); |
| POLYBENCH_FREE_ARRAY(ex_StrictFP); |
| POLYBENCH_FREE_ARRAY(ey_StrictFP); |
| POLYBENCH_FREE_ARRAY(hz_StrictFP); |
| POLYBENCH_FREE_ARRAY(_fict_); |
| |
| return 0; |
| } |