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

📄 mandelbrot_master.c

📁 PVM下mandel_brot集并行化代码
💻 C
字号:
/** * The mandelbrot set PVM source code. * Author:	Amal Cao * Since:	Dec. 18th 2008 *  GPL v2.0 */#include <stdlib.h>#include <stdio.h>#include <string.h>#include <time.h>#include <fcntl.h>#include <math.h>#include <sys/types.h>#include <X11/Xlib.h>#include <X11/Xutil.h>#include <X11/Xos.h>#include "pvm3.h"#define SLAVENAME "mandelbrot_slave"#define ENCODING  PvmDataDefault#define		X_RESN	800       /* x resolution */#define		Y_RESN	800       /* y resolution */#define 		DELAY	10#define		HOLDSCREEN   1	  /* Time to show the final image on screen                                     before terminate */                                                                      #define  H_MAX    360   /* HSB color space constant */#define  S_MAX    256#define  L_MAX    256#define  RGBMAX   255   /* R,G, and B vary over 0->RGBMAX */unsigned long HSLtoRGB(int H,int S,int L){  double fH = (double)(H) / H_MAX;  double fS = (double)(S) / S_MAX;  double fL = (double)(L) / L_MAX;  double fR,fG,fB;  double temp1,temp2;  double Rtemp3,Gtemp3,Btemp3;  int R,G,B;  if (fS == 0.0) {    fR = fB = fG = fL;  } else {    if (fL < 0.5) temp2 = fL * (1.0 + fS);    else temp2 = fL + fS - fL * fS;    temp1 = 2.0 * fL - temp2;        Rtemp3 = fH + 1.0/3.0;    Gtemp3 = fH;    Btemp3 = fH - 1.0/3.0;    if (Rtemp3 > 1.0) Rtemp3 -= 1.0; else if (Rtemp3 < 0.0) Rtemp3 += 1.0;    if (Gtemp3 > 1.0) Gtemp3 -= 1.0; else if (Gtemp3 < 0.0) Gtemp3 += 1.0;    if (Btemp3 > 1.0) Btemp3 -= 1.0; else if (Btemp3 < 0.0) Btemp3 += 1.0;        if (6.0 * Rtemp3 < 1.0) fR = temp1+(temp2-temp1)*6.0*Rtemp3;    else if (2.0 * Rtemp3 < 1.0) fR = temp2;    else if (3.0 * Rtemp3 < 2.0) fR = temp1+(temp2-temp1)*((2.0/3.0)-Rtemp3)*6.0;    else fR = temp1;      if (6.0 * Gtemp3 < 1.0) fG = temp1+(temp2-temp1)*6.0*Gtemp3;    else if (2.0 * Gtemp3 < 1.0) fG = temp2;    else if (3.0 * Gtemp3 < 2.0) fG = temp1+(temp2-temp1)*((2.0/3.0)-Gtemp3)*6.0;    else fG = temp1;      if (6.0 * Btemp3 < 1.0) fB = temp1+(temp2-temp1)*6.0*Btemp3;    else if (2.0 * Btemp3 < 1.0) fB = temp2;    else if (3.0 * Btemp3 < 2.0) fB = temp1+(temp2-temp1)*((2.0/3.0)-Btemp3)*6.0;    else fB = temp1;  }    R = (int)(fR * (double)(RGBMAX));  G = (int)(fG * (double)(RGBMAX));  B = (int)(fB * (double)(RGBMAX));    R = R & 0xFF;  G = G & 0xFF;  B = B & 0xFF;  return (unsigned long)( (R << 16) + (G << 8) + B);}int main(int argc, char** argv) {	//First step: to initialize the X11 window.		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];		int i, j, k, cc, ret;       /* precompute color value*/         	unsigned long colorCache[H_MAX];	for (k = 0;k < H_MAX;k++) {		double d,l;		d = ((1.0 * k) / 359.0);		d = (1.0 - d * d);		l = (int)(d * 128.0);		colorCache[k] = HSLtoRGB(k,204,l);	 }	       	/* 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));	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);	XSync(display, 0);			//Now to initialize the PVM environment		double time_start, time_end;	struct timeval tv;		int mytid;			/* parent task id */	int *stid;			/* slave task id */	int nhost;	int narch;	struct pvmhostinfo *hostp[10];		gettimeofday(&tv, NULL);	time_start = (double)tv.tv_sec +        	(double)tv.tv_usec /1000000.0;		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;		int interval=numSlave;		int loop_step = DELAY;	if (argc == 2) {		loop_step = atoi(argv[1]);		printf("loop step init!\n" );	}			int result[X_RESN][Y_RESN];	stid=(int *)malloc(numSlave*sizeof(int));		//printf("The slave number is : %d\n", 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);			goto bail;		}	}	 /* Wait for slave task to start up */    pvm_setopt(PvmRoute, PvmRouteDirect);    	    for(i=0;i<numSlave;i++)    {				 if(cc=pvm_recv(stid[i], 0) < 0)		 	goto bail;		 else		 		 	printf("slave %d is task t%x",i,stid[i]);				 pvm_initsend(ENCODING);		 pvm_pkint(&i, 1, 1);		 pvm_pkint(&interval,1,1);		 pvm_pkint(&loop_step,1,1);		 if (pvm_send(stid[i], stid[i]))		 {			  printf("can't send to \"%s\"\n", SLAVENAME);			  goto bail;		 }		 else 		 	printf("\tok,send parameter to task %d\n",i);		// printf("%c",0x07);    }     for(i=0;i<numSlave;i++)	{		if(cc=pvm_recv(stid[i], stid[i]) < 0)			goto bail;		else {			int step = X_RESN*Y_RESN/numSlave;			pvm_upkint(&result[0][0]+i*step,step,1);		}		//printf("receive ok %d\n", i);	}        //add some code...    	for(i=0; i < X_RESN; i++) {        for(j=0; j < Y_RESN; j++) {        	XSetForeground (display, gc, colorCache[result[i][j]]);        	XDrawPoint (display, win, gc, j, i);		}	}	    gettimeofday(&tv, NULL);	time_end = (double)tv.tv_sec +        	(double)tv.tv_usec /1000000.0;        	    printf("The computing time is: %f \n", time_end-time_start);    XFlush (display);	sleep (3);	goto ok;bail:	ret = 1;	free(stid);ok:	for(i=0;i<numSlave;i++)		if (stid[i] > 0)			pvm_kill(stid[i]);                	pvm_exit();	exit(ret);}

⌨️ 快捷键说明

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