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 with the contents

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

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

and compile it with

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

and then run test I get

test: 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.



yeah that did it. weird ;) thanks!!