C MPI数组传递

C MPI数组传递,c,arrays,mpi,C,Arrays,Mpi,为什么我不能从底部访问muhray 8行?以“!!”开头的打印行工作正常,但我似乎无法在最后获得正确的值 以下是我的输出: [computer@node01 ~]$ mpiexec -n 8 ./presum 1000 !! proc0's array is size 125 and goes from 1 to 1 proc0's array is size 125 and goes from 4693173 to 1819307369 !! proc2's array is size 125

为什么我不能从底部访问muhray 8行?以“!!”开头的打印行工作正常,但我似乎无法在最后获得正确的值

以下是我的输出:

[computer@node01 ~]$ mpiexec -n 8 ./presum 1000
!! proc0's array is size 125 and goes from 1 to 1
proc0's array is size 125 and goes from 4693173 to 1819307369
!! proc2's array is size 125 and goes from 1 to 1
proc2's array is size 125 and goes from 4693173 to 1819307369
!! proc3's array is size 125 and goes from 1 to 1
proc3's array is size 125 and goes from 4693173 to 1819307369
!! proc1's array is size 125 and goes from 1 to 1
proc1's array is size 125 and goes from 4693173 to 1819307369
!! proc4's array is size 125 and goes from 1 to 1
proc4's array is size 125 and goes from 4693173 to 1819307369
!! proc5's array is size 125 and goes from 1 to 1
proc5's array is size 125 and goes from 4693173 to 1819307369
!! proc6's array is size 125 and goes from 1 to 1
proc6's array is size 125 and goes from 4693173 to 1819307369
!! proc7's array is size 125 and goes from 1 to 1
proc7's array is size 125 and goes from 4693173 to 1819307369
以下是相关代码:

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

    //max size of the data array to split up
    #define MAXSIZE 1000000

    //methods
    int checkInput(int nprocs, int argc, char *argv[], int id);

    //mpi send & rec tags
    int ARSIZE = 0;     //array size
    int ARR = 1;        //array
    int MSM = 2;        //slave sum

    int main(int argc, char *argv[]) {

        int     ARsize;             /*size of the array to pre-sum*/
        int     id;                 /*process id number*/
        int     nprocs;             /*number of processors*/
        int     i, j, k;            /*counters*/
        int     muhsize;            /*size of personal array to calculate*/
        int     * muhray;           /**/

        //MPI framework
        MPI_Status status;
        MPI_Init(&argc, &argv);
        MPI_Comm_size(MPI_COMM_WORLD, &nprocs);
        MPI_Comm_rank(MPI_COMM_WORLD, &id);
        MPI_Barrier(MPI_COMM_WORLD);

        //pull input, check values, return ARsize
        ARsize = checkInput(nprocs, argc, argv, id);

        //set up array, serial run, send out chunks
        if (!id) {      

            //variables only the zero node needs
            int     data[ARsize];               /*full original array of numbers*/
            int     chunkSize, upper, lower;    /*vars to determine cunksize to send out*/
            int     smoothCount = 0;            /*BOOL for uneven division chunksize*/

            //fill array with numbers
            for (i = 0; i < ARsize; i++) {
                data[i] = 1;
            }

            //sequential solution here      


            //determine chunkSize
            chunkSize = (int) (ARsize/nprocs);  
            if (ARsize % nprocs != 0) {
                chunkSize = chunkSize + 1;
                smoothCount = 1;
            }

            //send chunks of data to procs
            for (i = 0; i < nprocs; i++) {          
                lower = i * chunkSize;
                upper = ((i+1) * chunkSize) - 1;
                if (i == nprocs-1 && smoothCount == 1) {
                    upper = ARsize-1;
                }
                int intarray[(upper-lower)];
                for (k = lower, j = 0; k <= upper; k++, j++) {
                    intarray[j] = data[k];   
                    }
                if(i > 0) {
                    //send array size
                    MPI_Send(&j, 1, MPI_INT, i, ARSIZE, MPI_COMM_WORLD);
                    //send actual array
                    MPI_Send(intarray, j, MPI_INT, i, ARR, MPI_COMM_WORLD); 
                }
                //zero no send to self, this data used later for all nodes calc
                else {
                    muhsize = j;
                    int muhray[muhsize];
                    for (j = 0; j <= chunkSize; j++) {
                        muhray[j] = intarray[j];
                    }   
                    printf("!! proc%d's array is size %d and goes from %d to %d\n", id, muhsize, muhray[0], muhray[(muhsize-1)]);
                }
            }       
        }
        else {

            MPI_Recv(&muhsize, 1, MPI_INT, 0, ARSIZE, MPI_COMM_WORLD, &status);
            int muhray[muhsize];
            MPI_Recv(muhray, muhsize, MPI_INT, 0, ARR, MPI_COMM_WORLD, &status);
            printf("!! proc%d's array is size %d and goes from %d to %d\n", id, muhsize, muhray[0], muhray[(muhsize-1)]);
            fflush(stdout);
        }

        printf("proc%d's array is size %d and goes from %d to %d\n", id, muhsize, muhray[0], muhray[muhsize]);
        fflush(stdout);
        //MPI_Send(&muhsize, 1, MPI_INT, 0, MSM, MPI_COMM_WORLD);       


        MPI_Finalize();

    }

