Я делаю проект, чтобы выяснить, как часто каждый элемент встречается в 5 группах. значения по умолчанию 10, команда mpirun -n 10 ./a.out дала правильные результаты, кроме результата в процессе 0. Когда я использую strcat для добавления строки, только процесс 0 добавляет странный символ '4' в конце кода. результат здесь: снимок экрана
Проблема в кодах:
if(indexListofEachItem[rank][j] == indexListofEachItem[i+1][k])
{
char ctemp[1];
ctemp[0] = indexListofEachItem[i+1][k];
strcat(frequence[n], ctemp);
count[n]++;
}
Вот полные коды:
#include <stdio.h>
#include <mpi.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <stdbool.h>
int main(int argc, char *argv[]) {
int gen_val,first_val, second_val, temp, rank,proc_size;
int allItem = 0;
int n = 0;
int listofAllItem[50];
char indexListofEachItem[50][50];
bool found = false;
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &proc_size);
int numbOfItem = 5 ;
int val[5][5] ={5,4,6,7,8,
5,0,4,9,8,
1,6,2,3,9,
3,7,1,0,5,
4,9,8,0,2};
int count[10] = {0,0,0,0,0,0,0,0,0,0};
int firstNum[50];
int secondNum[50];
char frequence[9][50] = {0};
int allListItem[5];
int index;int outofrangeVal = 10;
for(int i= 0; i< 5;i++)
{
if(rank == i)
{
int j = 0;
int min = 0;
int count = 0;
for(int ii = 0 ; ii < numbOfItem ; ii++)
storedVal[ii] = outofrangeVal;
printf("process %d: %d, %d, %d, %d, %d\n",rank, val[rank][0], val[rank][1], val[rank][2],val[rank][3], val[rank][4]);
if(rank != 0)
MPI_Send(val[rank], 5, MPI_INT, 0, 0, MPI_COMM_WORLD);
}
MPI_Barrier(MPI_COMM_WORLD);
}
if(rank == 0)
{
index = 0;
for(int i = 0; i < 5; i++)
{
listofAllItem[i] = val[0][i];
sprintf(indexListofEachItem[i],"%d", index);
allItem++;
}
for(int i = 1; i < 5; i++)
{
MPI_Recv(allListItem, 5, MPI_INT, i, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
index = i;
char c[5];
sprintf(c, "%d", i);
for(int k = 0; k < 5; k++)
{
for(int j = 0; j < allItem; j++)
if(allListItem[k] == listofAllItem[j])
{
strcat(indexListofEachItem[j], c);
found = true;
}
if(found == false)
{
listofAllItem[allItem] = allListItem[k];
strcpy(indexListofEachItem[allItem], c);
allItem++;
}
found = false;
}
}
for(int i = 0; i < proc_size - 1; i++)
for(int j = i+1; j< proc_size; j++)
{
if(listofAllItem[i] > listofAllItem[j])
{
int temp = listofAllItem[j];
listofAllItem[j] = listofAllItem[i];
listofAllItem[i] = temp;
char ctemp[50];
strcpy(ctemp, indexListofEachItem[j]);
strcpy(indexListofEachItem[j],indexListofEachItem[i]);
strcpy(indexListofEachItem[i],ctemp);
}
}
}
MPI_Barrier(MPI_COMM_WORLD);
MPI_Bcast(listofAllItem, 10, MPI_INT, 0, MPI_COMM_WORLD);
MPI_Bcast(indexListofEachItem, 2500, MPI_CHAR, 0, MPI_COMM_WORLD);
MPI_Bcast(&allItem, 1, MPI_INT, 0, MPI_COMM_WORLD);
MPI_Barrier(MPI_COMM_WORLD);
n = 0;
for(int r = 0 ; r < proc_size; r++)
if(r == rank)
{
for(int i = rank; i < proc_size-1; i++)
{
firstNum[n] = listofAllItem[rank];
secondNum[n] = listofAllItem[i+1];
for(int j = 0 ; j < strlen(indexListofEachItem[rank]); j++)
{
for(int k = 0; k < strlen(indexListofEachItem[i+1]); k++)
{
if(indexListofEachItem[rank][j] == indexListofEachItem[i+1][k])
{
char ctemp[1];
ctemp[0] = indexListofEachItem[i+1][k];
strcat(frequence[n], ctemp);
count[n]++;
}
}
}
n++;
}
}
MPI_Barrier(MPI_COMM_WORLD);
n = sizeof(secondNum)/sizeof(secondNum[0]);
int i = 0;
if(rank != proc_size-1)
do
{
printf("rank %d, %d and %d appear %d times in index %s", rank, firstNum[i],secondNum[i],count[i], frequence[i]);
printf("\n");
i++;
}while(i< n && secondNum[i]!= 0);
printf("\n");
MPI_Barrier(MPI_COMM_WORLD);
MPI_Finalize();
return 0;
}