gpt4 book ai didi

c++ - 大数据阵列 MPI 发送

转载 作者:行者123 更新时间:2023-11-30 03:52:46 30 4
gpt4 key购买 nike

使用发送/接收 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 节 + 服务器。

最佳答案

这并不奇怪。您正在使用 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);

关于c++ - 大数据阵列 MPI 发送,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30496849/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com