diff --git a/src/assignments/assignment13/small_exercises.rs b/src/assignments/assignment13/small_exercises.rs index 9c282a5..63b2363 100644 --- a/src/assignments/assignment13/small_exercises.rs +++ b/src/assignments/assignment13/small_exercises.rs @@ -19,7 +19,7 @@ pub fn sigma_par i64 + Sync + Send>( inner: impl ParallelIterator, f: F, ) -> i64 { - todo!() + inner.map(f).sum() } /// Alternate elements from three iterators until they have run out. @@ -40,7 +40,18 @@ pub fn interleave3_par( list2: impl IndexedParallelIterator, list3: impl IndexedParallelIterator, ) -> Vec { - todo!() + let list1: Vec = list1.collect(); + let list2: Vec = list2.collect(); + let list3: Vec = list3.collect(); + let mut result = vec![]; + + for ((e1, e2), e3) in list1.into_iter().zip(list2).zip(list3) { + result.push(e1); + result.push(e2); + result.push(e3); + } + + result } /// Parallel vector addition @@ -56,7 +67,10 @@ pub fn interleave3_par( /// assert_eq!(res, vec![2.0, 4.0, 6.0, 8.0, 10.0]); /// ``` pub fn vec_add_par(lhs: &[f64], rhs: &[f64]) -> Vec { - todo!() + lhs.par_iter() + .zip(rhs) + .map(|(lhs, rhs)| lhs + rhs) + .collect() } /// Parallel dot product of two arrays @@ -76,7 +90,7 @@ pub fn vec_add_par(lhs: &[f64], rhs: &[f64]) -> Vec { /// assert_eq!(res, 55.0); /// ``` pub fn dot_product_par(lhs: &[f64], rhs: &[f64]) -> f64 { - todo!() + lhs.par_iter().zip(rhs).map(|(lhs, rhs)| lhs * rhs).sum() } /// Parallel Matrix multiplication @@ -109,5 +123,11 @@ pub fn dot_product_par(lhs: &[f64], rhs: &[f64]) -> f64 { /// assert_eq!(ans, res); /// ``` pub fn matmul_par(lhs: &[Vec], rhs: &[Vec]) -> Vec> { - todo!() + lhs.par_iter() + .map(|row| { + rhs.par_iter() + .map(|col| dot_product_par(row, col)) + .collect() + }) + .collect() }