Answered step by step
Verified Expert Solution
Question
1 Approved Answer
In this project, you are asked to write two independent programs, thr_atomic.c and thr_reduce.c, each of which uses m computational threads to concurrently calculate the
In this project, you are asked to write two independent programs, thr_atomic.c and thr_reduce.c, each of which uses m computational threads to concurrently calculate the sum of quadruple roots from 1 to n, where m and n are powers of 2 and are specified in the command line. For the program thr_atomic.c, it gets the values m and n from the command line arguments and converts them to two integers, respectively. Next, it creates m threads using pthread_create() and each thread computes the sum of n/m quadruple roots. Namely, the first thread (i.e. thread O) computes the sum of quadruple roots from 1 to n/m, the second thread (i.e. thread 1) computes the sum of the quadruple roots from n/m + 1 to 2n/m, etc. When a thread finishes its computation, it should print its partial sum and atomically add it to a shared global variable. Note that your program needs to use pthread_barrier_wait() to let the main thread know that all of the m computational threads have done the atomic additions and hence it can print the result. Below is an example of running your thread program: bach> ./thr_atomic 2 65536 thr 0: 352703.926537 thr 1: 486164.553017 sum of quadruple roots: 838868.479554 In this project, you are asked to write two independent programs, thr_atomic.c and thr_reduce.c, each of which uses m computational threads to concurrently calculate the sum of quadruple roots from 1 to n, where m and n are powers of 2 and are specified in the command line. For the program thr_atomic.c, it gets the values m and n from the command line arguments and converts them to two integers, respectively. Next, it creates m threads using pthread_create() and each thread computes the sum of n/m quadruple roots. Namely, the first thread (i.e. thread O) computes the sum of quadruple roots from 1 to n/m, the second thread (i.e. thread 1) computes the sum of the quadruple roots from n/m + 1 to 2n/m, etc. When a thread finishes its computation, it should print its partial sum and atomically add it to a shared global variable. Note that your program needs to use pthread_barrier_wait() to let the main thread know that all of the m computational threads have done the atomic additions and hence it can print the result. Below is an example of running your thread program: bach> ./thr_atomic 2 65536 thr 0: 352703.926537 thr 1: 486164.553017 sum of quadruple roots: 838868.479554
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started