[ofa-general] [PATCH] opensm/osm_mesh.c: Reorder switches for lash

Hal Rosenstock hnrose at comcast.net
Mon Jul 20 05:46:47 PDT 2009


Signed-off-by: Robert Pearson <rpearson at systemfabricworks.com>
Signed-off-by: Hal Rosenstock <hal.rosenstock at gmail.com>
---
diff --git a/opensm/opensm/osm_mesh.c b/opensm/opensm/osm_mesh.c
index 23fad87..a463ca9 100644
--- a/opensm/opensm/osm_mesh.c
+++ b/opensm/opensm/osm_mesh.c
@@ -1272,6 +1272,80 @@ static int reorder_links(lash_t *p_lash, mesh_t *mesh)
 }
 
 /*
+ * compare two switches in a sort
+ */
+
+/* Sort switches never gets called more than once so we can use a
+   static structure to keep our context. */
+static struct {
+	lash_t *p_lash;
+	mesh_t *mesh;
+} sort_ctx;
+
+static int compare_switch(const void *p1, const void *p2)
+{
+	int i, j, d;
+	int dimension = sort_ctx.mesh->dimension;
+	switch_t *s1 = sort_ctx.p_lash->switches[*(int *)p1];
+	switch_t *s2 = sort_ctx.p_lash->switches[*(int *)p2];
+
+	for (i = 0; i < dimension; i++) {
+		j = sort_ctx.mesh->dim_order[i];
+		d = s1->node->coord[j] - s2->node->coord[j];
+
+		if (d > 0)
+			return 1;
+
+		if (d < 0)
+			return -1;
+	}
+
+	return 0;
+}
+
+/*
+ * sort_switches - reorder switch array in p_lash
+ */
+static void sort_switches(lash_t *p_lash, mesh_t *mesh)
+{
+	int i, j;
+	int num_switches = p_lash->num_switches;
+	int *index, *reverse;
+	switch_t *s;
+	switch_t **switches;
+
+	index = calloc(num_switches, sizeof(int));
+	reverse = calloc(num_switches, sizeof(int));
+	switches = calloc(num_switches, sizeof(switch_t *));
+
+	for (i = 0; i < num_switches; i++)
+		index[i] = i;
+
+	sort_ctx.mesh = mesh;
+	sort_ctx.p_lash = p_lash;
+	qsort(index, num_switches, sizeof(int), compare_switch);
+
+	for (i = 0; i < num_switches; i++)
+		reverse[index[i]] = i;
+
+	for (i = 0; i < num_switches; i++) {
+		s = p_lash->switches[index[i]];
+		switches[i] = s;
+		s->id = i;
+		for (j = 0; j < s->node->num_links; j++)
+			s->node->links[j]->switch_id =
+				reverse[s->node->links[j]->switch_id];
+	}
+
+	for (i = 0; i < num_switches; i++)
+		p_lash->switches[i] = switches[i];
+
+	free(switches);
+	free(index);
+	free(reverse);
+}
+
+/*
  * osm_mesh_delete - free per mesh resources
  */
 static void mesh_delete(mesh_t *mesh)
@@ -1470,6 +1544,8 @@ int osm_do_mesh_analysis(lash_t *p_lash)
 		if (reorder_links(p_lash, mesh))
 			goto err;
 
+		sort_switches(p_lash, mesh);
+
 		p = buf;
 		p += sprintf(p, "found ");
 		for (i = 0; i < mesh->dimension; i++)



More information about the general mailing list