代做DTS202TC Foundation of Parallel Computing Lab 7: Profiling, Shared-Memory Programming with Pthread
- 首页 >> C/C++编程DTS202TC Foundation of Parallel Computing
Lab 7: Profiling, Shared-Memory Programming with Pthreads
Task 1: Pthreads Hello World
Type the following source code manually (no copy and paste) in a pthhello.c file.
Compile the code like an ordinary C program, with linking in the Pthreads library:
1 gcc -g -Wall -o pth_hello pth_hello.c -lpthread
Run the program:
1 ./pth_hello <number of threads >
Task 2: Monte-Carlo Estimation with Pthreads
Suppose we toss darts randomly at a square dartboard, whose bullseye is at the origin, and whose sides are 2 feet in length. Suppose also that there’s a circle inscribed in the square dartboard. The radius of the circle is 1 foot, and it’s area is π square feet. If the points that are hit by the darts are uniformly distributed (and we always hit the square), then the number of darts that hit inside the circle should approximately satisfy the equation
(1)
This is called a “Monte Carlo” method, since it uses randomness (the dart tosses), more about Monte-Carlo method can be found at https://en.wikipedia.org/wiki/Monte_Carlo_method. Instead of estimating π, write a Pthreads program that uses a Monte Carlo method to estimate the shaded area in Figure 1. The main thread should read in the total number of tosses and print the estimate. You may want to use long long ints for the number of hits and the number of tosses, since both may have to be very large to get a reasonable estimation.
Figure 1: Estimate the shaded area
Task 3 (Optional)
Ask AI for more Pthreads practices if you have spare time.
