Advertisement
tftrgi11

Untitled

Jun 19th, 2020
1,924
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.50 KB | None | 0 0
  1. #include <stdint.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <getopt.h>
  5. #include <stdbool.h>
  6. #include <sys/time.h>
  7.  
  8. #include <pthread.h>
  9. #include "utils.h"
  10.  
  11. struct SumArgs {
  12.   int *array;
  13.   int begin;
  14.   int end;
  15. };
  16.  
  17. int count = 0;
  18.  
  19. int Sum(const struct SumArgs *args) {
  20.   int sum = 0;
  21.   int *ptr = args->array;
  22.   int bg = args[count].begin;
  23.   int ed = args[count].end;
  24.   count++;
  25.  
  26.   while (bg != ed)
  27.   {
  28.       sum += ptr[bg];
  29.       bg++;
  30.   }
  31.   return sum;
  32. }
  33.  
  34. void *ThreadSum(void *args) {
  35.   struct SumArgs *sum_args = (struct SumArgs *)args;
  36.   return (void *)(size_t)Sum(sum_args);
  37. }
  38.  
  39. int main(int argc, char **argv) {
  40.   uint32_t threads_num = 0;
  41.   uint32_t array_size = 0;
  42.   uint32_t seed = 0;
  43.   pthread_t threads[threads_num];
  44.  
  45.   while (true) {
  46.     int current_optind = optind ? optind : 1;
  47.  
  48.     static struct option options[] = {{"threads_num", required_argument, 0, 0},
  49.                                       {"seed", required_argument, 0, 0},
  50.                                       {"array_size", required_argument, 0, 0},
  51.                                       {0, 0, 0, 0}};
  52.  
  53.     int option_index = 0;
  54.     int c = getopt_long(argc, argv, "f", options, &option_index);
  55.  
  56.     if (c == -1) break;
  57.  
  58.     switch (c) {
  59.       case 0:
  60.         switch (option_index) {
  61.           case 0:
  62.             threads_num = atoi(optarg);
  63.             if (threads_num <= 0)
  64.             {
  65.                 printf("Threads number must be positive\n");
  66.                 return 1;
  67.             }
  68.             break;
  69.           case 1:
  70.             seed = atoi(optarg);
  71.             if (seed <= 0)
  72.             {
  73.                 printf("Seed must be positive\n");
  74.                 return 1;
  75.             }
  76.             break;
  77.           case 2:
  78.             array_size = atoi(optarg);
  79.             if (array_size <= 0)
  80.             {
  81.                 printf("Array size must be positive\n");
  82.                 return 1;
  83.             }
  84.             break;
  85.           defalut:
  86.             printf("Index %d is out of options\n", option_index);
  87.             break;
  88.         }
  89.         break;
  90.  
  91.       case '?':
  92.         break;
  93.  
  94.       default:
  95.         printf("getopt returned character code 0%o?\n", c);
  96.         break;
  97.     }
  98.   }
  99.  
  100. if (optind < argc) {
  101.     printf("Has at least one no option argument\n");
  102.     return 1;
  103.   }
  104.  
  105. if (seed == 0 || array_size == 0 || threads_num == 0) {
  106.     printf("Usage: %s --threads_num \"num\" --seed \"num\" --array_size \"num\" \n",
  107.            argv[0]);
  108.     return 1;
  109. }
  110.  
  111.  
  112.   int *array = malloc(sizeof(int) * array_size);
  113.   struct SumArgs args[threads_num];
  114.  
  115.   GenerateArray(array, array_size, seed);
  116.  
  117.   struct timeval start_time;
  118.   gettimeofday(&start_time, NULL);
  119.  
  120.   for (int i = 0; i < threads_num; i++)
  121.   {
  122.       args[i].array = array;
  123.       args[i].begin = i*array_size/threads_num;
  124.       args[i].end = (i+1)*array_size/threads_num;
  125.   }
  126.  
  127.   for (uint32_t i = 0; i < threads_num; i++) {
  128.     if (pthread_create(&threads[i], NULL, ThreadSum, (void *)&args)) {
  129.       printf("Error: pthread_create failed!\n");
  130.       return 1;
  131.     }
  132.   }
  133.  
  134.   int total_sum = 0;
  135.   for (uint32_t i = 0; i < threads_num; i++) {
  136.     int sum = 0;
  137.     pthread_join(threads[i], (void **)&sum);
  138.     total_sum += sum;
  139.   }
  140.  
  141.   struct timeval finish_time;
  142.   gettimeofday(&finish_time, NULL);
  143.   double elapsed_time = (double)(finish_time.tv_usec - start_time.tv_usec)/1000;
  144.  
  145.   free(array);
  146.   printf("Total: %d\n", total_sum);
  147.   printf("Elapsed time: %fms\n", elapsed_time);
  148.   return 0;
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement