/** * opal_carto_base_get_nodes_distance - returns the distance of * all the nodes from the reference node. * * @param graph * @param reference_node * @param node_type the type of the nodes in the returned array * @param dist_array * * @return int number of nodes in the returned array. */ int opal_carto_base_get_nodes_distance_fn(opal_carto_graph_t *graph, opal_carto_base_node_t *reference_node, const char *node_type, opal_value_array_t *dist_array) { opal_value_array_t *distance_array; vertex_distance_from_t *vertex_distance; opal_carto_base_node_t *node; uint32_t i, graph_order; int distance_array_size; opal_carto_node_distance_t node_distance; distance_array = OBJ_NEW(opal_value_array_t); opal_value_array_init(distance_array, sizeof(vertex_distance_from_t)); opal_value_array_reserve(distance_array,50); /* use dijkstra algorithm to receive the distance of all the nodes from the referenced node */ graph_order = opal_graph_dijkstra(graph, reference_node->vertex, distance_array); /* for all the nodes in the dijkstra array */ for (i = 0, distance_array_size = 0; i < graph_order; i++) { vertex_distance = opal_value_array_get_item(distance_array, i); node = vertex_distance->vertex->vertex_data; /* check if the node is in the correct type */ if (NULL == node_type || 0 == strcmp(node->node_type, node_type)) { /* assigne the result distance array */ node_distance.node = vertex_distance->vertex->vertex_data; node_distance.node_distance = vertex_distance->weight; opal_value_array_append_item(dist_array, (void *)&node_distance); } } /* return the result distance array */ return distance_array_size; }
uint32_t opal_graph_spf(opal_graph_t *graph, opal_graph_vertex_t *vertex1, opal_graph_vertex_t *vertex2) { opal_value_array_t *distance_array; uint32_t items_in_distance_array, spf = DISTANCE_INFINITY; vertex_distance_from_t *vertex_distance; uint32_t i; /** * Verify that the first vertex belongs to the graph. */ if (graph != vertex1->in_graph) { OPAL_OUTPUT((0,"opal_graph_spf 1 Vertex1 %p not in the graph %p\n",(void *)vertex1,(void *)graph)); return DISTANCE_INFINITY; } /** * Verify that the second vertex belongs to the graph. */ if (graph != vertex2->in_graph) { OPAL_OUTPUT((0,"opal_graph_spf 2 Vertex2 %p not in the graph %p\n",(void *)vertex2,(void *)graph)); return DISTANCE_INFINITY; } /** * Run Dijkstra algorithm on the graph from the start vertex. */ distance_array = OBJ_NEW(opal_value_array_t); opal_value_array_init(distance_array, sizeof(vertex_distance_from_t)); opal_value_array_reserve(distance_array,50); items_in_distance_array = opal_graph_dijkstra(graph, vertex1, distance_array); /** * find the end vertex in the distance array that Dijkstra * algorithm returned. */ for (i = 0; i < items_in_distance_array; i++) { vertex_distance = opal_value_array_get_item(distance_array, i); if (vertex_distance->vertex == vertex2) { spf = vertex_distance->weight; break; } } OBJ_RELEASE(distance_array); /* return the distance (weight) to the end vertex */ return spf; }
int main(int argc, char* argv[]) { int rank, size, distance, distance_array_size, i; opal_carto_graph_t *graph; opal_carto_base_node_t *slot0, *end_node; opal_carto_node_distance_t *node_distance; opal_value_array_t *distance_array; MPI_Init(&argc, &argv); MPI_Comm_rank(MPI_COMM_WORLD, &rank); MPI_Comm_size(MPI_COMM_WORLD, &size); printf("Hello, world, I am %d of %d\n", rank, size); if (0 == rank) { /** * */ opal_output(0," \n\nget_host_graph Full\n"); opal_carto_base_get_host_graph(&graph,NULL); opal_graph_print(graph); slot0 = opal_carto_base_find_node(graph, "slot0"); if (NULL == slot0) { opal_output(0,"couldnt find slot0 in the graph exiting\n"); opal_carto_base_free_graph(graph); return -1; } end_node = opal_carto_base_find_node(graph, "slot3"); if (NULL == end_node) { opal_output(0,"couldnt find mthca1 in the graph exiting\n"); opal_carto_base_free_graph(graph); return -1; } distance = opal_carto_base_spf(graph, slot0, end_node); opal_output(0,"\nThe distance between slot0 and slot3 is %d\n",distance); distance_array = OBJ_NEW(opal_value_array_t); opal_value_array_init(distance_array, sizeof(opal_carto_node_distance_t)); opal_value_array_reserve(distance_array, 50); distance_array_size = opal_carto_base_get_nodes_distance(graph, slot0, NULL, distance_array); for (i=0; i < distance_array_size; i++) { node_distance = opal_value_array_get_item(distance_array, i); opal_output(0,"Node %s distance from slot0 is %d\n",node_distance->node->node_name, node_distance->node_distance); } OBJ_RELEASE(distance_array); opal_carto_base_free_graph(graph); /** * */ opal_output(0," \n\nget_host_graph Infiniband\n"); opal_carto_base_get_host_graph(&graph,"Infiniband"); opal_graph_print(graph); slot0 = opal_carto_base_find_node(graph, "slot0"); if (NULL == slot0) { opal_output(0,"couldnt find slot0 in the graph exiting\n"); opal_carto_base_free_graph(graph); return -1; } end_node = opal_carto_base_find_node(graph, "mthca1"); if (NULL == end_node) { opal_output(0,"couldnt find mthca1 in the graph exiting\n"); opal_carto_base_free_graph(graph); return -1; } distance = opal_carto_base_spf(graph, slot0, end_node); opal_output(0,"\nThe distance between slot0 and mthca1 is %d\n",distance); distance_array = OBJ_NEW(opal_value_array_t); opal_value_array_init(distance_array, sizeof(opal_carto_node_distance_t)); opal_value_array_reserve(distance_array, 50); distance_array_size = opal_carto_base_get_nodes_distance(graph, slot0, "Infiniband", distance_array); for (i=0; i < distance_array_size; i++) { node_distance = opal_value_array_get_item(distance_array, i); opal_output(0,"Node %s distance from slot0 is %d\n",node_distance->node->node_name, node_distance->node_distance); } OBJ_RELEASE(distance_array); opal_carto_base_free_graph(graph); /** * */ opal_output(0," \n\nget_host_graph Ethernet\n"); opal_carto_base_get_host_graph(&graph,"Ethernet"); opal_graph_print(graph); slot0 = opal_carto_base_find_node(graph, "slot0"); if (NULL == slot0) { opal_output(0,"couldnt find slot0 in the graph exiting\n"); opal_carto_base_free_graph(graph); return -1; } end_node = opal_carto_base_find_node(graph, "eth1"); if (NULL == end_node) { opal_output(0,"couldnt find mthca1 in the graph exiting\n"); opal_carto_base_free_graph(graph); return -1; } distance = opal_carto_base_spf(graph, slot0, end_node); opal_output(0,"\nThe distance between slot0 and eth1 is %d\n",distance); distance_array = OBJ_NEW(opal_value_array_t); opal_value_array_init(distance_array, sizeof(opal_carto_node_distance_t)); opal_value_array_reserve(distance_array, 50); distance_array_size = opal_carto_base_get_nodes_distance(graph, slot0, "Ethernet", distance_array); for (i=0; i < distance_array_size; i++) { node_distance = opal_value_array_get_item(distance_array, i); opal_output(0,"Node %s distance from slot0 is %d\n",node_distance->node->node_name, node_distance->node_distance); } OBJ_RELEASE(distance_array); opal_carto_base_free_graph(graph); /** * */ opal_output(0," \n\nget_host_graph Memory\n"); opal_carto_base_get_host_graph(&graph,"Memory"); opal_graph_print(graph); slot0 = opal_carto_base_find_node(graph, "slot0"); if (NULL == slot0) { opal_output(0,"couldnt find slot0 in the graph exiting\n"); opal_carto_base_free_graph(graph); return -1; } end_node = opal_carto_base_find_node(graph, "mem3"); if (NULL == end_node) { opal_output(0,"couldnt find mthca1 in the graph exiting\n"); opal_carto_base_free_graph(graph); return -1; } distance = opal_carto_base_spf(graph, slot0, end_node); opal_output(0,"\nThe distance between slot0 and mem3 is %d\n",distance); distance_array = OBJ_NEW(opal_value_array_t); opal_value_array_init(distance_array, sizeof(opal_carto_node_distance_t)); opal_value_array_reserve(distance_array, 50); distance_array_size = opal_carto_base_get_nodes_distance(graph, slot0, "Memory", distance_array); for (i=0; i < distance_array_size; i++) { node_distance = opal_value_array_get_item(distance_array, i); opal_output(0,"Node %s distance from slot0 is %d\n",node_distance->node->node_name, node_distance->node_distance); } OBJ_RELEASE(distance_array); opal_carto_base_free_graph(graph); } MPI_Barrier(MPI_COMM_WORLD); MPI_Finalize(); return 0; }