Search code examples
cudathrust

Thrust Reduce with binary_function and multiple types


How do you use thrust reduce with binary_functions with multiple types? In my case I have a struct FaceUV which has a member 'distance'. I want to count all of the FaceUV's with a distance != 0. How do I do this?

I thought it was this, but it does not compile:

struct FaceHasUVCmp : public thrust::binary_function<FaceUV, uint32_t, uint32_t> {
    __device__
        uint32_t operator()(const FaceUV& o1, const uint32_t& count) const {
        return count + (o1.distance != 0);
    }
};

float get_percent_of_FACES_with_UVs(thrust::device_ptr<FaceUV> face_uvs, unsigned int size){


    uint32_t num_with_UVs = thrust::reduce(thrust::cuda::par, face_uvs, face_uvs + size, 0, FaceHasUVCmp());

    return num_with_UVs / (float)size;

}

Solution

  • it appears this is similar to this question:

    Thrust reduce not working with non equal input/output types

    "As talonmies notes, your reduction does not compile because thrust::reduce expects the binary operator's argument types to match its result type"

    So it is not possible with thrust::Reduce. I still havent figures out how to accomplish what I want to do.

    edit: I may need to use thrust::transform_reduce instead, but I cant figure out how to use that either.