Assignment 13 Done

This commit is contained in:
static
2024-12-16 17:13:04 +00:00
parent 02ab54a6b0
commit 8bd6df8551

View File

@@ -19,7 +19,7 @@ pub fn sigma_par<T, F: Fn(T) -> i64 + Sync + Send>(
inner: impl ParallelIterator<Item = T>,
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<T: Send>(
list2: impl IndexedParallelIterator<Item = T>,
list3: impl IndexedParallelIterator<Item = T>,
) -> Vec<T> {
todo!()
let list1: Vec<T> = list1.collect();
let list2: Vec<T> = list2.collect();
let list3: Vec<T> = 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<T: Send>(
/// assert_eq!(res, vec![2.0, 4.0, 6.0, 8.0, 10.0]);
/// ```
pub fn vec_add_par(lhs: &[f64], rhs: &[f64]) -> Vec<f64> {
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<f64> {
/// 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<f64>], rhs: &[Vec<f64>]) -> Vec<Vec<f64>> {
todo!()
lhs.par_iter()
.map(|row| {
rhs.par_iter()
.map(|col| dot_product_par(row, col))
.collect()
})
.collect()
}