【问题标题】:MPI error: subscripted value is neither array nor pointerMPI 错误:下标值既不是数组也不是指针
【发布时间】:2012-01-11 17:49:37
【问题描述】:

这个作业需要我开发一个 MPI 程序来实现并行奇偶排序,并把三个函数结合在一起:

  1. MPI_Compare_exchange() 用于 P2P 比较和交换操作
  2. MPI_Sort() 用于并行奇偶排序操作
  3. MPI_Is_sorted()测试并行数组排序是否完成

编译时出现这些错误

OddEvenSort.c:102: error: invalid operands to binary / (have ‘int *’ and ‘int’)
OddEvenSort.c:102: error: subscripted value is neither array nor pointer
OddEvenSort.c:104: error: subscripted value is neither array nor pointer
OddEvenSort.c:112: error: subscripted value is neither array nor pointer
OddEvenSort.c:116: error: subscripted value is neither array nor pointer
OddEvenSort.c:123: error: subscripted value is neither array nor pointer

这是代码:

int MPI_Sort(int n,double * array, int root, MPI_Comm comm){

    int rank, x, m, size, a, i;
    if( rank == 0 )
    {
        array = (double *) calloc( n, sizeof(double) );
        srand( ((unsigned)time(NULL)+rank) );
        for( x = 0; x < n; x++ ) array[x]=((double)rand()/RAND_MAX)*m;
    }

    MPI_Scatter(array, n/size, MPI_DOUBLE, &a[0], n/size, MPI_DOUBLE, 0, comm );

    merge_sort(n/size,&a[0]);

    for(i=0;i<size;i++){

        if( (i+rank)%2 ==0 ){

            if( rank < size-1 ) 

                exchange(n/size,&a[0],rank,rank+1,comm);

            } else{

                if( rank > 0 ) exchange(n/size,&a[0],rank-1,rank,comm);

            }

            MPI_Barrier(comm);
        }

    MPI_Gather(&a[0], n/size, MPI_DOUBLE, array, n/size, MPI_DOUBLE, 0, comm);

    if( rank == 0 )
    {
        for( x = 0; x < n; x++ )  printf( "Output : %f\n", array[x] );

    }


}

我认为它指的是这个:&amp;a[0],但我不知道如何解决它。有什么想法吗?

【问题讨论】:

  • 你为什么使用 &a[0]?你认为这意味着什么?
  • &a[0] 我的意思是数组a的第一个元素的地址。

标签: c arrays sorting mpi


【解决方案1】:

'a'被定义为一个int,而不是一个数组,所以不能传递元素0的地址。

我认为您的意图是将“a”定义为双精度,并且与“数组”的大小相同。

double a[n];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多