Advertisement
Guest User

Untitled

a guest
May 24th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.59 KB | None | 0 0
  1. //[dependencies]
  2. //ndarray = ""
  3. //rayon = "1.0.3"
  4. //ndarray-parallel = "0.9.0"
  5.  
  6.  
  7. #![allow(non_snake_case)]
  8. #![allow(unused)]
  9.  
  10. #[macro_use]
  11. extern crate ndarray;
  12.  
  13. use ndarray::prelude::*;
  14. use ndarray::Zip;
  15.  
  16. //extern crate ndarray_parallel;
  17. //use ndarray_parallel::prelude::*;
  18.  
  19. extern crate rayon;
  20. use rayon::prelude::*;
  21.  
  22. use std::time::{Duration, Instant};
  23.  
  24.  
  25. fn f(x: f64, x0: f64, dx: f64) -> f64 {
  26. let c = (x - x0) / dx;
  27. 1.0 / (c * c + 1.0)
  28. }
  29.  
  30. fn serial() {
  31. let nx = 100000;
  32. let xmin = 0.0;
  33. let xmax = 1.0;
  34. let Dx = (xmax - xmin) / 20.0;
  35. let x : Array1<f64> = Array::linspace(xmin, xmax, nx);
  36. let dx = x[1] - x[0];
  37. let mut y : Array1<f64> = Array1::<f64>::zeros(nx);
  38. let cc = 2.3;
  39.  
  40. for i in 0..nx {
  41. let x0 = x[i];
  42. let di = std::cmp::max(2, (Dx / dx * 10.0) as usize);
  43. let i1 = std::cmp::max(0, i - di);
  44. let i2 = std::cmp::max(nx - 1, i + di);
  45.  
  46. for j in i1..i2 {
  47. y[i] = y[i] + cc * f(x[i], x0, Dx);
  48. }
  49. }
  50. println!("{}", y.sum());
  51. }
  52.  
  53. fn parallel() {
  54. let nx = 100000;
  55. let xmin = 0.0;
  56. let xmax = 1.0;
  57. let Dx = (xmax - xmin) / 20.0;
  58. let x : Array1<f64> = Array::linspace(xmin, xmax, nx);
  59. let dx = x[1] - x[0];
  60. let mut y : Array1<f64> = Array1::<f64>::zeros(nx);
  61. let cc = 2.3;
  62.  
  63. for i in 0..nx {
  64. let x0 = x[i];
  65. let di = std::cmp::max(2, (Dx / dx * 10.0) as usize);
  66. let i1 = std::cmp::max(0, i - di);
  67. let i2 = std::cmp::min(nx - 1, i + di);
  68.  
  69. let mut xc = x.slice(s![i1..i2]);
  70. let mut yc = y.slice(s![i1..i2]);
  71.  
  72. Zip::from(&mut yc).and(&xc).par_apply(|y_, &x_| {*y_ = *y_ + cc * f(x_, x0, Dx);});
  73.  
  74. //error[E0277]: the trait bound `&mut ndarray::ArrayBase<ndarray::ViewRepr<&f64>, ndarray::Dim<[usize; 1]>>: ndarray::IntoNdProducer` is not satisfied
  75. // --> src/main.rs:62:9
  76. // |
  77. //62 | Zip::from(&mut yc).and(&xc).par_apply(|y_, &x_| {*y_ = *y_ + cc * f(x_, x0, Dx);});
  78. // | ^^^^^^^^^ the trait `ndarray::IntoNdProducer` is not implemented for `&mut ndarray::ArrayBase<ndarray::ViewRepr<&f64>, ndarray::Dim<[usize; 1]>>`
  79. // |
  80. // = help: the following implementations were found:
  81. // <&'a mut ndarray::ArrayBase<S, D> as ndarray::IntoNdProducer>
  82. // <&'a ndarray::ArrayBase<S, D> as ndarray::IntoNdProducer>
  83. // = note: required by `ndarray::Zip::<(P,), D>::from`
  84.  
  85. }
  86.  
  87. println!("{}", y.sum());
  88. }
  89.  
  90. fn main() {
  91. let t1 = Instant::now();
  92. serial();
  93. let t2 = Instant::now();
  94. parallel();
  95. let t3 = Instant::now();
  96.  
  97. println!("{:?} {:?}", t2.duration_since(t1), t3.duration_since(t2));
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement