Skip to content

Commit

Permalink
Remove solutions
Browse files Browse the repository at this point in the history
  • Loading branch information
LukeMathWalker committed Jan 15, 2025
1 parent cc78795 commit bbeec3f
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,7 @@
# Relevant links:
# - https://docs.python.org/3/library/multiprocessing.html
def word_count(text: str, n_processes: int) -> int:
result_queue = Queue()
processes = []
for chunk in split_into_chunks(text, n_processes):
p = Process(target=word_count_task, args=(chunk, result_queue))
p.start()
processes.append(p)
for p in processes:
p.join()
results = [result_queue.get() for _ in range(len(processes))]
return sum(results)
pass


# Compute the number of words in `text` and push the result into `result_queue`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,7 @@
# - https://docs.python.org/3/library/threading.html
# - https://docs.python.org/3/library/queue.html
def word_count(text: str, n_threads: int) -> int:
result_queue = Queue()
threads = []

for chunk in split_into_chunks(text, n_threads):
t = Thread(target=word_count_task, args=(chunk, result_queue))
t.start()
threads.append(t)

for t in threads:
t.join()

results = [result_queue.get() for _ in range(len(threads))]
return sum(results)
pass


# Compute the number of words in `text` and push the result into `result_queue`.
Expand Down
17 changes: 1 addition & 16 deletions exercises/03_concurrency/02_gil/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,7 @@ fn word_count(text: Bound<'_, PyString>, n_threads: usize) -> PyResult<usize> {
// directly as an argument, to avoid an extra copy of the string
let text = text.to_str()?;

let chunks = split_into_chunks(text, n_threads);
let mut count = 0;

std::thread::scope(|scope| {
let mut handles = Vec::with_capacity(n_threads);
for chunk in chunks {
let handle = scope.spawn(move || word_count_chunk(chunk));
handles.push(handle);
}

for handle in handles {
count += handle.join().unwrap();
}
});

Ok(count)
todo!()
}

/// Count words in a single chunk of text.
Expand Down
20 changes: 9 additions & 11 deletions exercises/03_concurrency/03_releasing_the_gil/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,16 @@ use pyo3::prelude::*;

#[pyfunction]
// Modify this function to release the GIL while computing the nth prime number.
fn nth_prime(python: Python<'_>, n: u64) -> u64 {
python.allow_threads(|| {
let mut count = 0;
let mut num = 2; // Start checking primes from 2
while count < n {
if is_prime(num) {
count += 1;
}
num += 1;
fn nth_prime(n: u64) -> u64 {
let mut count = 0;
let mut num = 2; // Start checking primes from 2
while count < n {
if is_prime(num) {
count += 1;
}
num - 1 // Subtract 1 because we increment after finding the nth prime
})
num += 1;
}
num - 1 // Subtract 1 because we increment after finding the nth prime
}

fn is_prime(n: u64) -> bool {
Expand Down
31 changes: 13 additions & 18 deletions exercises/03_concurrency/04_minimize_gil_locking/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,35 +1,30 @@
use std::collections::HashMap;

use primes::factors_uniq;
use pyo3::{
prelude::*,
types::{IntoPyDict, PyDict, PyList},
types::{PyDict, PyList},
};
use rayon::prelude::*;

#[pyfunction]
// You're given a Python list of non-negative numbers.
// You need to return a Python dictionary where the keys are the numbers in the list and the values
// are the unique prime factors of each number, sorted in ascending order.
//
// Constraints:
// - Don't hold the GIL while computing the prime factors
// # Resources
//
// You can use `factors_uniq` from the `primes` crate to compute the prime factors of a number.
//
// # Constraints
//
// Don't hold the GIL while computing the prime factors
//
// # Fun additional challenge
//
// Fun additional challenge:
// - Can you use multiple threads to parallelize the computation?
// Consider using `rayon` to make it easier.
// Can you use multiple threads to parallelize the computation?
// Consider using `rayon` to make it easier.
fn compute_prime_factors<'python>(
python: Python<'python>,
numbers: Bound<'python, PyList>,
) -> PyResult<Bound<'python, PyDict>> {
let inputs: Vec<u64> = numbers.extract()?;
let m: HashMap<u64, Vec<u64>> = python.allow_threads(|| {
inputs
.into_par_iter()
.map(|number| (number, factors_uniq(number)))
.collect()
});
m.into_py_dict(python)
todo!()
}

#[pymodule]
Expand Down

0 comments on commit bbeec3f

Please sign in to comment.