Is it much faster to re-initialize a vector using OpenMP threads?

前端 未结 2 506
孤城傲影
孤城傲影 2021-01-23 15:03

I\'m using OpenMP libraries for parallel computing. I use C++ vectors, whose size is usually in the order of 1*10^5. While going through iteration process, I need to re-initiali

2条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-23 15:34

    The general answer would need to be "it depends, you have to measure" since initialization in C++ can be, depending on the type, trivial or very expensive. You did not provide an awful lot of detail, so one has to guess a bit.
    If a class has a computionally expensive constructor, parallelizing work may very well be worth it.

    Your specific wording "initialize to value" suggests that your vector holds POD (say, for example, integers?). I will assume that this is the case.

    Assuming this, parallelizing will almost certainly not be any faster. This operation is bound by memory bandwidth, and one CPU thread should be able to saturate memory bandwidth to approximately 99%.

    Parallelizing may however very well be slower, due to several reasons (which I'm not going to elaborate, enough being said that it's unlikely to be faster).

提交回复
热议问题