Stxxl  1.4.0
containers/benchmark_naive_matrix.cpp
Go to the documentation of this file.
00001 /***************************************************************************
00002  *  containers/test_matrix.cpp
00003  *
00004  *  Part of the STXXL. See http://stxxl.sourceforge.net
00005  *
00006  *  Copyright (C) 2010 Johannes Singler <singler@kit.edu>
00007  *
00008  *  Distributed under the Boost Software License, Version 1.0.
00009  *  (See accompanying file LICENSE_1_0.txt or copy at
00010  *  http://www.boost.org/LICENSE_1_0.txt)
00011  **************************************************************************/
00012 
00013 //! \example containers/test_matrix.cpp
00014 //! This is an example of how to represent a 2D matrix by using an \c stxxl::vector.
00015 //! DISCLAIMER: The approach is very basic and the matrix multiplication is NOT I/O-EFFICIENT.
00016 
00017 #include <iostream>
00018 #include <stxxl/vector>
00019 
00020 template <typename T>
00021 class matrix2d
00022 {
00023     stxxl::vector<T> v;
00024     stxxl::unsigned_type width, height;
00025 
00026 public:
00027     matrix2d(stxxl::unsigned_type width, stxxl::unsigned_type height) : width(width), height(height)
00028     {
00029         v.resize(width * height);
00030     }
00031 
00032     stxxl::unsigned_type get_width() const
00033     {
00034         return width;
00035     }
00036 
00037     stxxl::unsigned_type get_height() const
00038     {
00039         return height;
00040     }
00041 
00042 
00043     T & element(stxxl::unsigned_type x, stxxl::unsigned_type y)
00044     {
00045         //row-major
00046         return v[y * width + x];
00047     }
00048 
00049     const T & const_element(stxxl::unsigned_type x, stxxl::unsigned_type y) const
00050     {
00051         //row-major
00052         return v[y * width + x];
00053     }
00054 };
00055 
00056 template <typename T>
00057 void matrix_multiply(const matrix2d<T> & a, const matrix2d<T> & b, matrix2d<T> & c)
00058 {
00059     assert(a.get_width() == b.get_height());
00060     assert(b.get_height() == c.get_height());
00061     assert(b.get_width() == c.get_width());
00062 
00063     for (stxxl::unsigned_type y = 0; y < c.get_height(); ++y)
00064         for (stxxl::unsigned_type x = 0; x < c.get_width(); ++x)
00065         {
00066             c.element(x, y) = 0;
00067             for (stxxl::unsigned_type t = 0; t < a.get_width(); ++t)
00068                 c.element(x, y) += a.const_element(t, y) * b.const_element(x, t);
00069         }
00070 }
00071 
00072 int main()
00073 {
00074     const stxxl::unsigned_type width = 416, height = 416;
00075 
00076     try
00077     {
00078         matrix2d<double> a(width, height), b(width, height), c(width, height);
00079 
00080         for (stxxl::unsigned_type y = 0; y < height; ++y)
00081             for (stxxl::unsigned_type x = 0; x < width; ++x)
00082             {
00083                 a.element(x, y) = 1.0;
00084                 b.element(x, y) = 1.0;
00085                 c.element(x, y) = 1.0;
00086             }
00087 
00088         matrix_multiply(a, b, c);
00089 
00090         std::cout << c.const_element(0, 0) << std::endl;
00091         std::cout << c.const_element(width - 1, height - 1) << std::endl;
00092     }
00093     catch (const std::exception & ex)
00094     {
00095         STXXL_MSG("Caught exception: " << ex.what());
00096     }
00097     catch (...)
00098     {
00099         STXXL_MSG("Caught unknown exception.");
00100     }
00101 
00102     return 0;
00103 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines