Just started to learn MPI - need some help!

For everything that's not in any way related to PureBasic. General chat etc...
User avatar
Fou-Lu
Enthusiast
Enthusiast
Posts: 201
Joined: Tue Jul 12, 2005 8:30 am
Location: I'm pretty sure this is all a nightmare
Contact:

Just started to learn MPI - need some help!

Post by Fou-Lu »

Hi Everybody. The code below is an atempt to make a Pointer Jumping* algorithm to find the roots of a randomly generated tree. The tree is generated correctly, but the result doesn't come as I expected (like, node 1 root is -16502...).

I really hope someone here knows a bit of MPI to help me.:wink:

Code: Select all

#include <stdio.h>
//#include <stdlib.h>
//#include <time.h>
#include "mpi.h"

main(int argc, char** argv){


//this is just initialization, the code itself is just some lines.

	int	my_rank;
	int	p;
	int 	i;
	
	MPI_Init(&argc, &argv);
	MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
	MPI_Comm_size(MPI_COMM_WORLD, &p);

	int max_size=p;

	int S[max_size];//Array of Successors


	int pai[max_size];

   	int aleatorio[max_size];



	MPI_Status	status;

	//criar um vetor com valores de 0 a i em posiçoes aleatórias
	//create an array with values from 0 to i in random positions
	srand(time(NULL));

	for (i=0;i<max_size;i++){

		aleatorio[i]=i;

	}



	for (i=0;i<max_size;i++){

		int place= rand() % (max_size);

		int temp= aleatorio[place];

		aleatorio[place]=aleatorio[i];

		aleatorio[i]=temp;

	}


	//usar o vetor anterior para criar o vetor pai[] que contem as arestas da árvore
	//use the previous array to create the array pai[] (father) which contains the arcs of the tree

	i=0;

	while(i<max_size){

		//criar um nó pai dele mesmo:
		//create a node that is father of itself (a root node)

		pai[aleatorio[i]]=aleatorio[i];

		i++;//pegar o próximo valor aleatório // get the next random value

		//criar uma lista de nós filhos de tamanho aleatório
		//create a list of child nodes of random size

		int size= rand() % (max_size);

		while((size>0)&&(i<max_size)){

			pai[aleatorio[i]]=aleatorio[i-1];//filho do nó anterior

			size--;

			i++;

		}

	}

//HERE. That's where the real algorithm starts (yes, it's just that)

	S[my_rank] = pai[my_rank];

	while (S[my_rank] != S[S[my_rank]])
	{	
		S[my_rank] = S[S[my_rank]]; //I'm not a root, let's see if my successor is
		MPI_Bcast(&S[0], max_size, MPI_INT, my_rank, MPI_COMM_WORLD);//tell everybody that I updated my successor
	}

	if (my_rank == 0) {
		for (i=0; i<max_size; i++)
			printf("\the root of %d é %d", i, S[i]);
	}

	MPI_Finalize();

}/*main*/
*The algorithm consists assigning for each process a single node and an array of successors. Initially the successor of each node is it's father. In each loop the process verifies if the successor of it's node is the same as the node (which means it's a root node) if it's not, it updates it's successor with the successor of its successor.

~Fou-Lu (aka Lørd Cinneris (actually Elias Sant'Ana))

Image Image