Advertisement
Guest User

Untitled

a guest
May 4th, 2015
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.84 KB | None | 0 0
  1. /*
  2. ---------------------------------------------------------------------------------
  3. -- Лабораторна робота №2 з дисципліни ПП-2
  4. -- Lab2. Win32
  5. -- ПІБ Сочка Олександр Олександрович
  6. -- Група ІП-22
  7. -- Дата 26.02.2015р
  8. -- A = max(MO) * MK * MT * a + b * MK * MR
  9. ---------------------------------------------------------------------------------
  10. */
  11. #define NOMINMAX
  12. #include <Windows.h>
  13. #include <iostream>
  14. #include <vector>
  15. #include <algorithm>
  16. #include <climits>
  17.  
  18. using namespace std;
  19.  
  20. typedef std::vector<int> Vector;
  21. typedef std::vector<Vector> Matrix;
  22.  
  23. const int N = 4;
  24. const int P = 4;
  25.  
  26. Matrix MA(N, Vector(N)), MO, MK, MT, MR;
  27. int alpha, beta;
  28.  
  29. HANDLE ev_io1; // IO is finished in t1
  30. HANDLE ev_io3; // IO is finished in t3
  31. HANDLE ev_io4; // IO is finished in t4
  32. HANDLE Sem1_1;
  33. HANDLE Sem1_2;
  34. HANDLE Sem1_3;
  35. HANDLE Sem1_4;
  36. HANDLE Sem2_1;
  37. HANDLE Sem2_2;
  38. HANDLE Sem2_3;
  39. CRITICAL_SECTION CS1; // copy1 & copy2
  40.  
  41. int t = INT_MIN;
  42.  
  43. void read_matrix(Matrix& m) {
  44. m = Matrix(N, Vector(N, 1));
  45. }
  46.  
  47. void read_num(int& value) {
  48. value = 1;
  49. }
  50.  
  51. void F1() {
  52. cout << "T1 started" << endl;
  53. read_matrix(MO);
  54. read_matrix(MK);
  55.  
  56. SetEvent(ev_io1);
  57. WaitForSingleObject(ev_io3, INFINITE);
  58. WaitForSingleObject(ev_io4, INFINITE);
  59.  
  60. EnterCriticalSection(&CS1);
  61. int alpha_c = alpha, beta_c = beta;
  62. cout << "T1 copied shared variables" << endl;
  63. LeaveCriticalSection(&CS1);
  64.  
  65. int t1 = INT_MIN;
  66. for (int r = 0; r < N / 4; ++r) {
  67. t1 = max(t1, *max_element(MO[r].begin(), MO[r].end()));
  68. }
  69.  
  70. t = max(t,t1);
  71. ReleaseSemaphore(Sem1_1, 1, NULL);
  72. WaitForSingleObject(Sem1_2, INFINITE);
  73. ReleaseSemaphore(Sem1_2, 1, NULL);
  74. WaitForSingleObject(Sem1_3, INFINITE);
  75. ReleaseSemaphore(Sem1_3, 1, NULL);
  76. WaitForSingleObject(Sem1_4, INFINITE);
  77. ReleaseSemaphore(Sem1_4, 1, NULL);
  78.  
  79. // main calculation
  80. for (int i = 0; i < N / 2; ++i) {
  81. for (int j = 0; j < N; ++j) {
  82. for (int k = 0; k < N / 2; ++k) {
  83. MA[i][k] += t * MK[i][j] * MT[j][k] * alpha_c + beta_c * MK[i][j] * MR[j][k];
  84. }
  85. }
  86. }
  87.  
  88. ReleaseSemaphore(Sem2_1, 1, NULL);
  89. // end of main calculation
  90. cout << "T1 finished" << endl;
  91. }
  92.  
  93. void F2() {
  94. cout << "T2 started" << endl;
  95.  
  96. WaitForSingleObject(ev_io1, INFINITE);
  97. WaitForSingleObject(ev_io3, INFINITE);
  98. WaitForSingleObject(ev_io4, INFINITE);
  99.  
  100. EnterCriticalSection(&CS1);
  101. int alpha_c = alpha, beta_c = beta;
  102. cout << "T2 copied shared variables" << endl;
  103. LeaveCriticalSection(&CS1);
  104.  
  105. int t2 = INT_MIN;
  106. for (int r = N / 4; r < N / 2; ++r) {
  107. t2 = max(t2, *max_element(MO[r].begin(), MO[r].end()));
  108. }
  109.  
  110. t = max(t,t2);
  111.  
  112. ReleaseSemaphore(Sem1_2, 1, NULL);
  113. WaitForSingleObject(Sem1_1, INFINITE);
  114. ReleaseSemaphore(Sem1_1, 1, NULL);
  115. WaitForSingleObject(Sem1_3, INFINITE);
  116. ReleaseSemaphore(Sem1_3, 1, NULL);
  117. WaitForSingleObject(Sem1_4, INFINITE);
  118. ReleaseSemaphore(Sem1_4, 1, NULL);
  119.  
  120.  
  121. // main calculation
  122. for (int i = N / 2; i < N; ++i) {
  123. for (int j = 0; j < N; ++j) {
  124. for (int k = 0; k < N / 2; ++k) {
  125. MA[i][k] += t * MK[i][j] * MT[j][k] * alpha_c + beta_c * MK[i][j] * MR[j][k];
  126. }
  127. }
  128. }
  129.  
  130. // end of main calculation
  131. ReleaseSemaphore(Sem2_2, 1, NULL);
  132. cout << "T2 finished" << endl;
  133. }
  134.  
  135. void F3() {
  136. cout << "T3 started" << endl;
  137. read_matrix(MT);
  138. read_num(alpha);
  139.  
  140. SetEvent(ev_io3);
  141.  
  142. // cout << "T3 waits for other threads input..." << endl;
  143. WaitForSingleObject(ev_io1, INFINITE);
  144. WaitForSingleObject(ev_io4, INFINITE);
  145.  
  146. // cout << "T3 enters critical section for copying globals" << endl;
  147.  
  148. EnterCriticalSection(&CS1);
  149. int alpha_c = alpha, beta_c = beta;
  150. cout << "T3 copied shared variables" << endl;
  151. LeaveCriticalSection(&CS1);
  152.  
  153. int t3 = INT_MIN;
  154. for (int r = N / 2; r < N / 4 * 3; ++r) {
  155. t3 = max(t3, *max_element(MO[r].begin(), MO[r].end()));
  156. }
  157. t = max(t,t3);
  158.  
  159. ReleaseSemaphore(Sem1_3, 1, NULL);
  160. WaitForSingleObject(Sem1_2, INFINITE);
  161. ReleaseSemaphore(Sem1_2, 1, NULL);
  162. WaitForSingleObject(Sem1_1, INFINITE);
  163. ReleaseSemaphore(Sem1_1, 1, NULL);
  164. WaitForSingleObject(Sem1_4, INFINITE);
  165. ReleaseSemaphore(Sem1_4, 1, NULL);
  166.  
  167. // main calculation
  168. for (int i = 0; i < N / 2; ++i) {
  169. for (int j = 0; j < N; ++j) {
  170. for (int k = N / 2; k < N; ++k) {
  171. MA[i][k] += t * MK[i][j] * MT[j][k] * alpha_c + beta_c * MK[i][j] * MR[j][k];
  172. }
  173. }
  174. }
  175.  
  176. // end of main calculation
  177.  
  178. ReleaseSemaphore(Sem2_3, 1, NULL);
  179. cout << "T3 finished" << endl;
  180. }
  181.  
  182. void F4() {
  183. cout << "T4 started" << endl;
  184. read_matrix(MR);
  185. read_num(beta);
  186.  
  187. SetEvent(ev_io4);
  188. WaitForSingleObject(ev_io1, INFINITE);
  189. WaitForSingleObject(ev_io3, INFINITE);
  190.  
  191. EnterCriticalSection(&CS1);
  192. int alpha_c = alpha, beta_c = beta;
  193. cout << "T4 copied shared variables" << endl;
  194. LeaveCriticalSection(&CS1);
  195.  
  196. int t4 = INT_MIN;
  197. for (int r = N / 4 * 3; r < N; ++r) {
  198. t4 = max(t4, *max_element(MO[r].begin(), MO[r].end()));
  199. }
  200. t = max(t,t4);
  201.  
  202. ReleaseSemaphore(Sem1_4, 1, NULL);
  203. WaitForSingleObject(Sem1_2, INFINITE);
  204. ReleaseSemaphore(Sem1_2, 1, NULL);
  205. WaitForSingleObject(Sem1_3, INFINITE);
  206. ReleaseSemaphore(Sem1_3, 1, NULL);
  207. WaitForSingleObject(Sem1_1, INFINITE);
  208. ReleaseSemaphore(Sem1_1, 1, NULL);
  209.  
  210. // main calculation
  211. for (int i = N / 2; i < N; ++i) {
  212. for (int j = 0; j < N; ++j) {
  213. for (int k = N / 2; k < N; ++k) {
  214. MA[i][k] += t * MK[i][j] * MT[j][k] * alpha_c + beta_c * MK[i][j] * MR[j][k];
  215. }
  216. }
  217. }
  218.  
  219. // end of main calculation
  220.  
  221. WaitForSingleObject(Sem2_1, INFINITE);
  222. WaitForSingleObject(Sem2_2, INFINITE);
  223. WaitForSingleObject(Sem2_3, INFINITE);
  224.  
  225. if (N <= 10) {
  226. for (int i = 0; i < N; ++i) {
  227. for (int j = 0; j < N; ++j) {
  228. cout << MA[i][j] << ' ';
  229. }
  230. cout << endl;
  231. }
  232. }
  233.  
  234. cout << "T4 finished" << endl;
  235. }
  236.  
  237. int main() {
  238.  
  239. InitializeCriticalSection(&CS1);
  240. ev_io1 = CreateEvent(NULL, TRUE, FALSE, NULL);
  241. ev_io3 = CreateEvent(NULL, TRUE, FALSE, NULL);
  242. ev_io4 = CreateEvent(NULL, TRUE, FALSE, NULL);
  243.  
  244. Sem1_1 = CreateSemaphore(NULL, 0, 1, NULL);
  245. Sem1_2 = CreateSemaphore(NULL, 0, 1, NULL);
  246. Sem1_3 = CreateSemaphore(NULL, 0, 1, NULL);
  247. Sem1_4 = CreateSemaphore(NULL, 0, 1, NULL);
  248. Sem2_1 = CreateSemaphore(NULL, 0, 1, NULL);
  249. Sem2_2 = CreateSemaphore(NULL, 0, 1, NULL);
  250. Sem2_3 = CreateSemaphore(NULL, 0, 1, NULL);
  251.  
  252. DWORD thread_id;
  253. HANDLE T1 = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)F1, NULL, 0, &thread_id);
  254. HANDLE T2 = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)F2, NULL, 0, &thread_id);
  255. HANDLE T3 = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)F3, NULL, 0, &thread_id);
  256. HANDLE T4 = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)F4, NULL, 0, &thread_id);
  257.  
  258. WaitForSingleObject(T1, INFINITE);
  259. WaitForSingleObject(T2, INFINITE);
  260. WaitForSingleObject(T3, INFINITE);
  261. WaitForSingleObject(T4, INFINITE);
  262. CloseHandle(T1);
  263. CloseHandle(T2);
  264. CloseHandle(T3);
  265. CloseHandle(T4);
  266. CloseHandle(ev_io1);
  267. CloseHandle(ev_io3);
  268. CloseHandle(ev_io4);
  269. CloseHandle(Sem1_1);
  270. CloseHandle(Sem1_2);
  271. CloseHandle(Sem1_3);
  272. CloseHandle(Sem1_4);
  273. CloseHandle(Sem2_1);
  274. CloseHandle(Sem2_2);
  275. CloseHandle(Sem2_3);
  276. DeleteCriticalSection(&CS1);
  277. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement