⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 allreduce-vector-sum.c

📁 并行计算的一些演示程序
💻 C
字号:

#include "mpi.h" /*MPI head file*/

#include <stdio.h>

int main( int argc, char** argv )
{
int i, rank, size;

double a[10];

double sum[10];


MPI_Status status; 

MPI_Init(&argc, &argv); /*initializing */
MPI_Comm_rank(MPI_COMM_WORLD, &rank); /*Process#*/
MPI_Comm_size(MPI_COMM_WORLD, &size); /*Total processes#*/

//A routine that computes the dot product of two vectors that are distributed 
//across a group of processes and returns the answer at node zero. 

//int MPI_Allreduce(
    // void* sendbuf, 
    // void* recvbuf, 
    // int count    ,
    // MPI_Datatype datatype, 
    // MPI_Op op, 
    // MPI_Comm comm
    // )


for(i=0;i<10;i++)
  a[i] = (double)(rank*10 + i);

MPI_Barrier(MPI_COMM_WORLD);

printf("Proc#%d :",rank);
for(i=0;i<10;i++) printf("%5.1f ", a[i]);
printf("\n");

MPI_Barrier(MPI_COMM_WORLD);

for(i=0;i<10;i++) 
  sum[i] = 0.0;

MPI_Allreduce( a, sum, 10, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD); 

MPI_Barrier(MPI_COMM_WORLD);
  printf("Proc# %d: ",rank);
  for(i=0;i<10;i++) printf("%5.1f ", sum[i]);
  printf("\n");
MPI_Barrier(MPI_COMM_WORLD);

MPI_Finalize(); /*quit from MPI world*/
return (0);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -