Advertisement
Guest User

Untitled

a guest
Oct 16th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.53 KB | None | 0 0
  1. #![feature(core_intrinsics)]
  2.  
  3. use std::{env, intrinsics::fadd_fast, time::Instant};
  4.  
  5. fn matrix_new(n: usize) -> Vec<f64> {
  6. let n = n as isize;
  7. let tmp = 1.0 / n as f64 / n as f64;
  8. (0..n)
  9. .flat_map(|i| (0..n).map(move |j| tmp * (i - j) as f64 * (i + j) as f64))
  10. .collect()
  11. }
  12.  
  13. fn matrix_transpose(n: usize, src: &[f64], dst: &mut [f64]) {
  14. for (i, dst_row) in dst.chunks_exact_mut(n).enumerate() {
  15. dst_row
  16. .iter_mut()
  17. .enumerate()
  18. .for_each(|(j, t)| *t = src[j * n + i]);
  19. }
  20. }
  21.  
  22. fn matrix_mul(n: usize, a: &[f64], b: &[f64], c: &mut [f64]) {
  23. let mut t = vec![0.0; n * n];
  24. matrix_transpose(n, b, &mut t);
  25.  
  26. for (c_row, a_row) in c.chunks_exact_mut(n).zip(a.chunks_exact(n)) {
  27. for (t_col, c) in t.chunks_exact(n).zip(c_row) {
  28. *c = inner_product(*c, a_row, t_col);
  29. }
  30. }
  31. }
  32.  
  33. fn inner_product(init: f64, a: &[f64], b: &[f64]) -> f64 {
  34. a.iter()
  35. .zip(b)
  36. .fold(init, |acc, (&x, &y)| unsafe { fadd_fast(acc, x * y) })
  37. }
  38.  
  39. fn main() -> Result<(), &'static str> {
  40. let n: usize = env::args()
  41. .nth(1)
  42. .ok_or("usage: <app> N")
  43. .and_then(|n| n.parse().map_err(|_| "N is not a number"))?;
  44.  
  45. let a = matrix_new(n);
  46. let b = matrix_new(n);
  47. let mut c = vec![0.0; n * n];
  48.  
  49. let start_time = Instant::now();
  50. matrix_mul(n, &a, &b, &mut c);
  51. let time = start_time.elapsed();
  52.  
  53. println!("time = {:?}", time);
  54. println!("n = {}", n);
  55. println!("result = {}", c[n * (n / 2) + n / 2]);
  56.  
  57. Ok(())
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement