Commit 5fb0e43e authored by berenger-bramas's avatar berenger-bramas

Add a simple memory profile, but there is a link problem.

Also add the compilation option to use it or not.

git-svn-id: svn+ssh://scm.gforge.inria.fr/svn/scalfmm/scalfmm/trunk@271 2616d619-271b-44dc-8df4-d4a8f33a7222
parent 5ce7fdfa
......@@ -13,6 +13,8 @@ OPTION( SCALFMM_USE_TRACE "Set to ON to print trace or use itac trace" OFF )
OPTION( SCALFMM_BUILD_TESTS "Set to ON to build fonctionnalities Tests" ON )
OPTION( SCALFMM_BUILD_UTESTS "Set to ON to build UTests" ON )
OPTION( SCALFMM_BUILD_DEBUG "Set to ON to build in Debug" OFF )
OPTION( SCALFMM_USE_MEM_STATS "Set to ON to profile memory" ON )
# MPI option has to be set before project
if( SCALFMM_USE_MPI )
......@@ -70,5 +72,8 @@ if( SCALFMM_BUILD_UTESTS )
add_subdirectory(UTests)
endif()
# Use Mem stats
MESSAGE( STATUS "SCALFMM_USE_MEM_STATS = ${SCALFMM_USE_MEM_STATS}" )
# Build - doc
add_subdirectory(Doc)
......@@ -21,4 +21,10 @@
#cmakedefine SCALFMM_USE_TRACE
#cmakedefine SCALFMM_USE_ITAC
///////////////////////////////////////////////////////
// Memory trace
///////////////////////////////////////////////////////
#cmakedefine SCALFMM_USE_MEM_STATS
#endif // CONFIG_H
......@@ -2,6 +2,17 @@
#define FGLOBAL_HPP
#include "ScalFmmConfig.h"
///////////////////////////////////////////////////////
// Memory profiling
///////////////////////////////////////////////////////
#include "FMemStats.h"
///////////////////////////////////////////////////////
// Stdlib
///////////////////////////////////////////////////////
#include <stdlib.h>
///////////////////////////////////////////////////////
......
#include "FMemStats.h"
FMemStats FMemStats::controler;
#ifdef SCALFMM_USE_MEM_STATS
// Regular scalar new
void* operator new(std::size_t n) throw(std::bad_alloc)
{
using namespace std;
for (;;) {
void* allocated_memory = ::operator new(n, nothrow);
if (allocated_memory != 0){
return allocated_memory;
}
// Store the global new handler
new_handler global_handler = set_new_handler(0);
set_new_handler(global_handler);
if (global_handler) {
global_handler();
} else {
throw bad_alloc();
}
}
}
// Nothrow scalar new
void* operator new(size_t n, std::nothrow_t const&) throw()
{
//if (n == 0) n = 1;
void* const allocated = malloc(n + 8);
if(allocated){
*(static_cast<size_t*>(allocated)) = n;
FMemStats::controler.allocate(n);
return static_cast<unsigned char*>(allocated) + 8;
}
return allocated;
}
// Regular array new
void* operator new[](size_t n) throw(std::bad_alloc)
{
return ::operator new(n);
}
// Nothrow array new
void* operator new[](size_t n, std::nothrow_t const&) throw()
{
return ::operator new(n, std::nothrow);
}
// Regular scalar delete
void operator delete(void* p) throw(){
if(p){
FMemStats::controler.deallocate( *(reinterpret_cast<size_t*>(static_cast<unsigned char*>(p) - 8)) );
free(reinterpret_cast<unsigned char*>(p) - 8);
}
}
// Nothrow scalar delete
void operator delete(void* p, std::nothrow_t const&) throw(){
::operator delete(p);
}
// Regular array delete
void operator delete[](void* p) throw()
{
::operator delete(p);
}
// Nothrow array delete
void operator delete[](void* p, std::nothrow_t const&) throw()
{
::operator delete(p);
}
#endif
#ifndef FMEMSTATS_H
#define FMEMSTATS_H
#include <new>
#include <stdexcept>
#include <stdlib.h>
#include <cstdio>
#ifdef SCALFMM_USE_MEM_STATS
void* operator new(std::size_t n) throw(std::bad_alloc);
void* operator new(size_t n, std::nothrow_t const&) throw();
void* operator new[](size_t n) throw(std::bad_alloc);
void* operator new[](size_t n, std::nothrow_t const&) throw();
void operator delete(void* p) throw();
void operator delete(void* p, std::nothrow_t const&) throw();
void operator delete[](void* p) throw();
void operator delete[](void* p, std::nothrow_t const&) throw();
#endif
class FMemStats {
private:
unsigned long long maxAllocated;
unsigned long long totalAllocated;
std::size_t currentAllocated;
FMemStats()
: maxAllocated(0), totalAllocated(0), currentAllocated(0) {
}
void allocate(const std::size_t size){
currentAllocated += size;
totalAllocated += size;
if(maxAllocated < currentAllocated){
maxAllocated = currentAllocated;
}
}
void deallocate(const std::size_t size){
currentAllocated -= size;
}
friend void* operator new(std::size_t n) throw(std::bad_alloc);
friend void* operator new(size_t n, std::nothrow_t const&) throw();
friend void* operator new[](size_t n) throw(std::bad_alloc);
friend void* operator new[](size_t n, std::nothrow_t const&) throw();
friend void operator delete(void* p) throw();
friend void operator delete(void* p, std::nothrow_t const&) throw();
friend void operator delete[](void* p) throw();
friend void operator delete[](void* p, std::nothrow_t const&) throw();
public:
static FMemStats controler;
unsigned long long getMaxAllocated() const{
return maxAllocated;
}
unsigned long long getTotalAllocated() const{
return totalAllocated;
}
std::size_t getCurrentAllocated() const{
return currentAllocated;
}
};
#endif // FMEMSTATS_H
......@@ -7,6 +7,7 @@
#include <time.h>
#include "../Src/Utils/FTic.hpp"
#include "../Src/Utils/FParameters.hpp"
#include "../Src/Containers/FOctree.hpp"
#include "../Src/Containers/FVector.hpp"
......@@ -69,7 +70,8 @@ int main(int argc, char ** argv){
{
// otree
OctreeClass tree(10, 3,loader.getBoxWidth(),loader.getCenterOfBox());
OctreeClass tree(FParameters::getValue(argc,argv,"-h", 9), FParameters::getValue(argc,argv,"-sh", 3),
loader.getBoxWidth(), loader.getCenterOfBox());
// -----------------------------------------------------
std::cout << "Inserting " << loader.getNumberOfParticles() << " particles ..." << std::endl;
......
......@@ -68,7 +68,8 @@ int main(int argc, char ** argv ){
{
// otree
OctreeClass tree(10, 3,loader.getBoxWidth(),loader.getCenterOfBox());
OctreeClass tree(FParameters::getValue(argc,argv,"-h", 9), FParameters::getValue(argc,argv,"-sh", 3),
loader.getBoxWidth(), loader.getCenterOfBox());
// -----------------------------------------------------
std::cout << "Inserting " << loader.getNumberOfParticles() << " particles ..." << std::endl;
......
// /!\ Please, you must read the license at the bottom of this page
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include "../Src/Utils/FParameters.hpp"
#include "../Src/Utils/FTic.hpp"
#include "../Src/Containers/FOctree.hpp"
#include "../Src/Containers/FVector.hpp"
#include "../Src/Components/FSimpleLeaf.hpp"
#include "../Src/Utils/F3DPosition.hpp"
#include "../Src/Components/FTestParticle.hpp"
#include "../Src/Components/FTestCell.hpp"
#include "../Src/Components/FTestKernels.hpp"
#include "../Src/Core/FFmmAlgorithm.hpp"
// Compile by : g++ testFmmAlgorithm.cpp ../Src/Utils/FDebug.cpp ../Src/Utils/FTrace.cpp -lgomp -fopenmp -O2 -o testFmmAlgorithm.exe
/** This program show an example of use of
* the fmm basic algo
* it also check that each particles is impacted each other particles
*/
// Simply create particles and try the kernels
int main(int argc, char ** argv){
typedef FTestParticle ParticleClass;
typedef FTestCell CellClass;
typedef FVector<ParticleClass> ContainerClass;
typedef FSimpleLeaf<ParticleClass, ContainerClass > LeafClass;
typedef FOctree<ParticleClass, CellClass, ContainerClass , LeafClass > OctreeClass;
typedef FTestKernels<ParticleClass, CellClass, ContainerClass > KernelClass;
typedef FFmmAlgorithm<OctreeClass, ParticleClass, CellClass, ContainerClass, KernelClass, LeafClass > FmmClass;
///////////////////////What we do/////////////////////////////
std::cout << ">> This executable has to be used to test the FMM algorithm.\n";
//////////////////////////////////////////////////////////////
const int NbLevels = FParameters::getValue(argc,argv,"-h", 9);
const int SizeSubLevels = FParameters::getValue(argc,argv,"-sh", 3);
const long NbPart = FParameters::getValue(argc,argv,"-nb", 2000000);
const FReal FRandMax = FReal(RAND_MAX);
FTic counter;
srand ( 1 ); // volontary set seed to constant
//////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////
OctreeClass tree(NbLevels, SizeSubLevels, 1.0, F3DPosition(0.5,0.5,0.5));
//////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////
std::cout << "Creating & Inserting " << NbPart << " particles ..." << std::endl;
std::cout << "\tHeight : " << NbLevels << " \t sub-height : " << SizeSubLevels << std::endl;
counter.tic();
{
FTestParticle particleToFill;
for(int idxPart = 0 ; idxPart < NbPart ; ++idxPart){
particleToFill.setPosition(FReal(rand())/FRandMax,FReal(rand())/FRandMax,FReal(rand())/FRandMax);
tree.insert(particleToFill);
}
}
counter.tac();
std::cout << "Done " << "(@Creating and Inserting Particles = " << counter.elapsed() << "s)." << std::endl;
//////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////
std::cout << "Working on particles ..." << std::endl;
counter.tic();
// FTestKernels FBasicKernels
KernelClass kernels;
FmmClass algo(&tree,&kernels);
algo.execute();
counter.tac();
std::cout << "Done " << "(@Algorithm = " << counter.elapsed() << "s)." << std::endl;
//////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////
ValidateFMMAlgo<OctreeClass, ParticleClass, CellClass, ContainerClass, LeafClass>(&tree);
//////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////
std::cout << "Memory used at the end " << FMemStats::controler.getCurrentAllocated() << " Bytes\n";
std::cout << "Max memory used " << FMemStats::controler.getMaxAllocated() << " Bytes\n";
std::cout << "Total memory used " << FMemStats::controler.getTotalAllocated() << " Bytes\n";
return 0;
}
// [--LICENSE--]
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