TransWikia.com

How does thrust determine arguments to pass to functor

Stack Overflow Asked by A_Man on November 11, 2020

My questions are:

1: How does Thrust determine what arguments to send to a functor and in which order to send them? Is it in the order of the input iterator data? I have not found any information on this.
I have seen examples of e.g. thrust transform usage with zip/tuple iterators, which then has two data types, for instance as in this question: CUDA thrust zip_iterator tuple transform_reduce, but never with basically more than 2 arguments to operator(). UPDATE: I guess i can just use a struct object with the wanted data here, and use a 1-argument operator() with a device vector iterator of the struct object..

2: Is there a max number of arguments allowed to pass to the functor operator() for use in a thrust function?

3: In special cases where you want to pass the CUDA kernel thread ID to the functor operator(), in addition to other arguments, how can this be done? Is the pseudocode in the unfinished example below even possible? If so, could you help me with a solution? (Preferably a generic solution if you want n arguments to a thrust functor operator()):

#include <thrust/device_vector.h>

struct functor
{
    __device__ operator()(unsigned int thread_id, int arg2, double arg3, float arg4) // n arguments of different types in general, this is just an example
    {
        // Do some operations here..
    }
};

int main()
{ 
    // How to zip multiple device vector iterators here such 
    // that one can match the given functor argument list for operator()?
    // and thus use the functor. 

   
    return 0;
}

One Answer

How does Thrust determine what arguments to send to a functor and in which order to send them? Is it in the order of the input iterator data? I have not found any information on this

There isn't any information on it because the order of operations in parallel algorithms in thrust is undefined.

Is there a max number of arguments allowed to pass to the functor operator() for use in a thrust function?

Yes. The functors themselves typically have to be models of unary, binary or ternary functions. The algorithms themselves define the form of acceptable functors for predicates and operators. That itself isn't a problem because you have containers you can pass. As I remember it, the thrust::tuple has static template specializations up to something like 10 members.

In special cases where you want to pass the CUDA kernel thread ID to the functor operator(), in addition to other arguments, how can this be done? Is the pseudocode in the unfinished example below even possible?

Never, ever, ever do that. It won't work. If you need a unique sequential identifier for a calculation, use a fancy iterator, like the counting iterator. Pass it via a zip operator to your functor.

Correct answer by talonmies on November 11, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP