Hone logo
Hone
Problems

Concurrent Summation with Thread Spawning in Rust

This challenge focuses on utilizing Rust's threading capabilities to efficiently calculate the sum of a large vector of numbers. The goal is to divide the vector into chunks, process each chunk in a separate thread, and then combine the results to obtain the final sum. This demonstrates a practical application of thread spawning for parallel processing.

Problem Description

You are tasked with writing a Rust program that calculates the sum of a given vector of i32 integers using multiple threads. The program should:

  1. Divide the input vector into n chunks. Each chunk will be processed by a separate thread.
  2. Spawn n threads. Each thread will receive a chunk of the vector and calculate the sum of the elements within that chunk.
  3. Collect the results from each thread. The main thread should wait for all spawned threads to complete and then sum the partial sums returned by each thread.
  4. Return the total sum. The program should return the final sum of all elements in the input vector.

Key Requirements:

  • The number of threads (n) should be configurable.
  • The vector size should be large enough to demonstrate the benefits of parallel processing (at least 10,000 elements).
  • Error handling is not required for this challenge; assume the input vector is valid.
  • Use std::thread for thread spawning.
  • Use std::sync::mpsc for communication between threads (specifically, to send the partial sums back to the main thread).

Expected Behavior:

The program should efficiently calculate the sum of the vector by leveraging multiple threads. The final sum should be accurate, and the program should terminate gracefully after all threads have completed.

Edge Cases to Consider:

  • Empty Vector: If the input vector is empty, the program should return 0.
  • n greater than vector size: If the number of threads requested (n) is greater than the size of the vector, each element should be processed by a separate thread.
  • n equals 1: If n is 1, the program should effectively run in a single thread.

Examples

Example 1:

Input: vector = [1, 2, 3, 4, 5], n = 2
Output: 15
Explanation: The vector is divided into two chunks: [1, 2] and [3, 4, 5]. Thread 1 calculates 1 + 2 = 3. Thread 2 calculates 3 + 4 + 5 = 12. The main thread sums 3 + 12 = 15.

Example 2:

Input: vector = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], n = 5
Output: 55
Explanation: The vector is divided into five chunks: [1], [2], [3], [4], [5, 6, 7, 8, 9, 10]. Each thread calculates the sum of its chunk. The main thread sums the partial sums to get the final result.

Example 3: (Edge Case - Empty Vector)

Input: vector = [], n = 3
Output: 0
Explanation: The vector is empty, so the sum is 0.

Constraints

  • The input vector will contain only i32 integers.
  • The vector size will be between 0 and 100,000 elements (inclusive).
  • The number of threads (n) will be between 1 and 100 (inclusive).
  • The program should complete within 5 seconds for a vector of size 100,000 and n = 10. (This is a soft constraint; the primary goal is correctness.)

Notes

  • Consider using a channel (std::sync::mpsc::channel) to communicate the partial sums from the worker threads back to the main thread.
  • The join() method on a std::thread::JoinHandle is crucial for waiting for a thread to complete before accessing its result.
  • Think about how to efficiently divide the vector into chunks of roughly equal size. You can use chunk_size = vector.len() / n; as a starting point, but handle the remainder appropriately.
  • Rust's ownership and borrowing rules are important to consider when passing data to threads. Ensure that data is moved or cloned appropriately to avoid errors.
Loading editor...
rust