Question

Write an application that uses parallelism to compute the sum of the integers in an array...

Write an application that uses parallelism to compute the sum of the integers in an array of size 100000. You can populate the array with random numbers and your application should display the sum.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Java Program for the aboveproblem statement :-

The Below program will generate the sum of 100000 random numbers both in SIngle summation and using Parallelism.

import java.util.Random;

class Summation extends Thread {

   private int[] arr;

   private int low, high;
   private long partial;

   public Summation(int[] arr, int low, int high) {
       this.arr = arr;
       this.low = low;
       this.high = Math.min(high, arr.length);
   }

   public long getPartialSum() {
       return partial;
   }

   public void run() {
       partial = sum(arr, low, high);
   }

   public static long sum(int[] arr) {
       return sum(arr, 0, arr.length);
   }

   public static long sum(int[] arr, int low, int high) {
       long total = 0;

       for (int i = low; i < high; i++) {
           total += arr[i];
       }

       return total;
   }

   public static long parallelSum(int[] arr) {
       return parallelSum(arr, Runtime.getRuntime().availableProcessors());
   }

   public static long parallelSum(int[] arr, int threads) {
       int size = (int) Math.ceil(arr.length * 1.0 / threads);

       Summation[] sums = new Summation[threads];

       for (int i = 0; i < threads; i++) {
           sums[i] = new Summation(arr, i * size, (i + 1) * size);
           sums[i].start();
       }

       try {
           for (Summation sum : sums) {
               sum.join();
           }
       } catch (InterruptedException e) {
       }

       int total = 0;

       for (Summation sum : sums) {
           total += sum.getPartialSum();
       }

       return total;
   }

   public static void main(String[] args) {
       Random rand = new Random();

       int[] arr = new int[100000];

       for (int i = 0; i < arr.length; i++) {
           arr[i] = rand.nextInt(101) + 1; // 1..100
      
       }

       long start = System.currentTimeMillis();

       System.out.println("single summantion value is :- " + Summation.sum(arr));

       System.out.println("Single: " + (System.currentTimeMillis() - start)); // Single:
                                                                               // 44

       start = System.currentTimeMillis();

          System.out.println("parallel summantion value is :- " + Summation.parallelSum(arr));


       System.out.println("Parallel: " + (System.currentTimeMillis() - start)); // Parallel:
                                                                                   // 25
   }

}

Add a comment
Know the answer?
Add Answer to:
Write an application that uses parallelism to compute the sum of the integers in an array...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a program to compute intersection of two sorted array of integers and compute the CPU...

    Write a program to compute intersection of two sorted array of integers and compute the CPU time for different sets of unsigned integers generated by a random number generator using HASH SET. Test this using the same data sets: atleast 3 of size 1000 integers, atleast 3 of size 10000 integers, atleast 3 of size 100000 integers, atleast 3 of one million integers and atleast 3 of size 10 million integers DONT FORGET CPU TIME FOR EACH ONE

  • Using JAVA, write an application that uses an Array of 30 Numbers (random integers from 1...

    Using JAVA, write an application that uses an Array of 30 Numbers (random integers from 1 - 100) and returns the maximum number, minimum number, average of all numbers, and prints a Bar Chart to show the number distribution (1-9, 10-19,20-29, …80-89, 90-100). Note; maximum number, minimum number, average number, and the Bar Chart must use implemented as separate methods in a separate class. Method call should pass an array as an argument. Methods should accept an array as an...

  • Write an application that does the following: (1) fill a 32-bit array with 10 pseudo-random integers...

    Write an application that does the following: (1) fill a 32-bit array with 10 pseudo-random integers between -50 and +49 ; (2) Loop through the array, displaying each value, and count the number of negative values; (3) After the loop finishes, display the count. Below is an assembly program template that you can refer, and complete the programming by filling in the codes into the specified place: Comment ! Title: Counting Array Values Description: Write an application that does the...

  • (+30) Provide a python program which will Populate an array(list) of size 25 with integers in...

    (+30) Provide a python program which will Populate an array(list) of size 25 with integers in the range -100 (negative 100)   to +100 inclusive Display the array and its length (use the len function) Display the average of all the integers in the array Display the number of even integers (how many) Display the number of odd integers (how many) Display the number of integers > 0   (how many) Display the number of integers < 0   (how many) Display the...

  • Java: Write an application that has an array of at least 20 integers. It should call...

    Java: Write an application that has an array of at least 20 integers. It should call a method that uses the sequential search algorithm to locate one of the values. The method should keep a count of the number of comparisons it makes until it finds the value. Then the program should call another method that uses the binary search algorithm to locate the same value. It should also keep count of the number of comparisons it makes. Display these...

  • Code a repetition control structure that: Processes an array of integers in index order to sum...

    Code a repetition control structure that: Processes an array of integers in index order to sum its elements. Only odd numbers should be included in the sum and processing should cease when either a negative integer or (be careful) the end of the array is encountered. The array can be any length but you do not have to deal with an empty array. Declare all required variables and give them initial values if required. Display the output using myWindow.writeOutLine(…). You...

  • Write a C program Design a program that uses an array to store 10 randomly generated...

    Write a C program Design a program that uses an array to store 10 randomly generated integer numbers in the range from 1 to 50. The program should first generate random numbers and save these numbers into the array. It will then provide the following menu options to the user: Display 10 random numbers stored in the array Compute and display the largest number in the array Compute and display the average value of all numbers Exit The options 2...

  • in C++ Write a program which uses the following arrays: empID: An array of 7 integers...

    in C++ Write a program which uses the following arrays: empID: An array of 7 integers to hold employee identification numbers. The array should be initialized with the following values: 1, 2, 3, 4, 5, 6, 7. Hours: an array of seven integers to hold the number of hours worked by each employee. payRate: an array of seven doubles to hold each employee’s hourly pay rate. Wages: an array of seven doubles to hold each employee’s gross salary. The program...

  • Parvati is given an array of integers. She is asked to return the sum of all...

    Parvati is given an array of integers. She is asked to return the sum of all the two-digit numbers in the array. Please help her to perform this task. Write a function: class Solution { public int solution (int[] A); } that, given an array A consisting of N integers, returns the sum of all two-digit numbers. For example, given A = [1, 1000, 80, -91], the function should return -11 (as the two-digit numbers are 80 and -91). Given...

  • Storing the Array: Write an application that uses an Array to store 10messages of type String....

    Storing the Array: Write an application that uses an Array to store 10messages of type String. You will store this Array with 10 messages of your choosing. For example, a message could be “I love Java the programming language!” or another message could be “I love Java the drink!” Initializing the Array: You may initialize your Array with the messages or have the user enter the messages. The choice is yours. Your Java code will contain a method called shoutOutCannedMessage()...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT