Skip to content

Latest commit

 

History

History
130 lines (89 loc) · 4.3 KB

README.md

File metadata and controls

130 lines (89 loc) · 4.3 KB

Exercise 2

Part 1

  • Riješiti problem sinkronizacije pomoću mutex–a (primjer inkrement).

Solution

Execute test code with:

g++ -std=c++0x main.cpp -o main -lpthread && ./main
  • Multiple threads are modifying same memory adress without any safety lock mechanism which causes unpredictable results.
  • Problem is solved by using mutex

Results

Initial code results (without mutex):

Execution No. counter value expected value
1 38838 50000
2 25749 50000
3 24484 50000
4 37992 50000
5 41798 50000
  • Bug: initial value = 0, thread 1 reads initial value and stores new modified value, simultanuosly second thread also reads the same initial value and stores the new modified value, causing that only result of one thread will be stored, other will be rewriten

Results after adding mutex, modified code can be found here:

Execution No. counter value expected value
1 50000 50000
2 50000 50000
3 50000 50000
4 50000 50000
5 50000 50000

Issues

  • Unable to use #include <semaphore>, because server is using C++14, C++20 is required.

Part 2

  • Napisati program koji ima jednu funkciju vremena izvođenja. (Primjer na portalu), duže trajanje i glavni program koji ima dvije niti koje izvode funkciju sekvencijalno i paralelno. Usporediti!
    • Isto ponoviti s četiri, osam i dvanaest niti.

Solution

Execute test code with:

g++ main2.cpp -o main2 -lpthread && ./main2
  • After creating thread, join thread is executed next in order to achieve sequential execution of threads

Results

4 threads

No. of test Sequential Parallel Diff (seq-par)
1 814 ms 544 ms +270 ms
2 1063 ms 493 ms +570 ms
3 497 ms 530 ms -33 ms
4 791 ms 816 ms -25 ms
5 1103 ms 672 ms +431 ms
6 730 ms 668 ms +62 ms
7 1100 ms 910 ms +190 ms
8 474 ms 524 ms -50 ms
9 1127 ms 645 ms +482 ms
10 1017 ms 517 ms +500 ms

Average diff: +239.7 ms

Parallel was faster than sequential for +239.7 ms at average.

8 threads

No. of test Sequential Parallel Diff (seq-par)
1 1287 ms 965 ms +322 ms
2 1171 ms 1164 ms +7 ms
3 1377 ms 944 ms +433 ms
4 1356 ms 1305 ms +51 ms
5 1887 ms 1381 ms +506 ms
6 1341 ms 911 ms +430 ms
7 1189 ms 880 ms +309 ms
8 1384 ms 1365 ms +19 ms
9 1532 ms 1004 ms +528 ms
10 1515 ms 1138 ms +377 ms

Average diff: +298.2 ms

Parallel was faster than sequential for +298.2 ms at average.

12 threads

No. of test Sequential Parallel Diff (seq-par)
1 2565 ms 1501 ms +1064 ms
2 1976 ms 1356 ms +620 ms
3 1580 ms 1335 ms +245 ms
4 9544 ms 1416 ms +8128 ms
5 2638 ms 1404 ms +1234 ms
6 1462 ms 1577 ms -115 ms
7 1542 ms 1118 ms +424 ms
8 2286 ms 2003 ms +283 ms
9 2373 ms 1216 ms +1157 ms
10 1974 ms 2546 ms -572 ms

Average diff: +1384.2 ms

Parallel was faster than sequential for +1384.2 ms at average.

Conclusion:

  • Parallel execution outperformed sequential, what responds to the expected behaviour.
  • In this case parallel and sequential exection share same overhead of handling threads (create threads, wait for joining), inside task_1 sequntial execution was single threaded and did not suffer from handling thread overhead