Advertisement
Guest User

Untitled

a guest
Mar 26th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.00 KB | None | 0 0
  1. #include <iostream>
  2. #include <windows.h>
  3. #include <fstream>
  4. #include <vector>
  5. #include <time.h>
  6. #include <sstream>
  7. #include <math.h>
  8. using namespace std;
  9.  
  10. struct myData {
  11. double a;
  12. double b;
  13. double eps;
  14. double delta;
  15. };
  16.  
  17.  
  18. int allThreadsTasks = 0;
  19. int noSolutions = 0;
  20. int mystakeTasks = 0;
  21. vector <myData> parametrs;
  22. vector <pair<myData, string>> results;
  23. int TestCount, ThreadCount;
  24. CRITICAL_SECTION cs1;
  25. CRITICAL_SECTION cs2;
  26. CRITICAL_SECTION cs3;
  27. CRITICAL_SECTION cs4;
  28.  
  29. double f(double a, double b, double x) {
  30. return a*cos(x) / (b*x*x);
  31. }
  32. double df(double a, double b, double x) {
  33. return -(cos(x)*a * 2 / (b * x*x*x) + a*sin(x) / (b*x*x));
  34. }
  35. double d2f(double a, double b, double x) {
  36. return a*(-cos(x) / b + 4 * sin(x) / (3 * x) + 2 * cos(x) / (x*x)) / (x*x);
  37. }
  38. bool CheckSolutions(double a, double b, double i1, double i2) {
  39. if (f(a, b, i1)*f(a, b, i2) > 0) {
  40. if (abs(i1 - i2) < 0.001) {
  41. return false;
  42. }
  43. double c = (i1 + i2) / 2;
  44. CheckSolutions(a, b, i1, c);
  45. CheckSolutions(a, b, c, i2);
  46. }
  47. else {
  48. return true;
  49. }
  50. }
  51.  
  52. class Thread {
  53. public:
  54. HANDLE tr;
  55. int ID;
  56. int tasks;
  57. boolean status;
  58. double maxTime;
  59. double minTime;
  60. double allTime;
  61. Thread() {
  62. ID = 0;
  63. tasks = 0;
  64. maxTime = 0;
  65. minTime = 100;
  66. allTime = 0;
  67. status = false;
  68. }
  69.  
  70. int rez(myData data) {
  71. LARGE_INTEGER liC_S, liC_E, liF;
  72. QueryPerformanceCounter(&liC_S);
  73. QueryPerformanceFrequency(&liF);
  74. double i1 = 0, i2 = 100;
  75. double xk = data.delta;
  76. double xk_1 = 0;
  77. string str;
  78. pair <myData, string> p;
  79. p.first = data;
  80. bool flag = true;
  81.  
  82. if (data.b == 0 || data.delta == 0) {
  83. mystakeTasks++;
  84. p.second = "Error in solution";
  85. flag = false;
  86. }
  87.  
  88. if (!CheckSolutions(data.a, data.b, i1, i2) && flag) {
  89. flag = false;
  90. p.second = "No solutions";
  91. noSolutions++;
  92. }
  93. while (abs(xk - xk_1) > data.eps && flag) {
  94. xk_1 = xk;
  95. if (df(data.a, data.b, xk_1) == 0) {
  96. mystakeTasks++;
  97. p.second = "Error in solution";
  98. flag = false;
  99. break;
  100. }
  101. xk = xk_1 - (f(data.a, data.b, xk_1) / df(data.a, data.b, xk_1));
  102. }
  103. if (flag) {
  104. stringstream ss;
  105. ss << xk;
  106. ss >> p.second;
  107. EnterCriticalSection(&cs3);
  108. allThreadsTasks++;
  109. LeaveCriticalSection(&cs3);
  110. tasks++;
  111. }
  112. QueryPerformanceCounter(&liC_E);
  113. double time = ((double)liC_E.QuadPart / (double)liF.QuadPart) - ((double)liC_S.QuadPart / (double)liF.QuadPart);
  114. if (maxTime < time) {
  115. maxTime = time;
  116. }
  117. if (minTime > time) {
  118. minTime = time;
  119. }
  120. allTime += time;
  121. EnterCriticalSection(&cs2);
  122. results.push_back(p);
  123. LeaveCriticalSection(&cs2);
  124. return 0;
  125. }
  126. };
  127.  
  128. vector <Thread> threads;
  129.  
  130. DWORD WINAPI resolve(LPVOID i) {
  131.  
  132. while (true) {
  133. bool flag = false;
  134. myData data;
  135. if (parametrs.size() == 0) {
  136. break;
  137. }
  138. EnterCriticalSection(&cs1);
  139. if (parametrs.size() != 0) {
  140. data = parametrs[0];
  141. parametrs.erase(parametrs.begin());
  142. flag = true;
  143. }
  144. LeaveCriticalSection(&cs1);
  145. if (flag) {
  146. threads[(int)i].rez(data);
  147. }
  148. }
  149. threads[(int)i].status = false;
  150. return 0;
  151. }
  152. bool fl;
  153.  
  154. DWORD WINAPI ch(LPVOID p) {
  155. cout << "1";
  156. while (fl) {
  157. EnterCriticalSection(&cs4);
  158. for (int i = 0; i < ThreadCount; i++) {
  159. cout << threads[i].ID << " " << threads[i].tasks << " ";
  160. if (threads[i].status) {
  161. cout << "работает" << endl;
  162. }
  163. else
  164. cout << "не работает" << endl;
  165. }
  166. LeaveCriticalSection(&cs1);
  167. Sleep(1000);
  168.  
  169. }
  170.  
  171. return 0;
  172. }
  173.  
  174. int main() {
  175.  
  176. srand((unsigned int)time(NULL));
  177. setlocale(LC_ALL, ".1251");
  178. InitializeCriticalSection(&cs1);
  179. InitializeCriticalSection(&cs2);
  180. InitializeCriticalSection(&cs3);
  181. InitializeCriticalSection(&cs4);
  182. //int TestCount, ThreadCount;
  183. cout << "TestCount = ";
  184. cin >> TestCount;
  185. cout << endl << "ThreadCount = ";
  186. cin >> ThreadCount;
  187. cout << endl;
  188.  
  189. DWORD IDThread;
  190.  
  191. for (int i = 0; i < TestCount; i++) {
  192. double a = rand() % 100;
  193. double b = rand() % 100;
  194. double eps = (rand() % 100) / 100000.0;
  195. double delta = (rand() % 10000) / 100;
  196. myData data;
  197. data.a = a;
  198. data.b = b;
  199. data.eps = eps;
  200. data.delta = delta;
  201. parametrs.push_back(data);
  202. }
  203. for (int i = 0; i < ThreadCount; i++) {
  204. Thread a;
  205. threads.push_back(a);
  206. threads[i].ID = i;
  207. }
  208. fl = true;
  209. HANDLE h = new HANDLE;
  210. h = CreateThread(NULL, 0, ch, NULL, 0, &IDThread);
  211. Sleep(1000);
  212. HANDLE *handles = new HANDLE[ThreadCount];
  213. int i = 0;
  214. while (i<ThreadCount) {
  215. handles[i] = threads[i].tr = CreateThread(NULL, 0, resolve, (void*)threads[i].ID, 0, &IDThread);
  216. threads[i].status = true;
  217. if (threads[i].tr == NULL)
  218. return GetLastError();
  219. i++;
  220. }
  221.  
  222. WaitForMultipleObjects(ThreadCount, handles, TRUE, INFINITE);
  223. fl = false;
  224. Sleep(1000);
  225. for (int i = 0; i < ThreadCount; ++i) {
  226. CloseHandle(threads[i].tr);
  227. }
  228. CloseHandle(h);
  229. DeleteCriticalSection(&cs1);
  230. DeleteCriticalSection(&cs2);
  231. DeleteCriticalSection(&cs3);
  232. DeleteCriticalSection(&cs4);
  233. system("pause");
  234. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement