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

fixed broken dimensions

fixed multiplication error for dim > and < MAX_THREAD
parent 8dd40bd0
...@@ -8,9 +8,9 @@ ...@@ -8,9 +8,9 @@
std::mutex m; std::mutex m;
void Matrix::print(std::ostream& a_r0s){ void Matrix::print(std::ostream& a_r0s){
for(int i = 0; i < this->dimy; i++){ for(int i = 0; i < this->dimx; i++){
a_r0s << "<p>"; a_r0s << "<p>";
for(int j = 0; j < this->dimx; j++){ for(int j = 0; j < this->dimy; j++){
a_r0s << (*this)(i,j) << " "; a_r0s << (*this)(i,j) << " ";
} }
a_r0s << "</p>"; a_r0s << "</p>";
...@@ -155,14 +155,21 @@ Matrix Matrix::operator*(const Matrix& b) { ...@@ -155,14 +155,21 @@ Matrix Matrix::operator*(const Matrix& b) {
} }
void Matrix::threadedmult(Matrix a, Matrix b, std::size_t from, std::size_t to){ void Matrix::threadedmult(Matrix a, Matrix b, std::size_t from, std::size_t to){
bool helper1, helper2, helper3, helper4;
for (size_t i = from; i < to; i++) { for (size_t i = from; i < to; i++) {
for (int j = 0; j < b.dimx; j++) { for (int j = 0; j < b.dimx; j++) {
bool helper; for (int k = 0; k < a.dimy; k++) {
(*this).set(i,j); if (k > 0){
for (int k = 0; k < this->dimy; k++) { helper3 = a(i, k) & b(k, j);
helper = (*this)(i, j) != (a(i, k) & b(k, j)); helper4 = helper2;
helper2 = helper4 ^ helper3;
}
helper1 = a(i, k) & b(k, j);
if (k == 0){
helper2 = helper1;
}
} }
if (helper) { if (helper2) {
(*this).set(i,j); (*this).set(i,j);
}else{ }else{
(*this).reset(i,j); (*this).reset(i,j);
...@@ -171,16 +178,16 @@ void Matrix::threadedmult(Matrix a, Matrix b, std::size_t from, std::size_t to){ ...@@ -171,16 +178,16 @@ void Matrix::threadedmult(Matrix a, Matrix b, std::size_t from, std::size_t to){
} }
} }
void Matrix::set(std::size_t y, std::size_t x){ void Matrix::set(std::size_t x, std::size_t y){
std::size_t matrixPos = dimx*y + x; std::size_t matrixPos = dimx*x + y;
const std::size_t arrayPos = matrixPos / (sizeof(uint64_t)*8); const std::size_t arrayPos = matrixPos / (sizeof(uint64_t)*8);
const std::size_t bitPos = matrixPos % (sizeof(uint64_t)*8); const std::size_t bitPos = matrixPos % (sizeof(uint64_t)*8);
const uint64_t pos = 1; const uint64_t pos = 1;
data[arrayPos] |= (pos << bitPos); data[arrayPos] |= (pos << bitPos);
} }
void Matrix::reset(std::size_t y, std::size_t x){ void Matrix::reset(std::size_t x, std::size_t y){
std::size_t matrixPos = dimx*y + x; std::size_t matrixPos = dimx*x + y;
const std::size_t arrayPos = matrixPos / (sizeof(uint64_t)*8); const std::size_t arrayPos = matrixPos / (sizeof(uint64_t)*8);
const std::size_t bitPos = matrixPos % (sizeof(uint64_t)*8); const std::size_t bitPos = matrixPos % (sizeof(uint64_t)*8);
const uint64_t pos = 1; const uint64_t pos = 1;
......
...@@ -55,11 +55,11 @@ public: ...@@ -55,11 +55,11 @@ public:
/** @brief Setzt ein Bit an stelle (y,x) auf 1 /** @brief Setzt ein Bit an stelle (y,x) auf 1
* */ * */
void set(std::size_t y, std::size_t x); void set(std::size_t x, std::size_t y);
/** @brief Setzt ein Bit an stelle (y,x) auf 0 /** @brief Setzt ein Bit an stelle (y,x) auf 0
* */ * */
void reset(std::size_t y, std::size_t x); void reset(std::size_t x, std::size_t y);
/** @brief Prüft ob eine Addition möglich ist. (Dimensionen exakt gleich) /** @brief Prüft ob eine Addition möglich ist. (Dimensionen exakt gleich)
* *
...@@ -78,8 +78,8 @@ public: ...@@ -78,8 +78,8 @@ public:
* @param unsigned x - Spalte der Matrix * @param unsigned x - Spalte der Matrix
* @return gibt den Wert der Matrix an der Stelle zurück * @return gibt den Wert der Matrix an der Stelle zurück
* */ * */
inline bool operator()(unsigned y, unsigned x) const { inline bool operator()(unsigned x, unsigned y) const {
std::size_t matrixPos = dimx*y + x; std::size_t matrixPos = dimx*x + y;
const std::size_t arrayPos = matrixPos / (sizeof(uint64_t)*8); const std::size_t arrayPos = matrixPos / (sizeof(uint64_t)*8);
const std::size_t bitPos = matrixPos % (sizeof(uint64_t)*8); const std::size_t bitPos = matrixPos % (sizeof(uint64_t)*8);
const uint64_t pos = 1; const uint64_t pos = 1;
......
...@@ -28,7 +28,7 @@ int main(){ ...@@ -28,7 +28,7 @@ int main(){
cout << "<p>File " << folderpath << " wurde gelöscht!</p>" << endl; cout << "<p>File " << folderpath << " wurde gelöscht!</p>" << endl;
} }
cout << "<a href=\"http://localhost:80/index.html\">Back to Index</a>"; cout << "<a href=\"http://localhost:80/cgi-bin/list.cgi\">Back</a>";
cout << "</div>" << endl; cout << "</div>" << endl;
cout << "</body>\n"; cout << "</body>\n";
cout << "</html>\n"; cout << "</html>\n";
......
...@@ -17,7 +17,6 @@ int main(){ ...@@ -17,7 +17,6 @@ int main(){
auto fi1 = formData.getElement("dimensionx"); auto fi1 = formData.getElement("dimensionx");
if( !fi1->isEmpty() && fi1 != (*formData).end()) { if( !fi1->isEmpty() && fi1 != (*formData).end()) {
dimx = stoi(**fi1); dimx = stoi(**fi1);
cout << "<p>" << dimx << "</p>" << endl;
} }
else else
{ {
...@@ -26,14 +25,13 @@ int main(){ ...@@ -26,14 +25,13 @@ int main(){
auto fi2 = formData.getElement("dimensiony"); auto fi2 = formData.getElement("dimensiony");
if( !fi2->isEmpty() && fi2 != (*formData).end()) { if( !fi2->isEmpty() && fi2 != (*formData).end()) {
dimy = stoi(**fi2); dimy = stoi(**fi2);
cout << "<p>" << dimy << "</p>" << endl;
} }
else else
{ {
cout << "No valid dimension for X entered!" << endl; cout << "No valid dimension for Y entered!" << endl;
} }
if((dimx != 0) && (dimy != 0)){ if((dimx != 0) && (dimy != 0)){
Matrix matrix(dimx, dimy); Matrix matrix(dimy, dimx);
matrix.fillRND(); matrix.fillRND();
matrix.saveMatrix(); matrix.saveMatrix();
} }
......
...@@ -55,8 +55,8 @@ int main(){ ...@@ -55,8 +55,8 @@ int main(){
vector<string> rs = parseHeader(entry.path()); vector<string> rs = parseHeader(entry.path());
cout << "<tr>" << endl; cout << "<tr>" << endl;
cout << "<td>" << rs[0] << "</td>" << endl; cout << "<td>" << rs[0] << "</td>" << endl;
cout << "<td>" << rs[1] << "</td>" << endl;
cout << "<td>" << rs[2] << "</td>" << endl; cout << "<td>" << rs[2] << "</td>" << endl;
cout << "<td>" << rs[1] << "</td>" << endl;
cout << "<td>" << entry.path() << "</td>" << endl; cout << "<td>" << entry.path() << "</td>" << endl;
cout << R"(<form action="/cgi-bin/show.cgi" method="GET">)" << endl; cout << R"(<form action="/cgi-bin/show.cgi" method="GET">)" << endl;
cout << "<td>" << endl; cout << "<td>" << endl;
......
...@@ -23,19 +23,21 @@ int main(){ ...@@ -23,19 +23,21 @@ int main(){
string idB = parseCgiElement(formData, "matrixB"); string idB = parseCgiElement(formData, "matrixB");
string operation = parseCgiElement(formData, "rechenop"); string operation = parseCgiElement(formData, "rechenop");
Matrix a(1, 1); vector<string> matrixA = parseHeader("Json/" + idA);
vector<string> matrixB = parseHeader("Json/" + idB);
Matrix a(stoi(matrixA[2]), stoi(matrixA[1]));
a.getmatrixfromfile("Json/" + idA); a.getmatrixfromfile("Json/" + idA);
Matrix b(1, 1); Matrix b(stoi(matrixB[2]), stoi(matrixB[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 dimyA = stoi(matrixA[2]);
int tsize = stoi(matrixA[1]) / MAX_THREADS; int tsize = dimyA / MAX_THREADS;
// cout << stoi(matrixA[1]) << " und " << stoi(matrixB[2]) << " tsize is " << tsize << endl;
thread matrixthread[MAX_THREADS]; thread matrixthread[MAX_THREADS];
if((operation.at(0) == '+') && a.checkAddSub(b)) { if((operation.at(0) == '+') && a.checkAddSub(b)) {
...@@ -51,19 +53,18 @@ int main(){ ...@@ -51,19 +53,18 @@ int main(){
}else if((operation.at(0) == '*') && a.checkMult(b)) { }else if((operation.at(0) == '*') && a.checkMult(b)) {
Matrix c(stoi(matrixA[1]),stoi(matrixB[2])); Matrix c(stoi(matrixA[1]),stoi(matrixB[2]));
auto start = std::chrono::high_resolution_clock::now(); auto start = std::chrono::high_resolution_clock::now();
if(stoi(matrixA[1]) >= MAX_THREADS) { if(stoi(matrixA[2]) >= (2 * MAX_THREADS)) {
for (size_t i = 0; i < MAX_THREADS; i++) { for (size_t i = 0; i < MAX_THREADS; i++) {
matrixthread[i] = thread(callMult, a, b, c, (i * tsize), (i * tsize) + tsize - 1); matrixthread[i] = thread(callMult, a, b, c, (i * tsize), (i * tsize) + tsize - 1);
} }
for (auto &i : matrixthread) { for (size_t i = 0; i < MAX_THREADS; i++) {
i.join(); matrixthread[i].join();
} }
if (((stoi(matrixA[1]) / MAX_THREADS) % MAX_THREADS) != 0) { if ((tsize % MAX_THREADS) != 0) {
callMult(a, b, c, stoi(matrixA[1]) - ((stoi(matrixA[1]) / MAX_THREADS) % MAX_THREADS), callMult(a, b, c, dimyA - (tsize % MAX_THREADS), dimyA);
stoi(matrixA[1]));
} }
}else{ }else{
callMult(a, b, c, 0, stoi(matrixA[1])); callMult(a, b, c, 0, stoi(matrixA[2]));
} }
auto end = std::chrono::high_resolution_clock::now(); auto end = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> elapsed = end - start; std::chrono::duration<double> elapsed = end - start;
......
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