Parallel fill std :: vector with zero

I want to fill std::vector<int>with zero using openmp. How to do it fast?

I heard that the vector loop for setting each item to zero was slow, but std::fillwas much faster. Is this still true?

The fastest way to reset each std :: vector <int> to 0

Do I need to manually divide std::vector<int>into regions, use a loop #pragma omp forfor each thread, and then use std::fillin a loop?

+4
source share
1 answer

You can break the vector into pieces for each stream that needs to be filled with std::fill:

#pragma omp parallel
{   
    auto tid = omp_get_thread_num();
    auto chunksize = v.size() / omp_get_num_threads();
    auto begin = v.begin() + chunksize * tid;
    auto end = (tid == omp_get_num_threads() -1) ? v.end() : begin + chunksize);
    std::fill(begin, end, 0);
}

, chunksize / (128 = 32 int s). , v.data() . , .

24 Haswell - 9x: 3.6s 1 , 0.4 24 , 4.8B ints = ~ 48 /, . .

, ( ) , . , , , , , , NUMA node.

, std::fill(..., 1); , std::fill(..., 0) , 24 . gcc 6.1.0, icc 17.0.1. , .

+3

Source: https://habr.com/ru/post/1668827/


All Articles