Advertisement
Guest User

Untitled

a guest
Jun 19th, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.23 KB | None | 0 0
  1. #include <stdio.h> //comments written in english coz greek is broken
  2. #include <semaphore.h>
  3. #include <pthread.h>
  4. #include <stdlib.h>
  5. #include <math.h>
  6. #include <time.h>
  7.  
  8. struct thread_arg { //struct creation in order two pass multiple parameters within the pthread creation
  9. int *table;
  10. int k;
  11. };
  12.  
  13. void *calculation(void *);
  14.  
  15. pthread_mutex_t sum_mutex = PTHREAD_MUTEX_INITIALIZER;
  16. int maxp, n, p, *sum = 0;
  17.  
  18. int main()
  19. {
  20. int i, j, k=0, check, *table;
  21. clock_t t;
  22. double time_taken;
  23. FILE *f=fopen ("C:\tmp\resultsLS.txt","w");
  24. if (f==NULL){
  25. printf("nError opening file!n");
  26. exit(1);
  27. }
  28.  
  29. printf("Give the max number of pthreads usedn");
  30. scanf("%d", &maxp);
  31. printf("Give the number of elements of the tablen");
  32. do{
  33. printf("The number of elements must be an integral multiple of the number of threadsn");
  34. scanf("%d",&n);
  35. check=n%maxp;
  36. if(check!=0){
  37. printf("Jesus how hard is it?nTry one more timenGive the number of elements of the tablen");
  38. }
  39. }while(check!=0);
  40.  
  41. table = (int*) malloc(n * sizeof(int));
  42. if(table == NULL){
  43. printf("Error! Memory not allocated.n");
  44. exit(0);
  45. }
  46.  
  47. srand(time(NULL));
  48. for (i=0; i<n; i++){
  49. table[i]=(rand()%200);
  50. }
  51.  
  52.  
  53. printf("n--------------------------------n");
  54. printf("nArray elements: ");
  55. for (i=0; i<n; i++) {
  56. printf(" %d ", table[i]);
  57. }
  58.  
  59. for(p=1;p<=maxp;p*2){
  60. fprintf(f, "t#Number of threadst%d", p);
  61. pthread_t threads[p];
  62. printf("n--------------------------------n");
  63.  
  64. struct thread_arg th_args[p]; //table creation to store arguments of each pthread;
  65.  
  66. t=clock();
  67. for(i=0;i<p;i++){ //thread creation
  68. th_args[i].table = table;
  69. th_args[i].k = k;
  70. pthread_create(&threads[i], NULL, calculation, &th_args[i]);
  71. k++; //k is a variable used to seperate table, explained later
  72. }
  73. t=clock()-t;
  74. time_taken=((double)t)*1000/CLOCKS_PER_SEC;
  75. fprintf(f,"nExecution time of function with %d threads: %d msn", i, (int)time_taken);
  76.  
  77. for(i=0;i<p;i++){
  78. pthread_join(threads[i],NULL);
  79. }
  80.  
  81. printf("--------------------------------n");
  82. printf("Sum of vector= %dn", *sum);
  83. *sum=0; //reset of sum
  84. }
  85.  
  86. fclose(f);
  87. free(table);
  88. exit(0);
  89. return 0;
  90. }
  91.  
  92. void *calculation(void *data){
  93. int i;
  94. int local_sum=0;
  95. int *table;
  96. int k;
  97.  
  98. struct thread_arg *th_args = data;
  99. k = th_args->k;
  100. table = th_args->table;
  101.  
  102. printf("--------------------------------n");
  103. for(i=(n/p)*k;i<(n/p)*(k+1);i++) //this algorithm seperates the table into equivelant pieces and
  104. { //every pthread is calculating its given piece, then stores that value in its local variable sum
  105. if((n/p)>n){ //and eventually it is updating the global variable
  106. pthread_exit(NULL);
  107. }
  108. local_sum+=pow(table[i], 2);
  109. printf("Thread's %lu calculation is %dn", pthread_self(), local_sum);
  110. }
  111.  
  112. pthread_mutex_lock(&sum_mutex); //mutex used here to protect the critical code
  113. *sum += local_sum;
  114. pthread_mutex_unlock(&sum_mutex);
  115.  
  116. return NULL;
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement