96
edits
Changes
→Progress
==== Calculation of Pi ====
For this assessment, we used code found at [https://helloacm.com/cc-coding-exercise-finding-approximation-of-pi-using-monto-carlo-algorithm/ helloacm.com]
<syntaxhighlight lang="cpp">
int main() {
srand(time(NULL));
cout.precision(10);
std::chrono::steady_clock::time_point ts, te;
const double N[] = {1e1,1e3, 1e4, 1e5, 1e6, 1e7, 1e8};
for (int j = 0; j < (sizeof(N) / sizeof(N[0])); j ++) {
ts = std::chrono::steady_clock::now();
int circle = 0;
for (int i = 0; i < N[j]; i ++) {
double x = static_cast<double>(rand()) / static_cast<double>(RAND_MAX);
double y = static_cast<double>(rand()) / static_cast<double>(RAND_MAX);
if (x * x + y * y <= 1.0) circle ++;
}
te = std::chrono::steady_clock::now();
cout << N[j] << (char)9 << (char)9 << (double)circle / N[j] * 4 ;
reportTime("", te - ts);
}
return 0;
}
</syntaxhighlight>
In this version, the value of PI is calculated using the Monte Carlo method.
This method states:
100000000 3.1419176 - took - 10035 millisecs
''The first column represents the "stride" or the number of digits of pi points we are calculating to.generating
With this method, we can see that the accuracy of the calculation is slightly off. This is due to the randomization of points within the circle. Running the program again will give us slightly different results.
'''Parallelizing
In order to parallelize the code from above, we decided to use a kernel to handle the calculations.
The logic largely remains the same , but we offload the results CPU calculations to the GPU. <br>This code generates random points within the kernel and the calculations are much fasteralso done in here.<br>
<br>
Offloading to the GPU results in a pi calculation time to be reduced
<br>
<br>
'''Kernel code used
}
</syntaxhighlight>
<br>
[http://docs.nvidia.com/cuda/curand/index.html cuRAND] documentation.
<br>
'''Results CPU vs GPU
<br>
[[File:Cpuvsgpusheet.PNG|600px]]
<br>
<br>
[[File:Cpuvsgpu.png|600px]]
<br>
As we can see above, the more iterations, the more accurate the calculation of PI. <br>
The CPU's results drastically change as we increase the iteration 10x. <br>
However, the parallelized results seem to stay accurate throughout the iterations. <br>
It seems as though the calculation time doesn't change much and stays consistent. <br> [[File:Cudamalloc.PNG|800px]] <br>[[File:Prof.PNG]] <br> Profiling the code shows that '''cudaMalloc''' takes up most of the time spent. Even when <br>there are 10 iterations, the time remains at 300 milliseconds. <br>As the iteration passes 100 25 million, we have a bit of memory leaks leak which results in inaccurate results. <br><br> In order to optimize the code, we must find a way reduce the time cudaMalloc takes. <br>
=== Assignment 3 ===
cudaMallocHost((void **)&host, size);
</syntaxhighlight>
<br/>
Here we can see where an error occurs, we suspect that a memory leak causes the problem resulting in an error in pi calculation
'''Optimized time run results
<br>
[[File:Chart3.PNG]]<br>
[[File:Chartp3.PNG]]<br>