Calling cudaMallocManaged always returns null, but only if a cu file

I’m having a really peculiar issue. I’m on ubuntu 16.04. The samples work fine - however if I have a cu file test.cu with the contents

#include <assert.h>
#include <cuda_runtime.h>

int main(int argc, char **argv) {
int *tmp = 0;
cudaMallocManaged(&tmp, 256);
assert(tmp);
cudaFree(tmp);
}

and compile it with

/usr/local/cuda-9.0/bin/nvcc -o test test.cu

and then run test I get

test: test.cu:7: int main(int, char**): Assertion `tmp’ failed.
Aborted (core dumped)


The allocation failed? What?

Ok so I rename the file to test.cpp and compile it, and run it – works FINE.

W T F? lol

So, the samples all build in .cu files, so what makes this different? I don’t get it.

can you try building this for a higher compute architecture by passing the appropriate -arch switch? Maybe the nvcc defaults target an architecture that did not yet support unified memory.

try e.g.

-arch=sm_61

Christian

yeah that did it. weird ;) thanks!!