代码之家  ›  专栏  ›  技术社区  ›  0x4141

MPI在C中发送接收字符串时出现分段错误

mpi c
  •  0
  • 0x4141  · 技术社区  · 1 年前

    我想发送一个从排名0到最后一个排名的字符串。我已经安装了所有的mpi依赖项,我想内存分配有一些问题:

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <mpi.h>
    
    int main(int argc, char **argv)
    {
        int rank, size;
        char *hello_world = NULL;
        int hello_world_size = (strlen("Hello world :)") + 1) * sizeof(char);
        hello_world = malloc(hello_world_size);
        if (hello_world == NULL) exit(EXIT_FAILURE);
    
        MPI_Init(&argc, &argv);
        MPI_Comm_rank(MPI_COMM_WORLD, &rank);
        MPI_Comm_size(MPI_COMM_WORLD, &size);
    
        if(rank == 0)
        {
            strcpy(hello_world, "Hello world :)");
    
            MPI_Send(
                &hello_world,
                hello_world_size,
                MPI_CHAR,
                size - 1,
                0,
                MPI_COMM_WORLD
            );
        }
    
        if(rank == size - 1)
        {
            MPI_Recv(
                &hello_world,
                hello_world_size,
                MPI_CHAR,
                0,
                0,
                MPI_COMM_WORLD,
                MPI_STATUS_IGNORE
            );
    
            printf("%s from rank %i", hello_world, rank);
        }
    
        free(hello_world);
        hello_world = NULL;
    
        MPI_Finalize();
        exit(EXIT_SUCCESS);
    }
    

    编译: mpicc -std=c11 -o hello_world -g -Wall -Wpedantic hello_world.c 执行: mpirun --use-hwthread-cpus -np 4 ./hello_world mpirun -np 2 ./hello_world

    但我一直有一个分割错误。我认为字符指针分配存在一些问题。

    输出:

    Authorization required, but no authorization protocol specified
    
    Authorization required, but no authorization protocol specified
    
    [debian:55189] *** Process received signal ***
    [debian:55189] Signal: Segmentation fault (11)
    [debian:55189] Signal code: Address not mapped (1)
    [debian:55189] Failing at address: 0x562a3c5652a0
    [debian:55189] [ 0] /lib/x86_64-linux-gnu/libc.so.6(+0x3bfd0)[0x7fe32c154fd0]
    [debian:55189] [ 1] /lib/x86_64-linux-gnu/libc.so.6(+0x155d59)[0x7fe32c26ed59]
    [debian:55189] [ 2] /lib/x86_64-linux-gnu/libc.so.6(+0x5e168)[0x7fe32c177168]
    [debian:55189] [ 3] /lib/x86_64-linux-gnu/libc.so.6(_IO_printf+0xab)[0x7fe32c16b56b]
    [debian:55189] [ 4] ./hello_world(+0x12f8)[0x55d9fb4882f8]
    [debian:55189] [ 5] /lib/x86_64-linux-gnu/libc.so.6(+0x271ca)[0x7fe32c1401ca]
    [debian:55189] [ 6] /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0x85)[0x7fe32c140285]
    [debian:55189] [ 7] ./hello_world(+0x1101)[0x55d9fb488101]
    [debian:55189] *** End of error message ***
    --------------------------------------------------------------------------
    Primary job  terminated normally, but 1 process returned
    a non-zero exit code. Per user-direction, the job has been aborted.
    --------------------------------------------------------------------------
    --------------------------------------------------------------------------
    mpirun noticed that process rank 1 with PID 0 on node debian exited on signal 11 (Segmentation fault).
    
    1 回复  |  直到 1 年前
        1
  •  0
  •   Dražen GraÅ¡ovec    1 年前

    难道不应该发送指向字符串的指针,而不是该指针的地址(可能在堆栈中的某个位置)吗

    char *hello_world 是自动变量,它将被放置在堆栈上,您传递的不是在堆上分配的字符串的地址,而是的地址 hello_world 在堆栈上创建的指针。

    正如我从MPI API中看到的,第一个参数是 void* ,不是 void**

    #include <mpi.h>
    
    int MPI_Send(const void *buf, int count, MPI_Datatype datatype, int dest,
         int tag, MPI_Comm comm)
    
    
    int MPI_Recv(void *buf, int count, MPI_Datatype datatype,
         int source, int tag, MPI_Comm comm, MPI_Status *status)