Commit ddacd2b3 authored by Mario Bajer's avatar Mario Bajer
Browse files

+ multithreading

+ doxy conf
+ gscheites makefile
parent 53c5a6d6
/** @file matrix.cpp
/** @file Matrix.cpp
* @brief Enthält alle Funktionen welche im matrix.hpp beschrieben sind.
* */
......@@ -17,7 +17,7 @@ void Matrix::print(std::ostream& a_r0s){
}
}
void Matrix::fillRND() const{
void Matrix::fillRND() {
std::mt19937 engine(std::random_device{}());
std::uniform_int_distribution<uint64_t> distribution;
for(int i = 0; i < datasize; i++){
......@@ -138,18 +138,39 @@ Matrix Matrix::operator-(const Matrix& b){
Matrix Matrix::operator*(const Matrix& b) {
Matrix c(b.dimx, this->dimy);
for (int i = 0; i < this->dimx; i++)
for (int j = 0; j < b.dimx; j++)
for (int k = 0; k < this->dimy; k++){
if (c(i,j) != ((*this)(i,k) & b(k,j))) {
c.set(i, j);
} else {
c.reset(i, j);
for (int i = 0; i < this->dimx; i++) {
for (int j = 0; j < b.dimx; j++) {
bool helper;
for (int k = 0; k < this->dimy; k++) {
helper = c(i, j) != ((*this)(i, k) & b(k, j));
}
if (helper) {
c.set(i,j);
}else{
c.reset(i,j);
}
}
}
return c;
}
void Matrix::threadedmult(Matrix a, Matrix b, std::size_t from, std::size_t to){
for (size_t i = from; i < to; i++) {
for (int j = 0; j < b.dimx; j++) {
bool helper;
(*this).set(i,j);
for (int k = 0; k < this->dimy; k++) {
helper = (*this)(i, j) != (a(i, k) & b(k, j));
}
if (helper) {
(*this).set(i,j);
}else{
(*this).reset(i,j);
}
}
}
}
void Matrix::set(std::size_t y, std::size_t x){
std::size_t matrixPos = dimx*y + x;
const std::size_t arrayPos = matrixPos / (sizeof(uint64_t)*8);
......
/** @file matrix.hpp
/** @file Matrix.hpp
* @brief Enthält die Klasse Matrix und alle ihre Funktionen
*
* Es kann eine Matrix mit einer quadratischen Dimension erstellt werden.
......@@ -17,7 +17,7 @@
using namespace std;
using namespace cgicc;
using json = nlohmann::json;
using json = nlohmann::json; //json external link
/** @class Matrix
* @brief Klasse Matrix
......@@ -41,7 +41,7 @@ public:
/** @brief Füllt eine Matrix mit gleichverteilten Zufallswerten
* */
void fillRND() const;
void fillRND();
/** @brief Generiert für eine Matrix eine ID auf Basis eines Files welches einen Counter speichert
* */
......@@ -109,6 +109,13 @@ public:
* */
Matrix operator*(const Matrix& b);
/** @brief Multiplikation zweier Matrizen durch Overload des * Operators
*
* @param const Matrix& b
* @return Gibt eine Matrix c, das Ergebnis der Multiplikation zweier Matrizen zurück.
* */
void threadedmult(Matrix a, Matrix b, std::size_t from, std::size_t to);
private:
int ID;
int dimx;
......
This diff is collapsed.
CC=g++
CFLAGS=-Wall -std=c++17
LDLIBS=-lcgicc
all: show generate list delete multi
show:
$(CC) $(CFLAGS) show.cpp Matrix.cpp notmatrix.cpp $(LDLIBS) -o /usr/lib/cgi-bin/show.cgi
generate:
$(CC) $(CFLAGS) generate.cpp Matrix.cpp notmatrix.cpp $(LDLIBS) -o /usr/lib/cgi-bin/generate.cgi
list:
$(CC) $(CFLAGS) list.cpp Matrix.cpp notmatrix.cpp $(LDLIBS) -o /usr/lib/cgi-bin/list.cgi
delete:
$(CC) $(CFLAGS) delete.cpp Matrix.cpp notmatrix.cpp $(LDLIBS) -o /usr/lib/cgi-bin/delete.cgi
multi:
$(CC) $(CFLAGS) multi.cpp Matrix.cpp notmatrix.cpp $(LDLIBS) -o /usr/lib/cgi-bin/multi.cgi
CXX = g++
CFLAGS = -std=c++17 -Wall -Wextra -Werror
LDLIBS = -lcgicc -pthread
CGIPATH = /usr/lib/cgi-bin/
MATRIXLIB = Matrix.o notmatrix.o
INCLUDE = -I external/
all: generate show list delete multi
generate: generate.o $(MATRIXLIB)
$(CXX) -g $< $(MATRIXLIB) -o $(CGIPATH)$@.cgi $(LDLIBS)
show: show.o $(MATRIXLIB)
$(CXX) -g $< $(MATRIXLIB) -o $(CGIPATH)$@.cgi $(LDLIBS)
list: list.o $(MATRIXLIB)
$(CXX) -g $< $(MATRIXLIB) -o $(CGIPATH)$@.cgi $(LDLIBS)
delete: delete.o $(MATRIXLIB)
$(CXX) -g $< $(MATRIXLIB) -o $(CGIPATH)$@.cgi $(LDLIBS)
multi: multi.o $(MATRIXLIB)
$(CXX) -g $< $(MATRIXLIB) -o $(CGIPATH)$@.cgi $(LDLIBS)
%.o: %.cpp
$(CXX) -c $(CFLAGS) $(INCLUDE) -o $@ $<
doxygen:
doxygen doxygen.conf
html:
cp index.html /var/www/html/index.html
-mv /var/www/html/index.html /var/www/html/index.html.old
cp external/index.html /var/www/html/index.html
clean:
rm -f /usr/lib/cgi-bin/*.cgi
rm -f $(CGIPATH)*.cgi
rm -f *.o
rm -rf html/
rm -rf latex/
......@@ -5,6 +5,12 @@
#include "Matrix.hpp"
#include "notmatrix.hpp"
#include <thread>
#define MAX_THREADS 4
void callMult(Matrix a, Matrix b, Matrix c, size_t from, size_t to){
c.threadedmult(a,b,from,to);
}
int main(){
Cgicc formData;
......@@ -21,25 +27,44 @@ int main(){
a.getmatrixfromfile("Json/" + idA);
Matrix b(1, 1);
b.getmatrixfromfile("Json/" + idB);
cout << "<p>Matrix A:</p>" << endl;
a.print(cout);
cout << "<p>Matrix B:</p>" << endl;
b.print(cout);
// cout << "<p>Matrix A:</p>" << endl;
// a.print(cout);
// cout << "<p>Matrix B:</p>" << endl;
// b.print(cout);
vector<string> matrixA = parseHeader("Json/" + idA);
vector<string> matrixB = parseHeader("Json/" + idB);
int tsize = stoi(matrixA[1]) / MAX_THREADS;
// cout << stoi(matrixA[1]) << " und " << stoi(matrixB[2]) << " tsize is " << tsize << endl;
thread matrixthread[MAX_THREADS];
if((operation.at(0) == '+') && a.checkAddSub(b)) {
Matrix c = a + b;
cout << "<p>Matrix C:</p>" << endl;
c.print(cout);
// cout << "<p>Matrix C:</p>" << endl;
// c.print(cout);
c.saveMatrix();
}else if((operation.at(0) == '-') && a.checkAddSub(b)) {
Matrix c = a - b;
cout << "<p>Matrix C:</p>" << endl;
c.print(cout);
// cout << "<p>Matrix C:</p>" << endl;
// c.print(cout);
c.saveMatrix();
}else if((operation.at(0) == '*') && a.checkMult(b)) {
Matrix c = a * b;
cout << "<p>Matrix C:</p>" << endl;
c.print(cout);
Matrix c(stoi(matrixA[1]),stoi(matrixB[2]));
for (size_t i = 0; i < MAX_THREADS; i++){
matrixthread[i] = thread(callMult, a, b, c, (i * tsize), (i*tsize) + tsize);
}
auto start = std::chrono::high_resolution_clock::now();
for (auto & i : matrixthread){
i.join();
}
if (((stoi(matrixA[1]) / MAX_THREADS) % MAX_THREADS) != 0){
callMult(a, b, c, stoi(matrixA[1]) - ((stoi(matrixA[1]) / MAX_THREADS) % MAX_THREADS),stoi(matrixA[1]));
}
auto end = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> elapsed = end - start;
cout << "<p>Multiplication took " << elapsed.count() << " sec</p>" << endl;
// cout << "<p>Matrix C:</p>" << endl;
// c.print(cout);
c.saveMatrix();
}
......
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