Prog2: hellowrold2.c 由进程0打印执行时间和命令行参数

1. 源码

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

        double wallTime1 = MPI_Wtime(); //starting time
        double precision = MPI_Wtick(); //WallTime's precision

        MPI_Comm_size(MPI_COMM_WORLD, &totalTaskNum);
        MPI_Comm_rank(MPI_COMM_WORLD, &rankID);

        printf("Hellow, world! %dth of totalTaskNum = %d\n", rankID, totalTaskNum);

        double wallTime2 = MPI_Wtime();

        if(rankID == 0){   //if(!rankID)
                printf("elapsedTime = %f, precision = %f\n", wallTime2 - wallTime1, precision);
        }

        MPI_Finalize();

        return 0;
}

2. 编译执行

[amao@amao991 mpi-study]$ mpicc -o helloworld2 helloworld2.c
[amao@amao991 mpi-study]$ mpiexec -n 10 ./helloworld2
Hellow, world! 0th of totalTaskNum = 10
elapsedTime = 0.000029, precision = 0.000001
Hellow, world! 1th of totalTaskNum = 10
Hellow, world! 2th of totalTaskNum = 10
Hellow, world! 3th of totalTaskNum = 10
Hellow, world! 6th of totalTaskNum = 10
Hellow, world! 4th of totalTaskNum = 10
Hellow, world! 7th of totalTaskNum = 10
Hellow, world! 8th of totalTaskNum = 10
Hellow, world! 9th of totalTaskNum = 10
Hellow, world! 5th of totalTaskNum = 10

3. 总结

(1)学习了两个函数

 double wallTime1 = MPI_Wtime();  //进程组的墙上时间, 在并行执行开头/结尾分别获取墙上时间,以second为单位
 double precision = MPI_Wtick(); //WallTime's precision,double双精度,以second为单位

(2) 由0号进程来打印结果

if(rankID == 0){   //if(!rankID)

4. 补充: 修改一下,由0号进程获取命令行参数并打印出来

(1)源码

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

        double wallTime1 = MPI_Wtime(); //starting time
        double precision = MPI_Wtick(); //WallTime's precision

        MPI_Comm_size(MPI_COMM_WORLD, &totalTaskNum);
        MPI_Comm_rank(MPI_COMM_WORLD, &rankID);

        printf("Hellow, world! %dth of totalTaskNum = %d\n", rankID, totalTaskNum);

        double wallTime2 = MPI_Wtime();

        if(rankID == 0){   //if(!rankID)
                printf("argc = %d\n", argc);
                int i;
                for(i = 0; i < argc; i++){
                        printf("%dth of arguments is: %s\n", i, argv[i]);
                }

                printf("elapsedTime = %f, precision = %f\n", wallTime2 - wallTime1, precision);
        }

        MPI_Finalize();

        return 0;
}

(2)编译执行

[amao@amao991 mpi-study]$ mpicc -o helloworld2 helloworld2.c 
[amao@amao991 mpi-study]$ mpiexec -n 10 ./helloworld2 beijing shanghai xian
Hellow, world! 0th of totalTaskNum = 10
argc = 4
0th of arguments is: ./helloworld2
1th of arguments is: beijing
2th of arguments is: shanghai
3th of arguments is: xian
elapsedTime = 0.000041, precision = 0.000001
Hellow, world! 1th of totalTaskNum = 10
Hellow, world! 6th of totalTaskNum = 10
Hellow, world! 7th of totalTaskNum = 10
Hellow, world! 8th of totalTaskNum = 10
Hellow, world! 9th of totalTaskNum = 10
Hellow, world! 2th of totalTaskNum = 10
Hellow, world! 3th of totalTaskNum = 10
Hellow, world! 4th of totalTaskNum = 10
Hellow, world! 5th of totalTaskNum = 10



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值