Skip to content

Commit 92e4ecb

Browse files
committed
Fix the offset computation.
Also ensure the marked array is correctly freed in all cases. Signed-off-by: George Bosilca <[email protected]>
1 parent 3d1a7dd commit 92e4ecb

File tree

1 file changed

+26
-32
lines changed

1 file changed

+26
-32
lines changed

ompi/mca/topo/treematch/topo_treematch_dist_graph_create.c

Lines changed: 26 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -131,20 +131,13 @@ int mca_topo_treematch_dist_graph_create(mca_topo_base_module_t* topo_module,
131131
double *local_pattern = NULL;
132132
int *vpids, *colors = NULL;
133133
int *lindex_to_grank = NULL;
134-
int *nodes_roots = NULL;
134+
int *nodes_roots = NULL, *k = NULL;
135135
int *localrank_to_objnum = NULL;
136136
int depth, effective_depth, obj_rank = -1;
137-
int num_objs_in_node = 0;
138-
int num_pus_in_node = 0;
139-
int numlevels = 0;
140-
int num_nodes = 0;
141-
int num_procs_in_node = 0;
142-
int rank, size;
143-
int *k = NULL;
144-
int newrank = -1;
145-
int hwloc_err;
137+
int num_objs_in_node = 0, num_pus_in_node = 0;
138+
int numlevels = 0, num_nodes = 0, num_procs_in_node = 0;
139+
int rank, size, newrank = -1, hwloc_err, i, j, idx;
146140
int oversubscribing_objs = 0, oversubscribed_pus = 0;
147-
int i, j, idx;
148141
uint32_t val, *pval;
149142

150143
/* We need to know if the processes are bound. We assume all
@@ -251,7 +244,7 @@ int mca_topo_treematch_dist_graph_create(mca_topo_base_module_t* topo_module,
251244
effective_depth = object->depth;
252245
num_objs_in_node = hwloc_get_nbobjs_by_depth(opal_hwloc_topology, effective_depth);
253246
}
254-
if( 0 == num_objs_in_node ) { /* deal with bozo cases: COVERITY 1418505 */
247+
if( (0 == num_objs_in_node) || (0 == num_pus_in_node) ) { /* deal with bozo cases: COVERITY 1418505 */
255248
free(colors);
256249
goto fallback; /* return with success */
257250
}
@@ -623,10 +616,10 @@ int mca_topo_treematch_dist_graph_create(mca_topo_base_module_t* topo_module,
623616
for(i = 0; i < tm_topology->nb_proc_units ; i++)
624617
if (obj_mapping[i] != -1)
625618
tm_topology->nb_constraints++;
626-
tm_topology->constraints = (int *)calloc(tm_topology->nb_constraints,sizeof(int));
619+
tm_topology->constraints = (int *)calloc(tm_topology->nb_constraints,sizeof(int));
627620
for(idx = 0, i = 0; i < tm_topology->nb_proc_units ; i++)
628621
if (obj_mapping[i] != -1)
629-
tm_topology->constraints[idx++] = obj_mapping[i];
622+
tm_topology->constraints[idx++] = obj_mapping[i];
630623

631624
tm_topology->oversub_fact = 1;
632625

@@ -656,11 +649,11 @@ int mca_topo_treematch_dist_graph_create(mca_topo_base_module_t* topo_module,
656649
}
657650
#endif
658651
tm_optimize_topology(&tm_topology);
659-
aff_mat = tm_build_affinity_mat(comm_pattern,size);
652+
aff_mat = tm_build_affinity_mat(comm_pattern,size);
660653
comm_tree = tm_build_tree_from_topology(tm_topology,aff_mat, NULL, NULL);
661654
sol = tm_compute_mapping(tm_topology, comm_tree);
662655

663-
assert(sol->k_length == size);
656+
assert((int)sol->k_length == size);
664657

665658
k = (int *)calloc(sol->k_length, sizeof(int));
666659
for(idx = 0 ; idx < (int)sol->k_length ; idx++)
@@ -708,7 +701,7 @@ int mca_topo_treematch_dist_graph_create(mca_topo_base_module_t* topo_module,
708701

709702
} else { /* partially distributed reordering */
710703
int *grank_to_lrank = NULL, *lrank_to_grank = NULL, *marked = NULL;
711-
int node_position = 0, offset = 0, done = 0, pos = 0;
704+
int node_position = 0, offset = 0, pos = 0;
712705
ompi_communicator_t *localcomm = NULL;
713706

714707
if (OMPI_SUCCESS != (err = ompi_comm_split(comm_old, colors[rank], rank,
@@ -800,7 +793,7 @@ int mca_topo_treematch_dist_graph_create(mca_topo_base_module_t* topo_module,
800793
tm_topology->nb_nodes[i] = nb_objs;
801794
tm_topology->arity[i] = tracker[i]->arity;
802795
tm_topology->node_id[i] = (int *)calloc(tm_topology->nb_nodes[i], sizeof(int));
803-
tm_topology->node_rank[i] = (int * )calloc(tm_topology->nb_nodes[i], sizeof(int));
796+
tm_topology->node_rank[i] = (int * )calloc(tm_topology->nb_nodes[i], sizeof(int));
804797
for(j = 0; j < (int)tm_topology->nb_nodes[i] ; j++){
805798
tm_topology->node_id[i][j] = j;
806799
tm_topology->node_rank[i][j] = j;
@@ -811,7 +804,7 @@ int mca_topo_treematch_dist_graph_create(mca_topo_base_module_t* topo_module,
811804
tm_topology->cost = (double*)calloc(tm_topology->nb_levels,sizeof(double));
812805

813806
tm_topology->nb_proc_units = num_objs_in_node;
814-
//tm_topology->nb_proc_units = num_procs_in_node;
807+
//tm_topology->nb_proc_units = num_procs_in_node;
815808
tm_topology->nb_constraints = 0;
816809
for(i = 0; i < num_procs_in_node ; i++)
817810
if (localrank_to_objnum[i] != -1)
@@ -837,8 +830,8 @@ int mca_topo_treematch_dist_graph_create(mca_topo_base_module_t* topo_module,
837830
aff_mat = tm_build_affinity_mat(comm_pattern,num_procs_in_node);
838831
comm_tree = tm_build_tree_from_topology(tm_topology,aff_mat, NULL, NULL);
839832
sol = tm_compute_mapping(tm_topology, comm_tree);
840-
841-
assert(sol->k_length == num_procs_in_node);
833+
834+
assert((int)sol->k_length == num_procs_in_node);
842835

843836
k = (int *)calloc(sol->k_length, sizeof(int));
844837
for(idx = 0 ; idx < (int)sol->k_length ; idx++)
@@ -875,22 +868,24 @@ int mca_topo_treematch_dist_graph_create(mca_topo_base_module_t* topo_module,
875868
/* compute the offset of newrank before the split */
876869
/* use the colors array, not the vpids */
877870
marked = (int *)malloc((num_nodes-1)*sizeof(int));
878-
for(int idx = 0 ; idx < num_nodes - 1 ; idx++)
871+
for(idx = 0 ; idx < num_nodes - 1 ; idx++)
879872
marked[idx] = -1;
880873

881-
while( (node_position != rank) && (colors[node_position] != colors[rank])){
882-
for(int idx = 0; idx < num_nodes - 1 ; idx++)
874+
while( (node_position != rank) && (colors[node_position] != colors[rank])) {
875+
/* Have we already counted the current color ? */
876+
for(idx = 0; idx < pos; idx++)
883877
if( marked[idx] == colors[node_position] )
884-
done = 1;
885-
if(!done) {
886-
for(int idx = 0 ; idx < size ; idx++)
887-
if(colors[idx] == colors[node_position])
888-
offset++;
889-
marked[pos++] = colors[node_position];
890-
}
878+
goto next_iter; /* yes, let's skip the rest */
879+
/* How many elements of this color are here ? none before the current position */
880+
for(; idx < size; idx++)
881+
if(colors[idx] == colors[node_position])
882+
offset++;
883+
marked[pos++] = colors[node_position];
884+
next_iter:
891885
node_position++;
892886
}
893887
newrank += offset;
888+
free(marked);
894889

895890
if (rank == lindex_to_grank[0])
896891
free(k);
@@ -900,7 +895,6 @@ int mca_topo_treematch_dist_graph_create(mca_topo_base_module_t* topo_module,
900895
ompi_comm_free(&localcomm);
901896
free(lrank_to_grank);
902897
free(grank_to_lrank);
903-
free(marked); marked = NULL;
904898
goto release_and_return;
905899
}
906900
/* end of TODO */

0 commit comments

Comments
 (0)