Setting up CUDA on MacBook Pro with NVIDIA GeForce GT 650M

Hello,

my name is Carl and I would like to speed up some code using the GPU with CUDA. First step seems to be a very big one. I’m coding with Python 3.6, having the latest version of numba (with the latest anaconda package). Today I downloaded the newest CUDA driver, since my GPU is listed as a CUDA supported GPU.

Unfortunately the example code, which is adding two vectors is not working. See the code below.
It throws an error, quite long. Last line of the error says: numba.cuda.cudadrv.error.CudaDriverError: CUDA initialized before forking, which is an error telling me that my device is not supported. But it is, I’ve verified that.
Contacting the nvidia support doesn’t help. They say this must be an apple related problem. Apple doesn’t provide CUDA support and now im here. I will list all useful information at the bottom of this post.

I really appreciate any help!

Carl

Versions:
Mac OS 10.13 (17A405)
GPU: NVIDIA GeForce GT 650M
CUDA Driver: 9.0.197 (not the newest since the support told me explicitly to try this older version)
GPU Driver: 10.24.28 355.11.10.10.10.160

Full code, which is throwing the error:

import numpy as np
import time

from numba import vectorize, cuda

@vectorize(['float32(float32, float32)'], target='cuda')
def VectorAdd(a, b):
    return a + b

def main():
    N = 3200000

    A = np.ones(N, dtype=np.float32)
    B = np.ones(N, dtype=np.float32)

    start = time.time()
    C = VectorAdd(A, B)
    vector_add_time = time.time() - start

    print("C[:5] = " + str(C[:5]))
    print("C[-5:] = " + str(C[-5:]))

    print("VectorAdd took for % seconds" % vector_add_time)

if __name__ == '__main__':
    main()

Full error:
Traceback (most recent call last):
File “/Users/Carl/Library/Mobile Documents/com~apple~CloudDocs/Mastersthesis/exampleScript.py”, line 26, in
main()
File “/Users/Carl/Library/Mobile Documents/com~apple~CloudDocs/Mastersthesis/exampleScript.py”, line 17, in main
C = VectorAdd(A, B)
File “/Users/Carl/anaconda3/lib/python3.6/site-packages/numba/cuda/dispatcher.py”, line 88, in call
return CUDAUFuncMechanism.call(self.functions, args, kws)
File “/Users/Carl/anaconda3/lib/python3.6/site-packages/numba/npyufunc/deviceufunc.py”, line 290, in call
dev_a = cr.to_device(a, stream=stream)
File “/Users/Carl/anaconda3/lib/python3.6/site-packages/numba/cuda/dispatcher.py”, line 203, in to_device
return cuda.to_device(hostary, stream=stream)
File “/Users/Carl/anaconda3/lib/python3.6/site-packages/numba/cuda/cudadrv/devices.py”, line 211, in _require_cuda_context
get_context()
File “/Users/Carl/anaconda3/lib/python3.6/site-packages/numba/cuda/cudadrv/devices.py”, line 194, in get_context
return _runtime.get_or_create_context(devnum)
File “/Users/Carl/anaconda3/lib/python3.6/site-packages/numba/cuda/cudadrv/devices.py”, line 162, in get_or_create_context
return self.push_context(self.gpus[devnum])
File “/Users/Carl/anaconda3/lib/python3.6/site-packages/numba/cuda/cudadrv/devices.py”, line 40, in getitem
return self.lst[devnum]
File “/Users/Carl/anaconda3/lib/python3.6/site-packages/numba/cuda/cudadrv/devices.py”, line 26, in getattr
numdev = driver.get_device_count()
File “/Users/Carl/anaconda3/lib/python3.6/site-packages/numba/cuda/cudadrv/driver.py”, line 334, in get_device_count
self.cuDeviceGetCount(byref(count))
File “/Users/Carl/anaconda3/lib/python3.6/site-packages/numba/cuda/cudadrv/driver.py”, line 268, in getattr
self.initialize()
File “/Users/Carl/anaconda3/lib/python3.6/site-packages/numba/cuda/cudadrv/driver.py”, line 225, in initialize
self.cuInit(0)
File “/Users/Carl/anaconda3/lib/python3.6/site-packages/numba/cuda/cudadrv/driver.py”, line 288, in safe_cuda_api_call
self._check_error(fname, retcode)
File “/Users/Carl/anaconda3/lib/python3.6/site-packages/numba/cuda/cudadrv/driver.py”, line 322, in _check_error
raise CudaDriverError(“CUDA initialized before forking”)
numba.cuda.cudadrv.error.CudaDriverError: CUDA initialized before forking

Process finished with exit code 1

That GPU driver doesn’t support CUDA 9.

When installing CUDA, you should follow the instructions in the mac install guide carefully, including verification, before trying anything else (e.g. numba code).

http://docs.nvidia.com/cuda/cuda-installation-guide-mac-os-x/index.html#verification

Since the GPU driver doesn’t support CUDA9, is there a list of CUDA versions supported by that driver? Or maybe any chance for new drivers? This seems to be non trivial for the Mac System.

The install guide you listed doesn’t show anything about a relation between GPU driver and CUDA support. It says that before considering CUDA I need to verify that my GPU is supported, which is positive. Then it says I need to download and install the CUDA driver and the CUDA toolkit, which i did.Then they ask for xcode using the clang compiler, which seems to be the default compiler for xcode versions 4.2 and above. I have xcode 9.0.1.

