multi.cpp 2.7 KB
Newer Older
Mario Bajer's avatar
Mario Bajer committed
1
2
3
/** @file multi.cpp
 *  @brief Führt Rechenoperationen zwischen zwei Matrizen durch und speichert diese im Filesystem.
 * */
4

5

Mario Bajer's avatar
Mario Bajer committed
6
7
#include "Matrix.hpp"
#include "notmatrix.hpp"
Mario Bajer's avatar
Mario Bajer committed
8
9
10
#include <thread>
#define MAX_THREADS 4

11
void callMult(Matrix* a, Matrix* b, Matrix c, size_t from, size_t to){     // c auch als ref!
Mario Bajer's avatar
Mario Bajer committed
12
13
    c.threadedmult(a,b,from,to);
}
14
15
16
17
18
19
20
21

int main(){
    Cgicc formData;

    htmlHeader("multi.cgi");
    cout << "<body>\n";
    cout << "<div class=\"container\">" << endl;

Mario Bajer's avatar
Mario Bajer committed
22
23
24
25
    string idA = parseCgiElement(formData, "matrixA");
    string idB = parseCgiElement(formData, "matrixB");
    string operation = parseCgiElement(formData, "rechenop");

Mario Bajer's avatar
Mario Bajer committed
26
27
28
29
    vector<string> matrixA = parseHeader("Json/" + idA);
    vector<string> matrixB = parseHeader("Json/" + idB);

    Matrix a(stoi(matrixA[2]), stoi(matrixA[1]));
Mario Bajer's avatar
Mario Bajer committed
30
    a.getmatrixfromfile("Json/" + idA);
Mario Bajer's avatar
Mario Bajer committed
31
    Matrix b(stoi(matrixB[2]), stoi(matrixB[1]));
Mario Bajer's avatar
Mario Bajer committed
32
    b.getmatrixfromfile("Json/" + idB);
Mario Bajer's avatar
Mario Bajer committed
33
34
35
36
37
//    cout << "<p>Matrix A:</p>" << endl;
//    a.print(cout);
//    cout << "<p>Matrix B:</p>" << endl;
//    b.print(cout);

Mario Bajer's avatar
Mario Bajer committed
38
39
40

    int dimyA = stoi(matrixA[2]);
    int tsize = dimyA / MAX_THREADS;
Mario Bajer's avatar
Mario Bajer committed
41
    thread matrixthread[MAX_THREADS];
Mario Bajer's avatar
Mario Bajer committed
42
43
44

    if((operation.at(0) == '+') && a.checkAddSub(b)) {
        Matrix c = a + b;
Mario Bajer's avatar
Mario Bajer committed
45
46
//        cout << "<p>Matrix C:</p>" << endl;
//        c.print(cout);
Mario Bajer's avatar
Mario Bajer committed
47
48
49
        c.saveMatrix();
    }else if((operation.at(0) == '-') && a.checkAddSub(b)) {
        Matrix c = a - b;
Mario Bajer's avatar
Mario Bajer committed
50
51
//        cout << "<p>Matrix C:</p>" << endl;
//        c.print(cout);
Mario Bajer's avatar
Mario Bajer committed
52
53
        c.saveMatrix();
    }else if((operation.at(0) == '*') && a.checkMult(b)) {
Mario Bajer's avatar
Mario Bajer committed
54
55
        Matrix c(stoi(matrixA[1]),stoi(matrixB[2]));
        auto start = std::chrono::high_resolution_clock::now();
Mario Bajer's avatar
Mario Bajer committed
56
        if(stoi(matrixA[2]) >= (2 * MAX_THREADS)) {
57
            for (size_t i = 0; i < MAX_THREADS; i++) {
58
                matrixthread[i] = thread(callMult, &a, &b, c, (i * tsize), (i * tsize) + tsize - 1);     // c als ref
59
            }
Mario Bajer's avatar
Mario Bajer committed
60
61
            for (size_t i = 0; i < MAX_THREADS; i++) {
                matrixthread[i].join();
62
            }
Mario Bajer's avatar
Mario Bajer committed
63
            if ((tsize % MAX_THREADS) != 0) {
64
                callMult(&a, &b, c, dimyA - (tsize % MAX_THREADS), dimyA);
65
66
            }
        }else{
67
            callMult(&a, &b, c, 0, stoi(matrixA[2]));
Mario Bajer's avatar
Mario Bajer committed
68
69
70
71
72
73
        }
        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);
Mario Bajer's avatar
Mario Bajer committed
74
        c.saveMatrix();
75
76
77
    }

    cout << "</div>" << endl;
Mario Bajer's avatar
Mario Bajer committed
78
    cout << "<a href=\"http://localhost:80/index.html\">Back to Index</a>";
79
80
81
82
    cout << "</body>\n";
    cout << "</html>\n";
    return 0;
}