DEVICE_NOT_FOUND while calling pyopencl.Context - python

I am struggling with the following Python code:
import pyopencl as cl
ctx = cl.Context(dev_type=cl.device_type.GPU)
It gives the following exception:
RuntimeError: clcreatecontextfromtype failed: DEVICE_NOT_FOUND
My OS is Linux Mint Debian Edition 2, running on a laptop with i7-5600U. It also has a graphic card, but I do not use it. I am using Python 3.4.2.
I have installed the Debian package amd-opencl-icd (I first tried beignet, but then the command clinfo failed).
I have installed pyopencl using pip and opencl using this tutorial. Note that I did not do the fourth step (creating the symbolic link to intel64.icd), since I did not have this file. The test at the end of the tutorial succeed.
Do you have any hint about what is happening? I am surprised that the C++ test of opencl (in the tutorial) and the installation of pyopencl both succeed, but this simple command of pyopencl fails.
EDIT
After installing the Intel driver, I now have a different issue.
The command clinfo gives the following:
terminate called after throwing an instance of 'unsigned long'
And the above Python code gives:
LogicError: clcreatecontextfromtype failed: INVALID_PLATFORM

You've installed the intel opencl SDK, which gives you the compiler and maybe the CPU runtime. You're trying to create a context consisting of GPU devices, which means that you need the runtime for intel HD graphics. Grab the 64-bit driver from the link below.
https://software.intel.com/en-us/articles/opencl-drivers#latest_linux_driver
The CPU runtime is also available from that link. You need to follow the same procedure as before for the opencl HD graphics driver (converting .rpm to .deb). The CPU driver has a script you can execute.
The INVALID_PLATFORM error you got after installing the runtime appears to be because it expects the platform to be passed as a property, when creating from device type. It expects the properties as a list of key-tuple pairs. This is shown in the snippet below for the first available platform. The keyword is one of the values in context_properties, and the value is the platform object itself.
import pyopencl as cl
platforms = cl.get_platforms()
ctx = cl.Context(dev_type=cl.device_type.GPU, properties=[(cl.context_properties.PLATFORM, platforms[0])])
print(ctx.devices)
On my platform this prints
[<pyopencl.Device 'Intel(R) HD Graphics 4600' on 'Intel(R) OpenCL' at 0x1c04b217140>]
as my first platform is intel.

Related

Opencv Cuda accelerated : Python can't see GPU device

I installed OpenCV for GPU use in python, following tutorials on youtube.
I encounter a major difficulty when I try to see if python recognizes the GPU.
After the installation, I executed this code, in order to verify if my GPU is detected or not :
import cv2
from cv2 import cuda
cuda.printCudaDeviceInfo(0)
The first output was :
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
cv2.error: OpenCV(4.5.5) D:\a\opencv-python\opencv-python\opencv\modules\core\include\opencv2/core/private.cuda.hpp:106
: error: (-216:No CUDA support) The library is compiled without CUDA support in function 'throw_no_cuda'
So I thought my install was wrong, then I did the install again.
After many attempts, I tried to do the same code verification as before but in the site-packages folder of my Miniconda install (In the same location of cv2 for GPU).
And surprisingly, when I use the method cuda.printCudaDeviceInfo(0) the output is :
*** CUDA Device Query (Runtime API) version (CUDART static linking) ***
Device count: 1
Device 0: "NVIDIA T400"
...
Compute Mode:
Default (multiple host threads can use ::cudaSetDevice() with device simultaneously)
deviceQuery, CUDA Driver = CUDART, CUDA Driver Version = 11.70, CUDA Runtime Version = 11.70, NumDevs = 1
So the GPU has been detected when I used python in this folder.
But my want is to use python in other folders.
I thought it was a PATH error, but I added to my path, the cv2 location in my variables system environment, and I got the same result.
Does anyone have an idea about how to fix this ?
Thank you.

Python OpenCV with Cuda not working after successful build

