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

📄 mandelbrot.c

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


#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xos.h>
#include <stdio.h>
#include <string.h>
#include <math.h>

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

typedef struct complextype
	{
        float real, imag;
	} Compl;


int main (int argc,char *argv[])
{
	struct timeval start_Time , end_Time ;	
	struct timeval display_StartTime , display_EndTime ;	
	struct timeval calculate_StartTime , calculate_EndTime ;	
	int display_cost , calculate_cost ;
	int kBuf[X_RESN] ;		
	int calMax = 20000 ;
  
  int i, j, k , lk =0 ;
  Compl	z, c;
  float	lengthsq, temp;	
	
		 
  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 = 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);
	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) ;
     

	display_cost = 0 ;
	calculate_cost = 0 ;

        /* Calculate and draw points */
	for(j=0 ; j < Y_RESN/2; j++)
	{
		gettimeofday(&calculate_StartTime , NULL) ;
		//计算一行:
        for(i=0 ; i < X_RESN; i++) 
        {
			z.real = z.imag = 0.0;
			c.real = ((float) i - 400.0)/200;               /* scale factors for 800 x 800 window */
			c.imag = ((float) j - 400.0)/200;
			k = 0;
			do
			{                                             /* iterate for pixel color */
				temp = z.real*z.real - z.imag*z.imag + c.real;
				z.imag = 2.0*z.real*z.imag + c.imag;
				z.real = temp;
				lengthsq = z.real*z.real+z.imag*z.imag;
				k++;

			}
			while (lengthsq < 4.0 && k < calMax);
			kBuf[i] = k ;
        }
		gettimeofday(&calculate_EndTime , NULL) ;
		calculate_cost += calculate_EndTime.tv_sec*1000000 + calculate_EndTime.tv_usec
			-calculate_StartTime.tv_sec*1000000 - calculate_StartTime.tv_usec ;
		
		gettimeofday(&display_StartTime , NULL) ;
		//绘制一行:
		for(i=0 ; i < X_RESN; i++) 
		{
			color.red = (unsigned int)(9000*kBuf[i]+20000);
			color.green = (unsigned int)(7000*kBuf[i]);
			color.blue = (unsigned int)(5000*kBuf[i]);
			if(lk != kBuf[i])
			{
				lk = kBuf[i] ;
				if (XAllocColor (display,cmap, &color))
					XSetForeground (display, gc, color.pixel);
			}
			XDrawPoint (display, win, gc, i, j);
			XDrawPoint (display, win, gc, i , 799-j);
		}
		gettimeofday(&display_EndTime , NULL) ;
		display_cost += display_EndTime.tv_sec*1000000 + display_EndTime.tv_usec
			-display_StartTime.tv_sec*1000000 - display_StartTime.tv_usec ;
	}

	XFlush (display);
	
	//取得结束时间
	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) ;
	printf("Display cost %fs\n",display_cost/1000000.0) ;
	printf("Calculate cost %fs\n",calculate_cost/1000000.0) ;
	
	sleep (30);
	return 1 ;

	/* Program Finished */

}

⌨️ 快捷键说明

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