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. * @brief Enthält alle Funktionen welche im matrix.hpp beschrieben sind.
* */ * */
...@@ -17,7 +17,7 @@ void Matrix::print(std::ostream& a_r0s){ ...@@ -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::mt19937 engine(std::random_device{}());
std::uniform_int_distribution<uint64_t> distribution; std::uniform_int_distribution<uint64_t> distribution;
for(int i = 0; i < datasize; i++){ for(int i = 0; i < datasize; i++){
...@@ -138,18 +138,39 @@ Matrix Matrix::operator-(const Matrix& b){ ...@@ -138,18 +138,39 @@ Matrix Matrix::operator-(const Matrix& b){
Matrix Matrix::operator*(const Matrix& b) { Matrix Matrix::operator*(const Matrix& b) {
Matrix c(b.dimx, this->dimy); Matrix c(b.dimx, this->dimy);
for (int i = 0; i < this->dimx; i++) for (int i = 0; i < this->dimx; i++) {
for (int j = 0; j < b.dimx; j++) for (int j = 0; j < b.dimx; j++) {
for (int k = 0; k < this->dimy; k++){ bool helper;
if (c(i,j) != ((*this)(i,k) & b(k,j))) { for (int k = 0; k < this->dimy; k++) {
c.set(i, j); helper = c(i, j) != ((*this)(i, k) & b(k, j));
} else {
c.reset(i, j);
}
} }
if (helper) {
c.set(i,j);
}else{
c.reset(i,j);
}
}
}
return c; 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){ void Matrix::set(std::size_t y, std::size_t x){
std::size_t matrixPos = dimx*y + x; std::size_t matrixPos = dimx*y + x;
const std::size_t arrayPos = matrixPos / (sizeof(uint64_t)*8); 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 * @brief Enthält die Klasse Matrix und alle ihre Funktionen
* *
* Es kann eine Matrix mit einer quadratischen Dimension erstellt werden. * Es kann eine Matrix mit einer quadratischen Dimension erstellt werden.
...@@ -17,7 +17,7 @@ ...@@ -17,7 +17,7 @@
using namespace std; using namespace std;
using namespace cgicc; using namespace cgicc;
using json = nlohmann::json; using json = nlohmann::json; //json external link
/** @class Matrix /** @class Matrix
* @brief Klasse Matrix * @brief Klasse Matrix
...@@ -41,7 +41,7 @@ public: ...@@ -41,7 +41,7 @@ public:
/** @brief Füllt eine Matrix mit gleichverteilten Zufallswerten /** @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 /** @brief Generiert für eine Matrix eine ID auf Basis eines Files welches einen Counter speichert
* */ * */
...@@ -109,6 +109,13 @@ public: ...@@ -109,6 +109,13 @@ public:
* */ * */
Matrix operator*(const Matrix& b); 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: private:
int ID; int ID;
int dimx; int dimx;
......
This diff is collapsed.
CC=g++ CXX = g++
CFLAGS=-Wall -std=c++17 CFLAGS = -std=c++17 -Wall -Wextra -Werror
LDLIBS=-lcgicc LDLIBS = -lcgicc -pthread
CGIPATH = /usr/lib/cgi-bin/
all: show generate list delete multi MATRIXLIB = Matrix.o notmatrix.o
INCLUDE = -I external/
show:
$(CC) $(CFLAGS) show.cpp Matrix.cpp notmatrix.cpp $(LDLIBS) -o /usr/lib/cgi-bin/show.cgi all: generate show list delete multi
generate:
$(CC) $(CFLAGS) generate.cpp Matrix.cpp notmatrix.cpp $(LDLIBS) -o /usr/lib/cgi-bin/generate.cgi generate: generate.o $(MATRIXLIB)
list: $(CXX) -g $< $(MATRIXLIB) -o $(CGIPATH)$@.cgi $(LDLIBS)
$(CC) $(CFLAGS) list.cpp Matrix.cpp notmatrix.cpp $(LDLIBS) -o /usr/lib/cgi-bin/list.cgi
delete: show: show.o $(MATRIXLIB)
$(CC) $(CFLAGS) delete.cpp Matrix.cpp notmatrix.cpp $(LDLIBS) -o /usr/lib/cgi-bin/delete.cgi $(CXX) -g $< $(MATRIXLIB) -o $(CGIPATH)$@.cgi $(LDLIBS)
multi:
$(CC) $(CFLAGS) multi.cpp Matrix.cpp notmatrix.cpp $(LDLIBS) -o /usr/lib/cgi-bin/multi.cgi 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: 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: 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 @@ ...@@ -5,6 +5,12 @@
#include "Matrix.hpp" #include "Matrix.hpp"
#include "notmatrix.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(){ int main(){
Cgicc formData; Cgicc formData;
...@@ -21,25 +27,44 @@ int main(){ ...@@ -21,25 +27,44 @@ int main(){
a.getmatrixfromfile("Json/" + idA); a.getmatrixfromfile("Json/" + idA);
Matrix b(1, 1); Matrix b(1, 1);
b.getmatrixfromfile("Json/" + idB); b.getmatrixfromfile("Json/" + idB);
cout << "<p>Matrix A:</p>" << endl; // cout << "<p>Matrix A:</p>" << endl;
a.print(cout); // a.print(cout);
cout << "<p>Matrix B:</p>" << endl; // cout << "<p>Matrix B:</p>" << endl;
b.print(cout); // 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)) { if((operation.at(0) == '+') && a.checkAddSub(b)) {
Matrix c = a + b; Matrix c = a + b;
cout << "<p>Matrix C:</p>" << endl; // cout << "<p>Matrix C:</p>" << endl;
c.print(cout); // c.print(cout);
c.saveMatrix(); c.saveMatrix();
}else if((operation.at(0) == '-') && a.checkAddSub(b)) { }else if((operation.at(0) == '-') && a.checkAddSub(b)) {
Matrix c = a - b; Matrix c = a - b;
cout << "<p>Matrix C:</p>" << endl; // cout << "<p>Matrix C:</p>" << endl;
c.print(cout); // c.print(cout);
c.saveMatrix(); c.saveMatrix();
}else if((operation.at(0) == '*') && a.checkMult(b)) { }else if((operation.at(0) == '*') && a.checkMult(b)) {
Matrix c = a * b; Matrix c(stoi(matrixA[1]),stoi(matrixB[2]));
cout << "<p>Matrix C:</p>" << endl; for (size_t i = 0; i < MAX_THREADS; i++){
c.print(cout); 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(); 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