I am on Windows 10, using Python 3.9.6 and my cv2 version is 4.4.0. I built OpenCV with Cuda successfully and after calling cv2.cuda.getCudaEnabledDeviceCount(), it returns 1 as expected. The following lines also work fine.
net = cv2.dnn.readNetFromCaffe(proto_file, weights_file)
net.setPreferableBackend(cv2.dnn.DNN_BACKEND_CUDA)
net.setPreferableTarget(cv2.dnn.DNN_TARGET_CUDA)
# multiple lines
# processing frame
# and setting input blob
net.setInput(in_blob)
However, executing the following line throws an exception.
output = net.forward()
The exception:
cv2.error: OpenCV(4.4.0)
G:\opencv-4.4.0\opencv-4.4.0\modules\dnn\src\dnn.cpp:2353: error:
(-216:No CUDA support) OpenCV was not built to work with the selected
device. Please check CUDA_ARCH_PTX or CUDA_ARCH_BIN in your build
configuration. in function
'cv::dnn::dnn4_v20200609::Net::Impl::initCUDABackend'
The message says that my Cuda was not built to work with the selected device (which I'm guessing is my GPU).
It seems to have encountered a conflict with CUDA_ARCH_BIN and/or CUDA_ARCH_PTX. My GPU model is NVIDIA Geforce MX130 whose CUDA_ARCH_BIN value is what I found to be 6.1 and I set it according on CMake.
How can I resolve these issues? Let me know if I need to provide any more information.
"Sources say" the MX130 has a Maxwell core, not a Pascal core. Maxwell is the predecessor of Pascal.
Hence, you only have CUDA compute capability 5.0.
You should check that with an appropriate tool such as GPU-Z that does its best to query the hardware instead of going by specs.
Sources:
https://en.wikipedia.org/wiki/GeForce_10_series#GeForce_10_(10xx)_series_for_notebooks (notice how the Fab (nm) is different and the code name is GM108, not GPxxx)
https://www.techpowerup.com/gpu-specs/geforce-mx130.c3043

PyOpenCL on Linux Mint: PLATFORM_NOT_FOUND_KHR

I've been trying to get PyOpenCL and PyCUDA running on a Linux Mint machine. I have things installed but the demo scripts fail with the error:
pyopencl.cffi_cl.LogicError: clgetplatformids failed: PLATFORM_NOT_FOUND_KHR
Configuration
$ uname -a && cat /etc/lsb-release && lspci | grep NV
Linux 3.13.0-24-generic #47-Ubuntu SMP Fri May 2 23:30:00 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux
DISTRIB_DESCRIPTION="Linux Mint 17.3 Rosa"
01:00.0 VGA compatible controller: NVIDIA Corporation GK208 [GeForce GT 730] (rev a1)
Relevant installed packages:
libcuda1-352-updates
libcudart5.5:amd64
nvidia-352-updates
nvidia-352-updates-dev
nvidia-cuda-dev
nvidia-cuda-toolkit
nvidia-opencl-icd-352-updates
nvidia-profiler
nvidia-settings
ocl-icd-libopencl1:amd64
ocl-icd-opencl-dev:amd64
opencl-headers
python-pycuda
python-pyopencl
python3-pycuda
python3-pyopencl
Research
This post describes a scenario in which the package-manager installed opencl/cuda implementation don't set up some simlinks correctly. That issue doesn't seem to be present on my system.
There was a version number mismatch between the graphics drivers (were nvidia-340) and the nvidia-opencl package (352). I update the graphics drivers to nvidia-352-updates-dev but the issue remains.
There is a bug in Arch linux that seems to revolve around the necessary device files not being created. However, I've verified that the /dev/nvidia0 and /dev/nvidiactl exist and have permissions 666, so they should be accessible.
Another Stackoverflow post suggests running the demos as root. I have tried this and the behavior does not change.
Older installation instructions for cuda/opencl say to download drivers directly from the NVidia website. I'm not sure this still applies, so I'm holding off on that for now since there seem to be plenty of relevant packages in the respositories.
The same error, but for an ATI card on a different linux system, was resolved by putting proper files in /usr/lib/OpenCL/vendors. That path isn't used on my system, However, I do have /etc/OpenCL/vendors/nvidia.icd which contains the line libnvidia-opencl.so.1, suggesting my issue is dissimilar.
This error has been observed on OSX, but for unrelated reasons. Similar error messages for PyCUDA also appear to be unrelated.
This error can occur under remote access since the device files are not initialized if X is not loaded. However, I'm testing this in a desktop environment. Furthermore, I've run the manual commands suggested in that thread just to be sure, and they are redundant since the relevant /dev entries already exist.
There is a note here about simply running a command a few times to get around some sort temporary glitch. That doesn't seem to help.
This post describes how the similar cuInit failed: no device CUDA error was caused by not having the user in the video group. To check, I ran usermod -a -G video $USER, but it did not resolve my issue.
In the past, routine updates have broken CUDA support. I have not taken the time to explore every permutation of package version numbers, and it's possible that downgrading some packages may change the situation. However, without further intuition about the source of the issue, I'm not going to invest time in doing that since I don't know whether it will work.
The most common google search result for this error, appearing four times on the first pages, is a short and unresolved email thread on the PyOpenCL list. Checking the permissions bits for /dev/nvidia0 and /dev/nvidiactl is suggested. On my machine user/group/other all have read and write access to these devices, so I don't think that's the source of the trouble.
I've also tried building and installing PyOpenCL form the latest source, rather than using the version in the repositories. This is failing at an earlier phase which suggests to me it is not building correctly.
Summary
The issue would appear to be that PyCUDA/PyOpenCL cannot locate the graphics card. There are several known issues that can cause this, but none of them seem to apply here. I'm missing something, and I'm not sure what else to do.

PyOpenCL returns errors the first run, then only 'invalid program' errors; examples also not working

I am trying to run an OpenCL kernel using the pyOpenCL bindings, to run on the GPU. I was trying to load the kernel to my program. I ran my program once and got an error. I ran it again without changing the code and got a different, 'invalid program' error.
This keeps happening to my own programs using pyOpenCL and also on example programs. I am able to use OpenCL through the C++ bindings, on both the CPU and GPU, with no problems. So I think this is a problem specific to the pyOpenCL bindings.
My OS is Linux Mint 17.1 64 bit. My GPU is an Intel HD graphics 4000 3rd Gen processor. I got the OpenCL support for it by installing beignet (see here). I am using python 2.7 and OpenCL 1.2.
The first time I ran my program, I got this error:
File "/usr/lib/python2.7/dist-packages/pyopencl/__init__.py", line 463, in kernel_call
self.set_args(*args)
File "/usr/lib/python2.7/dist-packages/pyopencl/__init__.py", line 488, in kernel_set_args
% (len(args), self.num_args))
AssertionError: length of argument list (4) and CL-generated number of arguments (9) do not agree
But then I ran it again, and started getting this different error:
File "/usr/lib/python2.7/dist-packages/pyopencl/__init__.py", line 206, in _build_and_catch_errors
raise err
pyopencl.RuntimeError: clBuildProgram failed: invalid program -
Build on <pyopencl.Device 'Intel HD Graphics Family' on 'Experiment Intel Gen OCL Driver' at 0x7fb77bf21720>:
(options: -I /usr/lib/python2.7/dist-packages/pyopencl/cl)
(source saved as /tmp/tmplLRgy9.cl)
I downloaded one of the pyOpenCL examples for here but I got the same problem: the first time I ran it, I got:
File "/usr/lib/python2.7/dist-packages/pyopencl/__init__.py", line 436, in __getattr__
return self.event.get_profiling_info(inf_attr)
pyopencl.RuntimeError: clGetEventProfilingInfo failed: profiling info not available
...and then I run it again, and I got the same 'invalid program' error from before.
I don't understand why the error changes the second time I run the programs, since I didn't change anything in the code in between runs.
Hope this is clear enough, thank you.
I solved it (sort of) by uninstalling beignet and installing the latest version, beignet 1.1.0, found here.
However, the only way to get the programs to detect my GPU device and work with no problems is by running them as root. Will probably post a different question for that specific issue.

