Skip to content

Additional tests for hybrid MPI + OpenMP for loop parallelization #28

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 15, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions test/c++/mpi_chunk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,54 @@ TEST(MPI, OMPHybrid) {
EXPECT_EQ(n * (n - 1) / 2, sum);
}

TEST(MPI, OMPHybridParallelForSingleLoop) {
// first divide a range among MPI processes and then among OMP threads
mpi::communicator world;
int const n = 10;
long sum = 0;
#pragma omp parallel for reduction(+ : sum)
for (auto i : mpi::chunk(range(n))) sum += i;

// reduce and check the sum, i.e. that every element of the range has been visited
sum = mpi::all_reduce(sum, world);
EXPECT_EQ(n * (n - 1) / 2, sum);
}

TEST(MPI, OMPHybridParallelForDoubleLoop) {
mpi::communicator world;
int const n1 = 11;
int const n2 = 13;

// divide the outer loop among MPI processes, collapse the inner and outer loop and divide it among OMP threads
long sum = 0;
#pragma omp parallel for collapse(2) reduction(+ : sum)
for (auto i : mpi::chunk(range(n1))) {
for (auto j : range(n2)) sum += i * n2 + j;
}

// reduce and check the sum, i.e. that every element of the range has been visited
sum = mpi::all_reduce(sum, world);
EXPECT_EQ(n1 * n2 * (n1 * n2 - 1) / 2, sum);
}

TEST(MPI, OMPHybridParallelForTripleLoop) {
mpi::communicator world;
int const n1 = 11;
int const n2 = 13;
int const n3 = 17;

// divide the outer loop among MPI processes, collapse the inner and outer loops and divide it among OMP threads
long sum = 0;
#pragma omp parallel for collapse(3) reduction(+ : sum)
for (auto i : mpi::chunk(range(n1))) {
for (auto j : range(n2)) {
for (auto k : range(n3)) sum += i * n2 * n3 + j * n3 + k;
}
}

// reduce and check the sum, i.e. that every element of the range has been visited
sum = mpi::all_reduce(sum, world);
EXPECT_EQ(n1 * n2 * n3 * (n1 * n2 * n3 - 1) / 2, sum);
}

MPI_TEST_MAIN;
Loading