Skip to content

Instantly share code, notes, and snippets.

@eguiraud
Created September 9, 2015 12:24
Show Gist options
  • Save eguiraud/c1556ff6b5f5ff1b327b to your computer and use it in GitHub Desktop.
Save eguiraud/c1556ff6b5f5ff1b327b to your computer and use it in GitHub Desktop.
test and benchmarks for ROOT/core/multiproc
#include "TPool.h"
#include "TH1F.h"
#include "TRandom3.h"
#include "TStopwatch.h"
#include "TApplication.h"
#include <iostream>
#include <vector>
TObject *FillHisto(unsigned long long nEvents) {
TH1F *h = new TH1F("h","h",100,-3,3);
TRandom3 *random = new TRandom3(0);
for(unsigned long long i=0; i<nEvents; ++i)
h->Fill(random->Gaus(0.,1.));
return h;
}
void PoolBenchmark(unsigned long long NFills = 10000000) {
TStopwatch sw;
for(unsigned nWorkers = 1; nWorkers < 6; ++nWorkers) {
for(unsigned count = 1; count < 10; ++count) {
std::cout << nWorkers << " " << std::flush;
TPool pool(nWorkers);
std::vector<unsigned long long> args(nWorkers, NFills/nWorkers);
args[0] = NFills - (nWorkers-1)*args[0];
sw.Start();
auto res = pool.Map(FillHisto, args);
sw.Stop();
std::cout << sw.RealTime() << std::endl;
for(auto& x : res)
delete x;
}
}
}
int main(int argc, char** argv) {
if(argc < 2) {
std::cerr << "usage: " << argv[0] << " <nfills>\n";
return 1;
}
unsigned long long nfills = std::stoull(argv[1]);
TApplication app("app",nullptr,nullptr);
PoolBenchmark(nfills);
return 0;
}
#include "TPool.h"
#include "TH1F.h"
#include "TRandom3.h"
#include "TStopwatch.h"
#include <iostream>
#include <vector>
TObject *FillHisto(unsigned long long nEvents) {
TH1F *h = new TH1F("h","h",100,-3,3);
TRandom3 *random = new TRandom3(0);
for(unsigned long long i=0; i<nEvents; ++i)
h->Fill(random->Gaus(0.,1.));
return h;
}
void PoolBenchmark(unsigned long long NFills = 10000000) {
TStopwatch sw;
for(unsigned nWorkers = 1; nWorkers < 5; ++nWorkers) {
for(unsigned count = 1; count < 10; ++count) {
std::cout << nWorkers << " " << std::flush;
TPool pool(nWorkers);
std::vector<unsigned long long> args(nWorkers, NFills/nWorkers);
args[0] = NFills - (nWorkers-1)*args[0];
sw.Start();
auto res = pool.Map(FillHisto, args);
sw.Stop();
std::cout << sw.RealTime() << std::endl;
for(auto& x : res)
delete x;
}
}
}
#include "TApplication.h"
#include "TH1F.h"
#include "TPool.h"
#include "TList.h"
#include <functional>
#include <list>
#include <vector>
#include <numeric> //accumulate
#include <iostream>
int f(int a)
{
return a+1;
}
class fClass {
public:
int operator()(int a)
{
return a+1;
}
};
TObject *rootF(TObject *o)
{
TH1F *h = (TH1F*)o;
h->FillRandom("gaus", 1);
return h;
}
int PoolTest() {
TPool pool;
std::vector<int> vargs = {0,0,0,0};
std::list<int> largs = {0,0,0,0};
TList *tlargs = new TList;
tlargs->Add(new TH1F("h1","h",100,-3,3));
tlargs->Add(new TH1F("h2","h",100,-3,3));
fClass c;
auto boundF = std::bind(f, 1);
/**** TPool::Map ****/
std::vector<int> truth = {1,1,1,1};
// init list and lambda
auto res = pool.Map([](int a) -> int { return a+1; }, {0,0,0,0});
if( res != truth)
return 1;
// vector and C++ function
auto res2 = pool.Map(f, vargs);
if(res2 != truth)
return 2;
// std::list and functor class
auto res3 = pool.Map(c, largs);
if(res3 != truth)
return 3;
// TList
auto res4 = pool.Map(rootF, tlargs);
if(res4.GetEntries() != 2 || ((TH1F*)res4[0])->GetEntries() != 1 || ((TH1F*)res4[1])->GetEntries() != 1)
return 4;
res4.Delete();
//nTimes signature and bound function
auto res6 = pool.Map(boundF, 100);
if(res6 != std::vector<int>(100,2))
return 6;
/**** TPool::MapReduce ****/
int redtruth = 4;
auto redfunc = [](std::vector<int> a) -> int { return std::accumulate(a.begin(), a.end(), 0); };
// init list and lambda
auto redres = pool.MapReduce([](int a) { return a+1; }, {0,0,0,0}, redfunc);
if(redres != redtruth)
return 8;
// vector and C++ function
auto redres2 = pool.MapReduce(f, vargs, redfunc);
if(redres2 != redtruth)
return 9;
// std::list and functor class
auto redres3 = pool.MapReduce(c, largs, redfunc);
if(redres3 != redtruth)
return 10;
// TList
TH1F* redres4 = static_cast<TH1F*>(pool.MapReduce(rootF, tlargs, PoolUtils::ReduceObjects));
if(redres4->GetEntries() != 2)
return 11;
delete redres4;
//nTimes signature and bound function
auto redres6 = pool.MapReduce(boundF, 100, redfunc);
if(redres6 != 200)
return 12;
/***** other tests *****/
//returning a c-string
auto extrares1 = pool.Map([]() { return "42"; }, 25);
for(auto c_str : extrares1)
if(strcmp(c_str, "42") != 0)
return 13;
for(auto c_str : extrares1)
delete [] c_str;
//returning a string
auto extrares2 = pool.Map([]() { return std::string("fortytwo"); }, 25);
for(auto str : extrares2)
if(str != "fortytwo")
return 14;
tlargs->Delete();
return 0;
}
int main() {
TApplication app("app",nullptr,nullptr);
return PoolTest();
}
#include "TH1F.h"
#include "TPool.h"
#include "TList.h"
#include <functional>
#include <list>
#include <vector>
#include <numeric> //accumulate
#include <iostream>
int f(int a)
{
return a+1;
}
class fClass {
public:
int operator()(int a)
{
return a+1;
}
};
TObject *rootF(TObject *o)
{
TH1F *h = (TH1F*)o;
h->FillRandom("gaus", 1);
return h;
}
int PoolTest() {
TPool pool;
std::vector<int> vargs = {0,0,0,0};
std::list<int> largs = {0,0,0,0};
TList *tlargs = new TList;
tlargs->Add(new TH1F("h1","h",100,-3,3));
tlargs->Add(new TH1F("h2","h",100,-3,3));
fClass c;
auto boundF = std::bind(f, 1);
/**** TPool::Map ****/
std::vector<int> truth = {1,1,1,1};
// init list and lambda
auto res = pool.Map([](int a) -> int { return a+1; }, {0,0,0,0});
if( res != truth)
return 1;
// vector and C++ function
auto res2 = pool.Map(f, vargs);
if(res2 != truth)
return 2;
// std::list and functor class
auto res3 = pool.Map(c, largs);
if(res3 != truth)
return 3;
// TList
auto res4 = pool.Map(rootF, tlargs);
if(res4.GetEntries() != 2 || ((TH1F*)res4[0])->GetEntries() != 1 || ((TH1F*)res4[1])->GetEntries() != 1)
return 4;
res4.Delete();
//nTimes signature and bound function
auto res6 = pool.Map(boundF, 100);
if(res6 != std::vector<int>(100,2))
return 6;
/**** TPool::MapReduce ****/
int redtruth = 4;
auto redfunc = [](std::vector<int> a) -> int { return std::accumulate(a.begin(), a.end(), 0); };
// init list and lambda
auto redres = pool.MapReduce([](int a) { return a+1; }, {0,0,0,0}, redfunc);
if(redres != redtruth)
return 8;
// vector and C++ function
auto redres2 = pool.MapReduce(f, vargs, redfunc);
if(redres2 != redtruth)
return 9;
// std::list and functor class
auto redres3 = pool.MapReduce(c, largs, redfunc);
if(redres3 != redtruth)
return 10;
// TList
TH1F* redres4 = static_cast<TH1F*>(pool.MapReduce(rootF, tlargs, PoolUtils::ReduceObjects));
if(redres4->GetEntries() != 2)
return 11;
delete redres4;
//nTimes signature and bound function
auto redres6 = pool.MapReduce(boundF, 100, redfunc);
if(redres6 != 200)
return 12;
/***** other tests *****/
//returning a c-string
auto extrares1 = pool.Map([]() { return "42"; }, 25);
for(auto c_str : extrares1)
if(strcmp(c_str, "42") != 0)
return 13;
for(auto c_str : extrares1)
delete [] c_str;
//returning a string
auto extrares2 = pool.Map([]() { return std::string("fortytwo"); }, 25);
for(auto str : extrares2)
if(str != "fortytwo")
return 14;
tlargs->Delete();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment