DmtcpCppDemo.cpp
// DmtcpCppDemo.cpp
#include
#include
#include
#include
#include "omp.h"
#include "unistd.h"
int main()
{
// Get the userID
const char* env_user = std::getenv("USER");
if(!env_user)
{
std::cerr << "Error: $USER variable is not set." << std::endl;
std::exit(1);
}
// Open the output file
std::ofstream outfile;
std::string fname = std::string("/scratch/")+env_user+"/DmtcpCppDemo.txt";
std::cout << "Writing output to " << fname << std::endl;
outfile.open(fname.c_str(), std::ios::trunc);
omp_set_num_threads(4);
int x=0;
int count=1;
while(count < 101)
{
std::cout << "==========================================" << std::endl;
#pragma omp parallel
{
int id = omp_get_thread_num();
int total = omp_get_num_threads();
std::cout << "Incrementing x from process " << id << std::endl;
#pragma omp atomic
x = x + id*10;
//std::cout << "x = " << x << std::endl;
fflush(stdout);
}
std::cout << "\n current value of count: " << count << std::endl;
sleep(1);
count++;
}
std::cout << "parallel execution ends & x= " << x << std::endl;
outfile << "The final value of x: " << x << std::endl;
outfile << "The expected value of x is 6000 if the while loop" << std::endl;
outfile << "runs 100 times with loop variable 1:101 "<< std::endl;
outfile.close();
return 0;
}
To compile DmtcpCppDemo.cpp, use the following commands:
module load intel/ps_xe
module load intel-mpi/64/5.1.2/150
mpiicpc -fopenmp DmtcpCppDemo.cpp -o DmtcpCppDemo