Guest User

Untitled

a guest
Apr 12th, 2014
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.88 KB | None | 0 0
  1. #define _GNU_SOURCE
  2. #include <stdio.h>
  3. #include <stdint.h>
  4. #include <omp.h>
  5. #include <pthread.h>
  6.  
  7. uint64_t inc(uint64_t i) { return ++i; }
  8.  
  9. uint64_t _inc(uint64_t i) {
  10.   pthread_t thr; uint64_t ret;
  11.   pthread_create(&thr, NULL, inc, i);
  12.   pthread_join(thr, &ret);
  13.   return ret;
  14. }
  15.  
  16. void inc_test(uint64_t count) {
  17.   uint64_t i = 0;  
  18.   while((i = _inc(i)) != count);
  19. }
  20.  
  21. void _run(uint64_t threads, uint64_t count) {
  22.   pthread_t thr[threads];
  23.   uint64_t i = 0;
  24.   do {
  25.     pthread_create(&thr[i], NULL, inc_test, count);
  26.   } while(++i != threads);
  27.   i = 0;  
  28.   do {
  29.     pthread_join(thr[i], NULL);
  30.   } while(++i != threads);
  31. }
  32.  
  33. int main(void) {
  34.   uint64_t i = 0, count = 100*1000, threads = 4;
  35.   double start = omp_get_wtime();
  36.   _run(threads, count);
  37.   double time = omp_get_wtime() - start;
  38.   fprintf(stderr, "%lftp/s\n", (count * threads)/time);
  39.   return 0;
  40. }
Advertisement
Add Comment
Please, Sign In to add comment