Advertisement
Guest User

OpenMP memsetter

a guest
Jul 20th, 2012
1,205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.01 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <omp.h>
  5.  
  6. void zero(char *buf, size_t size)
  7. {
  8.     size_t my_start, my_size;
  9.  
  10.     if (omp_in_parallel())
  11.     {
  12.         int id = omp_get_thread_num();
  13.         int num = omp_get_num_threads();
  14.  
  15.         my_start = (id*size)/num;
  16.         my_size = ((id+1)*size)/num - my_start;
  17.     }
  18.     else
  19.     {
  20.         my_start = 0;
  21.         my_size = size;
  22.     }
  23.  
  24.     memset(buf + my_start, 0, my_size);
  25. }
  26.  
  27. int main (void)
  28. {
  29.     char *buf;
  30.     size_t size = 1L << 31; // 2 GiB
  31.     double tmr;
  32.  
  33.     buf = malloc(size);
  34.  
  35.     // Touch
  36.     tmr = -omp_get_wtime();
  37.     #pragma omp parallel
  38.     {
  39.         zero(buf, size);
  40.     }
  41.     tmr += omp_get_wtime();
  42.     printf("Touch:   %.3f MB/s\n", size/(1.e+6*tmr));
  43.  
  44.     // Rewrite
  45.     tmr = -omp_get_wtime();
  46.     #pragma omp parallel
  47.     {
  48.         zero(buf, size);
  49.     }
  50.     tmr += omp_get_wtime();
  51.     printf("Rewrite: %.3f MB/s\n", size/(1.e+6*tmr));
  52.  
  53.     free(buf);
  54.  
  55.     return 0;
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement