Exemplo n.º 1
0
int main(int argc, char **argv)
{
    
    
	int nthreads = 0;
	#ifdef UNIX
	pthread_t threads[MAXTHREADS];
	#endif UNIX	
	grain_type rowGranularity = NONE;
	#ifdef WINDOWS
	HANDLE threads[MAXTHREADS];
	#endif
	long initSum = 0, finalSum = 0;
	int i;
	
	
	if (argc > 3)
	{
		gridsize = atoi(argv[1]);
		if (gridsize > MAXGRIDSIZE || gridsize < 1)
		{
			printf("Grid size must be between 1 and 10.\n");
			return(1);
		}
		nthreads = atoi(argv[2]);
		if (nthreads < 1 || nthreads > MAXTHREADS)
		{
			printf("Number of threads must be between 1 and 1000.");
			return(1);
		}
        
		if (argv[3][1] == 'r' || argv[3][1] == 'R')
			rowGranularity = ROW;
		if (argv[3][1] == 'c' || argv[3][1] == 'C')
			rowGranularity = CELL;
		if (argv[3][1] == 'g' || argv[3][1] == 'G')
            rowGranularity = GRID;
        
	}
	else
	{
		printf("Format:  gridapp gridSize numThreads -cell\n");
		printf("         gridapp gridSize numThreads -row\n");
		printf("         gridapp gridSize numThreads -grid\n");
		printf("         gridapp gridSize numThreads -none\n");
		return(1);
	}
    
	printf("Initial Grid:\n\n");
	initSum =  InitGrid(grid, gridsize);
	PrintGrid(grid, gridsize);
	printf("\nInitial Sum:  %d\n", initSum);
	printf("Executing threads...\n");
    

    //initialize all the mutex we need
	#ifdef UNIX
	init_row_mutex(row_mutex, gridsize);
    init_cell_mutex(cell_mutex, gridsize);
	#endif
	#ifdef WINDOWS
	init_row_mutex(row_mutex, gridsize);
    init_cell_mutex(cell_mutex, gridsize);
	#endif

	/* better to seed the random number generator outside
     of do swaps or all threads will start with same
     choice
     */
	srand((unsigned int)time( NULL ) );

	time(&start_t);
	for (i = 0; i < nthreads; i++)
	{
		//create thread
		#ifdef UNIX
		if (pthread_create(&(threads[i]), NULL, do_swaps, (void *)(&rowGranularity)) != 0)
		{
			perror("thread creation failed:");
			exit(-1);
		}
		#endif	

		#ifdef WINDOWS
		threads[i]=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)do_swaps,(void*)(&rowGranularity), 0, NULL);
		#endif
	}
    
    
	for (i = 0; i < nthreads; i++)
	{
		#ifdef UNIX
		pthread_detach(threads[i]);
		#endif
		#ifdef WINDOWS
		for (i = 0; i < nthreads; i++)
		CloseHandle(threads[i]);
		#endif
	}
    
    
	while (1)
	{
		#ifdef UNIX
		sleep(2);
		#endif
		#ifdef WINDOWS
		_sleep(2000);
		#endif
		if (threads_left == 0)
        {
		    fprintf(stdout, "\nFinal Grid:\n\n");
		    PrintGrid(grid, gridsize);
		    finalSum = SumGrid(grid, gridsize);
		    fprintf(stdout, "\n\nFinal Sum:  %d\n", finalSum);
		    if (initSum != finalSum){
                fprintf(stdout,"DATA INTEGRITY VIOLATION!!!!!\n");
		    } else {
                fprintf(stdout,"DATA INTEGRITY MAINTAINED!!!!!\n");
		    }
			#ifdef UNIX
		    fprintf(stdout, "Secs elapsed:  %g\n", difftime(end_t, start_t));
			#endif
			#ifdef WINDOWS
			printf("secs elapsed: %d\n", end_t - start_t);
			getchar();
			#endif
            
		    exit(0);
        }
	}	
	
	//destroy the mutex we have created 
#ifdef UNIX
    pthread_mutex_destroy(&grid_mutex);
    pthread_mutex_destroy(&row_mutex);
    pthread_mutex_destroy(&lock_enter);
    pthread_mutex_destroy(&lock_exit);
    pthread_mutex_destroy(&cell_mutex);
#endif
#ifdef WIINDOWS
	CloseHandle(grid_mutex);
	CloseHandle(row_mutex);
	CloseHandle(lock_enter);
	CloseHandle(lock_exit);
	CloseHandle(cell_mutex);
#endif
	system("pause");
	return(0);
	
}
Exemplo n.º 2
0
int main(int argc, char **argv)
{


	int nthreads = 0;
	pthread_t threads[MAXTHREADS];
	grain_type rowGranularity = NONE;
	long initSum = 0, finalSum = 0;
	int i;

	
	if (argc > 3)
	{
		gridsize = atoi(argv[1]);					
		if (gridsize > MAXGRIDSIZE || gridsize < 1)
		{
			printf("Grid size must be between 1 and 10.\n");
			return(1);
		}
		nthreads = atoi(argv[2]);
		if (nthreads < 1 || nthreads > MAXTHREADS)
		{
			printf("Number of threads must be between 1 and 1000.");
			return(1);
		}

		if (argv[3][1] == 'r' || argv[3][1] == 'R')
			rowGranularity = ROW;
		if (argv[3][1] == 'c' || argv[3][1] == 'C')
			rowGranularity = CELL;
		if (argv[3][1] == 'g' || argv[3][1] == 'G')
		  rowGranularity = GRID;
			
	}
	else
	{
		printf("Format:  gridapp gridSize numThreads -cell\n");
		printf("         gridapp gridSize numThreads -row\n");
		printf("         gridapp gridSize numThreads -grid\n");
		printf("         gridapp gridSize numThreads -none\n");
		return(1);
	}

	printf("Initial Grid:\n\n");
	initSum =  InitGrid(grid, gridsize);
	PrintGrid(grid, gridsize);
	printf("\nInitial Sum:  %d\n", initSum);
	printf("Executing threads...\n");

	/* better to seed the random number generator outside
	   of do swaps or all threads will start with same
	   choice
	*/
	srand((unsigned int)time( NULL ) );
	
	time(&start_t);
	for (i = 0; i < nthreads; i++)
	{
		if (pthread_create(&(threads[i]), NULL, do_swaps, (void *)(&rowGranularity)) != 0)
		{
			perror("thread creation failed:");
			exit(-1);
		} 
	}

	


	for (i = 0; i < nthreads; i++)
		pthread_detach(threads[i]);


	while (1)
	{
		sleep(2);
		if (threads_left == 0)
		  {
		    fprintf(stdout, "\nFinal Grid:\n\n");
		    PrintGrid(grid, gridsize);
		    finalSum = SumGrid(grid, gridsize); 
		    fprintf(stdout, "\n\nFinal Sum:  %d\n", finalSum);
		    if (initSum != finalSum){
		      fprintf(stdout,"DATA INTEGRITY VIOLATION!!!!!\n");
		    } else {
		      fprintf(stdout,"DATA INTEGRITY MAINTAINED!!!!!\n");
		    }
		    fprintf(stdout, "Secs elapsed:  %g\n", difftime(end_t, start_t));

		    exit(0);
		  }
	}	
	
	
	return(0);
	
}