How to load portable .NET library within Iron Python script?

I have serious troubles loading a portable .NET library (to be used in standard .NET and Silverlight environment) from a Python script.
.NET DLL file version is 4.0.3.319.233 (System.Core.DLL), IronPython is 2.7.1, running in 32bit/x86 mode. Visual Studio 2010 with C# under .NET 4. Microsoft .NET update KB2468871 for Portable library usage is also installed (Version 2).
If I try to load the library from the Python script:
clr.AddReferenceToFileAndPath(UsedPath+"\\MyNamespace\\MyPortableLibrary.dll")
it can't be accessed, and when the script reaches a type, it says:
"attribute [type in portable assembly] of 'namespace#' is read-only"
indicating the assembly has not been loaded at all (or as Silverlight, and can't be used by the Python script).
Changing the code to: (Assembly class from System.Reflection)
PortableAssembly = Assembly.LoadFrom(UsedPath+"\\MyNamespace\\MyPortableLibrary.dll") # load through .NET Reflection, Python won't load Portable assembly properly!
clr.AddReference(PortableAssembly)
results in an error:
exceptions.IOError occurred
Message: [Errno 2] Could not load file or assembly 'System.Core, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e, Retargetable=Yes' or one of its dependencies. The system cannot find the file specified.
The last code seems to work, when the Python script is called automatically from within another .NET program, instantiating it's own Python engine, but gives the above error when the script is executed from a Python project in Visual Studio. Python settings in VisualStudio, Tools\Options\Python Tools\Interpreter Options are for x86/32bit mode. All environment parameters show that .NET 4 is used.
I've got multiple ways now to fix it from a C#/.NET generated Python engine, but how can I load the portable assembly in a basic IronPython runtime environment, so that it works in the correct .NET 4 environment, not trying to load any .NET 2 stuff?
Update:
I restarted and rebuilt my portable library after the MS KB2468871 update, and also uninstalled IronPython and Python tools for VS, replacing them by versions 2.7.3 and 1.5 (VS2010). The error with 'System.Core, Version=2.0.5.0' still occurs.
The FileNotFoundException is an indication that something is loading the assemblies using Assembly.LoadFile instead of Assembly.LoadFrom, but not handling assembly policy correctly. I'm not sure how Python code in Visual Studio works, but if you are able to run any bootstrapper code before the portable assembly has been loaded, try the code that I showed here:
PCL Retargetable Assembly not redirected inside MS CRM Plugin.
use sys.path to add pathes to where your .net dlls are:
import sys
sys.path.append("c:\MyDotNetDir");
import clr
clr.AddReference("MyDotNetAssembly.dll")
# do not forget to import the namespace
import Erik.MyDotNetAssemblyNamespace
inst = Erik.MyDotNetAssemblyNamespace.MyDotNetObject()

Categories