Category : C++ Source Code
Archive   : BLFMATH.ZIP
Filename : TSTSMAT2.CPP

 
Output of file : TSTSMAT2.CPP contained in archive : BLFMATH.ZIP
//////////////////////////////////////////////////////////////
// tstsmat2.cpp: Timing test program for matrix multiplies
// of SimpleMatrix objects.
// Copyright(c) 1993 Azarona Software. All rights reserved.
//////////////////////////////////////////////////////////////

#include
#include
#include
#include
#include "simpmat.h"

typedef double Number;

const int d1 = 30; // We'll do 30x30 matrices
const int d2 = 30;

SimpleMatrix a(d1, d2);
SimpleMatrix b(d2, d1);
SimpleMatrix c(d1, d1);

void Test1(SimpleMatrix &c,
const SimpleMatrix &a, const SimpleMatrix &b)
// Uses double subscripting
{
unsigned i, j, k;

for (i = 0; i < a.NRows(); i++) {
for (j = 0; j < b.NCols(); j++) {
Number sum = 0;
for (k = 0; k < b.NRows(); k++) sum += a[i][k] * b[k][j];
c[i][j] = sum;
}
}
}

void Test2(SimpleMatrix &c,
const SimpleMatrix &a, const SimpleMatrix &b)
// Factors some of the vector construction out of the loops
{
unsigned i, j, k;

for (i = 0; i < a.NRows(); i++) {
VecPtr ar = a.RowPtr(i);
VecPtr cr = c.RowPtr(i);
for (j = 0; j < b.NCols(); j++) {
Number sum = 0;
VecPtr bc = b.ColPtr(j);
for (k = 0; k < b.NRows(); k++) {
sum += ar[k] * bc[k];
}
cr[j] = sum;
}
}
}

void Test3(SimpleMatrix &c,
const SimpleMatrix &a, const SimpleMatrix &b)
// Fastest form using vector pointers
{
unsigned i, j, k;

VecPtr ar = a.RowPtr();
VecPtr ac = a.ColPtr();
VecPtr br = b.RowPtr();
VecPtr bc = b.ColPtr();
VecPtr cr = c.RowPtr();
VecPtr cc = c.ColPtr();

Number const *bstart = br;

for (i = 0; i < a.NRows(); i++) {
cr = cc; // Point to start of row i. Note: stride not copied
br = bstart;
for (j = 0; j < b.NCols(); j++) {
Number sum = 0;
ar = ac; // Point to start of row i. Note: stride not copied
bc = br; // Point to start of col j. Note: stride not copied
for (k = 0; k < b.NRows(); k++) {
sum += *ar * *bc;
ar++; // Next col
bc++; // Next row
}
br++; // Next col
*cr = sum;
cr++; // Next col
}
ac++; // Next row
cc++; // Next row
}
}

void Test4(SimpleMatrix &c,
const SimpleMatrix &a, const SimpleMatrix &b)
// Possibly, fastest of all, by assuming row vectors have stride
// of 1, and thus using ordinary pointers instead
{
unsigned i, j, k;

const Number *ar = a.RowPtr();
VecPtr ac = a.ColPtr();
const Number *br = b.RowPtr();
VecPtr bc = b.ColPtr();
Number *cr = c.RowPtr();
VecPtr cc = c.ColPtr();

const Number *bstart = br;

for (i = 0; i < a.NRows(); i++) {
cr = cc; // Point to start of row i
br = bstart;
for (j = 0; j < b.NCols(); j++) {
Number sum = 0;
ar = ac; // Point to start of row i.
bc = br; // Point to start of col j.
for (k = 0; k < b.NRows(); k++) {
sum += *ar * *bc;
ar++; // Next col
bc++; // Next row
}
br++; // Next col
*cr = sum;
cr++; // Next col
}
ac++; // Next row
cc++; // Next row
}
}

void testn(int n, unsigned unsigned num_iter)
{
unsigned i;

cout << "Press return to start test " << n;
getch();
cout << '\n';
switch(n) {
case 1: for(i = 0; i break;
case 2: for(i = 0; i break;
case 3: for(i = 0; i break;
case 4: for(i = 0; i break;
}
cout << "Finished\n";
}

main(int argc, char *argv[])
{
unsigned n = 1;

if (argc <= 1) {
cout << "Usage: tstmat2 [num_iter]\n";
return 1;
}

n = (unsigned)atoi(argv[1]);

unsigned i, j, k;


for(i = 0, k = 1; i for(j = 0; j
for(i = 0, k = 2; i for(j = 0; j
testn(1, n);
testn(2, n);
testn(3, n);
testn(4, n);
return 0;
}



  3 Responses to “Category : C++ Source Code
Archive   : BLFMATH.ZIP
Filename : TSTSMAT2.CPP

  1. Very nice! Thank you for this wonderful archive. I wonder why I found it only now. Long live the BBS file archives!

  2. This is so awesome! 😀 I’d be cool if you could download an entire archive of this at once, though.

  3. But one thing that puzzles me is the “mtswslnkmcjklsdlsbdmMICROSOFT” string. There is an article about it here. It is definitely worth a read: http://www.os2museum.com/wp/mtswslnk/