/** * Performs the game of life operation over the given range. * \param range row/column range. */ void operator()(const Range2D& range) { BoolMatrix first = _first; BoolMatrix second = _second; const Range& rows = range.rows(); const Range& cols = range.cols(); for (index_t y = rows.begin(); y != rows.end(); ++y) { for (index_t x = cols.begin(); x != cols.end(); ++x) { index_t peers = sumNeighbours(x, y); if (peers < 2 || peers > 3) { MATRIX_RECT(second, y, x) = false; // hunger/overcrowding } else if (peers == 3) { MATRIX_RECT(second, y, x) = true; // breeding } else { MATRIX_RECT(second, y, x) = MATRIX_RECT(first, y, x); // nothing } if (MATRIX_RECT(second, y, x)) { aliveCount++; } } } }
void HeaterMOO::diffuse ( double** src, double** dest ) { // modèle 1: check les bounds uint32_t h = getH (); uint32_t w = getW (); #pragma omp parallel for if ( _omp ) for ( size_t i = 0; i < w; i++ ) { for ( size_t j = 0; j < h; j++ ) { double old = src[i][j]; double neighbours = sumNeighbours ( src, i, j ); dest[i][j] = old + _k * ( neighbours - ( 4 * old ) ); } } }
void CowichanMPI::life(BoolMatrix matrixIn, BoolMatrix matrixOut) { int i; // iteration index index_t r; // row index int alive; // number alive index_t lo, hi; // work controls index_t rlo, rhi; // for broadcast bool work; // useful work to do? int is_alive = 1; // some cells still alive? BoolMatrix m_tmp; // tmp pointer // work work = get_block (world, 0, nr, &lo, &hi); for (i = 0; (i < lifeIterations) && (is_alive > 0); i++) { // reset alive neighbour count alive = 0; // count neighbours and fill new matrix if (work) { for (r = lo; r < hi; r++) { for (index_t c = 0; c < nc; ++c) { index_t count = sumNeighbours(matrixIn, r, c, nr, nc); if (count == 3 || ((count == 2) && MATRIX_RECT(matrixIn, r, c))) { MATRIX_RECT(matrixOut, r, c) = true; ++alive; } else { MATRIX_RECT(matrixOut, r, c) = false; } } } } // broadcast matrix for (r = 0; r < world.size (); r++) { if (get_block (world, 0, nr, &rlo, &rhi, r)) { broadcast (world, &MATRIX_RECT(matrixOut, rlo, 0), (int)((rhi - rlo) * nc), (int)r); } } // is_alive is maximum of local alive's all_reduce (world, alive, is_alive, mpi::maximum<int>()); // swap matrices (ping-pong) m_tmp = matrixIn; matrixIn = matrixOut; matrixOut = m_tmp; } }