📄 mlp.ccode.html
字号:
<html><head><title>C Version of MLP Program</title><link rel="stylesheet" href="/MULTI/parcomp.css"></head><body><h2>C Version of MLP Parallel Search Program</h2><p>The comments in the Fortran version apply to the appropriateareas of the following C code as well. The system() function <b>is</b> part of the standard C language and can be found with any C compiler.<p><FONT FACE="COURIER NEW"><pre>#include <stdio.h>#include <stdlib.h>#include <mpi.h>#include <omp.h>#include <math.h>main(int argc, char* argv[]) { int rank,size,thread; int mark,i,j; const int N=3003; int sub_b[N/3],b[N]; MPI_Status status; char* command; FILE *s1,*s2,*s3; MPI_Init(&argc,&argv); MPI_Comm_rank(MPI_COMM_WORLD, &rank); MPI_Comm_size(MPI_COMM_WORLD, &size); omp_set_num_threads(4); mark=10; if (rank==0 ) { printf("R:%d ps with MPI only \n",rank); /* command="ps -u $USER -o pid,ppid,stime,etime,cpu,comm"; */ command="ps -m -o pid,ppid,tid,thcount,stime,etime,cpu,comm"; system(command); srand(7654592); /* drand48() will return random double between 0.0 and 1.0 */ for(i=0;i<N;++i) { b[i]=(int)(100*drand48()-50); } MPI_Send(b,N/3,MPI_INT,1,51,MPI_COMM_WORLD); MPI_Send(&b[N/3],N/3,MPI_INT,2,52,MPI_COMM_WORLD); MPI_Send(&b[2*N/3],N/3,MPI_INT,3,53,MPI_COMM_WORLD); }else if (rank == 1) { MPI_Recv(sub_b,N/3,MPI_INT,0,51,MPI_COMM_WORLD,&status); s1=fopen("slave1.txt","w");#pragma omp parallel private(i,thread,command,j) \ firstprivate(rank,mark,sub_b,s1) { thread=omp_get_thread_num(); #pragma omp for schedule(static) for(i=0;i<N/3;++i) { if (abs(sub_b[i]) == mark) { j=(rank-1)*N/3 +i; fprintf(s1,"R:%d T:%d j=%d\n",rank,thread,j); } } } fclose(s1); }else if (rank == 2) { MPI_Recv(sub_b,N/3,MPI_INT,0,52,MPI_COMM_WORLD,&status); s2=fopen("slave2.txt","w");#pragma omp parallel private(i,thread,command,j) \ firstprivate(rank,mark,sub_b,s2) { thread=omp_get_thread_num(); #pragma omp for schedule(static) for(i=0;i<N/3;++i) { if (abs(sub_b[i]) == mark) { j=(rank-1)*N/3 +i; fprintf(s2,"R:%d T:%d j=%d\n",rank,thread,j); } } } fclose(s2); }else if (rank == 3) { MPI_Recv(sub_b,N/3,MPI_INT,0,53,MPI_COMM_WORLD,&status); s3=fopen("slave3.txt","w");#pragma omp parallel private(i,thread,command,j) \ firstprivate(rank,mark,sub_b,s3) { thread=omp_get_thread_num(); if(thread == 0) { printf("R:%d T:%d ps with MPI and OpenMP\n",rank,thread); /* command="ps -u $USER -o pid,ppid,stime,etime,cpu,comm"; */ command="ps -m -o pid,ppid,tid,thcount,stime,etime,cpu,comm"; system(command); } #pragma omp for schedule(static) for(i=0;i<N/3;++i) { if (abs(sub_b[i]) == mark) { j=(rank-1)*N/3 +i; fprintf(s3,"R:%d T:%d j=%d\n",rank,thread,j); } } } fclose(s3); } MPI_Finalize();}</pre></FONT></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -