-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_tbb.cpp
More file actions
153 lines (119 loc) · 3.02 KB
/
test_tbb.cpp
File metadata and controls
153 lines (119 loc) · 3.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include <string>
#include <cstddef>
#include <vector>
#include <iostream>
#include <stdlib.h>
#include <iomanip>
#include <unistd.h>
#include <algorithm>
#include "TH1D.h"
#include "TChain.h"
#include "TH2D.h"
#include "TFile.h"
#include "TTree.h"
#include "TBranch.h"
#include "TLeaf.h"
#include "mpi.h"
#include "tbb/tbb.h"
#include "tbb/parallel_for_each.h"
#include "tbb/task_scheduler_init.h"
#include<nlohmann/json.hpp>
struct mytask{
mytask(size_t n)
:_n(n)
{}
void operator()(){
for (int i=0;i<100000;i++){}
std::cerr<<"["<<_n<<"]"<<std::endl;
}
size_t _n;
};
struct executor
{
executor(std::vector<mytask>&t)
:_tasks(t)
{}
executor(executor& e, tbb::split)
:_tasks(e._tasks)
{}
void operator()(const tbb::blocked_range<size_t>&r)const{
for(size_t i=r.begin();i!=r.end();++i)
_tasks[i]();
}
std::vector<mytask>& _tasks;
};
struct read_entries{
read_entries(double n,std::string _name)
:_n(n),name(_name)
{ std::cout<<" Value "<<_n<<" "<<name<<" "<<
tbb::task_arena::current_thread_index()<<
" "<<std::endl;
}
void operator()(){
}
double _n;
std::string name;
};
struct executor_entries
{
executor_entries(std::vector<read_entries>&t)
:_tasks(t)
{}
executor_entries(executor_entries& e, tbb::split)
:_tasks(e._tasks)
{}
void operator()(const tbb::blocked_range<size_t>&r)const{
for(size_t i=r.begin();i!=r.end();++i)
_tasks[i]();
}
std::vector<read_entries>& _tasks;
};
void test_tbb(){
tbb::task_scheduler_init init; // Automatic number of threads
// Uncomment below if explicit number of threads...
// tbb:task_scheduler_init init(2);
std::vector<mytask>tasks;
for(int i=0;i<1000;++i)
tasks.push_back(mytask(i));
executor exec(tasks);
tbb::parallel_for(tbb::blocked_range<size_t>(0,tasks.size()),exec);
std::cerr<<std::endl;
}
void ReadEntries(TTree *tree){
tbb::task_scheduler_init init(2);
auto l = tree->GetListOfBranches();
auto nentries = tree->GetEntriesFast();
nentries =10;
auto tot_branches = l->GetEntriesFast();
int _counter=0;
std::vector<read_entries>tasks;
for(int i = 0;i<nentries;i++){
for(Long64_t jentry=0;jentry<tot_branches;++jentry){
tree->GetEntry(jentry);
auto branch = dynamic_cast<TBranch*>((*l)[jentry]);
std::string branch_name = branch->GetName();
auto leaves = branch->GetListOfLeaves();
auto leaf = dynamic_cast<TLeaf*>((*leaves)[0]);
auto _val = leaf->GetValue(0);
tasks.push_back(read_entries(_val,branch_name));
if(_counter==5){
executor_entries exec(tasks);
tbb::parallel_for(
tbb::blocked_range<size_t>(0,tasks.size()),exec);
_counter=0;
tasks.clear();
}
_counter++;
}
}
}
int main(int argc, char* argv[]){
auto input_file = argv[1];
std::string ntuple_name = "ccqe_data";
auto f = TFile::Open(input_file);
auto e = (TTree*)f->Get(ntuple_name.c_str());
ReadEntries(e);
// test_tbb();
std::cout<<" END OF THE CODE "<<std::endl;
return 1;
}