| 1 | //
 | 
|---|
| 2 | // Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo
 | 
|---|
| 3 | //
 | 
|---|
| 4 | // The contents of this file are covered under the licence agreement in the
 | 
|---|
| 5 | // file "LICENCE" distributed with Cforall.
 | 
|---|
| 6 | //
 | 
|---|
| 7 | // poly-bench.cc -- 
 | 
|---|
| 8 | //
 | 
|---|
| 9 | // Author           : Aaron Moss
 | 
|---|
| 10 | // Created On       : Sat May 16 07:26:30 2015
 | 
|---|
| 11 | // Last Modified By : Peter A. Buhr
 | 
|---|
| 12 | // Last Modified On : Wed May 27 18:25:19 2015
 | 
|---|
| 13 | // Update Count     : 5
 | 
|---|
| 14 | //
 | 
|---|
| 15 | 
 | 
|---|
| 16 | extern "C" {
 | 
|---|
| 17 | #include <stdio.h>
 | 
|---|
| 18 | //#include "my_time.h"
 | 
|---|
| 19 | }
 | 
|---|
| 20 | 
 | 
|---|
| 21 | #define N 200000000
 | 
|---|
| 22 | 
 | 
|---|
| 23 | struct ipoint {
 | 
|---|
| 24 |         int x;
 | 
|---|
| 25 |         int y;
 | 
|---|
| 26 | };
 | 
|---|
| 27 | 
 | 
|---|
| 28 | struct ipoint ?+?(struct ipoint a, struct ipoint b) {
 | 
|---|
| 29 |         struct ipoint r;
 | 
|---|
| 30 |         r.x = a.x + b.x;
 | 
|---|
| 31 |         r.y = a.y + b.y;
 | 
|---|
| 32 |         return r;
 | 
|---|
| 33 | }
 | 
|---|
| 34 | 
 | 
|---|
| 35 | struct ipoint ?-?(struct ipoint a, struct ipoint b) {
 | 
|---|
| 36 |         struct ipoint r;
 | 
|---|
| 37 |         r.x = a.x - b.x;
 | 
|---|
| 38 |         r.y = a.y - b.y;
 | 
|---|
| 39 |         return r;
 | 
|---|
| 40 | }
 | 
|---|
| 41 | 
 | 
|---|
| 42 | struct ipoint ?*?(struct ipoint a, struct ipoint b) {
 | 
|---|
| 43 |         struct ipoint r;
 | 
|---|
| 44 |         r.x = a.x * b.x;
 | 
|---|
| 45 |         r.y = a.y * b.y;
 | 
|---|
| 46 |         return r;
 | 
|---|
| 47 | }
 | 
|---|
| 48 | 
 | 
|---|
| 49 | struct dpoint {
 | 
|---|
| 50 |         double x;
 | 
|---|
| 51 |         double y;
 | 
|---|
| 52 | };
 | 
|---|
| 53 | 
 | 
|---|
| 54 | struct dpoint ?+?(struct dpoint a, struct dpoint b) {
 | 
|---|
| 55 |         struct dpoint r;
 | 
|---|
| 56 |         r.x = a.x + b.x;
 | 
|---|
| 57 |         r.y = a.y + b.y;
 | 
|---|
| 58 |         return r;
 | 
|---|
| 59 | }
 | 
|---|
| 60 | 
 | 
|---|
| 61 | struct dpoint ?-?(struct dpoint a, struct dpoint b) {
 | 
|---|
| 62 |         struct dpoint r;
 | 
|---|
| 63 |         r.x = a.x - b.x;
 | 
|---|
| 64 |         r.y = a.y - b.y;
 | 
|---|
| 65 |         return r;
 | 
|---|
| 66 | }
 | 
|---|
| 67 | 
 | 
|---|
| 68 | struct dpoint ?*?(struct dpoint a, struct dpoint b) {
 | 
|---|
| 69 |         struct dpoint r;
 | 
|---|
| 70 |         r.x = a.x * b.x;
 | 
|---|
| 71 |         r.y = a.y * b.y;
 | 
|---|
| 72 |         return r;
 | 
|---|
| 73 | }
 | 
|---|
| 74 | 
 | 
|---|
| 75 | int a2b2_mono_int(int a, int b) {
 | 
|---|
| 76 |         return (a - b)*(a + b);
 | 
|---|
| 77 | }
 | 
|---|
| 78 | 
 | 
