MADNESS  version 0.9
worldtime.h
Go to the documentation of this file.
1 /*
2  This file is part of MADNESS.
3 
4  Copyright (C) 2007,2010 Oak Ridge National Laboratory
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with this program; if not, write to the Free Software
18  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 
20  For more information please contact:
21 
22  Robert J. Harrison
23  Oak Ridge National Laboratory
24  One Bethel Valley Road
25  P.O. Box 2008, MS-6367
26 
27  email: harrisonrj@ornl.gov
28  tel: 865-241-3937
29  fax: 865-572-0680
30 
31 
32  $Id$
33 */
34 
35 
36 #ifndef MADNESS_WORLD_WORLDTIME_H__INCLUDED
37 #define MADNESS_WORLD_WORLDTIME_H__INCLUDED
38 
39 #include <stdint.h>
40 #include <time.h>
41 #include <sys/time.h>
42 #include <unistd.h>
43 #include <madness/madness_config.h>
44 
45 #ifdef _CRAY
46 #include <catamount/dclock.h>
47 #endif
48 
49 #ifdef HAVE_IBMBGP
50 # define BG_CYCLES_PER_MICROSECOND 850
51 # define BG_SECONDS_PER_CYCLE 1.176470588235294033e-09
52 # include </bgsys/drivers/ppcfloor/arch/include/bpcore/ppc450_inlines.h>
53 #endif
54 
55 #ifdef HAVE_IBMBGQ
56 # define BG_CYCLES_PER_MICROSECOND 1600
57 # define BG_SECONDS_PER_CYCLE 6.25e-10
58 # include <hwi/include/bqc/A2_inlines.h>
59 #endif
60 
63 
64 
65 namespace madness {
66 
68 
71  double wall_time();
72 
74 
79  static inline uint64_t cycle_count() {
80  uint64_t x;
81 #if defined(HAVE_IBMBGP)
82  unsigned int rx, ry, rz;
83  do
84  {
85  asm volatile ( "mftbu %0" : "=r"(rx) );
86  asm volatile ( "mftb %0" : "=r"(ry) );
87  asm volatile ( "mftbu %0" : "=r"(rz) );
88  }
89  while ( rx != rz );
90  x = rx;
91  x = (x << 32) | ry;
92 #elif defined(HAVE_IBMBGQ)
93 /* Jeff could use the asm above but is pretending this is more portable */
94  x = GetTimeBase();
95 #elif defined(X86_32)
96 __asm__ volatile(".byte 0x0f, 0x31" : "=A"(x));
97 #elif defined(X86_64)
98  unsigned int a,d;
99 __asm__ volatile("rdtsc" : "=a"(a), "=d"(d));
100  x = ((uint64_t)a) | (((uint64_t)d)<<32);
101 #else
102  x = wall_time()*1e9;
103 #endif
104  return x;
105  }
106 
108 
117  double cpu_frequency();
118 
119 
121 
124  static inline double cpu_time() {
125 #if defined(X86_32) || defined(X86_64) || defined(HAVE_IBMBGP)
126  static const double rfreq = 1.0/cpu_frequency();
127  return cycle_count()*rfreq;
128 #elif defined(_CRAY)
129  return dclock();
130 #elif defined(HAVE_IBMBGP)
131  return BG_SECONDS_PER_CYCLE * _bgp_GetTimeBase();
132 #elif defined(HAVE_IBMBGQ)
133  return BG_SECONDS_PER_CYCLE * GetTimeBase();
134 #else
135  return double(clock())/CLOCKS_PER_SEC;
136 #endif
137  }
138 
139 
141  inline void cpu_relax() {
142 #if defined(X86_32) || defined(X86_64)
143  asm volatile("rep;nop" : : : "memory");
144 #elif defined(HAVE_IBMBGP)
145  //for (int i=0; i<25; i++) {
146  // asm volatile ("nop\n");
147  //}
148  asm volatile ("nop\n"); /* this might be dangerous */
149 #elif defined(HAVE_IBMBGQ)
150  //Delay(200); /* this is calling asm nop */
151  // for (int i=0; i<10; i++) {
152  asm volatile ("nop\n");
153  //}
154 #else
155 #error cpu_relax is not implemented!
156 #endif
157  }
158 
159 
161 
163  static inline void myusleep(unsigned int us) {
164 #if defined(HAVE_CRAYXT)
165  double secs = us*1e-6;
166  double start = cpu_time();
167  while (cpu_time()-start < secs) {
168  for (int i=0; i<100; ++i) cpu_relax();
169  }
170 #elif defined(HAVE_IBMBGP) || defined(HAVE_IBMBGQ)
171  int count = BG_CYCLES_PER_MICROSECOND*us;
172  for (int i=0; i<count; i++) {
173  asm volatile ("nop\n");
174  }
175 #else
176  usleep(us);
177 #endif
178  }
179 }
180 
181 
182 #endif // MADNESS_WORLD_WORLDTIME_H__INCLUDED
void cpu_relax()
Do nothing and especially do not touch memory.
Definition: worldtime.h:141
FLOAT a(int j, FLOAT z)
Definition: y1.cc:86
double cpu_frequency()
Estimates frequency of the processor in Hz.
Definition: world.cc:279
Holds machinery to set up Functions/FuncImpls using various Factories and Interfaces.
Definition: chem/atomutil.cc:45
double wall_time()
Returns the wall time in seconds relative to arbitrary origin.
Definition: world.cc:248