java-topology/defects/mpich/patch/mpich-0001-group-lpid-to-rank-hashmap.patch

45 lines
1.8 KiB
Diff

# UNDF: UNDF-2026-000000394
# UNDF: (pending)
--- a/src/include/mpir_group.h
+++ b/src/include/mpir_group.h
@@ -53,6 +53,7 @@ struct MPIR_Pmap {
} stride;
} u;
+ MPL_hash_t *lpid_to_rank_ht; /* non-NULL only when use_map==true; lpid->(rank+1) */
};
--- a/src/mpi/group/grouputil.c
+++ b/src/mpi/group/grouputil.c
@@ -218,9 +218,19 @@ int MPIR_Group_create_map(int size, int rank, MPIR_Session * session_ptr, MPIR_L
} else {
newgrp->pmap.use_map = true;
newgrp->pmap.u.map = map;
- /* TODO: build hash to accelerate MPIR_Group_lpid_to_rank */
+ /* Build reverse hash: lpid -> rank+1 (0 reserved for "not found") */
+ MPL_hash_t *ht = MPL_malloc(sizeof(MPL_hash_t), MPL_MEM_GROUP);
+ MPIR_ERR_CHKANDJUMP(!ht, mpi_errno, MPI_ERR_OTHER, "**nomem");
+ MPL_hash_init(ht);
+ for (int r = 0; r < size; r++) {
+ MPL_hash_set(ht, (uintptr_t) map[r], (uintptr_t)(r + 1));
+ }
+ newgrp->pmap.lpid_to_rank_ht = ht;
}
@@ -472,13 +482,9 @@ static int pmap_lpid_to_rank(struct MPIR_Pmap *pmap, int size, MPIR_Lpid lpid)
{
if (pmap->use_map) {
- /* Use linear search for now.
- * Optimization: build hash map in MPIR_Group_create_map and do O(1) hash lookup
- */
- for (int rank = 0; rank < size; rank++) {
- if (pmap->u.map[rank] == lpid) {
- return rank;
- }
- }
- return MPI_UNDEFINED;
+ /* O(1) reverse-hash lookup */
+ uintptr_t val = MPL_hash_get(pmap->lpid_to_rank_ht, (uintptr_t) lpid);
+ return val ? (int)(val - 1) : MPI_UNDEFINED;
} else {
/* NOTE: stride could be negative, in which case, make sure r_blk >= 0 */
int rank = (lpid - pmap->u.stride.offset) / pmap->u.stride.stride;