I have the following sorted vectors:
vector<unsigned> vector1;
vector<unsigned> vector2;
vector<unsigned> vector3;
...
vector<unsigned> vector30000;
I need to perform the intersection of vector1 with the rest of the vectors. i.e. I need to perform the following intersections:
vectori1=intersection of vector1 with vector2;
vectori2=intersection of vector1 with vector3;
vectori3=intersection of vector1 with vector4;
...
vectori30000=intersection of vector1 with vector30000;
Now I need to find out all the non-empty vector's vectori1,vectori2,vectori3,...,vectori30000 and store them in "intersected" vector.
In order to do so I wrote the following serialized code:
int main()
{
vector<unsigned> vector1;
vector1.push_back(10); vector1.push_back(20); vector1.push_back(30);
vector<vector<unsigned> > vecOfVectors;
vector<unsigned> vector2;
vector2.push_back(1); vector2.push_back(5); vector2.push_back(8); vector2.push_back(10);
vecOfVectors.push_back(vector2);
vector<unsigned> vector3;
vector3.push_back(3); vector3.push_back(20); vector3.push_back(25);
vecOfVectors.push_back(vector3);
vector<unsigned> vector4;
vector4.push_back(28); vector4.push_back(29); vector4.push_back(39);
vecOfVectors.push_back(vector4);
vector<vector<unsigned> > intersected;
for(vector<vector<unsigned> >::iterator it=vecOfVectors.begin(),l=vecOfVectors.end();it!=l;++it)
{
vector<unsigned> intersectedLocal;
std::set_intersection(vector1.begin(),vector1.end,(*it).begin(),(*it).end(),back_inserter(intersectedLocal));
if(!intersectedLocal.empty())
intersected.push_back(intersectedLocal);
}
}
In order to improve performance I need to parallelize the intersection algorithm. I am not getting how to do the same. My attempt is shown below:
void multThreadIntersect(vector<unsigned>& vector1, vector<vector<unsigned> >::iterator it, int size,int i,vector<vector<unsigned> >& intersected,vector<int>& idIntersected)
{
if(i<size)
{
vector<unsigned> intersectedLocal;
std::set_intersection(vector1.begin(),vector1.end,(*it).begin(),(*it).end(),back_inserter(intersectedLocal));
it++;
idIntersected.push_back(i);
intersected.push_back(intersectedLocal);
auto future = std::async(std::launch::async,multThreadIntersect, vector1, it, size,intersected,idIntersected);
future.wait();
i++;
}
else
{
return;
}
}
int main()
{
vector<unsigned> vector1;
vector1.push_back(10); vector1.push_back(20); vector1.push_back(30);
vector<vector<unsigned> > vecOfVectors;
vector<unsigned> vector2;
vector2.push_back(1); vector2.push_back(5); vector2.push_back(8); vector2.push_back(10);
vecOfVectors.push_back(vector2);
vector<unsigned> vector3;
vector3.push_back(3); vector3.push_back(20); vector3.push_back(25);
vecOfVectors.push_back(vector3);
vector<unsigned> vector4;
vector4.push_back(28); vector4.push_back(29); vector4.push_back(39);
vecOfVectors.push_back(vector4);
vector<vector<unsigned> >::iterator it=vecOfVectors.begin();
int size=vecOfVectors.size();
int i=0;
vector<vector<unsigned> > intersected;
vector<int> idIntersected; //contains those i's whose intersection was non-zero
long unsigned int nThreads = std::thread::hardware_concurrency();
multThreadIntersect(vector1,it,size,i,intersected,idIntersected);
cout<<"id intersected vector:";
for(vector<int>::iterator it=idIntersected.begin(),l=idIntersected.end();it!=l;++it)
cout<<" "<<(*it);
cout<<"\n";
}
The gcc version that I am using is: gcc (GCC) 4.8.2 20140120 (Red Hat 4.8.2-15)
I have already defined _GLIBCXX_PARALLEL in my program. However, since vector1's intersection with vector2,...,vector30000 are independent of each other. Therefore, I am thinking of parallelly intersecting vector1 with vector2, vector1 with vector3, and vector1 with vector30000