Don't use POSIX fnmatch() for pattern matching.
[gromacs/qmmm-gamess-us.git] / src / gmxlib / gmx_cyclecounter.c
blob60ff1d1d89f33d710360e9e4988270a21763e454
1 /* -*- mode: c; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; c-file-style: "stroustrup"; -*-
3 *
4 * This file is part of Gromacs Copyright (c) 1991-2006
5 * David van der Spoel, Erik Lindahl, Berk Hess, University of Groningen.
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
12 * To help us fund GROMACS development, we humbly ask that you cite
13 * the research papers on the package. Check out http://www.gromacs.org
15 * And Hey:
16 * Gnomes, ROck Monsters And Chili Sauce
18 #ifdef HAVE_CONFIG_H
19 #include <config.h>
20 #endif
22 #include <time.h>
23 #ifdef HAVE_SYS_TIME_H
24 #include <sys/time.h>
25 #endif
27 #ifdef _MSC_VER
28 #include <windows.h>
29 #endif
31 #include "gmx_cyclecounter.h"
37 /*! \brief Calculate number of seconds per cycle tick on host
39 * This routine runs a timer loop to calibrate the number of
40 * seconds per the units returned fro gmx_cycles_read().
42 * \param sampletime Minimum real sample time. It takes some trial-and-error
43 * to find the correct delay loop size, so the total runtime of
44 * this routine is about twice this time.
45 * \return Number of seconds per cycle unit. If it is not possible to
46 * calculate on this system (for whatever reason) the return value
47 * will be -1, so check that it is positive before using it.
49 double
50 gmx_cycles_calibrate(double sampletime)
52 #ifdef _MSC_VER
54 /* Windows does not have gettimeofday, but it provides a special
55 * routine that returns the cycle counter frequency.
57 LARGE_INTEGER i;
59 QueryPerformanceFrequency(&i);
61 return 1.0/((double) i.QuadPart);
62 /* end of MS Windows implementation */
64 #elif (defined HAVE_GETTIMEOFDAY)
66 /* generic implementation with gettimeofday() */
67 struct timeval t1,t2;
68 struct timezone tz = { 0,0 };
69 gmx_cycles_t c1,c2;
70 double timediff,cyclediff;
71 double d=0.1; /* Dummy variable so we don't optimize away delay loop */
72 int i;
74 if(!gmx_cycles_have_counter())
76 return -1;
79 #if (defined(__alpha__) || defined(__alpha))
80 /* Alpha cannot count to more than 4e9, but I don't expect
81 * that the architecture will go over 2GHz before it dies, so
82 * up to 2.0 seconds of sampling should be safe.
84 if(sampletime>2.0)
86 sampletime=2.0;
88 #endif
90 /* Start a timing loop. We want this to be largely independent
91 * of machine speed, so we need to start with a very small number
92 * of iterations and repeat it until we reach the requested time.
94 * We call gettimeofday an extra time at the start to avoid cache misses.
96 gettimeofday(&t1,&tz);
97 gettimeofday(&t1,&tz);
98 c1=gmx_cycles_read();
102 /* just a delay loop. To avoid optimizing it away, we calculate a number
103 * that will underflow to zero in most cases. By conditionally adding it
104 * to a result at the end it cannot be removed. n=10000 is arbitrary...
106 for(i=0;i<10000;i++)
107 d=d/(1.0+(double)i);
108 /* Read the time again */
109 gettimeofday(&t2,&tz);
110 c2=gmx_cycles_read();
111 timediff=(double)(t2.tv_sec-t1.tv_sec)+
112 (double)(t2.tv_usec-t1.tv_usec)*1e-6;
114 while( timediff < sampletime );
116 cyclediff=c2-c1;
118 /* Add a very small result so the delay loop cannot be optimized away */
119 if(d<1e-30)
121 timediff+=d;
124 /* Return seconds per cycle */
125 return timediff/cyclediff;
127 #else
128 /* No timing function available */
129 return -1;
130 #endif