While we do our best effort to support as many applications as possible, realistically, you will run into cases where things do not work. Here's a list of troubleshooting (or debugging) steps you can take, roughly in the order of increasing difficulty and power.
## Check for the presence of ROCm/HIP libraries
At the bare minimum ZLUDA needs to load the HIP runtime and the ROCm compiler support library. You can confirm that the HIP runtime is loaded by running your applications with environment variable:
```
AMD_LOG_LEVEL=3
```
you should see additional HIP debug output in the console. It will look like this:
If you are on Windows and are trying to run a GUI application that does not use the console then you can try to edit the subsystem of the application's exe from Windows GUI to Windows console using [CFF Explorer](https://ntcore.com/?page_id=388) or a similar tool.
If there is no HIP logging output, it most likely means that ZLUDA could not find the runtime libraries.
#### Linux
On Linux, ZLUDA depends on the presence of `libamdhip64.so` and `libamd_comgr.so.2` being present in the system library search paths. If ZLUDA can't find either of them, it it's usually a case of not adding `/opt/rocm/lib` to the system linker paths as instructed [here](https://rocm.docs.amd.com/projects/install-on-linux/en/latest/how-to/native-install/post-install.html). As a last resort, you can use the `LD_DEBUG=libs` environment variable to debug the library loading process.
#### Windows
On Windows, ZLUDA depends on the rpesence of `amdhip64.dll` and `amd_comgr.dll` being present in the system library search paths (usually `C:\Windows\System32`). If they are not present consider reinstalling your Radeon GPU driver.
## (Linux only) Trace CUDA calls
If an application using ZLUDA crashes or fails to run, then an easy way to check which function is failing is to run it under ltrace:
Look for the call with a non-zero return value. In our case `cuModuleGetFunction` with a return value of 500. You can check what that error code means in NVIDIA's documentation. Search [here](https://docs.nvidia.com/cuda/cuda-driver-api/group__CUDA__TYPES.html) for `enum CUresult`. Below you will find a list of error codes. In our case it is `CUDA_ERROR_NOT_FOUND`. The error code alone is rarely useful for the ZLUDA developers. If you are interested in a more precise CUDA trace, you can try the ZLUDA dumper as described in the section below.
## ZLUDA dumper
In addition to the "normal" implementation of CUDA API, ZLUDA ships with debugging implementation (sometimes called ZLUDA dumper). This implementation does the following:
- Intercept any call to CUDA APIs.
- Log imortant information: function name, arguments to console output and log file.
- On GPU code load, saves the GPU code input (PTX assembly or compiled binary code).
- Passes the CUDA API call for the execution to a real implementation (either ZLUDA or original CUDA).
First set by setting two environment variables:
*`ZLUDA_DUMP_DIR`: directory path where ZLUDA dumper will create a subdirectory with all the relevant information for you run. I usually set it to `/tmp/zluda` on Linux and `C:\temp\zluda` on Windows. The ZLUDA dumper will create the directory if it does not exist.
*`ZLUDA_CUDA_LIB`: path to the real CUDA library implementation that actually executes CUDA code. If this is not set, the ZLUDA dumper will try to load NVIDIA CUDA by default.
Once you have set the environment variables, you can start ZLUDA dumper:
### Windows
The ZLUDA loader (`zluda.exe`) can load `nvcuda.dll` from any arbitrary path with the `--nvcuda` argument. You should also use `--nvml` to select the correct `nvml.dll`.
You can build ZLUDA with debugging information by running:
```
cargo xtask
```
Aside from than the usual effects on the code it has two consequences:
* Most CUDA API functions will abort and print backtrace on failure. In release mode, ZLUDA tries to proceed gracefully and returns an error so that the application can handle the situation.
* GPU code is compiled with debug information (`-g`). Since ZLUDA does not emit CUDA debug information this only adds backtraces to the GPU code.
* The set of projects being built is slightly different.
### (Linux only) ROCgdb
To debug GPU code you can use ROCgdb. It's a fork of gdb that comes with ROCm. There are multiple articles out there explaining how to use gdb, so we won't go into the detail here. Some ZLUDA-specific notes:
* ZLUDA does not emit GPU debug information. If you are lucky you might get a partial stack trace.
* Consider using [gef](https://github.com/hugsy/gef). Among other things, it adds an `xinfo` command that can help you understand what a pointer is pointing to (device memory? function pointer? host memory?).
* https://github.com/vosen/amdgpu_debug has ROCgdb scripts that may be helpful.
### ZLUDA offline compiler (zoc)
If you are working with the PTX compiler, you should use zoc. Zoc is built by default when you build in debug mode. You use it like this:
```
<BUILD_DIRECTORY>/zoc <PATH_TO_PTX_FILE>
```
It will generate the final GPU binary and most of the intermediate files (LLVM IR, custom ELF section). It has several options: compilation mode, GPU ISA, etc.. For details, run it with `--help` argument.
Zoc does not use the compiler cache and will always do a full build.
For the best effect, run it with the ROCm compiler library debugging environment variables. see the details [here](https://github.com/ROCm/llvm-project/blob/amd-staging/amd/comgr/README.md#environment-variables).