Category : C++ Source Code
Archive   : BLFMATH.ZIP
Filename : TSTSMAT2.CPP
// 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
SimpleMatrix
SimpleMatrix
void Test1(SimpleMatrix
const SimpleMatrix
// 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
const SimpleMatrix
// Factors some of the vector construction out of the loops
{
unsigned i, j, k;
for (i = 0; i < a.NRows(); i++) {
VecPtr
VecPtr
for (j = 0; j < b.NCols(); j++) {
Number sum = 0;
VecPtr
for (k = 0; k < b.NRows(); k++) {
sum += ar[k] * bc[k];
}
cr[j] = sum;
}
}
}
void Test3(SimpleMatrix
const SimpleMatrix
// Fastest form using vector pointers
{
unsigned i, j, k;
VecPtr
VecPtr
VecPtr
VecPtr
VecPtr
VecPtr
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
const SimpleMatrix
// 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
const Number *br = b.RowPtr();
VecPtr
Number *cr = c.RowPtr();
VecPtr
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
case 2: for(i = 0; i
case 3: for(i = 0; i
case 4: for(i = 0; i
}
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(i = 0, k = 2; i
testn(1, n);
testn(2, n);
testn(3, n);
testn(4, n);
return 0;
}
Very nice! Thank you for this wonderful archive. I wonder why I found it only now. Long live the BBS file archives!
This is so awesome! 😀 I’d be cool if you could download an entire archive of this at once, though.
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/