void solve() { int i; int j; value=0; memset(vis,0,sizeof(vis)); for(i=0;i<row;i++) for(j=0;j<col;j++) if(!vis[i][j]) {value++;DFS(i,j,forest[i][j]);} PrintGrid(); row=col=0; return; }
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); }
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); }
int main(int argc, char* argv[]) { int** grid; // grid containing the cells and whether they are alive or dead // set up curses for displaying the grid #ifdef USE_CURSES initscr(); if(has_colors()) { start_color(); init_pair(1, COLOR_BLACK, COLOR_WHITE); init_pair(2, COLOR_WHITE, COLOR_CYAN); } #endif // allocate the grid grid = AllocateGrid(); if(grid == NULL) { printf("ERROR: Could not allocate memory for grid\n"); return -1; } // initialize and print the grid InitGrid(grid); #ifdef PRINT_GRID PrintGrid(grid); #endif for(int time = 1; time <= TIME_STEPS; ++time) { // sleep to observe updates #ifdef PRINT_GRID usleep(SLEEP_TIME); #endif // allocate temporary grid for storing the simultaneous update int** tempGrid = AllocateGrid(); if(tempGrid == NULL) { printf("ERROR: Could not allocate memory for grid\n"); return -1; } for(int i = 0; i < GRID_SIZE; ++i) { for(int j = 0; j < GRID_SIZE; ++j) { int numNeighborAlive = 0; for(int k = -1; k <= 1; ++k) { for(int l = -1; l <= 1; ++l) { int row = (i + k) % GRID_SIZE; int col = (j + l) % GRID_SIZE; if(row == -1) row = GRID_SIZE - 1; if(col == -1) col = GRID_SIZE - 1; if( (row != i) || (col != j) ) numNeighborAlive += grid[row][col]; } } switch(numNeighborAlive) { case 2: tempGrid[i][j] = grid[i][j]; break; case 3: tempGrid[i][j] = 1; break; default: tempGrid[i][j] = 0; } } } free(grid); grid = tempGrid; #ifdef USE_CURSES move(0, 0); #endif #ifdef PRINT_GRID PrintGrid(grid); #endif } CountLive(grid); #ifdef USE_CURSES printw("\nPress any key to continue..."); refresh(); getchar(); #endif free(grid); #ifdef USE_CURSES endwin(); #endif return 0; }