Search code examples
c++multithreadingposix

Trying to create a multithreaded program to find the total primes from 0-100000000


Hello I am trying to write a C++ multithreaded program using POSIX thread library to find the number of prime numbers between 1 and 10,000,000 (10 million) and find out how many microseconds it takes...

Creating my threads and running them works completely fine, however I feel as if there is an error found in my Prime function when determining if a number is prime or not...

I keep receiving 78496 as my output, however I desire 664579. Below is my code. Any hints or pointers would be greatly appreciated.

#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <unistd.h>
#include <iostream>
#include <sys/time.h> //measure the execution time of the computations

using namespace std;

//The number of thread to be generated
#define NUMBER_OF_THREADS 4

void * Prime(void* index);

long numbers[4] = {250000, 500000, 750000, 1000000};
long start_numbers[4] = {1, 250001, 500001, 750001};

int thread_numbers[4] = {0, 1, 2, 3};

int main(){
  pthread_t tid[NUMBER_OF_THREADS];

  int tn;

  long sum = 0;

  timeval start_time, end_time; 

  double start_time_microseconds, end_time_microseconds;

  gettimeofday(&start_time, NULL);

  start_time_microseconds = start_time.tv_sec * 1000000 + start_time.tv_usec;
  for(tn = 0; tn < NUMBER_OF_THREADS; tn++){
    if (pthread_create(&tid[tn], NULL, Prime, (void *) &thread_numbers[tn]) == -1 ) {
        perror("thread fail");
        exit(-1);
    }
  }
 long value[4];

 for(int i = 0; i < NUMBER_OF_THREADS; i++){
    if(pthread_join(tid[i],(void **) &value[i]) == 0){
        sum = sum + value[i]; //add four sums together
    }else{
      perror("Thread join failed");
      exit(-1);
    }
 }
 //get the end time in microseconds
 gettimeofday(&end_time, NULL);

 end_time_microseconds = end_time.tv_sec * 1000000 + end_time.tv_usec;

 //calculate the time passed
 double time_passed = end_time_microseconds - start_time_microseconds;

 cout << "Sum is: " << sum << endl;
 cout << "Running time is: " << time_passed << " microseconds" << endl;

 exit(0);
}


//Prime function
void* Prime(void* index){
  int temp_index;

  temp_index = *((int*)index);
  long  sum_t = 0;

  for(long i = start_numbers[temp_index]; i <= numbers[temp_index]; i++){
        for (int j=2; j*j <= i; j++)
        {
            if (i % j == 0) 
            {
                break;
            }
            else if (j+1 > sqrt(i)) {
                sum_t++;
            }
         }

  }

  cout << "Thread " << temp_index << " terminates" << endl;
  pthread_exit( (void*) sum_t);
}```

Solution

  • This is because, you used 10^6 instead of 10^7.

    Also, added some corner cases for numbers 1, 2 and 3:

    //Prime function
    void* Prime(void* index){
      int temp_index;
    
      temp_index = *((int*)index);
      long  sum_t = 0;
    
      for(long i = start_numbers[temp_index]; i <= numbers[temp_index]; i++){
    
          // Corner cases
    
          if(i<=1)continue;  
          if (i <= 3){
                sum_t++;
                continue;
          }
    
    
          for (int j=2; j*j <= i; j++)
          {
              if ((i % j == 0) ||  (i %( j+2))==0 ) 
              {
                  break;
              }
              else if (j+1 > sqrt(i)) {
                  sum_t++;
              }
           }
    
      }
    
        cout << "Thread " << temp_index << " terminates" << endl;
        pthread_exit( (void*) sum_t);
    }
    

    I tested your code with correct number and got the correct number of primes as output:

    Thread 0 terminates
    Thread 1 terminates
    Thread 2 terminates
    Thread 3 terminates
    Sum is: 664579
    Running time is: 4.69242e+07 microseconds
    

    Thanks to @chux - Reinstate Monica for pointing this out