home *** CD-ROM | disk | FTP | other *** search
/ ftp.dataforce.net / 2014.05.ftp.dataforce.net.tar / ftp.dataforce.net / pub / solar / mflops.c < prev    next >
C/C++ Source or Header  |  1998-12-17  |  2KB  |  96 lines

  1. /*
  2.  * Measure peak MFLOPS: no memory accesses, a lot of parallelism.
  3.  */
  4.  
  5. #include <stdio.h>
  6. #include <string.h>
  7. #include <signal.h>
  8. #include <setjmp.h>
  9. #include <sys/time.h>
  10.  
  11. #define TIME                1
  12.  
  13. #define ITERATIONS            100
  14. #define INCREASING            1
  15. #define MIN                -1e6
  16. #define MAX                1e6
  17.  
  18. #define C0                -111.1
  19. #define C1                22.2
  20. #define C2                33.3
  21. #define C3                44.4
  22. #define C4                155.5
  23.  
  24. #define f(x, y) \
  25. { \
  26.     double x2; \
  27.     x2 = x * x; \
  28.     y = C0 + C1 * x + C2 * x2 + C3 * x * x2 + C4 * x2 * x2; \
  29. }
  30.  
  31. #define F(x, y) \
  32. { \
  33.     double u1, u2, u3, u4, u5; \
  34.     double v1, v2, v3, v4, v5; \
  35.     u1 = x * 0.123 + 0.678; \
  36.     u2 = x * 0.234 + 0.567; \
  37.     u3 = x * 0.345 + 0.456; \
  38.     u4 = x * 0.456 + 0.345; \
  39.     u5 = x * 0.567 + 0.234; \
  40.     f(x, y); \
  41.     f(u1, v1); \
  42.     f(u2, v2); \
  43.     f(u3, v3); \
  44.     f(u4, v4); \
  45.     f(u5, v5); \
  46.     y += v1 + v2 + v3 + v4 + v5; \
  47. }
  48.  
  49. #define FLOPS \
  50.     (ITERATIONS * (11 + (11 + 3) * (6 - 1) + 2))
  51.  
  52. jmp_buf there;
  53.  
  54. void handler()
  55. {
  56.     longjmp(there, 1);
  57. }
  58.  
  59. int main()
  60. {
  61.     struct itimerval tv;
  62.     volatile int count;
  63.     volatile double x, y;
  64.     double a, b, c, fc;
  65.     int i;
  66.  
  67.     count = 0;
  68.     if (setjmp(there)) {
  69.         printf("%.2f MFLOPS (x=%e y=%e)\n",
  70.             (double)count * FLOPS / TIME / 1e6, x, y);
  71.         return 0;
  72.     }
  73.  
  74.     signal(SIGVTALRM, handler);
  75.     memset(&tv, 0, sizeof(tv));
  76.     tv.it_value.tv_sec = TIME;
  77.     setitimer(ITIMER_VIRTUAL, &tv, NULL);
  78.  
  79.     while (1) {
  80.         a = MIN; b = MAX;
  81.         i = ITERATIONS;
  82.         do {
  83.             c = (a + b) * 0.5;
  84.             F(c, fc);
  85. #if INCREASING
  86.             if (fc > 0) b = c; else a = c;
  87. #else
  88.             if (fc < 0) b = c; else a = c;
  89. #endif
  90.         } while (--i);
  91.  
  92.         x = c; y = fc;
  93.         count++;
  94.     };
  95. }
  96.