groupio.c
来自「VDmpi VdmpiVDmpi VdmpiVDmpi VdmpiVDmpi V」· C语言 代码 · 共 176 行
C
176 行
/******************************************************************************* FILE: groupIO.c* OTHER FILES: make.groupIO.c groupIO_makefn.c* DESCRIPTION: This SPMD code demonstrates the use of task groups and * concurrent I/O with multiple worker tasks. The master first creates a* number of input files which will be read by the worker tasks. Worker * tasks are assigned into one of two groups. A one-to-one correlation exists* between worker task instance/group numbers and file identifiers. For * example, the task in :* instance 0 group 1 reads file.000 and writes file.000.1 * instance 0 group 2 reads file.000 and writes file.000.2 * instance 2 group 1 reads file.002 and writes file.002.1 * instance 2 group 2 reads file.002 and writes file.002.2 and so on. * The master task will wait to hear back from all worker tasks before * quitting.* AUTHOR: Blaise Barney* LAST REVISED: 11/17/93 Roslyn Leibensperger******************************************************************************/#include <stdio.h>#include "mpi.h"#define MASTER 0 /* task id of master */#define MAXTASKS 16 /* maximum number of tasks */#define ROWS 20 /* x dimension of data array */#define COLS 10 /* y dimension of data array */#define START 1 /* define a message type */#define DONE 2 /* define a message type */MPI_Comm comm, comm1, comm2;MPI_Group group0, group1, group2;MPI_Status status;extern void makefilename(char *, int, int); /* creates unique filenames */main(int argc, char **argv) {void CreateInputFiles(int, int, int); /* creates the input files */FILE *infile, /* for input file */ *outfile; /* for output file */int numtasks, /* number of tasks in partition */ taskid, /* task identifier */ numworkers, /* number of worker tasks */ gid, /* group identifier */ gsize, /* number of tasks in group */ glist[MAXTASKS], /* list of task ids in group */ mtype, /* messsage type */ sync_sig, /* set a message type */ dest, /* task id of message destination */ source, /* task id of message source */ nbytes, /* number of bytes in message */ rank, /* a task's place/order in the group */ filedata, /* file data element */ i,j,k, color, /* counters, indexes */ rc; /* for return codes */char grp, /* group id */ ifile[80], /* input file name */ ofile[80]; /* output file name */MPI_Init(&argc, &argv);MPI_Comm_rank(MPI_COMM_WORLD, &taskid);MPI_Comm_size(MPI_COMM_WORLD, &numtasks);numworkers = numtasks-1;/**************************** master task ************************************/if(taskid == 0) color = 0;else if(taskid <= numtasks/2) color = 1;else if(taskid > numtasks/2) color = 2;MPI_Comm_split(MPI_COMM_WORLD, color, taskid, &comm);if(color == 0) { MPI_Comm_group(comm, &group0); MPI_Group_size(group0, &gsize); MPI_Group_rank(group0, &rank); printf("group= 0, taskid = %d, rank = %d, size = %d\n", taskid, rank,gsize); CreateInputFiles(numtasks/2, ROWS, COLS); /* Master needs to sync all worker processes so that they don't try to */ /* start reading the input files before the master is done creating them. */ sync_sig = START; mtype = START; for (dest=1; dest<= numworkers; dest++) MPI_Send(&sync_sig, 1, MPI_INT, dest, mtype, MPI_COMM_WORLD); /* Now master waits for all workers to complete */ mtype = DONE; for (i=1; i<=numworkers; i++) { source = i; MPI_Recv(&sync_sig, 1, MPI_INT, source, mtype, MPI_COMM_WORLD, &status); } printf("\n***** All worker tasks reported back. Done.*****\n"); }/**************************** worker task ************************************/if (taskid != MASTER) { /* Figure out some of my group info and display it */ if(color == 1) { grp = '1'; MPI_Comm_group(comm, &group1); MPI_Group_size(group1, &gsize); MPI_Group_rank(group1, &rank); printf("group= 1, taskid = %d, rank = %d, size = %d\n", taskid, rank,gsize); } else if(color == 2) { grp = '2'; MPI_Comm_group(comm, &group2); MPI_Group_size(group2, &gsize); MPI_Group_rank(group2, &rank); printf("group= 2, taskid = %d, rank = %d, size = %d\n", taskid, rank,gsize); } /* Syncronize with master and then start doing I/O */ mtype = START; source = MASTER; MPI_Recv(&sync_sig, 1, MPI_INT, source, mtype, MPI_COMM_WORLD, &status); makefilename(ifile, rank, '0'); infile = fopen(ifile,"r"); printf("Instance %d group %c reading=%s\n",rank,grp,ifile); makefilename(ofile, rank, grp); outfile = fopen(ofile,"w"); printf("Instance %d group %c writing=%s\n",rank,grp,ofile); /* Create output file depending upon which group I am in */ for (j=0; j<ROWS; j++) { for (k=0; k<COLS; k++){ rc = fscanf(infile,"%6d",&filedata); if (grp == '1') filedata = filedata+rank; if (grp == '2') filedata = filedata*(-1); fprintf(outfile,"%6d", filedata); } fprintf(outfile,"\n"); } fclose(infile); fflush(outfile); fclose(outfile); /* Tell master that I am done */ sync_sig = DONE; mtype = DONE; MPI_Send(&sync_sig, 1, MPI_INT, MASTER, mtype, MPI_COMM_WORLD); } MPI_Comm_free(&comm); MPI_Group_free(&group0); MPI_Group_free(&group1); MPI_Group_free(&group2); MPI_Finalize();}/*****************************************************************************/void CreateInputFiles(int nfiles, int rows, int cols) {/*****************************************************************************/FILE *outfile;int i,j,k;char fname[80];for (i=0; i<nfiles; i++) { makefilename(fname, i, '0'); outfile = fopen(fname,"w"); for (j=0; j<rows; j++) { for (k=0; k<cols; k++) fprintf(outfile,"%6d",j*k+i); fprintf(outfile,"\n"); } fflush(outfile); fclose(outfile); printf("Created input file = %s\n",fname); }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?