Thread Lab

Due Friday, December 4, 2026 @ 11:59pm PT
Work on
lnxsrv15.seas.ucla.edu (connect directly)
Grading server
lnxsrv08: please do not run the lab there; other students' scores depend on it being quiet
Download / scoreboard
lnxsrv08.seas.ucla.edu:18214 (UCLA network or VPN)
Lab TA
Yuyue Wang

Before you start

  • This is an individual assignment. All submitted code must be your own; no code sharing.
  • Edit only histo.c, and keep your directory named target-NUMBER.
  • Use make submit for your official score. Local timing is only a guide.
  • Save your work often.

Introduction

Use pthreads to speed up a histogram while keeping every bucket count correct. For each input value data[i], the program increments hist[data[i] % B]. You will implement two worker functions in histo.c:

Case Input elements Histogram buckets Worker function
1 100,000,000 8 compute_histogram_case1
2 25,000,000 16,000,000 compute_histogram_case2

An eight-bucket histogram: each input value increments the bucket selected by value modulo 8.

The starter creates eight worker threads, but only thread 0 does any counting. Your task is to divide the work effectively without races. The two cases have very different cache behavior, so an optimization that helps one may hurt the other.

Your score combines the speedups of both cases. The starter earns 0 points; 3× earns 100, and 4× earns 105. See Scoring for the formula.

The driver creates and joins the workers. Each worker receives a HistogramArgs containing the shared input and output arrays and its thread ID. Use NTHREADS for the active thread count and MAX_THREADS for the size of global per-thread arrays. NTHREADS is a runtime value, so it cannot be a global array dimension.

Getting Started

Download your lab

On the UCLA network or VPN, open the lab website, choose Request Lab, and enter your UID and UCLA email. Upload the resulting tarball to lnxsrv15; run this command on your own computer:

scp target-YOURNUMBER.tar.gz <yourusername>@lnxsrv15.seas.ucla.edu:~/

You can also download directly from lnxsrv15:

curl -X POST -d "uid=YOURUID&email=YOUREMAIL" \
  -O --remote-header-name \
  "http://lnxsrv08.seas.ucla.edu:18214/get-tar-file"
Connect and unpack

Connect directly to lnxsrv15, including in VS Code Remote-SSH. The general cs33.seas.ucla.edu address may send you to the grading machine.

ssh <yourusername>@lnxsrv15.seas.ucla.edu
tar -xzf target-YOURNUMBER.tar.gz
cd target-YOURNUMBER
Build and check

Edit histo.c, then run:

make
./histo

To run only case 2:

./histo -i 2

Fix correctness failures before submitting.

Interpreting the output

Compare Time(ms) for the same case when trying an optimization. Your final score combines both cases; making one fast is not enough if the other becomes much slower.

Example output and what the numbers mean

These are the best measured times from one three-trial starter submission on the grading server:

Case Elements Buckets Best time (ms) Speedup (rounded)
1 100,000,000 8 146.3 1.4×
2 25,000,000 16,000,000 332.6 1.2×
Geomean Speedup1.28×
Grade0.0
Output Meaning
Time(ms) Measured time for that case, including worker creation and joins. Lower is better.
CPE A time-based estimate of cycles per element using an assumed clock rate.
Speedup Fixed reference time divided by measured time for one case.
Geomean Speedup The geometric mean of the two speedups, calculated before rounding the displayed numbers.
Grade Points for that submission, including any bonus. Maximum: 105.

The server prints a row for each case in every trial, then one summary. Best times can come from different trials. A local run normally uses one trial; ./histo -t 3 runs three.

Submit
make submit

The server shows your queue position, compiles your code, checks both cases and reports your score. The scoreboard shows your best successful result, including any bonus. Put an optional short public comment in comment.txt.

Submission limits

  • One submission per UID may be queued or running at a time.
  • Each UID may submit up to 5 times per rolling hour. An accepted submission counts even if compilation or grading fails.

Thread Count and Cache Experiments

These experiments are optional and have no separate deliverable. Build once, then compare the Time(ms) columns:

./histo --threads 1
./histo --threads 2
./histo --threads 4
./histo --threads 8

Official grading always uses 8 worker threads. Keep your work partitioning and synchronization consistent with NTHREADS; do not change its value.

On a supported machine with PMU access, you can also inspect cache statistics:

./histo --threads 8 --cache-stats

This reports L1D and L2 hit/miss statistics across the workers. Unsupported or unavailable counters produce a message or N/A.

What the cache percentages count
  • L1D uses retired-load hit/miss events. Intel’s L1 hit event also includes locked instructions and software prefetches.
  • L2 uses demand-data-read events, which can include requests from L1D prefetchers.
  • The percentages use different denominators and do not describe all memory traffic.

Counters cover the worker functions, including counting and merging. They exclude input generation and correctness checking.

Cache profiling adds overhead. Turn it off when comparing execution times. Cache statistics do not contribute to your grade.

Optimization Ideas

Approaches to compare

Try different approaches and compare both correctness and speed:

  • Atomic increments, such as __sync_fetch_and_add(&hist[bucket], 1).
  • A shared lock, per-bucket locks, or locks covering ranges of buckets. Consider contention and initialization costs.
  • A private histogram for each thread followed by a merge. Consider false sharing, memory usage, and the cost of merging.
  • Assigning each thread a range of buckets. This avoids simultaneous writes to the same bucket, but may require reading the input more than once.
Private storage and initialization

For case 2, a private histogram occupies about 64 MB, which is too large for a typical thread stack. Global storage is one option:

int private_hists[MAX_THREADS][T2B];

Use init() to initialize your locks and private storage before each trial. The driver measures worker creation, histogram computation and worker joins; init() is outside that timed interval.

Compiler and cache parameters

The grader uses GCC 16.2 with -O3 -ftree-vectorize. Its reported cache sizes are 32 KiB L1 data and 1 MiB L2 per core, with 16.5 MiB shared L3.

Grading and Rules

Scoring

For each case, the speedup is its fixed reference time divided by your measured time. The reference times are 200 ms for case 1 and 400 ms for case 2.

Your overall speedup is the geometric mean:

\[S = \sqrt{\frac{200\text{ ms}}{T_1}\times\frac{400\text{ ms}}{T_2}}\]

The server runs three trials, checks correctness in every trial, and uses the best time for each case. Both cases must be correct. Each successful submission receives:

Overall speedup Grade
At or below 1.35× 0
1.35× to 3× Linear from 0 to 100: 100 × (S − 1.35) / (3 − 1.35)
3× up to, but not including, 4× 100
4× or higher 105

Rules

  1. Submit all your implementation in histo.c. The server supplies its own driver, headers, checker and Makefile; changes to those files do not affect grading.
  2. Use the workers provided by the driver. Do not create additional threads or processes, or change NTHREADS.
  3. Compute the answer from the supplied input on every call. Do not hardcode bucket counts, retain answers from earlier trials, change the input, or tamper with the driver or grading output. Inputs can change between runs.
  4. Do not add external libraries. Use the declarations provided by histo.h and your own helper functions.
  5. No assembly, #pragma, or __builtin_prefetch. The server checks these constructs in the source, including comments.
  6. Do not attack or interfere with the grading server. Work on lnxsrv15, and submit through make submit.