Commit 296b2eee authored by Christian Godenschwager's avatar Christian Godenschwager
Browse files

Added c++ files

parent ffd84f21
a.out
\ No newline at end of file
a.out
build
......@@ -16,4 +16,6 @@ endif()
add_executable(hello_world HelloWorld.cpp)
add_executable(sanitizer Sanitizer.cpp)
\ No newline at end of file
add_executable(sanitizer Sanitizer.cpp)
add_executable(matmult MatMult.cpp Matrix.cpp)
\ No newline at end of file
#include "Matrix.h"
#include "Timer.h"
#include <vector>
#include <string>
#include <iostream>
#include <fstream>
int main( int argc, const char * argv[] )
{
std::vector<std::string> args( argv, argv + argc );
std::cout << "Called with arguments: ";
for( const auto & arg : args )
{
std::cout << "\"" << arg << "\" ";
}
std::cout << "\n";
if( args.size() == 4 )
{
std::cout << "Reading lhs matrix...\n";
Matrix m0( readMatrixFromFile( args[1] ) );
std::cout << "Reading rhs matrix...\n";
Matrix m1( readMatrixFromFile( args[2] ) );
Timer timer;
std::cout << "Computing product...\n";
timer.start();
Matrix m2( m0 * m1 );
timer.stop();
std::cout << "Product took " << timer.elapsed() << "ms\n";
std::cout << "Writing result matrix...\n";
writeMatrixToFile( m2, args[3] );
}
else
{
std::cerr << "Usage: " << args[0] << " [IN_MATRIX_FILE IN_MATRIX_FILE OUT_MATRIX_FILE]\n";
}
}
#include "Matrix.h"
#include <algorithm>
#include <sstream>
#include <string>
#include <istream>
#include <fstream>
#include <iterator>
Matrix::Matrix( std::istream & is )
{
is >> m_ >> n_;
values_.resize( m_ * n_ );
std::copy_n( std::istream_iterator<double>(is), m_ * n_, std::begin( values_ ) );
if(!is)
throw std::runtime_error("Error parsing Matrix!");
}
Matrix Matrix::operator+( const Matrix & other ) const
{
Matrix result( *this ); // copy *this into the result
std::transform( result.values_.begin(), result.values_.end(),
other.values_.begin(), result.values_.begin(),
std::plus<double>() );
return result;
}
Matrix Matrix::operator-( const Matrix & other ) const
{
Matrix result( *this ); // copy *this into the result
std::transform( result.values_.begin(), result.values_.end(),
other.values_.begin(), result.values_.begin(),
std::minus<double>() );
return result;
}
Matrix Matrix::operator*( const Matrix & other ) const
{
if( n_ != other.m_ )
throw std::invalid_argument("Matrix multiplication failed due to invalid layout!");
Matrix result( m_, other.n_ );
for( size_t i = 0; i < m_; ++i )
for( size_t j = 0; j < other.n_; ++j )
for( size_t k = 0; k < n_; ++k )
result.get( i, j ) += get( i, k ) * other.get( k, j );
return result;
}
std::ostream & operator<<( std::ostream & os, const Matrix & m )
{
for( size_t i = 0; i < m.rows(); ++i )
{
if( i != 0 )
os << "\n";
for( size_t j = 0; j < m.cols(); ++j )
{
if( j != 0 )
os << " ";
os << m.get( i, j );
}
}
return os;
}
void Matrix::transpose()
{
for( size_t i = 0; i < m_; ++i )
for( size_t j = i + 1; j < n_; ++j )
std::swap( get(i,j), get(j,i) );
}
Matrix readMatrixFromFile( const std::string & filename )
{
std::ifstream ifs( filename );
if(!ifs)
throw std::runtime_error("Error opening file \"" + filename + "\"!" );
return Matrix( ifs );
}
void writeMatrixToFile( const Matrix & m, const std::string & filename )
{
std::ofstream ofs( filename );
if(!ofs)
throw std::runtime_error("Error opening file \"" + filename + "\" for writing!" );
ofs << m.rows() << " " << m.cols() << "\n";
std::copy( std::begin( m.getValues() ), std::end( m.getValues() ), std::ostream_iterator<double>(ofs, "\n") );
if(!ofs)
throw std::runtime_error("Error writing to file \"" + filename + "\"!" );
}
\ No newline at end of file
#pragma once
#include <vector>
#include <istream>
class Matrix
{
public:
Matrix() = default;
Matrix( const size_t m, const size_t n, const double v )
: m_(m), n_(n), values_( m * n, v ) { }
Matrix( const size_t m, const size_t n ) : Matrix( m, n, 0.0 ) { }
explicit Matrix( std::istream & is );
double get( const size_t row, const size_t col ) const { return values_[ row * n_ + col ]; }
double & get( const size_t row, const size_t col ) { return values_[ row * n_ + col ]; }
size_t rows() const { return m_; }
size_t cols() const { return n_; }
Matrix operator+( const Matrix & other ) const;
Matrix operator-( const Matrix & other ) const;
Matrix operator*( const Matrix & other ) const;
inline bool operator==( const Matrix & other ) const;
inline bool operator!=( const Matrix & other ) const;
void transpose();
const std::vector<double> & getValues() const { return values_; }
private:
size_t m_ = 0;
size_t n_ = 0;
std::vector<double> values_;
};
Matrix readMatrixFromFile( const std::string & filename );
void writeMatrixToFile( const Matrix & m, const std::string & filename );
std::ostream & operator<<( std::ostream & os, const Matrix & m );
bool Matrix::operator==( const Matrix & other ) const
{
return m_ == other.m_ && n_ == other.n_ && values_ == other.values_;
}
bool Matrix::operator!=( const Matrix & other ) const
{
return !(*this == other);
}
\ No newline at end of file
#pragma once
#include <chrono>
class Timer
{
public:
Timer() : begin_(), end_( begin_ ) { }
void start()
{
begin_ = std::chrono::high_resolution_clock::now();
end_ = begin_;
}
void stop()
{
end_ = std::chrono::high_resolution_clock::now();
}
double elapsed() const
{
std::chrono::duration<double, std::milli> elapsed = end_ - begin_;
return elapsed.count();
}
private:
std::chrono::high_resolution_clock::time_point begin_;
std::chrono::high_resolution_clock::time_point end_;
};
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment