Commit 68aef9ad authored by Matthieu Schaller's avatar Matthieu Schaller
Browse files

Merge branch 'lock_before_removing_particles_debug' into 'master'

Fix rare removing particles bug

See merge request !871
parents 57f2253e 31aed3eb
......@@ -4379,13 +4379,7 @@ void cell_drift_part(struct cell *c, const struct engine *e, int force) {
hydro_remove_part(p, xp);
/* Remove the particle entirely */
struct gpart *gp = p->gpart;
cell_remove_part(e, c, p, xp);
/* and it's gravity friend */
if (gp != NULL) {
cell_remove_gpart(e, c, gp);
}
}
if (lock_unlock(&e->s->lock) != 0)
......@@ -4547,8 +4541,10 @@ void cell_drift_gpart(struct cell *c, const struct engine *e, int force) {
if (!gpart_is_inhibited(gp, e)) {
/* Remove the particle entirely */
if (gp->type == swift_type_dark_matter) {
cell_remove_gpart(e, c, gp);
}
}
if (lock_unlock(&e->s->lock) != 0)
error("Failed to unlock the space!");
......@@ -4688,11 +4684,7 @@ void cell_drift_spart(struct cell *c, const struct engine *e, int force) {
if (!spart_is_inhibited(sp, e)) {
/* Remove the particle entirely */
struct gpart *gp = sp->gpart;
cell_remove_spart(e, c, sp);
/* and it's gravity friend */
cell_remove_gpart(e, c, gp);
}
if (lock_unlock(&e->s->lock) != 0)
......@@ -4863,11 +4855,7 @@ void cell_drift_bpart(struct cell *c, const struct engine *e, int force) {
if (!bpart_is_inhibited(bp, e)) {
/* Remove the particle entirely */
struct gpart *gp = bp->gpart;
cell_remove_bpart(e, c, bp);
/* and it's gravity friend */
cell_remove_gpart(e, c, gp);
}
if (lock_unlock(&e->s->lock) != 0)
......@@ -5327,6 +5315,9 @@ void cell_remove_part(const struct engine *e, struct cell *c, struct part *p,
if (c->nodeID != e->nodeID)
error("Can't remove a particle in a foreign cell.");
/* Don't remove a particle twice */
if (p->time_bin == time_bin_inhibited) return;
/* Mark the particle as inhibited */
p->time_bin = time_bin_inhibited;
......@@ -5337,12 +5328,15 @@ void cell_remove_part(const struct engine *e, struct cell *c, struct part *p,
p->gpart->type = swift_type_dark_matter;
}
/* Un-link the part */
p->gpart = NULL;
/* Update the space-wide counters */
const size_t one = 1;
atomic_add(&e->s->nr_inhibited_parts, one);
if (p->gpart) {
atomic_add(&e->s->nr_inhibited_gparts, one);
}
/* Un-link the part */
p->gpart = NULL;
}
/**
......@@ -5357,6 +5351,14 @@ void cell_remove_part(const struct engine *e, struct cell *c, struct part *p,
*/
void cell_remove_gpart(const struct engine *e, struct cell *c,
struct gpart *gp) {
/* Quick cross-check */
if (c->nodeID != e->nodeID)
error("Can't remove a particle in a foreign cell.");
/* Don't remove a particle twice */
if (gp->time_bin == time_bin_inhibited) return;
/* Quick cross-check */
if (c->nodeID != e->nodeID)
error("Can't remove a particle in a foreign cell.");
......@@ -5385,6 +5387,9 @@ void cell_remove_spart(const struct engine *e, struct cell *c,
if (c->nodeID != e->nodeID)
error("Can't remove a particle in a foreign cell.");
/* Don't remove a particle twice */
if (sp->time_bin == time_bin_inhibited) return;
/* Mark the particle as inhibited and stand-alone */
sp->time_bin = time_bin_inhibited;
if (sp->gpart) {
......@@ -5393,12 +5398,15 @@ void cell_remove_spart(const struct engine *e, struct cell *c,
sp->gpart->type = swift_type_dark_matter;
}
/* Un-link the spart */
sp->gpart = NULL;
/* Update the space-wide counters */
const size_t one = 1;
atomic_add(&e->s->nr_inhibited_sparts, one);
if (sp->gpart) {
atomic_add(&e->s->nr_inhibited_gparts, one);
}
/* Un-link the spart */
sp->gpart = NULL;
}
/**
......@@ -5418,6 +5426,9 @@ void cell_remove_bpart(const struct engine *e, struct cell *c,
if (c->nodeID != e->nodeID)
error("Can't remove a particle in a foreign cell.");
/* Don't remove a particle twice */
if (bp->time_bin == time_bin_inhibited) return;
/* Mark the particle as inhibited and stand-alone */
bp->time_bin = time_bin_inhibited;
if (bp->gpart) {
......@@ -5426,12 +5437,15 @@ void cell_remove_bpart(const struct engine *e, struct cell *c,
bp->gpart->type = swift_type_dark_matter;
}
/* Un-link the bpart */
bp->gpart = NULL;
/* Update the space-wide counters */
const size_t one = 1;
atomic_add(&e->s->nr_inhibited_bparts, one);
if (bp->gpart) {
atomic_add(&e->s->nr_inhibited_gparts, one);
}
/* Un-link the bpart */
bp->gpart = NULL;
}
/**
......
......@@ -3872,10 +3872,10 @@ void runner_do_gas_swallow(struct runner *r, struct cell *c, int timer) {
* by another thread before we do the deed. */
if (!part_is_inhibited(p, e)) {
/* Finally, remove the gas particle from the system */
struct gpart *gp = p->gpart;
/* Finally, remove the gas particle from the system
* Recall that the gpart associated with it is also removed
* at the same time. */
cell_remove_part(e, c, p, xp);
cell_remove_gpart(e, c, gp);
}
if (lock_unlock(&e->s->lock) != 0)
......@@ -3916,9 +3916,7 @@ void runner_do_gas_swallow(struct runner *r, struct cell *c, int timer) {
if (!part_is_inhibited(p, e)) {
/* Finally, remove the gas particle from the system */
struct gpart *gp = p->gpart;
cell_remove_part(e, c, p, xp);
cell_remove_gpart(e, c, gp);
}
if (lock_unlock(&e->s->lock) != 0)
......@@ -4092,10 +4090,10 @@ void runner_do_bh_swallow(struct runner *r, struct cell *c, int timer) {
message("BH %lld removing BH particle %lld", bp->id, cell_bp->id);
/* Finally, remove the gas particle from the system */
struct gpart *cell_gp = cell_bp->gpart;
/* Finally, remove the gas particle from the system
* Recall that the gpart associated with it is also removed
* at the same time. */
cell_remove_bpart(e, c, cell_bp);
cell_remove_gpart(e, c, cell_gp);
}
/* In any case, prevent the particle from being re-swallowed */
......@@ -4126,9 +4124,7 @@ void runner_do_bh_swallow(struct runner *r, struct cell *c, int timer) {
bp->id, cell_bp->id);
/* Finally, remove the gas particle from the system */
struct gpart *cell_gp = cell_bp->gpart;
cell_remove_bpart(e, c, cell_bp);
cell_remove_gpart(e, c, cell_gp);
found = 1;
break;
......
Supports Markdown
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