Unified memory on a std::ofstream pointer

Hi,

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;
}

Hi Pierre,

I’m not sure what’s wrong here so have filed a problem report, TPR #35338, and sent it to engineering for investigation.

Thanks,
Mat

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.

1 Like

Thanks Mat !

We are going to update our Nvidia HPC SDK to see that.

FYI, this ā€œinterceptā€ mode is now supported and documented in 24.5. Note that they changed the name of the flag to ā€œ-⁠gpu=interceptdeallocationsā€.

1 Like