From 4289b28cff3f9f8ae91cad891234232a51bb52ff Mon Sep 17 00:00:00 2001 From: Joss Whittle Date: Fri, 8 Jul 2016 02:15:50 +0100 Subject: [PATCH 1/2] formatting --- example-code/ompExample.cpp | 187 ++++++++++++++++++------------------ 1 file changed, 94 insertions(+), 93 deletions(-) diff --git a/example-code/ompExample.cpp b/example-code/ompExample.cpp index 807517c..ec13a45 100644 --- a/example-code/ompExample.cpp +++ b/example-code/ompExample.cpp @@ -30,97 +30,98 @@ SOFTWARE. // Example Usage: mpirun --pernode --hostfile [path] ./ompExample // //-----------------------------------------------------------------// int main(int argc, char *argv []) { - MEL::Init(argc, argv); - - // Who are we? - MEL::Comm comm = MEL::Comm::WORLD; - const int rank = MEL::CommRank(comm), - size = MEL::CommSize(comm); - - double start, end; - - // Allocate buffers - const int LEN = 1e8; - int *src = MEL::MemAlloc(LEN, 1), // Initilize to 1 - *dst = (rank == 0) ? MEL::MemAlloc(LEN) : nullptr; - - // Perform the Reduction on 1 thread with MPI_SUM - start = MEL::Wtime(); - MEL::Reduce(src, dst, LEN, MEL::Op::SUM, 0, comm); // Equivalent to standard MPI_Reduce call - end = MEL::Wtime(); - - if (rank == 0) - std::cout << "Reduced " << LEN << " elements in " << std::setw(10) << (end - start) - << " seconds on 1 thread with MEL::Op::SUM == MPI_SUM." << std::endl; - - // Create a MEL User Defined Operation using a functor wrapped in a map function - auto SUM = MEL::OpCreate(); - - // Perform the Reduction on 1 thread - start = MEL::Wtime(); - MEL::Reduce(src, dst, LEN, SUM, 0, comm); - end = MEL::Wtime(); - - if (rank == 0) - std::cout << "Reduced " << LEN << " elements in " << std::setw(10) << (end - start) - << " seconds on 1 thread with mapped MEL::FUNCTOR::SUM." << std::endl; - - // Create a MEL User Defined Operation using a functor wrapped in a parallel map function - auto ompSUM = MEL::OMP::OpCreate(); - - omp_set_num_threads(2); - omp_set_schedule(omp_sched_static, 0); - - // Perform the Reduction on 4 threads - start = MEL::Wtime(); - MEL::Reduce(src, dst, LEN, ompSUM, 0, comm); - end = MEL::Wtime(); - - if (rank == 0) - std::cout << "Reduced " << LEN << " elements in " << std::setw(10) << (end - start) - << " seconds on 2 threads with parallel mapped MEL::FUNCTOR::SUM." << std::endl; - - omp_set_num_threads(4); - omp_set_schedule(omp_sched_static, 0); - - // Perform the Reduction on 8 threads - start = MEL::Wtime(); - MEL::Reduce(src, dst, LEN, ompSUM, 0, comm); - end = MEL::Wtime(); - - if (rank == 0) - std::cout << "Reduced " << LEN << " elements in " << std::setw(10) << (end - start) - << " seconds on 4 threads with parallel mapped MEL::FUNCTOR::SUM." << std::endl; - - omp_set_num_threads(8); - omp_set_schedule(omp_sched_static, 0); - - // Perform the Reduction on 8 threads - start = MEL::Wtime(); - MEL::Reduce(src, dst, LEN, ompSUM, 0, comm); - end = MEL::Wtime(); - - if (rank == 0) - std::cout << "Reduced " << LEN << " elements in " << std::setw(10) << (end - start) - << " seconds on 8 threads with parallel mapped MEL::FUNCTOR::SUM." << std::endl; - - omp_set_num_threads(16); - omp_set_schedule(omp_sched_static, 0); - - // Perform the Reduction on 16 threads - start = MEL::Wtime(); - MEL::Reduce(src, dst, LEN, ompSUM, 0, comm); - end = MEL::Wtime(); - - if (rank == 0) - std::cout << "Reduced " << LEN << " elements in " << std::setw(10) << (end - start) - << " seconds on 16 threads with parallel mapped MEL::FUNCTOR::SUM." << std::endl; - - // Clean up the operations when we are done with them - MEL::OpFree(SUM, ompSUM); - // Clean up buffers - MEL::MemFree(src, dst); - - MEL::Finalize(); - return 0; + MEL::Init(argc, argv); + + // Who are we? + MEL::Comm comm = MEL::Comm::WORLD; + const int rank = MEL::CommRank(comm), + size = MEL::CommSize(comm); + + double start, end; + + // Allocate buffers + const int LEN = 1e8; + int *src = MEL::MemAlloc(LEN, 1), // Initilize to 1 + *dst = (rank == 0) ? MEL::MemAlloc(LEN) : nullptr; + + // Perform the Reduction on 1 thread with MPI_SUM + start = MEL::Wtime(); + //MPI_Reduce(src, dst, LEN, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD); + MEL::Reduce(src, dst, LEN, MEL::Op::SUM, 0, comm); + end = MEL::Wtime(); + + if (rank == 0) + std::cout << "Reduced " << LEN << " elements in " << std::setw(10) << (end - start) + << " seconds on 1 thread with MEL::Op::SUM == MPI_SUM." << std::endl; + + // Create a MEL User Defined Operation using a functor wrapped in a map function + auto SUM = MEL::OpCreate(); + + // Perform the Reduction on 1 thread + start = MEL::Wtime(); + MEL::Reduce(src, dst, LEN, SUM, 0, comm); + end = MEL::Wtime(); + + if (rank == 0) + std::cout << "Reduced " << LEN << " elements in " << std::setw(10) << (end - start) + << " seconds on 1 thread with mapped MEL::FUNCTOR::SUM." << std::endl; + + // Create a MEL User Defined Operation using a functor wrapped in a parallel map function + auto ompSUM = MEL::OMP::OpCreate(); + + omp_set_num_threads(2); + omp_set_schedule(omp_sched_static, 0); + + // Perform the Reduction on 4 threads + start = MEL::Wtime(); + MEL::Reduce(src, dst, LEN, ompSUM, 0, comm); + end = MEL::Wtime(); + + if (rank == 0) + std::cout << "Reduced " << LEN << " elements in " << std::setw(10) << (end - start) + << " seconds on 2 threads with parallel mapped MEL::FUNCTOR::SUM." << std::endl; + + omp_set_num_threads(4); + omp_set_schedule(omp_sched_static, 0); + + // Perform the Reduction on 8 threads + start = MEL::Wtime(); + MEL::Reduce(src, dst, LEN, ompSUM, 0, comm); + end = MEL::Wtime(); + + if (rank == 0) + std::cout << "Reduced " << LEN << " elements in " << std::setw(10) << (end - start) + << " seconds on 4 threads with parallel mapped MEL::FUNCTOR::SUM." << std::endl; + + omp_set_num_threads(8); + omp_set_schedule(omp_sched_static, 0); + + // Perform the Reduction on 8 threads + start = MEL::Wtime(); + MEL::Reduce(src, dst, LEN, ompSUM, 0, comm); + end = MEL::Wtime(); + + if (rank == 0) + std::cout << "Reduced " << LEN << " elements in " << std::setw(10) << (end - start) + << " seconds on 8 threads with parallel mapped MEL::FUNCTOR::SUM." << std::endl; + + omp_set_num_threads(16); + omp_set_schedule(omp_sched_static, 0); + + // Perform the Reduction on 16 threads + start = MEL::Wtime(); + MEL::Reduce(src, dst, LEN, ompSUM, 0, comm); + end = MEL::Wtime(); + + if (rank == 0) + std::cout << "Reduced " << LEN << " elements in " << std::setw(10) << (end - start) + << " seconds on 16 threads with parallel mapped MEL::FUNCTOR::SUM." << std::endl; + + // Clean up the operations when we are done with them + MEL::OpFree(SUM, ompSUM); + // Clean up buffers + MEL::MemFree(src, dst); + + MEL::Finalize(); + return 0; } \ No newline at end of file From 428fcc1a877e54c0826fb3cf105bbeb37d6e9577 Mon Sep 17 00:00:00 2001 From: Joss Whittle Date: Fri, 8 Jul 2016 02:24:20 +0100 Subject: [PATCH 2/2] updated example to be more concise --- example-code/ompExample.cpp | 59 ++++++++----------------------------- 1 file changed, 13 insertions(+), 46 deletions(-) diff --git a/example-code/ompExample.cpp b/example-code/ompExample.cpp index ec13a45..c85edd2 100644 --- a/example-code/ompExample.cpp +++ b/example-code/ompExample.cpp @@ -69,53 +69,20 @@ int main(int argc, char *argv []) { // Create a MEL User Defined Operation using a functor wrapped in a parallel map function auto ompSUM = MEL::OMP::OpCreate(); - omp_set_num_threads(2); - omp_set_schedule(omp_sched_static, 0); - - // Perform the Reduction on 4 threads - start = MEL::Wtime(); - MEL::Reduce(src, dst, LEN, ompSUM, 0, comm); - end = MEL::Wtime(); - - if (rank == 0) - std::cout << "Reduced " << LEN << " elements in " << std::setw(10) << (end - start) - << " seconds on 2 threads with parallel mapped MEL::FUNCTOR::SUM." << std::endl; - - omp_set_num_threads(4); - omp_set_schedule(omp_sched_static, 0); - - // Perform the Reduction on 8 threads - start = MEL::Wtime(); - MEL::Reduce(src, dst, LEN, ompSUM, 0, comm); - end = MEL::Wtime(); - - if (rank == 0) - std::cout << "Reduced " << LEN << " elements in " << std::setw(10) << (end - start) - << " seconds on 4 threads with parallel mapped MEL::FUNCTOR::SUM." << std::endl; + // For 2, 4, 8, 16 threads + for (int n = 2; n <= 16; n <<= 1) { + omp_set_num_threads(n); + omp_set_schedule(omp_sched_static, 0); + + // Perform the Reduction on n threads + start = MEL::Wtime(); + MEL::Reduce(src, dst, LEN, ompSUM, 0, comm); + end = MEL::Wtime(); - omp_set_num_threads(8); - omp_set_schedule(omp_sched_static, 0); - - // Perform the Reduction on 8 threads - start = MEL::Wtime(); - MEL::Reduce(src, dst, LEN, ompSUM, 0, comm); - end = MEL::Wtime(); - - if (rank == 0) - std::cout << "Reduced " << LEN << " elements in " << std::setw(10) << (end - start) - << " seconds on 8 threads with parallel mapped MEL::FUNCTOR::SUM." << std::endl; - - omp_set_num_threads(16); - omp_set_schedule(omp_sched_static, 0); - - // Perform the Reduction on 16 threads - start = MEL::Wtime(); - MEL::Reduce(src, dst, LEN, ompSUM, 0, comm); - end = MEL::Wtime(); - - if (rank == 0) - std::cout << "Reduced " << LEN << " elements in " << std::setw(10) << (end - start) - << " seconds on 16 threads with parallel mapped MEL::FUNCTOR::SUM." << std::endl; + if (rank == 0) + std::cout << "Reduced " << LEN << " elements in " << std::setw(10) << (end - start) + << " seconds on " << n << " threads with parallel mapped MEL::FUNCTOR::SUM." << std::endl; + } // Clean up the operations when we are done with them MEL::OpFree(SUM, ompSUM);