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

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*/