bugfix: Windows compatibility of 'apply' and 'eoEvalUserTimeThrowException'

This commit is contained in:
Loïc Jean David Arjanen 2012-07-16 14:18:22 +02:00 committed by Johann Dreo
commit 6cb15cfecf
3 changed files with 62 additions and 10 deletions

View file

@ -1,4 +1,5 @@
* current version
- fixed compilation issues in Microsoft Visual C++
* release 1.2 (16. May. 2011)
- fixed the incremental allocation issue in variation operators which were

View file

@ -58,14 +58,26 @@ void apply(eoUF<EOT&, void>& _proc, std::vector<EOT>& _pop)
if (!eo::parallel.isDynamic())
{
#pragma omp parallel for if(eo::parallel.isEnabled()) //default(none) shared(_proc, _pop, size)
#ifdef _MSC_VER
//Visual Studio supports only OpenMP version 2.0 in which
//an index variable must be of a signed integral type
for (long long i = 0; i < size; ++i) { _proc(_pop[i]); }
#else // _MSC_VER
for (size_t i = 0; i < size; ++i) { _proc(_pop[i]); }
#endif
}
else
{
#pragma omp parallel for schedule(dynamic) if(eo::parallel.isEnabled())
#ifdef _MSC_VER
//Visual Studio supports only OpenMP version 2.0 in which
//an index variable must be of a signed integral type
for (long long i = 0; i < size; ++i) { _proc(_pop[i]); }
#else // _MSC_VER
//doesnot work with gcc 4.1.2
//default(none) shared(_proc, _pop, size)
for (size_t i = 0; i < size; ++i) { _proc(_pop[i]); }
#endif
}
if ( eo::parallel.enableResults() )

View file

@ -21,27 +21,30 @@ Authors:
Johann Dréo <johann.dreo@thalesgroup.com>
*/
#ifndef __unix__
#warning "Warning: class 'eoEvalUserTimeThrowException' is only available under UNIX systems (defining 'rusage' in 'sys/resource.h'), contributions for other systems are welcomed."
#else
#if !defined(__unix__) && !defined(_WINDOWS)
#warning "Warning: class 'eoEvalUserTimeThrowException' is only available under UNIX (defining 'rusage' in 'sys/resource.h') or Win32 (defining 'GetProcessTimes' in 'WinBase.h') systems, contributions for other systems are welcomed."
#else //!defined(__unix__) && !defined(_WINDOWS)
#ifndef __EOEVALUSERTIMETHROWEXCEPTION_H__
#define __EOEVALUSERTIMETHROWEXCEPTION_H__
#include <sys/time.h>
#include <sys/resource.h>
#include <eoExceptions.h>
/** Check at each evaluation if a given CPU user time contract has been reached.
*
* Throw an eoMaxTimeException if the given max time has been reached.
* Usefull if you want to end the search independently of generations.
* This class uses (almost-)POSIX headers.
* This class uses (almost-)POSIX or Win32 headers, depending on the platform.
* It uses a computation of the user time used on the CPU. For a wallclock time measure, see eoEvalTimeThrowException
*
* @ingroup Evaluation
*/
#include <eoExceptions.h>
#ifdef __unix__
#include <sys/time.h>
#include <sys/resource.h>
template< class EOT >
class eoEvalUserTimeThrowException : public eoEvalFuncCounter< EOT >
{
@ -68,5 +71,41 @@ protected:
struct rusage _usage;
};
#else
#ifdef _WINDOWS
//here _WINDOWS is defined
#include <WinBase.h>
template< class EOT >
class eoEvalUserTimeThrowException : public eoEvalFuncCounter< EOT >
{
public:
eoEvalUserTimeThrowException( eoEvalFunc<EOT> & func, const long max ) : eoEvalFuncCounter<EOT>( func, "CPU-user"), _max(max) {}
virtual void operator() ( EOT & eo )
{
if( eo.invalid() ) {
FILETIME dummy;
GetProcessTimes(GetCurrentProcess(), &dummy, &dummy, &dummy, &_usage);
ULARGE_INTEGER current;
current.LowPart = _usage.dwLowDateTime;
current.HighPart = _usage.dwHighDateTime;
if( current.QuadPart >= _max ) {
throw eoMaxTimeException( current.QuadPart );
} else {
func(eo);
}
}
}
protected:
const long _max;
FILETIME _usage;
};
#endif // _WINDOWS
#endif //__unix__
#endif // __EOEVALUSERTIMETHROWEXCEPTION_H__
#endif // __UNIX__
#endif //!defined(__unix__) && !defined(_WINDOWS)