[Have to make another thread, because I belive my fucking private
modem provider has some problems with his new service]
n Beitrag DZWdnYgvL9NvBNXdRVn-vA@io.com schrieb Duke Robillard unter
duke@NOSPAMio.com am 05.03.2004 16:18 Uhr:
I am well aware that MHZ is not everything. I bought the ibook simply
out of "friendship" to apple and because I like their embracing of
Unix and BSD. I am not a Unix guy, but I felt better when supporting a
company, because sometimes I went sour of all that "open source"
zealots. Btw: I am a PhD student in physics. And Btw: that message is
typed and consigned from the ibook (connecting to the internet was
easy, though):
The following has been simply pinched from the language shootout page.
I am not a versed C programmer. I have also a C++ version which relies
on the vector class and the behavior is the same.
NOTE: I use mostly Bigloo for scientific computing. And to my great
surprise the Bigloo code is on the ibook 2 times faster than the same
code on the stationary machine. That is a bit surprising, because the
Scheme (bigloo) compiler produce a.out's which are more or less ANSI
C. BUT as I wrote if I use a simple C version (see below) then that C
version performs much better on the 450 MHz P2.
Use it as follows:
g++ -faltivec -fast -O3 bench.cpp
I do not think that a pure C version based on gcc instead of g++ (lets
say without "cout" will be faster).
I must honestly say I have never used any matrix matrix multiplication
in my life (at least not conciously), but it gives a quick hint on
performance.
NOTE: The version is not correct, because one should transpose the
second array, but that does not matter yet.
==
#include <iostream>
#include <stdlib.h>
using namespace std;
double **mkmatrix(int rows, int cols) {
int i, j, count =1;
double **m = (double**)malloc(rows * sizeof(double*));
for (i=0; i<rows; i++) {
m[i] = (double*)malloc(cols*sizeof(double));
for (j=0;j<cols;j++){
m[i][j] = count++;
}
}
return(m);
}
void zeromatrix(int rows, int cols, double **m) {
int i,j;
for (i=0; i<rows;i++)
for (j=0;j<cols;j++)
m[i][j]=0;}
void freematrix(int rows, double **m) {
while (--rows > -1) {free(m[rows]);}
free(m);}
double **mmult(int rows, int cols, double **m1, double **m2, double
**m3) {
int i,j,k;
double val;
for (i=0; i<rows;i++){
for (j=0;j<cols;j++){
val = double(0.0);
for (k=0; k<cols; k++){
val += m1[i][k] * m3[j][k];
}
m3[i][j]=val;
}}return(m3);}
int main() {
int i, SIZE=512;
double **m1 = mkmatrix(SIZE, SIZE);
double **m2 = mkmatrix(SIZE, SIZE);
double **mm = mkmatrix(SIZE, SIZE);
mm = mmult(SIZE,SIZE, m1, m2,mm);
//cout << mm[0][0] << endl;
freematrix(SIZE,m1);
freematrix(SIZE,m2);
freematrix(SIZE,mm);
return(0);
}>
==
Enclosed also a C++ version. I haven't written more than 10 C++
programs in my life; I just wanted to check whether it is possible to
"simulate" a bit Scheme style programming.
==
#include <iostream>
#include <vector>
using namespace std;
vector<vector<double> > mkmatrix(int rows, int cols)
{
double count=double(1.0);
vector<vector<double> > m(rows,cols);
for(int i=0; i<rows; i++)
{
for(int j=0; j<cols; j++)
{
m[i][j]=count;
count += 1.0;
}
}
return(m);
}
vector<double> mkvector(int cols)
{
vector<double> erg;
double val = double(0.0);
for(int i=0; i<cols; i++)
{
erg.push_back(val);
}
return(erg);
}
vector<vector<double> > mmult(int rows, int cols,
vector<vector<double> > m1, vector<vector<double> > m2)
{
double val;
vector<vector<double> > m3(rows,cols);
for(int i=0; i<rows; i++)
{
vector<double> m1i = m1[i];
vector<double> row(cols);
for(int j=0; j<rows; j++)
{
val = double(0.0);
for(int k=0; k<cols; k++)
{
val += m1i[k] * m2[j][k];
}
row[j] = val;
}
m3[i]=row;
}
return(m3);
}
int main()
{
int SIZE=512;
vector<vector<double> > m1 = mkmatrix(SIZE,SIZE);
vector<vector<double> > m2 = mkmatrix(SIZE,SIZE);
vector<vector<double> > mm = mmult(SIZE,SIZE,m1,m2);
cout << mm[0][0] << " " << mm[4][4] << endl;
return(0);
}
==
Fensterbrett