Skip to content

Commit

Permalink
Merge pull request #16 from L2Program/master
Browse files Browse the repository at this point in the history
Master
  • Loading branch information
JossWhittle authored Jul 8, 2016
2 parents ae03b76 + 428fcc1 commit baf4243
Showing 1 changed file with 61 additions and 93 deletions.
154 changes: 61 additions & 93 deletions example-code/ompExample.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,97 +30,65 @@ 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<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;
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();
//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<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>();

// 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();

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);
// Clean up buffers
MEL::MemFree(src, dst);

MEL::Finalize();
return 0;
}

0 comments on commit baf4243

Please sign in to comment.