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

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899603
Joined: Mon Aug 02, 2021 8:13 am

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

Post by answerhappygod »

 1
1 (388.92 KiB) Viewed 42 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)
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply