Intel® oneAPI Threading Building Blocks
Ask questions and share information about adding parallelism to your applications when using this threading library.

Push_back API concurrent vector

PritamLakhmani
Beginner
2,585 Views

Hi,

Through push_back API of concurrent vector container, I tried to push_back around (10000000) my custom objects, my sample application is a single threaded application.

I tested push_back API, through two tbb versions , first one is old tbb (version libtbb2.4.2~20130725) & second one is new oneapi tbb (version oneapi-tbb-2021.5.0-lin.tgz).

Through oneapi tbb (oneapi-tbb-2021.5.0-lin.tgz)),Time taken by push_back API in pushing 10000000 custom objects is approximately twice than time taken by push_back API of old tbb (libtbb2.4.2~20130725).

I am using Ubuntu 20.04.5 version on my desktop PC. As per my view, time taken by latest oneapi tbb APIs should be less than (or equal) to the time taken by old tbb APIs.

 

Pls suggest.
Regards,
Pritam

0 Kudos
24 Replies
Mark_L_Intel
Moderator
376 Views

My last answer was not correct.  The concurrent_vector is not contiguous, like a std::vector, but it does keep track of the index internally. And because it does that, its operator- is implemented in constant time, not O(n).

See https://github.com/oneapi-src/oneTBB/blob/5e91b2c07d82e2b983551d26a24d13f5155f794b/include/oneapi/tbb/concurrent_vector.h#L187

 

So, you can compute (it - v.begin()) in constant time, which would be equal to the index

#include <iostream>
#include <oneapi/tbb.h>

int main(int argc, char** argv){

  tbb::concurrent_vector<int> conc_vec;
  tbb::concurrent_vector<int>::iterator it;
  int index_of_pushed_element;

  int size = 10;

  tbb::parallel_for(5, size, 1, [&](int i){
      it = conc_vec.push_back(i);
      index_of_pushed_element = (it - conc_vec.begin());
      std::cout << "index_of_pushed_element = " << index_of_pushed_element << "\n";
    });

   for(int i = 0; i < conc_vec.size(); i++)
    printf(" %d", conc_vec[i]);
   printf("\n");

}

produces:

icpx tbb_concurrent_v.cpp -tbb
./a.out
index_of_pushed_element = 0
index_of_pushed_element = 1
index_of_pushed_element = 2
index_of_pushed_element = 3
index_of_pushed_element = 4
 5 6 7 8 9

0 Kudos
Mark_L_Intel
Moderator
368 Views

Please let us know if last post answered your question.

0 Kudos
SeshaP_Intel
Moderator
348 Views

Hi,


Has the information provided above helped? If yes, could you please confirm whether we can close this thread from our end?


Thanks and Regards,

Pendyala Sesha Srinivas


0 Kudos
Mark_L_Intel
Moderator
317 Views

We have not heard back from you. I assume that your issue is resolved. If you need any additional information, please post a new question -- ideally in a new thread -- this thread will no longer be monitored by Intel.


0 Kudos
Reply