PandA-2024.02
2mm.c
Go to the documentation of this file.
1 
10 /* 2mm.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 "2mm.h"
22 
23 
24 /* Array initialization. */
25 static
26 void init_array(int ni, int nj, int nk, int nl,
27  DATA_TYPE *alpha,
28  DATA_TYPE *beta,
29  DATA_TYPE POLYBENCH_2D(A,NI,NK,ni,nk),
30  DATA_TYPE POLYBENCH_2D(B,NK,NJ,nk,nj),
31  DATA_TYPE POLYBENCH_2D(C,NJ,NL,nj,nl),
32  DATA_TYPE POLYBENCH_2D(D,NI,NL,ni,nl))
33 {
34  int i, j;
35 
36  *alpha = 1.5;
37  *beta = 1.2;
38  for (i = 0; i < ni; i++)
39  for (j = 0; j < nk; j++)
40  A[i][j] = (DATA_TYPE) ((i*j+1) % ni) / ni;
41  for (i = 0; i < nk; i++)
42  for (j = 0; j < nj; j++)
43  B[i][j] = (DATA_TYPE) (i*(j+1) % nj) / nj;
44  for (i = 0; i < nj; i++)
45  for (j = 0; j < nl; j++)
46  C[i][j] = (DATA_TYPE) ((i*(j+3)+1) % nl) / nl;
47  for (i = 0; i < ni; i++)
48  for (j = 0; j < nl; j++)
49  D[i][j] = (DATA_TYPE) (i*(j+2) % nk) / nk;
50 }
51 
52 
53 /* DCE code. Must scan the entire live-out data.
54  Can be used also to check the correctness of the output. */
55 static
56 void print_array(int ni, int nl,
57  DATA_TYPE POLYBENCH_2D(D,NI,NL,ni,nl))
58 {
59  int i, j;
60 
63  for (i = 0; i < ni; i++)
64  for (j = 0; j < nl; j++) {
65  if ((i * ni + j) % 20 == 0) fprintf (POLYBENCH_DUMP_TARGET, "\n");
67  }
68  POLYBENCH_DUMP_END("D");
70 }
71 
72 
73 /* Main computational kernel. The whole function will be timed,
74  including the call and return. */
75 __attribute__((noinline))
76 void kernel_2mm(int ni, int nj, int nk, int nl,
77  DATA_TYPE alpha,
78  DATA_TYPE beta,
79  DATA_TYPE POLYBENCH_2D(tmp,NI,NJ,ni,nj),
80  DATA_TYPE POLYBENCH_2D(A,NI,NK,ni,nk),
81  DATA_TYPE POLYBENCH_2D(B,NK,NJ,nk,nj),
82  DATA_TYPE POLYBENCH_2D(C,NJ,NL,nj,nl),
83  DATA_TYPE POLYBENCH_2D(D,NI,NL,ni,nl))
84 {
85  int i, j, k;
86 
87 #pragma scop
88  /* D := alpha*A*B*C + beta*D */
89  for (i = 0; i < _PB_NI; i++)
90  for (j = 0; j < _PB_NJ; j++)
91  {
92  tmp[i][j] = SCALAR_VAL(0.0);
93  for (k = 0; k < _PB_NK; ++k)
94  tmp[i][j] += alpha * A[i][k] * B[k][j];
95  }
96  for (i = 0; i < _PB_NI; i++)
97  for (j = 0; j < _PB_NL; j++)
98  {
99  D[i][j] *= beta;
100  for (k = 0; k < _PB_NJ; ++k)
101  D[i][j] += tmp[i][k] * C[k][j];
102  }
103 #pragma endscop
104 
105 }
106 
107 
108 int main(int argc, char** argv)
109 {
110  /* Retrieve problem size. */
111  int ni = NI;
112  int nj = NJ;
113  int nk = NK;
114  int nl = NL;
115 
116  /* Variable declaration/allocation. */
117  DATA_TYPE alpha;
118  DATA_TYPE beta;
124 
125  /* Initialize array(s). */
126  init_array (ni, nj, nk, nl, &alpha, &beta,
130  POLYBENCH_ARRAY(D));
131 
132  /* Start timer. */
134 
135  /* Run kernel. */
136  kernel_2mm (ni, nj, nk, nl,
137  alpha, beta,
138  POLYBENCH_ARRAY(tmp),
142  POLYBENCH_ARRAY(D));
143 
144  /* Stop and print timer. */
147 
148  /* Prevent dead-code elimination. All live-out data must be printed
149  by the function call in argument. */
151 
152  /* Be clean. */
158 
159  return 0;
160 }
#define POLYBENCH_ARRAY(x)
Definition: polybench.h:84
#define NI
Definition: gemm.h:39
#define POLYBENCH_DUMP_BEGIN(s)
Definition: polybench.h:167
#define C
Definition: generate.c:15
#define POLYBENCH_FREE_ARRAY(x)
Definition: polybench.h:88
#define NL
Definition: 2mm.h:45
#define POLYBENCH_2D(var, dim1, dim2, ddim1, ddim2)
Definition: polybench.h:98
static void print_array(int ni, int nl, DATA_TYPE POLYBENCH_2D(D, NI, NL, ni, nl))
Definition: 2mm.c:56
#define A
Definition: generate.c:13
static const uint32_t k[]
Definition: sha-256.c:22
#define NK
Definition: gemm.h:41
int main(int argc, char **argv)
Definition: 2mm.c:108
#define _PB_NJ
Definition: gemm.h:54
#define POLYBENCH_DUMP_START
Definition: polybench.h:165
#define D
Definition: generate.c:16
#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: 2mm.c:75
#define DATA_PRINTF_MODIFIER
Definition: correlation.h:73
#define polybench_prevent_dce(func)
Definition: polybench.h:170
#define NJ
Definition: gemm.h:40
#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 init_array(int ni, int nj, int nk, int nl, DATA_TYPE *alpha, DATA_TYPE *beta, DATA_TYPE POLYBENCH_2D(A, NI, NK, ni, nk), DATA_TYPE POLYBENCH_2D(B, NK, NJ, nk, nj), DATA_TYPE POLYBENCH_2D(C, NJ, NL, nj, nl), DATA_TYPE POLYBENCH_2D(D, NI, NL, ni, nl))
This version is stamped on May 10, 2016.
Definition: 2mm.c:26
#define _PB_NL
Definition: 2mm.h:61
#define POLYBENCH_DUMP_FINISH
Definition: polybench.h:166
#define B
Definition: generate.c:14
#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 _PB_NK
Definition: gemm.h:55
#define DATA_TYPE
Definition: correlation.h:72
#define _PB_NI
Definition: gemm.h:53

Generated on Mon Feb 12 2024 13:02:50 for PandA-2024.02 by doxygen 1.8.13