MPI_UNPACK截断,缓冲区大小无效

时间:2016-09-05 23:53:14

标签: c malloc mpi

我正在使用MPI来广播包含4个整数和1个浮点数的压缩消息。

我计算缓冲区大小的方法不正确吗?我在此函数的MPI_BCast上收到消息截断错误。这应该意味着我的缓冲区大小不足以存储接收的数据。

我已经以完全相同的方式计算了发送/打包数据的缓冲区大小。

   void bcast_sharedData(SharedData *d) {
        fprintf(stderr, "Broadcasting Shared Data\n");

        int position = 0;
        int buff_size = sizeof(int) * 4 + sizeof(float);
        char * buffer = malloc(buff_size);

        int result = MPI_SUCCESS;
        while(result == MPI_SUCCESS) {
            result = MPI_Pack(&d->max_tree_depth, 1, MPI_INT, buffer, 
                                buff_size, &position, MPI_COMM_WORLD);
            result = MPI_Pack(&d->bit_depth, 1, MPI_INT, buffer, buff_size, 
                                &position, MPI_COMM_WORLD);
            result = MPI_Pack(&d->dimensions, 1, MPI_INT, buffer, buff_size, 
                                &position, MPI_COMM_WORLD); 
            result = MPI_Pack(&d->origin, 1, MPI_INT, buffer, buff_size, 
                                &position, MPI_COMM_WORLD);
            result = MPI_Pack(&d->colour_max, 1, MPI_FLOAT, buffer, 
                                buff_size, &position, MPI_COMM_WORLD);  
            break;
        }

        if (result != MPI_SUCCESS) {
            fprintf(stderr, 
                    "ID 0: Could not Pack Shared Data for Broadcasting");
            MPI_Abort(MPI_COMM_WORLD, -1);
        }

        if (MPI_Bcast(
            buffer, 5, MPI_PACKED, 0, MPI_COMM_WORLD) != MPI_SUCCESS) {

            fprintf(stderr, "ID 0: Could not Broadcast Shared Data\n");
            MPI_Abort(MPI_COMM_WORLD, -1);
        };

        MPI_Barrier(MPI_COMM_WORLD);

        fprintf(stderr, "Broadcasting Kernel\n");
        if (MPI_Bcast(
            &d->kernel, d->dimensions, MPI_FLOAT, 0, MPI_COMM_WORLD) != MPI_SUCCESS) {

            fprintf(stderr, "ID 0: Could not Broadcast Shared Data -> Kernel\n");
            MPI_Abort(MPI_COMM_WORLD, -1);
        }

        MPI_Barrier(MPI_COMM_WORLD);
        fprintf(stderr, "DONE Broadcasting Kernel\n");
    }

    void recv_sharedData(int me, SharedData *d) {
        fprintf(stderr, "%d: RECEIVING SHARED DATA\n", me);
        int buff_size = sizeof(int) * 4 + sizeof(float);
        char * buffer = malloc(buff_size);
        int position = 0;

        if (MPI_Bcast(
                buffer, 5, MPI_PACKED, 0, MPI_COMM_WORLD) != MPI_SUCCESS) {
            fprintf(stderr, "ID %d: Could not receive SharedData", me);
            MPI_Abort(MPI_COMM_WORLD, -1);
        }

        fprintf(stderr, "%d: UNPACKING SHARED DATA\n", me);
        MPI_Unpack(buffer, 1, &position, &d->max_tree_depth, buff_size, 
            MPI_INT, MPI_COMM_WORLD);
        MPI_Unpack(buffer, 1, &position, &d->bit_depth, buff_size, 
            MPI_INT, MPI_COMM_WORLD);
        MPI_Unpack(buffer, 1, &position, &d->dimensions, buff_size, 
            MPI_INT, MPI_COMM_WORLD);
        MPI_Unpack(buffer, 1, &position, &d->origin, buff_size, 
            MPI_INT, MPI_COMM_WORLD);   
        MPI_Unpack(buffer, 1, &position, &d->colour_max, buff_size, 
            MPI_FLOAT, MPI_COMM_WORLD); 

        MPI_Barrier(MPI_COMM_WORLD);
        fprintf(stderr, "%d: Done Recieving SharedData\n", me);

        d->kernel = (float **) malloc (d->dimensions * sizeof (float *));
        int i;
        for (i = 0; i < d->dimensions; i++)
        {
            d->kernel[i] = (float *) malloc (d->dimensions * sizeof (float));
        }

        if (MPI_Bcast(
            d->kernel, d->dimensions, MPI_FLOAT, 0, MPI_COMM_WORLD) != MPI_SUCCESS) {
            fprintf(stderr, "ID %d: Could not receive SharedData->kernel", me);
        }

        MPI_Barrier(MPI_COMM_WORLD);
        fprintf(stderr, "%d: Done Recieving SharedData->Kernel\n", me);
    }

1 个答案:

答案 0 :(得分:0)

我有“buffsize”(arg5)和“解压件”(arg2)的顺序不正确 - 应该已经切换了。