Skip to content

Instantly share code, notes, and snippets.

@michaelilyin
Last active November 21, 2016 20:06
Show Gist options
  • Save michaelilyin/b7874a3747e75c3784ab85253e67b5fc to your computer and use it in GitHub Desktop.
Save michaelilyin/b7874a3747e75c3784ab85253e67b5fc to your computer and use it in GitHub Desktop.
OMP-test
cmake_minimum_required(VERSION 3.6)
project(test-app)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -openmp -fopenmp")
set(SOURCE_FILES main.cpp easylogging++.h)
add_executable(test-app ${SOURCE_FILES})
#include <iostream>
#include <omp.h>
#include "easylogging++.h"
using namespace std;
INITIALIZE_EASYLOGGINGPP
void test_areas() {
LOG(INFO) << "test_areas";
#pragma omp parallel num_threads(2)
LOG(INFO) << "test area";
}
void test_private() {
LOG(INFO) << "test_private";
int n = 123;
LOG(INFO) << "n before parallel: " << n;
#pragma omp parallel num_threads(2) private(n)
{
LOG(INFO) << "n in parallel (begin): " << n;
n = omp_get_thread_num();
LOG(INFO) << "n in parallel (end): " << n;
}
LOG(INFO) << "n after parallel: " << n;
}
void test_first_private() {
LOG(INFO) << "test_first_private";
int n = 123;
LOG(INFO) << "n before private: " << n;
#pragma omp parallel num_threads(2) firstprivate(n)
{
LOG(INFO) << "n in parallel (begin): " << n;
n = omp_get_thread_num();
LOG(INFO) << "n in parallel (end): " << n;
}
LOG(INFO) << "n after parallel: " << n;
}
void test_reduction() {
LOG(INFO) << "test_reduction";
int count = 0;
#pragma omp parallel num_threads(2) reduction(+:count)
{
count++;
LOG(INFO) << "current count: " << count;
}
LOG(INFO) << "total count: " << count;
}
void test_max_values() {
LOG(INFO) << "test_max_values";
LOG(INFO) << "max threads: " << omp_get_max_threads();
LOG(INFO) << "max processors: " << omp_get_num_procs();
}
void test_single_copyprivate() {
LOG(INFO) << "test_single_copy_private";
int n;
#pragma omp parallel num_threads(2) private(n)
{
n = omp_get_thread_num();
LOG(INFO) << "n value (begin): " << n;
#pragma omp single copyprivate(n)
{
n = 100;
}
LOG(INFO) << "n value (end): " << n;
}
}
void test_for() {
LOG(INFO) << "test_for";
int A[10], B[10], C[10], i, n;
for (i = 0; i < 10; ++i) {
A[i] = i;
B[i] = 2 * i;
C[i] = 0;
}
#pragma omp parallel num_threads(2) shared(A, B, C) private(i, n)
{
n = omp_get_thread_num();
#pragma omp for
for (i = 0; i < 10; i++) {
C[i] = A[i] + B[i];
LOG(INFO) << "Thread " << n << " was added elements " << i;
}
}
}
int main(int argc, char* argv[]) {
START_EASYLOGGINGPP(argc, argv);
LOG(INFO) << "Starting";
#ifdef _OPENMP
LOG(INFO) << "OpenMP is supported";
#endif
double start_time = omp_get_wtime();
test_areas();
test_private();
test_first_private();
test_reduction();
test_max_values();
test_single_copyprivate();
test_for();
double end_time = omp_get_wtime();
double tick = omp_get_wtick();
LOG(INFO) << "time: " << end_time - start_time;
LOG(INFO) << "accuracy: " << tick;
LOG(INFO) << "Quit";
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment