updated MPI logic for drawing using multiple nodes

git-svn-id: svn://anubis/gvsu@324 45c1a28c-8058-47b2-ae61-ca45b979098e
This commit is contained in:
josh 2008-12-07 23:09:01 +00:00
parent 4cb7cc0e38
commit fc4cf4b420

View File

@ -22,7 +22,8 @@ bool createWindow(int width, int height,
SDL_Surface ** screen, Uint32 ** pixels); SDL_Surface ** screen, Uint32 ** pixels);
void getSizes(int * rank, int * size, int * nprocs); void getSizes(int * rank, int * size, int * nprocs);
void draw(int rank, int world_size, int nprocs, int width, int height, void draw(int rank, int world_size, int nprocs, int width, int height,
Uint32 * pixels, Computation * computation); Uint32 * pixels, Uint32 * taskVals, Computation * computation);
void sendWindowVals(double * winVals, int world_size);
static double x_center = 0.0; static double x_center = 0.0;
static double y_center = 0.0; static double y_center = 0.0;
@ -76,6 +77,8 @@ int main(int argc, char * argv[])
break; break;
} }
unsigned int * taskVals = new unsigned int[width + 1];
double window_vals[4];
if (my_rank == 0) if (my_rank == 0)
{ {
SDL_Event event; SDL_Event event;
@ -86,12 +89,18 @@ int main(int argc, char * argv[])
if (!window_success) if (!window_success)
going = false; going = false;
/* master loop */
while (going && SDL_WaitEvent(&event) != 0) while (going && SDL_WaitEvent(&event) != 0)
{ {
if (redraw) if (redraw)
{ {
window_vals[0] = 0.0;
window_vals[1] = x_center;
window_vals[2] = y_center;
window_vals[3] = zoom;
sendWindowVals(&window_vals[0], world_size);
draw(my_rank, world_size, nprocs, width, height, draw(my_rank, world_size, nprocs, width, height,
pixels, computation); pixels, taskVals, computation);
redraw = false; redraw = false;
} }
SDL_UpdateRect(screen, 0, 0, 0, 0); SDL_UpdateRect(screen, 0, 0, 0, 0);
@ -130,7 +139,27 @@ int main(int argc, char * argv[])
break; break;
} }
} }
window_vals[0] = 1.0;
sendWindowVals(&window_vals[0], world_size);
} }
else
{
/* slave loop */
for (;;)
{
/* wait for a redraw or quit command */
MPI_Recv(&window_vals[0], 4, MPI_DOUBLE,
MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, NULL);
if (window_vals[0] != 0.0)
break;
x_center = window_vals[1];
y_center = window_vals[2];
zoom = window_vals[3];
draw(my_rank, world_size, nprocs, width, height,
NULL, taskVals, computation);
}
}
delete[] taskVals;
MPI_Finalize(); MPI_Finalize();
@ -195,8 +224,9 @@ void getSizes(int * rank, int * size, int * nprocs)
} }
void draw(int rank, int world_size, int nprocs, int width, int height, void draw(int rank, int world_size, int nprocs, int width, int height,
Uint32 * pixels, Computation * computation) Uint32 * pixels, Uint32 * taskVals, Computation * computation)
{ {
MPI_Status mpi_status;
if (world_size == 1) if (world_size == 1)
{ {
for (int y = 0; y < height; y++) for (int y = 0; y < height; y++)
@ -211,22 +241,70 @@ void draw(int rank, int world_size, int nprocs, int width, int height,
} }
else if (rank == 0) else if (rank == 0)
{ {
int done_val = -1;
int num_pixels = width * height; int num_pixels = width * height;
int pixel = 0;
for (int to_proc = 1; to_proc < world_size; to_proc++) for (int to_proc = 1; to_proc < world_size; to_proc++)
{ {
if (pixel < num_pixels)
{
MPI_Send(&pixel, 1, MPI_INT, to_proc, 0, MPI_COMM_WORLD);
pixel += TASK_SIZE;
}
else
{
MPI_Send(&done_val, 1, MPI_INT, to_proc, 0, MPI_COMM_WORLD);
}
}
int num_tasks = (num_pixels + TASK_SIZE - 1) / TASK_SIZE;
for (int received = 0; received < num_tasks; received++)
{
MPI_Recv(taskVals, width + 1, MPI_INT,
MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, &mpi_status);
if (pixel < num_pixels)
{
MPI_Send(&pixel, 1, MPI_INT,
mpi_status.MPI_SOURCE, 0, MPI_COMM_WORLD);
pixel += TASK_SIZE;
}
else
{
MPI_Send(&done_val, 1, MPI_INT,
mpi_status.MPI_SOURCE, 0, MPI_COMM_WORLD);
}
memcpy(pixels + taskVals[0], taskVals + 1, width * sizeof(int));
} }
} }
else else
{ {
for (;;) for (;;)
{ {
int pixel_num;
/* wait to be told what to do */ /* wait to be told what to do */
#if 0 MPI_Recv(&pixel_num, 1, MPI_INT,
MPI_Recv(); MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, NULL);
if () /* exit if we are done */ if (pixel_num < 0) /* exit if we are done */
break; break;
MPI_Send(); /* report results back */ for (int i = 0; i < TASK_SIZE; i++)
#endif {
int this_pixel_num = pixel_num + i;
int x = this_pixel_num % width;
int y = this_pixel_num / width;
double x_virt = getXVirt(x);
double y_virt = getYVirt(y);
pixels[this_pixel_num] = computation->compute(x_virt, y_virt);
}
/* send the computed pixel data to the master node */
MPI_Send(pixels, TASK_SIZE, MPI_INT, 0, 0, MPI_COMM_WORLD);
} }
} }
} }
void sendWindowVals(double * winVals, int world_size)
{
for (int to_proc = 1; to_proc < world_size; to_proc++)
{
MPI_Send(winVals, 4, MPI_DOUBLE, to_proc,
0, MPI_COMM_WORLD);
}
}