示例#1
0
int main(int argc, char **argv ) {
	char *filename = NULL;
	char temp[10];
	int file;
	int a;
	double modelang=0;
	long vx=0;
	long vy=0;
	long vz=0;
	
	// set camera position
	camera.x = 0;
	camera.y = 100;
	camera.z = 0;
	camera.ang = 3*PI/2;

	// load a voxel model
	if( argc>1 ) {
		for(a=1; a<argc; a++) {
			if( argv[a] != NULL ) {
				if( !strcmp(argv[a], "-fullscreen") ) {
					fullscreen = 1;
				}
				else if( !strncmp(argv[a], "-size=", 6) ) {
					strncpy(temp,argv[a]+6,strcspn(argv[a]+6,"x"));
					xres = max(320,atoi(temp));
					yres = max(200,atoi(argv[a]+6+strcspn(argv[a]+6,"x")+1));
				}
				else {
					filename = (char *) malloc(sizeof(char)*strlen(argv[a])+4);
					strcpy(filename,argv[a]);
					if( strstr(filename,".vox") == NULL )
						strcat(filename,".vox");
					if((file = open(filename,O_RDONLY))==-1) {
						printf("Failed to open specified file.");
						return 1;
					}
				}
			}
		}
	}
	if( filename == NULL ) {
		printf("Usage: voxrender FILE\n");
		printf("Ex: voxrender desklamp.vox\n");
		return 1;
	}
	read(file,&model.sizex,4);
	read(file,&model.sizey,4);
	read(file,&model.sizez,4);
	model.data = (unsigned char *) malloc(model.sizex*model.sizey*model.sizez);
	read(file,model.data,model.sizex*model.sizey*model.sizez);
	read(file,model.palette,768);
	close(file);
	
	// load the font
	font8_bmp = SDL_LoadBMP("8font.bmp");
	SDL_SetColorKey( font8_bmp, SDL_SRCCOLORKEY, SDL_MapRGB( font8_bmp->format, 255, 0, 255 ) );
	
	// initialize sdl
	if( SDL_Init( SDL_INIT_VIDEO | SDL_INIT_TIMER ) == -1 ) {
		printf("Could not initialize SDL. Aborting...\n\n");
		return 1;
	}
	if( fullscreen ) {
		screen = SDL_SetVideoMode( xres, yres, 32, SDL_HWSURFACE | SDL_FULLSCREEN );
		SDL_ShowCursor(0);
	}
	else
		screen = SDL_SetVideoMode( xres, yres, 32, SDL_HWSURFACE | SDL_RESIZABLE );
	SDL_WM_SetCaption( "VoxRender", 0 );
	zbuffer = (double *) malloc(xres*yres*sizeof(double));
	
	// main loop
	while(mainloop) {
		// receive input + move camera
		ReceiveInput();
		
		// rotate the model
		modelang += timesync*.0025;
		while( modelang > PI*2 )
			modelang -= PI*2;
		if(keystatus[SDLK_SPACE]) {
			vx = camera.x;
			vy = camera.y;
			vz = camera.z;
			modelang = camera.ang;
		}
		
		// rendering
		memset( zbuffer, 0, xres*yres*sizeof(double) );
		SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format,50,50,150)); // wipe screen
		DrawVoxel(&model,vx,vy,vz,modelang,modelang,modelang);
		
		// print some debug info
		PrintText(font8_bmp,8,yres-16,"FPS:%6.1f", fps);
		PrintText(font8_bmp,8,8,"angle: %d", (long)(camera.ang*180.0/PI));
		PrintText(font8_bmp,8,16,"x: %d", (long)camera.x);
		PrintText(font8_bmp,8,24,"y: %d", (long)camera.y);
		PrintText(font8_bmp,8,32,"z: %d", (long)camera.z);
		
		SDL_Flip( screen );
		cycles++;
	}
	
	// deinit
	free(model.data);
	free(zbuffer);
	free(filename);
	SDL_FreeSurface(font8_bmp);
	SDL_Quit();
	return 0;
}
int main(int argc, char* argv[]) 
{
//
	//MPI_Comm    comm;
	MPI_Init(&argc, &argv);
	MPI_Comm_size(MPI_COMM_WORLD, &size);
	MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
	MPI_Comm_dup(MPI_COMM_WORLD, &comm);

	int p,t,n; // processorts_count, thread_count, element_count
	int *input;
	int * inputList = (int *) malloc(n * sizeof(int));
	
	int * localList;
	// Time calculation - average of wtime() execution
	double startTime,endTime;
	double wtime_overhead = 0.0;
    for (int i = 0; i < 200; i++) {
        startTime = MPI_Wtime();
        endTime = MPI_Wtime();
        wtime_overhead = wtime_overhead + (startTime - endTime);
    }
    wtime_overhead = wtime_overhead/200.0;
	//
	//printf("i m started\n");
	
	if(my_rank == 0)
	{
		input = ReceiveInput(&t,&n);
		inputList = input;
		p = size;		//uncomment then
		printf("Value of p n t %d %d %d \n",p,n,t);
		
		//Start timer..
		startTime = MPI_Wtime();
		
		//Here we received all inputList array, processors and threadCount.
		/*for(int i=0;i<n;i++)
		{
			printf("%d ",*(inputList+i));
		}*/
		for(int i=1;i<p;i++)
		{
			MPI_Send(&n,1,MPI_INT,i,2,comm);
		}
		localList = (int *) malloc((n/p) * sizeof(int));
		for(int i=0;i<n/p;i++)
		{
			*(localList + i) = *(inputList + i);
		}
		
		distribute(inputList,p,t,n);
	}
	else
	{
		//receive n
		MPI_Recv(&n, 1, MPI_INT, 0, 2, comm,&status);
		p=size;
		
		localList = (int *) malloc((n/p) * sizeof(int));
		//recieve localList for all slave nodes.
		MPI_Recv(localList, (n/p), MPI_INT, 0, 1, comm,&status);
	}
	//Display received local copies..
	sort(localList,n/p);
	//MPI_Barrier(comm);
	//printf("\nlocalcopy sorted at %d \n",my_rank);
	//for(int i=0;i<(n/p);i++)
	//{
	//		printf("rank %d %d ",my_rank,*(localList+i));
	//}
	//printf("\n");
	//Find spliters at each node.
	bool isPsplitter = true;;
	int * splitterArray = (int *) malloc(p*sizeof(int));
	splitterArray = localSplitter(localList,(n/p),p,isPsplitter);
	//MPI_Barrier(comm);
	//printf("\nSplitters for %d are : \n",my_rank);
	//for(int i=0;i<p;i++)
	//{
	//		printf("rank %d %d ",my_rank,*(splitterArray+i));
	//}
	//All nodes should send splitterArray to root 0 for merging.
	int * sampleArray = (int *) malloc(p*p*sizeof(int));
MPI_Barrier(comm);	
	MPI_Allgather(splitterArray,p,MPI_INT,sampleArray,p,MPI_INT,comm);
MPI_Barrier(comm);
	//printf("\nSampleArray at %d :\n",my_rank);
	//for(int i=0;i<(p*p);i++)
	//{
	//	printf("%d ",*(sampleArray+i));
	//}
	//printf("\n");
	//Sort sample array.
	sort(sampleArray,(p*p));
	//Find final (p-1) splitter array for entire range of numbers.
	isPsplitter = false;
	int * FinalsplitterArray = (int *) malloc((p-1)*sizeof(int));
	FinalsplitterArray = localSplitter(sampleArray,(p*p),p,isPsplitter);
	//MPI_Barrier(comm);
	//printf("FinalsplitterArray at %d :\n",my_rank);
	//for(int i=0;i<(p-1);i++)
	//{
	//	printf("%d ",*(FinalsplitterArray+i));
	//}
	//printf("\n");
	//
	//**********************************************************
	//This will prepare scounts, sdispls, rcounts, rdispls for each process..
	int * scounts = (int *) malloc(p*sizeof(int));
	int * sdispls = (int *) malloc(p*sizeof(int));
	//Initialization
	for(int i=0;i<p;i++)
	{
		*(scounts + i) = 0;
		*(sdispls + i) = 0;
	}
	
	allToAllCalculation(localList,FinalsplitterArray,scounts,sdispls,n/p);
	//creating rcounts & rdispls with max n length
	int * rcounts = (int *)malloc(p*sizeof(int));
	int * rdispls = (int *)malloc(p*sizeof(int));
	//Initialization
	for(int i=0;i<p;i++)
	{
		*(rcounts + i) = 0;
		*(rdispls + i) = 0;
	}
	//This will move scount from all to all processed and each process will 
	// get rcounts updated.
MPI_Barrier(comm);	
	MPI_Alltoall(scounts, 1, MPI_INT, rcounts, 1, MPI_INT, comm);
MPI_Barrier(comm);
	//printf("\n");
	//for(int i=0;i<p;i++)
	//{
	//	printf("rcounts rank %d :  %d",my_rank,rcounts[i]);
	//}
	//printf("\n");
	//Generate rdispl list.
	rdispls[0] = 0;
	for(int i=1;i<p;i++)
	{
		//if(rcounts[i] != 0)
		{
			rdispls[i] = rdispls[i-1] + rcounts[i-1];
		}	
	}
	//for(int i=0;i<p;i++)
	//{
	//	printf("rdisp rank %d : %d ",my_rank,rdispls[i]);
	//}
	//printf("\n");
	//***********************************************************************
	//This will do MPI_Alltoallv communication with vectors
	int bucketSize = 0;
	for(int i=0;i<p;i++)
	{
		bucketSize += rcounts[i];
	}
	//printf("bucketSize rank %d : %d \n",my_rank,bucketSize);
	int * localBucket = (int *)malloc(bucketSize*sizeof(int));
MPI_Barrier(comm);	
	MPI_Alltoallv(localList,scounts,sdispls,MPI_INT,localBucket,rcounts,rdispls,MPI_INT,comm);
MPI_Barrier(comm);
	//printf("\n");
	//for(int i=0;i<bucketSize;i++)
	//{
	//	printf("bucket @ rank %d :  %d , ",my_rank,localBucket[i]);
	//}
	//printf("\n");
	//Sort bucket on every node. (Merge algorithm is used)
	int * sortedBucket = (int *) malloc(bucketSize * sizeof(int));
	merge(localBucket,rdispls,bucketSize,p,sortedBucket);
	//for(int i=0;i<bucketSize;i++)
	//{
	//	printf("sortedBucket %d: %d ",my_rank,sortedBucket[i]);
	//}
	//printf("\n");
	//***********************************************************************
	//Final result formation
	//send sortedBucket to root node 0.
	int * finalRcvCounts = (int *) malloc(p * sizeof(int));
	int * finalRcvDispls = (int *) malloc(p * sizeof(int));
	int * finalResults   = (int *) malloc(n * sizeof(int));
	//Gather counts of every process bucket.
MPI_Barrier(comm);	
	MPI_Gather(&bucketSize,1,MPI_INT,finalRcvCounts,1,MPI_INT,0,comm);
MPI_Barrier(comm);	
	
	if(my_rank == 0)
	{
		/*for(int i=0;i<p;i++)
		{
			printf("finalRcvCounts %d ",finalRcvCounts[i]);
		}
		printf("\n");
		finalRcvDispls[0] = 0;
		for(int i=1;i<p;i++)
		{
			if(finalRcvDispls[i] != 0)
			{
				finalRcvDispls[i] = finalRcvDispls[i-1] + finalRcvCounts[i-1];
			}	
		}
		for(int i=0;i<p;i++)
		{
			printf("finalRcvDispls %d ",finalRcvDispls[i]);
		}
		printf("\n");*/
		prepareGatherv(finalRcvCounts,finalRcvDispls,p);
	}
	//receive all to one gather vectors at root node 0.
MPI_Barrier(comm);	
	MPI_Gatherv(sortedBucket,bucketSize,MPI_INT,finalResults,finalRcvCounts,finalRcvDispls,MPI_INT,0,comm);
MPI_Barrier(comm);	
	
	if(my_rank == 0)
	{
		//Stop timer.
		endTime = MPI_Wtime();
		
		printf("\nTHE SORTED OUTPUT IS - \n");
		for(int i=0;i<n;i++)
		{
			printf(" %d ",finalResults[i]);
		}
		printf("\n");
		printf("Approx. Time for execution : %f ",(endTime - startTime - wtime_overhead));
	}
	
	MPI_Finalize();
	return 0;
}
void glutMainLoop(void)
{
   int idleiters;

   if(ReshapeFunc)
      ReshapeFunc(VarInfo.xres, VarInfo.yres);

   if(!DisplayFunc) {
      sprintf(exiterror, "Fatal Error: No Display Function registered\n");
      exit(0);
   }   

   for(;;) {
      ProcessTimers();

      if(Active)
	 ReceiveInput();
      else
	 if(VisiblePoll)
	    TestVisible();

      if(IdleFunc)
	 IdleFunc();
      
      if(VisibleSwitch) {
	 VisibleSwitch = 0;
	 if(VisibilityFunc)
	    VisibilityFunc(Visible ? GLUT_VISIBLE : GLUT_NOT_VISIBLE);
      }

      if(Resized) {
         SetVideoMode();
         CreateBuffer();

         if(!glFBDevMakeCurrent( Context, Buffer, Buffer )) {
            sprintf(exiterror, "Failure to Make Current\n");
            exit(0);
         }

         InitializeMenus();

         if(ReshapeFunc)
            ReshapeFunc(VarInfo.xres, VarInfo.yres);

         Redisplay = 1;
         Resized = 0;
      }

      if(Visible && Redisplay) {
	 Redisplay = 0;
         EraseCursor();
	 DisplayFunc();
	 if(!(DisplayMode & GLUT_DOUBLE)) {
	    if(ActiveMenu)
	       DrawMenus();
            DrawCursor();
	 }
         idleiters = 0;
      } else {
         /* we sleep if not receiving redisplays, and
            the main loop is running faster than 2khz */

         static int lasttime;
         int time = glutGet(GLUT_ELAPSED_TIME);
         if(time > lasttime) {
            if(idleiters >= 2)
               usleep(100);

            idleiters = 0;
            lasttime = time;
         }
         idleiters++;         
      }
   }
}