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

PushBack API in Latest oneAPI Versions

AdarshSonkar
Beginner
1,053 Views

History of my query  can  be found here :

 https://community.intel.com/t5/Intel-oneAPI-Threading-Building/Push-back-API-concurrent-vector/td-p/1417316/page/2

 

So, My query is regarding the pushback API in Intel latest Tbb version. In the older version of tbb (libtbb2.4.2~20130725), we were able to get an index while doing push_back.

like in this code example :

   vector<my_data_structure> sample_vector;

    int sizes = sample_vector.size();

    index_of_pushed_element = sample_vector.push_back(my_data_structure_element);

    

and code of push_back api in older version of tbb is:

 

#if TBB_DEPRECATED
    size_type push_backconst_reference item )
#else
    /** Returns iterator pointing to the new element. */
    iterator push_backconst_reference item )
#endif
    {
        size_type k;
        void *ptr = internal_push_back(sizeof(T),k);
        internal_loop_guide loop(1ptr);
        loop.init(&item);
#if TBB_DEPRECATED
        return k;
#else
        return iterator(*this, k, ptr);
#endif

 

In this function k variable returning index of last inserted element in vector.

 

 

 

But in the newer version of tbb(oneapi-tbb-2021.6.0-lin.tgz) Intel has changed the code of push_back API. where we get only the iterator of the last push_back element. 

 

 

 push_back function code in new tbb(oneapi-tbb-2021.6.0-lin.tg) : 

 

template <typename... Args>
    iterator internal_emplace_backArgs&&... args ) {
        size_type old_size = this->my_size++;
        this->assign_first_block_if_necessary(default_first_block_size);
        auto element_address = &base_type::template internal_subscript</*allow_out_of_range_access=*/true>(old_size);

        // try_call API is not convenient here due to broken
        // variadic capture on GCC 4.8.5
        auto value_guard = make_raii_guard([&] {
            zero_unconstructed_elements(element_address, /*count =*/1);
        });

        segment_table_allocator_traits::construct(base_type::get_allocator(), element_addressstd::forward<Args>(args)...);
        value_guard.dismiss();
        return iterator(*this, old_size, element_address);
    }
 
iterator push_backconst value_type& item ) {
        return internal_emplace_back(item);
    }

 

 

 

So my query is how we can get the index of elements while doing push_back in vector in a multithreaded system?

 

Note:  We tried to get an index by  computing  (iter- v.begin().

iter is iterator of last index element in v vector.

but there is one problem that this will not work in multithread system because the v.begin() value can changed by other threads. 

this is link of  - operator of vector_iterator class. 

https://github.com/oneapi-src/oneTBB/blob/2110128e4e78647b0d299d07c82ac8e4273964f6/include/oneapi/tbb/concurrent_vector.h#L191

 

 

I have one suggestion if possible please check this:

 

In vector_iterator class of new tbb(oneapi-tbb-2021.6.0-lin.tgz)  which defined in concurrent_vector.h file. In that class  my_index  is defined and this is the value of the index in the iterator but the problem is,  it is defined in private so we can't access it from iterator directly.

 

private:
// concurrent_vector over which we are iterating.
vector_type* my_vector;

// Index into the vector
size_type my_index;

// Caches my_vector *it;
// If my_item == nullptr cached value is not available use internal_subscript(my_index)
mutable value_type* my_item;
}; // class vector_iterator

 

 

link of that line of my_index.

https://github.com/oneapi-src/oneTBB/blob/2110128e4e78647b0d299d07c82ac8e4273964f6/include/oneapi/tbb/concurrent_vector.h#L172

 

So my suggestion is that , if possible please make this variable ( my_index ) public or add some getter function to get this index of iterator so we can get index of element while push_back it by using iterator.

 

 

 

 

0 Kudos
3 Replies
VaishnaviV_Intel
Moderator
994 Views

Hi,

 

Thanks for posting in Intel Communities.

 

We kindly request that you provide us with further details to assist us in resolving your issue. Specifically, we would appreciate if you could provide the following information:

1. A sample reproducer and the steps you followed.

2. Please provide information on how you are attempting to set up multiple threads.

3. We would like to understand how the application's behavior differs when multiple threads are utilized.

4. Have you tried running your code with oneTBB 2021.8.0? If yes, Please do share the results.

 

Thanks & Regards,

Vankudothu Vaishnavi.

 

0 Kudos
VaishnaviV_Intel
Moderator
911 Views

Hi,


We have not heard back from you. Could you please provide us with an update on your issue?


Thanks & Regards,

Vankudothu Vaishnavi.





0 Kudos
VaishnaviV_Intel
Moderator
832 Views

Hi,

 

We have not heard back from you, so we will close this inquiry now. If you need further assistance, please post a new question.

 

Thanks & Regards,

Vankudothu Vaishnavi.

 

0 Kudos
Reply