⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mpi_rle6.c

📁 基于MPI 的网络并行计算环境的RLE编码的并行程序
💻 C
字号:
#include "mpi.h"#include <stdio.h>#include <math.h>//#include <conio.h>#include <stdlib.h>int main( int argc, char *argv[]) {    double startwtime, endwtime, stime, rtime;        FILE * fp, * fp1;	  int i,k=1,n=0;    int Row=1, Line=108576;        int myid,numprocs,namelen;         //float sbuf[12*12], rbuf[36];	  	  int root=0;	  	  int n0, n1, n2, n3;        char processor_name[MPI_MAX_PROCESSOR_NAME];        MPI_Status status;        	           MPI_Init(&argc,&argv);    MPI_Comm_size(MPI_COMM_WORLD,&numprocs);    MPI_Comm_rank(MPI_COMM_WORLD,&myid);    MPI_Get_processor_name(processor_name,&namelen);    printf("Process %d on %s\n", myid, processor_name);        int Points=Row*Line, MPI_Points=(Row*Line)/numprocs;        float * sbuf=(float*)malloc(sizeof(float)*Points);	  	  float * rbuf=(float*)malloc(sizeof(float)*MPI_Points);	  	          if(myid==0)       {       	  startwtime=MPI_Wtime();       	         	  if((fp1=fopen("data1.dat","w"))==NULL)          {		         printf("cannot open this file\n");		         exit(0);	        }       	          if((fp=fopen("wavelet.dat","r"))==NULL)            {             printf("cannot open this file\n");             exit(0);            }           for(i=0;i<Points;i++)			           fread(&sbuf[i],sizeof(float),1,fp);    ////压缩前数据文件读入数组buffer[]          	      	             fclose(fp);	       	               }     //MPI_Bcast(sbuf,2,MPI_INT,root,MPI_COMM_WORLD);           MPI_Scatter(sbuf,MPI_Points,MPI_FLOAT,rbuf,MPI_Points,MPI_FLOAT,root,MPI_COMM_WORLD);         // MPI_Barrier(MPI_COMM_WORLD);          if(myid==0)    	{    	    	      	    for(i=0;i<MPI_Points;i++)            {             if(rbuf[i+1]!=rbuf[i])                 {			           fwrite(&rbuf[i],sizeof(float),1,fp1);			           k=1;                                  }              else k++;             }            printf("\n");            endwtime=MPI_Wtime();            stime=endwtime-startwtime;            printf("elasped clock time = %f\n",stime);                   }           	        if(myid==1)    	{    		    		startwtime=MPI_Wtime();    		    		printf("%d\n",MPI_Points);    		    		FILE * fp2;    		    		if((fp2=fopen("data2.dat","wb"))==NULL)                  {		      printf("cannot open this file\n");		      exit(0);	       }    		    	      	    for(i=0;i<MPI_Points;i++)            {                          if(rbuf[i+1]!=rbuf[i])                 {			           fwrite(&rbuf[i],sizeof(float),1,fp2);			           k=1;                                  }              else k++;             }             printf("\n");             endwtime=MPI_Wtime();             stime=endwtime-startwtime;            printf("elasped clock time = %f\n",stime);       }                    if(myid==2)    	{    		startwtime=MPI_Wtime();    		FILE * fp3;    		if((fp3=fopen("data3.dat","w"))==NULL)         {		      printf("cannot open this file\n");		      exit(0);	       }    		    	      	    for(i=0;i<MPI_Points;i++)            {             if(rbuf[i+1]!=rbuf[i])                 {			           fwrite(&rbuf[i],sizeof(float),1,fp3);			           k=1;                                  }              else k++;             }              printf("\n");              endwtime=MPI_Wtime();              stime=endwtime-startwtime;              printf("elasped clock time = %f\n",stime);       }    	    	    	    if(myid==3)    	{    		startwtime=MPI_Wtime();    		FILE * fp4;    		if((fp4=fopen("data4.dat","w"))==NULL)         {		      printf("cannot open this file\n");		      exit(0);	       }    		    	      	    for(i=0;i<MPI_Points;i++)            {             if(rbuf[i+1]!=rbuf[i])                 {			           fwrite(&rbuf[i],sizeof(float),1,fp4);			           k=1;                                  }              else k++;             }              printf("\n");              endwtime=MPI_Wtime();              stime=endwtime-startwtime;              printf("elasped clock time = %f\n",stime);       }            if(myid==4)    	{    		startwtime=MPI_Wtime();    		FILE * fp5;    		if((fp5=fopen("data5.dat","w"))==NULL)         {		      printf("cannot open this file\n");		      exit(0);	       }    		    	      	    for(i=0;i<MPI_Points;i++)            {             if(rbuf[i+1]!=rbuf[i])                 {			           fwrite(&rbuf[i],sizeof(float),1,fp5);			           k=1;                                  }              else k++;             }              printf("\n");              endwtime=MPI_Wtime();              stime=endwtime-startwtime;              printf("elasped clock time = %f\n",stime);       }              if(myid==5)    	{    		startwtime=MPI_Wtime();    		FILE * fp6;    		if((fp6=fopen("data6.dat","w"))==NULL)         {		      printf("cannot open this file\n");		      exit(0);	       }    		    	      	    for(i=0;i<MPI_Points;i++)            {             if(rbuf[i+1]!=rbuf[i])                 {			           fwrite(&rbuf[i],sizeof(float),1,fp6);			           k=1;                                  }              else k++;             }              printf("\n");              endwtime=MPI_Wtime();              stime=endwtime-startwtime;              printf("elasped clock time = %f\n",stime);       }    	    	    	    	    	    	//MPI_Gather(sbuf, 2, MPI_INT, rbuf, 2, MPI_INT, root, MPI_COMM_WORLD);    	    	//endwtime=MPI_Wtime();    	    //printf("elasped clock time = %f\n",endwtime-startwtime);               MPI_Reduce(&stime,&rtime, 1, MPI_DOUBLE, MPI_SUM, root, MPI_COMM_WORLD);        if(myid==0)    	{    		printf("the total elasped clock time = %f\n",rtime);    	}        MPI_Finalize();    return 0;}            

⌨️ 快捷键说明

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