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

📄 mandelbrot_master.c

📁 1、PVM&XPVM并行环境的配置与测试。 2、mandelbrot程序的并行化实现
💻 C
字号:
/* Mandlebrot program master */


#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xos.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <sys/types.h>
#include <sys/time.h> 
#include <fcntl.h>
#include <math.h>
#include "pvm3.h"

#define		X_RESN	800       /* x resolution */
#define		Y_RESN	800       /* y resolution */
#define 	LOOP_STEP	100
#define SLAVENAME "mandelbrot_slave"
#define ENCODING  PvmDataDefault

int main (int argc,char *argv[])
{
	int i ,j , lk = 0 ;
	int slaveID , row , recvBuf[800] ;
	struct timeval start_Time , end_Time ;	
	int calMax = 20000 ;      /*最大迭代次数*/
				
	/* pvm ********************************************************/
	int mytid;			/* parent task id */
	int *stid;			/* slave task id */

	int nhost;
	int narch;
	struct pvmhostinfo *hostp[10];	
	/**************************************************************/

	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];
	XColor 			color;
	Colormap 		cmap;

	
	XSetWindowAttributes attr[1];
       
	/* connect to Xserver */

	if (  (display = XOpenDisplay (display_name)) == NULL ) {
	   fprintf (stderr, "drawon: cannot connect to X server %s\n",
				XDisplayName (display_name) );
	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));

	cmap=DefaultColormap(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 = 200;
	size_hints.min_height = 200;
	
	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);
	XSync(display, 0);
	sleep (1);

	if(argc == 2)
	{
		if( atoi(argv[1])>0 )
		{
			calMax = atoi(argv[1]) ;		
		}
	}
	printf("Max iterative times %d\n", calMax) ;

	
	gettimeofday(&start_Time , NULL) ;
	/* pvm ********************************************************/
	if ((mytid = pvm_mytid()) < 0) 
	{
		exit(1);
	}
	printf("I'm t%x\n", mytid);

    //get the state of the pvm
	if (pvm_config(&nhost,&narch,hostp) < 0) {
		exit(1);
	}
	printf("there are %d machines!\n", nhost);             /*本程序使用两个节点*/

	int numSlave=0;	
	numSlave=nhost;	
	if(numSlave<=0)
		numSlave=1;
	int interval=numSlave;
	stid=(int*)malloc(numSlave*sizeof(int));
	
	/* start up slave task */
    for(i=0;i<numSlave;i++)
	{
        if (pvm_spawn(SLAVENAME, (char**)0, 0, "", 1, stid+i) < 0 || stid[i] < 0)
		{
			printf("Can't initiate slave %d\n", i);
			goto bail;
		}
	}						

    /* Wait for slave task to start up , send first task to each slave */
  pvm_setopt(PvmRoute, PvmRouteDirect);
	for(i=0;i<numSlave;i++)
    {
		pvm_recv(stid[i], 0 );
		printf("slave %d is task t%x\n",i,stid[i]);

		pvm_initsend(ENCODING);
		pvm_pkint(&calMax, 1, 1);
		pvm_pkint(&i, 1, 1);
		pvm_send(stid[i], stid[i]);
		printf("Send to slave %d task %x\n",i,stid[i]) ;
	}
	/**************************************************************/
    
	
	for(j=numSlave ; j <Y_RESN/2+numSlave; j++) 
	{
		//receive:
		if( pvm_recv(-1 , 2)<0 )
			return 0 ;
		
		pvm_upkint(&slaveID, 1, 1) ;
		pvm_upkint(&row, 1, 1) ;
		pvm_upkint(recvBuf, 800, 1) ;

		/*如果还有没有计算的行,则继续向刚刚送回结果的slave进程发送接下来的行*/
		pvm_initsend(ENCODING);
		pvm_pkint(&calMax, 1, 1);
		pvm_pkint(&j, 1, 1);
		pvm_send(slaveID, slaveID);
		

                /*根据迭代次数决定颜色并绘制象素点*/
		for(i=0 ; i<800 ; i++)
		{
                                              
			
                        if( lk != recvBuf[i])
			{
                        color.red = (unsigned int)(9000*recvBuf[i]+20000);
			color.green = (unsigned int)(7000*recvBuf[i]);
			color.blue = (unsigned int)(5000*recvBuf[i]);
				lk = recvBuf[i] ;
				if (XAllocColor (display,cmap, &color))
				XSetForeground (display, gc, color.pixel);
			}
			XDrawPoint(display, win, gc, i , row);
			XDrawPoint(display, win, gc, i , 799-row);
		}
	}
	 
	XFlush (display);
        /*杀掉进程*/

bail:
	for(i=0;i<numSlave;i++)
    	if (stid[i] > 0)
           pvm_kill(stid[i]);
                
     pvm_exit();

	gettimeofday(&end_Time , NULL) ;
	printf("\nTime cost : %fs\n", end_Time.tv_sec+end_Time.tv_usec/1000000.0
		-start_Time.tv_sec-start_Time.tv_usec/1000000.0) ;

	sleep (30);
	return 1 ;

	/* master Program Finished */

}

⌨️ 快捷键说明

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