Skip to content

Commit

Permalink
Allow for 0 or 1 indexing with bu_parallel_id
Browse files Browse the repository at this point in the history
Got bit by this with the new BoT raytracing - on Windows,
bu_parallel_id() starts counting from 1, so the thread indexing into the
hit array was offset accordingly.  On Linux, however, bu_parallel_id()
DOES return 0 - so that offset generated an invalid index and
segfault.

Simplest approach appears to be to increase the hit array allocation by
1, to allow either indexing approach to succeed.  Additional cost of a
single unused hit container is negligible and avoids any other need for
conditional logic.
  • Loading branch information
starseeker committed Aug 2, 2024
1 parent a2d4fb5 commit bd8577d
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/librt/primitives/bot/bot.c
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,10 @@ rt_bot_prep(struct soltab *stp, struct rt_db_internal *ip, struct rt_i *rtip)
sps->tris = tris;
sps->vertex_normals = tri_norms;
sps->num_cpus = bu_avail_cpus(); // NOTE: this does NOT respect user requested cpu count (ie if -P was used)
sps->hit_arrays_per_cpu = (hit_da *) bu_calloc(sps->num_cpus, sizeof(hit_da), "thread-local bot hit arrays");

// We need an extra array entry here, to accommodate platforms using both 0
// and 1 indexing for bu_parallel_id()
sps->hit_arrays_per_cpu = (hit_da *) bu_calloc(sps->num_cpus+1, sizeof(hit_da), "thread-local bot hit arrays");
bot->tie = (void*) sps;

// struct bvh_build_node and struct bvh_flat_node are puns for fastf_t[6] which are the bounds
Expand Down Expand Up @@ -703,7 +706,7 @@ rt_bot_shot(struct soltab *stp, struct xray *rp, struct application *ap, struct
if (UNLIKELY(!sps))
return 0;

int thread_ind = bu_parallel_id() - 1; // 0-index
int thread_ind = bu_parallel_id();
hit_da *hits_da = &sps->hit_arrays_per_cpu[thread_ind];
hits_da->count = 0;

Expand Down Expand Up @@ -865,7 +868,9 @@ rt_bot_free(struct soltab *stp)
bu_free(sps->tris, "bot triangles");
bu_free(sps->vertex_normals, "bot normals");
if (sps->hit_arrays_per_cpu) {
for (size_t i = 0; i < sps->num_cpus; i++) {
// As in allocation, we have one extra entry beyond num_cpus to allow
// for various platforms using either 0 or 1 bu_parallel_id indexing
for (size_t i = 0; i < sps->num_cpus+1; i++) {
if (sps->hit_arrays_per_cpu[i].items) bu_free(sps->hit_arrays_per_cpu[i].items, "bot thread-local hit arrays");
}
bu_free(sps->hit_arrays_per_cpu, "bot array of dynamic thread-local hit arrays");
Expand Down

0 comments on commit bd8577d

Please sign in to comment.