Guest User

testing xeon phi openmp

a guest
Mar 27th, 2015
397
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.77 KB | None | 0 0
  1. //
  2. // helloflops1
  3. //
  4. // A simple example to try
  5. // to get lots of Flops on
  6. // Intel(r) Xeon Phi(tm) co-processors.
  7. //
  8.  
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <sys/time.h>
  13. #include <omp.h>
  14.  
  15. // dtime -
  16. //
  17. // utility routine to return
  18. // the current wall clock time
  19. //
  20. double dtime()
  21. {
  22.     double tseconds = 0.0;
  23.     struct timeval mytime;
  24.     gettimeofday(&mytime,(struct timezone*)0);
  25.     tseconds = (double)(mytime.tv_sec + mytime.tv_usec*1.0e-6);
  26.     return( tseconds );
  27. }
  28.  
  29.  
  30. #define FLOPS_ARRAY_SIZE (1024*1024)
  31. #define MAXFLOPS_ITERS 100000000
  32. #define LOOP_COUNT 128
  33. // Floating pt ops per inner loop iteration
  34. #define FLOPSPERCALC 2    
  35. // define some arrays - 64 byte aligned for fastest cache access
  36. float fa[FLOPS_ARRAY_SIZE] __attribute__((aligned(64)));
  37. float fb[FLOPS_ARRAY_SIZE] __attribute__((aligned(64)));
  38.  
  39. //
  40. // Main program - pedal to the metal...calculate tons o'flops!
  41. //
  42. int main(int argc, char *argv[] )
  43. {
  44.  
  45.         int i,j,k;
  46.         double tstart, tstop, ttime;
  47.     double gflops = 0.0;
  48.     float a=1.1;
  49.     int numthreads;
  50.  
  51.        
  52.         //
  53.         // initialize the compute arrays
  54.         //
  55.         omp_set_dynamic( 0 );
  56.         omp_set_num_threads(2);
  57.         kmp_set_defaults("KMP_AFFINITY=compact");
  58.        
  59. #pragma omp parallel
  60. #pragma omp master
  61.     numthreads = omp_get_num_threads();
  62.    
  63.         printf("Initializing\r\n");
  64.        
  65.        
  66.         #pragma omp parallel for
  67.         for(i=0; i<FLOPS_ARRAY_SIZE; i++)
  68.         {
  69.             if ( i == 0 ) numthreads = omp_get_num_threads();
  70.             fa[i] = (float)i + 0.1;
  71.             fb[i] = (float)i + 0.2;
  72.         }  
  73.  
  74.         printf("Starting Compute\r\n");
  75.  
  76.         tstart = dtime();  
  77.         // loop many times to really get lots of calculations
  78.         #pragma omp parallel for private( j,k )
  79.         for ( i = 0; i < numthreads; i++ )
  80.         {
  81.        
  82.             int offset = i * LOOP_COUNT;
  83.            
  84.        
  85.         for(j=0; j<MAXFLOPS_ITERS; j++)  
  86.         {
  87.         //
  88.         // scale 1st array and add in the 2nd array
  89.         // example usage - y = mx + b;
  90.         //
  91.             for(k=0; k<LOOP_COUNT; k++)  
  92.         {
  93.                 fa[k + offset ] = a * fa[k + offset ] + fb[k + offset ];
  94.             }
  95.      }
  96.      
  97.      }
  98.          tstop = dtime();
  99.  
  100.          // # of gigaflops we just calculated  
  101.          gflops = (double)( 1.0e-9*LOOP_COUNT*MAXFLOPS_ITERS*FLOPSPERCALC);    
  102.  
  103.      // elasped time
  104.          ttime = tstop - tstart;
  105.  
  106.          //
  107.          // Print the results
  108.          //
  109.          if ((ttime) > 0.0)
  110.          {
  111.              printf("GFlops = %10.3lf, Secs = %10.3lf, GFlops per sec = %10.3lf\r\n",
  112.                      gflops, ttime, gflops/ttime);
  113.          }   
  114.          return( 0 );
  115. }
Advertisement
Add Comment
Please, Sign In to add comment