Sum of an array using pthreads


Pthreads is an execution model that helps use multiple processors to work at the same time for solving a problem. It is independent of the programming language.

Problem Statement

Given an array of integers. Find the sum of all the elements of the array using pthreads.

Need for Multithreading for Calculating sum

The problem is to add the elements in an array. Although it is a simple problem where a linear traversal of the array can do the work very easily with a time complexity of O(n) where n is the number of elements in the array. But if we are given a very large array of elements, the time taken by the program to execute also increases.

An efficient solution to this problem can be to divide the array into parts and compute the sum of each part together or parallelly. This concept is called multithreading, where the program or the problem domain is divided into parts and each part is tackled by different threads at the same time. Each of these parts that run concurrently is called a thread.

Thus if in the above problem, we divide the array into two parts and compute the sum using multithreading the time taken will be reduced by a factor of 2. And if the array is divided into x parts the time taken by the program to calculate the sum will be reduced by a factor of x.

Creating a Pthread

It creates a new executable thread. It has the following parameters passed −

  • threadID − Identifier for new thread

  • attr − for setting the thread attributes

  • func − function executed by the thread

  • funcArg − argument passed to func

Terminating a Pthread

A thread is terminated when its execution is complete and it is no longer required.

pthread_exit (status);

Joining a Pthread

Joining threads means waiting for the thread to terminate before terminating the main() block.

pthread_join (threadID, status);

Compiling the Program

$gcc program.cpp -lpthread

Pseudocode

sumArray[5]
array[15]
tPart = 0
procedure sum ()
   part_of_array = tPart++
   for i = part_of_array*size_of_thread to (part_of_array+1)*size_of_thread
      sumArray[part_of_array] = sumArray[part_of_array] + array[i]
   end for
end procedure

procedure main()
   for i = 0 to 5
      create pthreads[i]
   end for
   for i = 0 to 5
      join pthreads[i]
   end for
   sum = 0
   for i = 0 to 5
      sum = sum + sumArray[i]
   end for
end procedure

Input-Output

Input: 2, 2, 3, 3, 4, 4, 5, 5, 6, 6
Output: 40

Explanation

Five parts of the array are − 2, 2, 3, 3, 4, 4, 5, 5, 6, 6

Sum of thread 1 i.e. sumArray[0] = 4
Sum of thread 2 i.e. sumArray[1] = 6
Sum of thread 3 i.e. sumArray[2] = 8
Sum of thread 4 i.e. sumArray[3] = 10
Sum of thread 5 i.e. sumArray[4] = 12
Total sum = 40

Example

In the following program, the array is divided into 5 parts whose sum is calculated concurrently using pthreads. The first 5 threads are created and joined. Each thread executes the sum function where each thread first finds the part of the array they are responsible to find the sum for and then they calculated the sum and store it in an array of the size that has the sum for all the five parts of the array. Later on, the sum of all five parts is added up together to find the final sum of the complete array.

#include<bits/stdc++.h>
#include<pthread.h>
using namespace std;
// tNum depicts the number of threads the problem is divided into
// Array is divided into 5 parts
int tNum = 5;

// initializing the sum array
int sumArray[5] = {0};
int arr[15] = {10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10};

// Calculating the maximum number of elements in each thread
int tSize = ceil(15/tNum), tPart = 0;

// function used by each thread to compute sum
void *sum (void* arg){

   // defining the part of array for which sum is being computed
   int part = tPart;
   tPart++;
   for (int i = part*tSize ; i < (part+1)*tSize ; i++) {
      sumArray[part] += arr[i];
   }
   pthread_exit (NULL);
}

// main() block that calls upon various threads for executing parallelly
int main(){
   pthread_t threadID[tNum];
   
   // 5 threads are created and executed
   for (int i = 0 ; i < tNum ; i++) {
      pthread_create (&threadID[i], NULL, sum, (void*)NULL);
   }
   
   // 5 threads are joined
   for (int i = 0 ; i < tNum ; i++) {
      pthread_join (threadID[i], NULL);
   }
   
   // the sum of all the subparts is added
   int sum = 0;
   for (int i = 0 ; i < tNum ; i++) {
      sum += sumArray[i];
   }
   cout << "Sum of array = " << sum;
   return 0;
}

Output

Sum of array = 150

Conclusion

In conclusion, using pthreads to find sum of array, we calculate the sum of two or more parts of the array parallelly at the same time. This parallel execution is the core concept of multithreading and is used to reduce the computational time of the entire problem.

Updated on: 28-Sep-2023

428 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements