0

Thank you for being generous with your time and helping me in this matter. I am trying to calculate the sum of the squared numbers using pthread. However, it seems that it is even slower than the serial implementation. Moreover, when I increase the number of threads the program becomes even slower. I made sure that each thread is running on a different core (I have 6 cores assigned to the virtual machine)

This is the serial program:

#include <stdio.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/time.h>
#include <time.h>


int main(int argc, char *argv[]) {
  
  struct timeval start, end;
  gettimeofday(&start, NULL);   //start time of calculation
  
   int n = atoi(argv[1]);
   long int sum = 0;
   for (int i = 1; i < n; i++){
      sum += (i * i);
   }
  
   gettimeofday(&end, NULL); //end time of calculation
   printf("The sum of squares in [1,%d): %ld | Time Taken: %ld mirco seconds \n",n,sum, 
   ((end.tv_sec * 1000000 + end.tv_usec) - (start.tv_sec * 1000000 + start.tv_usec)));

   return 0;
}

This the Pthreads program:

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <sys/types.h>
#include <sys/time.h>
#include <time.h>


void *Sum(void *param);

// structure for thread arguments
struct thread_args {
int tid;
int a; //start
int b; //end
long int result; // partial results
};


int main(int argc, char *argv[])
{
struct timeval start, end;
gettimeofday(&start, NULL);   //start time of calculation

int numthreads;
int number;
double totalSum=0;

if(argc < 3 ){
printf("Usage: ./sum_pthreads <numthreads> <number> ");
return 1;
}

numthreads = atoi(argv[1]);
number = atoi(argv[2]);;
pthread_t   tid[numthreads];
struct thread_args targs[numthreads];

printf("I am Process | range: [%d,%d)\n",1,number);
printf("Running Threads...\n\n");

for(int i=0; i<numthreads;i++ ){
//Setting up the args
targs[i].tid = i;
targs[i].a = (number)*(targs[i].tid)/(numthreads);
targs[i].b = (number)*(targs[i].tid+1)/(numthreads);
if(i == numthreads-1 ){
targs[i].b = number;
}

pthread_create(&tid[i],NULL,Sum, &targs[i]);

}


for(int i=0; i< numthreads; i++){
pthread_join(tid[i],NULL);
}

printf("Threads Exited!\n");
printf("Process collecting information...\n");

for(int i=0; i<numthreads;i++ ){
  totalSum += targs[i].result;
}

gettimeofday(&end, NULL); //end time of calculation

 
printf("Total Sum is: %.2f | Taken Time: %ld mirco seconds \n",totalSum, 
((end.tv_sec * 1000000 + end.tv_usec) - (start.tv_sec * 1000000 + start.tv_usec)));

return 0;
}



void *Sum(void *param) {
int start = (*(( struct  thread_args*) param)).a;
int end = (*((struct  thread_args*) param)).b;
int id = (*((struct thread_args*)param)).tid;
long int sum =0;

printf("I am thread %d | range: [%d,%d)\n",id,start,end);

for (int i = start; i < end; i++){
      sum += (i * i);
   }
   
(*((struct thread_args*)param)).result =  sum;
printf("I am thread %d | Sum: %ld\n\n", id ,(*((struct thread_args*)param)).result );
pthread_exit(0);
}

Results:

hamza@hamza:~/Desktop/lab4$ ./sum_serial 10
The sum of squares in [1,10): 285 | Time Taken: 7 mirco seconds 
hamza@hamza:~/Desktop/lab4$ ./sol 2 10
I am Process | range: [1,10)
Running Threads...

I am thread 0 | range: [0,5)
I am thread 0 | Sum: 30

I am thread 1 | range: [5,10)
I am thread 1 | Sum: 255

Threads Exited!
Process collecting information...
Total Sum is: 285.00 | Taken Time: 670 mirco seconds 
hamza@hamza:~/Desktop/lab4$ ./sol 3 10
I am Process | range: [1,10)
Running Threads...

I am thread 0 | range: [0,3)
I am thread 0 | Sum: 5

I am thread 1 | range: [3,6)
I am thread 1 | Sum: 50

I am thread 2 | range: [6,10)
I am thread 2 | Sum: 230

Threads Exited!
Process collecting information...
Total Sum is: 285.00 | Taken Time: 775 mirco seconds 
hamza@hamza:~/Desktop/lab4$ 
1
  • Please read the description of the tags that you applied. In particular the "linux" tag is inappropriate. That said, it's "microseconds" not "mirco seconds" and you really should format your code consistently, if you want anyone to read and understand it. As a new user, please also take the tour and read How to Ask. Commented Nov 24, 2020 at 20:41

1 Answer 1

1

The two programs do very different things. For example, the threaded program produces much more text output and creates a bunch of threads. You're comparing very short runs (less than a thousandth of a second) so the overhead of those additional things is significant.

You have to test with much longer runs such that the cost of producing additional output and creating and synchronizing threads is lost.

To use an analogy, one person can tighten three screws faster than three people can because of the overhead of getting a tool to each person, deciding who will tighten which screw, and so on. But if you have 500 screws to tighten, then three people will get it done faster.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.