blob: 9d434b5bf57e4a9335dab38f869688bc0d3f12f5 [file] [log] [blame]
/**
* trisolv.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 "trisolv.h"
/* Array initialization. */
static
void init_array(int n,
DATA_TYPE POLYBENCH_2D(A,N,N,n,n),
DATA_TYPE POLYBENCH_1D(x,N,n),
#if !FMA_DISABLED
DATA_TYPE POLYBENCH_1D(x_StrictFP,N,n),
#endif
DATA_TYPE POLYBENCH_1D(c,N,n))
{
#pragma STDC FP_CONTRACT OFF
int i, j;
/*
LLVM: This change ensures we do not calculate nan values, which are
formatted differently on different platforms and which may also
be optimized unexpectedly.
Original code:
for (i = 0; i < n; i++)
{
c[i] = x[i] = ((DATA_TYPE) i) / n;
for (j = 0; j < n; j++)
A[i][j] = ((DATA_TYPE) i*j) / n;
}
*/
for (i = 0; i < n; i++)
{
c[i] =
#if !FMA_DISABLED
x_StrictFP[i] =
#endif
x[i] = ((DATA_TYPE) i+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(x,N,n))
{
int i;
char *printmat = malloc(n*16 + 1); printmat[n*16] = 0;
for (i = 0; i < n; i++)
print_element(x[i], i*16, printmat);
fputs(printmat, stderr);
free(printmat);
}
/* Main computational kernel. The whole function will be timed,
including the call and return. */
static
void kernel_trisolv(int n,
DATA_TYPE POLYBENCH_2D(A,N,N,n,n),
DATA_TYPE POLYBENCH_1D(x,N,n),
DATA_TYPE POLYBENCH_1D(c,N,n))
{
int i, j;
#pragma scop
for (i = 0; i < _PB_N; i++)
{
x[i] = c[i];
for (j = 0; j <= i - 1; j++)
x[i] = x[i] - A[i][j] * x[j];
x[i] = x[i] / A[i][i];
}
#pragma endscop
}
#if !FMA_DISABLED
// NOTE: FMA_DISABLED is true for targets where FMA contraction causes
// discrepancies which cause the accuracy checks to fail.
// In this case, the test runs with the option -ffp-contract=off
static void
kernel_trisolv_StrictFP(int n,
DATA_TYPE POLYBENCH_2D(A,N,N,n,n),
DATA_TYPE POLYBENCH_1D(x,N,n),
DATA_TYPE POLYBENCH_1D(c,N,n))
{
#pragma STDC FP_CONTRACT OFF
int i, j;
for (i = 0; i < _PB_N; i++)
{
x[i] = c[i];
for (j = 0; j <= i - 1; j++)
x[i] = x[i] - A[i][j] * x[j];
x[i] = x[i] / A[i][i];
}
}
/* 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 int
check_FP(int n,
DATA_TYPE POLYBENCH_1D(A,N,n),
DATA_TYPE POLYBENCH_1D(B,N,n)) {
int i;
double AbsTolerance = FP_ABSTOLERANCE;
for (i = 0; i < _PB_N; i++)
{
double V1 = A[i];
double V2 = B[i];
double Diff = fabs(V1 - V2);
if (Diff > AbsTolerance) {
fprintf(stderr, "A[%d] = %lf and B[%d] = %lf differ more than"
" FP_ABSTOLERANCE = %lf\n", i, V1, i, V2, AbsTolerance);
return 0;
}
}
return 1;
}
#endif
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(x, DATA_TYPE, N, n);
#if !FMA_DISABLED
POLYBENCH_1D_ARRAY_DECL(x_StrictFP, DATA_TYPE, N, n);
#endif
POLYBENCH_1D_ARRAY_DECL(c, DATA_TYPE, N, n);
/* Initialize array(s). */
init_array (n, POLYBENCH_ARRAY(A), POLYBENCH_ARRAY(x),
#if !FMA_DISABLED
POLYBENCH_ARRAY(x_StrictFP),
#endif
POLYBENCH_ARRAY(c));
/* Start timer. */
polybench_start_instruments;
/* Run kernel. */
kernel_trisolv (n, POLYBENCH_ARRAY(A), POLYBENCH_ARRAY(x), POLYBENCH_ARRAY(c));
/* Stop and print timer. */
polybench_stop_instruments;
polybench_print_instruments;
#if FMA_DISABLED
/* 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(x)));
#else
kernel_trisolv (n, POLYBENCH_ARRAY(A), POLYBENCH_ARRAY(x_StrictFP), POLYBENCH_ARRAY(c));
/* 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(x_StrictFP)));
#endif
/* Be clean. */
POLYBENCH_FREE_ARRAY(A);
POLYBENCH_FREE_ARRAY(x);
#if !FMA_DISABLED
POLYBENCH_FREE_ARRAY(x_StrictFP);
#endif
POLYBENCH_FREE_ARRAY(c);
return 0;
}