/* w1mpi.c test X Windows parallel with MPI */ #include "mpi.h" #include #include #include #include #include #include static int myid; static Display *dpy; static Window window1; static GC gc; static Screen *screen; static void doInitialize(void); /* function prototypes used below */ static void doCreateWindows(void); static void doCreateGraphicsContext(void); static void doExpose(XEvent *eventP); static void doHandleEvents(void); int main(int argc, char *argv[]) { int proc, numprocs; int master = 0; double esec = 0.0; MPI_Init(&argc,&argv); MPI_Comm_size(MPI_COMM_WORLD,&numprocs); MPI_Comm_rank(MPI_COMM_WORLD,&myid); if(myid == 0) { printf("w1mpi.c numprocs=%d \n", numprocs); } /* all */ doInitialize(); doHandleEvents(); if(myid == 0) { esec = MPI_Wtime(); printf("w1mpi.c took %g seconds \n", esec); } MPI_Finalize(); return 0; } /* end main of w1mpi.c */ /***************** doInitialize **************************/ static void doInitialize(void) { char msg[100]; dpy = XOpenDisplay(0); if (!dpy){ printf("Display not opened!\n"); exit(1); } screen = XDefaultScreenOfDisplay(dpy); doCreateWindows(); doCreateGraphicsContext(); sprintf(msg, "Process %d window", myid); XStoreName(dpy, window1, msg); XMapWindow(dpy, window1); } /******* doCreateWindows *********/ static void doCreateWindows(void) { int window1X = 300; int window1Y = 200; int window1W = 250; int window1H = 100; XSetWindowAttributes xswa; /* Create the window1 window */ xswa.event_mask = ExposureMask | ButtonPressMask; xswa.background_pixel = XWhitePixelOfScreen(screen); window1 = XCreateWindow(dpy, XRootWindowOfScreen(screen), window1X, window1Y, window1W, window1H, 0, XDefaultDepthOfScreen(screen), InputOutput, XDefaultVisualOfScreen(screen), CWEventMask | CWBackPixel, &xswa); } /******** Create the graphics context *********/ static void doCreateGraphicsContext(void) { XGCValues xgcv; /* Create graphics context. */ xgcv.background = XWhitePixelOfScreen(screen); xgcv.foreground = XBlackPixelOfScreen(screen); gc = XCreateGC(dpy, window1, GCForeground | GCBackground, &xgcv); } /****************** doHandleEvents ***********************/ static void doHandleEvents(void) { XEvent event; for ( ; ; ) { XNextEvent(dpy, &event); switch (event.type) { case Expose: doExpose(&event); break; case ButtonPress: XDestroyWindow(dpy, window1); XFlush(dpy); return; /* shut down */ } } } /***** draw and write the message in the window *****/ static void doExpose(XEvent *eventP) { static char message[] = {"Click here to exit"}; /* If this is an expose event on our window1 window, then write the text and rectangle. */ if (eventP->xexpose.window != window1) return; XClearWindow(dpy, window1); XDrawImageString(dpy, window1, gc, 50, 25, message, strlen(message)); XDrawRectangle(dpy, window1, gc, 50, 50, 100, 25); XFlush(dpy); }