USE SIMPLE PYTHON MULTI THREADING code to perform parallel array summing. replace the mthod of using processes to complete and use threading techniques to do the same the thing.
1) Basic version with two levels of threads (master and slaves)
One master thread aggregates and sums the result of n slave-threads where each slavethread sums a different range of values in an array of 1000 random integers (please
program to generate 1000 random integers to populate the array).
The number of slave-threads is a parameter which the user can change. For example, if the
user chooses 4 slave threads, each slave thread will sum 1000/4 = 250 numbers. If the user
chooses 3 slave threads, the first two may each sum 333 numbers and the third slave thread
sums the rest 334 numbers.
2) Advanced version with more than two levels of threads
The master thread creates two slave-threads where each slave-thread is responsible to sum
half segment of the array.
Each slave thread will fork/spawn two new slave-threads where each new slave-thread
sums half of the array segment received by its parent. Each slave thread will return the
subtotal to its parent thread and the parent thread aggregates and returns the total to its
parent thread. Start with 7 nodes thread tree, when you are comfortable, you can extend it
to a full thread tree.
Here's a sample code for the basic version of parallel array summing using threading in Python:
CODE in PYTHON:
import random
import threading
def sum_array(arr, start, end, result):
total = 0
for i in range(start, end):
total += arr[i]
result.append(total)
def parallel_sum(arr, num_threads):
n = len(arr)
chunk_size = n // num_threads
threads = []
result = []
for i in range(num_threads):
start = i * chunk_size
end = start + chunk_size
if i == num_threads - 1:
end = n
thread = threading.Thread(target=sum_array, args=(arr, start, end, result))
threads.append(thread)
thread.start()
for thread in threads:
thread.join()
return sum(result)
# Generate 1000 random integers
arr = [random.randint(0, 1000) for _ in range(1000)]
# Test parallel_sum function with 4 threads
print(parallel_sum(arr, 4))
Trending nowThis is a popular solution!
Step by stepSolved in 2 steps
- What should the array will look like after the function Partition () has been called once in a Quicksort. Input Array: (Hint: pivot should be the last element) 2 8 7 1 3 5 6 4arrow_forwardIs there a way to optimize the following multithreaded quick sort algorithm, in C? I am looking for suggestions to make it run faster. I am trying to get closer to the qsort function's time. I don't want anything too complicated, like thread pooling. Here is the code: #include <stdio.h>#include <stdlib.h>#include <errno.h>#include <string.h>#include <pthread.h>#include <time.h>#define SORT_THRESHOLD 40typedef struct _sortParams {char** array;int left;int right;int* threadsLeft; // A counter to keep track of how many more threads are available to be created.pthread_mutex_t* mutex;} SortParams;static int maximumThreads; /* maximum # of threads to be used */static void insertSort(char** array, int left, int right) {int i, j;for (i = left + 1; i <= right; i++) {char* pivot = array[i];j = i - 1;while (j >= left && (strcmp(array[j], pivot) > 0)) {array[j + 1] = array[j];j--;}array[j + 1] = pivot;}}/*** This function uses a mutex to…arrow_forwardReference the algorithm in the picture below: (a) Write code in python to implement the Quicksort algorithm. Your code should print both the inputand output arrays to the screen.(b) Using the list LArray = {26, 14, 52, 7, 15, 37, 91, 2, 28, 73} illustrate how quicksortworks.(c) Using the list LArray = {37, 91, 2, 26, 14, 52, 7, 28, 15, 73} illustrate how quicksortworksarrow_forward
- C Sharp How do I format my code to 2 decimal places whem I am calling my code in Main using System.Threading.Tasks; namespace ssullivan_A2_1.cs{ class Program { //The output should display the Object Type (Circle, Square or Triangle) //the values used to initialize the shape, the area, and the perimeter static void Main(string[] args) { ////Store the objects in a Collection object (List, Array, HashSet, etc...) objects in the collection will be of type Shape Shapes[] arrayOfShapes = new Shapes[3]; arrayOfShapes[0] = new Circle(5);// arrayOfShapes[1] = new Rectangle(5, 5);// arrayOfShapes[2] = new Triangle(3, 5, 5);// //the values used to initialize the shape, the area, and the perimeter foreach (Shapes objects in arrayOfShapes) {// use arrayOfShapes to call methods //The output should display the Object Type (Circle, Square or Triangle)…arrow_forwarddef sorting(x, i, j) if j+1-i < 10 then Mergesort(x, i, j); t1 = i + (j+1-i)/3 t2 = i + 2*(j+1-i)/3 Sorting(x, i, t2) Sorting(x, i, j) Sorting(x, i, t2) // x is an array, I and j are first and last indices of this part of the array // on k elements, takes O(k log k) time worst case analysis?arrow_forwardExplain the term relocation. Explain the structure of Typical ELF executable object file. What would be the output on the terminal when a user tries to compile and execute the following files. Write the steps to compile and run as well. #include <stdio.h> int buf[]={1,3}; int main() { swap(); printf("x= %d , y=%d\n", buf[0], buf[1]); return 0; } #include <stdio.h> extern int buf[]; int *bufp0 = &buf[0]; static int *bufp1; void swap() { int temp; bufp1 = &buf[1]; temp = *bufp0; *bufp0 = *bufp1; *bufp1 = temp; }arrow_forward
- Please answer the below question for operating system using shell script. write a program in bash that has a user_defined function array_sum that takes each element of array as argument andcalculates their sum. At each step the sum will printed and then the final sum will be printed. For example:Input Array:[2 3 7 9]Output: 2 5 12 21final sum: 21arrow_forwardWhich of the following statement or statements are true ? Please check all that apply. Both the linkedlist and array size needs to be pre-decided Arrays have better cache locality that can make them better in terms of performance. It's easier to insert and delete elements from array than from the linkedlist Data type of the linked list doesnot need to be same, whereas in array, it must be the same memory can be allocated at runtime both for linkedlist and dynamic array Which of the following statements are true? Check all that applies 5 points Accessing any element of an array has the time complexity of O(n) Searching for an element in array and linkedlist has the same time complexity Inserting or Deleting an element from a stack or queue has the same time complexity Accessing an element from a singly…arrow_forwardSelect all explicit instructions that apply to the following prompt: Implement an open addressing hash table to store a list of integers. Your task is to handle collisions using open addressing and implement basic operations such as insertion, deletion, and searching. The open addressing technique you will use is linear probing.arrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education