如何通过MPI加速此问题

如何通过MPI加速此问题

本文介绍了如何通过MPI加速此问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(1).我想知道如何使用MPI在下面的代码循环中加速耗时的计算吗?

(1). I am wondering how I can speed up the time-consuming computation in the loop of my code below using MPI?

 int main(int argc, char ** argv)
 {
 // some operations
 f(size);
 // some operations
 return 0;
 }

 void f(int size)
 {
 // some operations
 int i;
 double * array =  new double [size];
 for (i = 0; i < size; i++) // how can I use MPI to speed up this loop to compute all elements in the array?
 {
 array[i] = complicated_computation(); // time comsuming computation
 }
 // some operations using all elements in array
 delete [] array;
 }

如代码中所示,我想在与MPI并行化的部分之前和之后进行一些操作,但是我不知道如何指定并行部分的开始和结束位置.

As shown in the code, I want to do some operations before and after the part to be paralleled with MPI, but I don't know how to specify where the parallel part begins and ends.

(2)我当前的代码正在使用OpenMP来加快转换速度.

(2) My current code is using OpenMP to speed up the comutation.

 void f(int size)
 {
 // some operations
 int i;
 double * array =  new double [size];
 omp_set_num_threads(_nb_threads);
 #pragma omp parallel shared(array) private(i)
 {
 #pragma omp for schedule(dynamic) nowait
 for (i = 0; i < size; i++) // how can I use MPI to speed up this loop to compute all elements in the array?
 {
 array[i] = complicated_computation(); // time comsuming computation
 }
 }
 // some operations using all elements in array
 }

我想知道是否更改为使用MPI,是否可以同时为OpenMP和MPI编写代码?如果可能,如何编写代码以及如何编译和运行代码?

I wonder if I change to use MPI, is it possible to have the code written both for OpenMP and MPI? If it is possible, how to write the code and how to compile and run the code?

(3)我们的集群具有三个版本的MPI:mvapich-1.0.1,mvapich2-1.0.3,openmpi-1.2.6.它们的用法是否相同?特别是在我的情况下.哪一个最适合我使用?

(3) Our cluster has three versions of MPI: mvapich-1.0.1, mvapich2-1.0.3, openmpi-1.2.6.Are their usage same? Especially in my case.Which one is best for me to use?

感谢和问候!

更新:

我想解释一下有关如何指定并行部分的开始和结束的问题.在下面的玩具代码中,我想限制函数f()中的并行部分:

I like to explain a bit more about my question about how to specify the start and end of the parallel part. In the following toy code, I want to limit the parallel part within function f():

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

void f();

int main(int argc, char **argv)
{
printf("%s\n", "Start running!");
f();
printf("%s\n", "End running!");
return 0;
}


void f()
{
char idstr[32]; char buff[128];
int numprocs; int myid; int i;
MPI_Status stat;

printf("Entering function f().\n");

MPI_Init(NULL, NULL);
MPI_Comm_size(MPI_COMM_WORLD,&numprocs);
MPI_Comm_rank(MPI_COMM_WORLD,&myid);

if(myid == 0)
{
  printf("WE have %d processors\n", numprocs);
  for(i=1;i<numprocs;i++)
  {
    sprintf(buff, "Hello %d", i);
    MPI_Send(buff, 128, MPI_CHAR, i, 0, MPI_COMM_WORLD); }
    for(i=1;i<numprocs;i++)
    {
      MPI_Recv(buff, 128, MPI_CHAR, i, 0, MPI_COMM_WORLD, &stat);
      printf("%s\n", buff);
    }
}
else
{
  MPI_Recv(buff, 128, MPI_CHAR, 0, 0, MPI_COMM_WORLD, &stat);
  sprintf(idstr, " Processor %d ", myid);
  strcat(buff, idstr);
  strcat(buff, "reporting for duty\n");
  MPI_Send(buff, 128, MPI_CHAR, 0, 0, MPI_COMM_WORLD);
}
MPI_Finalize();

printf("Leaving function f().\n");
}

但是,不期望正在运行的输出.并行部分之前和之后的printf部分已经由每个进程执行,而不仅仅是主进程:

However, the running output is not expected. The printf parts before and after the parallel part have been executed by every process, not just the main process:

$ mpirun -np 3 ex2
Start running!
Entering function f().
Start running!
Entering function f().
Start running!
Entering function f().
WE have 3 processors
Hello 1 Processor 1 reporting for duty

Hello 2 Processor 2 reporting for duty

Leaving function f().
End running!
Leaving function f().
End running!
Leaving function f().
End running!

所以在我看来,并行部分并不局限于MPI_Init()和MPI_Finalize()之间.

So it seems to me the parallel part is not limited between MPI_Init() and MPI_Finalize().

除此以外,我仍然希望有人能够回答我的其他问题.谢谢!

Besides this one, I am still hoping someone could answer my other questions. Thanks!

推荐答案

快速编辑(因为我要么不知道如何发表评论,要么我不允许发表评论)-3lectrologos关于MPI程序的并行部分.您不能在MPI_Init之前和MPI_Finalize之后执行串行工作,并期望它实际上是串行的-它仍将由所有MPI线程执行.

Quick edit (because I either can't figure out how to leave comments, or I'm not allowed to leave comments yet) -- 3lectrologos is incorrect about the parallel part of MPI programs. You cannot do serial work before MPI_Init and after MPI_Finalize and expect it to actually be serial -- it will still be executed by all MPI threads.

我认为问题的一部分是MPI程序的并行部分"是整个程序. MPI将在您几乎同时指定的每个节点上开始执行相同的程序(您的主要功能). MPI_Init调用只是为程序设置了某些功能,因此它可以正确使用MPI调用.

I think part of the issue is that the "parallel part" of an MPI program is the entire program. MPI will start executing the same program (your main function) on each node you specify at approximately the same time. The MPI_Init call just sets certain things up for the program so it can use the MPI calls correctly.

对于我认为您要执行的操作,正确的模板"(采用伪代码)将是:

The correct "template" (in pseudo-code) for what I think you want to do would be:

int main(int argc, char *argv[]) {
    MPI_Init(&argc, &argv);
    MPI_Comm_size(MPI_COMM_WORLD,&numprocs);
    MPI_Comm_rank(MPI_COMM_WORLD,&myid);

    if (myid == 0) { // Do the serial part on a single MPI thread
        printf("Performing serial computation on cpu %d\n", myid);
        PreParallelWork();
    }

    ParallelWork();  // Every MPI thread will run the parallel work

    if (myid == 0) { // Do the final serial part on a single MPI thread
        printf("Performing the final serial computation on cpu %d\n", myid);
        PostParallelWork();
    }

    MPI_Finalize();
    return 0;
}

这篇关于如何通过MPI加速此问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 00:23