Last active
June 19, 2018 17:52
-
-
Save MareArts/c2b1c9c3914ac9fdb1334255a7db4d1f to your computer and use it in GitHub Desktop.
OpenMP test code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
http://study.marearts.com/2017/05/openmp-test-on-visual-studio.html | |
#include <stdio.h> | |
#include <time.h> | |
#include <omp.h> //for omp_get_thread_num() | |
void main() | |
{ | |
///////////////////////////////////////////////////////// | |
printf("zero case : OpenMP Test (max thread:%d)\n ", omp_get_max_threads() ); | |
int i; | |
omp_set_num_threads(4); //Set Thread Number | |
#pragma omp parallel for | |
for (i = 0; i < 8; i++) | |
{ | |
printf("%d Hello, World! Thread ID: %d\n", i, | |
omp_get_thread_num()); //thread id | |
} | |
///////////////////////////////////////////////////////// | |
printf("\nfirst case : no threading! \n "); | |
double pi = 0.0; | |
const int iterationCount = 200000000; | |
clock_t startTime = clock(); | |
for (int i = 0; i < iterationCount; i++) | |
{ | |
pi += 4 * (i % 2 ? -1 : 1) / (2.0 * i + 1.0); | |
} | |
printf("Elpase Time : %.3lf sec \n", (clock() - startTime) / 1000.); | |
printf("pi = %.8f\n", pi); | |
///////////////////////////////////////////////////////// | |
/////////////////////////////////////////////////////////// | |
printf("\nsecond case : OpenMP - threading! \n "); | |
pi = 0.0; | |
startTime = clock(); | |
#pragma omp parallel for | |
for (int i = 0; i < iterationCount; i++) | |
{ | |
pi += 4 * (i % 2 ? -1 : 1) / (2.0 * i + 1.0); | |
} | |
printf("Elpase Time : %.3lf sec \n", (clock() - startTime) / 1000.); | |
printf("pi = %.8f\n", pi); | |
///////////////////////////////////////////////////////// | |
///////////////////////////////////////////////////////// | |
printf("\nthird case : OpenMP - reduction set\n"); | |
pi = 0.0; | |
startTime = clock(); | |
//#pragma omp parallel num_threads(40) | |
//{ | |
#pragma omp parallel for reduction(+:pi) //num_threads( omp_get_max_threads() ) | |
for (int i = 0; i < iterationCount; i++) | |
{ | |
pi += 4 * (i % 2 ? -1 : 1) / (2.0 * i + 1.0); | |
} | |
//} | |
printf("Elpase Time : %.3lf sec \n", (clock() - startTime) / 1000.); | |
printf("pi = %.8f\n", pi); | |
///////////////////////////////////////////////////////// | |
///////////////////////////////////////////////////////// | |
printf("\nthird case : OpenMP - critical set\n"); | |
pi = 0.0; | |
startTime = clock(); | |
#pragma omp parallel | |
{ | |
#pragma omp for | |
for (int i = 0; i < iterationCount; i++) | |
{ | |
#pragma omp critical | |
{ | |
pi += 4 * (i % 2 ? -1 : 1) / (2.0 * i + 1.0); | |
} | |
} | |
} | |
printf("Elpase Time : %.3lf sec \n", (clock() - startTime) / 1000.); | |
printf("pi = %.8f\n", pi); | |
///////////////////////////////////////////////////////// | |
///////////////////////////////////////////////////////// | |
printf("\nfourth case : OpenMP - private and reduction test\n"); | |
int num_steps = 200000000; | |
double x, step, sum = 0.0; | |
step = 1.0 / (double)num_steps; | |
startTime = clock(); | |
#pragma omp parallel for private(x) reduction(+:sum) | |
for (i = 0; i<num_steps; i++) { | |
x = (i + 0.5) * step; | |
sum += 4.0 / (1.0 + x*x); | |
} | |
printf("Elpase Time : %.3lf sec \n", (clock() - startTime) / 1000.); | |
printf("PI = %.8f (sum = %.8f)\n", step*sum, sum); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
http://study.marearts.com/2017/05/openmp-test-on-visual-studio.html