//pull input, check values, return ARsize
int checkInput(int nprocs, int argc, char *argv[], int id) {

    int size;

    if (nprocs % 2 != 0 || nprocs == 6 || nprocs > 8) {
        if (!id) printf("run with 2^k procs, (1 >= k <= 3)\n");  
        fflush(stdout);
        MPI_Finalize();
        exit(1);
    }
    if (argc != 2) {
        if (!id) printf("Usage: presum [array size (max: %d)]\n", MAXSIZE);
        fflush(stdout);
        MPI_Finalize();
        exit(1);        
    }
    size = atoi(argv[1]);
    if (size <= nprocs) {
        if (!id) printf("search range must be greater than processor count\n");
        fflush(stdout);
        MPI_Finalize();
        exit(1);
    }
    if (size > MAXSIZE) {
        if (!id) printf("array size must be less than or equal to %d\n", MAXSIZE);
        fflush(stdout);
        MPI_Finalize();
        exit(1);
    }
    return size;
}
#包括“mpi.h”
#包括
#包括
#包括
//要拆分的数据数组的最大大小
#定义最大大小1000000
//方法
int checkInput(int nprocs,int argc,char*argv[],int id);
//mpi发送和接收标记
int-ARSIZE=0//数组大小
int-ARR=1//排列
int-MSM=2//从属和
int main(int argc,char*argv[]){
int ARsize;/*要预求和的数组大小*/
int id;/*进程id号*/
int nprocs;/*处理器数量*/
int i,j,k;/*计数器*/
int muhsize;/*要计算的个人数组的大小*/
国际*muhray/**/
//MPI框架
MPI_状态;
MPI_Init(&argc,&argv);
MPI通信大小(MPI通信世界和NPROC);
MPI通信等级(MPI通信世界和id);
MPI_屏障(MPI_通信世界);
//拉入输入,检查值,返回ARsize
ARsize=检查输入(nprocs、argc、argv、id);
//设置阵列、串行运行、发送块
如果(!id){
//变量只有零节点需要
int data[ARsize];/*完整的原始数字数组*/
int chunkSize,上限,下限;/*变量用于确定要发送的cunksize*/
int smoothCount=0;/*BOOL用于不均匀分割块大小*/
//用数字填充数组
对于(i=0;i如果(!id)printf(“使用2^k程序运行,(1>=k似乎您正在打印“muhray[(muhsize-1)]”两次,并在最后打印“muhray[muhsize]”。您应始终打印“muhray[(muhsize-1)]的值。”


带有两个MPI_Recv调用的else部分使用一个本地定义的变量“muhray”,它与最初在主函数中定义的变量不同(它实际上隐藏了在主函数开头定义的muhray)。因此,“printf(!!…)使用的变量与上一个“printf”(“proc。。。“调用。

您遇到的问题很可能与变量的作用域有关。例如:

...
else {
    MPI_Recv(&muhsize, 1, MPI_INT, 0, ARSIZE, MPI_COMM_WORLD, &status);
    int muhray[muhsize];
    MPI_Recv(muhray, muhsize, MPI_INT, 0, ARR, MPI_COMM_WORLD, &status);
    printf("!! proc%d's array is size %d and goes from %d to %d\n", id, muhsize, muhray[0], muhray[(muhsize-1)]);
    fflush(stdout);
}

printf("proc%d's array is size %d and goes from %d to %d\n", id, muhsize, muhray[0], muhray[muhsize]);
您在
else
构造的范围内声明
int-muhray[muhsize];
当您退出此范围时,
muhray
将被销毁,因为它是一个局部变量。您在上一个
printf
中使用的似乎是一个未初始化的
int*muhray;
在主变量之后立即声明


请注意,虽然它们的名称相同,但这两个变量却不同。

请您发布
int checkInput(int nprocs,int argc,char*argv[],int id);
?与其他人一样正确,谢谢……希望我能给你们两个绿色的检查。