0% found this document useful (0 votes)
49 views

Problem: Compute PI Using A Monte Carlo Approach in C With OpenMP.

The document describes computing Pi using a Monte Carlo simulation approach in C with OpenMP. It tests the code on a Windows 10 system with 4 physical cores and hyperthreading enabled. It runs the simulation with 1 to 16 threads and records the execution times and speedups. The code generates random x and y coordinates, checks if they fall within the unit circle, and uses that to estimate Pi. It outputs the number of trials, estimated Pi value, and execution time in milliseconds.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views

Problem: Compute PI Using A Monte Carlo Approach in C With OpenMP.

The document describes computing Pi using a Monte Carlo simulation approach in C with OpenMP. It tests the code on a Windows 10 system with 4 physical cores and hyperthreading enabled. It runs the simulation with 1 to 16 threads and records the execution times and speedups. The code generates random x and y coordinates, checks if they fall within the unit circle, and uses that to estimate Pi. It outputs the number of trials, estimated Pi value, and execution time in milliseconds.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Problem 1: Compute PI using a Monte Carlo approach in C with OpenMP.

Tested on Windows 10 Home (Latest Edition, x64) using Visual Studio 2019 (Latest Enterprise
Edition). I have 4 physical cores and hyperthreading enable.

M = 4 * 1.3 = 5.2
N = 256 * UINT_MAX = 4294967040

THREADS Execution time [Relative] Speedup [Relative] Efficiency


[milliseconds] S(n) = T(1) / T(n) E(n) = S(n) / M
1 758688.9467 1 0.192307692
2 486709.7779 1.558811804 0.299771501
3 375821.5639 2.018747777 0.388220726
4 321346.4944 2.360968487 0.454032401
5 294979.7082 2.572003855 0.494616126
6 272930.1173 2.779791964 0.534575378
7 250789.1329 3.025206626 0.581770505
8 236104.7323 3.213357646 0.617953393
9 240459.4349 3.155163976 0.606762303
10 242122.4248 3.133493097 0.602594826
11 235698.646 3.218893955 0.619018068
12 226988.841 3.342406364 0.642770455
13 234031.1904 3.241828345 0.623428528
14 245090.5923 3.095544956 0.595297107
15 229756.5315 3.302143107 0.635027521
16 227246.9048 3.338610695 0.642040518

TABLE 1. Performance parameters for


Problem 1
1 #define _CRT_RAND_S
2
3 #include "stdio.h"
4 #include "stdlib.h"
5 #include "time.h"
6 #include <omp.h>
7
8
9
10 void main()
11 {
12 srand(time(NULL));
13
14 long long int N = 256 * UINT_MAX;
15
16 double x, y;
17 unsigned int seed; /* # of points in the 1st quadrant of unit circle */
18 long long int i;
19 long long int M = 0;
20 double z;
21 double pi;
22
23 int nthreads = 16;
24 omp_set_num_threads(nthreads);
25
26 double t0 = omp_get_wtime();
27 M = 0;
28
29 /* initialize random numbers */
30 #pragma omp parallel private(i, x, y, seed) shared(N)
31 {
32 seed = 35791246 + omp_get_thread_num() * 1999;
33 rand_s(&seed);
34
35 #pragma omp for reduction(+:M) schedule(guided)
36
37 for (i = 0; i < N; i++) {
38 x = (double)rand() / RAND_MAX;
39 y = (double)rand() / RAND_MAX;
40 z = x * x + y * y;
41 if (z <= 1) M++;
42 }
43 }
44
45 double t1 = omp_get_wtime();
46
47 pi = (double)M / N * 4;
48 printf("# of trials= %lld , estimate of pi is %g \n", N, pi);
49
50 printf("Time: %f\n", (t1 - t0) * 1000);
51 }

You might also like