| /** |
| * 3mm.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 4000. */ |
| #include "3mm.h" |
| |
| |
| /* Array initialization. */ |
| static |
| void init_array(int ni, int nj, int nk, int nl, int nm, |
| DATA_TYPE POLYBENCH_2D(A,NI,NK,ni,nk), |
| DATA_TYPE POLYBENCH_2D(B,NK,NJ,nk,nj), |
| DATA_TYPE POLYBENCH_2D(C,NJ,NM,nj,nm), |
| DATA_TYPE POLYBENCH_2D(D,NM,NL,nm,nl)) |
| { |
| #pragma STDC FP_CONTRACT OFF |
| int i, j; |
| |
| for (i = 0; i < ni; i++) |
| for (j = 0; j < nk; j++) |
| A[i][j] = ((DATA_TYPE) i*j) / ni; |
| for (i = 0; i < nk; i++) |
| for (j = 0; j < nj; j++) |
| B[i][j] = ((DATA_TYPE) i*(j+1)) / nj; |
| for (i = 0; i < nj; i++) |
| for (j = 0; j < nm; j++) |
| C[i][j] = ((DATA_TYPE) i*(j+3)) / nl; |
| for (i = 0; i < nm; i++) |
| for (j = 0; j < nl; j++) |
| D[i][j] = ((DATA_TYPE) i*(j+2)) / nk; |
| } |
| |
| |
| /* 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 ni, int nl, |
| DATA_TYPE POLYBENCH_2D(G,NI,NL,ni,nl)) |
| { |
| int i, j; |
| char *printmat = malloc(nl*16 + 1); printmat[nl*16] = 0; |
| |
| for (i = 0; i < ni; i++) { |
| for (j = 0; j < nl; j++) |
| print_element(G[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_3mm(int ni, int nj, int nk, int nl, int nm, |
| DATA_TYPE POLYBENCH_2D(E,NI,NJ,ni,nj), |
| DATA_TYPE POLYBENCH_2D(A,NI,NK,ni,nk), |
| DATA_TYPE POLYBENCH_2D(B,NK,NJ,nk,nj), |
| DATA_TYPE POLYBENCH_2D(F,NJ,NL,nj,nl), |
| DATA_TYPE POLYBENCH_2D(C,NJ,NM,nj,nm), |
| DATA_TYPE POLYBENCH_2D(D,NM,NL,nm,nl), |
| DATA_TYPE POLYBENCH_2D(G,NI,NL,ni,nl)) |
| { |
| int i, j, k; |
| |
| #pragma scop |
| /* E := A*B */ |
| for (i = 0; i < _PB_NI; i++) |
| for (j = 0; j < _PB_NJ; j++) |
| { |
| E[i][j] = 0; |
| for (k = 0; k < _PB_NK; ++k) |
| E[i][j] += A[i][k] * B[k][j]; |
| } |
| /* F := C*D */ |
| for (i = 0; i < _PB_NJ; i++) |
| for (j = 0; j < _PB_NL; j++) |
| { |
| F[i][j] = 0; |
| for (k = 0; k < _PB_NM; ++k) |
| F[i][j] += C[i][k] * D[k][j]; |
| } |
| /* G := E*F */ |
| for (i = 0; i < _PB_NI; i++) |
| for (j = 0; j < _PB_NL; j++) |
| { |
| G[i][j] = 0; |
| for (k = 0; k < _PB_NJ; ++k) |
| G[i][j] += E[i][k] * F[k][j]; |
| } |
| #pragma endscop |
| |
| } |
| |
| static |
| void kernel_3mm_StrictFP(int ni, int nj, int nk, int nl, int nm, |
| DATA_TYPE POLYBENCH_2D(E,NI,NJ,ni,nj), |
| DATA_TYPE POLYBENCH_2D(A,NI,NK,ni,nk), |
| DATA_TYPE POLYBENCH_2D(B,NK,NJ,nk,nj), |
| DATA_TYPE POLYBENCH_2D(F,NJ,NL,nj,nl), |
| DATA_TYPE POLYBENCH_2D(C,NJ,NM,nj,nm), |
| DATA_TYPE POLYBENCH_2D(D,NM,NL,nm,nl), |
| DATA_TYPE POLYBENCH_2D(G,NI,NL,ni,nl)) |
| { |
| #pragma STDC FP_CONTRACT OFF |
| int i, j, k; |
| |
| /* E := A*B */ |
| for (i = 0; i < _PB_NI; i++) |
| for (j = 0; j < _PB_NJ; j++) |
| { |
| E[i][j] = 0; |
| for (k = 0; k < _PB_NK; ++k) |
| E[i][j] += A[i][k] * B[k][j]; |
| } |
| /* F := C*D */ |
| for (i = 0; i < _PB_NJ; i++) |
| for (j = 0; j < _PB_NL; j++) |
| { |
| F[i][j] = 0; |
| for (k = 0; k < _PB_NM; ++k) |
| F[i][j] += C[i][k] * D[k][j]; |
| } |
| /* G := E*F */ |
| for (i = 0; i < _PB_NI; i++) |
| for (j = 0; j < _PB_NL; j++) |
| { |
| G[i][j] = 0; |
| for (k = 0; k < _PB_NJ; ++k) |
| G[i][j] += E[i][k] * F[k][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 ni, int nl, |
| DATA_TYPE POLYBENCH_2D(A,NI,NL,ni,nl), |
| DATA_TYPE POLYBENCH_2D(B,NI,NL,ni,nl)) { |
| int i, j; |
| double AbsTolerance = FP_ABSTOLERANCE; |
| for (i = 0; i < _PB_NI; i++) |
| for (j = 0; j < _PB_NL; 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; |
| } |
| } |
| |
| return 1; |
| } |
| |
| int main(int argc, char** argv) |
| { |
| /* Retrieve problem size. */ |
| int ni = NI; |
| int nj = NJ; |
| int nk = NK; |
| int nl = NL; |
| int nm = NM; |
| |
| /* Variable declaration/allocation. */ |
| POLYBENCH_2D_ARRAY_DECL(E, DATA_TYPE, NI, NJ, ni, nj); |
| POLYBENCH_2D_ARRAY_DECL(A, DATA_TYPE, NI, NK, ni, nk); |
| POLYBENCH_2D_ARRAY_DECL(B, DATA_TYPE, NK, NJ, nk, nj); |
| POLYBENCH_2D_ARRAY_DECL(F, DATA_TYPE, NJ, NL, nj, nl); |
| POLYBENCH_2D_ARRAY_DECL(C, DATA_TYPE, NJ, NM, nj, nm); |
| POLYBENCH_2D_ARRAY_DECL(D, DATA_TYPE, NM, NL, nm, nl); |
| POLYBENCH_2D_ARRAY_DECL(G, DATA_TYPE, NI, NL, ni, nl); |
| POLYBENCH_2D_ARRAY_DECL(G_StrictFP, DATA_TYPE, NI, NL, ni, nl); |
| |
| /* Initialize array(s). */ |
| init_array (ni, nj, nk, nl, nm, |
| POLYBENCH_ARRAY(A), |
| POLYBENCH_ARRAY(B), |
| POLYBENCH_ARRAY(C), |
| POLYBENCH_ARRAY(D)); |
| |
| /* Start timer. */ |
| polybench_start_instruments; |
| |
| /* Run kernel. */ |
| kernel_3mm (ni, nj, nk, nl, nm, |
| POLYBENCH_ARRAY(E), |
| POLYBENCH_ARRAY(A), |
| POLYBENCH_ARRAY(B), |
| POLYBENCH_ARRAY(F), |
| POLYBENCH_ARRAY(C), |
| POLYBENCH_ARRAY(D), |
| POLYBENCH_ARRAY(G)); |
| |
| /* Stop and print timer. */ |
| polybench_stop_instruments; |
| polybench_print_instruments; |
| |
| kernel_3mm_StrictFP(ni, nj, nk, nl, nm, |
| POLYBENCH_ARRAY(E), |
| POLYBENCH_ARRAY(A), |
| POLYBENCH_ARRAY(B), |
| POLYBENCH_ARRAY(F), |
| POLYBENCH_ARRAY(C), |
| POLYBENCH_ARRAY(D), |
| POLYBENCH_ARRAY(G_StrictFP)); |
| if (!check_FP(ni, nl, POLYBENCH_ARRAY(G), POLYBENCH_ARRAY(G_StrictFP))) |
| return 1; |
| |
| /* Prevent dead-code elimination. All live-out data must be printed |
| by the function call in argument. */ |
| polybench_prevent_dce(print_array(ni, nl, POLYBENCH_ARRAY(G_StrictFP))); |
| |
| /* Be clean. */ |
| POLYBENCH_FREE_ARRAY(E); |
| POLYBENCH_FREE_ARRAY(A); |
| POLYBENCH_FREE_ARRAY(B); |
| POLYBENCH_FREE_ARRAY(F); |
| POLYBENCH_FREE_ARRAY(C); |
| POLYBENCH_FREE_ARRAY(D); |
| POLYBENCH_FREE_ARRAY(G); |
| POLYBENCH_FREE_ARRAY(G_StrictFP); |
| |
| return 0; |
| } |