Funktionsübersicht . . Virtuelle Topologien

Hilfesystem für den Message Passing Interface Standard MPI


Beispiel

Erzeugt eine Graphtopologie, demonstriert die Rückgewinnung der Graphinformation und bildet schließlich die Summe aller Ränge im Graph.

#include <mpi.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/param.h>


#define	MASTER	0
#define	TAG	1
#define	FALSE	0
#define	TRUE	1

char*	PrintTopoType(int, char*);

int main(int argc, char *argv[])
{
MPI_Status	status;
MPI_Comm	GraphComm;
char		hostname[80];
char		String[80];
int		i, j;
int 		rank, nTasks;
int		TopoType, nNodes, nEdges, *rIndex, *rEdges;
int		Sum, Recv;
int		GraphRank, nNeighbors, *NeighborList;
int		Index[] = {1, 4, 5, 6};		/* index[rank] indicats the first
						Element with rank+1 in Edges */
int		Edges[] = { 1, 			/* neighbors of 0 */
			    0, 2, 3, 		/* neighbors of 1 */
			    1, 			/* neighbors of 2 */
			    1};			/* neighbors of 3 */

/* build this graph topology     0
				 \
				  1
				 / \
				2   3
*/

	MPI_Init(&argc, &argv);

	MPI_Comm_rank(MPI_COMM_WORLD, &rank);
	MPI_Comm_size(MPI_COMM_WORLD, &nTasks);
	gethostname(hostname, 79);


	MPI_Graph_create(MPI_COMM_WORLD, 4, Index, Edges, TRUE, &GraphComm);

	if (rank == MASTER) {
		MPI_Topo_test(GraphComm, &TopoType);
		printf ("Topologytype is %s\n", PrintTopoType(TopoType, String));
		MPI_Graphdims_get(GraphComm, &nNodes, &nEdges);
		rIndex = (int*) malloc (nNodes * sizeof(int));
		rEdges = (int*) malloc (nEdges * sizeof(int));
		MPI_Graph_get(GraphComm, nNodes, nEdges, rIndex, rEdges);
	        printf("Index : "); for(i = 0; i < nNodes; printf("%d ", rIndex[i++]));
		printf("Edges : "); for(i = 0; i < nEdges; printf("%d ", rEdges[i++]));
		printf("\n");
	}

	MPI_Comm_rank(GraphComm, &GraphRank);		
	MPI_Graph_neighbors_count(GraphComm, GraphRank, &nNeighbors);
	NeighborList = (int*) malloc (nNeighbors * sizeof(int));
	MPI_Graph_neighbors(GraphComm, GraphRank, nNeighbors, NeighborList);

	printf("%-15.12s: Rank in GraphComm = %d : Neighbor-Ranks are : ", \
		hostname, GraphRank);
	for (i = 0; i < nNeighbors; i++) printf ("%d ", NeighborList[i]);
	printf("\n");

/* compute globalsum in GraphComm */
	Sum = GraphRank;
	for (i = nNeighbors-1; i > -1; i--) 
		if (GraphRank > NeighborList[i]) 
			MPI_Send(&Sum, 1, MPI_INT, NeighborList[i], TAG, GraphComm);
		else {
			MPI_Recv(&Recv, 1, MPI_INT, NeighborList[i], TAG, GraphComm,
				 &status);
			Sum += Recv;
		}
	printf("%-15.12s: Sum = %d\n", hostname, Sum);

	MPI_Finalize();
	return 0;
}

char* PrintTopoType(int tt, char *str)
{
	if (str != NULL)
		switch (tt) {
			case MPI_UNDEFINED	: strcpy(str, "MPI_UNDEFINED");	break;
			case MPI_GRAPH		: strcpy(str, "MPI_GRAPH"); break;
			case MPI_CART		: strcpy(str, "MPI_CART"); break;
		}
	return str;
}


Makefile C-Quelltext