-1

i want to floor all the items in a std::vector and use bellow code :

for(const float &number : numbers)
     number = floor(number);

but I need to use a parallel algorithm for this operation.is there any parallel algorithm in standard c++ for do efficiently an action like floor function on all items?

SajadBlog
  • 514
  • 2
  • 12
  • 1
    Does this answer your question? [Parallel Loops in C++](https://stackoverflow.com/questions/36246300/parallel-loops-in-c) – user202729 Jan 20 '21 at 14:22
  • consider also vectorize your code which usually result in more speed improvement unless your CPU has too many cores – phuclv Jan 20 '21 at 16:28

1 Answers1

1

If you can use C++17, you can use the ExecutionPolicy argument from std::for_each to parallelize your loop

#include <algorithm>
#include <cmath>
#include <execution>

std::for_each(std::execution::par_unseq,
              numbers.begin(),
              numbers.end(),
              [](double& number){ number = std::floor(number); });
Cory Kramer
  • 114,268
  • 16
  • 167
  • 218