0

I need to calculate 2 different functions which using the same parameters (only for read). After I had made the program multithreaded, program running needs 2x time (instead of 0.5x). I am new in multi thread programming but I've suspected for false sharing.

My original code (cut):

#include <iostream>

double frac_twins(double mu, double sigma,p){
    return 1;
}
double dist_twins(double mu, double sigma,p){
    return 2;
}

int main(){

int n_t=100;

double* num_t = new double[n_t];
double* dist_t = new double[n_t];

double mu=2; double sigma=1;
double num,dist;

for(double p=0.001; p<=0.101;p+=0.001){

    num=frac_twins(mu,sigma,p);
    dist=dist_twins(mu,sigma,p);

      num_t[i]=num;
      dist_t[i]=dist;
      i++;
}

return 0;
}

Works fine. Then I'd tried to use threads:

#include <iostream>
#include <thread>

double frac_twins(double mu, double sigma,p){
    return 1;
}
double dist_twins(double mu, double sigma,p){
    return 2;
}

int main(){

int n_t=100;

double* num_t = new double[n_t];
double* dist_t = new double[n_t];

double mu=2; double sigma=1;
double num,dist;

for(double p=0.001; p<=0.101;p+=0.001){

      std::thread t1([&num,mu,sigma,p](){
    num=frac_twins(mu,sigma,p);
      });
      std::thread t2([&dist,mu,sigma,p](){
    dist=dist_twins(mu,sigma,p);
      });

      t1.join();
      t2.join();

      num_t[i]=num;
      dist_t[i]=dist;
      i++;
}

return 0;
}

Which works, but 2x times slower. Then I've tried to 'free' variables 'mu, sigma and p', but it still 2x times slower:

#include <iostream>
#include <thread>

double frac_twins(double mu, double sigma,p){
    return 1;
}
double dist_twins(double mu, double sigma,p){
    return 2;
}

int main(){

int n_t=100;

double* num_t = new double[n_t];
double* dist_t = new double[n_t];

double mu=2; double sigma=1;
double mu2=2; double sigma2=1; double p2;

double num,dist;

for(double p=0.001; p<=0.101;p+=0.001){

      std::thread t1([&num,mu,sigma,p](){
    num=frac_twins(mu,sigma,p);
      });
      mu2=mu; sigma2=sigma; p2=p;
      std::thread t2([&dist,mu2,sigma2,p2](){
    dist=dist_twins(mu,sigma,p);
      });

      t1.join();
      t2.join();

      num_t[i]=num;
      dist_t[i]=dist;
      i++;
}

return 0;
}
Tom Solid
  • 2,226
  • 1
  • 13
  • 32
  • 1
    You are starting a new thread every iteration. Thread startup is a costly process, however. This might explain your performance loss. – Ben Steffan Apr 23 '17 at 13:40

1 Answers1

1

The functions you are calling in your threads do so little work, the cost of starting up those threads exceeds the gain you get by using multiple threads. False sharing has nothing to do with it.

Since mu, sigma, and p are passed by value, these can be shared between the two threads (and, in any event, are copied as part of the lambda function overhead).

1201ProgramAlarm
  • 32,384
  • 7
  • 42
  • 56