1f08c3bdfSopenharmony_ci/* 2f08c3bdfSopenharmony_ci * 3f08c3bdfSopenharmony_ci * Copyright (c) International Business Machines Corp., 2002 4f08c3bdfSopenharmony_ci * 5f08c3bdfSopenharmony_ci * This program is free software; you can redistribute it and/or modify 6f08c3bdfSopenharmony_ci * it under the terms of the GNU General Public License as published by 7f08c3bdfSopenharmony_ci * the Free Software Foundation; either version 2 of the License, or 8f08c3bdfSopenharmony_ci * (at your option) any later version. 9f08c3bdfSopenharmony_ci * 10f08c3bdfSopenharmony_ci * This program is distributed in the hope that it will be useful, 11f08c3bdfSopenharmony_ci * but WITHOUT ANY WARRANTY; without even the implied warranty of 12f08c3bdfSopenharmony_ci * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See 13f08c3bdfSopenharmony_ci * the GNU General Public License for more details. 14f08c3bdfSopenharmony_ci * 15f08c3bdfSopenharmony_ci * You should have received a copy of the GNU General Public License 16f08c3bdfSopenharmony_ci * along with this program; if not, write to the Free Software 17f08c3bdfSopenharmony_ci * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 18f08c3bdfSopenharmony_ci */ 19f08c3bdfSopenharmony_ci 20f08c3bdfSopenharmony_ci/* Group Bull & IBM Corporation */ 21f08c3bdfSopenharmony_ci/* 11/20/2002 Port to LTP robbiew@us.ibm.com */ 22f08c3bdfSopenharmony_ci/* jacky.malcles@bull.net */ 23f08c3bdfSopenharmony_ci/* IBM Corporation */ 24f08c3bdfSopenharmony_ci/* 06/30/2001 Port to Linux nsharoff@us.ibm.com */ 25f08c3bdfSopenharmony_ci 26f08c3bdfSopenharmony_ci/* 27f08c3bdfSopenharmony_ci * fptest02.c -- Floating point test. 28f08c3bdfSopenharmony_ci * 29f08c3bdfSopenharmony_ci * This is similar to fptest1. Random values are used for some of the 30f08c3bdfSopenharmony_ci * math in routine "gauss". The value "avgspd" computed in routine 31f08c3bdfSopenharmony_ci * "term()" should come out to a known value. If this happens this 32f08c3bdfSopenharmony_ci * program prints a "passed" message and exits 0, otherwise a "failed" 33f08c3bdfSopenharmony_ci * message is printed and it exits with value 1. 34f08c3bdfSopenharmony_ci * 35f08c3bdfSopenharmony_ci */ 36f08c3bdfSopenharmony_ci 37f08c3bdfSopenharmony_ci#include <stdio.h> 38f08c3bdfSopenharmony_ci#include <errno.h> 39f08c3bdfSopenharmony_ci#include <math.h> 40f08c3bdfSopenharmony_ci#include <stdlib.h> 41f08c3bdfSopenharmony_ci#include <unistd.h> 42f08c3bdfSopenharmony_ci#include <sys/types.h> 43f08c3bdfSopenharmony_ci#include <sys/stat.h> 44f08c3bdfSopenharmony_ci#include <fcntl.h> 45f08c3bdfSopenharmony_ci#include <time.h> 46f08c3bdfSopenharmony_ci 47f08c3bdfSopenharmony_ci#define MAGIC 0.777807 48f08c3bdfSopenharmony_ci#define DIFF 0.001 49f08c3bdfSopenharmony_ci#define EVENTMX 256 50f08c3bdfSopenharmony_ci#define BIG 1.e50 51f08c3bdfSopenharmony_ci#define FALSE 0 52f08c3bdfSopenharmony_ci#define TRUE 1 53f08c3bdfSopenharmony_ci#define TRYCRIT 1 54f08c3bdfSopenharmony_ci#define ENTERCRIT 2 55f08c3bdfSopenharmony_ci#define LEAVECRIT 3 56f08c3bdfSopenharmony_ci#define ATBARRIER 4 57f08c3bdfSopenharmony_ci#define ENTERWORK 5 58f08c3bdfSopenharmony_ci#define LEAVEWORK 6 59f08c3bdfSopenharmony_ci#define NULLEVENT 999 60f08c3bdfSopenharmony_ci 61f08c3bdfSopenharmony_ci/** LTP Port **/ 62f08c3bdfSopenharmony_ci#include "test.h" 63f08c3bdfSopenharmony_ci 64f08c3bdfSopenharmony_cichar *TCID = "fptest02"; /* Test program identifier. */ 65f08c3bdfSopenharmony_ciint TST_TOTAL = 1; /* Total number of test cases. */ 66f08c3bdfSopenharmony_ci/**************/ 67f08c3bdfSopenharmony_ci 68f08c3bdfSopenharmony_cistruct event { 69f08c3bdfSopenharmony_ci int proc; 70f08c3bdfSopenharmony_ci int type; 71f08c3bdfSopenharmony_ci double time; 72f08c3bdfSopenharmony_ci}; 73f08c3bdfSopenharmony_ci 74f08c3bdfSopenharmony_cistatic int init(void); 75f08c3bdfSopenharmony_cistatic int doevent(struct event *); 76f08c3bdfSopenharmony_cistatic int term(void); 77f08c3bdfSopenharmony_cistatic int addevent(int, int, double); 78f08c3bdfSopenharmony_ci 79f08c3bdfSopenharmony_cistatic void gaussinit(double, double, int); 80f08c3bdfSopenharmony_cistatic double gauss(void); 81f08c3bdfSopenharmony_ci 82f08c3bdfSopenharmony_cistruct event eventtab[EVENTMX]; 83f08c3bdfSopenharmony_cistruct event rtrevent; 84f08c3bdfSopenharmony_ciint waiting[EVENTMX]; /* array of waiting processors */ 85f08c3bdfSopenharmony_ciint nwaiting; /* number of waiting processors */ 86f08c3bdfSopenharmony_cidouble sgtime; /* global clock */ 87f08c3bdfSopenharmony_cidouble lsttime; /* time used for editing */ 88f08c3bdfSopenharmony_cidouble dtc, dts, alpha; /* timing parameters */ 89f08c3bdfSopenharmony_ciint nproc; /* number of processors */ 90f08c3bdfSopenharmony_ciint barcnt; /* number of processors ATBARRIER */ 91f08c3bdfSopenharmony_ciint ncycle; /* number of cycles completed */ 92f08c3bdfSopenharmony_ciint ncycmax; /* number of cycles to run */ 93f08c3bdfSopenharmony_ciint critfree; /* TRUE if critical section not occupied */ 94f08c3bdfSopenharmony_ci 95f08c3bdfSopenharmony_cistatic struct event *nextevent(void ); 96f08c3bdfSopenharmony_ci 97f08c3bdfSopenharmony_ciint main(void) 98f08c3bdfSopenharmony_ci{ 99f08c3bdfSopenharmony_ci struct event *ev; 100f08c3bdfSopenharmony_ci 101f08c3bdfSopenharmony_ci nproc = 128; 102f08c3bdfSopenharmony_ci ncycmax = 10; 103f08c3bdfSopenharmony_ci dtc = 0.01; 104f08c3bdfSopenharmony_ci dts = 0.0; 105f08c3bdfSopenharmony_ci alpha = 0.1; 106f08c3bdfSopenharmony_ci 107f08c3bdfSopenharmony_ci init(); 108f08c3bdfSopenharmony_ci 109f08c3bdfSopenharmony_ci while ((ev = nextevent()) != NULL) { 110f08c3bdfSopenharmony_ci doevent(ev); 111f08c3bdfSopenharmony_ci } 112f08c3bdfSopenharmony_ci 113f08c3bdfSopenharmony_ci term(); 114f08c3bdfSopenharmony_ci tst_resm(TPASS, "PASS"); 115f08c3bdfSopenharmony_ci tst_exit(); 116f08c3bdfSopenharmony_ci} 117f08c3bdfSopenharmony_ci 118f08c3bdfSopenharmony_ci/* 119f08c3bdfSopenharmony_ci initialize all processes to "entering work section" 120f08c3bdfSopenharmony_ci*/ 121f08c3bdfSopenharmony_cistatic int init(void) 122f08c3bdfSopenharmony_ci{ 123f08c3bdfSopenharmony_ci int p; 124f08c3bdfSopenharmony_ci double dtw, dtwsig; 125f08c3bdfSopenharmony_ci 126f08c3bdfSopenharmony_ci ncycle = 0; 127f08c3bdfSopenharmony_ci sgtime = 0; 128f08c3bdfSopenharmony_ci lsttime = 0; 129f08c3bdfSopenharmony_ci barcnt = 0; 130f08c3bdfSopenharmony_ci nwaiting = 0; 131f08c3bdfSopenharmony_ci critfree = TRUE; 132f08c3bdfSopenharmony_ci 133f08c3bdfSopenharmony_ci dtw = 1. / nproc; /* mean process work time */ 134f08c3bdfSopenharmony_ci dtwsig = dtw * alpha; /* std deviation of work time */ 135f08c3bdfSopenharmony_ci gaussinit(dtw, dtwsig, time(0)); 136f08c3bdfSopenharmony_ci 137f08c3bdfSopenharmony_ci for (p = 1; p <= nproc; p++) { 138f08c3bdfSopenharmony_ci eventtab[p].type = NULLEVENT; 139f08c3bdfSopenharmony_ci } 140f08c3bdfSopenharmony_ci 141f08c3bdfSopenharmony_ci for (p = 1; p <= nproc; p++) { 142f08c3bdfSopenharmony_ci addevent(ENTERWORK, p, sgtime); 143f08c3bdfSopenharmony_ci } 144f08c3bdfSopenharmony_ci 145f08c3bdfSopenharmony_ci return (0); 146f08c3bdfSopenharmony_ci} 147f08c3bdfSopenharmony_ci 148f08c3bdfSopenharmony_ci/* 149f08c3bdfSopenharmony_ci print edit quantities 150f08c3bdfSopenharmony_ci*/ 151f08c3bdfSopenharmony_cistatic int term(void) 152f08c3bdfSopenharmony_ci{ 153f08c3bdfSopenharmony_ci double avgspd; 154f08c3bdfSopenharmony_ci double v; 155f08c3bdfSopenharmony_ci 156f08c3bdfSopenharmony_ci avgspd = ncycle / sgtime; 157f08c3bdfSopenharmony_ci v = avgspd - MAGIC; 158f08c3bdfSopenharmony_ci if (v < 0.0) 159f08c3bdfSopenharmony_ci v *= -1.0; 160f08c3bdfSopenharmony_ci if (v > DIFF) { 161f08c3bdfSopenharmony_ci tst_resm(TFAIL, "FAIL"); 162f08c3bdfSopenharmony_ci v = avgspd - MAGIC; 163f08c3bdfSopenharmony_ci tst_resm(TINFO, "avgspd = %.15f\n", avgspd); 164f08c3bdfSopenharmony_ci tst_resm(TINFO, "expected %.15f\n", MAGIC); 165f08c3bdfSopenharmony_ci tst_resm(TINFO, "diff = %.15f\n", v); 166f08c3bdfSopenharmony_ci tst_exit(); 167f08c3bdfSopenharmony_ci } 168f08c3bdfSopenharmony_ci return (0); 169f08c3bdfSopenharmony_ci} 170f08c3bdfSopenharmony_ci 171f08c3bdfSopenharmony_ci/* 172f08c3bdfSopenharmony_ci add an event to the event queue 173f08c3bdfSopenharmony_ci*/ 174f08c3bdfSopenharmony_cistatic int addevent(int type, int proc, double t) 175f08c3bdfSopenharmony_ci{ 176f08c3bdfSopenharmony_ci int i; 177f08c3bdfSopenharmony_ci int ok = FALSE; 178f08c3bdfSopenharmony_ci 179f08c3bdfSopenharmony_ci for (i = 1; i <= nproc; i++) { 180f08c3bdfSopenharmony_ci if (eventtab[i].type == NULLEVENT) { 181f08c3bdfSopenharmony_ci eventtab[i].type = type; 182f08c3bdfSopenharmony_ci eventtab[i].proc = proc; 183f08c3bdfSopenharmony_ci eventtab[i].time = t; 184f08c3bdfSopenharmony_ci ok = TRUE; 185f08c3bdfSopenharmony_ci break; 186f08c3bdfSopenharmony_ci } 187f08c3bdfSopenharmony_ci } 188f08c3bdfSopenharmony_ci if (ok) 189f08c3bdfSopenharmony_ci return (0); 190f08c3bdfSopenharmony_ci else 191f08c3bdfSopenharmony_ci tst_brkm(TBROK, NULL, "No room for event"); 192f08c3bdfSopenharmony_ci 193f08c3bdfSopenharmony_ci return (0); 194f08c3bdfSopenharmony_ci} 195f08c3bdfSopenharmony_ci 196f08c3bdfSopenharmony_ci/* 197f08c3bdfSopenharmony_ci get earliest event in event queue 198f08c3bdfSopenharmony_ci*/ 199f08c3bdfSopenharmony_cistatic struct event *nextevent(void) 200f08c3bdfSopenharmony_ci{ 201f08c3bdfSopenharmony_ci double mintime = BIG; 202f08c3bdfSopenharmony_ci int imin = 0; 203f08c3bdfSopenharmony_ci int i; 204f08c3bdfSopenharmony_ci 205f08c3bdfSopenharmony_ci for (i = 1; i <= nproc; i++) { 206f08c3bdfSopenharmony_ci if ((eventtab[i].type != NULLEVENT) 207f08c3bdfSopenharmony_ci && (eventtab[i].time < mintime)) { 208f08c3bdfSopenharmony_ci imin = i; 209f08c3bdfSopenharmony_ci mintime = eventtab[i].time; 210f08c3bdfSopenharmony_ci } 211f08c3bdfSopenharmony_ci } 212f08c3bdfSopenharmony_ci 213f08c3bdfSopenharmony_ci if (imin) { 214f08c3bdfSopenharmony_ci rtrevent.type = eventtab[imin].type; 215f08c3bdfSopenharmony_ci rtrevent.proc = eventtab[imin].proc; 216f08c3bdfSopenharmony_ci rtrevent.time = eventtab[imin].time; 217f08c3bdfSopenharmony_ci eventtab[imin].type = NULLEVENT; 218f08c3bdfSopenharmony_ci return (&rtrevent); 219f08c3bdfSopenharmony_ci } else 220f08c3bdfSopenharmony_ci return (NULL); 221f08c3bdfSopenharmony_ci} 222f08c3bdfSopenharmony_ci 223f08c3bdfSopenharmony_ci/* 224f08c3bdfSopenharmony_ci add a processor to the waiting queue 225f08c3bdfSopenharmony_ci*/ 226f08c3bdfSopenharmony_cistatic int addwaiting(int p) 227f08c3bdfSopenharmony_ci{ 228f08c3bdfSopenharmony_ci waiting[++nwaiting] = p; 229f08c3bdfSopenharmony_ci return (0); 230f08c3bdfSopenharmony_ci} 231f08c3bdfSopenharmony_ci 232f08c3bdfSopenharmony_ci/* 233f08c3bdfSopenharmony_ci remove the next processor from the waiting queue 234f08c3bdfSopenharmony_ci*/ 235f08c3bdfSopenharmony_cistatic int getwaiting(void) 236f08c3bdfSopenharmony_ci{ 237f08c3bdfSopenharmony_ci if (nwaiting) 238f08c3bdfSopenharmony_ci return (waiting[nwaiting--]); 239f08c3bdfSopenharmony_ci else 240f08c3bdfSopenharmony_ci return (0); 241f08c3bdfSopenharmony_ci} 242f08c3bdfSopenharmony_ci 243f08c3bdfSopenharmony_cistatic double dtcrit(void) 244f08c3bdfSopenharmony_ci{ 245f08c3bdfSopenharmony_ci return (dtc); 246f08c3bdfSopenharmony_ci} 247f08c3bdfSopenharmony_ci 248f08c3bdfSopenharmony_cistatic double dtspinoff(void) 249f08c3bdfSopenharmony_ci{ 250f08c3bdfSopenharmony_ci return (dts); 251f08c3bdfSopenharmony_ci} 252f08c3bdfSopenharmony_ci 253f08c3bdfSopenharmony_cistatic double dtwork(void) 254f08c3bdfSopenharmony_ci{ 255f08c3bdfSopenharmony_ci return (gauss()); 256f08c3bdfSopenharmony_ci} 257f08c3bdfSopenharmony_ci 258f08c3bdfSopenharmony_ci/* 259f08c3bdfSopenharmony_ci take the action prescribed by 'ev', update the clock, and 260f08c3bdfSopenharmony_ci generate any subsequent events 261f08c3bdfSopenharmony_ci*/ 262f08c3bdfSopenharmony_cistatic int doevent(struct event *ev) 263f08c3bdfSopenharmony_ci{ 264f08c3bdfSopenharmony_ci double nxttime; 265f08c3bdfSopenharmony_ci int i, p, proc; 266f08c3bdfSopenharmony_ci 267f08c3bdfSopenharmony_ci sgtime = ev->time; 268f08c3bdfSopenharmony_ci proc = ev->proc; 269f08c3bdfSopenharmony_ci 270f08c3bdfSopenharmony_ci switch (ev->type) { 271f08c3bdfSopenharmony_ci case TRYCRIT: 272f08c3bdfSopenharmony_ci if (critfree == TRUE) 273f08c3bdfSopenharmony_ci addevent(ENTERCRIT, proc, sgtime); 274f08c3bdfSopenharmony_ci else 275f08c3bdfSopenharmony_ci addwaiting(proc); 276f08c3bdfSopenharmony_ci break; 277f08c3bdfSopenharmony_ci case ENTERCRIT: 278f08c3bdfSopenharmony_ci critfree = FALSE; 279f08c3bdfSopenharmony_ci nxttime = sgtime + dtcrit(); 280f08c3bdfSopenharmony_ci addevent(LEAVECRIT, proc, nxttime); 281f08c3bdfSopenharmony_ci break; 282f08c3bdfSopenharmony_ci case LEAVECRIT: 283f08c3bdfSopenharmony_ci critfree = TRUE; 284f08c3bdfSopenharmony_ci addevent(ATBARRIER, proc, sgtime); 285f08c3bdfSopenharmony_ci if ((p = getwaiting()) != 0) { 286f08c3bdfSopenharmony_ci nxttime = sgtime; 287f08c3bdfSopenharmony_ci addevent(ENTERCRIT, p, nxttime); 288f08c3bdfSopenharmony_ci } 289f08c3bdfSopenharmony_ci break; 290f08c3bdfSopenharmony_ci case ATBARRIER: 291f08c3bdfSopenharmony_ci barcnt++; 292f08c3bdfSopenharmony_ci if (barcnt == nproc) { 293f08c3bdfSopenharmony_ci nxttime = sgtime; 294f08c3bdfSopenharmony_ci for (i = 1; i <= nproc; i++) { 295f08c3bdfSopenharmony_ci nxttime += dtspinoff(); 296f08c3bdfSopenharmony_ci addevent(ENTERWORK, i, nxttime); 297f08c3bdfSopenharmony_ci } 298f08c3bdfSopenharmony_ci barcnt = 0; 299f08c3bdfSopenharmony_ci ncycle++; 300f08c3bdfSopenharmony_ci } 301f08c3bdfSopenharmony_ci break; 302f08c3bdfSopenharmony_ci case ENTERWORK: 303f08c3bdfSopenharmony_ci nxttime = sgtime + dtwork(); 304f08c3bdfSopenharmony_ci if (ncycle < ncycmax) 305f08c3bdfSopenharmony_ci addevent(LEAVEWORK, proc, nxttime); 306f08c3bdfSopenharmony_ci break; 307f08c3bdfSopenharmony_ci case LEAVEWORK: 308f08c3bdfSopenharmony_ci addevent(TRYCRIT, proc, sgtime); 309f08c3bdfSopenharmony_ci break; 310f08c3bdfSopenharmony_ci default: 311f08c3bdfSopenharmony_ci tst_brkm(TBROK, NULL, "Illegal event"); 312f08c3bdfSopenharmony_ci break; 313f08c3bdfSopenharmony_ci } 314f08c3bdfSopenharmony_ci return (0); 315f08c3bdfSopenharmony_ci} 316f08c3bdfSopenharmony_ci 317f08c3bdfSopenharmony_cistatic int alternator = 1; 318f08c3bdfSopenharmony_cistatic double mean; 319f08c3bdfSopenharmony_cistatic double stdev; 320f08c3bdfSopenharmony_cistatic double u1, u2; 321f08c3bdfSopenharmony_cistatic double twopi; 322f08c3bdfSopenharmony_cistatic double rnorm = 2147483647; 323f08c3bdfSopenharmony_ci 324f08c3bdfSopenharmony_cistatic void gaussinit(double m, double s, int seed) 325f08c3bdfSopenharmony_ci{ 326f08c3bdfSopenharmony_ci srand48(seed); 327f08c3bdfSopenharmony_ci mean = m; 328f08c3bdfSopenharmony_ci stdev = s; 329f08c3bdfSopenharmony_ci twopi = 2. * acos((double)-1.0); 330f08c3bdfSopenharmony_ci return; 331f08c3bdfSopenharmony_ci} 332f08c3bdfSopenharmony_ci 333f08c3bdfSopenharmony_cistatic double gauss(void) 334f08c3bdfSopenharmony_ci{ 335f08c3bdfSopenharmony_ci double x1, x2; 336f08c3bdfSopenharmony_ci 337f08c3bdfSopenharmony_ci if (alternator == 1) { 338f08c3bdfSopenharmony_ci alternator = -1; 339f08c3bdfSopenharmony_ci u1 = lrand48() / rnorm; 340f08c3bdfSopenharmony_ci u2 = lrand48() / rnorm; 341f08c3bdfSopenharmony_ci x1 = sqrt(-2.0 * log(u1)) * cos(twopi * u2); 342f08c3bdfSopenharmony_ci return (mean + stdev * x1); 343f08c3bdfSopenharmony_ci } else { 344f08c3bdfSopenharmony_ci alternator = 1; 345f08c3bdfSopenharmony_ci x2 = sqrt(-2.0 * log(u1)) * sin(twopi * u2); 346f08c3bdfSopenharmony_ci return (mean + stdev * x2); 347f08c3bdfSopenharmony_ci } 348f08c3bdfSopenharmony_ci} 349