Message Passing Extensions Graphics


/*
 *Joel Adams, Calvin College 2013.
 *  Master Task draws a bulls-eye, with worker Tasks drawing concentric rings
 * Modified Jerrold Siegel Fall 2016 - As written, there is a bug. Worker tasks are written as infinite loops.
 */
#include <mpi.h>
#include <mpe.h>
#include <stdlib.h>  /* getenv() */
#include <string.h>  /* strcmp() */
#include <stdio.h>   /* printf(), etc. */

char *mystring="hello";
char* getDisplay() {
    char * display = getenv("DISPLAY");
    if ( strncmp(display, "(null)", 7) == 0 ) {
        fprintf(stderr, "\n*** Fatal: DISPLAY variable not set.\n");
        exit(1);
    }
    return display;
}
int main(int argc, char* argv[]) {
    const int  WINDOW_WIDTH = 800;
    const int  WINDOW_HEIGHT = 800;
    const int  CIRCLE_RADIUS = 5;
    const int  MOUSE_RIGHT_BUTTON = 3;
    int        x = 0, y = 0,height=100;
    int        rank = 0, button = 0,i;
    char buffer[2],msg[20];
    MPE_XGraph canvas;
    MPI_Init(&argc,&argv);
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);
    if ( rank == 0 ) {
        printf("Opening %dx%d window.\n", WINDOW_WIDTH, WINDOW_HEIGHT);
    }
    MPE_Open_graphics( &canvas, MPI_COMM_WORLD,
                       getDisplay(),
                       -1, -1,
                       WINDOW_WIDTH, WINDOW_HEIGHT, 0 );

    MPE_Line_thickness( canvas, 4 );
    if ( rank == 0 ) {
        MPE_Fill_circle( canvas,
                         WINDOW_WIDTH / 2,
                         WINDOW_HEIGHT / 2,
                         8,
                         MPE_BLACK);
        /* int MPE_Draw_string( graph, x, y, color, string )*/
        MPE_Draw_string( canvas,
                         WINDOW_WIDTH / 2,
                         WINDOW_HEIGHT / 2, MPE_BLACK, " Task 0" );

    } else {
        snprintf(buffer, 2, "%d", rank);
        MPE_Fill_circle( canvas,
                         WINDOW_WIDTH / 2+(rank * 55),
                         (WINDOW_HEIGHT /2 ),
                         8,
                         MPE_RED);
        MPE_Draw_circle( canvas,
                         WINDOW_WIDTH / 2, WINDOW_HEIGHT / 2,
                         (rank * 55),
                         MPE_RED);
        MPE_Draw_string( canvas,
                         ((WINDOW_WIDTH/2)+rank*45),
                         ((WINDOW_WIDTH/2)+rank*45), MPE_BLACK, buffer);
        MPE_Update(canvas);
    }
    /*Draw blue circles wherever the user left-clicks  */
    if ( rank == 0 ) {
        printf("Click:\n- left mouse button to draw blue circles;\n");
        printf("- right mouse button to quit.\n");
        do {
            MPE_Get_mouse_press( canvas, &x, &y, &button );
            if (button == MOUSE_RIGHT_BUTTON) {
                MPE_Close_graphics( &canvas);
                MPI_Finalize();
                if ( rank == 0 ) {
                    printf("Program complete.\n\n");
                }
                return 0;
            }
            for (i=0;i<5;i++) {
                printf("%i %i  %i\n",x,(430+i*45), abs(x-(430+i*45)));
                if (abs(x-(430+i*45))<25) {
                    //printf("%i\n",i);
                    snprintf(buffer, 2, "%d", i);
                    MPI_Send(buffer, 2, MPI_INT, i, i, MPI_COMM_WORLD);
                    MPI_Recv(msg,20, MPI_CHAR, i, i, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
                    MPE_Draw_string( canvas,100, height, MPE_BLACK, msg );
                    height=height+12;
                }
            }
            MPE_Fill_circle( canvas, x, y, CIRCLE_RADIUS, MPE_BLUE );
        } while (button != MOUSE_RIGHT_BUTTON);
    } else {
        snprintf(msg,20,"This is rank %d", rank);
        while (1) {
            MPI_Recv(buffer,2, MPI_INT, 0, rank, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
            MPI_Send(msg, 20, MPI_CHAR, 0, rank, MPI_COMM_WORLD);  
        }
    }
    /* clean up */
    MPE_Close_graphics( &canvas);
    MPI_Finalize();
    if ( rank == 0 ) {
        printf("Program complete.\n\n");
    }
    return 0;
}







makempe.sh

mpicc -DMPE_GRAPHICS -Wall $1.c -o $1 -L/usr/X11R6/lib -lmpe -lX11 -lm

Console Output

[siegelj@tc-login mpe]$ mpirun -n 5 mpeGraphics
Opening 800x800 window.
Click:
- left mouse button to draw blue circles;
- right mouse button to quit.
625 430  195
625 475  150
625 520  105
625 565  60
625 610  15
566 430  136
566 475  91
566 520  46
566 565  1
566 610  44
514 430  84
514 475  39
514 520  6
514 565  51
514 610  96