Page 1 of 1

Consider the following Rust code to implement the π calculation using multi threads. 1 use rayon::prelude::*; 2 use std:

Posted: Fri Jul 01, 2022 5:33 am
by answerhappygod
 1
1 (388.92 KiB) Viewed 44 times
Consider the following Rust code to implement the π calculation using multi threads. 1 use rayon::prelude::*; 2 use std:: f64::consts::*; 3 use std::time:: Instant; use std::env; 5 6 7 8 9 10 11 12 fn calculatePI (NIter: usize) --> £64 { 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 fn factorial (N: usize) -> £64 { //max N is 170 let retval = 1.0; for n in 1..-N { retval= retval* (n as f64); } retval } let Factor = (2.0*SQRT_2)/9801.0; // Using parallel reduce let Sum: f64 = (0..=NIter).iter ().map (|i) { let ni as f64; let Part Factor 33 34 35 36 } factorial (4*i) * (1103.0 +26390.0*n) / (factorial (i).powf (4.0) 396_f64.powf (4.0*n)); Part }). reduce (1 0.0, la, bl a + b); 1.0/Sum } fn main() { let StartTime = Instant:: now(); // Check the calculated PI with the constant PI println! ("Estimated Pi: {:.50}", calculatePI (40)); println! (" Constant Pi: {:.50}", PI); let ElapsedTime = StartTime.elapsed (); match env::var ("RAYON_NUM_THREADS") { Ok (NThreads) => println! ("Using {} threads.", NThreads), Err (e) => println! ("Could not read env variable {}", e), }; println! ("Time elapsed is: {!}", ElapsedTime); i. Debug the Rust code so that it can be run in multi-threads using Rayon. (4 marks) ii. The Ramanujan's calculation Rust code has been compiled and run on a multicore system with AMD Ryzen 9 5900X 4.80 GHz (12 cores with 24 threads) and 64 GBytes RAM. By setting the Rayon environment variable (RAYON_NUM_THREADS), the number of threads was set to 1 and 24, respectively, and the elapsed time is shown in Table 4. Calculate the speed up and predict the fraction of the serial part for NThreads = 24 using Amdahl's law. (4 marks) Table 4. Elapsed Parallel Processing Time NThreads 1 24 Elapsed Processing Time (in milliseconds) 108.4 7.2 iii. Calculate the upper bound limit of achievable speedup using Amdahl's law. (2 marks)