Main Page | Namespace List | Data Structures | File List | Namespace Members | Data Fields | Globals

ublastest.cpp

Go to the documentation of this file.
00001 /* main.cpp  -  main routines */
00002 /***************************************************************************
00003     begin                : Don Mär 14 10:19:20 MET 2002
00004     copyright            : (C) 2002 by Gunter Winkler
00005     email                : guwi17@gmx.de
00006  ***************************************************************************/
00007 
00008 /***************************************************************************
00009  *                                                                         *
00010  *   This program is free software; you can redistribute it and/or modify  *
00011  *   it under the terms of the GNU General Public License as published by  *
00012  *   the Free Software Foundation; either version 2 of the License, or     *
00013  *   (at your option) any later version.                                   *
00014  *                                                                         *
00015  ***************************************************************************/
00016 
00017 /*
00018  * $Log$
00019  */
00020 
00021 #ifdef HAVE_CONFIG_H
00022 #include <config.h>
00023 #endif
00024 
00025 #include <iostream>
00026 #include <iomanip>
00027 #include <sstream>
00028 #include <fstream>
00029 
00030 #include <sys/time.h>
00031 #include <unistd.h>
00032 
00033 #include "itl/interface/boost.h"
00034 #include "itl/preconditioner/ilu_new.h"
00035 
00036 #include <boost/numeric/ublas/matrix.hpp>
00037 #include <boost/numeric/ublas/matrix_sparse.hpp>
00038 #include <boost/numeric/ublas/matrix_proxy.hpp>
00039 #include <boost/numeric/ublas/vector.hpp>
00040 #include <boost/numeric/ublas/io.hpp>
00041 #include <boost/numeric/ublas/operation.hpp>
00042 
00043 
00044 /* private includes */
00045 
00046 #ifndef NDEBUG
00047 volatile const static char _main_RCSID[] = "$Id$";
00048 #endif
00049 
00050 using std::cout;
00051 using std::cin;
00052 using std::endl;
00053 using std::ofstream;
00054 
00055 using std::min;
00056 using std::max;
00057 
00058 using namespace boost::numeric::ublas;
00059 
00060 typedef sparse_matrix<double> uBLASSparseMatrix;
00061 typedef vector<double>            uBLASVector;
00062 
00063 int main(int argc, char *argv[])
00064 {
00065 
00066   // type       of stiffnes matrix
00067   typedef sparse_matrix<double>   MY_STIMA;
00068   typedef matrix_row< MY_STIMA >  MY_ROW;
00069   typedef vector<double>          MY_VEC;
00070   timeval t[7];
00071 
00072   int N = 100;
00073   //  cin >> N;
00074 
00075   MY_STIMA M(4,4,16);
00076 
00077   for (int i=0; i<M.size1(); ++i) M(i,i)=1.0;
00078   M(0,0)=3;
00079   M(0,1)=-1;
00080   M(0,2)=-2;
00081   M(1,0)=-1;
00082   M(1,1)=4;
00083   M(1,2)=-2;
00084   M(2,0)=-2;
00085   M(2,1)=-2;
00086   M(2,2)=9;
00087 
00088   itl::ILU<MY_STIMA> precond(M);
00089 
00090   MY_VEC xx(M.size2());
00091   MY_VEC yy(M.size2());
00092 
00093   yy(0) = 0; yy(1) = 1; yy(2) = 5;
00094 
00095   precond().solve(yy,xx);
00096 
00097   cout << "x= " << xx << "\n";
00098 
00099 //   for (MY_STIMA::iterator1 i=M.begin1(); i!=M.end1(); ++i) {
00100 //      cout << i.index1() << " " << i.index2() << " " << (*i) << "\n";
00101 //   }
00102 
00103   MY_ROW r(M, 2);
00104 
00105   for (MY_ROW::iterator i=r.begin(); i!=r.end(); ++i) {
00106         cout << i.index() << " " << (*i) << "\n";
00107   }
00108 
00109   MY_STIMA::iterator1 r_it1 = M.begin1();
00110   MY_STIMA::iterator2 c_it1 = M.begin2();
00111   while ( (r_it1 != M.end1()) && (c_it1 != M.end2()) ) {
00112         MY_STIMA::iterator1  r_it2 = c_it1.begin();
00113         MY_STIMA::iterator2  c_it2 = r_it1.begin();
00114         MY_STIMA::iterator2  c;
00115 
00116         c = c_it2;
00117         while ( (r_it2 != c_it1.end()) && (c != r_it1.end()) ) {
00118           while (c.index2() < r_it2.index1()) { ++c; }
00119           while (c.index2() > r_it2.index1()) { ++r_it2; }
00120           if (c.index2() == r_it2.index1()) {
00121                 cout << r_it2.index1() << ", " << c.index2() << ": " << (*c) << " += " << (*r_it2);
00122                 (*c) += (*r_it2);
00123                 cout << " --> " << (*c) << " ? \n";
00124                 ++ r_it2;
00125                 ++ c;
00126           }
00127         }
00128         ++ r_it1;
00129         ++ c_it1;
00130   }
00131 
00132   cout << "M = " << M << endl;
00133 
00134   cout << "Benchmark for linear algebra in uBLAS" << endl;
00135   cout << "--------------------------------------" << endl;
00136 
00137   gettimeofday( &t[0], NULL );
00138 
00139   cout << "Creating a vector with " << N << " elements" << endl;
00140   uBLASVector x(N);
00141 
00142   gettimeofday( &t[1], NULL );
00143 
00144   cout << "Creating another vector with " << N << " elements" << endl;
00145   uBLASVector y(N);
00146 
00147   gettimeofday( &t[2], NULL );
00148 
00149   cout << "Creating a " << N << " x " << N << " matrix" << endl;
00150   uBLASSparseMatrix A(N, N);
00151 
00152   gettimeofday( &t[3], NULL );
00153 
00154   cout << "Assembling" << endl;
00155   for (int i = 0; i < N; i++) {
00156     A(i,i) += 2.0;
00157 
00158     if ( i > 0 )
00159       A(i, i - 1) += -1.0;
00160 
00161     if ( i < (N-1) )
00162       A(i, i + 1) += 1.0;
00163   }
00164 
00165   gettimeofday( &t[4], NULL );
00166 
00167   cout << "Multiplying: y = A*x" << endl;
00168   axpy_prod(A, x, y);
00169 
00170   gettimeofday( &t[5], NULL );
00171 
00172   double res = 0;
00173   for (int i = 0; i<N; ++i) res += y[i];
00174   cout << res << endl;
00175 
00176   for (int i = 0; i<5; ++i) {
00177         double t1=(double(t[i].tv_sec)+t[i].tv_usec/1000000.0);
00178         double t2=(double(t[i+1].tv_sec)+t[i+1].tv_usec/1000000.0);
00179         double time=(t2-t1);
00180         cout << "time " << i << ": " << time << "\n";
00181   }
00182   cout << endl;
00183 
00184   return EXIT_SUCCESS;
00185 };

Generated on Wed Oct 1 14:41:00 2003 for Sample Code by doxygen 1.3.2