Advertisement
Guest User

Untitled

a guest
Mar 26th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.38 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <cmath>
  4. #include <windows.h>
  5. #include <ctime>
  6. #include <vector>
  7. using namespace std;
  8. ofstream fout("out.txt");
  9. HANDLE hMutex;
  10. int stat[10000000][2];
  11. long TaskCount;
  12. long ThreadCount;
  13. long Tasks;
  14. //-------------------------------
  15. struct Matrix {
  16. double matrix[5][2];
  17. bool check;
  18. int doneBy;
  19. int answer;
  20. int solved;
  21. bool newT;
  22. double time;
  23. Matrix(){
  24. check = false;
  25. newT = false;
  26. }
  27. Matrix(double m[5][2]){
  28. check = false;
  29. newT = false;
  30. for(int i = 0;i < 5;i++){
  31. for(int j = 0;j < 2;j++){
  32. matrix[i][j] = m[i][j];
  33. }
  34. }
  35. }
  36. };
  37. struct Arg{
  38. Matrix *matr;
  39. int id;
  40. Arg(Matrix *m){
  41. matr = new Matrix[TaskCount];
  42. matr = m;
  43. }
  44. };
  45. void generator(Matrix *m){
  46. srand(time(NULL));
  47. double matrixTask[5][2];
  48. for(int i = 0;i < TaskCount;i++){
  49. matrixTask[0][0] = rand() % 6;
  50. matrixTask[0][1] = rand() % 6;
  51. matrixTask[1][0] = 5 + rand() % 6;
  52. matrixTask[1][1] = rand() % 3;
  53. matrixTask[2][0] = 5 + rand() % 11;
  54. matrixTask[2][1] = rand() % 11;
  55. matrixTask[3][0] = 5 + rand() % 6;
  56. matrixTask[3][1] = 7 + rand() % 4;
  57. matrixTask[4][0] = rand() % 6;
  58. matrixTask[4][1] = 5 + rand() % 6;
  59. Matrix m1(matrixTask);
  60. m[i] = m1;
  61. }
  62. }
  63. void showMatr(Matrix *m){
  64. for(int i = 0;i < TaskCount;i++){
  65. for(int k = 0;k < 5;k++){
  66. for(int j = 0;j < 2;j++){
  67. cout << m[i].matrix[k][j] << " ";
  68. }
  69. cout << endl;
  70. }
  71. cout << endl << endl;
  72.  
  73. }
  74. }
  75.  
  76. bool hasTasks(Matrix *m){
  77. for(int i = 0;i < TaskCount;i++){
  78. if(m[i].check == false){
  79. return true;
  80. }
  81. }
  82. return false;
  83. }
  84. int checker(double c[5][2]){
  85. double matrix[5][2];
  86. double res[5];
  87. for(int i = 0; i < 5;i++){
  88. if(i == 4){
  89. matrix[i][0] = c[0][0] - c[i][0];
  90. matrix[i][1] = c[0][1] - c[i][1];
  91. }
  92. else{
  93. matrix[i][0] = c[i + 1][0] - c[i][0];
  94. matrix[i][1] = c[i + 1][1] - c[i][1];
  95. }
  96. }
  97. //---------------------
  98. double T = matrix[4][0] * matrix[0][1] - matrix[4][1] * matrix[0][0];
  99. T = T/abs(T);
  100. double P = 1;
  101. for(int i = 0;i < 5;i++){
  102. double R = matrix[i][0] * matrix[i + 1][1] - matrix[i][1] * matrix[i + 1][0];
  103. P = T * P * R/abs(R);
  104. if(P < 0)
  105. return 0;
  106. }
  107. return 1;
  108. }
  109. DWORD WINAPI myThread(void *arg){
  110. int curr = ((Arg*)arg)->id;
  111. int i = Tasks;
  112. while(i < TaskCount){
  113. Sleep(10);
  114.  
  115. if(((Arg *)arg)->matr[i].check == false){
  116. WaitForSingleObject( hMutex, INFINITE );
  117. LARGE_INTEGER l1, l2, l3;
  118. QueryPerformanceCounter(&l1);
  119. QueryPerformanceFrequency(&l3);
  120.  
  121.  
  122. if(checker(((Arg *)arg)->matr[i].matrix) == 1){
  123.  
  124. ((Arg *)arg)->matr[i].check = true;
  125. //-----------------
  126. stat[i][0] = curr;
  127. stat[i][1] = 1;
  128. ((Arg *)arg)->matr[i].answer = 1;
  129. ((Arg *)arg)->matr[i].doneBy = curr;
  130. ((Arg *)arg)->matr[i].solved = 1;
  131. cout << curr << " Solved" << endl;
  132. //-------------------
  133.  
  134. }
  135. else if(((Arg *)arg)->matr[i].check == 0){
  136.  
  137. ((Arg *)arg)->matr[i].check = true;
  138. //-----------------
  139. stat[i][0] = curr;
  140. stat[i][1] = 0;
  141. ((Arg *)arg)->matr[i].answer = 0;
  142. ((Arg *)arg)->matr[i].doneBy = curr;
  143. ((Arg *)arg)->matr[i].solved = 1;
  144. cout << curr << " Solved" << endl;
  145. //-------------------
  146.  
  147. }
  148. QueryPerformanceCounter(&l2);
  149. double totalTime = ((double)l2.QuadPart / (double)l3.QuadPart) - ((double)l1.QuadPart / (double)l3.QuadPart);//итоговое время
  150.  
  151. ((Arg *)arg)->matr[i].time = totalTime;
  152. ReleaseMutex( hMutex );
  153. }
  154.  
  155.  
  156. i++;
  157. Tasks = i;
  158. if(i >= TaskCount && hasTasks(((Arg *)arg)->matr)){
  159. i = 0;
  160. Tasks = i;
  161. }
  162. }
  163. return 0;
  164. }
  165. void solvedN(Matrix *m){
  166. int counter = 0;
  167. for(int i = 0;i < TaskCount;i++){
  168. if(m[i].answer == 0) counter++;
  169. }
  170. cout << "Tasks that havent solutions = " << counter << endl;
  171. }
  172. void threads(Matrix *m){
  173. int totalTasks = 0;
  174. for(int i = 0;i < ThreadCount;i++){
  175. int counter = 0;
  176. double time = 0;
  177. for(int j = 0; j < TaskCount;j++){
  178. if(i == m[j].doneBy){
  179. counter++;
  180. time += m[j].time;
  181. }
  182. }
  183. cout << "Thread with ID = " << i << " solved = " << counter << " tasks. Total time = " << time << endl;
  184. totalTasks += counter;
  185. }
  186. cout << "Total Solved Tasks = " << totalTasks << endl;
  187. }
  188. void minMax(Matrix *m){
  189. double min = 10;
  190. double max = 0;
  191. for(int i = 0; i < TaskCount;i++){
  192. if(m[i].time < min && m[i].time != 0){
  193. min = m[i].time;
  194. }
  195. if(m[i].time > max){
  196. max = m[i].time;
  197. }
  198. }
  199. if(min == 10)
  200. min = max;
  201. cout << "Max time for solving = " << max << endl << "Min time for solving = " << min << endl;
  202. }
  203. DWORD WINAPI myCheck(void *args){
  204. while(true){
  205. for(int i = 0;i < TaskCount;i++){
  206.  
  207. if(((Arg *)args)->matr[i].check == true && ((Arg *)args)->matr[i].newT == false){
  208. cout << ((Arg *)args)->matr[i].doneBy << " Solved task number " << i << endl;
  209. ((Arg *)args)->matr[i].newT = true;
  210. }
  211.  
  212. }
  213. //Sleep(1);
  214.  
  215. }
  216. return 0;
  217. }
  218. int main(){
  219. Tasks = 0;
  220. hMutex=CreateMutex( NULL, FALSE, NULL );
  221. TaskCount = 0;
  222. cout << "Enter Task Count" << endl;
  223. cin >> TaskCount;
  224. Matrix *m = new Matrix[TaskCount];
  225. generator(m);
  226.  
  227. //----------------------------------
  228. cout << "Enter ThreadCount" << endl;
  229. ThreadCount = 0;
  230. cin >> ThreadCount;
  231.  
  232. HANDLE *hThread = new HANDLE[ThreadCount];
  233. DWORD *IDThread = new DWORD[ThreadCount];
  234. Arg args(m);
  235. HANDLE hCheck;
  236. DWORD idCheck;
  237. //hCheck = CreateThread(NULL, 0, myCheck, &args, 0, &idCheck);
  238. for(int i = 0;i < ThreadCount;i++){
  239. args.id = i;
  240. hThread[i] = CreateThread(NULL, 0, myThread, &args, 0, &IDThread[i]);
  241. Sleep(0.0005);
  242. if (hThread[i] == NULL){
  243. cout<<"Thread doesnt exist"<<endl;
  244. return GetLastError();
  245. }
  246. }
  247. if (WaitForMultipleObjects(ThreadCount, hThread, TRUE, INFINITE))
  248. {
  249. cout << "Wait for multiple objects failed." << endl;
  250. }
  251. for(int i = 0;i < ThreadCount;i++){
  252. CloseHandle(hThread[i]);
  253. }
  254. //--------------------------
  255. double writeTimeBeg = clock();
  256. for(int k = 0;k < TaskCount;k++){
  257. fout <<"Answer = " << m[k].answer << endl;
  258. for(int i = 0;i < 5;i++){
  259. for(int j = 0;j < 2;j++){
  260. fout << m[k].matrix[i][j] << " ";
  261. }
  262. fout << endl;
  263. }
  264. fout << "---------------" <<endl;
  265. }
  266. double writeTimeEnd = clock();
  267. //--------------------------
  268.  
  269. //solvedN(args.matr);
  270. //threads(args.matr);
  271. //minMax(args.matr);
  272. cout << "Time for writting data = " << ((writeTimeEnd - writeTimeBeg) / CLOCKS_PER_SEC) << endl;
  273. system("pause");
  274. return 0;
  275. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement