正在加载图片...
#include <math. h> #define size 10 oid main (int argc, char *argv) nt myid, numprocs int data[SIze], i, x, low, high, myresult, result char fn[255 char x*fp MPI Init(&argc, &argv) PI Comm size(MPI COMM WORLD, &numprocs MPI Comm rank(MPI COMM WORLD, &myid) if (myid= 0)[/* Open input file and initialize data * strcpy(fn, getenv("HOMe")) strcat(fn, " /data") f ((fp fopen(fn, " r"))== NULL)( printf("Can't open the input file: %s \n\n", fn) exit(1) for(i=0: i SIZE; i++) fscanf(fp, %d", &data[il) MPI Bcast(data, SIZE, MPI INT, 0, MPI COMM WORLD); id*k 1)high= SIZE ult =0: f ult + datai] /=* Compute global I Reduce( &myresult, &result, 1, MPI INT, MPI SUM, 0, MPI COMM WORLD) f(myid 0) printf(" The sum is %d \n", result) MPI Finalize o 请回答以下问题: (1)请问上述并行程序的并行执行的进程数是何时指定的,如何确定的?在程序中 使用什么函数得到了进程数的信息。- 2 - #include <math.h> #define SIZE 10 void main(int argc, char *argv) { int myid, numprocs; int data[SIZE], i, x, low, high, myresult, result; char fn[255]; char *fp; MPI_Init(&argc,&argv); MPI_Comm_size(MPI_COMM_WORLD,&numprocs); MPI_Comm_rank(MPI_COMM_WORLD,&myid); if (myid == 0) { /* Open input file and initialize data */ strcpy(fn,getenv("HOME")); strcat(fn,"/data"); if ((fp = fopen(fn,"r")) == NULL) { printf("Can’t open the input file: %s\n\n", fn); exit(1); } for(i = 0; i < SIZE; i++) fscanf(fp,"%d", &data[i]); } /* broadcast data */ MPI_Bcast(data, SIZE, MPI_INT, 0, MPI_COMM_WORLD); /* Add my portion Of data */ x = SIZE/numprocs; low = myid * x; high = low + x; if(myid == numprocs - 1) high = SIZE; myresult = 0; for(i = low; i < high; i++) myresult += data[i]; /* Compute global sum */ MPI_Reduce(&myresult, &result, 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD); if (myid == 0) printf("The sum is %d.\n", result); MPI_Finalize(); } 请回答以下问题: (1) 请问上述并行程序的并行执行的进程数是何时指定的,如何确定的?在程序中, 使用什么函数得到了进程数的信息
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有