From bc6cb23d47aacc8f5f8684f21437efb98e748b81 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bal=C3=A1zs=20Gy=C3=B6ngy=C3=B6si?= Date: Tue, 23 Jan 2018 20:10:54 +0100 Subject: [PATCH] early release of the multi threaded Crusher and minor fixes --- BM1D.cc | 7 ++- BM1D.cc_ | 2 +- include/Draw2D.hh | 12 ++++- include/Draw2DWorkerThread.hh | 71 +++++++++++++++++++++++++ src/Draw2D.cc | 98 ++++++++++++++++++++++------------- src/Draw2DWorkerThread.cc | 67 ++++++++++++++++++++++++ src/Lattice.cc | 2 +- 7 files changed, 219 insertions(+), 40 deletions(-) create mode 100644 include/Draw2DWorkerThread.hh create mode 100644 src/Draw2DWorkerThread.cc diff --git a/BM1D.cc b/BM1D.cc index efca86b..db0ebe4 100644 --- a/BM1D.cc +++ b/BM1D.cc @@ -71,7 +71,7 @@ int main(int argc, char* argv[]) rat=0.9; percent = 0.99; - nGenerated = 8000; + nGenerated = 4000; nop = 30; vis = 1; @@ -114,7 +114,10 @@ int main(int argc, char* argv[]) Plotter* myPlotter = new Plotter(vis==1); myPlotter->Plot(nRuns, nSteps, myBM1DProcess->GetT(), myBM1DProcess->GetX()); - Draw2D *myDraw2D = new Draw2D(nop, percent, nGenerated, myBM1DProcess->GetT(), myBM1DProcess->GetX()); //nop percent nruns + //int numCPU = sysconf(_SC_NPROCESSORS_ONLN); + //std::cout <<"cpus:"<GetT(), myBM1DProcess->GetX(),2); //nop percent nruns switch(random_type){ case 'g' : diff --git a/BM1D.cc_ b/BM1D.cc_ index 7ae7687..4bac8df 100644 --- a/BM1D.cc_ +++ b/BM1D.cc_ @@ -39,7 +39,7 @@ int main(int argc, char* argv[]) { //default runs with less parameters random_type = 'g'; - nSteps = 1000; + nSteps = 100; nRuns = 1; p0 = 0.5; p1 = 0; diff --git a/include/Draw2D.hh b/include/Draw2D.hh index b6d237b..e40f77d 100644 --- a/include/Draw2D.hh +++ b/include/Draw2D.hh @@ -6,21 +6,27 @@ #include #include +#include + #include "Analyse.hh" #include "Lattice.hh" #include "Crusher.hh" #include "Plotter.hh" +#include "Draw2DWorkerThread.hh" #include "TH2.h" #include "TCanvas.h" #include "TGraph.h" + + + class Draw2D { public: - Draw2D(int nop, double percent, int nRunsp, std::vector tp, std::vector xp); + Draw2D(int nop, double percent, int nRunsp, std::vector tp, std::vector xp, int nThreads_); ~Draw2D(); void Histo2D(); @@ -30,6 +36,8 @@ class Draw2D std::vector x; int nRuns; + int nThreads; + Analyse *myAnalyse; Crusher *myCrusher; Lattice *myLattice; @@ -39,5 +47,7 @@ class Draw2D TH2D *BM1D_histo2; TGraph *gr; + struct graphArray graphArray; + }; \ No newline at end of file diff --git a/include/Draw2DWorkerThread.hh b/include/Draw2DWorkerThread.hh new file mode 100644 index 0000000..7d2d8a5 --- /dev/null +++ b/include/Draw2DWorkerThread.hh @@ -0,0 +1,71 @@ +#pragma once + +#include +#include +#include +#include +#include + +#include + +#include "Lattice.hh" +#include "Crusher.hh" + +#include "TH2.h" +#include "TCanvas.h" +#include "TGraph.h" + +typedef void * (*THREADFUNCPTR)(void *); + +struct graphArray + { + double *X; + double *Y; + int size; +}; + +class Draw2DWorkerThread +{ +public: + + Draw2DWorkerThread(int jobOffset_, int jobSize_, std::vector x_, std::vector t_, int nRuns_, Lattice *myLattice_, TH2D *BM1D_histo_, TH2D *BM1D_histo2_, struct graphArray graphArray_, pthread_mutex_t *histoMutex_); + + ~Draw2DWorkerThread(); + + void WorkerFunction(); + + +private: + //output histo pointers + TH2D *BM1D_histo; + TH2D *BM1D_histo2; + + //lattice instance pointer + Lattice *myLattice; + + int nRuns; //Chrusher parameter number of tested "kukac" + + std::vector t; //original "kukac" + std::vector x; + + Crusher *myCrusher; + + pthread_mutex_t *histoMutex; + + //lattice parameters + double XbinSize; + double YbinSize; + double XbinSizePer2; + double YbinSizePer2; + int latticeHeight; + int latticeWidth; + double latticeMuMin; + double latticeSigmaMin; + + int jobOffset; + int jobSize; + + struct graphArray graphArray; + + +}; \ No newline at end of file diff --git a/src/Draw2D.cc b/src/Draw2D.cc index 2d7d0b0..b77e75f 100644 --- a/src/Draw2D.cc +++ b/src/Draw2D.cc @@ -1,17 +1,17 @@ #include "Draw2D.hh" -Draw2D::Draw2D(int nop, double percent, int nRunsp, std::vector tp, std::vector xp) //int nop, double percent, int nRunsp, std::vector tp, std::vector xp + +Draw2D::Draw2D(int nop, double percent, int nRunsp, std::vector tp, std::vector xp, int nThreads_) //int nop, double percent, int nRunsp, std::vector tp, std::vector xp { t = tp; x = xp; nRuns = nRunsp; - + nThreads = nThreads_; + myAnalyse = new Analyse(); myAnalyse -> AnalyseGaus(t,x); - - myCrusher = new Crusher(t,x); - + myLattice = new Lattice(); myLattice -> SetNop(nop); @@ -25,7 +25,12 @@ Draw2D::Draw2D(int nop, double percent, int nRunsp, std::vector tp, st BM1D_histo = new TH2D("BM1D_histo", "BM1D_histo", myLattice -> GetWidth() , myLattice -> GetMuMin(), (myLattice -> GetMuMax()) , myLattice -> GetHeight() , myLattice -> GetSigmaMin(), (myLattice -> GetSigmaMax()) ); BM1D_histo2 = new TH2D("BM1D_histo2", "BM1D_histo2", myLattice -> GetWidth() , myLattice -> GetMuMin(), (myLattice -> GetMuMax()) , myLattice -> GetHeight() , myLattice -> GetSigmaMin(), (myLattice -> GetSigmaMax()) ); - gr = new TGraph(); + + graphArray.size = myLattice -> GetLatticeSize(); + graphArray.X = new double[graphArray.size]; + graphArray.Y = new double[graphArray.size]; + + gr = new TGraph(graphArray.size); delete myAnalyse; } @@ -44,47 +49,69 @@ Draw2D::~Draw2D() delete gr; delete myLattice; - delete myCrusher; + + delete[] graphArray.X; + delete[] graphArray.Y; } + + void Draw2D::Histo2D() { - double XbinSize = (myLattice -> GetMuMax() - myLattice -> GetMuMin())/ myLattice -> GetWidth() ; - double YbinSize =( myLattice -> GetSigmaMax() - myLattice -> GetSigmaMin() ) / myLattice -> GetHeight(); - - double graphArrayX[myLattice -> GetLatticeSize()] = {}; - double graphArrayY[myLattice -> GetLatticeSize()] = {}; - std::cout << "maxSigma:" << myLattice -> GetSigmaMax() << std::endl; std::cout << "maxMu:" << myLattice -> GetMuMax() << std::endl; std::cout << "minSigma:" << myLattice -> GetSigmaMin() << std::endl; std::cout << "minMu:" << myLattice -> GetMuMin() << std::endl; - for(int i = 0; i < (myLattice -> GetLatticeSize()); i++) - { - std::cout<<"progress: "< GetLatticeSize()<<"point\n"; - - if(myCrusher -> RunMachine(nRuns, myLattice -> GetMuSigma(i))) - { - BM1D_histo -> Fill((i / myLattice -> GetHeight()) * XbinSize + (XbinSize/2) + myLattice -> GetMuMin(),(i % myLattice -> GetHeight()) * YbinSize + (YbinSize/2) + myLattice -> GetSigmaMin() ); - BM1D_histo2 -> Fill((i / myLattice -> GetHeight()) * XbinSize + (XbinSize/2) + myLattice -> GetMuMin(),(i % myLattice -> GetHeight()) * YbinSize + (YbinSize/2) + myLattice -> GetSigmaMin()); - - graphArrayX[i] = (i / myLattice -> GetHeight()) * XbinSize; - graphArrayY[i] = (i % myLattice -> GetHeight()) * YbinSize; - - } - - } - + pthread_t thread[2]; + Draw2DWorkerThread *worker[2]; + + pthread_attr_t attr; + + pthread_mutex_t histoMutex; + pthread_mutex_init(&histoMutex, NULL); + int rc; + + void *status; + + + + worker[0] = new Draw2DWorkerThread(0, (myLattice -> GetLatticeSize()/2), x, t, nRuns, myLattice, BM1D_histo, BM1D_histo2, graphArray, &histoMutex); + worker[1] = new Draw2DWorkerThread((myLattice -> GetLatticeSize()/2), (myLattice -> GetLatticeSize()/2), x, t, nRuns, myLattice, BM1D_histo, BM1D_histo2, graphArray, &histoMutex); + + pthread_attr_init(&attr); + pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE); + + for(int i=0; i<2; i++) + { + std::cout<<"creating thread:"< GetLatticeSize(),graphArrayX,graphArrayY); - + gr -> SetMarkerColor(46); canvasB2 -> cd(); - //gr -> LineStyle() - gr -> DrawGraph(myLattice -> GetLatticeSize(),graphArrayX,graphArrayY,"AP*"); + gr -> DrawGraph(graphArray.size, graphArray.X, graphArray.Y, "AP*"); BM1D_histo -> SetMarkerStyle(kFullCircle); BM1D_histo-> SetMarkerStyle(21); @@ -94,5 +121,6 @@ void Draw2D::Histo2D() canvasB -> cd(2); BM1D_histo2 -> Draw("COLZ"); - std::cout << "Histo done" << std::endl; -} \ No newline at end of file + std::cout << "Histo draw done" << std::endl; +} + diff --git a/src/Draw2DWorkerThread.cc b/src/Draw2DWorkerThread.cc new file mode 100644 index 0000000..be73203 --- /dev/null +++ b/src/Draw2DWorkerThread.cc @@ -0,0 +1,67 @@ +#include "Draw2DWorkerThread.hh" + +Draw2DWorkerThread::Draw2DWorkerThread(int jobOffset_, int jobSize_, std::vector x_, std::vector t_, int nRuns_, Lattice *myLattice_, TH2D *BM1D_histo_, TH2D *BM1D_histo2_, struct graphArray graphArray_, pthread_mutex_t *histoMutex_) +{ + std::cout << "Draw2DWorkerThread::Draw2DWorkerThread" < GetMuMax() - myLattice -> GetMuMin())/ myLattice -> GetWidth() ; + YbinSize =( myLattice -> GetSigmaMax() - myLattice -> GetSigmaMin() ) / myLattice -> GetHeight(); + XbinSizePer2 = XbinSize/2; + YbinSizePer2 = YbinSize/2; + latticeHeight = myLattice -> GetHeight(); + latticeWidth = myLattice -> GetWidth(); + latticeMuMin = myLattice -> GetMuMin(); + latticeSigmaMin = myLattice -> GetSigmaMin(); +} + + +Draw2DWorkerThread::~Draw2DWorkerThread() +{ + delete myCrusher; +} + + +void Draw2DWorkerThread::WorkerFunction() +{ + std::cout << "Draw2DWorkerThread::WorkerFunction" < GetLatticeSize()<<"point\n"; + + if(myCrusher -> RunMachine(nRuns, myLattice -> GetMuSigma(i))) + { + pthread_mutex_lock (histoMutex); //fill histo thread safe??? + + BM1D_histo ->Fill((i / latticeHeight) * XbinSize + XbinSizePer2 + latticeMuMin, (i % latticeHeight) * YbinSize + YbinSizePer2 + latticeSigmaMin ); + BM1D_histo2 ->Fill((i / latticeHeight) * XbinSize + XbinSizePer2 + latticeMuMin, (i % latticeHeight) * YbinSize + YbinSizePer2 + latticeSigmaMin ); + + graphArray.X[i] = (i / latticeHeight) * XbinSize; + graphArray.Y[i] = (i % latticeHeight)* YbinSize; + + pthread_mutex_unlock (histoMutex); + + } + } + pthread_exit((void*) 0); //kell?? vagy NULL?? +} \ No newline at end of file diff --git a/src/Lattice.cc b/src/Lattice.cc index fcfdccc..736e6ee 100644 --- a/src/Lattice.cc +++ b/src/Lattice.cc @@ -105,7 +105,7 @@ void Lattice::SetLattice(Double_t mu_est, Double_t sigma_est){ ms_vect[i].mu = loc_mu; ms_vect[i].sigma = loc_sigma; - std::cout << "i : " << ms_vect[i].mu << " " << ms_vect[i].sigma << std::endl; + //std::cout << "i : " << ms_vect[i].mu << " " << ms_vect[i].sigma << std::endl; i++; loc_sigma += ds;