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

来源:互联网 发布:水利水电设计院知乎 编辑:程序博客网 时间:2024/06/05 06:51

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 ./helloworld2Hellow, world! 0th of totalTaskNum = 10elapsedTime = 0.000029, precision = 0.000001Hellow, world! 1th of totalTaskNum = 10Hellow, world! 2th of totalTaskNum = 10Hellow, world! 3th of totalTaskNum = 10Hellow, world! 6th of totalTaskNum = 10Hellow, world! 4th of totalTaskNum = 10Hellow, world! 7th of totalTaskNum = 10Hellow, world! 8th of totalTaskNum = 10Hellow, world! 9th of totalTaskNum = 10Hellow, 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 xianHellow, world! 0th of totalTaskNum = 10argc = 40th of arguments is: ./helloworld21th of arguments is: beijing2th of arguments is: shanghai3th of arguments is: xianelapsedTime = 0.000041, precision = 0.000001Hellow, world! 1th of totalTaskNum = 10Hellow, world! 6th of totalTaskNum = 10Hellow, world! 7th of totalTaskNum = 10Hellow, world! 8th of totalTaskNum = 10Hellow, world! 9th of totalTaskNum = 10Hellow, world! 2th of totalTaskNum = 10Hellow, world! 3th of totalTaskNum = 10Hellow, world! 4th of totalTaskNum = 10Hellow, world! 5th of totalTaskNum = 10



原创粉丝点击