I am new to multithreading and I will try to be as clear as possible. I am creating a multithread algorithm in C++ with standard library, std::thread. My code is compiling and running with no errors. I put the code for two threads. Two threads are creating with different id than the main one (I checked with getid() and the thread window in Visual Studio). The problem is that I have no gain in time, and the % utilization of my CPU is the same so it seems that they run on the same CPU thread.
Does it means that threading doesn't automatically mean running on different CPU cores and threads to gain time?
Do I need to add instructions or combine this code with a multithread library? Or is there just a mistake? Any help would be really appreciated, thanks
double internalenergy=0;
unsigned int NUM_THREADS = std::thread::hardware_concurrency();
NUM_THREADS=2;
vtkIdType num_cells = referencemesh_->GetNumberOfCells();
int start_node[8];
int end_node[8];
int intervalle=num_cells/NUM_THREADS;
for( unsigned int i=0; i < NUM_THREADS; ++i )
{
start_node[i] = (float)num_cells*i/NUM_THREADS;
end_node[i] = (float)num_cells*(i+1)/NUM_THREADS;
}
const double* pointeurpara;
pointeurpara=¶ms(0);
double value1=0;
double* P_value1;
P_value1=&value1;
double value2=0;
double* P_value2;
P_value2=&value2;
std::thread first(threadinternalenergy,activemesh,referencemesh_,start_node[0],P_value1,pointeurpara);
std::thread second (threadinternalenergy,activemesh,referencemesh_,start_node[1],P_value2,pointeurpara);
first.join();
second.join();
double displacementneighpoint=*P_value1+*P_value2;
Here is the source code of threadinternalenergy
void threadinternalenergy(vtkSmartPointer<vtkPolyData>,
activemesh,vtkSmartPointer<vtkPolyData> referencemesh,int startcell,double*
displacementneighpoint,const double* p)
{
unsigned long processnumber;
processnumber=GetCurrentProcessorNumber();
cout<<"process "<<processnumber<<endl;
unsigned int NUM_THREADS = std::thread::hardware_concurrency();
NUM_THREADS=2;
vtkIdType num_cells = referencemesh->GetNumberOfCells();
int intervalle=num_cells/NUM_THREADS;
int endcell=startcell+intervalle;
//cout<<"start cell "<<startcell<<endl;
//cout<<"end cell "<<endcell<<endl;
int numcell_th=startcell-endcell;
vtkSmartPointer<vtkEdgeTable> vtk_edge_tableT =
vtkSmartPointer<vtkEdgeTable>::New();
vtk_edge_tableT->InitEdgeInsertion(numcell_th*3);
// Initialize edge table
vtkSmartPointer<vtkEdgeTable> vtk_edge_table =
vtkSmartPointer<vtkEdgeTable>::New();
vtk_edge_table->InitEdgeInsertion( numcell_th*3 );
cout<<"start cell "<<startcell<<endl;
for( vtkIdType i=startcell; i < endcell; ++i )
{
vtkCell* cell = referencemesh->GetCell( i );
// cout<<"cell"<<endl;
// Traverse edges in cell -- assuming a linear cell (line, triangle; NOT
rectangle)
for( vtkIdType j=0; j < cell->GetNumberOfEdges(); ++j )
{
// cout<<"edge"<<endl;
vtkCell* edge = cell->GetEdge( j );
vtkIdType pt0 = edge->GetPointId(0);
vtkIdType pt1 = edge->GetPointId(1);
//consider edge if a displacement has been point by at least one point
//if(params(pt0)!=0 || params(pt1)!=0){
if( vtk_edge_table->IsEdge( pt0, pt1 ) == -1 ){ // If this edge
is not in the edge table
vtk_edge_table->InsertEdge( pt0, pt1 );
//acess point coordinate
// vtkPoints* listpoints=edge->GetPoints();
double p0 [3];
double p1 [3];
referencemesh->GetPoint(pt0,p0);
referencemesh->GetPoint(pt1,p1);
//2e Mesh (Mesh transform)
double p0T [3];
double p1T [3];
activemesh->GetPoint(pt0,p0T);
activemesh->GetPoint(pt1,p1T);
// If this edge is not in the edge table
vtk_edge_tableT->InsertEdge( pt0, pt1 );
//find displacement difference for 2 points sharing an edge
double squaredDistancep0 =
vtkMath::Distance2BetweenPoints(p0, p0T);
double distancep0 = sqrt(squaredDistancep0);
double squaredDistancep1 =
vtkMath::Distance2BetweenPoints(p1, p1T);
double distancep1 = sqrt(squaredDistancep1);
double difference = abs(distancep0 - distancep1);
difference=((difference+1)*(difference+1))-1;
//if(difference>0.25){
//cout<<"grosse difference "<<difference<<endl;
//}
*displacementneighpoint+=difference;
//displacementpointneigh.push_back(difference);
}
//}
}
}
cout<<"Fin du thread "<<endl;
}