File: IpUtils.cpp

package info (click to toggle)
coinor-ipopt 3.11.9-2.1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 24,616 kB
  • sloc: cpp: 75,373; sh: 11,170; ansic: 2,798; makefile: 2,247; java: 632; fortran: 342
file content (313 lines) | stat: -rw-r--r-- 6,452 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
// Copyright (C) 2005, 2010 International Business Machines and others.
// All Rights Reserved.
// This code is published under the Eclipse Public License.
//
// $Id: IpUtils.cpp 2500 2014-08-16 15:31:35Z stefan $
//
// Authors:  Carl Laird, Andreas Waechter    IBM       2005-08-12

#include "IpoptConfig.h"
#include "IpUtils.hpp"

#ifdef HAVE_CSTDLIB
# include <cstdlib>
#else
# ifdef HAVE_STDLIB_H
#  include <stdlib.h>
# endif
#endif

#ifdef HAVE_CMATH
# include <cmath>
#else
# ifdef HAVE_MATH_H
#  include <math.h>
# else
#  error "don't have header file for math"
# endif
#endif

#ifdef HAVE_CFLOAT
# include <cfloat>
#else
# ifdef HAVE_FLOAT_H
#  include <float.h>
# endif
#endif

#ifdef HAVE_CIEEEFP
# include <cieeefp>
#else
# ifdef HAVE_IEEEFP_H
#  include <ieeefp.h>
# endif
#endif

#ifdef HAVE_CTIME
# include <ctime>
#else
# ifdef HAVE_TIME_H
#  include <time.h>
# else
#  error "don't have header file for time"
# endif
#endif

#ifdef HAVE_CSTDIO
# include <cstdio>
//  The special treatment of vsnprintf on SUN has been suggsted by Lou
//  Hafer 2010/07/04
# if defined(HAVE_VSNPRINTF) && defined(__SUNPRO_CC)
namespace std
{
#  include <iso/stdio_c99.h>
}
# endif
#else
# ifdef HAVE_STDIO_H
#  include <stdio.h>
# else
#  error "don't have header file for stdio"
# endif
#endif

#ifdef HAVE_CSTDARG
# include <cstdarg>
#else
# ifdef HAVE_STDARG_H
#  include <stdarg.h>
# else
#  error "don't have header file for stdarg"
# endif
#endif

#include <limits>

// The following code has been copied from CoinUtils' CoinTime

/** 8< (BEGIN) ******************************** */

#if defined(_MSC_VER)
// Turn off compiler warning about long names
#  pragma warning(disable:4786)
#else
// MacOS-X and FreeBSD needs sys/time.h
#if defined(__MACH__) || defined (__FreeBSD__)
#include <sys/time.h>
#endif
#if !defined(__MSVCRT__)
#include <sys/resource.h>
#endif
#endif

//#############################################################################

#if defined(_MSC_VER)

#if 0 // change this to 1 if want to use the win32 API
#include <windows.h>
#ifdef small
/* for some unfathomable reason (to me) rpcndr.h (pulled in by windows.h) does a
   '#define small char' */
#undef small
#endif
#define TWO_TO_THE_THIRTYTWO 4294967296.0
#define DELTA_EPOCH_IN_SECS  11644473600.0
inline double IpCoinGetTimeOfDay()
{
  FILETIME ft;

  GetSystemTimeAsFileTime(&ft);
  double t = ft.dwHighDateTime * TWO_TO_THE_THIRTYTWO + ft.dwLowDateTime;
  t = t/10000000.0 - DELTA_EPOCH_IN_SECS;
  return t;
}
#else
#include <sys/types.h>
#include <sys/timeb.h>
inline double IpCoinGetTimeOfDay()
{
  struct _timeb timebuffer;
#pragma warning(disable:4996)
  _ftime( &timebuffer ); // C4996
#pragma warning(default:4996)
  return timebuffer.time + timebuffer.millitm/1000.0;
}
#endif

#else

#include <sys/time.h>

inline double IpCoinGetTimeOfDay()
{
  struct timeval tv;
  gettimeofday(&tv, NULL);
  return static_cast<double>(tv.tv_sec) + static_cast<double>(tv.tv_usec)/1000000.0;
}

#endif // _MSC_VER

/** 8< (END) ******************************** */

namespace Ipopt
{

