Skip to content

Commit

Permalink
Merge pull request #15 from L2Program/master
Browse files Browse the repository at this point in the history
OMP Functor improvements and benchmark example
  • Loading branch information
JossWhittle authored Jul 8, 2016
2 parents e7c8fd3 + d9ac35c commit ae03b76
Show file tree
Hide file tree
Showing 2 changed files with 134 additions and 21 deletions.
29 changes: 8 additions & 21 deletions MEL_omp.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,6 @@ namespace MEL {
* Extensions to MEL that leverage OpenMP for within node paralellism
*/

/**
* \ingroup OMP
*/
enum Schedule : int {
STATIC = omp_sched_static,
DYNAMIC = omp_sched_dynamic,
GUIDED = omp_sched_guided,
AUTO = omp_sched_auto
};

namespace Functor {

/**
Expand All @@ -59,10 +49,9 @@ namespace MEL {
* \param[in] len Pointer to a single int representing the number of elements to be processed
* \param[in] dptr Pointer to a single derived datatype representing the data to be processed
*/
template<int NUM_THREADS, int CHUNK, Schedule SCHEDULE, typename T, T(*F)(T&, T&)>
template<typename T, T(*F)(T&, T&)>
void ARRAY_OP_FUNC(T *in, T *inout, int *len, MPI_Datatype *dptr) {
omp_set_schedule((omp_sched_t) SCHEDULE, CHUNK);
#pragma omp parallel for num_threads(NUM_THREADS) schedule(dynamic)
#pragma omp parallel for
for (int i = 0; i < *len; ++i)
inout[i] = F(in[i], inout[i]);
};
Expand All @@ -76,10 +65,9 @@ namespace MEL {
* \param[in] len Pointer to a single int representing the number of elements to be processed
* \param[in] dptr Pointer to a single derived datatype representing the data to be processed
*/
template<int NUM_THREADS, int CHUNK, Schedule SCHEDULE, typename T, T(*F)(T&, T&, MEL::Datatype)>
template<typename T, T(*F)(T&, T&, MEL::Datatype)>
void ARRAY_OP_FUNC(T *in, T *inout, int *len, MPI_Datatype *dptr) {
omp_set_schedule((omp_sched_t) SCHEDULE, CHUNK);
#pragma omp parallel for num_threads(NUM_THREADS) schedule(dynamic)
#pragma omp parallel for
for (int i = 0; i < *len; ++i)
inout[i] = F(in[i], inout[i], dt);
};
Expand All @@ -94,10 +82,10 @@ namespace MEL {
* \param[in] commute Is the operation commutative
* \return Returns a handle to a new Op
*/
template<int NUM_THREADS, int CHUNK, Schedule SCHEDULE, typename T, T(*F)(T&, T&)>
template<typename T, T(*F)(T&, T&)>
inline MEL::Op OpCreate(bool commute = true) {
MPI_Op op;
MEL_THROW( MPI_Op_create((void(*)(void*, void*, int*, MPI_Datatype*)) MEL::OMP::Functor::ARRAY_OP_FUNC<NUM_THREADS, CHUNK, SCHEDULE, T, F>, commute, (MPI_Op*) &op), "OMP::Op::CreatOp" );
MEL_THROW( MPI_Op_create((void(*)(void*, void*, int*, MPI_Datatype*)) MEL::OMP::Functor::ARRAY_OP_FUNC<T, F>, commute, (MPI_Op*) &op), "OMP::Op::CreatOp" );
return MEL::Op(op);
};

Expand All @@ -110,12 +98,11 @@ namespace MEL {
* \param[in] commute Is the operation commutative
* \return Returns a handle to a new Op
*/
template<int NUM_THREADS, int CHUNK, Schedule SCHEDULE, typename T, T(*F)(T&, T&, MEL::Datatype)>
template<typename T, T(*F)(T&, T&, MEL::Datatype)>
inline MEL::Op OpCreate(bool commute = true) {
MPI_Op op;
MEL_THROW( MPI_Op_create((void(*)(void*, void*, int*, MPI_Datatype*)) MEL::OMP::Functor::ARRAY_OP_FUNC<NUM_THREADS, CHUNK, SCHEDULE, T, F>, commute, (MPI_Op*) &op), "OMP::Op::CreatOp" );
MEL_THROW( MPI_Op_create((void(*)(void*, void*, int*, MPI_Datatype*)) MEL::OMP::Functor::ARRAY_OP_FUNC<T, F>, commute, (MPI_Op*) &op), "OMP::Op::CreatOp" );
return MEL::Op(op);
};

};
};
126 changes: 126 additions & 0 deletions example-code/ompExample.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
/*
The MIT License(MIT)
Copyright(c) 2016 Joss Whittle
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files(the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions :
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#define MEL_IMPLEMENTATION
#include "MEL_omp.hpp"

#include <iomanip>

//-----------------------------------------------------------------//
// 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<int>(LEN, 1), // Initilize to 1
*dst = (rank == 0) ? MEL::MemAlloc<int>(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<int, MEL::Functor::SUM>();

// 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<int, MEL::Functor::SUM>();

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;
}

0 comments on commit ae03b76

Please sign in to comment.