void cellularAutomataRound(short **grid, char birthParameters[9], char survivalParameters[9]) { short i, j, nbCount, newX, newY; enum directions dir; short **buffer2; buffer2 = allocGrid(); copyGrid(buffer2, grid); // Make a backup of grid in buffer2, so that each generation is isolated. for(i=0; i<DCOLS; i++) { for(j=0; j<DROWS; j++) { nbCount = 0; for (dir=0; dir< DIRECTION_COUNT; dir++) { newX = i + nbDirs[dir][0]; newY = j + nbDirs[dir][1]; if (coordinatesAreInMap(newX, newY) && buffer2[newX][newY]) { nbCount++; } } if (!buffer2[i][j] && birthParameters[nbCount] == 't') { grid[i][j] = 1; // birth } else if (buffer2[i][j] && survivalParameters[nbCount] == 't') { // survival } else { grid[i][j] = 0; // death } } } freeGrid(buffer2); }
void freeGame(Game* game){ int i; for(i=0; i<NUM_SPRITES_GAME; i++) if(game->sprites[i].image != NULL) SDL_FreeSurface(game->sprites[i].image); freeGrid(game->grid); TTF_CloseFont(game->font); Enemy *curEnemy = game->enemies; Enemy *tempE; while(curEnemy != NULL){ tempE = curEnemy; curEnemy = curEnemy->nextEnemy; free(tempE); } Tower *curTower = game->towers; Tower *tempT; while(curTower != NULL){ tempT = curTower; curTower = curTower->nextTower; free(tempT); } };
short pathingDistance(short x1, short y1, short x2, short y2, unsigned long blockingTerrainFlags) { short retval, **distanceMap = allocGrid(); calculateDistances(distanceMap, x2, y2, blockingTerrainFlags, NULL, true, true); retval = distanceMap[x1][y1]; freeGrid(distanceMap); return retval; }
//! Destructor of the Map Class. Map::~Map() { freeGrid(); if(pixbuf) gdk_pixbuf_unref(pixbuf); if(drawingPixBuf) gdk_pixbuf_unref(drawingPixBuf); }
int main(int argc, char** argv) { int status; char forking; message_t message; pid_t pid, cpid; ipc_t ipc; grid_t grid = gnew(); LOGPID("Using IPC method: %s.\n", IPC_METHOD); /* Before doing anything else, map data should be loaded! */ /* (so as to know the number of ants beforehand, at least */ if((status = loadGrid(grid, "configurationFile")) != NO_ERRORS) { printf("An error occurred while loading the configuration file\n"); exit(status); } /* This process will act as IPC server/simulation control */ sid = 1; /* Control has simulation ID 1 */ pid = getpid(); /* and it's pid, obviously */ LOGPID("Control process started.\n"); ipc = initServer(); /* Good! IPC server working. Let's spawn those ants. */ if (cpid = fork()) { /* Control code here */ int aux; if((aux = launchControl(ipc, grid)) != NO_ERROR){ printf("Simulation fail: %d\n", aux ); }else{ LOGPID("Simulation ended succesfully!\n"); } } else { /* Ants here */ do { sid++; pid = getpid(); ipc = initClient(); if (forking = (sid - 1 < grid->antsQuant)) forking = ((cpid = fork()) == 0); /* Child will keep forking */ } while (forking); /* We can do our own stuff now */ status = antLoop(ipc, grid); exit(status); } freeGrid(grid); }
static void monteCarloTaskStop(void *state) { MonteCarloState *mcs = (MonteCarloState*) state; printf("Acceptance ratio: %f\n", ((double) mcs->accepted) / mcs->attempted); freeGrid(); free(mcs); }
/*! This method allocates memory for the grid representation of the Map. * This grid is an WxH matrix of bool which represent if the underlying image * pixels are free or occupied. */ void Map::allocateGrid() { if(grid) freeGrid(); this->grid = new bool * [width]; // printf("\nAllocating Grid %d X %d map, at %.3f m/pix",this->width, this->height, this->mapRes); fflush(stdout); for(int i=0; i < width; i++) { grid[i] = new bool [height]; for(int j=0;j < height;j++) grid[i][j] = true; } center.setX(width/2.0f); center.setY(height/2.0f); };
/* Free architecture data structures */ void free_arch(t_arch* Arch) { int i; t_model *model, *prev; t_model_ports *port, *prev_port; struct s_linked_vptr *vptr, *vptr_prev; freeGrid(); free(chan_width_x); chan_width_x = NULL; free(chan_width_y); chan_width_y = NULL; for (i = 0; i < Arch->num_switches; i++) { if (Arch->Switches->name != NULL) { free(Arch->Switches[i].name); } } free(Arch->Switches); free(switch_inf); for (i = 0; i < Arch->num_segments; i++) { if (Arch->Segments->cb != NULL) { free(Arch->Segments[i].cb); } if (Arch->Segments->sb != NULL) { free(Arch->Segments[i].sb); } } free(Arch->Segments); model = Arch->models; while (model) { port = model->inputs; while (port) { prev_port = port; port = port->next; free(prev_port->name); free(prev_port); } port = model->outputs; while (port) { prev_port = port; port = port->next; free(prev_port->name); free(prev_port); } vptr = model->pb_types; while (vptr) { vptr_prev = vptr; vptr = vptr->next; free(vptr_prev); } prev = model; model = model->next; if (prev->instances) free(prev->instances); free(prev->name); free(prev); } for (i = 0; i < 4; i++) { vptr = Arch->model_library[i].pb_types; while (vptr) { vptr_prev = vptr; vptr = vptr->next; free(vptr_prev); } } for (i = 0; i < Arch->num_directs; i++) { free(Arch->Directs[i].name); free(Arch->Directs[i].from_pin); free(Arch->Directs[i].to_pin); } free(Arch->Directs); free(Arch->model_library[0].name); free(Arch->model_library[0].outputs->name); free(Arch->model_library[0].outputs); free(Arch->model_library[1].inputs->name); free(Arch->model_library[1].inputs); free(Arch->model_library[1].name); free(Arch->model_library[2].name); free(Arch->model_library[2].inputs[0].name); free(Arch->model_library[2].inputs[1].name); free(Arch->model_library[2].inputs); free(Arch->model_library[2].outputs->name); free(Arch->model_library[2].outputs); free(Arch->model_library[3].name); free(Arch->model_library[3].inputs->name); free(Arch->model_library[3].inputs); free(Arch->model_library[3].outputs->name); free(Arch->model_library[3].outputs); free(Arch->model_library); if (Arch->clocks) { free(Arch->clocks->clock_inf); } free_complex_block_types(); free_chunk_memory_trace(); }
/* * Sets globals: nx, ny * Allocs globals: chan_width_x, chan_width_y, grid * Depends on num_clbs, pins_per_clb */ void vpr_init_pre_place_and_route(INP t_vpr_setup vpr_setup, INP t_arch Arch) { int *num_instances_type, *num_blocks_type; int i; int current, high, low; boolean fit; /* Read in netlist file for placement and routing */ if (vpr_setup.FileNameOpts.NetFile) { read_netlist(vpr_setup.FileNameOpts.NetFile, &Arch, &num_blocks, &block, &num_nets, &clb_net); /* This is done so that all blocks have subblocks and can be treated the same */ check_netlist(); } /* Output the current settings to console. */ printClusteredNetlistStats(); if (vpr_setup.Operation == TIMING_ANALYSIS_ONLY) { do_constant_net_delay_timing_analysis(vpr_setup.Timing, vpr_setup.constant_net_delay); } else { current = nint((float)sqrt((float)num_blocks)); /* current is the value of the smaller side of the FPGA */ low = 1; high = -1; num_instances_type = (int*) my_calloc(num_types, sizeof(int)); num_blocks_type = (int*) my_calloc(num_types, sizeof(int)); for (i = 0; i < num_blocks; i++) { num_blocks_type[block[i].type->index]++; } if (Arch.clb_grid.IsAuto) { /* Auto-size FPGA, perform a binary search */ while (high == -1 || low < high) { /* Generate grid */ if (Arch.clb_grid.Aspect >= 1.0) { ny = current; nx = nint(current * Arch.clb_grid.Aspect); } else { nx = current; ny = nint(current / Arch.clb_grid.Aspect); } #if DEBUG vpr_printf(TIO_MESSAGE_INFO, "Auto-sizing FPGA at x = %d y = %d\n", nx, ny); #endif alloc_and_load_grid(num_instances_type, &Arch); freeGrid(); /* Test if netlist fits in grid */ fit = TRUE; for (i = 0; i < num_types; i++) { if (num_blocks_type[i] > num_instances_type[i]) { fit = FALSE; break; } } /* get next value */ if (!fit) { /* increase size of max */ if (high == -1) { current = current * 2; if (current > MAX_SHORT) { vpr_printf(TIO_MESSAGE_ERROR, "FPGA required is too large for current architecture settings.\n"); exit(1); } } else { if (low == current) current++; low = current; current = low + ((high - low) / 2); } } else { high = current; current = low + ((high - low) / 2); } } /* Generate grid */ if (Arch.clb_grid.Aspect >= 1.0) { ny = current; nx = nint(current * Arch.clb_grid.Aspect); } else { nx = current; ny = nint(current / Arch.clb_grid.Aspect); } alloc_and_load_grid(num_instances_type, &Arch); vpr_printf(TIO_MESSAGE_INFO, "FPGA auto-sized to x = %d y = %d\n", nx, ny); } else { nx = Arch.clb_grid.W; ny = Arch.clb_grid.H; alloc_and_load_grid(num_instances_type, &Arch); } vpr_printf(TIO_MESSAGE_INFO, "The circuit will be mapped into a %d x %d array of clbs.\n", nx, ny); /* Test if netlist fits in grid */ fit = TRUE; for (i = 0; i < num_types; i++) { if (num_blocks_type[i] > num_instances_type[i]) { fit = FALSE; break; } } if (!fit) { vpr_printf(TIO_MESSAGE_ERROR, "Not enough physical locations for type %s, number of blocks is %d but number of locations is %d.\n", type_descriptors[i].name, num_blocks_type[i], num_instances_type[i]); exit(1); } vpr_printf(TIO_MESSAGE_INFO, "\n"); vpr_printf(TIO_MESSAGE_INFO, "Resource usage...\n"); for (i = 0; i < num_types; i++) { vpr_printf(TIO_MESSAGE_INFO, "\tNetlist %d\tblocks of type: %s\n", num_blocks_type[i], type_descriptors[i].name); vpr_printf(TIO_MESSAGE_INFO, "\tArchitecture %d\tblocks of type: %s\n", num_instances_type[i], type_descriptors[i].name); } vpr_printf(TIO_MESSAGE_INFO, "\n"); chan_width_x = (int *) my_malloc((ny + 1) * sizeof(int)); chan_width_y = (int *) my_malloc((nx + 1) * sizeof(int)); free(num_blocks_type); free(num_instances_type); } }
boolean getQualifyingPathLocNear(short *retValX, short *retValY, short x, short y, boolean hallwaysAllowed, unsigned long blockingTerrainFlags, unsigned long blockingMapFlags, unsigned long forbiddenTerrainFlags, unsigned long forbiddenMapFlags, boolean deterministic) { short **grid, **costMap; short loc[2]; // First check the given location to see if it works, as an optimization. if (!cellHasTerrainFlag(x, y, blockingTerrainFlags | forbiddenTerrainFlags) && !(pmap[x][y].flags & (blockingMapFlags | forbiddenMapFlags)) && (hallwaysAllowed || passableArcCount(x, y) <= 1)) { *retValX = x; *retValY = y; return true; } // Allocate the grids. grid = allocGrid(); costMap = allocGrid(); // Start with a base of a high number everywhere. fillGrid(grid, 30000); fillGrid(costMap, 1); // Block off the pathing blockers. getTerrainGrid(costMap, PDS_FORBIDDEN, blockingTerrainFlags, blockingMapFlags); if (blockingTerrainFlags & (T_OBSTRUCTS_DIAGONAL_MOVEMENT | T_OBSTRUCTS_PASSABILITY)) { getTerrainGrid(costMap, PDS_OBSTRUCTION, T_OBSTRUCTS_DIAGONAL_MOVEMENT, 0); } // Run the distance scan. grid[x][y] = 1; costMap[x][y] = 1; dijkstraScan(grid, costMap, true); findReplaceGrid(grid, 30000, 30000, 0); // Block off invalid targets that aren't pathing blockers. getTerrainGrid(grid, 0, forbiddenTerrainFlags, forbiddenMapFlags); if (!hallwaysAllowed) { getPassableArcGrid(grid, 2, 10, 0); } // Get the solution. randomLeastPositiveLocationInGrid(grid, retValX, retValY, deterministic); // dumpLevelToScreen(); // displayGrid(grid); // if (coordinatesAreInMap(*retValX, *retValY)) { // hiliteCell(*retValX, *retValY, &yellow, 100, true); // } // temporaryMessage("Qualifying path selected:", true); freeGrid(grid); freeGrid(costMap); // Fall back to a pathing-agnostic alternative if there are no solutions. if (*retValX == -1 && *retValY == -1) { if (getQualifyingLocNear(loc, x, y, hallwaysAllowed, NULL, (blockingTerrainFlags | forbiddenTerrainFlags), (blockingMapFlags | forbiddenMapFlags), false, deterministic)) { *retValX = loc[0]; *retValY = loc[1]; return true; // Found a fallback solution. } else { return false; // No solutions. } } else { return true; // Found a primary solution. } }
int main(int argc, char* argv[]){ // domain variables int iter = 0; int i; int file_index; int max_iter; double max_time = 1.0; double dt = .0000001; double domain_box[4] = {0.0, 0.0, 1.0, 2.0}; double domain_m_box[4] = {.10, 0.2, 0.90, 2.00}; int domain_num_cells[2] = {10, 20}; int pp_cell[2] = {2,2}; double ipart_dim[2]; int domain_num_particles[2]; double test_px, test_py; char p_filename[40]; char p_filename2[40]; char g_filename[40]; //timing variables double start_time; double end_time; // patch variables int rank = 0; int size; int num_proc[2]; double box[4]; double m_box[4]; int num_cells[2]; int halo_cells[4] = {1,1,1,1}; int num_particles[2]; int sfactor = 2; GridData grid_data; Node** grid; InitialParticleData ipart_data; Particle* particles; Particle* post_particles; Particle* particle_list; Particle* rhalo_parts[8]; Particle* shalo_parts[8]; MPI_Init(&argc, &argv); MPI_Comm_size(MPI_COMM_WORLD, &size); MPI_Comm_rank(MPI_COMM_WORLD, &rank); MPI_Request halo_req[8][2]; MPI_Status halo_stat[8][2]; max_iter = (int)(max_time/dt); if(rank == 0){ if(argc == 3){ num_proc[0] = atoi(argv[1]); num_proc[1] = atoi(argv[2]); }else if(argc == 6){ num_proc[0] = atoi(argv[1]); num_proc[1] = atoi(argv[2]); domain_num_cells[0] = atoi(argv[3]); domain_num_cells[1] = atoi(argv[4]); max_iter = atoi(argv[5]); } } MPI_Bcast(num_proc, 2, MPI_INT, 0, MPI_COMM_WORLD); MPI_Bcast(domain_num_cells, 2, MPI_INT, 0, MPI_COMM_WORLD); MPI_Bcast(&max_iter, 1, MPI_INT, 0, MPI_COMM_WORLD); MPI_Barrier(MPI_COMM_WORLD); grid_data.gravity[0] = 0; grid_data.gravity[1] = -900.8; grid_data.cell_dim[0] = (domain_box[2] - domain_box[0])/domain_num_cells[0]; grid_data.cell_dim[1] = (domain_box[3] - domain_box[1])/domain_num_cells[1]; test_px = grid_data.cell_dim[0]/pp_cell[0]; test_py = grid_data.cell_dim[1]/pp_cell[1]; domain_num_particles[0] = (int)((domain_m_box[2] - domain_m_box[0])/test_px); domain_num_particles[1] = (int)((domain_m_box[3] - domain_m_box[1])/test_py); ipart_dim[0] = (domain_m_box[2] - domain_m_box[0])/domain_num_particles[0]; ipart_dim[1] = (domain_m_box[3] - domain_m_box[1])/domain_num_particles[1]; decomposeGrid(rank, num_proc, domain_num_cells, num_cells, domain_box, box, halo_cells, &grid_data); decomposeMaterial(box, domain_m_box, m_box, ipart_dim, num_particles); ipart_data.density = 1000; ipart_data.bulk = 3; ipart_data.shear = 4; ipart_data.E = 90000; ipart_data.poisson = .30; ipart_data.idim[0] = ipart_dim[0]; ipart_data.idim[1] = ipart_dim[1]; ipart_data.velocity[0] = 0; ipart_data.velocity[1] = 0; ipart_data.box[0] = m_box[0]; ipart_data.box[1] = m_box[1]; ipart_data.box[2] = m_box[2]; ipart_data.box[3] = m_box[3]; ipart_data.num_particles[0] = num_particles[0]; ipart_data.num_particles[1] = num_particles[1]; ipart_data.domain_num_particles[0] = domain_num_particles[0]; ipart_data.domain_num_particles[1] = domain_num_particles[1]; grid = createGrid(&grid_data, box, num_cells); particles = createMaterial(&grid_data, &ipart_data); post_particles = createMaterial(&grid_data, &ipart_data); initializeGrid(&grid_data, grid); initializeMaterial(&grid_data, &ipart_data, particles); //allocate halo particles allocateHaloParticles(&grid_data, sfactor, pp_cell, rhalo_parts, shalo_parts); file_index = 0; start_time = MPI_Wtime(); for(iter = 0; iter <= max_iter; iter++){ gatherHaloParticles(&grid_data, particles, rhalo_parts, shalo_parts); sendRecvParticles(&grid_data, rhalo_parts, shalo_parts); clearGridValues(&grid_data, grid); mapToGrid(&grid_data, grid, particles); for(i = 0; i < 8; i++){ if(grid_data.rank[i] >= 0 && rhalo_parts[i][0].particle_count > 0){ mapToGrid(&grid_data, grid, rhalo_parts[i]); } } momentumToVelocityOnGrid(&grid_data, grid); computeForces(&grid_data, grid, particles); for(i = 0; i < 8; i++){ if(grid_data.rank[i] >= 0 && rhalo_parts[i][0].particle_count > 0){ computeForces(&grid_data, grid, rhalo_parts[i]); } } computeAcceleration(&grid_data, grid); /** particle_list = gatherParticles(rank, size, domain_num_particles[0] * domain_num_particles[1], particles, post_particles); if(rank == 0){ //if(iter%100 == 0){ sprintf(p_filename, "ppart%06d.vtk", file_index); //sprintf(p_filename2, "center%06d.vtk", file_index); //sprintf(g_filename, "grid_output%06d.vtk", file_index); //writeParticlesVtkFile(&grid_data, grid, rhalo_parts[0], p_filename); writeParticlesVtkFile(&grid_data, grid, particle_list, p_filename); //writeParticlesVtkFile(&grid_data, grid, particle_list, p_filename); //writeParticlesVtkFile(&grid_data, grid, particles, p_filename); //writeGridVtkFile(&grid_data, grid, g_filename); //writeGridVtkFile(&grid_data, grid, g_filename); //free(particle_list); file_index++; //} } **/ updateNodalValues(&grid_data, grid, dt); updateStressAndStrain(&grid_data, grid, particles, dt); pUpdateParticlePosition(&grid_data, grid, particles, post_particles, shalo_parts, dt); sendRecvParticles(&grid_data, rhalo_parts, shalo_parts); updateParticleList(&grid_data, particles, rhalo_parts); /** particle_list = gatherParticles(rank, size, domain_num_particles[0] * domain_num_particles[1], particles, post_particles); if(rank == 0){ sprintf(p_filename, "particle_output%06d.vtk", file_index); file_index++; //sprintf(g_filename, "grid_output%06d.vtk", file_index); writeParticlesVtkFile(&grid_data, grid, particles, p_filename); //writeParticlesVtkFile(&grid_data, grid, particle_list, p_filename); //writeParticlesVtkFile(&grid_data, grid, particles, p_filename); //writeGridVtkFile(&grid_data, grid, g_filename); free(particle_list); } **/ MPI_Barrier(MPI_COMM_WORLD); } end_time = MPI_Wtime(); MPI_Barrier(MPI_COMM_WORLD); //particle_list = gatherParticles(rank, size, domain_num_particles[0] * domain_num_particles[1], // particles, post_particles); if(rank == 0){ printf("Parallel, np: %d, iterations: %d, time: %f\n", size, max_iter, end_time-start_time); //writeParticlesVtkFile(&grid_data, grid, particle_list, "pparts.vtk"); //free(particle_list); } freeGrid(grid, &grid_data); freeMaterial(particles); freeMaterial(post_particles); freeHaloParticles(&grid_data, rhalo_parts, shalo_parts); //MPI_Barrier(MPI_COMM_WORLD); MPI_Finalize(); return 0; }