坑爹的Windows系统,连着试了很多遍都是各种报错,身为一个小白,被他搞得很难过。
索性放弃,换成Linux试了,很快就弄好了。
一、安装
只需要两个指令:
1、sudo apt update检查一下源是否要更新
2、sudo apt install mpich直接开始在线安装mpich
二、检查
同样用两个指令来检查一下安装成功了没有:
1、mpicc 这个是编译的指令,如果你直接敲这个指令,会给出这样的字样:
xb@bob:~/桌面$ mpicc
Error: Command line argument is needed!
这样的报错没关系。他是提示你输入的参数不够,编译编译,你当然得给他一个需要编译的文件啦。
2、mpiexec这个是运行的指令,如果你直接敲这个指令,会给出这样的字样:
xb@bob:~/桌面$ mpiexec
[mpiexec@bob] set_default_values (ui/mpich/utils.c:1528): no executable provided
[mpiexec@bob] HYD_uii_mpx_get_parameters (ui/mpich/utils.c:1739): setting default values failed
[mpiexec@bob] main (ui/mpich/mpiexec.c:153): error parsing parameters
同样没关系,我们可以写一个简单的小程序,helloworld来测试一下,看看程序运行的结果,就知道了。
三、问候世界
编写程序helloworld!
mpi_helloworld.c | |
#include<stdio.h> #include"mpi.h" int main(int argc, char *argv[]){ int totalTaskNum, rankID;//总任务数和优先级标志 if(rt != MPI_SUCCESS){ printf("Error starting MPI.\n"); MPI_Abort(MPI_COMM_WORLD, rt); } MPI_Comm_size(MPI_COMM_WORLD, &totalTaskNum); MPI_Comm_rank(MPI_COMM_WORLD, &rankID); printf("Hellow, world! %dth of totalTaskNum = %d\n", rankID, totalTaskNum); MPI_Finalize(); return 0; } | |
这个程序,没什么好说的。 |
xb@bob:~/桌面$ mpicc -o a mpi_helloworld.c
xb@bob:~/桌面$ mpiexec -n 6 ./a
Hellow, world! 5th of totalTaskNum = 6
Hellow, world! 0th of totalTaskNum = 6
Hellow, world! 2th of totalTaskNum = 6
Hellow, world! 4th of totalTaskNum = 6
Hellow, world! 3th of totalTaskNum = 6
Hellow, world! 1th of totalTaskNum = 6
这就是编译运行的结果,运行时的-n参数为指定你要起的进程数量,6表示同时6个进程执行这个程序。