int main(void) { char *line; t_map map; bzero(&map, sizeof(map)); if (!(map.cells = ht_create(2048)) || !(map.list = new_vector(sizeof(char *))) || !extract_nb_ants(&map) || map.ants_nb > 1000000) exit(write(1, "Error\n", 6)); line = NULL; while (get_next_line(0, &line) == 1) { write(1, line, ft_strlen(line)); write(1, "\n", 1); if (!analyze_line(line, &map)) break ; free(line); } write(1, "\n", 1); if (map.start && map.end) solve_master(&map); else write(1, "Error\n", 6); return (1); }
/* invoked by the master to solve find all solutions of the first k columns solutions are put onto the jobs queue */ void solve_master(int col, int *hist) { if (col == K) { enqueue(jobs, hist); //found a k length solution, throw it on the queue return; } # define attack(i, j) (hist[j] == i || abs(hist[j] - i) == col - j) for (int i = 0, j = 0; i < N; i++) { //for each row for (j = 0; j < col && !attack(i, j); j++); //if this piece is safe, move to the next column if (j < col) continue; hist[col] = i; //this row is safe for the current column solve_master(col + 1, hist); } }
void master() { jobs = init_queue(); MPI_Status status; int hist[N]; solve_master(0, hist); int processors, rank; MPI_Comm_size(MPI_COMM_WORLD, &processors); int * work; // send everyone some work! // keep track of how many nodes received work int deployed = 0; for(rank = 1; rank < processors; rank++) { if(!is_empty(jobs)) { work = pop(jobs); MPI_Send( work, K, MPI_INT, rank, WORKTAG, MPI_COMM_WORLD); deployed++; } } // now listen for completions and dispatch work until done! int done[K]; //garbage buffer used to match signature while(!is_empty(jobs)) { //receive completion MPI_Recv( done, /* message buffer */ 1, /* one data item */ MPI_INT, /* of type double real */ MPI_ANY_SOURCE, /* receive from any sender */ MPI_ANY_TAG, /* any type of message */ MPI_COMM_WORLD, /* default communicator */ &status); /* info about the received message */ // send it more work work = pop(jobs); MPI_Send( work, K, MPI_INT, status.MPI_SOURCE, WORKTAG, MPI_COMM_WORLD); } // work is done, receive outstanding jobs // receive outstanding jobs only from the number of machines that received work for(rank = 1; rank <= deployed; rank++) { MPI_Recv( done, 1, MPI_INT, MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, &status); } // Set the slaves free! for(rank=1; rank<processors; rank++) { MPI_Send( done, K, MPI_INT, rank, DIETAG, MPI_COMM_WORLD); } }