I notice a weird error when running this with -gpu=managed option:
nvc++ is 23.5.
Thanks
nvc++ -mp -target=gpu -cuda -gpu=managed -o test test.cpp && ./test
/usr/bin/ld: warning: /export/home/catA/pl254994/.tmp_TRUST_trust/pgcudafatd7icjHgPkJJn.o: missing .note.GNU-stack section implies executable stack
/usr/bin/ld: NOTE: This behaviour is deprecated and will be removed in a future version of the linker
vector OK
free(): invalid pointer
Aborted (core dumped)
test.cpp:
#include <fstream>
#include <memory>
#include <vector>
#include <iostream>
using std::vector;
using std::ofstream;
int main() {
vector<int>* a = new vector<int>(10);
delete a;
std::cerr << "vector OK" << std::endl;
ofstream* ofstream_ = new ofstream("file.txt", std::ios::out);
delete ofstream_;
std::cerr << "ofstream KO" << std::endl;
return 1;
}
Engineering investigated. What appears to be happening is that the I/O library is allocating memory but the deallocation is being done in the header files. Hence the deallocation is getting replaced with the calls to cudaManagedFree which in turn causes the error given the memory isnāt managed.
The work around is to use the flag ā-gpu=managed:interceptā which instead of replacing the allocation and deallocation with the managed versions at compile time, the runtime intercepts all memory allocation/deallocation calls. While available in 24.1 as a hidden option, it wont be publicly documented until 24.3, and should be considered experimental.
FYI, this āinterceptā mode is now supported and documented in 24.5. Note that they changed the name of the flag to ā-ā gpu=interceptdeallocationsā.