⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mandelbrotmaster.c

📁 pvm下实现的mandelbrot程序源码
💻 C
字号:
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xos.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <time.h>
#include <sys/types.h>
#include <fcntl.h>
#include "pvm3.h"

#define ENCODING  PvmDataDefault
#define SLAVENAME "MandelbrotSlave"

#define		X_RESN	800       /* x resolution */
#define		Y_RESN	800       /* y resolution */
#define 	LOOP_STEP	100

void main ()
{
	/* window variables */
	Window		win;                            /* initialization for a window */
	unsigned
	int             width, height,                  /* window size */
                        x, y,                           /* window position */
                        border_width,                   /*border width in pixels */
                        display_width, display_height,  /* size of screen */
                        screen;                         /* which screen */

	char            *window_name = "Mandelbrot Set", *display_name = NULL;
	GC              gc;
	unsigned
	long		valuemask = 0;
	XGCValues	values;
	Display		*display;
	XSizeHints	size_hints;
	Pixmap		bitmap;
	XPoint		points[800];
	FILE		*fp, *fopen ();
	char		str[100];	
	XSetWindowAttributes attr[1];

       /* Mandlebrot variables */
        int i, j, k,row=0;
        XColor color1;
	int mytid;			/* parent task id */
	int *stid;			/* slave task id */
	int nhost;
	int narch;
	struct pvmhostinfo *hostp[10];

	/* init the pvm environments */
	if ((mytid = pvm_mytid()) < 0) {
		exit(1);
	}
    	//get the state of the pvm
	if (pvm_config(&nhost,&narch,hostp) < 0) {
		exit(1);
	}
	int numSlave=0;
	numSlave=nhost;
	if(numSlave<=0)numSlave=1;
	stid=(int *)malloc(sizeof(int)*numSlave);
	
	/* start up slave task */
    	for(i=0;i<numSlave;i++)
        	if (pvm_spawn(SLAVENAME, (char**)0, 0, "", 1, stid+i) < 0 || stid[i] < 0)
           	{
        		fputs("can't initiate slave\n", stderr);
    			for(i=0;i<numSlave;i++)
    			if (stid[i] > 0)
           			pvm_kill(stid[i]);                
     				pvm_exit();
     				exit(1);
	        }
	/* Wait for slave task to start up */
   	 pvm_setopt(PvmRoute, PvmRouteDirect);
	/* Initial placement */
    	for(i=0;i<numSlave;i++)
    	{
     		pvm_initsend(ENCODING);
     		pvm_pkint(&row, 1, 1);
     		if (pvm_send(stid[i],999))	/*Send task to worker*/
	 	{
	  		printf("can't send to \"%s\"\n", SLAVENAME);
	  		for(i=0;i<numSlave;i++)
    				if (stid[i] > 0)
           			pvm_kill(stid[i]);                
     				pvm_exit();
     				exit(1);
	 	}
		row=row+50;
		if(row>=X_RESN)break;
         }
       
	/* connect to Xserver */

	if (  (display = XOpenDisplay (display_name)) == NULL ) {
	   fprintf (stderr, "drawon: cannot connect to X server %s\n",
				XDisplayName (display_name) );
	 for(i=0;i<numSlave;i++)
    		if (stid[i] > 0)
           	pvm_kill(stid[i]);                
     		pvm_exit();
     		exit(-1);
	}
	
	/* get screen size */

	screen = DefaultScreen (display);
	display_width = DisplayWidth (display, screen);
	display_height = DisplayHeight (display, screen);

	/* set window size */

	width = X_RESN;
	height = Y_RESN;

	/* set window position */

	x = 0;
	y = 0;

        /* create opaque window */

	border_width = 4;
	win = XCreateSimpleWindow (display, RootWindow (display, screen),
				x, y, width, height, border_width, 
				BlackPixel (display, screen), WhitePixel (display, screen));

	size_hints.flags = USPosition|USSize;
	size_hints.x = x;
	size_hints.y = y;
	size_hints.width = width;
	size_hints.height = height;
	size_hints.min_width = 300;
	size_hints.min_height = 300;
	
	XSetNormalHints (display, win, &size_hints);
	XStoreName(display, win, window_name);

        /* create graphics context */

	gc = XCreateGC (display, win, valuemask, &values);

	XSetBackground (display, gc, WhitePixel (display, screen));
	XSetForeground (display, gc, BlackPixel (display, screen));
	XSetLineAttributes (display, gc, 1, LineSolid, CapRound, JoinRound);

	attr[0].backing_store = Always;
	attr[0].backing_planes = 1;
	attr[0].backing_pixel = BlackPixel(display, screen);

	/*XChangeWindowAttributes(display, win, CWBackingStore | CWBackingPlanes | CWBackingPixel, attr);*/
	
	XMapWindow (display, win);
	
      	Colormap my_colormap = DefaultColormap(display, DefaultScreen(display)); 
        /* receive and draw points */
	int *bytes,*msgtag,tid;
	for(i=0; i < X_RESN; i++) 
        	for(j=0; j < Y_RESN; j++) {
			/* receive point */
			pvm_recv(-1,888);
			pvm_upkint((int *)&x,1,1);
			pvm_upkint((int *)&y,1,1);
			pvm_upkint((int *)&k,1,1);
			/* receive request task frome slave */
			int bufid=pvm_nrecv(-1,999);
			if(bufid>0)
			{
			pvm_bufinfo(bufid,bytes,msgtag,&tid);
			if(row>=X_RESN)	/* all points had been computed */
			{
				int temp=-1;
				pvm_pkint(&temp, 1, 1);
				pvm_send(tid,999);
			}
			else		/* allocate a task to the slave dynamically*/
			{
				pvm_pkint(&row, 1, 1);
				pvm_send(tid,999);
				row=row+50;
			}
			}
				
			/* draw point */	
			color1.red=(255-k)*0x100;
			color1.green=(255-k*10)*0x100;
			color1.blue=(255-k*10)*0x100;
			Status rc = XAllocColor(display,my_colormap,&color1);
			XSetForeground(display,gc, color1.pixel);
			XDrawPoint (display, win, gc, y, x);
        }
	 
	XFlush (display);
	sleep (30);

     for(i=0;i<numSlave;i++)
    	if (stid[i] > 0)
           pvm_kill(stid[i]);
                
     pvm_exit();
     exit(1);
	/* Program Finished */

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -