$39
Purpose: Become more familiar with operating system interaction, threading, and race conditions.
Points: 100 10 – Header and comments, 45 – Program, and 45 – Write-up
Scoring will include functionality, documentation, coding style, and write-up
Assignment:
In recreational number theory, a Happy Numbers1 is a number defined by the following process: starting with any positive integer, replace the number by the sum of the squares of its digits, and repeat the process until the number either equals 1 (where it will stay), or it loops endlessly in a cycle
that does not include 1. Those numbers for which this process ends in 1 are referred to as happy numbers, while those that do not end in 1 are unhappy numbers (or sad numbers).
For example, 19 is happy, as the associated sequence is:
12+92 = 82
82+22= 68
62+82= 100
12+02+02= 1
It turns out that all unhappy numbers have a 4 in the endless sequence. This allows us to stop when the process results in a 4.
Write an assembly language program
to find the count of happy and sad numbers between 1 and some user provided limit. In order to improve performance, the program should use threads to perform some of the computations in parallel. The program should read the thread count option and number limit in base 13 from the command line in the following format; “./happyNums -t<1|2|3|4> -lm <base13Number>”. For example:
./happyNums -t4 -lm 41B3427
The provided C++ main program calls the following routines:
•
•
A value returning function, getCommandLineArgs(), that reads and verifies the command line arguments. This includes error and range checking based on provided parameters (in the template). The error strings are predefined in the template. The function should call the base13toint() function (you can change the name of this function).
A void function, findHappyNumbers() , that will be called as a thread function and determine of a number of happy or sad and update a global counter (for each). The thread must perform updates to the global variables in a critical section to avoid race conditions. See below sections for additional detail.
A main program and a functions template will be provided. All functions must be in a separate source file, a11procs.asm, that is independently assembled. Only the functions file, not the provided main, will be submitted on-line. As such, you must not change the provided main! You may use static variables as needed (no requirement to create stack-dynamic locals).
1 For more information, refer to: https://en.wikipedia.org/wiki/Happy_number
Submission:
Program Header Block
All source files must include your name, section number, assignment, NSHE number, and program description. The required format is as follows:
Failure to include your name in this format will result in a loss of up to 3%.
Scoring Rubric
Scoring will include functionality, code quality, and documentation. Below is a summary of the scoring rubric for this assignment.
Thread Function:
Create a thread function, findHappyNumbers(), that performs the following operations:
When obtaining the next group of numbers and updating (+COUNT_SET) the idxCounter variable, must be performed as a critical section (i.e., locked and unlocked using the provided spinLock() and spinUnlock() functions). As the numbers are being checked, no locks are needed. Once a number has been determined to be happy or sad, the appropriate counter should locked (via lock prefix) and incremented accordingly. For example:
lock inc qword [happyCount]
It is recommended to complete and test the program initially with only the single sequential thread before testing the parallel threads. In order to debug, you may wish to temporarily insert a direct call (non-threaded) to the findHappyNumbers() function.
Results Write-Up
When the program is working, complete additional timing and testing actions as follows;
SpeedUp = ExecTimesequential
ExecTime parallel
The explanation part of the write-up (not including the output and timing data) should be less than ~300 words. Overly long explanations will be not be scored.
2 For more information, refer to: https://en.wikipedia.org/wiki/Speedup
Below is an example of the type of chart to include
CS 218 - Assignment #12
Times (seconds)
Execution Time vs Thread Count
600
500
400
300
200
100
0
1 2 3 4
Threads
Such graphs are most easily done using a spreadsheet. An optional example spreadsheet is provided for reference.
Assignment #12 Timing Script
In order to obtain the times for the write-up a timing script is provided. After you download the script file, a12timer, set the execute permission as follows:
ed-vm$ chmod +x a12timer
ed-vm$ ./a12timer happyNums
The script file will expect the name of the executable as an argument (as shown).
The script may take a while to execute, but no interaction is required. The script will create an output file, a12times.txt, which will be included in the submission and the data be used create the write-up.
Example Execution:
The following is an example execution for the sequential version:
ed-vm% ./happyNums
Usgae: ./happyNums -t<1|2|3|4> -lm <base13Number> ed-vm%
ed-vm% ./happyNums -t 2 -lm 836851
Error, invalid command line options.
ed-vm%
ed-vm% ./happyNums -t2 -lim 836851
Error, invalid limit specifier.
ed-vm%
ed-vm% ./happyNums -ttwo -lm 836851
Error, invalid thread count specifier.
ed-vm%
ed-vm% ./happyNums -t4 -lm 83x6851
Error, limit invalid.
ed-vm%
ed-vm%
ed-vm% ./happyNums -t4 -lm 8396851
------------------------------------------------------------
CS 218 - Assignment #12
Happy/Sad Numbers Program
Hardware Cores: 12
Thread Count: 4
Numbers Limit: 40000000
Start Counting...
...Thread starting...
...Thread starting...
...Thread starting...
...Thread starting...
Results:
--------
Happy Count: 5577647
Sad Count: 34422353
ed-vm%