  bool IsFiniteNumber(Number val)
  {
#ifdef COIN_C_FINITE
    return (bool)COIN_C_FINITE(val);
#else

    return true;
#endif

  }

  Number IpRandom01()
  {
#ifdef HAVE_DRAND48
    return Number(drand48());
#else
# ifdef HAVE_RAND
    return Number(rand())/Number(RAND_MAX);
# else
#  ifdef HAVE_STD__RAND
    return Number(std::rand())/Number(RAND_MAX);
#  else
    /* this is a workaround for gcc 4.8.3, for which the test for rand() that our old configure sets up does not work */
#   if defined(__GNUC__) && __GNUC__ == 4 && __GNUC_MINOR__ == 8 && __GNUC_PATCHLEVEL__ == 3
      return Number(rand())/Number(RAND_MAX);
#   else
#    error "don't have function for random number generator"
#   endif
#  endif
# endif
#endif
  }

  void IpResetRandom01()
  {
#ifdef HAVE_DRAND48
    srand48(1);
#else
# ifdef HAVE_RAND
    srand(1);
# else
#  ifdef HAVE_STD__RAND
    std::srand(1);
#  else
#   if defined(__GNUC__) && __GNUC__ == 4 && __GNUC_MINOR__ == 8 && __GNUC_PATCHLEVEL__ == 3
    srand(1);
#   else
#    error "don't have function for random number generator"
#   endif
#  endif
# endif
#endif
  }


  static double Wallclock_firstCall_ = -1.;

  // The following function were taken from CoinTime.hpp in COIN/Coin
  Number CpuTime()
  {
    double cpu_temp;
#if defined(_MSC_VER) || defined(__MSVCRT__)

    unsigned int ticksnow;        /* clock_t is same as int */

    ticksnow = (unsigned int)clock();

    cpu_temp = (double)((double)ticksnow/CLOCKS_PER_SEC);
#else

    struct rusage usage;
    getrusage(RUSAGE_SELF,&usage);
    cpu_temp = (double)usage.ru_utime.tv_sec;
    cpu_temp += 1.0e-6*((double) usage.ru_utime.tv_usec);
#endif

    return cpu_temp;
  }

  Number SysTime()
  {
    double sys_temp;
#if defined(_MSC_VER) || defined(__MSVCRT__)

    // not yet implemented for Windows
    sys_temp = 0.;
#else

    struct rusage usage;
    getrusage(RUSAGE_SELF,&usage);
    sys_temp = (double)usage.ru_stime.tv_sec;
    sys_temp += 1.0e-6*((double) usage.ru_stime.tv_usec);
#endif

    return sys_temp;
  }

  Number WallclockTime()
  {
    double callTime = IpCoinGetTimeOfDay();
    if (Wallclock_firstCall_ == -1.) {
      Wallclock_firstCall_ = callTime;
    }
    return callTime - Wallclock_firstCall_;
  }

  bool Compare_le(Number lhs, Number rhs, Number BasVal)
  {
    Number mach_eps = std::numeric_limits<Number>::epsilon();
    return (lhs - rhs <= 10.*mach_eps*fabs(BasVal));
  }

  int Snprintf(char* str, long size, const char* format, ...)
  {
#if defined(HAVE_VSNPRINTF) && defined(__SUNPRO_CC)
    std::va_list ap;
#else
    va_list ap;
#endif
    va_start(ap, format);
    int ret;
#ifdef HAVE_VA_COPY
    va_list apcopy;
    va_copy(apcopy, ap);
# ifdef HAVE_VSNPRINTF
#  ifdef __SUNPRO_CC
    ret = std::vsnprintf(str, size, format, apcopy);
#  else
    ret = vsnprintf(str, size, format, apcopy);
#  endif
# else
#  ifdef HAVE__VSNPRINTF
    ret = _vsnprintf(str, size, format, apcopy);
#  else
    ret = vsprintf(str, format, apcopy);
#  endif
    va_end(apcopy);
# endif
#else
# ifdef HAVE_VSNPRINTF
#  ifdef __SUNPRO_CC
    ret = std::vsnprintf(str, size, format, ap);
#  else
    ret = vsnprintf(str, size, format, ap);
#  endif
# else
#  ifdef HAVE__VSNPRINTF
    ret = _vsnprintf(str, size, format, ap);
#  else
    ret = vsprintf(str, format, ap);
#  endif
# endif
#endif
    va_end(ap);
    return ret;
  }

} //namespace Ipopt