In the end they ask for running sample code. Here is another error.

Calling:
Carls-MacBook-Pro:samples Carl$ make -C 0_Simple/vectorAdd

Returns:
/Developer/NVIDIA/CUDA-9.0/bin/nvcc -ccbin clang++ -I…/…/common/inc -m64 -Xcompiler -arch -Xcompiler x86_64 -gencode arch=compute_30,code=sm_30 -gencode arch=compute_35,code=sm_35 -gencode arch=compute_37,code=sm_37 -gencode arch=compute_50,code=sm_50 -gencode arch=compute_52,code=sm_52 -gencode arch=compute_60,code=sm_60 -gencode arch=compute_60,code=compute_60 -o vectorAdd.o -c vectorAdd.cu
nvcc fatal : The version (‘90000’) of the host compiler (‘Apple clang’) is not supported
make: *** [vectorAdd.o] Error 1

I found this article from 2012:


Which says CUDA4 works well on this very GPU. Unfortunately after installation i get exaclty the same error.

If the GPU Driver is the problem, does anyone know how to change the GPU driver to a driver which supports CUDA9?

I am having the same problem with CUDA when I was trying to do MATLAB GPU tasks – and it really was frustrating.

Coincidentally, I’m using exactly the same model of MacBook Pro as yours.

I’m now using CUDA Driver 9.0.214 (I tried 9.0.197, and it had the same result).
I read from http://www.xlr8yourmac.com/archives/oct17/Nvidia_HighSierra_Updates.html (updated on Oct 27 when I posted this response) that a user using MacBook Pro 2013 had this solved after updating to 9.0.214, but unfortunately not for me. (Does this model also use 650M?) (I checked Apple website https://support.apple.com/kb/SP690 and found that the 2013 model was on 750M) This post also had links to Nvidia Web Driver downloads, which are said to have “BETA support for MacBook Pro (10,1)” (till Oct 29). But it was kind of risky so I didn’t try it.

I think both the CUDA Driver, and the CUDA Toolkit are not fully updated, hence not functioning correctly.

1. The CUDA Toolkit (the compiler, etc) is certainly not yet fully updated for the macOS High Sierra era (and Xcode 9).

When I tried to “verify” the CUDA Toolkit installation, it gave the same Apple clang version 90000 not supported error as you experienced when posting this question – partly because Xcode 9 was used. I had to deliberately download Xcode Command Line Tool 8.3.2 and switch back to it using xcode-select. And it worked for the compiling phase, but that’s it.

2. The CUDA Driver (for applications to work properlysuch as MATLAB Parallel Computing Toolbox) is not yet fully updated for all graphic cards listed as compatible or supported as the release notes said.

The release note didn’t mention if it would not be working for the 650M model we are using, but said to support it. But there certainly are some issues. Each time I restart my Mac, CUDA panel in System Preferences jumps out to ask me to update CUDA – I am using the latest version already.

I was originally doing my MATLAB programs with GPU, as I mentioned earlier, and I got errors (MATLAB could not find a compatible GPU, suggesting whether the GPU was not supported, or the CUDA Driver was not supported). I was not sure if this was a compatibility error with MATLAB R2017b, so I contacted MATLAB customer support and they told me this was due to an out-of-date CUDA Driver my Mac is using.

I verified the MATLAB agent’s point by trying to run CUDA sample programs. I followed the CUDA Toolkit documentation and attempted to run the CUDA example programs (despite built with Xcode CLT 8.3.2 on macOS High Sierra, as mentioned earlier). As expected, the CUDA sample programs did not work at all as well. It also reported that the CUDA Driver was not supported.

And those are all steps I have tried.

I don’t think using an archived version as old as version 4 would tackle the problem, since macOS may no longer be able to even open the installer anymore. Besides, it would introduce some restrictions or errors when applications call it.

I found this page by Googling the error, and I think I have solved the problem.

Same specs as you:

Versions:
Mac OS 10.13 (17A405)
GPU: NVIDIA GeForce GT 650M
CUDA Driver: 9.0.222 (It still threw the error with this version)
NEW
GPU Diver: 10.25.28 378.10.10.10.15.121

nVidia has released new drivers, but they are confusingly not listed for Macbook Pros or the GT 650M. Instead, it’s listed as being compatible with the GTX 680, and when I run the installer it assumes I’m using a Mac Pro. You can’t find the driver by putting inthe GT 650M, because it will then (bizarrely) not let you select macOS 10.13 as the OS. Here is the helpful link instead:

http://www.nvidia.com/download/driverResults.aspx/126157/en-us

After installing and restarting, it successfully ran this block of code, though I am still new to Numba and CUDA in general:

import numpy as np
from numba import cuda

original = np.arange(0.0, 10.0, 0.01, dtype='f4')
shuffled = original.copy()
np.random.shuffle(shuffled)

@cuda.jit("void(f4[:])")
def bubblesort(X):
    N = len(X)
    for end in range(N, 1, -1):
        for i in range(end - 1):
            cur = X[i]
            if cur > X[i + 1]:
                tmp = X[i]
                X[i] = X[i + 1]
                X[i + 1] = tmp

sorted = shuffled.copy()
bubblesort(sorted)

print(np.array_equal(sorted, original))

I hope this helps!