In: Computer Science
Turn Ins:
Source code of two programs and a short report on to answer the following question:
input.txt looks like below
55 59 56 23 93 9 39 76 27 81 46 99 35 11 93 43 10 22 29 89 8 10 11 86 48 34 39 3 54 4 76 2 35 52 58 92 75 15 48 51 29 81 96 25 46 90 67 40 55 67 44 76 3 48 31 61 48 9 64 68 78 4 17 26 63 64 92 11 58 54 71 51 11 16 27 76 91 8 77 35 12 84 87 93 71 14 5 36 16 35 30 47 85 81 78 34
Here is the solution -
I have used a input.txt file that has almost 20,000 integers, for testing purpose.
Code for both parts of question are exactly same, just change the " #define MAX_THREAD <value>"
to have that many threads in program. Here is the code for both cases:
CASE 1 : Single Thread Program
Time taken : 85890 microseconds (see output image)
#include <bits/stdc++.h> 
#include <chrono> 
#include <fstream>
#include <iostream> 
#include <pthread.h> 
  
// size of array 
#define MAX 100000
  
// maximum number of threads 
#define MAX_THREAD 1
//chrono is used for record the time in execution
using namespace std::chrono;  
using namespace std; 
//global variables  a[]=array of long integers, sum[] = array of threads   
long long a[MAX];
long long sum[MAX_THREAD] = { 0 }; 
int part = 0; //current thread(part)
//function to sum the values of thread's part  
void* sum_array(void* arg) 
{   
    // Each thread computes sum of 1/nth of array 
    long long thread_part = part++; 
    for (int i = thread_part * (MAX / MAX_THREAD); i < (thread_part + 1) * (MAX / MAX_THREAD); i++) 
        sum[thread_part] += a[i]; 
} 
// Driver Code 
int main() 
{   
    // Get starting timepoint 
    auto start = high_resolution_clock::now(); 
    
    
    //reading file and populating array
    fstream file;
        int x;
        file.open("input.txt");
    int count = 0;
        while(file>>x){
            a[count] = x;
            count++;
                }
        file.close();
    //initialize the array of threads
    pthread_t threads[MAX_THREAD]; 
    
    
    // Creating 10 threads 
    for (int i = 0; i < MAX_THREAD; i++) 
        pthread_create(&threads[i], NULL, sum_array, (void*)NULL); 
  
    // joining 10 threads i.e. waiting for all 4 threads to complete 
    for (int i = 0; i < MAX_THREAD; i++) 
        pthread_join(threads[i], NULL); 
  
    // adding sum of all parts  (10 if using 10 threads, 1 if using 1 thread)
    int total_sum = 0; 
    for (int i = 0; i <MAX_THREAD; i++) 
        total_sum += sum[i]; 
  
    cout << "sum is " << total_sum << endl; 
        
    // Get ending timepoint 
    auto stop = high_resolution_clock::now(); 
  
  
    // Get duration. Substart timepoints to  
    // get durarion. To cast it to proper unit 
    // use duration cast method 
    auto duration = duration_cast<microseconds>(stop - start); 
  
    cout << "Time taken by program: "
         << duration.count() << " microseconds" << endl; 
  
    return 0; 
} 
OUTPUT:

CASE 2: 10 Threads Program
Time taken : 8145 microseconds (see output image)
#include <bits/stdc++.h> 
#include <chrono> 
#include <fstream>
#include <iostream> 
#include <pthread.h> 
  
// size of array 
#define MAX 100000
  
// maximum number of threads 
#define MAX_THREAD 10
//chrono is used for record the time in execution
using namespace std::chrono;  
using namespace std; 
//global variables  a[]=array of long integers, sum[] = array of threads   
long long a[MAX];
long long sum[MAX_THREAD] = { 0 }; 
int part = 0; //current thread(part)
//function to sum the values of thread's part  
void* sum_array(void* arg) 
{   
    // Each thread computes sum of 1/nth of array 
    long long thread_part = part++; 
    for (int i = thread_part * (MAX / MAX_THREAD); i < (thread_part + 1) * (MAX / MAX_THREAD); i++) 
        sum[thread_part] += a[i]; 
} 
// Driver Code 
int main() 
{   
    // Get starting timepoint 
    auto start = high_resolution_clock::now(); 
    
    
    //reading file and populating array
    fstream file;
        int x;
        file.open("input.txt");
    int count = 0;
        while(file>>x){
            a[count] = x;
            count++;
                }
        file.close();
    //initialize the array of threads
    pthread_t threads[MAX_THREAD]; 
    
    
    // Creating 10 threads 
    for (int i = 0; i < MAX_THREAD; i++) 
        pthread_create(&threads[i], NULL, sum_array, (void*)NULL); 
  
    // joining 10 threads i.e. waiting for all 4 threads to complete 
    for (int i = 0; i < MAX_THREAD; i++) 
        pthread_join(threads[i], NULL); 
  
    // adding sum of all parts  (10 if using 10 threads, 1 if using 1 thread)
    int total_sum = 0; 
    for (int i = 0; i <MAX_THREAD; i++) 
        total_sum += sum[i]; 
  
    cout << "sum is " << total_sum << endl; 
        
    // Get ending timepoint 
    auto stop = high_resolution_clock::now(); 
  
  
    // Get duration. Substart timepoints to  
    // get durarion. To cast it to proper unit 
    // use duration cast method 
    auto duration = duration_cast<microseconds>(stop - start); 
  
    cout << "Time taken by program: "
         << duration.count() << " microseconds" << endl; 
  
    return 0; 
} 

Questions:
Answers:
1. 85890 microsecond
2. 8145 microsecond
3. Hence with above observations, it is clear that multithreaded program( 10 threads) takes very less time than the single threaded program. While a thread is waiting for hardware to response the CPU switches to another thread that is not waiting for hardware to response. ... When two CPU are available both calculations can be done at the same time thus speeding up your program.