📄 parallelfish.c
字号:
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xos.h>
#include <stdio.h>
#include <string.h>
#include <math.h>#include <mpi.h>#define false 0;#define true 1;
Window win; Display *display;GC gc;XColor color[6]; int myid, numprocs;
void makeAllCellsOcean ( );void makeCellsFish ();void countFishSharks();void makeCellsSharks();void moveAndFeed();void sharksDigest();void sharksSearch();void renderOcean();void run();/*---------------------------------------变量定义-------------------------------------------------------------------*/
int ocean[80][80][2];
int e, s, east, south, emove, smove;
int i,j,k,l,m,n;
int maxSize=80;
int iterations=0;
int fishBegPop=1300;
int sharkBegPop=320;
int sharkType;
int fishGestation=8;
int sharkGestation=16;
int fishPop, sharkPop; Colormap colormap; int task1,task2,lost; /*------------------------------------------------------------------------------------------------------------------*/
int main (int argc,char *argv[])
{ MPI_Init(&argc,&argv); MPI_Comm_size(MPI_COMM_WORLD,&numprocs); MPI_Comm_rank(MPI_COMM_WORLD,&myid); if (myid==0) {
unsigned
int width, height,
x, y,
border_width,
display_width, display_height,
screen;
char *window_name = "Mandelbrot Set", *display_name = NULL;
unsigned
long valuemask = 0;
XGCValues values;
XSizeHints size_hints;
Pixmap bitmap;
XPoint points[800];
FILE *fp, *fopen ();
char str[100];
XSetWindowAttributes attr[1];
if ( (display = XOpenDisplay (display_name)) == NULL ) {
fprintf (stderr, "drawon: cannot connect to X server %s\n",
XDisplayName (display_name) );
exit (-1);
}
screen = DefaultScreen (display);
display_width = DisplayWidth (display, screen);
display_height = DisplayHeight (display, screen);
width = 450;
height = 450;
x = 0;
y =0;
border_width = 4;
win = XCreateSimpleWindow (display, RootWindow (display, screen),
x, y, width, height, border_width,
BlackPixel (display, screen), WhitePixel (display, screen));
size_hints.flags = USPosition|USSize;
size_hints.x = x;
size_hints.y = y;
size_hints.width = width;
size_hints.height = height;
size_hints.min_width = 300;
size_hints.min_height = 300;
XSetNormalHints (display, win, &size_hints);
XStoreName(display, win, window_name);
/* create graphics context */
gc = XCreateGC (display, win, valuemask, &values);
XSetBackground (display, gc, color[2].pixel);
XSetForeground (display, gc, BlackPixel (display, screen));
XSetLineAttributes (display, gc, 1, LineSolid, CapRound, JoinRound);
attr[0].backing_store = Always;
attr[0].backing_planes = 1;
attr[0].backing_pixel = BlackPixel(display, screen);
XChangeWindowAttributes(display, win, CWBackingStore | CWBackingPlanes | CWBackingPixel, attr);
XMapWindow (display, win);
XSync(display, 0); //1---红色;2-----黄色;3-------蓝色;5-------绿色;6--------黑色; colormap = DefaultColormap(display, DefaultScreen(display)); color[1].red =65535; color[1].green=0;color[1].blue=0; color[2].red = color[2].green =65535;color[2].blue=0; color[3].red=0; color[3].green=0; color[3].blue=65535; color[5].red=color[5].blue=00;color[5].green=65535; color[6].red =0; color[6].green=0;color[6].blue=0; color[1].flags = color[2].flags=color[2].flags=color[3].flags=color[5].flags=color[6].flags=DoRed | DoGreen | DoBlue; XAllocColor(display, colormap, &color[1]); XAllocColor(display, colormap, &color[2]); XAllocColor(display, colormap, &color[3]); XAllocColor(display, colormap, &color[5]); XAllocColor(display, colormap, &color[6]); /*------------------------------------------函数调用----------------------------------------------------------------*/ master( ); /*------------------------------------------------------------------------------------------------------------------*/
XFlush (display); sleep(5); } else { slave(); }
MPI_Finalize(); exit(0);}
void makeAllCellsOcean ( )
{ for (east=0; east<maxSize; east++)
{ for (south=0; south<maxSize; south++)
{ ocean[east][south][0]=0;
XFillRectangle(display,win,gc,east*5+25,south*5+25,5,5);
}
}
}void makeCellsFish ( )
{ int position[2]; for (i=1; i<=fishBegPop; i++)
{
south=random(maxSize); south=south%80; east=random(maxSize); east=east%80;
ocean[east][south][0]=6;
int f=random(8); f=f%8;
ocean[east][south][1]=f; position[0]=east;position[1]=south;position[2]=5; if(myid<2) MPI_Send(&position,3, MPI_INT,0,98, MPI_COMM_WORLD);
}
iterations=0;
//countFishSharks(myid);
}void countFishSharks()
{ fishPop=0;
sharkPop=0;
float pctSharks;
for (east=0; east<maxSize; east++)
{ for (south=0; south<maxSize; south++)
{ if (ocean[east][south][0]==6)
{ fishPop++;
}
else if (ocean[east][south][0]>0)
{ sharkPop++;
ocean[east][south][1]++;
}
}
}
pctSharks = 100*sharkPop/(fishPop+sharkPop+1);
}void makeCellsSharks( )
{ int position[2]; for (i=1; i<=sharkBegPop; i++)
{ south=random(maxSize); south=south%80; east=random(maxSize); east=east%80; int f=random(5); f=f%5;
sharkType=f;
ocean[east][south][0]=sharkType;
ocean[east][south][1] = random(sharkGestation);
switch (sharkType)
{ case 0:
// a starving shark
ocean[east][south][0]=1; position[0]=east;position[1]=south;position[2]=1; if(myid<2) MPI_Send(&position,3, MPI_INT,0,98, MPI_COMM_WORLD);
break;
// a not full shark
case 1: case 2: case 3:
ocean[east][south][0]=3;
position[0]=east;position[1]=south;position[2]=2; if(myid<2) MPI_Send(&position,3, MPI_INT,0,98, MPI_COMM_WORLD);
break;
// a full shark
case 4:
ocean[east][south][0]=5; position[0]=east;position[1]=south;position[2]=3; if(myid<2) MPI_Send(&position,3, MPI_INT,0,98, MPI_COMM_WORLD);
break;
}
}
countFishSharks(myid);
}void moveAndFeed()
{ for (east=0; east<maxSize; east++)
{ for (south=(myid-1)*task1; south<task2; south++)
{ if (ocean[east][south][0]==6)
{ int f=random(9);int move; f=f%9; move=f;
emove=east; smove=south;
switch (move)
{ case 0: // stay put
emove=east; smove=south; break;
case 1: // move NW
emove=east-1; smove=south-1; break;
case 2: // move W
emove=east-1; smove=south; break;
case 3: // move SW
emove=east-1; smove=south+1; break;
case 4: //move N
emove=east; smove=south-1; break;
case 5: // move S
emove=east; smove=south+1; break;
case 6: // move NE
emove=east+1; smove=south-1; break;
case 7: // move E
emove=east+1; smove=south; break;
case 8: // move SE
emove=east+1; smove=south+1; break;
}
if (emove<0) emove=maxSize-1;
if (emove==maxSize) emove=0;
if (smove<0) smove=maxSize-1;
if (smove==maxSize) smove=0;
if (ocean[emove][smove][0]==0)
{
if (ocean[east][south][1]>=fishGestation)
{ ocean[east][south][1]=0;
ocean[emove][smove][1]=-1;
}
else ocean[east][south][0]=0;
ocean[emove][smove][0]=6;
ocean[emove][smove][1]++;
}
else if (ocean[emove][smove][0]<6) // we've found a shark
{ switch (ocean[emove][smove][0])
{ case 1: case 2: case 3: case 4:
ocean[east][south][0]=ocean[emove][smove][0]+1;
ocean[emove][smove][0]=0;
ocean[east][south][1]=ocean[emove][smove][1];
if (ocean[emove][smove][1]>=sharkGestation)
{ ocean[emove][smove][0]=1;
ocean[emove][smove][1]=1;
ocean[east][south][0]--;
}
break;
case 5:
break;
}
}
}
}
}
}void sharksDigest()
{ for (east=0; east<maxSize; east++)
{ for (south=(myid-1)*task1; south<task2; south++)
{ int t=random(4); if (t%4>0) continue;
else if (ocean[east][south][0]>0 && ocean[east][south][0]<6)
{ ocean[east][south][0]=ocean[east][south][0]-1;
}
}
}
}void sharksSearch()
{ for (east=0; east<maxSize; east++)
{ for (south=(myid-1)*task1; south<task2; south++)
{ if (ocean[east][south][0]>0 && ocean[east][south][0]<5)
{ int f=random(9); int move; f=f%9; move=f;
switch (move)
{ case 0: // stay put
emove=east; smove=south; break;
case 1: // move NW
emove=east-1; smove=south-1; break;
case 2: // move W
emove=east-1; smove=south; break;
case 3: // move SW
emove=east-1; smove=south+1; break;
case 4: //move N
emove=east; smove=south-1; break;
case 5: // move S
emove=east; smove=south+1; break;
case 6: // move NE
emove=east+1; smove=south-1; break;
case 7: // move E
emove=east+1; smove=south; break;
case 8: // move SE
emove=east+1; smove=south+1; break;
}
if (emove<0) emove=maxSize-1;
if (emove==maxSize) emove=0;
if (smove<0) smove=maxSize-1;
if (smove==maxSize) smove=0;
if (ocean[emove][smove][0]==0)
{ ocean[emove][smove][0]=ocean[east][south][0];
ocean[east][south][0]=0;
}
else if (ocean[emove][smove][0]==6)
{ ocean[emove][smove][0]=ocean[east][south][0]+1;
ocean[east][south][0]=0;
}
}
}
}
}void renderOcean()
{ int position[2]; for (east=0; east<maxSize; east++)
{ for (south=(myid-1)*task1; south<task2; south++)
{ switch ( ocean[east][south][0] )
// 0 is nothing, 1-5 is shark, 6 is fish
{ case 0: position[0]=east;position[1]=south;position[2]=6; MPI_Send(&position,3, MPI_INT,0,98, MPI_COMM_WORLD);
break;
case 1: position[0]=east;position[1]=south;position[2]=1; MPI_Send(&position,3, MPI_INT,0,98, MPI_COMM_WORLD);
break;
case 2: case 3: case 4: position[0]=east;position[1]=south;position[2]=2; MPI_Send(&position,3, MPI_INT,0,98, MPI_COMM_WORLD);
break;
case 5: position[0]=east;position[1]=south;position[2]=3; MPI_Send(&position,3, MPI_INT,0,98, MPI_COMM_WORLD);
break;
case 6: position[0]=east;position[1]=south;position[2]=5; MPI_Send(&position,3, MPI_INT,0,98, MPI_COMM_WORLD);
break;
}
}
}
}void run()
{
while(1) {
moveAndFeed();
sharksSearch();
sharksDigest();
renderOcean(win,display,gc);
countFishSharks();
iterations++;
}
}master( ) { int position[2]; MPI_Status status; int meast,msouth,mcolor; makeAllCellsOcean( ); while(1) { MPI_Recv(&position, 3, MPI_INT,MPI_ANY_SOURCE, MPI_ANY_TAG,MPI_COMM_WORLD, &status); meast=position[0],msouth=position[1],mcolor=position[2]; XSetForeground (display, gc, color[mcolor].pixel);
XFillRectangle(display,win,gc,meast*5+25,msouth*5+25,5,5); } }slave( ) { task1=(int)maxSize/(numprocs-1); lost=maxSize-task1*(numprocs-1); if(myid==numprocs-1) task2=myid*task1+lost+1; else task2=myid*task1; makeCellsFish ( ); makeCellsSharks( ); while(1){ moveAndFeed();
sharksSearch();
sharksDigest();
renderOcean(win,display,gc);
countFishSharks();
iterations++;} exit(0); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -