output.cpp

来自「VDmpi VdmpiVDmpi VdmpiVDmpi VdmpiVDmpi V」· C++ 代码 · 共 98 行

CPP
98
字号
#include "mpi.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define	BUFSIZE	16	
void main(int argc,char *argv[]) {
	int numtasks, rank, dest, source, rc,tag=1;  

	//////for file
	FILE	*infile,			/* for input file */
			*outfile;			/* for output file */
	
	char * ifile;			/* input file name */
	char * ofile;			/* output file name */
	long isize,osize;
	char  idata[10];
	char  odata[10];
///////////////////////
	MPI_Status Stat;
	MPI_Init(&argc,&argv);
	MPI_Comm_size(MPI_COMM_WORLD, &numtasks);
	MPI_Comm_rank(MPI_COMM_WORLD, &rank);
	if (rank == 0) 
	{ 
		char proname1[MPI_MAX_PROCESSOR_NAME];
		char *proname2;

		int len;
		MPI_Get_processor_name(proname1,&len);
		proname2="NGOCDTB";
		int i=0;
		while ((i<len) && (proname1[i]==proname2[i])) i++;
		if (i>=len) printf("dung");
		printf(proname1);
		dest = 1; source = 1;

		ofile = "C:\\Output.dat";

		outfile = fopen(ofile,"r");
		if (outfile==NULL) { printf("Hong roi");exit (1);}
		// lay kich thuoc file
		fseek (outfile , 0 , SEEK_END);
		osize = ftell(outfile);
		rewind (outfile);
		
		rc = MPI_Send(&osize,sizeof(long),MPI_LONG,dest,tag,MPI_COMM_WORLD);
		//gui file
		while( osize>=BUFSIZE){
			fread (odata,1,BUFSIZE,outfile);
		//	fseek (outfile,BUFSIZE,SEEK_CUR);

			rc = MPI_Send(&odata,BUFSIZE,MPI_CHAR,dest,tag,MPI_COMM_WORLD);
			osize = osize - BUFSIZE;
			
		
		}
		//gui phan du con lai cua file
		if (osize >0) {
			fread (odata,1,osize,outfile);
			rc = MPI_Send(&odata,osize,MPI_CHAR,dest,tag,MPI_COMM_WORLD);
		}


		printf("ra ok!");// printf(odata);
		
		fclose(outfile);
	} 
	else if (rank == 1) 
	{ dest = 0; source = 0;
	
		ifile = "C:\\Input.dat";
		infile = fopen(ifile,"w"); 
		rc=MPI_Recv(&isize,sizeof(long),MPI_LONG,source,tag, MPI_COMM_WORLD, &Stat);
		while (isize>=BUFSIZE){
			rc=MPI_Recv(&idata,BUFSIZE,MPI_CHAR,source,tag, MPI_COMM_WORLD, &Stat);
			fwrite (idata , 1 , BUFSIZE , infile);
		//	fseek (infile,BUFSIZE,SEEK_CUR);

			isize = isize-BUFSIZE;
	
		}

		if (isize>0) {
			rc = MPI_Recv(&idata,isize,MPI_CHAR,source,tag,MPI_COMM_WORLD,&Stat);
			fwrite(idata,1,isize,infile);
			
		}
		printf("vao ok!");
		
		fclose (infile);

	}


	MPI_Finalize();
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?