Advertisement
Guest User

aa

a guest
Jan 19th, 2020
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.63 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <pthread.h>
  4. #include <time.h>
  5.  
  6. float sum = 0;
  7. int n;
  8. int w;
  9. float *random_numbers;
  10.  
  11. pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
  12.  
  13. void* array_sum(void* arg)
  14. {
  15. int start_index = *(int *) arg;
  16. float tmp_sum = 0;
  17. printf("Thread #%ld size=%d\n", pthread_self(), (n/w));
  18. for (int i = 0; i < n/w; i++)
  19. tmp_sum += random_numbers[start_index+i];
  20.  
  21. pthread_mutex_lock(&mutex);
  22. sum += tmp_sum;
  23. pthread_mutex_unlock(&mutex);
  24.  
  25. printf("Thread #%ld sum=%f\n", pthread_self(), tmp_sum);
  26. pthread_exit(NULL);
  27. }
  28.  
  29. void* array_sum2(void* arg)
  30. {
  31. int start_index = *(int *) arg;
  32. float tmp_sum = 0;
  33. printf("Thread #%ld size=%d\n", pthread_self(), n - (w-1) * (n/w));
  34. for (int i = 0; i < n - (w-1) * (n/w); i++)
  35. tmp_sum += random_numbers[start_index+i];
  36.  
  37. pthread_mutex_lock(&mutex);
  38. sum += tmp_sum;
  39. pthread_mutex_unlock(&mutex);
  40.  
  41. printf("Thread #%ld sum=%f\n", pthread_self(), tmp_sum);
  42. pthread_exit(NULL);
  43. }
  44.  
  45. int main(int argc, char **argv)
  46. {
  47. if (argc != 3)
  48. {
  49. printf("Nalezy podac dokladnie 2 argumenty do wywolania tego programu!\n");
  50. exit(-1);
  51. }
  52. if (atoi(argv[1]) >= 1000000)
  53. {
  54. printf("Za duze n!\n");
  55. exit(-2);
  56. }
  57. if (atoi(argv[2]) < 0 || atoi(argv[2]) > atoi(argv[1]))
  58. {
  59. printf("Niepoprawne w!\n");
  60. exit(-3);
  61. }
  62.  
  63. n = atoi(argv[1]);
  64. w = atoi(argv[2]);
  65. random_numbers = malloc(n * sizeof(*random_numbers));
  66.  
  67. /* Generate random numbers */
  68. srand(0);
  69. for (int i = 0; i < n; i++)
  70. random_numbers[i] = 1000. * rand() / RAND_MAX;
  71.  
  72. /* Args to functions*/
  73. int arguments[w];
  74. arguments[0] = 0;
  75. for (int i = 1; i < w - 1; i++)
  76. arguments[i] = i*(n/w);
  77. arguments[w-1] = n - (n - (w-1) * (n/w));
  78. /* Threads version */
  79. pthread_t threads_ids[w];
  80. clock_t start = clock();
  81. for (int i = 0; i < w-1; i++)
  82. {
  83. pthread_create(&threads_ids[i], NULL, array_sum, &arguments[i]);
  84. }
  85. pthread_create(&threads_ids[w-1], NULL, array_sum2, &arguments[w-1]);
  86.  
  87. for (int i = 0; i < w; i++)
  88. pthread_join(threads_ids[i], NULL);
  89. clock_t end = clock();
  90. printf("w/Threads: sum = %f, time = %f sec \n", sum, ((double) (end - start) / CLOCKS_PER_SEC));
  91.  
  92.  
  93. /* Without threads version */
  94. sum = 0;
  95. start = clock();
  96. for (int i = 0; i < n; i++)
  97. sum += random_numbers[i];
  98. end = clock();
  99. printf("wo/Threads: sum = %f, time = %f sec \n", sum, ((double) (end - start) / CLOCKS_PER_SEC));
  100.  
  101.  
  102. free(random_numbers);
  103. return 0;
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement