Exemplo n.º 1
0
void simulate(const int tmax, int rank, int size, const int gran, body *bodies){
	int t=tmax, i, round;
	int sendto = (rank + 1) % size;
	int recvfrom = ((rank + size) - 1) % size;
	
	MPI_Datatype bodytype;
	MPI_Type_contiguous(3, MPI_DOUBLE, &bodytype);
	MPI_Type_commit(&bodytype);
	MPI_Status status;

	MPE_XGraph bodyWin;	
	int winSize = 800;
	
	body *inbuf = (body *) malloc(gran*sizeof(body));
	body *outbuf = (body *) malloc(gran*sizeof(body));
	fvp *f = (fvp *) malloc(gran*sizeof(fvp));
	fvp *v = (fvp *) malloc(gran*sizeof(fvp));
	for(i=0; i < gran; i++){
		v[i].x=0;
		v[i].y=0;
	}

	MPE_Open_graphics(&bodyWin,MPI_COMM_WORLD,NULL,-1,-1,winSize,winSize,0);

	while(1){
		--t;
		round=size;
		memcpy(outbuf, bodies, gran*sizeof(body));		
		frcInit(gran, bodies, f);
		while (round > 1) {
			--round;
			if (!(rank % 2)){
				MPI_Send(outbuf, gran, bodytype, sendto, 0, MPI_COMM_WORLD);
				MPI_Recv(inbuf, gran, bodytype, recvfrom, 0, MPI_COMM_WORLD, &status);						
			}
			else
			{
				MPI_Recv(inbuf, gran, bodytype, recvfrom, 0, MPI_COMM_WORLD, &status);
				MPI_Send(outbuf, gran, bodytype, sendto, 0, MPI_COMM_WORLD);
			}
			memcpy(outbuf, inbuf, gran*sizeof(body));
			frcUpdt(gran, bodies, inbuf, f);
		}
		posUpdt(gran, bodies, f, v);
		for(i=0; i<gran; i++)
			MPE_Draw_circle(bodyWin,bodies[i].x,bodies[i].y,2,(rank*gran+i)%15+1);
		MPE_Update(bodyWin);
	  	usleep(300000);
		if(t > 0) 
			for(i=0;i<gran;i++) 
				MPE_Draw_circle(bodyWin,bodies[i].x,bodies[i].y,2,MPE_WHITE);
		if(t==0) break;
	}
	MPE_Close_graphics(&bodyWin);
	free(inbuf);
	free(outbuf);
	free(f);
	free(v);
}
Exemplo n.º 2
0
int main( int argc, char** argv )
{
    MPE_XGraph graph;
    int ierr, mp_size, my_rank;
	MPE_Color my_color;
    char ckey;
    /*
    char displayname[MPI_MAX_PROCESSOR_NAME+4] = "";
    */

    MPI_Init( &argc, &argv );
    MPI_Comm_size( MPI_COMM_WORLD, &mp_size );
    MPI_Comm_rank( MPI_COMM_WORLD, &my_rank );

    /*
    if ( my_rank == 0 )
        strcpy( displayname, getenv( "DISPLAY" ) );

    MPI_Bcast( displayname, MPI_MAX_PROCESSOR_NAME+4, MPI_CHAR, 
               0, MPI_COMM_WORLD );
    fprintf( stdout, "%d : $DISPLAY at process 0 = %s\n",
             my_rank, displayname );
    fflush( stdout );

    ierr = MPE_Open_graphics( &graph, MPI_COMM_WORLD, displayname,
                              -1, -1, 400, 400, 0 );
    */

    ierr = MPE_Open_graphics( &graph, MPI_COMM_WORLD, NULL,
                              -1, -1, 400, 400, 0 );
    if ( ierr != MPE_SUCCESS ) {
        fprintf( stderr, "%d : MPE_Open_graphics() fails\n", my_rank );
        ierr = MPI_Abort( MPI_COMM_WORLD, 1 );
        exit(1);
    }
    my_color = (MPE_Color) (my_rank + 1);
    if ( my_rank == 0 )
        ierr = MPE_Draw_string( graph, 187, 205, MPE_BLUE, "Hello" );
    ierr = MPE_Draw_circle( graph, 200, 200, 20+my_rank*5, my_color );
    ierr = MPE_Update( graph );

    if ( my_rank == 0 ) {
        fprintf( stdout, "Hit any key then return to continue  " );
        fscanf( stdin, "%s", &ckey );
        fprintf( stdout, "\n" );
    }
    MPI_Barrier( MPI_COMM_WORLD );

    ierr = MPE_Close_graphics( &graph );

    MPI_Finalize();
    
    return 0;
}
Exemplo n.º 3
0
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);
}