Reputation: 135
I'm trying to learn how to use multithreading and then I made a code (see below). As expected the parallel function takes 2.6s while the non-parallel takes 6.4s.
Then I tried to modify the compilation by use of the optimization -O3 available at gcc. With that the non-parallel function took 0s and the parallel one took 2s! Showing that -O3 didn't act on the parallel function.
Why didn't the optimization level worked with the thread?? Is there some fundamental problem? Did I just make something wrong?
Follows the code:
#include <iostream>
#include <ctime>
#include <cmath>
#include <vector>
#include <thread>
using namespace std;
#define N 1E8
#define num_sum std::thread::hardware_concurrency()
void fS(void){
double sum;
int i;
for(i=0;i<N;i++){
sum += sin(i*0.1);
}
// cout << endl << sum << endl;
return;
}
void fS_N(vector<double>& sum,int j){
int i;
int imin,imax,intervalo;
intervalo = N/num_sum;
imin = j*intervalo;
imax = (j+1)*intervalo;
for(i=imin;i<imax;i++){
sum[j] += sin(i*0.1);
}
return;
}
int main(){
clock_t t;
cout << "# disponiveis de nos/cpu: " << num_sum << endl;
//no parallel
t = clock();
fS();
t = clock()-t;
cout << "\n Time (no parallel):";
cout << ((double)t)/CLOCKS_PER_SEC << endl;
//parallel
t = clock();
vector<thread> allsum;
vector<double> Svalue(num_sum,0.);
int j;
for(j = 0; j<num_sum; j++){
allsum.push_back(thread (fS_N, ref(Svalue),j) );
}
for(auto &t : allsum){
t.join();
}
double Soma=0.;
for (j=0;j<num_sum;j++){
Soma += Svalue[j];
}
// cout << Soma << endl;
t = clock()-t;
cout << "\n Time (parallel):";
cout << ((double)t)/CLOCKS_PER_SEC << endl;
return 0;
}
Upvotes: 3
Views: 249
Reputation: 44258
Your "no parallel" function fS()
calculates values into local variable which does not go anywhere, where fs_N()
do calculation on it's arguments. So when optimization enabled compiler eliminates all code for fS()
as it does not affect anything. You need to store that result somewhere (probably return for that function and maybe print it out later):
double fS(void){
double sum;
int i;
for(i=0;i<N;i++){
sum += sin(i*0.1);
}
// cout << endl << sum << endl;
return sum;
}
Upvotes: 2