📄 mpi_gather_mollc.c.txt
字号:
#include<stdio.h>
#include<mpi.h>
#include<stdlib.h>
#define SIZE 3
static int source=0;
void generate ( int *send_buffer, int size)
{
int i;
for (i = 0; i < size; i++)
send_buffer[i] = i + 10;
}
int main(int argc, char **argv)
{
int gsize, myid, i, j;
int send_buffer[SIZE];
int *recv_buff, *recv_buffer;
int buff_size, tsize;
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &gsize);
MPI_Comm_rank(MPI_COMM_WORLD, &myid);
if (myid == source) generate(send_buffer, SIZE);
MPI_Pack_size(SIZE, MPI_INT, MPI_COMM_WORLD, &buff_size);
recv_buff = (int *)malloc (gsize*buff_size+2*MPI_BSEND_OVERHEAD);
if (!recv_buff)
{
printf("##### Could not allocate receive buffer of size %d", buff_size);
MPI_Abort(MPI_COMM_WORLD, 1);
}
MPI_Buffer_attach(recv_buff, gsize*buff_size+2*MPI_BSEND_OVERHEAD);
printf("Process %d is alive\n", myid);
if (gsize != 1)
{
printf("###### Out of the rank!! ######\n");
MPI_Abort(MPI_COMM_WORLD, 1);
}
for (i = 0; i < 3; i++)
printf("a[%d] = %d; ", i, send_buffer[i]);
printf("\n");
MPI_Gather(&send_buffer, 3, MPI_INT, &recv_buff, 3, MPI_INT, 0, MPI_COMM_WORLD);
MPI_Barrier(MPI_COMM_WORLD);
if (myid == 0)
{
for (j = 0; j < 9; j++)
{
printf("recv_buff[%d] = %d;", j, recv_buff[j]);
for (i = 0; i < 3; i++)
printf("\n");
}
}
MPI_Buffer_detach(&recv_buffer, &tsize);
MPI_Finalize();
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -