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

📄 kkk.c

📁 simple calculate area in MPI
💻 C
字号:
#include <stdio.h>
#include <mpi.h>

/*An example using MPI_Bcast*/

int main (int argc, char *argv[]) 
{
int myrank, size, i;
double pi;
double area = 0;
double total, *back_ray;
pi=0.0;

MPI_Init(&argc, &argv); /* Initialize MPI */
MPI_Comm_rank(MPI_COMM_WORLD, &myrank); /* Get my rank */
MPI_Comm_size(MPI_COMM_WORLD, &size); /* Get the total number of processes */

//printf("Iam process %d of %d: My value of Pi=%f!\n", myrank, size,pi);

if(myrank==0)/*This initializes the value of pi on the root process*/
{
pi=3.14;
printf("I am broadcasting the value of Pi\n");
back_ray=(double*)malloc(size*sizeof(double));
}

MPI_Bcast(&pi,1,MPI_DOUBLE,0,MPI_COMM_WORLD);/*Note that every process calls MPI_Bcast!*/
area=pi*myrank*myrank;


MPI_Gather(&area,1,MPI_DOUBLE, back_ray,1,MPI_DOUBLE, 0,MPI_COMM_WORLD);



if(myrank == 0){
total=0;
for(i=0;i<size;i++){
total=total+back_ray[i];
}
printf("total= %f \n ",total);
}

MPI_Finalize(); /* Terminate MPI */
}

⌨️ 快捷键说明

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