The benefit here is that applying f to all the elements can be done in any order without changing program output since there are no dependencies.
Do C++ or ISPC do any checking for the UnaryOperation (or is UnaryOperation a class)? That is, to make sure that the operations really are independent.
@jiaju UnaryOperation is an operation that takes only one input (as we expect from a map). So it should read just one input and produce just one output, otherwise it would not make sense to use in a map. For example in C++, ++
is a unary operation since it takes in a value and gives out an incremented version of that value.
You can read more about this here: https://en.cppreference.com/w/cpp/algorithm/transform
. I don't think C++ does any checking explicitly, but if you look at the signature for UnaryOperation
: Ret fun(const Type &a);
I think it would be difficult to make it have side effects.
Nvidia's Thrust library also has a transform function which accelerates map on GPUs by taking advantage of their parallelism https://docs.nvidia.com/cuda/thrust/index.html#transformations
Please log in to leave a comment.
I was previously confused as to how the map worked/what it was exactly mapping (i.e. function that takes a func as an arg). This visualization really helped me understand how the map worked.