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

时间:2018-02-23 21:16:34

标签: c mpi

我尝试使用MPI_Scatterv为任何comm_size做两个矢量点。 (c语言)

    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

当我使用MPI_Scatter时,没有问题。那么问题可能在于我对MPI_Scatterv的“计数”和“位移”的定义?

此外,如果我将代码分成几个函数,它也可以很好地工作。 vectors []是函数中的参数。因此,在仅使用一个完整函数时,我对vector []的定义可能不正确。

有人可以帮帮我吗?

0 个答案:

没有答案