一、mpi
来自教材《并行程序设计导论》
mpi的helloworld程序
//test3_1.c #include <stdio.h> #include <string.h> #include <mpi.h> const int MAX_STRING = 100; int main(void){ char greeting[MAX_STRING]; int comm_sz; int my_rank; int q; MPI_Init(NULL,NULL); MPI_Comm_size(MPI_COMM_WORLD,&comm_sz); MPI_Comm_rank(MPI_COMM_WORLD,&my_rank); if(my_rank!=0) { sprintf(greeting,"greetings from process %d of %d!",my_rank,comm_sz); //sprintf是给greeting赋值 MPI_Send(greeting,strlen(greeting)+1,MPI_CHAR,0,0,MPI_COMM_WORLD); } else{ printf("Greeting from process %d of %d! ",my_rank,comm_sz); for(q=1;q<comm_sz;q++) { MPI_Recv(greeting,MAX_STRING,MPI_CHAR,q,0,MPI_COMM_WORLD,MPI_STATUS_IGNORE); printf("%s ",greeting); } } MPI_Finalize(); return 0; }
可以通过mpicc -g -Wall -o test3_1 test3_1.c或者mpiexec -n 1 ./test3_1运行,获得可执行文件
然后
pbs脚本
#!/bin/bash #PBS -N test3_1 #PBS -l nodes=1:ppn=8 #PBS -q AA000_queue #PBS -j oe cd $PBS_O_WORKDIR procs=$(cat $PBS_NODEFILE | wc -l) mpirun -np $procs -machinefile $PBS_NODEFILE ./test3_1 >& test.log
提交:qsub test3_1.pbs
查看:qstat
pbs脚本中
$PBS_NODEFILE,##这个环境变量表示由pbs 自动分配给作业的节点列表##
procs=$(cat $PBS_NODEFILE | wc -l) ##计算申请的 cpu 核数目,并后赋值给变量 NP)##
cd $PBS_O_WORKDIR ## (进入工作目录) ##
time mpirun -np &procs -machinefile $PBS_NODEFILE ./mpi 0.01 >& run1.log ##应该是传递两个参数,第一个为0.01,第二个为&procs,表示线程数##
查看test.log文件,可得到输出结果
Greeting from process 0 of 8! greetings from process 1 of 8! greetings from process 2 of 8! greetings from process 3 of 8! greetings from process 4 of 8! greetings from process 5 of 8! greetings from process 6 of 8! greetings from process 7 of 8!
ppt中程序
错误代码
#include "mpi.h" #include <stdio.h> int foo(int i) { return i; } int main(void) { int i,tmp,group_size,my_rank,N=10,sum=0; MPI_Status status; MPI_Init(NULL,NULL); MPI_Comm_size(MPI_COMM_WORLD,&group_size); MPI_Comm_rank(MPI_COMM_WORLD,&my_rank); if(my_rank==0) { printf("group_size:%d ",group_size); for(i=1;i<group_size;i++) { printf("N=%d,i=%d ",N,i); MPI_Send(&N,1,MPI_INT,i,i,MPI_COMM_WORLD); printf("after send "); } printf("after send2 ,my_rank=%d ",my_rank); for(i=my_rank;i<N;i=i+group_size) { printf("my_rank=0,j=%d",i); sum=sum+foo(i); } printf("sum=sum+foo(i); %d",sum); for(i=1;i<group_size;i++){ printf("for(i=1;i<group_size;i++)"); MPI_Recv(&tmp,1,MPI_INT,i,i,MPI_COMM_WORLD,&status); sum=sum+tmp; printf("sum=sum+tmp; %d",sum); } printf(" the result = %d",sum); } else { printf("MPI_Recv"); MPI_Recv(&N,1,MPI_INT,0,i,MPI_COMM_WORLD,&status); //printf("else :i=%d,my_rank=%d",i,my_rank); printf("%d ",my_rank); // for(i=my_rank;i<N;i=i+group_size) { // sum=sum+foo(i); // printf("i in else is %d;sum in else %d ",i,sum); // } printf("before send in else"); MPI_Send(&sum,1,MPI_INT,0,i,MPI_COMM_WORLD); printf("after send in else"); } MPI_Finalize(); }