并行程序设计:小白日记(一)Linux安装配置mpich

坑爹的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;//总任务数和优先级标志


        int rt = MPI_Init(&argc, &argv);
        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个进程执行这个程序。




  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值