|---|
| 79 | double a2b2_mono_double(double a, double b) {
 | 
|---|
| 80 |         return (a - b)*(a + b);
 | 
|---|
| 81 | }
 | 
|---|
| 82 | 
 | 
|---|
| 83 | struct ipoint a2b2_mono_ipoint(struct ipoint a, struct ipoint b) {
 | 
|---|
| 84 |         return (a - b)*(a + b);
 | 
|---|
| 85 | }
 | 
|---|
| 86 | 
 | 
|---|
| 87 | struct dpoint a2b2_mono_dpoint(struct dpoint a, struct dpoint b) {
 | 
|---|
| 88 |         return (a - b)*(a + b);
 | 
|---|
| 89 | }
 | 
|---|
| 90 | 
 | 
|---|
| 91 | forall(type T | { T ?+?(T,T); T ?-?(T,T); T ?*?(T,T); })
 | 
|---|
| 92 | T a2b2_poly(T a, T b) {
 | 
|---|
| 93 |         return (a - b)*(a + b);
 | 
|---|
| 94 | }
 | 
|---|
| 95 | 
 | 
|---|
| 96 | typedef int clock_t;
 | 
|---|
| 97 | long ms_between(clock_t start, clock_t end) {
 | 
|---|
| 98 | //      return (end - start) / (CLOCKS_PER_SEC / 1000);
 | 
|---|
| 99 |         return 0;
 | 
|---|
| 100 | }
 | 
|---|
| 101 | int clock() { return 3; }
 | 
|---|
| 102 | 
 | 
