3
import pycuda.driver as cuda
import pycuda.autoinit
from pycuda.compiler import SourceModule
import numpy
a = numpy.random.randn(4,4)
a = a.astype(numpy.float32)
a_gpu = cuda.mem_alloc(a.nbytes)
cuda.memcpy_htod(a_gpu, a)
mod = SourceModule("""
__global__ void doublify(float *a)
{
int idx = threadIdx.x + threadIdx.y*4;
a[idx] *= 2;
}
""")

I just installed CUDA 9.0 and pycuda, and I am following the tutorial to run the first cuda program. But it always turns out error:

CompileError: nvcc compilation of c:\users\rl74173\appdata\local\temp\tmp6nww2c\kernel.cu failed

I did some research and find some answers to this before. So I add this before running:

import os
os.system("vcvarsamd64.bat")

But it is still error.

I also see someone figure it out by adding line below to nvcc.profile

COMPILER-BINDIR = C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\bin\amd64

I installed visual studio community 2017,so in my case, I tried

COMPILER-BINDIR = C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\atlmfc\lib\amd64

But it doesn't help.

talonmies
  • 70,661
  • 34
  • 192
  • 269
billinair
  • 93
  • 1
  • 11
  • Did you ever fix this? Where is `nvcc.profiles` located? edit: by the way, shouldn't this have been "C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\bin\Hostx64\amd64" or more recently "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.12.25827\bin\Hostx64\x64" – JHBonarius Dec 17 '17 at 10:08
  • I have tried a lot of different ways in different machines, but never succeed yet. I cannot figure out why, cuda just doesn't work with me. – billinair Dec 19 '17 at 03:37
  • I've managed to get CUDA v9.1.xxx to work with Visal Studio 2017 by installing an older version (V15.4). I can create a DLL now, which I can probably source from Python. However, I would prefer using PyCuda. Still having this error though. – JHBonarius Dec 19 '17 at 12:03
  • Ok, I've added a `print(cmdline)` in PyCuda's compiler.py. I've run the *exact* command in a windows cmd window (with vcvars64 run): `nvcc --cubin -arch sm_50 -m64 -Ic:\programdata\anaconda3\lib\site-packages\pycuda\cuda kernel.cu`... and it works! Why doesn't pycuda work then.... – JHBonarius Dec 19 '17 at 14:38
  • better idea: I've added `print(stdout)` after the call. Output: `"nvcc fatal : Cannot find compiler 'cl.exe' in PATH"`. So it seems the call to `vcvars[...].bat` does not change the PATH correctly. – JHBonarius Dec 19 '17 at 18:13

1 Answers1

1

OK, so I fixed it for me. The problem is that running vcvars64.bat sets the path environment in a subshell... and then closes it, so the set path disappears again.

What you want, is to change the path yourself: add the path to the cl.exe compiler file. For that, I referenced this post. In my case, I had to add this at the start to my .py file:

import os
if (os.system("cl.exe")):
    os.environ['PATH'] += ';'+r"C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.11.25503\bin\HostX64\x64"
if (os.system("cl.exe")):
    raise RuntimeError("cl.exe still not found, path probably incorrect")

I hope this works for you.

Edit: you need to run a MSVS version compatible with CUDA. I.e. CUDA v9.0 doesn't support MSVS2017 and CUDA v9.1 only supports version 15.4, not later versions. Try if it works by running nvcc.exe from the Native Tools Command Prompt for Visual Studio.

JHBonarius
  • 10,824
  • 3
  • 22
  • 41
  • Thanks! I'd like to have a try. – billinair Dec 20 '17 at 03:38
  • I tried with old versions but failed again. So I just move on to Visual Studio 2017 and CUDA 9.0, but I am confused now. After I installed these two, do I still need to add certain path to the system environment? Or do I still need to add any lines into CUDA files? BTW: I installed the pycuda from the PyCUDA prebuilt binary from Christoph Golke. In Anaconda prompt tpye: pip install \pycuda‑2017.1.1+cuda9185‑cp36‑cp36m‑win_amd64.whl – billinair Jan 05 '18 at 21:00
  • CompileError: nvcc compilation of C:\Users\rl74173\AppData\Local\Temp\tmpy396ip6k\kernel.cu failed – billinair Jan 05 '18 at 21:11
  • @billinair: you should probably edit the `compiler.py`, to see the actual error nvcc is throwing. – JHBonarius Jan 05 '18 at 21:41
  • @billinair looked it up. Vs2017 is only supported form Cuda 9.1. And even then, you should install vs v15.4, not the latest version. – JHBonarius Jan 06 '18 at 07:33
  • @billinair Also, `cuda9185` in the PyCuda package you used refers to Cuda v9.1.85. So if you installed CUDA v9.0 it will not be compatible with that PyCuda package. – JHBonarius Jan 06 '18 at 13:39
  • I cannot locate the right compiler.py, because no pycuda in the folder C:\Users\******\AppData\Local\Continuum\anaconda3\pkgs – billinair Jan 08 '18 at 22:15
  • that's because it is probably in `C:\Users******\AppData\Local\Continuum\anaconda3\Lib\site-packages\pycuda`. In the file, around line 120, you have the line `result, stdout, stderr = call_capture_output(cmdline, cwd=file_dir, error_on_nonzero=False)`. Below that you can add print(result); print(stdout); print(stderr), etc. – JHBonarius Jan 09 '18 at 14:52
  • I add some lines and get this. `b"nvcc fatal : Value 'sm_21' is not defined for option 'gpu-architecture'\r\n"` As I search online, it seems that my GPU is out of date or the driver is too new for the GPU. Not very sure. – billinair Jan 10 '18 at 17:03
  • Yep, too old and have been removed. Can't help you there. – JHBonarius Jan 10 '18 at 17:47