Is it possible for nvc++ to use function objects defined in a different translation unit to the one calling a C++ standard library algorithm? As a small example, if the contents of 3 files, main.cpp, squared.cpp and squared.hpp are:
… a command such as nvc++ -stdpar -std=c++17 squared.cpp main.cpp will fail to link due to an undefined reference to squared::operator()(int&). I’m using nvc++ from v21.9 of the HPC SDK on Ubuntu 20.10.
The C++ standard doesn’t have a method to decorate routines to note that a device version needs to be created. Hence we rely on the compiler being able to implicitly generate this. However, it does need to discover this and can’t do so across compilation units so instead needs to rely on non-standard extensions here.
You can either decorate the routine with the CUDA “hostdevice” attribute, or the OpenACC “acc routine” pragma.
That’s a good question. We have that functionality in OpenACC (-acc=routineseq i.e. compile every routine for the device), but I don’t think we’ve tested it with stdpar.
I just tried on your simple example and it looks like the compiler attempts to offload some of the implicitly include Thrust routines. Plus it looks like it’s trying redefine some device attribute routines. It is a big hammer approach so likely would need refinement before it could be used with stdpar.
Let me ask our C++ folks if it would even be feasible to implement, and if so, I can add a request for enhancement (RFE).
Yes, this is still the way I’d recommend handling compilation of device routines who’s definition is in a separate source file.
Another option to try if you can’t decorate the routines would be to use cross-file inlining so the call isn’t needed. Though cross-file inlining is a bit of a pain since it requires a two pass compile. First with the “-Mextract=lib:libname” flag across all the sources to create an inline library, and then a second pass with “-Minline=lib:libname” to inline the routines. Not all routines can be inlined, in particular larger routines, so you still might need to fall back to using “acc routine”.