|---|
| 103 | int main(int argc, char** argv) {
 | 
|---|
| 104 |         clock_t start, end;
 | 
|---|
| 105 |         int i;
 | 
|---|
| 106 |         
 | 
|---|
| 107 |         int a, b;
 | 
|---|
| 108 |         double c, d;
 | 
|---|
| 109 |         struct ipoint p, q;
 | 
|---|
| 110 |         struct dpoint r, s;
 | 
|---|
| 111 |         
 | 
|---|
| 112 |         printf("\n## a^2-b^2 ##\n");
 | 
|---|
| 113 |         
 | 
|---|
| 114 |         a = 5, b = 3;
 | 
|---|
| 115 |         start = clock();
 | 
|---|
| 116 |         for (i = 0; i < N/2; ++i) {
 | 
|---|
| 117 |                 a = a2b2_mono_int(a, b);
 | 
|---|
| 118 |                 b = a2b2_mono_int(b, a);
 | 
|---|
| 119 |         }
 | 
|---|
| 120 |         end = clock();
 | 
|---|
| 121 |         printf("mono_int:   %7ld  [%d,%d]\n", ms_between(start, end), a, b);
 | 
|---|
| 122 |         
 | 
|---|
| 123 |         a = 5, b = 3;
 | 
|---|
| 124 |         start = clock();
 | 
|---|
| 125 |         for (i = 0; i < N/2; ++i) {
 | 
|---|
| 126 |                 a = a2b2_poly(a, b);
 | 
|---|
| 127 |                 b = a2b2_poly(b, a);
 | 
|---|
| 128 |         }
 | 
|---|
| 129 |         end = clock();
 | 
|---|
| 130 |         printf("poly_int:   %7ld  [%d,%d]\n", ms_between(start, end), a, b);
 | 
|---|
| 131 |         
 | 
|---|
| 132 | /*      {
 | 
|---|
| 133 |         a = 5, b = 3;
 | 
|---|
| 134 |         // below doesn't actually work; a2b2_poly isn't actually assigned, just declared
 | 
|---|
| 135 |         * [int] (int, int) a2b2_poly = a2b2_mono_int;
 | 
|---|
| 136 |         start = clock();
 | 
|---|
| 137 |         for (i = 0; i < N/2; ++i) {
 | 
|---|
| 138 | //                      printf("\t[%d,%d]\n", a, b);
 | 
|---|
| 139 | a = a2b2_poly(a, b);
 | 
|---|
| 140 | //                      printf("\t[%d,%d]\n", a, b);
 | 
|---|
| 141 | b = a2b2_poly(b, a);
 | 
|---|
| 142 | }
 | 
|---|
| 143 | end = clock();
 | 
|---|
| 144 | printf("spec_int:   %7ld  [%d,%d]\n", ms_between(start, end), a, b);
 | 
|---|
| 145 | }
 | 
|---|
| 146 | */      
 | 
|---|
| 147 |         c = 5.0, d = 3.0;
 | 
|---|
| 148 |         start = clock();
 | 
|---|
| 149 |         for (i = 0; i < N/2; ++i) {
 | 
|---|
| 150 |                 c = a2b2_mono_double(c, d);
 | 
|---|
| 151 |                 d = a2b2_mono_double(d, c);
 | 
|---|
| 152 |         }
 | 
|---|
| 153 |         end = clock();
 | 
|---|
| 154 |         printf("mono_double:%7ld  [%f,%f]\n", ms_between(start, end), c, d);
 | 
|---|
| 155 |                 
 | 
|---|
| 156 |         c = 5.0, d = 3.0;
 | 
|---|
| 157 |         start = clock();
 | 
|---|
| 158 |         for (i = 0; i < N/2; ++i) {
 | 
|---|
| 159 |                 c = a2b2_poly(c, d);
 | 
|---|
| 160 |                 d = a2b2_poly(d, c);
 | 
|---|
| 161 |         }
 | 
|---|
| 162 |         end = clock();
 | 
|---|
| 163 |         printf("poly_double:%7ld  [%f,%f]\n", ms_between(start, end), c, d);
 | 
|---|
| 164 |         
 | 
|---|
| 165 |         p.x = 5, p.y = 5, q.x = 3, q.y = 3;
 | 
|---|
| 166 |         start = clock();
 | 
|---|
| 167 |         for (i = 0; i < N/2; ++i) {
 | 
|---|
| 168 |                 p = a2b2_mono_ipoint(p, q);
 | 
|---|
| 169 |                 q = a2b2_mono_ipoint(q, p);
 | 
|---|
| 170 |         }
 | 
|---|
| 171 |         end = clock();
 | 
|---|
| 172 |         printf("mono_ipoint:%7ld  [(%d,%d),(%d,%d)]\n", ms_between(start, end), p.x, p.y, q.x, q.y);
 | 
|---|
| 173 |                 
 | 
|---|
| 174 |         p.x = 5, p.y = 5, q.x = 3, q.y = 3;
 | 
|---|
| 175 |         start = clock();
 | 
|---|
| 176 |         for (i = 0; i < N/2; ++i) {
 | 
|---|
| 177 |                 p = a2b2_poly(p, q);
 | 
|---|
| 178 |                 q = a2b2_poly(q, p);
 | 
|---|
| 179 |         }
 | 
|---|
| 180 |         end = clock();
 | 
|---|
| 181 |         printf("poly_ipoint:%7ld  [(%d,%d),(%d,%d)]\n", ms_between(start, end), p.x, p.y, q.x, q.y);
 | 
|---|
| 182 |         
 | 
|---|
| 183 |         r.x = 5.0, r.y = 5.0, s.x = 3.0, s.y = 3.0;
 | 
|---|
| 184 |         start = clock();
 | 
|---|
| 185 |         for (i = 0; i < N/2; ++i) {
 | 
|---|
| 186 |                 r = a2b2_mono_dpoint(r, s);
 | 
|---|
| 187 |                 s = a2b2_mono_dpoint(s, r);
 | 
|---|
| 188 |         }
 | 
|---|
| 189 |         end = clock();
 | 
|---|
| 190 |         printf("mono_dpoint:%7ld  [(%f,%f),(%f,%f)]\n", ms_between(start, end), r.x, r.y, s.x, s.y);
 | 
|---|
| 191 |                 
 | 
|---|
| 192 |         r.x = 5.0, r.y = 5.0, s.x = 3.0, s.y = 3.0;
 | 
|---|
| 193 |         start = clock();
 | 
|---|
| 194 |         for (i = 0; i < N/2; ++i) {
 | 
|---|
| 195 |                 r = a2b2_poly(r, s);
 | 
|---|
| 196 |                 s = a2b2_poly(s, r);
 | 
|---|
| 197 |         }
 | 
|---|
| 198 |         end = clock();
 | 
|---|
| 199 |         printf("poly_dpoint:%7ld  [(%f,%f),(%f,%f)]\n", ms_between(start, end), r.x, r.y, s.x, s.y);
 | 
|---|
| 200 | 
 | 
|---|
| 201 |         return 0;
 | 
|---|
| 202 | }
 | 
|---|
| 203 | 
 | 
|---|
| 204 | // Local Variables: //
 | 
|---|
| 205 | // tab-width: 4 //
 | 
|---|
| 206 | // compile-command: "cfa poly-bench.c" //
 | 
|---|
| 207 | // End: //
 | 
|---|