如何使用MPI或OpenMP并行化函数调用

时间:2021-05-27 14:29:47

1st question:

I wonder how I can parallelize function calls to the same function, but with different input parameters in a for loop. For example (C code):

我想知道如何将函数调用并行化到同一个函数,但在for循环中使用不同的输入参数。例如(C代码):

//a[i] and b[i] are defined as elements of a list with 2 columns and N rows
//i is the row number

#pragma omp parallel
{
char cmd[1000];
  #pragma omp for nowait
  for(i=0; i<N; i++) {
    //call the serial programm
    sprintf(cmd, "./serial_program %f %f", a[i], b[i]);
    system(cmd);
  }
}

If I just apply a pragma omp for (+the omp header of course) nothing happens. Maybe this is not possible with OpenMP, but would it be possible with MPI and how would it look like then? I have experience only with OpenMP so far, but not with MPI. update: defined cmd within parallel region

如果我只是应用pragma omp(当然+ + omp标题)没有任何反应。也许这对于OpenMP来说是不可能的,但是它可以用MPI实现,那么它会是什么样子呢?到目前为止,我只对OpenMP有经验,但对MPI没有经验。更新:在并行区域内定义cmd

Status: solved

2nd question:

If i have a OpenMP parallelized program and i want to use it among different nodes within a cluster, how can i distribute the calls among the different nodes with MPI and how would i compile it?

如果我有一个OpenMP并行化程序,并且我想在群集中的不同节点之间使用它,我如何使用MPI在不同节点之间分配调用以及如何编译它?

//a[i] and b[i] are defined as elements of a list with 2 columns and N rows
//i is the row number

  for(i=0; i<N; i++) {
    //call the parallelized program
    sprintf(cmd, "./openmp_parallelized_program %f %f", a[i], b[i]);
    system(cmd);
  }

Status: unsolved

2 个解决方案

#1


0  

MPI is a method to communicate between nodes of a computing cluster. It enables one motherboard to talk to another. MPI is for clusters and large computing tasks, it is not for parallelizing desktop applications.

MPI是一种在计算集群的节点之间进行通信的方法。它使一个主板可以与另一个主板通信。 MPI适用于群集和大型计算任务,不适用于并行化桌面应用程序。

Communications in MPI are done by explicitly sending and receiving data.

MPI中的通信通过显式发送和接收数据来完成。

Unlike OpenMP, there is no #pragma that will automatically facilitate parallelization.

与OpenMP不同,没有#pragma会自动促进并行化。

Also there is something really messed up about the code that you posted, specifically, it is a C program that acts like a bash script.

还有一些关于你发布的代码真的搞砸了,具体来说,它是一个像bash脚本一样的C程序。

#!/bin/bash
N=10
for i in `seq 1 $N`;
do
./program $i &
done

On many clusters calls to system will execute only on the host node, resulting in no speedup and io problems. The command you showed is wholly unworkable.

在许多集群上,对系统的调用只会在主机节点上执行,导致没有加速和io问题。你展示的命令完全不可行。

#2


0  

With MPI you would do something like:

使用MPI,您可以执行以下操作:

int rank, size;

MPI_Init();
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);

int start = (rank*N)/size;
int end = ((rank+1)*N)/size;

for (i = start; i < end; i++)
{
   sprintf(cmd, "./openmp_parallelized_program %f %f", a[i], b[i]);
   system(cmd);
}

MPI_Finalize();

Then run the MPI job with one process per node. There is a caveat though. Some MPI implementations do not allow processes to call fork() under certain conditions (and system() calls fork()), e.g. if they communicate over RDMA-based networks like InfiniBand. Instead, you could merge both programs in order to create one hybrid MPI/OpenMP program.

然后运行每个节点一个进程的MPI作业。但有一点需要注意。某些MPI实现不允许进程在某些条件下调用fork()(而system()调用fork()),例如如果他们通过基于RDMA的网络进行通信,如InfiniBand。相反,您可以合并两个程序以创建一个混合MPI / OpenMP程序。

#1


0  

MPI is a method to communicate between nodes of a computing cluster. It enables one motherboard to talk to another. MPI is for clusters and large computing tasks, it is not for parallelizing desktop applications.

MPI是一种在计算集群的节点之间进行通信的方法。它使一个主板可以与另一个主板通信。 MPI适用于群集和大型计算任务,不适用于并行化桌面应用程序。

Communications in MPI are done by explicitly sending and receiving data.

MPI中的通信通过显式发送和接收数据来完成。

Unlike OpenMP, there is no #pragma that will automatically facilitate parallelization.

与OpenMP不同,没有#pragma会自动促进并行化。

Also there is something really messed up about the code that you posted, specifically, it is a C program that acts like a bash script.

还有一些关于你发布的代码真的搞砸了,具体来说,它是一个像bash脚本一样的C程序。

#!/bin/bash
N=10
for i in `seq 1 $N`;
do
./program $i &
done

On many clusters calls to system will execute only on the host node, resulting in no speedup and io problems. The command you showed is wholly unworkable.

在许多集群上,对系统的调用只会在主机节点上执行,导致没有加速和io问题。你展示的命令完全不可行。

#2


0  

With MPI you would do something like:

使用MPI,您可以执行以下操作:

int rank, size;

MPI_Init();
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);

int start = (rank*N)/size;
int end = ((rank+1)*N)/size;

for (i = start; i < end; i++)
{
   sprintf(cmd, "./openmp_parallelized_program %f %f", a[i], b[i]);
   system(cmd);
}

MPI_Finalize();

Then run the MPI job with one process per node. There is a caveat though. Some MPI implementations do not allow processes to call fork() under certain conditions (and system() calls fork()), e.g. if they communicate over RDMA-based networks like InfiniBand. Instead, you could merge both programs in order to create one hybrid MPI/OpenMP program.

然后运行每个节点一个进程的MPI作业。但有一点需要注意。某些MPI实现不允许进程在某些条件下调用fork()(而system()调用fork()),例如如果他们通过基于RDMA的网络进行通信,如InfiniBand。相反,您可以合并两个程序以创建一个混合MPI / OpenMP程序。