使用MPI_Send

时间:2015-12-10 17:54:10

标签: c arrays mpi

我是MPI的新人,

我正在尝试使用MPI_Send函数和Recv函数发送数据。节点0接受数字输入并读取.dat文件,并且在执行时定义的其余节点计算.dat文件中的频率(它出现的次数)。我在尝试拆分数组时遇到了麻烦,因为它没有分配正确的值。任何人都可以指导我如何做到这一点?我在sendData数组中得到随机垃圾数。使用MPI_Bcast可以更容易地实现这样的事情吗?

#include <mpi.h>
#include <stdio.h>
#include <stdlib.h>

int send();
int main(int argc, char *argv[])
{
        int input, m, npes, myrank, newComm, data[1000];
        MPI_Init(&argc, &argv);
        MPI_Comm_size(MPI_COMM_WORLD, &npes);
        MPI_Comm_rank(MPI_COMM_WORLD, &myrank);
        int divideArray = 1000/npes;
        int sendData[divideArray];
        int z;
        int count = 0;
        if(myrank == 0)
        {
                FILE *file = fopen("input1.dat", "r");
                int i=0;
                int num, input;
                while(fscanf(file, "%d", &num) > 0)
                {
                        data[i] = num;
                        i++;
                }
                int  j, k;
                printf("Enter the number to find (1 to 100) : \n");
                scanf("%d", &input);
                //MPI_Bcast(&input, 1, MPI_INT, 0, MPI_COMM_WORLD);
                for(m = 0; m < npes; m++)
                {
                        //printf("Dataset %d: \n", m + 1);
                        for(k = divideArray * m; k < divideArray * (m+1);  k++)
                        {
                                //printf("%d\n", data[k]);
                                int counter = 0;
                                sendData[counter++] += data[k];
                                printf("%d\n", sendData[k]);
                                //MPI_Send(&value, 1, MPI_INT, 1, 0, MPI_COMM_WORLD);
                        }
                        printf("%d\n", sendData[m]);
                        MPI_Send(&sendData, 250, MPI_INT, m, 0, MPI_COMM_WORLD);
                        MPI_Send(&input, 1, MPI_INT, m, 1, MPI_COMM_WORLD);
                        //send();
                }
                //printf("The total frequency of %d is %d \n", input, count);
        }
        int zz;
        for(z = 1; z < npes; z++)
        {
                if(myrank == z)
                {
                        MPI_Recv(&sendData, 250, MPI_INT, 0, 0, MPI_COMM_WORLD, NULL);
                        MPI_Recv(&input, 1, MPI_INT, 0, 1, MPI_COMM_WORLD, NULL);
                        for(zz = 0; zz < divideArray; zz++)
                        {
                                if(sendData[z] == input)
                                {
                                        count = count + 1;
                                        printf("Found One \n");
                                }
                        }
                        printf("My ID is %d and the frequency of %d is %d \n", myrank, input, count );
                }
        }

        MPI_Finalize();
        return 0;

}

0 个答案:

没有答案