dimanche 4 décembre 2016

Initializing vector in multithread

I have a large vector std::vector<some_class> some_vector which I need to initialize with different values of constructors some_class(constructor_parameters). The normal way to do it would be something like

std::vector<some_class> some_vector
some_vector.reserve(length)

for (...) some_vector.push_back(some_class(constructor_parameters))

But as this vector is large I want to do this in parallel. Is there a way how to split the vector and push_back at different position of vector so each thread can start initializing different part of vector?

I read some answers with splitting / joining vector and haven`t found anything useful. As my vector is really large I have to avoid something like creating new vector for each thread and then copying them into the original one - I can use only one big chunk of memory.

I tried to use some_vector.at(some_loc) = some_class(constructor_parameters) but this isn`t working with uninitialized vector.

I can initialize vector to some dump values and then use at to initiaize it to proper values, but it is not efficient...

So my question - how to efficiently (in terms of memory consumption and computing time) initialize large vector?

Aucun commentaire:

Enregistrer un commentaire