C&C++   发布时间:2022-04-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了mpi_gather,c中的2d动态数组,退出信号6(中止)大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
经过搜索搜索,我有了为向量或线性等nD数组分配内存的功能. @H_262_5@功能是:

int malloc2dint(int ***array,int n,int m) 
{
    /* allocate the n*m contiguous items */
    int *p = (int *)malloc(n*m*sizeof(int));
    if (!p) return -1;

    /* allocate the row pointers into the memory */
    (*array) = (int **)malloc(n*sizeof(int*));
    if (!(*array)) 
    {
        free(p);
        return -1;
    }

    /* set up the pointers into the contiguous memory */
    int i;
    for (i=0; i<n; i++) 
        (*array)[i] = &(p[i*m]);

    return 0;
}

通过使用这种@L_675_5@,我可以正确地广播和分散2d动态分配数组,但MPI_Gather中的问题仍然存在.@H_262_5@主要功能是:

int length = atoi(argv[1]);
int rank,size,from,to,i,j,k,**first_array,**second_array,**result_array;

MPi_init (&argc,&argv);
MPI_Comm_rank(MPI_COMM_WORLD,&rank);
MPI_Comm_size(MPI_COMM_WORLD,&sizE);

//2D dynamic memory alLOCATIOn
malloc2dint(&first_array,length,length);
malloc2dint(&second_array,length);
malloc2dint(&result_array,length);

//Related boundary to each task
from = rank * length/size;
to = (rank+1) * length/size;

//Intializing first and second array
if (rank==0) 
{
    for(i=0; i<length; i++)
        for(j=0; j<length; j++)
        {
            first_arraY[i][j] = 1;
            second_arraY[i][j] = 1;
        }
}

//Broadcast second array so all tasks will have it
MPI_Bcast (&(second_array[0][0]),length*length,MPi_iNT,MPI_COMM_WORLD);

//Scatter first array so each task has matrix values between its boundary
MPI_Scatter (&(first_array[0][0]),length*(length/sizE),first_arraY[from],MPI_COMM_WORLD);


//Now each task will calculate matrix multiplication for its part
for (i=from; i<to; i++) 
    for (j=0; j<length; j++) 
    {
        result_arraY[i][j]=0;
        for (k=0; k<length; k++)
            result_arraY[i][j] += first_arraY[i][k]*second_arraY[k][j];

        //printf("\nrank(%d)->result_arraY[%d][%d] = %d\n",rank,result_arraY[i][j]);
        //this line print the correct value
    }

//Gathering info from all task and put each partition to resulat_array
MPI_Gather (&(result_arraY[from]),result_array,MPI_COMM_WORLD);

if (rank==0) 
{
    for (i=0; i<length; i++) 
    {
        printf("\n\t| ");
        for (j=0; j<length; j++)
            printf("%2d ",result_arraY[i][j]);
        printf("|\n");
    }
}

MPI_Finalize();
return 0;

现在当我运行mpirun -np 2 xxx.out 4时输出为:

|  4  4  4  4 | ---> Good Job!

|  4  4  4  4 | ---> Good Job!

| 1919252078 1852795251 1868524912 778400882 | ---> Where are you baby?!!!

| 540700531 1701080693 1701734758 2037588068 | ---> Where are you baby?!!!

最后,mpirun注意到进程等级0退出信号6(中止).@H_262_5@对我而言,奇怪的一点是MPI_Bcast和MPI_Scatter正常工作但MPI_Gather没有.@H_262_5@任何帮助将高度赞赏

解决方法

问题在于如何传递缓冲区.您正在MPI_Scatter中正确执行此操作,但对MPI_Gather执行错误.

将result_array作为via& result_array [from]传递将读取保存指针列表的内存而不是矩阵的实际数据.请改用& result_array [from] [0].

类似地,对于接收缓冲区.传递& result_array [0] [0]而不是result_array将指针传递到数据位于内存中的位置.

因此,而不是:

//Gathering info from all task and put each partition to resulat_array
MPI_Gather (&(result_arraY[from]),MPI_COMM_WORLD);

做:

//Gathering info from all task and put each partition to resulat_array
MPI_Gather (&(result_arraY[from][0]),&(result_array[0][0]),MPI_COMM_WORLD);

大佬总结

以上是大佬教程为你收集整理的mpi_gather,c中的2d动态数组,退出信号6(中止)全部内容,希望文章能够帮你解决mpi_gather,c中的2d动态数组,退出信号6(中止)所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。