Advertisement
Guest User

hw4.c

a guest
Apr 23rd, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.95 KB | None | 0 0
  1. #include <pthread.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <time.h>
  5.  
  6. unsigned long long int sum = 0;
  7. unsigned long size;
  8. char* filename;
  9. int* arr;
  10.  
  11. void *runner(void *params);
  12. void fileReader();
  13. void getFileInfo(int c);
  14.  
  15. pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;
  16.  
  17. struct thread_args { // [lower, upper)
  18. int lower; // Inclusive
  19. int upper; // Exclusive
  20. };
  21.  
  22. void fileReader() {
  23. FILE *fp;
  24. char buffer[30];
  25. arr = (int*)malloc(size * sizeof(int));
  26.  
  27. fp = fopen(filename,"r");
  28. int counter = 0;
  29.  
  30. printf("Reading file\n");
  31. fflush(stdout);
  32. while(fgets(buffer,30,fp)) {
  33. arr[counter] = atoi(buffer);
  34. counter++;
  35. }
  36. printf("Finished reading file\n");
  37. fflush(stdout);
  38. fclose(fp);
  39. }
  40.  
  41. void getFileInfo(int c) {
  42. switch(c) {
  43. case 1:
  44. size = 10000;
  45. filename = "arr1.txt";
  46. break;
  47. case 2:
  48. size = 100000;
  49. filename = "arr2.txt";
  50. break;
  51. case 3:
  52. size = 1000000;
  53. filename = "arr3.txt";
  54. break;
  55. case 4:
  56. size = 10000000l;
  57. filename = "arr4.txt";
  58. break;
  59. case 5:
  60. size = 100000000;
  61. filename = "arr5.txt";
  62. break;
  63. case 6:
  64. size = 1000000000;
  65. filename = "arr6.txt";
  66. break;
  67. default:
  68. size = 10;
  69. filename = "arr.txt";
  70. }
  71. }
  72.  
  73. int main(int argc, char *argv[]) {
  74.  
  75. //Args: [num of threads] [filename]
  76. if(argc != 3) {
  77. fprintf(stderr,"usage: a.out <integer value>\n");
  78. return -1;
  79. }
  80. if(atoi(argv[1]) <0) {
  81. fprintf(stderr,"%d must be >= 0\n", atoi(argv[1]));
  82. return -1;
  83. }
  84. if(atoi(argv[2]) < 1 || atoi(argv[2]) > 6) {
  85. fprintf(stderr,"%d must be > 0 and <= 6\n", atoi(argv[2]));
  86. }
  87. getFileInfo(atoi(argv[2]));
  88. fileReader();
  89.  
  90.  
  91. // Let's create all the threads
  92. const int NUM_OF_THREADS = atoi(argv[1]);
  93. const int CHUNK_SIZE = size / NUM_OF_THREADS;
  94.  
  95. pthread_t tids[NUM_OF_THREADS];
  96. pthread_attr_t attrs[NUM_OF_THREADS];
  97.  
  98. int i;
  99. clock_t t;
  100. t = clock();
  101.  
  102. for(i = 0; i < NUM_OF_THREADS; i++) {
  103. pthread_attr_init(&attrs[i]);
  104.  
  105. // Each thread has a struct as an argument
  106. // The struct holds the lower and upper bounds of the array
  107. struct thread_args *args = malloc(sizeof *args);
  108. args->lower = (i * CHUNK_SIZE);
  109. args->upper = args->lower + CHUNK_SIZE;
  110.  
  111. pthread_create(&tids[i],&attrs[i],runner, args);
  112. }
  113.  
  114. for(i = 0; i < NUM_OF_THREADS; i++) {
  115. pthread_join(tids[i], NULL);
  116. }
  117. t = clock() - t;
  118. double time_taken = ((double)t)/CLOCKS_PER_SEC;
  119.  
  120. printf("sum = %llu\n", sum);
  121.  
  122. float mean = (sum / (float)size);
  123. printf("mean = %f\n", mean);
  124.  
  125. printf("Time taken: %f\n",time_taken);
  126. }
  127.  
  128.  
  129. void *runner(void *param) {
  130. struct thread_args *args = param;
  131.  
  132. int lower = args->lower;
  133. int upper = args->upper;
  134.  
  135. unsigned long long int tempSum = 0;
  136.  
  137. int i;
  138. for(i = lower; i < upper; i++) {
  139. tempSum += arr[i];
  140. }
  141.  
  142. // CRITICAL SECTION
  143. pthread_mutex_lock(&mutex1);
  144. sum += tempSum; // Race condition
  145. pthread_mutex_unlock(&mutex1);
  146. // End of CRITICAL SECTION
  147. pthread_exit(0);
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement