hanchenye-Polygeist/tools/mlir-clang/Test/polybench/linear-algebra/kernels/mvt/mvt.c

191 lines
5.6 KiB
C

// TODO: mlir-clang %s %stdinclude -S | FileCheck %s
// RUN: clang %s -O3 %stdinclude %polyverify -o %s.exec1 && %s.exec1 &> %s.out1
// RUN: mlir-clang %s %polyverify %stdinclude -O3 -o %s.execm && %s.execm &> %s.out2
// RUN: rm -f %s.exec1 %s.execm
// RUN: diff %s.out1 %s.out2
// RUN: rm -f %s.out1 %s.out2
// RUN: mlir-clang %s %polyexec %stdinclude -O3 -o %s.execm && %s.execm > %s.mlir.time; cat %s.mlir.time | FileCheck %s --check-prefix EXEC
// RUN: clang %s -O3 %polyexec %stdinclude -o %s.exec2 && %s.exec2 > %s.clang.time; cat %s.clang.time | FileCheck %s --check-prefix EXEC
// RUN: rm -f %s.exec2 %s.execm
// RUN: clang %s -O3 %stdinclude %polyverify -o %s.exec1 && %s.exec1 &> %s.out1
// RUN: mlir-clang %s %polyverify %stdinclude -detect-reduction -O3 -o %s.execm && %s.execm &> %s.out2
// RUN: rm -f %s.exec1 %s.execm
// RUN: diff %s.out1 %s.out2
// RUN: rm -f %s.out1 %s.out2
/**
* 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
*/
/* mvt.c: this file is part of PolyBench/C */
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <math.h>
/* Include polybench common header. */
#include <polybench.h>
/* Include benchmark-specific header. */
#include "mvt.h"
/* Array initialization. */
static
void init_array(int n,
DATA_TYPE POLYBENCH_1D(x1,N,n),
DATA_TYPE POLYBENCH_1D(x2,N,n),
DATA_TYPE POLYBENCH_1D(y_1,N,n),
DATA_TYPE POLYBENCH_1D(y_2,N,n),
DATA_TYPE POLYBENCH_2D(A,N,N,n,n))
{
int i, j;
for (i = 0; i < n; i++)
{
x1[i] = (DATA_TYPE) (i % n) / n;
x2[i] = (DATA_TYPE) ((i + 1) % n) / n;
y_1[i] = (DATA_TYPE) ((i + 3) % n) / n;
y_2[i] = (DATA_TYPE) ((i + 4) % n) / n;
for (j = 0; j < n; j++)
A[i][j] = (DATA_TYPE) (i*j % n) / n;
}
}
/* 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 n,
DATA_TYPE POLYBENCH_1D(x1,N,n),
DATA_TYPE POLYBENCH_1D(x2,N,n))
{
int i;
POLYBENCH_DUMP_START;
POLYBENCH_DUMP_BEGIN("x1");
for (i = 0; i < n; i++) {
if (i % 20 == 0) fprintf (POLYBENCH_DUMP_TARGET, "\n");
fprintf (POLYBENCH_DUMP_TARGET, DATA_PRINTF_MODIFIER, x1[i]);
}
POLYBENCH_DUMP_END("x1");
POLYBENCH_DUMP_BEGIN("x2");
for (i = 0; i < n; i++) {
if (i % 20 == 0) fprintf (POLYBENCH_DUMP_TARGET, "\n");
fprintf (POLYBENCH_DUMP_TARGET, DATA_PRINTF_MODIFIER, x2[i]);
}
POLYBENCH_DUMP_END("x2");
POLYBENCH_DUMP_FINISH;
}
/* Main computational kernel. The whole function will be timed,
including the call and return. */
static
void kernel_mvt(int n,
DATA_TYPE POLYBENCH_1D(x1,N,n),
DATA_TYPE POLYBENCH_1D(x2,N,n),
DATA_TYPE POLYBENCH_1D(y_1,N,n),
DATA_TYPE POLYBENCH_1D(y_2,N,n),
DATA_TYPE POLYBENCH_2D(A,N,N,n,n))
{
int i, j;
#pragma scop
for (i = 0; i < _PB_N; i++)
for (j = 0; j < _PB_N; j++)
x1[i] = x1[i] + A[i][j] * y_1[j];
for (i = 0; i < _PB_N; i++)
for (j = 0; j < _PB_N; j++)
x2[i] = x2[i] + A[j][i] * y_2[j];
#pragma endscop
}
int main(int argc, char** argv)
{
/* Retrieve problem size. */
int n = N;
/* Variable declaration/allocation. */
POLYBENCH_2D_ARRAY_DECL(A, DATA_TYPE, N, N, n, n);
POLYBENCH_1D_ARRAY_DECL(x1, DATA_TYPE, N, n);
POLYBENCH_1D_ARRAY_DECL(x2, DATA_TYPE, N, n);
POLYBENCH_1D_ARRAY_DECL(y_1, DATA_TYPE, N, n);
POLYBENCH_1D_ARRAY_DECL(y_2, DATA_TYPE, N, n);
/* Initialize array(s). */
init_array (n,
POLYBENCH_ARRAY(x1),
POLYBENCH_ARRAY(x2),
POLYBENCH_ARRAY(y_1),
POLYBENCH_ARRAY(y_2),
POLYBENCH_ARRAY(A));
/* Start timer. */
polybench_start_instruments;
/* Run kernel. */
kernel_mvt (n,
POLYBENCH_ARRAY(x1),
POLYBENCH_ARRAY(x2),
POLYBENCH_ARRAY(y_1),
POLYBENCH_ARRAY(y_2),
POLYBENCH_ARRAY(A));
/* Stop and print timer. */
polybench_stop_instruments;
polybench_print_instruments;
/* Prevent dead-code elimination. All live-out data must be printed
by the function call in argument. */
polybench_prevent_dce(print_array(n, POLYBENCH_ARRAY(x1), POLYBENCH_ARRAY(x2)));
/* Be clean. */
POLYBENCH_FREE_ARRAY(A);
POLYBENCH_FREE_ARRAY(x1);
POLYBENCH_FREE_ARRAY(x2);
POLYBENCH_FREE_ARRAY(y_1);
POLYBENCH_FREE_ARRAY(y_2);
return 0;
}
// CHECK: func @kernel_mvt(%arg0: i32, %arg1: memref<2000xf64>, %arg2: memref<2000xf64>, %arg3: memref<2000xf64>, %arg4: memref<2000xf64>, %arg5: memref<2000x2000xf64>) {
// CHECK-NEXT: %0 = index_cast %arg0 : i32 to index
// CHECK-NEXT: affine.for %arg6 = 0 to %0 {
// CHECK-NEXT: %1 = affine.load %arg1[%arg6] : memref<2000xf64>
// CHECK-NEXT: affine.for %arg7 = 0 to %0 {
// CHECK-NEXT: %2 = affine.load %arg5[%arg6, %arg7] : memref<2000x2000xf64>
// CHECK-NEXT: %3 = affine.load %arg3[%arg7] : memref<2000xf64>
// CHECK-NEXT: %4 = mulf %2, %3 : f64
// CHECK-NEXT: %5 = addf %1, %4 : f64
// CHECK-NEXT: affine.store %5, %arg1[%arg6] : memref<2000xf64>
// CHECK-NEXT: }
// CHECK-NEXT: }
// CHECK-NEXT: affine.for %arg6 = 0 to %0 {
// CHECK-NEXT: %1 = affine.load %arg2[%arg6] : memref<2000xf64>
// CHECK-NEXT: affine.for %arg7 = 0 to %0 {
// CHECK-NEXT: %2 = affine.load %arg5[%arg7, %arg6] : memref<2000x2000xf64>
// CHECK-NEXT: %3 = affine.load %arg4[%arg7] : memref<2000xf64>
// CHECK-NEXT: %4 = mulf %2, %3 : f64
// CHECK-NEXT: %5 = addf %1, %4 : f64
// CHECK-NEXT: affine.store %5, %arg2[%arg6] : memref<2000xf64>
// CHECK-NEXT: }
// CHECK-NEXT: }
// CHECK-NEXT: return
// CHECK-NEXT:}
// EXEC: {{[0-9]\.[0-9]+}}