我想发送一个从 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).
您不应该发送指向字符串的指针,而不是该指针的地址(可能位于堆栈上的某个位置)
#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)