MPI程序使用MPI_Scatter和MPI_Reduce

发布于 2025-01-30 02:14:18 字数 299 浏览 2 评论 0原文

编写一个有效计算数组元素总和的MPI程序。 程序1:任务与MPI_Scatter和MPI_Reduce通信。

  1. 程序可以假定过程数量是两个的幂。
  2. 程序应在范围0到100中添加2^15 = 65536随机双打。 任务0必须生成数字,将它们存储在数组中并将其分配到任务。
  3. 每个任务都执行分配的数字的串行总和。当地总和是 使用树结构化并行总和一起添加。
  4. 并行总和完成后,任务0应计算一个串行总和 相同的数字(验证结果)。
  5. 任务0必须打印并行总和,串行和 并行总和(包括数据分布)。

Write an MPI program that efficiently compute the sum of array elements.
Program 1: Tasks communicate with MPI_Scatter and MPI_Reduce.

  1. The programs can assume that the number of processes is a power of two.
  2. The programs should add 2^15 = 65536 random doubles in the range 0 to 100.
    Task 0 must generate the numbers, store them in array and distribute them to the tasks.
  3. Each task does a serial sum of the numbers it is assigned. The local sums are then
    added together using a tree structured parallel sum.
  4. After the parallel sum is complete, task 0 should compute a serial sum of the
    same numbers (to verify the result).
  5. Task 0 must print the parallel sum, the serial sum and the time required for the
    parallel sum (including data distribution).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

娇妻 2025-02-06 02:14:18
#include <stdio.h>
#include <mpi.h>

int main(int argc,char *argv[]){
MPI_Init(NULL,NULL); // Initialize the MPI environment
int rank; 
int comm_size;
MPI_Comm_rank(MPI_COMM_WORLD,&rank);
MPI_Comm_size(MPI_COMM_WORLD,&comm_size);

int number1[2];           
int number[4];
if(rank == 0){
  number[0]=1;
  number[1]=2;
  number[2]=3;
  number[3]=4;   
  //number[4]=5;            
 }



double local_start, local_finish, local_elapsed, elapsed;
MPI_Barrier(MPI_COMM_WORLD);
local_start = MPI_Wtime();


//All processes
 MPI_Scatter(number, 2, MPI_INT, &number1, 2, MPI_INT, 0, MPI_COMM_WORLD);
//printf("I'm process %d , I received the array : ",rank);
   
int sub_sum = 0;
for(int i=0 ; i<2 ; i++){
   // printf("%d ",number1[i]);
    sub_sum = sub_sum + number1[i];
}
printf("\n");        
int sum = 0;
MPI_Reduce(&sub_sum, &sum, 1, MPI_INT, MPI_SUM,0,MPI_COMM_WORLD);            
local_finish = MPI_Wtime();
local_elapsed = local_finish -local_start;
MPI_Reduce(&local_elapsed,&elapsed,1,MPI_DOUBLE,MPI_MAX,0,MPI_COMM_WORLD);


   if(rank == 0)
{
  printf("\nthe sum of array is: %d\n",sum);
  printf("Elapsed time = %e seconds\n",elapsed);
}
 


MPI_Finalize();
return 0;

}

#include <stdio.h>
#include <mpi.h>

int main(int argc,char *argv[]){
MPI_Init(NULL,NULL); // Initialize the MPI environment
int rank; 
int comm_size;
MPI_Comm_rank(MPI_COMM_WORLD,&rank);
MPI_Comm_size(MPI_COMM_WORLD,&comm_size);

int number1[2];           
int number[4];
if(rank == 0){
  number[0]=1;
  number[1]=2;
  number[2]=3;
  number[3]=4;   
  //number[4]=5;            
 }



double local_start, local_finish, local_elapsed, elapsed;
MPI_Barrier(MPI_COMM_WORLD);
local_start = MPI_Wtime();


//All processes
 MPI_Scatter(number, 2, MPI_INT, &number1, 2, MPI_INT, 0, MPI_COMM_WORLD);
//printf("I'm process %d , I received the array : ",rank);
   
int sub_sum = 0;
for(int i=0 ; i<2 ; i++){
   // printf("%d ",number1[i]);
    sub_sum = sub_sum + number1[i];
}
printf("\n");        
int sum = 0;
MPI_Reduce(&sub_sum, &sum, 1, MPI_INT, MPI_SUM,0,MPI_COMM_WORLD);            
local_finish = MPI_Wtime();
local_elapsed = local_finish -local_start;
MPI_Reduce(&local_elapsed,&elapsed,1,MPI_DOUBLE,MPI_MAX,0,MPI_COMM_WORLD);


   if(rank == 0)
{
  printf("\nthe sum of array is: %d\n",sum);
  printf("Elapsed time = %e seconds\n",elapsed);
}
 


MPI_Finalize();
return 0;

}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文