两个矢量点使用MPI_Scatterv,但得到malloc错误

两个矢量点使用MPI_Scatterv,但得到malloc错误,c,mpi,C,Mpi,我试着用MPI_Scatterv做两个矢量点,用于任何通信大小。(c语言) 当我使用MPI_散射时,没有问题。所以问题可能在于我对MPI_散射体V的“计数”和“位移”的定义 此外,如果我将代码划分为几个函数,那么它的工作效果相当好。向量[]是函数中的参数。因此,当只在一个完整函数中使用时,可能我对向量[]的定义不正确 有人能帮我吗?n在vector1=(double*)malloc(n*sizeof(double))时未初始化被调用。代码有很多感谢!现在可以了。奇怪的是,当我使用MPI_散射时,

我试着用MPI_Scatterv做两个矢量点,用于任何通信大小。(c语言)

当我使用MPI_散射时,没有问题。所以问题可能在于我对MPI_散射体V的“计数”和“位移”的定义

此外,如果我将代码划分为几个函数,那么它的工作效果相当好。向量[]是函数中的参数。因此,当只在一个完整函数中使用时,可能我对向量[]的定义不正确


有人能帮我吗?

n
vector1=(double*)malloc(n*sizeof(double))时未初始化被调用。代码有很多感谢!现在可以了。奇怪的是,当我使用MPI_散射时,它可以在没有错误报告的情况下给我结果。下次。请准备一个,正确设置代码格式,听取编译器警告(启用
-Wall
!)
    int main(){
        int n; /*size of vectors*/
        int local_n;
        double scalar, *vector1, *vector2;
        double *local_vector1, *local_vector2;
        double local_sum, result;
        int my_rank, comm_sz;
        int i;
        int *scounts, *displs;

        MPI_Init(NULL,NULL);
        MPI_Comm_size(MPI_COMM_WORLD, &comm_sz);
        MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);


        /*read in vectors and scalar*/
        if(my_rank==0){
            vector1 = (double*)malloc(n*sizeof(double));
            vector2 = (double*)malloc(n*sizeof(double));
            printf("Enter the size of vectors\n");
            scanf("%d",&n);
            printf("Enter the scalar\n");
            scanf("%lf",&scalar);
            printf("Enter the first vector\n");
            for(i=0;i<n;i++){
                scanf("%lf", &vector1[i]);
            }
            printf("Enter the second vector\n");
            for(i=0;i<n;i++){
            scanf("%lf", &vector2[i]);
            }
        }

/*distribute the n and scalar*/
MPI_Bcast(&n,1,MPI_INT,0,MPI_COMM_WORLD);
MPI_Bcast(&scalar,1,MPI_DOUBLE,0,MPI_COMM_WORLD);

/*calculate local_n*/
if(my_rank < (n%comm_sz)){
    local_n = n/comm_sz + 1;
}
else{
    local_n = n/comm_sz;
}

/* scatterv inputs*/
scounts = (int*)malloc(comm_sz*sizeof(int));
displs = (int*)malloc(comm_sz*sizeof(int));

for(i=0;i<n%comm_sz;i++){
    scounts[i] = n/comm_sz + 1;
    displs[i] = i*scounts[i];
}
for(i=n%comm_sz;i<comm_sz;i++){
    scounts[i] = n/comm_sz;
    displs[i] = i*scounts[i]+n%comm_sz;
}

/*allocate vectors*/
local_vector1 = (double*)malloc(local_n*sizeof(double));
local_vector2 = (double*)malloc(local_n*sizeof(double));


/*distribute vectors to processes*/
     MPI_Scatterv(vector1,scounts,displs,MPI_DOUBLE,local_vector1,local_n,MPI_DOUBLE,0,MPI_COMM_WORLD);
     MPI_Scatterv(vector2,scounts,displs,MPI_DOUBLE,local_vector2,local_n,MPI_DOUBLE,0,MPI_COMM_WORLD);

    free(vector1);
    free(vector2);

/*calculate vector multiply scalar and dot product the other vector at each process*/
for(i=0;i<local_n;i++){
    local_vector1[i] *= scalar;
    local_vector1[i] *= local_vector2[i];
    local_sum += local_vector1[i];
}

/*add the local results to get the final result*/
MPI_Reduce(&local_sum,&result,1,MPI_DOUBLE,MPI_SUM,0,MPI_COMM_WORLD);

/*print out the result*/
if(my_rank==0){
    printf("The final result:%lf\n", result);
}


free(local_vector1);
free(local_vector2);
free(scounts);
free(displs);

MPI_Finalize();
return 0;
}
    malloc: *** mach_vm_map(size=18446744069599207424) failed (error code=3)
    *** error: can't allocate region
    *** set a breakpoint in malloc_error_break to debug