您的位置:首页 > 其它

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

2011-12-21 11:30 309 查看
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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: