Commit 1ec36b2a authored by Laurent Belcour's avatar Laurent Belcour

Adding function in the core library to acces the memory left on the system.

Then I added a memory checking in the parallel code to check if the program
fits in memory.
parent 419a1851
......@@ -158,3 +158,23 @@ fitter* plugins_manager::get_fitter(const std::string& n) const
return _fitters.find(n)->second ;
}
}
// \todo implement the Darwin (MACOS) version.
#ifdef WIN32
#include <windows.h>
size_t plugins_manager::get_available_memory()
{
MEMORYSTATUSEX status;
status.dwLength = sizeof(status);
GlobalMemoryStatusEx(&status);
return status.ullTotalPhys;
}
#else
#include <unistd.h>
size_t plugins_manager::get_available_memory()
{
long pages = sysconf(_SC_PHYS_PAGES);
long page_size = sysconf(_SC_PAGE_SIZE);
return pages * page_size;
}
#endif
......@@ -32,6 +32,11 @@ class plugins_manager
data* get_data(const std::string& n) const ;
fitter* get_fitter(const std::string& n) const ;
//! \brief Provide a measure of how much free memory is left on the system.
//! \details It permits to know is one can allocate more memory for a fitting
//! procedure for example.
static size_t get_available_memory() ;
private: //data
std::map<std::string, function*> _functions ;
......
#include "rational_fitter.h"
#include <core/plugins_manager.h>
#include <Eigen/SVD>
#include <Array.hh>
#include <QuadProg++.hh>
......@@ -57,6 +59,27 @@ bool rational_fitter_parallel::fit_data(const data* dat, function* fit)
QTime time ;
time.start() ;
// Allocate enough processor to run fully in parallel, but account for
// the fact that each thread will require its own memory.
size_t need_memory = (i+1) * d->size() * 2 * sizeof(double);
size_t avai_memory = plugins_manager::get_available_memory();
int nb_cores = avai_memory / need_memory ;
nb_cores = std::min<int>(nb_cores, omp_get_num_procs());
if(nb_cores == 0)
{
std::cerr << "<<ERROR>> not enough memory to perform the fit" << std::endl ;
#ifndef DEBUG
std::cout << "<<DEBUG>> " << need_memory / 1024 << "MB required / "
<< avai_memory / 1024 << "MB available" << std::endl;
#endif
return false;
}
#ifndef DEBUG
std::cout << "<<DEBUG>> will use " << nb_cores << " threads to compute the quadratic programs" << std::endl ;
#endif
omp_set_num_threads(nb_cores) ;
double min_delta = std::numeric_limits<double>::max();
int nb_sol_found = 0;
int np, nq ;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment