Hi @gabrielribcesario and @hschindl , thank you for this detail !
Nevertheless, even after pulling last commit, I still have issues when launching my code with OMP_NUM_THREADS
> 1. Errors like these occur.
AskedDensity_mt(87454,0x1f74960c0) malloc: *** error for object 0x600001c37ca0: pointer being freed was not allocated
AskedDensity_mt(87454,0x1f74960c0) malloc: *** set a breakpoint in malloc_error_break to debug
*** Break *** segmentation violation
*** Break *** segmentation violation
*** Break *** segmentation violation
[1] 87454 abort OMP_NUM_THREADS=7 ./AskedDensity_mt --density 9e10
Here is the OpenMP-parallelized part of my code; the full code is in the attached file. I am trying to drift e-, i+, and i- in parallel, but unlike the example, I want to drift them step by step in time in order to update some density maps.
Thanks for your help.
// -- Simulation -- //
// Create buffers
const int T = omp_get_max_threads();
std::vector<std::vector<P>> electrons_b(T);
std::vector<std::vector<P>> ions_b(T);
std::vector<std::vector<P>> negions_b(T);
double t = tStartSimu;
while (t < tEndSimu) {
for (int k=0;k<T;++k) {
electrons_b[k].clear();
ions_b[k].clear();
negions_b[k].clear();
}
// --- Drift e- ------------------------------------------------------
const int nelectrons = electrons.size();
#pragma omp parallel
{
const int tid = omp_get_thread_num();
#pragma omp for reduction(+: nAttaElectrons)
for (int i = 0; i < nelectrons; ++i) {
AvalancheMicroscopic aval;
aval.SetSensor(&sensor);
aval.SetTimeWindow(t, t+dt);
aval.DriftElectron(electrons[i].x, electrons[i].y, electrons[i].z,
electrons[i].t, 0.1, 0., 0., 0., electrons[i].w);
for (const auto& p : aval.GetElectrons()) {
const auto& p1 = p.path.back();
if (p.status == -7) {
nAttaElectrons++;
negions_b[tid].push_back(P{p1.x, p1.y, p1.z, p1.t, p.weight});
}
else electrons_b[tid].push_back(P{p1.x, p1.y, p1.z, p1.t, p.weight});
}
}
}
for (auto& b : negions_b) {
negions.insert(negions.end(), b.begin(), b.end());
}
for (auto& k : negions_b) k.clear();
// --- Drift i- ------------------------------------------------------
const int nnegions = negions.size();
#pragma omp parallel
{
const int tid = omp_get_thread_num();
#pragma omp for reduction(+: nRecombNegions)
for (int i = 0; i < nnegions; ++i) {
AvalancheMC driftnegion;
driftnegion.SetSensor(&sensor);
driftnegion.EnableRecombination(withIonRecombination, alpha);
driftnegion.EnableDiffusion(withIonDiffusion);
driftnegion.EnableDensityMap(withDensityMap);
driftnegion.SetTimeSteps(.2); // [ns] default 0.02
driftnegion.SetTimeWindow(t, t+dt);
driftnegion.DriftNegativeIon(negions[i].x, negions[i].y, negions[i].z,
negions[i].t, negions[i].w);
for (const auto& p : driftnegion.GetNegativeIons()) {
const auto& p1 = p.path.back();
if (p.status == -9) nRecombNegions++;
else negions_b[tid].push_back(P{p1.x, p1.y, p1.z, p1.t, p.weight});
}
}
}
// --- Drift i+ ------------------------------------------------------
const int nions = ions.size();
#pragma omp parallel
{
const int tid = omp_get_thread_num();
#pragma omp for reduction(+: nRecombIons)
for (int i = 0; i < nions; ++i) {
AvalancheMC driftion;
driftion.SetSensor(&sensor);
driftion.EnableRecombination(withIonRecombination, alpha);
driftion.EnableDiffusion(withIonDiffusion);
driftion.EnableDensityMap(withDensityMap);
driftion.SetTimeSteps(.2); // [ns] default 0.02
driftion.SetTimeWindow(t, t+dt);
driftion.DriftIon(ions[i].x, ions[i].y, ions[i].z,
ions[i].t, ions[i].w);
for (const auto& p : driftion.GetIons()) {
const auto& p1 = p.path.back();
if (p.status == -9) nRecombIons++;
else ions_b[tid].push_back(P{p1.x, p1.y, p1.z, p1.t, p.weight});
}
}
}
// --- Update global variable and clear buffers -----------------------
electrons.clear();
for (auto& b : electrons_b) {
electrons.insert(electrons.end(), b.begin(), b.end());
}
negions.clear();
for (auto& b : negions_b) {
negions.insert(negions.end(), b.begin(), b.end());
}
ions.clear();
for (auto& b : ions_b) {
ions.insert(ions.end(), b.begin(), b.end());
}
grid.ClearFields();
grid.SetUniformElectricField(0., 0., 0.);
for (auto& ion : ions) grid.AddIon(ion.x, ion.y, ion.z, ion.w);
for (auto& nion : negions) grid.AddNegativeIon(nion.x, nion.y, nion.z, nion.w);
t += dt;
}
AskedDensity_mt.cc (10.9 KB)
Edit: I just ran the code with OMP_NUM_THREADS=4
and it suddenly worked without changing anything. Then I ran it again and got this error. It seems very unstable — the more I increase the number of threads or the number of simulated pairs, the more likely it fails.
*** Break *** segmentation violation
[/usr/lib/system/libsystem_platform.dylib] _sigtramp (no debug info)
[/Users/pierregerard/Applications/garfieldpp/install/lib/libGarfield.0.3.dylib] std::__1::vector<double, std::__1::allocator<double>>::vector(std::__1::vector<double, std::__1::allocator<double>> const&) (no debug info)
[/Users/pierregerard/Applications/garfieldpp/install/lib/libGarfield.0.3.dylib] std::__1::vector<double, std::__1::allocator<double>>::vector(std::__1::vector<double, std::__1::allocator<double>> const&) (no debug info)
[/Users/pierregerard/Applications/garfieldpp/install/lib/libGarfield.0.3.dylib] void std::__1::allocator<std::__1::vector<double, std::__1::allocator<double>>>::construct[abi:ne190102]<std::__1::vector<double, std::__1::allocator<double>>, std::__1::vector<double, std::__1::allocator<double>>&>(std::__1::vector<double, std::__1::allocator<double>>*, std::__1::vector<double, std::__1::allocator<double>>&) (no debug info)
[/Users/pierregerard/Applications/garfieldpp/install/lib/libGarfield.0.3.dylib] void std::__1::allocator_traits<std::__1::allocator<std::__1::vector<double, std::__1::allocator<double>>>>::construct[abi:ne190102]<std::__1::vector<double, std::__1::allocator<double>>, std::__1::vector<double, std::__1::allocator<double>>&, 0>(std::__1::allocator<std::__1::vector<double, std::__1::allocator<double>>>&, std::__1::vector<double, std::__1::allocator<double>>*, std::__1::vector<double, std::__1::allocator<double>>&) (no debug info)
[/Users/pierregerard/Applications/garfieldpp/install/lib/libGarfield.0.3.dylib] std::__1::vector<double, std::__1::allocator<double>>* std::__1::__uninitialized_allocator_copy_impl[abi:ne190102]<std::__1::allocator<std::__1::vector<double, std::__1::allocator<double>>>, std::__1::vector<double, std::__1::allocator<double>>*, std::__1::vector<double, std::__1::allocator<double>>*, std::__1::vector<double, std::__1::allocator<double>>*>(std::__1::allocator<std::__1::vector<double, std::__1::allocator<double>>>&, std::__1::vector<double, std::__1::allocator<double>>*, std::__1::vector<double, std::__1::allocator<double>>*, std::__1::vector<double, std::__1::allocator<double>>*) (no debug info)
[/Users/pierregerard/Applications/garfieldpp/install/lib/libGarfield.0.3.dylib] std::__1::vector<double, std::__1::allocator<double>>* std::__1::__uninitialized_allocator_copy[abi:ne190102]<std::__1::allocator<std::__1::vector<double, std::__1::allocator<double>>>, std::__1::vector<double, std::__1::allocator<double>>*, std::__1::vector<double, std::__1::allocator<double>>*, std::__1::vector<double, std::__1::allocator<double>>*>(std::__1::allocator<std::__1::vector<double, std::__1::allocator<double>>>&, std::__1::vector<double, std::__1::allocator<double>>*, std::__1::vector<double, std::__1::allocator<double>>*, std::__1::vector<double, std::__1::allocator<double>>*) (no debug info)
[/Users/pierregerard/Applications/garfieldpp/install/lib/libGarfield.0.3.dylib] void std::__1::vector<std::__1::vector<double, std::__1::allocator<double>>, std::__1::allocator<std::__1::vector<double, std::__1::allocator<double>>>>::__construct_at_end<std::__1::vector<double, std::__1::allocator<double>>*, std::__1::vector<double, std::__1::allocator<double>>*>(std::__1::vector<double, std::__1::allocator<double>>*, std::__1::vector<double, std::__1::allocator<double>>*, unsigned long) (no debug info)
[/Users/pierregerard/Applications/garfieldpp/install/lib/libGarfield.0.3.dylib] void std::__1::vector<std::__1::vector<double, std::__1::allocator<double>>, std::__1::allocator<std::__1::vector<double, std::__1::allocator<double>>>>::__init_with_size[abi:ne190102]<std::__1::vector<double, std::__1::allocator<double>>*, std::__1::vector<double, std::__1::allocator<double>>*>(std::__1::vector<double, std::__1::allocator<double>>*, std::__1::vector<double, std::__1::allocator<double>>*, unsigned long) (no debug info)
[/Users/pierregerard/Applications/garfieldpp/install/lib/libGarfield.0.3.dylib] std::__1::vector<std::__1::vector<double, std::__1::allocator<double>>, std::__1::allocator<std::__1::vector<double, std::__1::allocator<double>>>>::vector(std::__1::vector<std::__1::vector<double, std::__1::allocator<double>>, std::__1::allocator<std::__1::vector<double, std::__1::allocator<double>>>> const&) (no debug info)
[/Users/pierregerard/Applications/garfieldpp/install/lib/libGarfield.0.3.dylib] std::__1::vector<std::__1::vector<double, std::__1::allocator<double>>, std::__1::allocator<std::__1::vector<double, std::__1::allocator<double>>>>::vector(std::__1::vector<std::__1::vector<double, std::__1::allocator<double>>, std::__1::allocator<std::__1::vector<double, std::__1::allocator<double>>>> const&) (no debug info)
[/Users/pierregerard/Applications/garfieldpp/install/lib/libGarfield.0.3.dylib] void std::__1::allocator<std::__1::vector<std::__1::vector<double, std::__1::allocator<double>>, std::__1::allocator<std::__1::vector<double, std::__1::allocator<double>>>>>::construct[abi:ne190102]<std::__1::vector<std::__1::vector<double, std::__1::allocator<double>>, std::__1::allocator<std::__1::vector<double, std::__1::allocator<double>>>>, std::__1::vector<std::__1::vector<double, std::__1::allocator<double>>, std::__1::allocator<std::__1::vector<double, std::__1::allocator<double>>>> const&>(std::__1::vector<std::__1::vector<double, std::__1::allocator<double>>, std::__1::allocator<std::__1::vector<double, std::__1::allocator<double>>>>*, std::__1::vector<std::__1::vector<double, std::__1::allocator<double>>, std::__1::allocator<std::__1::vector<double, std::__1::allocator<double>>>> const&) (no debug info)
[/Users/pierregerard/Applications/garfieldpp/install/lib/libGarfield.0.3.dylib] void std::__1::allocator_traits<std::__1::allocator<std::__1::vector<std::__1::vector<double, std::__1::allocator<double>>, std::__1::allocator<std::__1::vector<double, std::__1::allocator<double>>>>>>::construct[abi:ne190102]<std::__1::vector<std::__1::vector<double, std::__1::allocator<double>>, std::__1::allocator<std::__1::vector<double, std::__1::allocator<double>>>>, std::__1::vector<std::__1::vector<double, std::__1::allocator<double>>, std::__1::allocator<std::__1::vector<double, std::__1::allocator<double>>>> const&, 0>(std::__1::allocator<std::__1::vector<std::__1::vector<double, std::__1::allocator<double>>, std::__1::allocator<std::__1::vector<double, std::__1::allocator<double>>>>>&, std::__1::vector<std::__1::vector<double, std::__1::allocator<double>>, std::__1::allocator<std::__1::vector<double, std::__1::allocator<double>>>>*, std::__1::vector<std::__1::vector<double, std::__1::allocator<double>>, std::__1::allocator<std::__1::vector<double, std::__1::allocator<double>>>> const&) (no debug info)
[/Users/pierregerard/Applications/garfieldpp/install/lib/libGarfield.0.3.dylib] std::__1::vector<std::__1::vector<std::__1::vector<double, std::__1::allocator<double>>, std::__1::allocator<std::__1::vector<double, std::__1::allocator<double>>>>, std::__1::allocator<std::__1::vector<std::__1::vector<double, std::__1::allocator<double>>, std::__1::allocator<std::__1::vector<double, std::__1::allocator<double>>>>>>::__construct_at_end(unsigned long, std::__1::vector<std::__1::vector<double, std::__1::allocator<double>>, std::__1::allocator<std::__1::vector<double, std::__1::allocator<double>>>> const&) (no debug info)
[/Users/pierregerard/Applications/garfieldpp/install/lib/libGarfield.0.3.dylib] std::__1::vector<std::__1::vector<std::__1::vector<double, std::__1::allocator<double>>, std::__1::allocator<std::__1::vector<double, std::__1::allocator<double>>>>, std::__1::allocator<std::__1::vector<std::__1::vector<double, std::__1::allocator<double>>, std::__1::allocator<std::__1::vector<double, std::__1::allocator<double>>>>>>::assign(unsigned long, std::__1::vector<std::__1::vector<double, std::__1::allocator<double>>, std::__1::allocator<std::__1::vector<double, std::__1::allocator<double>>>> const&) (no debug info)
[/Users/pierregerard/Applications/garfieldpp/install/lib/libGarfield.0.3.dylib] Garfield::Medium::Init(unsigned long, unsigned long, unsigned long, std::__1::vector<std::__1::vector<std::__1::vector<double, std::__1::allocator<double>>, std::__1::allocator<std::__1::vector<double, std::__1::allocator<double>>>>, std::__1::allocator<std::__1::vector<std::__1::vector<double, std::__1::allocator<double>>, std::__1::allocator<std::__1::vector<double, std::__1::allocator<double>>>>>>&, double) (no debug info)
[/Users/pierregerard/Applications/garfieldpp/install/lib/libGarfield.0.3.dylib] Garfield::Medium::VelocityFromMobility(std::__1::vector<std::__1::vector<std::__1::vector<double, std::__1::allocator<double>>, std::__1::allocator<std::__1::vector<double, std::__1::allocator<double>>>>, std::__1::allocator<std::__1::vector<std::__1::vector<double, std::__1::allocator<double>>, std::__1::allocator<std::__1::vector<double, std::__1::allocator<double>>>>>> const&, std::__1::vector<std::__1::vector<std::__1::vector<double, std::__1::allocator<double>>, std::__1::allocator<std::__1::vector<double, std::__1::allocator<double>>>>, std::__1::allocator<std::__1::vector<std::__1::vector<double, std::__1::allocator<double>>, std::__1::allocator<std::__1::vector<double, std::__1::allocator<double>>>>>>&) (no debug info)
[/Users/pierregerard/Applications/garfieldpp/install/lib/libGarfield.0.3.dylib] Garfield::Medium::IonVelocity(double, double, double, double, double, double, double&, double&, double&) (no debug info)
[/Users/pierregerard/Applications/garfieldpp/install/lib/libGarfield.0.3.dylib] Garfield::AvalancheMC::GetVelocity(Garfield::Particle, Garfield::Medium*, std::__1::array<double, 3ul> const&, std::__1::array<double, 3ul> const&, std::__1::array<double, 3ul> const&, std::__1::array<double, 3ul>&) const (no debug info)
[/Users/pierregerard/Applications/garfieldpp/install/lib/libGarfield.0.3.dylib] Garfield::AvalancheMC::DriftLine(Garfield::AvalancheMC::Seed const&, std::__1::vector<Garfield::AvalancheMC::Point, std::__1::allocator<Garfield::AvalancheMC::Point>>&, std::__1::vector<Garfield::AvalancheMC::Seed, std::__1::allocator<Garfield::AvalancheMC::Seed>>&, bool, bool) const (no debug info)
[/Users/pierregerard/Applications/garfieldpp/install/lib/libGarfield.0.3.dylib] Garfield::AvalancheMC::TransportParticles(std::__1::vector<Garfield::AvalancheMC::Seed, std::__1::allocator<Garfield::AvalancheMC::Seed>>&, bool, bool, bool) (no debug info)
[/Users/pierregerard/Applications/garfieldpp/install/lib/libGarfield.0.3.dylib] Garfield::AvalancheMC::DriftIon(double, double, double, double, unsigned long) (no debug info)
[/Users/pierregerard/Desktop/Thesis/garfieldpp_projects/TestComponentGrid/build/AskedDensity_mt] main.omp_outlined.22 (no debug info)
[/opt/homebrew/Cellar/llvm/21.1.0/lib/libomp.dylib] __kmp_invoke_microtask (no debug info)
[<unknown binary>] (no debug info)
AskedDensity_mt(89270,0x38380b000) malloc: double free for ptr 0x14e019200
AskedDensity_mt(89270,0x38380b000) malloc: *** set a breakpoint in malloc_error_break to debug
[1] 89270 abort OMP_NUM_THREADS=4 ./AskedDensity_mt --density 9e10 --pairs 1e3