Skip to content

Commit

Permalink
shell: sort rcalc ranks if needed before assigning shell ranks
Browse files Browse the repository at this point in the history
Problem: The shell builds its list of ranks in the order they appear
in the R_lite array of Rv1, but when ranks appear out of order with
respect to broker ranks, this breaks assumptions elsewhere (e.g. in
the taskmap code) that shell ranks are a direct index into the sorted
broker ranks idset and associated hostlist.

Since the common case will be a sorted R_lite array, detect if the
ranks are not sorted and, if so, sort the rcalc rank array by broker
rank and reassign shell ranks.

Fixes flux-framework#6582
  • Loading branch information
grondo committed Jan 27, 2025
1 parent 0836ef6 commit 2c0fc68
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/shell/rcalc.c
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,22 @@ static int rankinfo_get_children (struct rankinfo *ri,
return 0;
}

static int cmp_rank (const void *a, const void *b)
{
const struct rankinfo *ra = a;
const struct rankinfo *rb = b;
return (ra->rank < rb->rank ? -1 : 1);
}


static int rcalc_process_all_ranks (rcalc_t *r, flux_error_t *errp)
{
json_t *entry;
size_t index;
json_error_t error;
int n = 0;
unsigned int max_rank = 0;
bool sorted = true;

json_array_foreach (r->R_lite, index, entry) {
const char *rank;
Expand All @@ -156,6 +166,14 @@ static int rcalc_process_all_ranks (rcalc_t *r, flux_error_t *errp)
struct rankinfo *ri = &r->ranks[n];
ri->id = n;
ri->rank = i;

/* Detect if R_lite ranks are unordered:
*/
if (i >= max_rank)
max_rank = i;
else
sorted = false;

if (rankinfo_get_children (ri, children, errp) < 0) {
idset_destroy (ids);
return -1;
Expand All @@ -167,6 +185,15 @@ static int rcalc_process_all_ranks (rcalc_t *r, flux_error_t *errp)
}
idset_destroy (ids);
}

if (!sorted) {
/* Need to sort r->ranks by broker rank and reassign local rank (id):
*/
qsort (r->ranks, r->nranks, sizeof (struct rankinfo), cmp_rank);
for (int id = 0; id < r->nranks; id++)
r->ranks[id].id = id;
}

return 0;
}

Expand Down

0 comments on commit 2c0fc68

Please sign in to comment.