// do all the work on 'np' partitions, 'nx' data points each, for 'nt' // time steps space do_work(std::size_t np, std::size_t nx, std::size_t nt) { // U[t][i] is the state of position i at time t. std::vector<space> U(2); for (space& s: U) s.resize(np); // Initial conditions: f(0, i) = i # pragma omp parallel { // Visual Studio requires OMP loop variables to be signed :/ # pragma omp for schedule(static) for (boost::int64_t i = 0; i < (boost::int64_t)np; ++i) U[0][i] = partition_data(nx, double(i)); // Actual time step loop for (std::size_t t = 0; t != nt; ++t) { space const& current = U[t % 2]; space& next = U[(t + 1) % 2]; // Visual Studio requires OMP loop variables to be signed :/ # pragma omp for schedule(static) for (boost::int64_t i = 0; i < (boost::int64_t)np; ++i) next[i] = heat_part(current[idx(i-1, np)], current[i], current[idx(i+1, np)]); } } // Return the solution at time-step 'nt'. return U[nt % 2]; }
int main ( int argc, char *argv[] ) /******************************************************************************/ { double a = 0.0; double b = 1.0; int i; int id; int n; int p; double x_max; double x_min; /* Startup: */ MPI_Init ( &argc, &argv ); MPI_Comm_rank ( MPI_COMM_WORLD, &id ); MPI_Comm_size ( MPI_COMM_WORLD, &p ); /* Determine the portion of [A,B] to be assigned to processor ID. */ n = 12; i = 0; x_min = ( ( double )( p * n + 1 - id * n - i ) * a + ( double )( id * n + i ) * b ) / ( double ) ( p * n + 1 ); i = n + 1; x_max = ( ( double )( p * n + 1 - id * n - i ) * a + ( double )( id * n + i ) * b ) / ( double )( p * n + 1 ); heat_part ( n, p, id, x_min, x_max ); /* Shut down. */ MPI_Finalize ( ); return p; }
// do all the work on 'np' partitions, 'nx' data points each, for 'nt' // time steps space do_work(std::size_t np, std::size_t nx, std::size_t nt) { // U[t][i] is the state of position i at time t. std::vector<space> U(2); for (space& s: U) s.resize(np); // Initial conditions: f(0, i) = i for (std::size_t i = 0; i != np; ++i) U[0][i] = partition_data(nx, double(i)); // Actual time step loop for (std::size_t t = 0; t != nt; ++t) { space const& current = U[t % 2]; space& next = U[(t + 1) % 2]; for (std::size_t i = 0; i != np; ++i) next[i] = heat_part(current[idx(i, -1, np)], current[i], current[idx(i, +1, np)]); } // Return the solution at time-step 'nt'. return U[nt % 2]; }