Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Check this code for errors: import random import time import os from multipyparallel import Client def compute_pi(number_of_tosses, num_threads): # Initialize variables number_in_circle = 0 #

Check this code for errors:

import random import time import os

from multipyparallel import Client

def compute_pi(number_of_tosses, num_threads): # Initialize variables number_in_circle = 0

# Set the number of threads to use os.environ['OMP_NUM_THREADS'] = str(num_threads)

# Toss darts randomly at the dartboard # Use OpenMP to parallelize the loop # Each iteration should be independent, so we can use the 'parallel for' construct # The critical region is the update to the shared variable 'number_in_circle' # Use the 'atomic' construct to protect this region # Note: The 'atomic' construct is not supported in Python, so we will use a lock to protect the critical region lock = threading.Lock() with nogil, parallel(): for toss in range(number_of_tosses): x = random.uniform(-1, 1) y = random.uniform(-1, 1) distance_squared = x*2 + y*2

if distance_squared <= 1: with lock: number_in_circle += 1 # Calculate the estimate of pi pi_estimate = 4 * number_in_circle / number_of_tosses return pi_estimate

def main(): # Choose the number of tosses such that at least the first 20 digits of pi are obtained number_of_tosses = 100000000

# Run the computation for different number of threads num_threads = [2, 4, 8, 16] for n in num_threads: # Time the computation start = time.time() pi = compute_pi(number_of_tosses, n) end = time.time() elapsed = end - start

# Print the results print(f"Number of threads: {n}") print(f"Pi estimate: {pi:.20f}") print(f"Elapsed time: {elapsed:.2f} seconds") print()

if name == "main": main()

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2015 Porto Portugal September 7 11 2015 Proceedings Part 2 Lnai 9285

Authors: Annalisa Appice ,Pedro Pereira Rodrigues ,Vitor Santos Costa ,Joao Gama ,Alipio Jorge ,Carlos Soares

1st Edition

3319235249, 978-3319235240

More Books

Students also viewed these Databases questions

Question

Stonewalling and how do they deal with it?

Answered: 1 week ago

Question

1. Identify three approaches to culture.

Answered: 1 week ago

Question

3. Identify and describe nine cultural value orientations.

Answered: 1 week ago

Question

4. Describe how cultural values influence communication.

Answered: 1 week ago