c++ - Windows 中的 Mpi 用法

标签 c++ parallel-processing mpi

我将 mpi 安装到 Windows 中,我可以使用它的库。问题是当我在 Windows 中写

mpiexec -n 4 proj.exe 

进入命令提示符后无法进行正确的操作。 4个不同的进程分别使用整个代码文件。它们的行为不像仅在 MPI_Init 和 MPI_Finalize 行中工作的并行进程。我该如何解决这个问题?在 Windows 中无法使用 MPI。

P.s : 我正在使用 Dev c++

最佳答案

根据您所说的,MPI 正在正确运行——相反,您的假设是不正确的。在每个 MPI 实现(无论如何我都使用过)中,整个程序在每个 进程上从头到尾运行。 MPI_Init 和 MPI_Finalize 函数需要为每个进程设置和拆除 MPI 结构,但它们不指定并行执行的开始和结束。并行部分的开始是main中的第一条指令,结束是最后的返回。

一个好的"template"程序看起来像你想要的(也在 How to speed up this problem by MPI 中回答):

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;  
}  

关于c++ - Windows 中的 Mpi 用法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2015673/

相关文章:

两个处理器之间的通信(并行编程)

c++ - Opengl superbible example compliation errors 源代码 : drawing a triangle

c++ - glsl 蒙皮障碍,谁能跳过那个?

c++ - 原生类型的封装会影响效率吗?

c - 当我尝试运行 MPI 代码时,我得到了对 MPI_File_Seek@12 的 undefined reference ,这可能是什么原因?

types - 向 MPI 结构添加填充

c++ - 使用 cublas saxpy 时出错

c++ - OpenMP - 并行代码有意想不到的结果

haskell - 列表并行评估

c++ - std::atomic<int>:x.fetch_add(1) 和 x++ 的区别;