예제 #1
0
// Advance the global grid one step.
void gol_advance(void)
{
    // Input grid.
    struct grid *in = grids[cur_grid];

    // Output grid
    struct grid *out = grids[1 - cur_grid];

    // TODO Communicate boundary elements with neighboring processes (left and/or right).
    int rank = 0, num_procs = 0;
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);
    MPI_Comm_size(MPI_COMM_WORLD, &num_procs);

    int right = ( rank + 1) % num_procs;
    int left = ( rank + num_procs - 1) % num_procs;
    

    if(num_procs > 1) {
        if(rank == 0) {
            mpi_get_row(in, in->width, rank, rank+1, right);
        }
        else if(rank+1 == num_procs) {
            mpi_get_row(in, 1, rank, rank-1, left);
        }
        else {
            mpi_get_row(in, 1, rank, rank-1, left);
            mpi_get_row(in, in->width, rank, rank+1, right);
        }
    }

    // Loop over all interior cells and advance them.
    for (int col = 0; col < in->width; ++col)
    {
        for (int row = 0; row < in->height; ++row)
        {
            advance_cell(in, out, row, col);
        }
    }
    
    // Swap input/output grids.
    cur_grid = 1 - cur_grid;
}
예제 #2
0
파일: sudoku.c 프로젝트: albanijr/sudoku-1
/* The main function, a fairly generic backtracking algorithm. */
void solve_sudoku(void)
{
    int pos = 0;
    while (1) {
        while (pos < 81 && known[pos/9][pos%9]) {
            ++pos;
        }
        if (pos >= 81) {
            break;
        }
        if (advance_cell(pos/9, pos%9)) {
            ++pos;
        } else {
            do {
                --pos;
            } while (pos >= 0 && known[pos/9][pos%9]);
            if (pos < 0) {
                break;
            }
        }
    }
}