linux-sysprog · beginner · ~15 min

Ideal parallel runtime

Estimate perfect-scaling runtime.

Challenge

Under perfect scaling, splitting serial work across N threads cuts the runtime to roughly serial/N (rounded up). Estimate that ideal time.

Task

Implement long ideal_runtime(long serial_ms, int threads) that returns the perfectly-scaled runtime.

Input

  • serial_ms: the single-threaded runtime in milliseconds.
  • threads: number of threads.

Output

ceil(serial_ms / threads), or serial_ms unchanged if threads <= 1.

Example

ideal_runtime(1000, 4)   ->   250
ideal_runtime(1000, 3)   ->   334   (ceiling)
ideal_runtime(500, 1)    ->   500

Edge cases

  • threads <= 1: return serial_ms unchanged.

Input format

serial_ms: single-threaded runtime; threads: thread count.

Output format

Ceiling of serial_ms / threads, or serial_ms if threads <= 1.

Constraints

threads <= 1 returns serial_ms unchanged.

Starter code

long ideal_runtime(long serial_ms, int threads) {
    /* TODO */
    return serial_ms;
}

Solve this exercise in the browser editor — compile and run against the test harness, no setup required.