#include #include #include #include #include //-- timer for benchmarks ------------------------------------------------------ double walltime() { struct tms ts; static double ClockTick=0.0; if (ClockTick==0.0) { ClockTick = 1.0 / ((double) sysconf(_SC_CLK_TCK)); } return ((double) times(&ts)) * ClockTick; } //-- setup and print matrices -------------------------------------------------- void initGeMatrix(int m, int n, double *A, int incRowA, int incColA) { int i, j; for (i=0; i=j)) || (!lower && (i<=j))) { printf("%10.4lf ", A[i*incRowA+j*incColA]); } else { printf("%10.4lf ", 0.0); } } printf("\n"); } printf("\n\n"); } //-- some BLAS Level 1 procedures and functions -------------------------------- double ddot(int n, const double *x, int incX, const double *y, int incY) { int i; double alpha = 0; for (i=0; iresult) { result = fabs(x[i*incX]); } } return result; } double dgenrm1(int m, int n, const double *A, int incRowA, int incColA) { int i, j; double result = 0; for (j=0; jresult) { result = sum; } } return result; } double dtrnrm1(int m, int n, int unitDiag, int lower, const double *A, int incRowA, int incColA) { int i, j; double result = 0; for (j=0; j=j)) || (!lower && (i<=j))) { sum += fabs(A[i*incRowA+j*incColA]); } } if (sum>result) { result = sum; } } return result; } //-- error bounds -------------------------------------------------------------- double err_dgemv(int m, int n, double alpha, const double *A, int incRowA, int incColA, const double *x, int incX, double beta, const double *y0, int incY0, double *y1, int incY1) { int max_mn = (m>n) ? m : n; double normA = fabs(alpha)*dgenrm1(m, n, A, incRowA, incColA); double normX = damax(n, x, incX); double normY0 = fabs(beta)*damax(m, y0, incY0); double normD; daxpy(m, -1.0, y0, incY0, y1, incY1); normD = damax(n, y1, incY1); return normD/(normA*normX*normY0*max_mn); } double err_dtrmv(int n, int unitDiag, int lower, const double *A, int incRowA, int incColA, const double *x0, int incX0, double *x1, int incX1) { double normA = dtrnrm1(n, n, unitDiag, lower, A, incRowA, incColA); double normX0 = damax(n, x0, incX0); double normD; daxpy(n, -1.0, x0, incX0, x1, incX1); normD = damax(n, x1, incX1); return normD/(n*normA*normX0); } double err_dtrsv(int n, int unitDiag, int lower, const double *A, int incRowA, int incColA, const double *x0, int incX0, double *x1, int incX1) { double normA = dtrnrm1(n, n, unitDiag, lower, A, incRowA, incColA); double normX0 = damax(n, x0, incX0); double normD; daxpy(n, -1.0, x0, incX0, x1, incX1); normD = damax(n, x1, incX1); return normD/(n*normA*normX0); } double err_dger(int m, int n, double alpha, const double *x, int incX, const double *y, int incY, const double *A0, int incRowA0, int incColA0, double *A, int incRowA, int incColA) { double normA0 = dgenrm1(m, n, A0, incRowA0, incColA0); double normX = fabs(alpha)*damax(m, x, incX); double normY = damax(n, y, incY); double normD; int mn = (m>n) ? m : n; dgeaxpy(m, n, -1.0, A0, incRowA0, incColA0, A, incRowA, incColA); normD = dgenrm1(m, n, A, incRowA, incColA); return normD/(mn*normA0*normX*normY); } void dgemm(int m, int n, int k, double alpha, const double *A, int incRowA, int incColA, const double *B, int incRowB, int incColB, double beta, double *C, int incRowC, int incColC) { int i, j, l; if (beta!=1) { for (i=0; ij) ? LU[i*incRowLU+j*incColLU] : (i==j) ? 1 : 0; } } for (j=0; j=0; --j) { for (i=j+1; i=0; --k) { daxpy(n-k-1, x[k*incX], &A[(k+1)*incRowA+k*incColA], incRowA, &x[(k+1)*incX], incX); } } else { for (k=n-1; k>=0; --k) { daxpy(n-k-1, x[k*incX], &A[(k+1)*incRowA+k*incColA], incRowA, &x[(k+1)*incX], incX); x[k*incX] *= A[k*(incRowA+incColA)]; } } } void dtrlmv_row(int n, int unitDiag, const double *A, int incRowA, int incColA, double *x, int incX) { int k; if (unitDiag) { for (k=n-1; k>=0; --k) { x[k*incX] += ddot(k, &A[k*incRowA], incColA, x, incX); } } else { for (k=n-1; k>=0; --k) { x[k*incX] = ddot(k+1, &A[k*incRowA], incColA, x, incX); } } } void dtrlmv(int n, int unitDiag, const double *A, int incRowA, int incColA, double *x, int incX) { if (incRowA=0; k-=bs) { dgemv_mxBS(n-k-bs, 1.0, &A[(k+bs)*incRowA+k*incColA], incRowA, incColA, &x[k*incX], incX, &x[(k+bs)*incX], incX); dtrlmv_col(bs, unitDiag, &A[k*(incRowA+incColA)], incRowA, incColA, &x[k*incX], incX); } } else { double buffer[DGEMV_BSxN]; int bs = DGEMV_BSxN; int k = (n/bs)*bs; dgemv(n-k, k, 1.0, &A[k*incRowA], incRowA, incColA, x, incX, 0.0, buffer,1); dtrlmv_row(n-k, unitDiag, &A[k*(incRowA+incColA)], incRowA, incColA, &x[k*incX], incX); daxpy(n-k, 1.0, buffer, 1, &x[k*incX], incX); for (k-=bs; k>=0; k-=bs) { dgemv_BSxn(k, 1.0, &A[k*incRowA], incRowA, incColA, x, incX, 0.0, buffer, 1); dtrlmv_col(bs, unitDiag, &A[k*(incRowA+incColA)], incRowA, incColA, &x[k*incX], incX); daxpy(bs, 1.0, buffer, 1, &x[k*incX], incX); } } } //-- TRSV implementations ------------------------------------------------------ void dtrlsv_ref(int n, int unitDiag, const double *A, int incRowA, int incColA, double *x, int incX) { int i, j; for (i=0; in) ? m : n; int j; for (j=1; j(Y) ? (X) : (Y)) double A_[MAX_N*MAX_N]; double A_0[MAX_N*MAX_N*MIN(INCROW_A,INCCOL_A)]; int main() { int m, n; randGeMatrix(MAX_N, MAX_N, A_, 1, MAX_M); printf("#%9s %9s %9s %9s", "m", "n", "INCROW_A", "INCCOL_A"); printf(" %12s %12s %12s", "t", "MFLOPS", "err"); printf(" %12s %12s %12s", "t", "MFLOPS", "err"); printf(" %12s %12s %12s", "t", "MFLOPS", "err"); printf(" %12s %12s %12s", "t", "MFLOPS", "err"); printf(" %12s %12s %12s", "t", "MFLOPS", "err"); printf("\n"); for (m=MIN_M, n=MIN_N; n<=MAX_N && m<=MAX_M; m+=INC_M, n+=INC_N) { double t, dt, err; int runs = 1; int minMN = MIN(m,n); int maxMN = MAX(m,n); double ops = minMN*minMN*maxMN -(minMN*minMN*minMN/3.) -(minMN*minMN/2.); printf(" %9d %9d %9d %9d", m, n, INCROW_A, INCCOL_A); t = 0; runs = 0; do { dgecopy(m, n, A_, 1, MAX_M, A_0, INCROW_A, INCCOL_A); dt = walltime(); lu_unblk_var1(m, n, A_0, INCROW_A, INCCOL_A); dt = walltime() - dt; t += dt; ++runs; } while (t<0.3); t /= runs; err = err_lu(m, n, A_, 1, MAX_M, A_0, INCROW_A, INCCOL_A); printf(" %12.2e %12.2lf %12.2e", t, ops/(1000*1000*t), err); t = 0; runs = 0; do { dgecopy(m, n, A_, 1, MAX_M, A_0, INCROW_A, INCCOL_A); dt = walltime(); lu_unblk_var2(m, n, A_0, INCROW_A, INCCOL_A); dt = walltime() - dt; t += dt; ++runs; } while (t<0.3); t /= runs; err = err_lu(m, n, A_, 1, MAX_M, A_0, INCROW_A, INCCOL_A); printf(" %12.2e %12.2lf %12.2e", t, ops/(1000*1000*t), err); t = 0; runs = 0; do { dgecopy(m, n, A_, 1, MAX_M, A_0, INCROW_A, INCCOL_A); dt = walltime(); lu_unblk_var3(m, n, A_0, INCROW_A, INCCOL_A); dt = walltime() - dt; t += dt; ++runs; } while (t<0.3); t /= runs; err = err_lu(m, n, A_, 1, MAX_M, A_0, INCROW_A, INCCOL_A); printf(" %12.2e %12.2lf %12.2e", t, ops/(1000*1000*t), err); t = 0; runs = 0; do { dgecopy(m, n, A_, 1, MAX_M, A_0, INCROW_A, INCCOL_A); dt = walltime(); lu_unblk_var4(m, n, A_0, INCROW_A, INCCOL_A); dt = walltime() - dt; t += dt; ++runs; } while (t<0.3); t /= runs; err = err_lu(m, n, A_, 1, MAX_M, A_0, INCROW_A, INCCOL_A); printf(" %12.2e %12.2lf %12.2e", t, ops/(1000*1000*t), err); t = 0; runs = 0; do { dgecopy(m, n, A_, 1, MAX_M, A_0, INCROW_A, INCCOL_A); dt = walltime(); lu_unblk_var5(m, n, A_0, INCROW_A, INCCOL_A); dt = walltime() - dt; t += dt; ++runs; } while (t<0.3); t /= runs; err = err_lu(m, n, A_, 1, MAX_M, A_0, INCROW_A, INCCOL_A); printf(" %12.2e %12.2lf %12.2e", t, ops/(1000*1000*t), err); printf("\n"); } return 0; }