大数据阵列MPI发送

时间:2015-05-28 03:43:35

标签: c++ mpi

使用send / recv MPI发送大于250的数组。 我该怎么办?使用这种属性会产生缓冲区错误。

我的数组是text1,text2,Text3。 谢谢= D

#include <stdio.h>
#include <mpi.h>
#include <time.h>
#include <math.h>
#include <string.h>
#define maxn 12000 //AoLéo
#include <stdlib.h>

int max(int a,int b);
int LongestCommonSubsequence(char text[]);


static char search[] = "ab"; //array que irá ser buscado nos arrays
static const char alphanum[]               ="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
int stringLength = sizeof(alphanum) - 1;



int max(int a,int b)
{
    return a>b?a:b;
}

   int LongestCommonSubsequence(char text[]){



    int Slength = strlen(search); //Procurado
    int Tlength = strlen(text); //Procura
    int iter,jter=0;


   int common[Slength+1][Tlength+1];


    for(iter=0; iter<=Slength; iter++){
            for(jter=0; jter<=Tlength; jter++){

                    if(iter == 0 || jter == 0){
                        common[iter][jter] = 0;
                    }

                    else if(search[iter-1] == text[jter-1] ){
                            common[iter][jter] = common[iter-1][jter-1] + 1;
                    }
                    else{
                            common[iter][jter]= max(common[iter][jter-1],common[iter-1][jter]);


                    }

            }
    }


 return common[Slength][Tlength];

}



 main(int argc, char **argv)
{   
double startwtime = 0.0, endwtime; //var de tempo de execução
int id;//id : Rank de um processo
int p;//p : Numero de Processos


char texto1[maxn];
char texto2[maxn];
char texto3[maxn];



    //Inicializa os processos.
MPI_Init(&argc,&argv);
//determina o rank de um processo
MPI_Comm_rank(MPI_COMM_WORLD,&id);
//determina o numero de processos executando
MPI_Comm_size(MPI_COMM_WORLD,&p);
MPI_Status status;
    //mostra dados
 printf("Processo iniciado do id: %d\n ",id);
     printf("Numeros de processos p: %d \n",p);
if(id==0)
{
startwtime = MPI_Wtime();//iniciando o contador de tempo

strcpy(texto1,"auhauhsushauhs");
strcpy(texto2, "oplpadasdasdasdasdafadvalk");
strcpy(texto3, "cbfgrwtgjyunyhs");

int lengthText1 = strlen(texto1);//Atribuindo o tamanho do vetor
int lengthText2 = strlen(texto2);//Atribuindo o tamanho do vetor        
int lengthText3 = strlen(texto3);//Atribuindo o tamanho do vetor
printf("TAMANHO DO VETOR 1 >>>%d<<<", lengthText1);

printf("Enviando o tamanho dos vetores para os %d processadores....\n", p);
MPI_Send(&lengthText1, 1, MPI_CHAR, 1, 0, MPI_COMM_WORLD);  
MPI_Send(&lengthText2, 1, MPI_CHAR, 2, 0, MPI_COMM_WORLD);
MPI_Send(&lengthText3, 1, MPI_CHAR, 3, 0, MPI_COMM_WORLD);


printf("Enviando os dados para os %d processadores....\n", p);
MPI_Send(texto1, lengthText1, MPI_CHAR, 1, 0, MPI_COMM_WORLD);  
MPI_Send(texto2, lengthText2, MPI_CHAR, 2, 0, MPI_COMM_WORLD);
MPI_Send(texto3, lengthText3, MPI_CHAR, 3, 0, MPI_COMM_WORLD);
printf("Dados enviados com sucesso!\n");


}

if(id == 1){
int answer; 
int lengthText1;
MPI_Recv(&lengthText1, 1, MPI_CHAR, MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, &status);//Recebendo o tamanho do vetor
MPI_Recv(texto1, lengthText1, MPI_CHAR, MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, &status);//Recebendo o vetor
printf("Dados recebidos com sucesso no processador: %d \n", id);

answer = LongestCommonSubsequence(texto1);
printf("Resposta do processador %d eh de %d incidencias\n", id, answer);
MPI_Send(&answer, 1, MPI_INT, 0, 0, MPI_COMM_WORLD);
}

if(id == 2){
int answer;
int lengthText2;
MPI_Recv(&lengthText2, 1, MPI_CHAR, MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, &status);//Recebendo o tamanho do vetor
MPI_Recv(texto2, lengthText2, MPI_CHAR, MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, &status);//Recebendo o vetor
printf("Dados recebidos com sucesso no processador: %d \n", id);

answer = LongestCommonSubsequence(texto2);
printf("Resposta do processador %d eh de %d incidencias\n", id, answer);
MPI_Send(&answer, 1, MPI_INT, 0, 0, MPI_COMM_WORLD);
}

if(id == 3){
int answer; 
int lengthText3;
MPI_Recv(&lengthText3, 1, MPI_CHAR, MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, &status);//Recebendo o tamanho do vetor
MPI_Recv(texto3, lengthText3, MPI_CHAR, MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, &status);//Recebendo o vetor
printf("Dados recebidos com sucesso no processador: %d \n", id);

answer = LongestCommonSubsequence(texto3);
printf("Resposta do processador %d eh de %d incidencias\n", id, answer);
MPI_Send(&answer, 1, MPI_INT, 0, 0, MPI_COMM_WORLD);
}




//Retorno do Jedi
if(id == 0 ){
int answer=0;
int i;
int respostas[2];
endwtime = MPI_Wtime();

printf("Recebendo dados dos processadores\n");
for(i=1; i<4; i++){
MPI_Recv(&answer, 1, MPI_INT, i, MPI_ANY_TAG, MPI_COMM_WORLD, &status);
respostas[i] = answer;
answer=0;
}
printf("Dados recebidos com sucesso no servidor!\n");
for(i=1; i<4; i++){
printf("Incidencias no noh %d foi de: %d\n", i, respostas[i]);



}
printf("\n\n************ Tempo de execucao foi de %.4f segundos ***********\n", endwtime-startwtime);
  }


MPI_Finalize(); //Finalizar MPI
}

我正在使用动态编程,我是3节+服务器。

1 个答案:

答案 0 :(得分:2)

这并不奇怪。您正在使用MPI数据类型MPI_CHAR来发送和接收字符串长度。这只发送1个字节。使用MPI_INT作为MPI数据类型来发送完整的4字节整数:

// what you are doing:
MPI_Send(&lengthText1, 1, MPI_CHAR, 1, 0, MPI_COMM_WORLD);
// ... and
MPI_Recv(&lengthText1, 1, MPI_CHAR, MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, &status);

// what you should be doing:
MPI_Send(&lengthText1, 1, MPI_INT, 1, 0, MPI_COMM_WORLD);
// ... and
MPI_Recv(&lengthText1, 1, MPI_INT, MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, &status);