2

以下代码在进程 0(主)中创建一个窗口,其他进程在其中放入一些值,我每次都试图从其他进程中获取主窗口来打印它,但我得到的结果完全令人困惑。这是代码:

int main ( int argc, char *argv[] )
{
    int id;
    MPI_Init ( &argc, &argv );

    MPI_Comm_rank ( MPI_COMM_WORLD, &id );
    MPI_Win win;

    if (id == 0)
    {
        int *arr;
        int in = 0;
        MPI_Alloc_mem(10 * sizeof(int), MPI_INFO_NULL, &arr);
        for(in = 1; in < 10; in++)
        {
            arr[in] = -1;
        }
        arr[0] = 0;
        MPI_Win_create(arr, 10 * sizeof(int), sizeof(int), MPI_INFO_NULL,MPI_COMM_WORLD, &win);
        int ready = 0;
        while (!ready)
        {
            MPI_Win_lock(MPI_LOCK_SHARED, 0, 0, win);
            ready = fini(arr);
            MPI_Win_unlock(0, win);
        }
        printf("All workers checked in using RMA\n");

        MPI_Win_free(&win);
        MPI_Free_mem(arr);

        printf("Master done\n");
    }
    else
    {
        int one = id;
        int *local;
        int i = 0;
        MPI_Alloc_mem(sizeof(int)*10, MPI_INFO_NULL, &local);
        MPI_Win_create(NULL, 0, 1, MPI_INFO_NULL, MPI_COMM_WORLD, &win);

        MPI_Win_lock(MPI_LOCK_EXCLUSIVE, 0, 0, win);

        for(i = 0; i < 10; i++)
        {
            MPI_Get(&local[i], 1, MPI_INT, 0, i,1, MPI_INT, win);
        }

        printf("Worker before %d: ", id);
        pt(local); //Printing array "local"
        MPI_Put(&one, 1, MPI_INT, 0, id, 1, MPI_INT, win);
        for(i = 0; i < 10; i++)
        {
            MPI_Get(&local[i], 1, MPI_INT, 0, i,1, MPI_INT, win);
        }

        printf("Worker %d done: ", id);
        pt(local);

        MPI_Win_unlock(0, win);

        MPI_Win_free(&win);
    }

    MPI_Finalize ( );

    return 0;
}

int fini ( int table[] )
{
    int i = 0;

    while(i < 10)
    {
        if(table[i] == -1) return 0;
        i++;
    }

    return 1;
}

void pt(int t[])
{
    int i = 0;
    while(i < 10)
    {
        printf("%d ",t[i]);
        i++;
    }
    printf("\n");
}

这实际上给了我以下结果:

Worker before 4: 1152288776 32731 1152288776 32731 4 0 0 0 48 0 
Worker 4 done: 1152288776 32731 1152288776 32731 4 0 0 0 48 0 
Worker before 1: 1525372936 32743 1525372936 32743 4 0 0 0 48 0 
Worker 1 done: 1525372936 32743 1525372936 32743 4 0 0 0 48 0 
Worker before 3: 1422645256 32661 1422645256 32661 4 0 0 0 48 0 
Worker 3 done: 1422645256 32661 1422645256 32661 4 0 0 0 48 0 
Worker before 6: 1553508328 32675 34348016 0 0 0 33 0 1553508280 32675 
Worker 6 done: 1553508328 32675 34348016 0 0 0 33 0 1553508280 32675 
Worker before 5: -1745405976 32676 33995760 0 0 0 33 0 -1745406024 32676 
Worker 5 done: -1745405976 32676 33995760 0 0 0 33 0 -1745406024 32676 
Worker before 9: -990742552 32568 20737008 0 0 0 33 0 -990742600 32568 
Worker 9 done: -990742552 32568 20737008 0 0 0 33 0 -990742600 32568 
Worker before 2: 1455122440 32635 1455122440 32635 4 0 0 0 48 0 
Worker 2 done: 1455122440 32635 1455122440 32635 4 0 0 0 48 0 
Worker before 7: -1086933016 32650 18463728 0 0 0 33 0 -1086933064 32650 
Worker 7 done: -1086933016 32650 18463728 0 0 0 33 0 -1086933064 32650 
Worker before 8: 1328670696 32548 24464368 0 0 0 33 0 1328670648 32548 
Worker 8 done: 1328670696 32548 24464368 0 0 0 33 0 1328670648 32548

你能帮我弄清楚我的代码有什么问题吗?谢谢。

编辑:显然问题是 MPI_Get 没有填充“本地”缓冲区......

4

0 回答 0