Pi day in parallel!

In an earlier activity, you were able to approximate $\pi$ to two or three decimal places; however, it took quite a while for the computer to get to that level of precision.

We are going to try to find a faster way to estimate and, at the same time, reveal how many core processors our computer has.

Imagine you are a customer at a trucking company and you have a large number of boxes that need to be moved. If you hire one worker, then the one worker could take a long time moving the boxes. If you hire two workers, they can divide the number of boxes to move between them and it would take less time.

If you wanted to venture a guess, what would be the relationship between the length of time it takes two workers to complete the job compared to one worker?

What about three workers to one worker?

Clearly the more workers we have working on the job, the faster the job should get done.

Step 0: Open the code

Click here to open up the code

Step 1. Look at the code in worker.js

Click on the > in the upper left corner to open up the Sketch Files and click on worker.js

Here is the code you will find

radius = 250;

function random(_min, _max) {
 return  (_max - _min)*Math.random() + _min ;
}

this.onmessage = function(e) {
    var sum, n;
    sum = 0;
    for (n = 0; n < e.data.count; ++n) {
	x = random(-radius,radius);
	y = random(-radius,radius);
        if (x*x + y*y < radius*radius) {
	    sum += 1;
	}
    }
    this.postMessage({id: e.data.id, sum: sum});
};

This code is similar to the original Pi day activity. If you completed that activity, how does this code compare to the solution to the earlier activity? Explain in your own words.

Step 2. Look at the first few lines of code

Click on the sketch.js file to go back to the main page. Let’s look at the first few lines of code.

What do you think each line represents? (Hint: N = number of dots)

Notice that line 3 states “Assume four cores.” We are assuming that the number of core processors on a particular computer is 4. Some have more, some have less. We are going to figure out how many core processors there are on your device. Be sure to close out all other tabs and windows before starting as this may alter your results.

Step 3. Change the number of workers from 4 to 1

Start by changing the number of workers from 4 to 1. Click the play button to run the program. You should see in the preview “Initializing 1 workers Worker # 1 is initialized and running” After some time, the preview will show something similar to what is shown below

Note there are two different "Time elapsed" at the end. We are more interested in the last one (measured in seconds) and the number of workers being used.

Step 4. Increase the number of workers

We are going to run the program a number of times, each time with a different number of workers.

Use the table below and fill in the number of workers (start with 1 and increment by 1) and the length of time (round to 3 decimal places). Keep increasing the number of workers until you fill the entire table. What happens as you increase the number of workers?

Number of Workers Time Elasped
1
2
3
4
 
 
 
 
 
 

Step 5. Make a graph!

Now open either a Google Sheets or Microsoft Excel.

Input the above table in the first two columns and create a line graph of your data

Step 6. Make sense of the graph

Look at the graph and think about the trucking company where there are boxes that need to be moved.

At what point does it no longer become useful to increase the number of workers?

What is an analogy for a trucking company trying to move boxes when the number of workers is large?

Advice: Remember that the "number of workers" in the program was actually a count of how many processing cores we were trying to use.

What does the graph tell you about the number of cores your processor has?

Optional Step 7. Try it on another device!

Take your program and run it on another device. It can be another computer, a phone, a tablet or iPad or even a game system (assuming you can open a browser).

Here is a version of the program that might work more easily with phones and tablets because it has a slider for the number of workers

Fill in the table and graph the results as before. Is there a different number of workers where the "Time Elapsed" no longer increases? What does that mean about that device?

Optional Step 8. What else can you measure? What can you learn about your device

See if you can find a performance monitor on your device that can show how many cores are active. Or if you can't find a performance monitor, try to figure out the CPU name and then do a google search to figure out how many cores it has. Is the number of cores consistent with your results?

How does running with more workers affect the battery of your device? Some devices can predict how many minutes of power remaining before it needs to plug in. Does the number of minutes remaining get smaller when you run with many workers?

Do you get different results based on whether the device is plugged in or not? Some devices have higher performance when it is plugged in and slower performance when it is on battery power.