void DrawBlock(MPE_XGraph graph, MPE_Point *pointData, rect *r) { //printf("block color: %d\n", pointData->c); MPE_Fill_rectangle(graph, r->l, r->t, r->r - r->l + 1, r->b - r->t + 1, pointData->c); MPE_Update(graph); }
static void DrawScreen_0(int procid, int np) { int width, procNum, radius; double angle; readyToDraw_0 = 0; procCoords_0 = (point *) malloc( sizeof( point ) * np ); radius = (PROC_SEPARATION_0*np)/3.1416; width = (radius + PROC_RADIUS_0) * 2 * MARGIN_0; MPE_Open_graphics( &prof_graph_0, MPI_COMM_WORLD, 0, xpos_0, ypos_0, width, width, 0 ); readyToDraw_0 = 1; if (procid == 0) MPE_Fill_rectangle( prof_graph_0, 0, 0, width, width, MPE_WHITE ); MPE_Draw_logic( prof_graph_0, MPE_LOGIC_INVERT ); for (procNum=0; procNum < np; procNum++) { angle = (((double)procNum)/np)*3.1416*2 + 3.1416/2; procCoords_0[procNum].x = width/2 + radius * cos( angle ); procCoords_0[procNum].y = width/2 - radius * sin( angle ); if (procid_0 == 0) { MPE_Fill_circle( prof_graph_0, procCoords_0[procNum].x, procCoords_0[procNum].y, PROC_RADIUS_0, MPE_GREEN ); } } MPE_Update( prof_graph_0 ); }
double life( int matrix_size, int ntimes, MPI_Comm comm ) { int rank, size ; int next, prev ; int i, j, k; int mysize, sum ; int **matrix, **temp, **addr ; double slavetime, totaltime, starttime ; int my_offset; /* Determine size and my rank in communicator */ MPI_Comm_size(comm, &size) ; MPI_Comm_rank(comm, &rank) ; /* Set neighbors */ if (rank == 0) prev = MPI_PROC_NULL; else prev = rank-1; if (rank == size - 1) next = MPI_PROC_NULL; else next = rank+1; /* Determine my part of the matrix */ mysize = matrix_size/size + ((rank < (matrix_size % size)) ? 1 : 0 ) ; my_offset = rank * (matrix_size/size); if (rank > (matrix_size % size)) my_offset += (matrix_size % size); else my_offset += rank; /* allocate the memory dynamically for the matrix */ matrix = (int **) malloc(sizeof(int *)*(mysize+2)) ; temp = (int **) malloc(sizeof(int *)*(mysize+2)) ; for (i = 0; i < mysize+2; i++) { matrix[i] = (int *) malloc(sizeof(int)*(matrix_size+2)) ; temp[i] = (int *) malloc(sizeof(int)*(matrix_size+2)) ; } /* Initialize the boundaries of the life matrix */ for (j = 0; j < matrix_size+2; j++) matrix[0][j] = matrix[mysize+1][j] = temp[0][j] = temp[mysize+1][j] = DIES ; for (i = 0; i < mysize+2; i++) matrix[i][0] = matrix[i][matrix_size+1] = temp[i][0] = temp[i][matrix_size+1] = DIES ; /* Initialize the life matrix */ for (i = 1; i <= mysize; i++) { srand48((long)(1000^(i-1+mysize))) ; for (j = 1; j<= matrix_size; j++) if (drand48() > 0.5) matrix[i][j] = BORN ; else matrix[i][j] = DIES ; } /* Open the graphics display */ MPE_Open_graphics( &graph, MPI_COMM_WORLD, displayname, -1, -1, width, height, 0 ); /* Play the game of life for given number of iterations */ starttime = MPI_Wtime() ; for (k = 0; k < ntimes; k++) { MPI_Request req[4]; MPI_Status status[4]; /* Send and receive boundary information */ MPI_Isend(&matrix[1][0],matrix_size+2,MPI_INT,prev,0,comm,req); MPI_Irecv(&matrix[0][0],matrix_size+2,MPI_INT,prev,0,comm,req+1); MPI_Isend(&matrix[mysize][0],matrix_size+2,MPI_INT,next,0,comm,req+2); MPI_Irecv(&matrix[mysize+1][0],matrix_size+2,MPI_INT,next,0,comm,req+3); MPI_Waitall(4, req, status); /* For each element of the matrix ... */ for (i = 1; i <= mysize; i++) { for (j = 1; j < matrix_size+1; j++) { /* find out the value of the current cell */ sum = matrix[i-1][j-1] + matrix[i-1][j] + matrix[i-1][j+1] + matrix[i][j-1] + matrix[i][j+1] + matrix[i+1][j-1] + matrix[i+1][j] + matrix[i+1][j+1] ; /* check if the cell dies or life is born */ if (sum < 2 || sum > 3) temp[i][j] = DIES ; else if (sum == 3) temp[i][j] = BORN ; else temp[i][j] = matrix[i][j] ; { int xloc, yloc, xwid, ywid; xloc = ((my_offset + i - 1) * width) / matrix_size; yloc = ((j - 1) * height) / matrix_size; xwid = ((my_offset + i) * width) / matrix_size - xloc; ywid = (j * height) / matrix_size - yloc; MPE_Fill_rectangle( graph, xloc, yloc, xwid, ywid, temp[i][j] ); } } } MPE_Update( graph ); /* Swap the matrices */ addr = matrix ; matrix = temp ; temp = addr ; } /* Return the average time taken/processor */ slavetime = MPI_Wtime() - starttime; MPI_Reduce(&slavetime, &totaltime, 1, MPI_DOUBLE, MPI_SUM, 0, comm); return (totaltime/(double)size); }
main(int argc, char* argv[]) { int me, np, i; int x, y, w, h; int x1, y1, x2, y2; int color, button, pressed; int space, num_colors; MPE_Color col_array[64]; /* Color array */ MPE_Point points[80]; /* Array to store 80 pixels in */ w=10; h=20; /* Width and height of colored rectangles */ MPI_Init (&argc, &argv); /* Initialize MPI */ MPI_Comm_size(MPI_COMM_WORLD, &np); /* Get nr of processes */ MPI_Comm_rank(MPI_COMM_WORLD, &me); /* Get own id */ /* Check that we have exactly two processes */ if (np != 2) { if (me == 0) printf("You have to run this program with 2 processes\n"); MPI_Finalize(); exit(0); } if (me == 0) printf("Opening a graphics window\n") ; /* Open the graphics window in position (0,0) on the display */ MPE_Open_graphics(&graph, MPI_COMM_WORLD, displayname, 0, 0, width, height, 0 ); /* Get number of colors */ MPE_Num_colors(graph, &num_colors); if (me == 0) { printf("Number of colors = %d,\n", num_colors) ; } /* Processor 0 puts a pink rectangle of size 100*240 in position (0,0) */ if (me==0) { MPE_Fill_rectangle( graph, 0, 0, 100, 240, MPE_PINK ); MPE_Update( graph ); } /* Both processors draws one rectangle for each colour, placed in a row */ x=0; y=(me)*50; space=2; for (color=0; color<num_colors; color++) { x=x+w+space; /* Set position of rectangles */ MPE_Fill_rectangle( graph, x, y, w, h, color ); } MPE_Update( graph ); /* Create a new color array with 64 colours */ MPE_Make_color_array(graph, 64, col_array); /* Get number of colors */ MPE_Num_colors(graph, &num_colors); if ( me == 0 ) { printf("Number of colors = %d,\n", num_colors) ; } /* Both processors draw one rectangle for each colour, placed in a row */ x=0; y=(me+1)*100; for (color=0; color<num_colors; color++) { x=x+w+space; /* Set position of rectangles */ MPE_Fill_rectangle( graph, x, y, w, h, color ); } MPE_Update( graph ); /* Set lines to be 2 pixels thick */ MPE_Line_thickness(graph, 2); /* Both processes draw a line, each with different color */ MPE_Draw_line(graph, 300+(20*me), 10+(20*me), 800+(20*me), 70+(20*me), 30+(20*me)); MPE_Update( graph ); /* Both processes draw a circle of different size and color */ MPE_Draw_circle(graph, 100+(50*me), 300+(50*me), 20+(5*me), num_colors-(20*me)); MPE_Update(graph); /* Both processes draw 50 pixels with different colors */ for (i=0; i<50; i++) { int rx, ry; rx = 200+rand()%15; /* Random positions where to draw points */ ry = 300+rand()%15+40*me; MPE_Draw_point(graph, rx, ry, num_colors-(20*me)); } MPE_Update(graph); /* Build 80 pixels with randomly chosen colors in the array points */ for (i=0; i<80; i++) { points[i].x = 250+rand()%15; /* Set x coordinate */ points[i].y = 300+rand()%15+40*me; /* Set y coordinate */ points[i].c = rand()%num_colors; /* Set a color */ } MPE_Draw_points(graph, points, 80); /* Draw all 80 points */ MPE_Update(graph); /* Draw a text string */ MPE_Draw_string(graph, 400, 250, MPE_BLACK, "Hello from MPE_Draw_string"); MPE_Update(graph); /* Wait until the user selects a region of the graph, using button 1 */ /* Print out the coordinates of the selected region and draw a black */ /* rectangle around the selected region */ button=1; /* Use button 1 */ if (me==0) { printf("Select a region of the graph using mouse button %d\n", button); MPE_Get_drag_region( graph, button, MPE_DRAG_RECT, &x1, &y1, &x2, &y2 ); printf("Selected region is (%d,%d) to (%d,%d)\n", x1, y1, x2, y2); /* Draw a black rectangle around the selected area */ /* This should of course be implemented in a procedure MPE_Draw_rectangle */ /* Maybe I will write one some day ... */ MPE_Draw_line(graph, x1, y1, x1, y2, MPE_BLACK); MPE_Draw_line(graph, x1, y2, x2, y2, MPE_BLACK); MPE_Draw_line(graph, x2, y2, x2, y1, MPE_BLACK); MPE_Draw_line(graph, x2, y1, x1, y1, MPE_BLACK); MPE_Update(graph); } /* Wait until the user presses a mouse button */ if (me == 0) { printf("Press a mouse button to terminate\n"); MPE_Get_mouse_press(graph, &x, &y, &button); printf("User clicked button %d in position (%d,%d)\n", button, x, y); } /* We can also poll the mouse buttons with MPE_Iget_mouse_pressed */ /* pressed = 0; */ /* if (me == 0) { */ /* do { */ /* MPE_Iget_mouse_press(graph, &x, &y, &button, &pressed); */ /* } while (!pressed); */ /* printf("User clicked button %d in position (%d,%d)\n", button, x, y); */ /* } */ /* Wait here for terminationn signal from process 0 */ /* MPI_Barrier(MPI_COMM_WORLD); */ /* Close the graphics window and terminate */ MPE_Close_graphics(&graph); MPI_Finalize(); exit(0); }