Category : C++ Source Code
Archive   : BLFMATH.ZIP
Filename : TSTMAT4.CPP
// tstmat4.cpp: This program compares using static 2-d matrices
// with the more general SimpleMatrix and Matrix objects when
// it comes to doing matrix multiplies.
// Copyright(c) 1993 Azarona Software. All rights reserved.
//
// Usage: tstmat4 [num_iter]
//
// Meant to be used with a stop-watch to time the algorithms.
//
// Test 1: Uses statically allocated matrices -- fast!
// Test 2: Uses dynamically allocated SimpleMatrix objects,
// with m[i][j] style of subscripting.
// Test 3: Uses fast vector pointer form for SimpleMatrix objects
// Test 4: Uses Matrix objects with m[i][j] subscripting. (Slow!)
// Test 5: Uses Matrix objects with m(i,j) subscripting.
// Test 6: Uses fastest form possible for Matrix objects, using
// fast vector pointers.
/////////////////////////////////////////////////////////////////
#include
#include
#include
#include
#include "simpmat.h"
#include "matrix.h"
typedef double Number;
INITNULLVEC(Number);
const int d1 = 30;
const int d2 = 30;
Number raw_a[d1][d2];
Number raw_b[d2][d1];
Number raw_c[d1][d1];
SimpleMatrix
SimpleMatrix
SimpleMatrix
Matrix
Matrix
Matrix
void Test1(Number (*c)[d1], const Number (*a)[d2], const Number (*b)[d1])
// Fastest, cause it uses statically allocated matrices, with constant
// dimensions. However, very inflexible.
{
unsigned i, j, k;
for (i = 0; i < d1; i++) {
for (j = 0; j < d1; j++) {
Number sum = 0;
for (k = 0; k < d2; k++) sum += a[i][k] * b[k][j];
c[i][j] = sum;
}
}
}
template
void Test2(SimpleMatrix
const SimpleMatrix
// Uses dynamically allocated SimpleMatrix objects with subscripting.
// Kind of slow.
{
unsigned i, j, k;
for (i = 0; i < a.NRows(); i++) {
for (j = 0; j < b.NCols(); j++) {
TYPE sum = 0;
for (k = 0; k < b.NRows(); k++) sum += a[i][k] * b[k][j];
c[i][j] = sum;
}
}
}
template
void Test3(SimpleMatrix
const SimpleMatrix
// Fastest form for SimpleMatrices, by assuming row vectors
// have a stride of 1, thus using ordinary pointers instead
{
unsigned i, j, k;
#ifdef SUPPOSEDLYFASTEST
const TYPE *ar = a.RowPtr();
#else
VecPtr
#endif
VecPtr
#ifdef SUPPOSEDLYFASTEST
const TYPE *br = b.RowPtr();
#else
VecPtr
#endif
VecPtr
#ifdef SUPPOSEDLYFASTEST
TYPE *cr = c.RowPtr();
#else
VecPtr
#endif
VecPtr
const TYPE *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++) {
TYPE 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
}
}
template
void Test4(Matrix
// Uses dynamically allocated Matrix objects with double-subscripting.
// Really S--L--O--W.
{
unsigned i, j, k;
for (i = 0; i < a.NRows(); i++) {
for (j = 0; j < b.NCols(); j++) {
TYPE sum = 0;
for (k = 0; k < b.NRows(); k++) sum += a[i][k] * b[k][j];
c[i][j] = sum;
}
}
}
template
void Test5(Matrix
// Uses dynamically allocated Matrix objects with 2-d subscripting.
// Better than Test4, but still slow.
{
unsigned i, j, k;
for (i = 0; i < a.NRows(); i++) {
for (j = 0; j < b.NCols(); j++) {
TYPE sum = 0;
for (k = 0; k < b.NRows(); k++) sum += a(i,k) * b(k,j);
c(i,j) = sum;
}
}
}
template
void Test6(Matrix
// Fastest routine for Matrix objects, using vector pointers.
// Should be pretty decent.
{
unsigned i, j, k;
VecPtr
VecPtr
VecPtr
VecPtr
VecPtr
VecPtr
const TYPE *bstart = b.PtrToAll();
for (i = 0; i < a.NRows(); i++) {
cr = cc; // Point to start of row i
br = bstart;
for (j = 0; j < b.NCols(); j++) {
TYPE 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
case 5: for(i = 0; i
case 6: for(i = 0; i
}
cout << "Finished\n";
}
main(int argc, char *argv[])
{
unsigned n = 1;
if (argc <= 1) {
cout << "Usage: tstmat4 [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
for(i = 0, k = 1; i
for(i = 0, k = 2; i
for(i = 0, k = 1; i
for(i = 0, k = 2; i
testn(1, n);
testn(2, n);
testn(3, n);
testn(4, n);
testn(5, n);
testn(6, 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/