【发布时间】:2015-01-08 11:14:12
【问题描述】:
我是并行计算的新手,所以我决定从使用 Mpich2 编译的 hello world 开始。这是代码:
/* helloworld.c */
#include <stdio.h>
/* You MUST include this for the MPI_* functions */
#include "mpi.h"
int main(int argc, char **argv) {
int rank;
char host[150];
int namelen;
/* Initialize MPI. This handles mpich-specific command line arguments */
MPI_Init(&argc, &argv);
/* Get my rank. My rank number gets stored in the 'rank' variable */
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
/* Look up what computer I am running on. Store it in 'host' */
MPI_Get_processor_name(host,&namelen);
printf("Hello world (Rank: %d / Host: %s)\n", rank, host);
fflush(stdout);
/* Finalize: Close connections to the other children, clean up memory
* the MPI library has allocated, etc */
MPI_Finalize();
return 0;
}
我是这样编译和运行的:
mpicc helloworld.c -o myhello
mpirun -nc 2 ./myhello
它有效。但是我注意到通过增加 CPU 的数量,挂钟时间增加了,我希望它会减少?!此外,CPU 的数量没有限制,但是我的笔记本电脑有 5 个核心,但我可以在代码中设置任意数量的 CPU,如果超过实际 CPU 的数量,我预计会出现错误。
【问题讨论】:
-
这不是“CPU 数量”,而是“进程数量”,系统中运行的进程可能比 CPU 数量多得多(即使现在你可能已经接近 100在您的系统上运行的进程)。你能分享你的hello world程序代码吗?它可能有助于回答这个问题,但我的猜测是 MPI 必须创建进程并同步它们,这会增加运行时间。
-
如果您真的是新手并且认真对待它,我建议您阅读一本书。在您的情况下,简单地说,n 个进程做同样的事情将花费与 1 个进程做同样的事情一样多的时间。通过在每个流程上打印一些内容,您不会分配任何工作,并且您实际上是在争夺资源,例如输入/输出。
-
启动更多进程并在最后同步它们需要更多时间。除非你以某种方式在他们之间分配工作 - 你没有 - 总时间只会增加。就是这样。
标签: c++ parallel-processing fortran mpi mpich