#include <stdio.h>
#include <math.h>
//-- setup and print matrices --------------------------------------------------
void
initGeMatrix(int m, int n, double *A, int incRowA, int incColA)
{
int i, j;
for (i=0; i<m; ++i) {
for (j=0; j<n; ++j) {
A[i*incRowA+j*incColA] = i*n + j + 1;
}
}
}
void
printGeMatrix(int m, int n, const double *A, int incRowA, int incColA)
{
int i, j;
for (i=0; i<m; ++i) {
for (j=0; j<n; ++j) {
printf("%10.4lf ", A[i*incRowA+j*incColA]);
}
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; i<n; ++i) {
alpha += x[i*incX]*y[i*incY];
}
return alpha;
}
void
daxpy(int n, double alpha, const double *x, int incX, double *y, int incY)
{
int i;
if (alpha==0) {
return;
}
for (i=0; i<n; ++i) {
y[i*incY] += alpha*x[i*incX];
}
}
void
dscal(int n, double alpha, double *x, int incX)
{
int i;
if (alpha==1) {
return;
}
for (i=0; i<n; ++i) {
x[i*incX] *= alpha;
}
}
void
dcopy(int n, const double *x, int incX, double *y, int incY)
{
int i;
for (i=0; i<n; ++i) {
y[i*incY] = x[i*incX];
}
}
void
dswap(int n, double *x, int incX, double *y, int incY)
{
int i;
for (i=0; i<n; ++i) {
double tmp;
tmp = x[i*incX];
x[i*incX] = y[i*incY];
y[i*incY] = tmp;
}
}
double
damax(int n, const double *x, int incX)
{
int i;
double result = 0;
for (i=0; i<n; ++i) {
if (fabs(x[i*incX])>result) {
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; j<n; ++j) {
double sum = 0;
for (i=0; i<m; ++i) {
sum += fabs(A[i*incRowA+j*incColA]);
}
if (sum>result) {
result = sum;
}
}
return result;
}
//-- GEMV implementations ------------------------------------------------------
void
dgemv_col(int m, int n, double alpha,
const double *A, int incRowA, int incColA,
const double *x, int incX,
double beta,
double *y, int incY)
{
// your code here
}
void
dgemv_row(int m, int n, double alpha,
const double *A, int incRowA, int incColA,
const double *x, int incX,
double beta,
double *y, int incY)
{
// your code here
}
//------------------------------------------------------------------------------
#ifndef DIM_M
#define DIM_M 5
#endif
#ifndef DIM_N
#define DIM_N 6
#endif
#ifndef ROWMAJOR
#define ROWMAJOR 0
#endif
#if (ROWMAJOR==1)
# define INCROW_A DIM_N
# define INCCOL_A 1
#else
# define INCROW_A 1
# define INCCOL_A DIM_M
#endif
#ifndef INC_X
#define INC_X 1
#endif
#ifndef INC_Y
#define INC_Y 1
#endif
#ifndef ALPHA
#define ALPHA 2
#endif
#ifndef BETA
#define BETA 3
#endif
//
// A, x, y_ will get initialized with initGeMatrix
//
double A[DIM_M*DIM_N];
double x[INC_X*DIM_N];
double y_[DIM_M];
//
// If BETA is not zero the result of BETA*y + ALPHA * A *x depends on the
// initial value of y. We initialize y with y_ before each call of a gemv
// implementation. So the results are always the same.
//
double y[INC_Y*DIM_M];
int
main()
{
initGeMatrix(DIM_M, DIM_N, A, INCROW_A, INCCOL_A);
initGeMatrix(DIM_N, 1, x, INC_X, 0);
initGeMatrix(DIM_M, 1, y_, 1, 0);
printf("A =\n");
printGeMatrix(DIM_M, DIM_N, A, INCROW_A, INCCOL_A);
printf("x =\n");
printGeMatrix(DIM_N, 1, x, INC_X, 0);
printf("y =\n");
printGeMatrix(DIM_M, 1, y_, INC_Y, 0);
//
// Aufruf von gemv_col
//
printf("gemv_col: %10.4lf * y + %10.4lf * A * x =\n\n",
(double)BETA, (double)ALPHA);
dcopy(DIM_M, y_, 1, y, INC_Y);
dgemv_col(DIM_M, DIM_N,
ALPHA,
A, INCROW_A, INCCOL_A,
x, INC_X,
BETA,
y, INC_Y);
printGeMatrix(DIM_M, 1, y, INC_Y, 0);
//
// Aufruf von gemv_row
//
printf("gemv_row: %10.4lf * y + %10.4lf * A * x =\n\n",
(double)BETA, (double)ALPHA);
dcopy(DIM_M, y_, 1, y, INC_Y);
dgemv_row(DIM_M, DIM_N,
ALPHA,
A, INCROW_A, INCCOL_A,
x, INC_X,
BETA,
y, INC_Y);
printGeMatrix(DIM_M, 1, y, INC_Y, 0);
return 0;
}