?? heatdis-mn0-pq.c
字號:
/*******************************************************
*****************Title: Heat Distribution Problem******
*****************File: heatdis.c **********************
*****************Author: Sheng Di**********************
*****************Email: sdi@cs.hku.hk******************
*******************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include <mpi.h>
#define ITERATIVE_TIMES 100000
#define PRECISION 0.001
#define WORKTAG 11
#define QUITTAG 22
char filename[100];
enum BOOLEAN {FALSE=0,TRUE=1};
void init(int DEVIDE_SIZE_M,int DEVIDE_SIZE_N, double original[][]);
void reduceData(int rowsize, int numprocs, int DEVIDE_SIZE_N, double original[][]);
void doDistribution(int numprocs, int currentRank, int rowsize, int DEVIDE_SIZE_N, double **h,double **g);
//void resetData(int numprocs, int currentRank, int rowsize, double h[][], double g[][]);
void print(int rowsize,int DEVIDE_SIZE_N, double original[][]);
//void printlines(int DEVIDE_SIZE_N, int row1, int row2, double original[][]);
void initializeData2(int procs, int currentRank, int _rowsize_, double **h, double **g);
void print_solution (int M, int N, char *filename, double u[][]);
void allocate_2d_array (int r, int c, double ***a);
void print2(int rowsize,int DEVIDE_SIZE_N, double **original);
//void copy(int rowsize, double **g,double g0[][]);
/**
* usage: input one argument: DEVIDE_SIZE
*/
int DEVIDE_SIZE_M = 200;
int DEVIDE_SIZE_N = 200;
double starttime, endtime;
int main(int argc, char *argv[])
{
double **h;
double **g;
int myrank, numprocs,i,j;
double tick;
int rowsize;
printf("-");
if(argc != 7)
{
//printf("Usage: input one argument: DEVIDE_SIZE\nmpirun -np [num] -machinefile ~/machines1 [M] [N]\n");
}
else
{
DEVIDE_SIZE_M = atoi(argv[1]);
DEVIDE_SIZE_N = atoi(argv[2]);
}
double original[DEVIDE_SIZE_M][DEVIDE_SIZE_N];
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &numprocs);
MPI_Comm_rank(MPI_COMM_WORLD, &myrank);/* get the rank */
fflush(stdout);
// printf("num=%d\n",numprocs);
int tempdata[2];
tempdata[0] = DEVIDE_SIZE_M;
tempdata[1] = DEVIDE_SIZE_N;
MPI_Bcast(tempdata, 2, MPI_INT, 0, MPI_COMM_WORLD);
DEVIDE_SIZE_M = tempdata[0];
DEVIDE_SIZE_N = tempdata[1];
rowsize = DEVIDE_SIZE_M / numprocs;
//printf("DEVIDE_SIZE=%d\n",DEVIDE_SIZE);
//printf("numprocs=%d\n",numprocs);
//printf("rowsize=%d\n",rowsize);
//printf("DEVIDE_SIZE mod numprocs = %d\n",DEVIDE_SIZE%numprocs);
allocate_2d_array(rowsize+2,DEVIDE_SIZE_N,&h);
allocate_2d_array(rowsize+2,DEVIDE_SIZE_N,&g);
if(myrank == 0)
{
initializeData2(numprocs, myrank , rowsize+2, h , g);
sprintf(filename, "%s.dat", argv[0]);
printf("\nProblem size: M=%d,N=%d\n",DEVIDE_SIZE_M,DEVIDE_SIZE_N);
fflush(stdout);
MPI_Barrier(MPI_COMM_WORLD);
starttime = MPI_Wtime();
//printf("rank: %d, elapsed=%lf\n", myrank,MPI_Wtime()-starttime);
doDistribution(numprocs, myrank, rowsize, DEVIDE_SIZE_N,h,g);
/**
* converge data.
*/
reduceData(rowsize, numprocs, DEVIDE_SIZE_N, original);
for(i = 0;i < rowsize;i++)
for(j = 0;j <= DEVIDE_SIZE_N - 1; j++)
original[i][j] = h[i+1][j];
endtime = MPI_Wtime();
printf("WTIME=%lf\n", endtime-starttime);
tick = MPI_Wtick();
printf("The precision of time is:%lf\n",tick);
/**
* print out the result.
*/
//print(DEVIDE_SIZE_M, DEVIDE_SIZE_N, original);
printf("Finished^^\n");
printf("The heat-distribution result copying... \n");
print_solution(DEVIDE_SIZE_M,DEVIDE_SIZE_N, filename, original);
}
else
{
//printf("M=%d,N=%d\n",DEVIDE_SIZE_M,DEVIDE_SIZE_N);
initializeData2(numprocs, myrank , rowsize+2, h , g);
//printf("rank: %d, elapsed=%lf loc: before barrier.\n", myrank,MPI_Wtime()-starttime);fflush(stdout);
MPI_Barrier(MPI_COMM_WORLD);
//printf("rank: %d, elapsed=%lf loc: after barrier.\n", myrank,MPI_Wtime()-starttime);fflush(stdout);
doDistribution(numprocs, myrank, rowsize, DEVIDE_SIZE_N,h,g);
}
MPI_Finalize();
return 0;
}
/**
* Initialize data. (never been used in this program, maybe usable for future^^)
*/
void init(int DEVIDE_SIZE_M, int DEVIDE_SIZE_N, double original[][DEVIDE_SIZE_N])
{
int i,j;
for(i = 1;i < DEVIDE_SIZE_M - 1; i++)
for(j = 1;j < DEVIDE_SIZE_N - 1; j++)
original[i][j] = 25;
for(i = 0;i < DEVIDE_SIZE_M; i++)
{
original[i][DEVIDE_SIZE_N-1] = 0;
original[i][0] = 0;
original[DEVIDE_SIZE_M-1][i] = 0;
original[0][i] = 1000.0;
}
}
/**
* Reduce data by rows
**
**********************/
void reduceData(int rowsize, int numprocs, int DEVIDE_SIZE_N, double original[][DEVIDE_SIZE_N])
{
MPI_Status status;
int count;
int source_ID,i,j;
double p[rowsize+2][DEVIDE_SIZE_N];
//printf("====reduceData\n");
for(count = 1; count <= numprocs - 1; count++ )
{
MPI_Recv(&p[0], (rowsize+2)*DEVIDE_SIZE_N, MPI_DOUBLE, MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, &status);
//printf("reduce data from %d------------------------\n",status.MPI_SOURCE);
//print(rowsize+2,DEVIDE_SIZE_N,p);
if(status.MPI_TAG == QUITTAG)
{
source_ID = status.MPI_SOURCE;
for(i = 1; i <= rowsize; i++)
{
int rownumber = source_ID * rowsize + i - 1;
for(j = 0; j < DEVIDE_SIZE_N; j++)
{
original[rownumber][j] = p[i][j];
}
}
}
else
printf("EXCEPT ERROR!\n");
}
}
/**
* distribute data (when running processes)
* assume the DEVIDE_SIZE%numprocs == 0
* currentRank ( from 1 to (numprocs -1))
* Notice: numprocs includes the root process ( whose ID = 0).
* g[][] and h[][] are both of rowsize X DEVIDE_SIZE. rowsize is heigth, DEVIDE_SIZE is width.
* compute from 1 to DEVIDE_SIZE-2.
* including accelerator-strategy
*/
void doDistribution(int numprocs, int currentRank, int rowsize, int DEVIDE_SIZE_N,double **h,double **g)
{
MPI_Status status;
int k,i,j;
double timestamp;
int _rowsize_ = rowsize + 2; //include two ghost arrays.
double p[_rowsize_][DEVIDE_SIZE_N];
// printf("====numprocs=%d\n",numprocs);
//printf("DEVIDE_SIZE=%d-----------------------------------------------------------\n",DEVIDE_SIZE);
double localerror;
double globalerror = 0;
//printf("rank: %d, elapsed=%lf loc: begin doDistribution.\n", currentRank,MPI_Wtime()-starttime);fflush(stdout);
for(k = 0;k < ITERATIVE_TIMES; k++)
{
//timestamp = MPI_Wtime();
//if(k % 100 == 0 )
// printf("for: %d rank: %d, elapsed=%lf\n",k, currentRank,MPI_Wtime()-starttime);fflush(stdout);
//printf("<<%d>>k=%d,ITERATIVE_TIMES=%d\n",currentRank,k,ITERATIVE_TIMES);
localerror = 0;
double time000 = MPI_Wtime();
if(currentRank == 0)
{
for(i = 2; i <= rowsize; i++)
for(j = 1; j < DEVIDE_SIZE_N - 1; j++)
{
g[i][j] = 0.25*(h[i-1][j]+h[i+1][j]+h[i][j-1]+h[i][j+1]);
if(localerror < fabs(g[i][j] - h[i][j]))
localerror = fabs(g[i][j] - h[i][j]);
}
for(i = 1; i <= rowsize; i++)
for(j = 1; j < DEVIDE_SIZE_N - 1; j++)
h[i][j] = g[i][j];
//printf("localerror=%lf\n",localerror);
// printf("for: %d rank: %d(1), ----------compute time=%lf\n",k, currentRank,timestamp2-timestamp);fflush(stdout);
MPI_Send(&g[rowsize][0], DEVIDE_SIZE_N, MPI_DOUBLE, 1, WORKTAG, MPI_COMM_WORLD);
MPI_Recv(&h[rowsize + 1][0], DEVIDE_SIZE_N, MPI_DOUBLE, 1, WORKTAG, MPI_COMM_WORLD, &status);
}
else if(currentRank == (numprocs -1))
{
for(i = 1; i <= rowsize - 1; i++)
for(j = 1; j < DEVIDE_SIZE_N - 1; j++)
{
g[i][j] = 0.25*(h[i-1][j]+h[i+1][j]+h[i][j-1]+h[i][j+1]);
if(localerror < fabs(g[i][j] - h[i][j]))
localerror = fabs(g[i][j] - h[i][j]);
}
for(i = 1; i <= rowsize; i++)
for(j = 1; j < DEVIDE_SIZE_N - 1; j++)
h[i][j] = g[i][j];
MPI_Send(&g[1][0], DEVIDE_SIZE_N, MPI_DOUBLE, currentRank - 1, WORKTAG, MPI_COMM_WORLD);
MPI_Recv(&h[0][0], DEVIDE_SIZE_N, MPI_DOUBLE, currentRank - 1, WORKTAG, MPI_COMM_WORLD, &status);
}
else // currentRank == 1 ... numprocs - 2
{
//compute all elements from the 1st line to the second last line.
for(i = 1; i <= rowsize; i++)
for(j = 1; j < DEVIDE_SIZE_N - 1; j++)
{
g[i][j] = 0.25*(h[i-1][j]+h[i+1][j]+h[i][j-1]+h[i][j+1]);
if(localerror < fabs(g[i][j] - h[i][j]))
localerror = fabs(g[i][j] - h[i][j]);
}
for(i = 1; i <= rowsize; i++)
for(j = 1; j < DEVIDE_SIZE_N - 1; j++)
h[i][j] = g[i][j];
MPI_Send(&g[1][0], DEVIDE_SIZE_N, MPI_DOUBLE, currentRank - 1, WORKTAG, MPI_COMM_WORLD);
MPI_Send(&g[rowsize][0], DEVIDE_SIZE_N, MPI_DOUBLE, currentRank + 1, WORKTAG, MPI_COMM_WORLD);
MPI_Recv(&h[0][0], DEVIDE_SIZE_N, MPI_DOUBLE, currentRank - 1, WORKTAG, MPI_COMM_WORLD, &status);
MPI_Recv(&h[rowsize + 1][0], DEVIDE_SIZE_N, MPI_DOUBLE, currentRank + 1, WORKTAG, MPI_COMM_WORLD, &status);
}
//printf("<>currentRank: %d,localerror:%lf,globalerror:%lf\n",currentRank,localerror,globalerror);
//double timestamp2 = MPI_Wtime();
//if(k % 100 == 0 )
// printf("for: %d rank: %d(1), compute time=%lf\n",k, currentRank,timestamp2-timestamp);fflush(stdout);
MPI_Allreduce(&localerror,&globalerror,1,MPI_DOUBLE,MPI_MAX, MPI_COMM_WORLD); // very weird! why k will be changed?
//printf("--currentRank: %d, k:%d,globalerror=%lf\n",currentRank, k,globalerror);
//if(k % 100 == 0 )
// printf("for: %d rank: %d(2), allreduce w-time=%lf\n",k, currentRank,MPI_Wtime()-timestamp2);fflush(stdout);
if(globalerror < PRECISION)
{
// printf("Rank: %d, execution times: %d\n",currentRank, k);
break;
}
}
if(currentRank !=0)
{
//printf("data from %d------------------------\n",currentRank);
//print2(rowsize+2,DEVIDE_SIZE_N,h);
for(i = 0;i<rowsize+2;i++)
for( j = 0;j<DEVIDE_SIZE_N;j++)
p[i][j] = h[i][j];
MPI_Send(&p[0], (rowsize+2)*DEVIDE_SIZE_N, MPI_DOUBLE, 0, QUITTAG, MPI_COMM_WORLD);
}
}
/*
* Initialize data
* 1000 0 0 0
* 1000 25 25 0
* 1000 25 25 0
* 1000 0 0 0
*/
void initializeData2(int procs, int currentRank, int _rowsize_, double **h, double **g)
{
extern int DEVIDE_SIZE_N;
int i, j;
for(i = 0; i < _rowsize_; i = i + 1)
{
for(j = 0; j < DEVIDE_SIZE_N - 1; j++)
{
g[i][j] = h[i][j] = 25.0;
}
}
for(j = 0;j < DEVIDE_SIZE_N;j++)
{
g[_rowsize_ - 1][j] = h[_rowsize_ - 1][j] = 25;
}
//initialize the 1st line
if(currentRank == 0)
{
for(j = 0;j < DEVIDE_SIZE_N;j++)
{
g[1][j] = h[1][j] = 0;
}
}
if(currentRank == procs - 1)
for(j = 0;j < DEVIDE_SIZE_N;j++)
{
g[_rowsize_ - 2][j] = h[_rowsize_ - 2][j] = 0.0;
}
for(i = 0;i < _rowsize_;i++)
{
g[i][0] = h[i][0] = 1000;
g[i][DEVIDE_SIZE_N-1] = h[i][DEVIDE_SIZE_N-1] = 0.0;
}
g[0][0]=g[_rowsize_-1][0]=h[0][0]=h[_rowsize_-1][0] = 0;
//printf("initialize data................................\n");
//print(_rowsize_ ,DEVIDE_SIZE_M,h);
}
/* Print solution to standard output or a file */
void print_solution (int M, int N, char *filename, double u[][DEVIDE_SIZE_N])
{
int i, j;
char sep;
FILE *outfile;
if (!filename) {
sep = '\t'; /* just for easier view */
outfile = stdout;
} else {
sep = '\n'; /* for gnuplot format */
outfile = fopen(filename,"w");
if (outfile == NULL) {
printf("Can't open output file.");
exit(-1);
}
}
/* Print the solution array */
for (i = 0; i < M; i++) {
for (j = 0; j < N; j++)
fprintf (outfile, "%6.2f%c", u[i][j], sep);
fprintf(outfile, "\n"); /* Empty line for gnuplot */
}
if (outfile != stdout)
fclose(outfile);
#ifdef INTERACTIVE
/* Copy to another data file for gnuplot to plot */
if (outfile != stdout) {
char tempname[100]; /* Temp file name for gnuplot */
char cmd[1024]; /* System command buffer */
sprintf(tempname, "jacobi.dat");
sprintf(cmd, "cp %s %s", filename, tempname);
system(cmd);
}
#endif
}
/* Allocate two-dimensional array. */
void allocate_2d_array (int r, int c, double ***a)
{
double *storage;
int i;
storage = (double *) malloc (r * c * sizeof(double));
*a = (double **) malloc (r * sizeof(double *));
for (i = 0; i < r; i++)
(*a)[i] = &storage[i * c];
}
void print(int rowsize,int DEVIDE_SIZE_N, double original[][DEVIDE_SIZE_N])
{
int i,j;
// printf("print result.\n");
for(i = 0; i < rowsize; i ++)
{
for(j = 0; j < DEVIDE_SIZE_N; j++)
{
printf("%10.4f", original[i][j]);
}
printf("\n");
}
fflush(stdout);
}
void print2(int rowsize,int DEVIDE_SIZE_N, double **original)
{
int i,j;
// printf("print result.\n");
for(i = 0; i < rowsize; i ++)
{
for(j = 0; j < DEVIDE_SIZE_N; j++)
{
printf("%10.4f", original[i][j]);
}
printf("\n");
}
fflush(stdout);
}
/*
void printlines(int DEVIDE_SIZE_N, int row1, int row2, double original[][DEVIDE_SIZE_N])
{
int i,j;
fflush(stdout);
for(i = row1; i <= row2; i ++)
{
for(j = 0; j < DEVIDE_SIZE_N; j++)
{
printf("%10.4f", original[i][j]);
}
printf("\n");
}
fflush(stdout);
}
*/
/*
* Initialize data
* 1000 1000 1000 1000
* 0 25 25 0
* 0 25 25 0
* 0 0 0 0
*/
/*
void initializeData(int procs, int currentRank, int _rowsize_, double h[][DEVIDE_SIZE_N], double g[][DEVIDE_SIZE_N])
{
extern int DEVIDE_SIZE_N;
int i, j;
//init the last line
for(i = 0;i < _rowsize_;i++)
g[i][0] = h[i][0] = g[i][DEVIDE_SIZE_N-1] = h[i][DEVIDE_SIZE_N-1] = 0.0;
for(j = 0;j < DEVIDE_SIZE_N;j++)
{
g[_rowsize_ - 1][j] = h[_rowsize_ - 1][j] = 25;
}
// init 1 ~ rowsize lines
for(i = 1; i < _rowsize_ - 1; i = i + 1)
{
for(j = 1; j < DEVIDE_SIZE_N - 1; j++)
{
g[i][j] = h[i][j] = 25.0;
}
}
//initialize the 1st line
if(currentRank == 0)
{
for(j = 0;j < DEVIDE_SIZE_N;j++)
{
g[1][j] = h[1][j] = 1000.0;
}
}
else // currentRank != 1
for(j = 0;j < DEVIDE_SIZE_N;j++)
{
g[0][j] = h[0][j] = 25.0;
}
if(currentRank == procs - 1)
for(j = 0;j < DEVIDE_SIZE_N;j++)
{
g[_rowsize_ - 2][j] = h[_rowsize_ - 2][j] = 0.0;
}
}
*/
/**
* copy g data to g0
* **g g0[][]
*/
/*
void copy(int rowsize, double **g,double g0[][DEVIDE_SIZE_N])
{
int i,j;
for(i = 0; i < rowsize;i++)
for(j = 0; j < DEVIDE_SIZE_N;j++)
{
g0[i+1][j] = g[i][j];
}
}
*/
/**
* print out the result
*/
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -