Intel® oneAPI Threading Building Blocks
Ask questions and share information about adding parallelism to your applications when using this threading library.
The Intel sign-in experience has changed to support enhanced security controls. If you sign in, click here for more information.

parallel_for of a several of iterator


Dear all,

I am now considering how to iterate several of iterator in tbb::parallel_for,

because I believe iterator's access is faster than index access.

For example, is the below sequential code converted to tbb::parallel_for sentence?

In addition, though this example has two iterators,

I need to handle more iterators'case.

I would appreciate it if you could explain to me.

== Example code ================================================== 

std::vector<ClsA>::iterator iterA = A.begin(); // A is data of std::vector<ClsA>
std::vector<ClsB>::iterator iterB = B.begin(); // B is data of std::vector<ClsB>
 for (; iterB != B.end(); iterB++, iterA++)
  (*iterB) = (*iterA).method();



Best regards,

Mitsuru Nishikawa

0 Kudos
2 Replies

Dear Mitsuru,

To iterate over two or more iterators, you may want to consider the zip iterator approach (e.g. tbb::zip_iterator or boost::zip_iterator).

because I believe iterator's access is faster than index access.

It depends on iterator type and the compiler capabilities. If you have a random access iterator (e.g. your example with std::vector), the performance should be the same in the both cases. If we are speaking about parallel loops, usually, it is difficult to apply them for non-random access iterators. So, choosing between index approach vs iterator approach, I would recommend considering convenience related aspects rather than abstract performance considerations.



Dear Alex,


I always appreciate your helpful teaching, thank you very much.

I will try tbb::zip_iterator and check the performance.

By the way, if you do not mind,

I would appreciate it if you could teach my comment in parallel_for of "Mixture of data and task parallelization in TBB " thread,

about scalable performance.


Kind regards,

Mitsuru Nishikawa