From 6cdb1349239a793fddbb81409f4ab2d746049413 Mon Sep 17 00:00:00 2001 From: Istvan Kiss Date: Thu, 23 May 2024 19:32:42 +0200 Subject: [PATCH] WIP --- CONTRIBUTING.md | 4 +- docs/how-to/faq.md | 188 +++++++++++++++++++++++++++------------------ 2 files changed, 117 insertions(+), 75 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 052c7c8a8e..3696f0ccce 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -61,6 +61,7 @@ For applications and benchmarks outside the hip-tests environment, developments * Single-line if statement should still use `{/}` pair (even though C++ does not require). * For functions, the opening `{` should be placed on a new line. * For if/else blocks, the opening `{` is placed on same line as the if/else. Use a space to separate `{` from if/else. For example, + ```console if (foo) { doFoo() @@ -103,7 +104,8 @@ In particular : HIP is an open source library. Because of this, we include the following license description at the top of every source file. If you create new source files in the repository, please include this text in them as well (replacing "xx" with the digits for the current year): -``` + +```C++ // Copyright (c) 20xx Advanced Micro Devices, Inc. All rights reserved. // // Permission is hereby granted, free of charge, to any person obtaining a copy diff --git a/docs/how-to/faq.md b/docs/how-to/faq.md index 2c6a11c7d8..6c41b26fe8 100644 --- a/docs/how-to/faq.md +++ b/docs/how-to/faq.md @@ -1,93 +1,101 @@ # Frequently asked questions ## What APIs and features does HIP support? + HIP provides the following: -- Devices (hipSetDevice(), hipGetDeviceProperties(), etc.) -- Memory management (hipMalloc(), hipMemcpy(), hipFree(), etc.) -- Streams (hipStreamCreate(),hipStreamSynchronize(), hipStreamWaitEvent(), etc.) -- Events (hipEventRecord(), hipEventElapsedTime(), etc.) -- Kernel launching (hipLaunchKernel/hipLaunchKernelGGL is the preferred way of launching kernels. hipLaunchKernelGGL is a standard C/C++ macro that can serve as an alternative way to launch kernels, replacing the CUDA triple-chevron (<<< >>>) syntax). -- HIP Module API to control when adn how code is loaded. -- CUDA-style kernel coordinate functions (threadIdx, blockIdx, blockDim, gridDim) -- Cross-lane instructions including shfl, ballot, any, all -- Most device-side math built-ins -- Error reporting (hipGetLastError(), hipGetErrorString()) + +* Devices (hipSetDevice(), hipGetDeviceProperties(), etc.) +* Memory management (hipMalloc(), hipMemcpy(), hipFree(), etc.) +* Streams (hipStreamCreate(),hipStreamSynchronize(), hipStreamWaitEvent(), etc.) +* Events (hipEventRecord(), hipEventElapsedTime(), etc.) +* Kernel launching (hipLaunchKernel/hipLaunchKernelGGL is the preferred way of launching kernels. hipLaunchKernelGGL is a standard C/C++ macro that can serve as an alternative way to launch kernels, replacing the CUDA triple-chevron (<<< >>>) syntax). +* HIP Module API to control when adn how code is loaded. +* CUDA-style kernel coordinate functions (threadIdx, blockIdx, blockDim, gridDim) +* Cross-lane instructions including shfl, ballot, any, all +* Most device-side math built-ins +* Error reporting (hipGetLastError(), hipGetErrorString()) The HIP API documentation describes each API and its limitations, if any, compared with the equivalent CUDA API. ## What is not supported? ### Runtime/Driver API features + At a high-level, the following features are not supported: -- Textures (partial support available) -- Dynamic parallelism (CUDA 5.0) -- Graphics interoperability with OpenGL or Direct3D -- CUDA IPC Functions (Under Development) -- CUDA array, mipmappedArray and pitched memory -- Queue priority controls + +* Textures (partial support available) +* Dynamic parallelism (CUDA 5.0) +* Graphics interoperability with OpenGL or Direct3D +* CUDA IPC Functions (Under Development) +* CUDA array, mipmappedArray and pitched memory +* Queue priority controls See the [API Support Table](https://github.com/ROCm/HIPIFY/blob/amd-staging/docs/tables/CUDA_Runtime_API_functions_supported_by_HIP.md) for more detailed information. ### Kernel language features -- C++-style device-side dynamic memory allocations (free, new, delete) (CUDA 4.0) -- Virtual functions, indirect functions and try/catch (CUDA 4.0) -- `__prof_trigger` -- PTX assembly (CUDA 4.0). HIP-Clang supports inline GCN assembly. -- Several kernel features are under development. See the {doc}`/reference/kernel_language` for more information. + +* C++-style device-side dynamic memory allocations (free, new, delete) (CUDA 4.0) +* Virtual functions, indirect functions and try/catch (CUDA 4.0) +* `__prof_trigger` +* PTX assembly (CUDA 4.0). HIP-Clang supports inline GCN assembly. +* Several kernel features are under development. See the {doc}`/reference/kernel_language` for more information. ## Is HIP a drop-in replacement for CUDA? + No. HIP provides porting tools which do most of the work to convert CUDA code into portable C++ code that uses the HIP APIs. Most developers will port their code from CUDA to HIP and then maintain the HIP version. HIP code provides the same performance as native CUDA code, plus the benefits of running on AMD platforms. ## What specific version of CUDA does HIP support? -HIP APIs and features do not map to a specific CUDA version. HIP provides a strong subset of the functionality provided in CUDA, and the hipify tools can scan code to identify any unsupported CUDA functions - this is useful for identifying the specific features required by a given application. + +HIP APIs and features do not map to a specific CUDA version. HIP provides a strong subset of the functionality provided in CUDA, and the hipify tools can scan code to identify any unsupported CUDA functions * this is useful for identifying the specific features required by a given application. However, we can provide a rough summary of the features included in each CUDA SDK and the support level in HIP. Each bullet below lists the major new language features in each CUDA release and then indicate which are supported/not supported in HIP: -- CUDA 4.0 and earlier : - - HIP supports CUDA 4.0 except for the limitations described above. -- CUDA 5.0 : - - Dynamic Parallelism (not supported) - - cuIpc functions (under development). -- CUDA 6.0 : - - Managed memory (under development) -- CUDA 6.5 : - - __shfl intrinsic (supported) -- CUDA 7.0 : - - Per-thread default streams (supported) - - C++11 (Hip-Clang supports all of C++11, all of C++14 and some C++17 features) -- CUDA 7.5 : - - float16 (supported) -- CUDA 8.0 : - - Page Migration including cudaMemAdvise, cudaMemPrefetch, other cudaMem* APIs(not supported) -- CUDA 9.0 : - - Cooperative Launch, Surface Object Management, Version Management +* CUDA 4.0 and earlier : + * HIP supports CUDA 4.0 except for the limitations described above. +* CUDA 5.0 : + * Dynamic Parallelism (not supported) + * cuIpc functions (under development). +* CUDA 6.0 : + * Managed memory (under development) +* CUDA 6.5 : + * __shfl intrinsic (supported) +* CUDA 7.0 : + * Per-thread default streams (supported) + * C++11 (Hip-Clang supports all of C++11, all of C++14 and some C++17 features) +* CUDA 7.5 : + * float16 (supported) +* CUDA 8.0 : + * Page Migration including cudaMemAdvise, cudaMemPrefetch, other cudaMem* APIs(not supported) +* CUDA 9.0 : + * Cooperative Launch, Surface Object Management, Version Management ## What libraries does HIP support? HIP includes growing support for the four key math libraries using hipBlas, hipFFt, hipRAND and hipSPARSE, as well as MIOpen for machine intelligence applications. These offer pointer-based memory interfaces (as opposed to opaque buffers) and can be easily interfaced with other HIP applications. The hip interfaces support both ROCm and CUDA paths, with familiar library interfaces. -- [hipBlas](https://github.com/ROCmSoftwarePlatform/hipBLAS), which utilizes [rocBlas](https://github.com/ROCmSoftwarePlatform/rocBLAS). -- [hipFFt](https://github.com/ROCmSoftwarePlatform/hipfft) -- [hipsSPARSE](https://github.com/ROCmSoftwarePlatform/hipsparse) -- [hipRAND](https://github.com/ROCmSoftwarePlatform/hipRAND) -- [MIOpen](https://github.com/ROCmSoftwarePlatform/MIOpen) +* [hipBlas](https://github.com/ROCmSoftwarePlatform/hipBLAS), which utilizes [rocBlas](https://github.com/ROCmSoftwarePlatform/rocBLAS). +* [hipFFt](https://github.com/ROCmSoftwarePlatform/hipfft) +* [hipsSPARSE](https://github.com/ROCmSoftwarePlatform/hipsparse) +* [hipRAND](https://github.com/ROCmSoftwarePlatform/hipRAND) +* [MIOpen](https://github.com/ROCmSoftwarePlatform/MIOpen) Additionally, some of the cublas routines are automatically converted to hipblas equivalents by the HIPIFY tools. These APIs use cublas or hcblas depending on the platform and replace the need to use conditional compilation. ## How does HIP compare with OpenCL? Both AMD and Nvidia support OpenCL 1.2 on their devices so that developers can write portable code. HIP offers several benefits over OpenCL: -- Developers can code in C++ as well as mix host and device C++ code in their source files. HIP C++ code can use templates, lambdas, classes and so on. -- The HIP API is less verbose than OpenCL and is familiar to CUDA developers. -- Because both CUDA and HIP are C++ languages, porting from CUDA to HIP is significantly easier than porting from CUDA to OpenCL. -- HIP uses the best available development tools on each platform: on Nvidia GPUs, HIP code compiles using NVCC and can employ the nSight profiler and debugger (unlike OpenCL on Nvidia GPUs). -- HIP provides pointers and host-side pointer arithmetic. -- HIP provides device-level control over memory allocation and placement. -- HIP offers an offline compilation model. + +* Developers can code in C++ as well as mix host and device C++ code in their source files. HIP C++ code can use templates, lambdas, classes and so on. +* The HIP API is less verbose than OpenCL and is familiar to CUDA developers. +* Because both CUDA and HIP are C++ languages, porting from CUDA to HIP is significantly easier than porting from CUDA to OpenCL. +* HIP uses the best available development tools on each platform: on Nvidia GPUs, HIP code compiles using NVCC and can employ the nSight profiler and debugger (unlike OpenCL on Nvidia GPUs). +* HIP provides pointers and host-side pointer arithmetic. +* HIP provides device-level control over memory allocation and placement. +* HIP offers an offline compilation model. ## How does porting CUDA to HIP compare to porting CUDA to OpenCL? Both HIP and CUDA are dialects of C++, and thus porting between them is relatively straightforward. @@ -101,8 +109,8 @@ As a result, the OpenCL syntax is different from CUDA, and the porting tools hav The tools also struggle with more complex CUDA applications, in particular, those that use templates, classes, or other C++ features inside the kernel. ## What hardware does HIP support? -- For AMD platforms, see the [ROCm documentation](https://github.com/RadeonOpenCompute/ROCm#supported-gpus) for the list of supported platforms. -- For Nvidia platforms, HIP requires unified memory and should run on any device supporting CUDA SDK 6.0 or newer. We have tested the Nvidia Titan and Tesla K40. +* For AMD platforms, see the [ROCm documentation](https://github.com/RadeonOpenCompute/ROCm#supported-gpus) for the list of supported platforms. +* For Nvidia platforms, HIP requires unified memory and should run on any device supporting CUDA SDK 6.0 or newer. We have tested the Nvidia Titan and Tesla K40. ## Do HIPIFY tools automatically convert all source code? Typically, HIPIFY tools can automatically convert almost all run-time code. @@ -112,62 +120,77 @@ Additional porting may be required to deal with architecture feature queries or In general, developers should always expect to perform some platform-specific tuning and optimization. ## What is NVCC? + NVCC is Nvidia's compiler driver for compiling "CUDA C++" code into PTX or device code for Nvidia GPUs. It's a closed-source binary compiler that is provided by the CUDA SDK. ## What is HIP-Clang? + HIP-Clang is a Clang/LLVM based compiler to compile HIP programs which can run on AMD platform. ## Why use HIP rather than supporting CUDA directly? + While HIP is a strong subset of the CUDA, it is a subset. The HIP layer allows that subset to be clearly defined and documented. Developers who code to the HIP API can be assured their code will remain portable across Nvidia and AMD platforms. In addition, HIP defines portable mechanisms to query architectural features and supports a larger 64-bit wavesize which expands the return type for cross-lane functions like ballot and shuffle from 32-bit ints to 64-bit ints. ## Can I develop HIP code on an Nvidia CUDA platform? + Yes. HIP's CUDA path only exposes the APIs and functionality that work on both NVCC and AMDGPU back-ends. "Extra" APIs, parameters, and features which exist in CUDA but not in HIP-Clang will typically result in compile-time or run-time errors. Developers need to use the HIP API for most accelerator code and bracket any CUDA-specific code with preprocessor conditionals. Developers concerned about portability should, of course, run on both platforms, and should expect to tune for performance. -In some cases, CUDA has a richer set of modes for some APIs, and some C++ capabilities such as virtual functions - see the HIP @API documentation for more details. +In some cases, CUDA has a richer set of modes for some APIs, and some C++ capabilities such as virtual functions * see the HIP @API documentation for more details. ## Can I develop HIP code on an AMD HIP-Clang platform? -Yes. HIP's HIP-Clang path only exposes the APIs and functions that work on AMD runtime back ends. "Extra" APIs, parameters and features that appear in HIP-Clang but not CUDA will typically cause compile- or run-time errors. Developers must use the HIP API for most accelerator code and bracket any HIP-Clang specific code with preprocessor conditionals. Those concerned about portability should, of course, test their code on both platforms and should tune it for performance. Typically, HIP-Clang supports a more modern set of C++11/C++14/C++17 features, so HIP developers who want portability should be careful when using advanced C++ features on the HIP-Clang path. + +Yes. HIP's HIP-Clang path only exposes the APIs and functions that work on AMD runtime back ends. "Extra" APIs, parameters and features that appear in HIP-Clang but not CUDA will typically cause compile* or run-time errors. Developers must use the HIP API for most accelerator code and bracket any HIP-Clang specific code with preprocessor conditionals. Those concerned about portability should, of course, test their code on both platforms and should tune it for performance. Typically, HIP-Clang supports a more modern set of C++11/C++14/C++17 features, so HIP developers who want portability should be careful when using advanced C++ features on the HIP-Clang path. ## How to use HIP-Clang to build HIP programs? + The environment variable can be used to set compiler path: -- HIP_CLANG_PATH: path to hip-clang. When set, this variable let hipcc to use hip-clang for compilation/linking. + +* HIP_CLANG_PATH: path to hip-clang. When set, this variable let hipcc to use hip-clang for compilation/linking. There is an alternative environment variable to set compiler path: -- HIP_ROCCLR_HOME: path to root directory of the HIP-ROCclr runtime. When set, this variable let hipcc use hip-clang from the ROCclr distribution. + +* HIP_ROCCLR_HOME: path to root directory of the HIP-ROCclr runtime. When set, this variable let hipcc use hip-clang from the ROCclr distribution. NOTE: If HIP_ROCCLR_HOME is set, there is no need to set HIP_CLANG_PATH since hipcc will deduce them from HIP_ROCCLR_HOME. ## What is AMD clr? + AMD clr (Common Language Runtime) is a repository for the AMD platform, which contains source codes for AMD's compute languages runtimes as follows, -- hipamd - contains implementation of HIP language for AMD GPU. -- rocclr - contains virtual device interfaces that compute runtimes interact with backends, such as ROCr on Linux and PAL on Windows. -- opencl - contains implementation of OpenCLâ„¢ on the AMD platform. +* hipamd * contains implementation of HIP language for AMD GPU. +* rocclr * contains virtual device interfaces that compute runtimes interact with backends, such as ROCr on Linux and PAL on Windows. +* opencl * contains implementation of OpenCLâ„¢ on the AMD platform. ## What is hipother? + A new repository 'hipother' is added in the ROCm 6.1 release, which is branched out from HIP. hipother supports the HIP back-end implementation on some non-AMD platforms, like NVIDIA. ## Can I get HIP open source repository for Windows? + No, there is no HIP repository open publicly on Windows. ## Can a HIP binary run on both AMD and Nvidia platforms? + HIP is a source-portable language that can be compiled to run on either AMD or NVIDIA platform. HIP tools don't create a "fat binary" that can run on either platform, however. ## On HIP-Clang, can I link HIP code with host code compiled with another compiler such as gcc, icc, or clang ? + Yes. HIP generates the object code which conforms to the GCC ABI, and also links with libstdc++. This means you can compile host code with the compiler of your choice and link the generated object code with GPU code compiled with HIP. Larger projects often contain a mixture of accelerator code (initially written in CUDA with nvcc) and host code (compiled with gcc, icc, or clang). These projects can convert the accelerator code to HIP, compile that code with hipcc, and link with object code from their preferred compiler. ## Can HIP API support C style application? What is the difference between C and C++ ? + HIP is C++ runtime API that supports C style applications as well. Some C style applications (and interfaces to other languages (FORTRAN, Python)) would call certain HIP APIs but not use kernel programming. They can be compiled with a C compiler and run correctly, however, small details must be considered in the code. For example, initialization, as shown in the simple application below, uses HIP structs dim3 with the file name "test.hip.cpp" -``` + +```C++ #include "hip/hip_runtime_api.h" #include "stdio.h" @@ -181,41 +204,50 @@ int main(int argc, char** argv) { ``` When using a C++ compiler, -``` + +```bash $ gcc -x c++ $(hipconfig --cpp_config) test3.hip.cpp -o test $ ./test dim3 grid1; x=1, y=1, z=1 dim3 grid2 = {1,1,1}; x=1, y=1, z=1 ``` + In which "dim3 grid1;" will yield a dim3 grid with all dimensional members x,y,z initalized to 1, as the default constructor behaves that way. Further, if written: -``` + +```C++ dim3 grid(2); // yields {2,1,1} dim3 grid(2,3); yields {2,3,1} ``` In comparison, when using the C compiler, -``` + +```bash $ gcc -x c $(hipconfig --cpp_config) test.hip.cpp -o test $ ./test dim3 grid1; x=646881376, y=21975, z=1517277280 dim3 grid2 = {1,1,1}; x=1, y=1, z=1 ``` + In which "dim3 grid;" does not imply any initialization, no constructor is called, and dimensional values x,y,z of grid are undefined. NOTE: To get the C++ default behavior, C programmers must additionally specify the right-hand side as shown below, -``` + +```C++ dim3 grid = {1,1,1}; // initialized as in C++ ``` ## Can I install both CUDA SDK and HIP-Clang on the same machine? + Yes. You can use HIP_PLATFORM to choose which path hipcc targets. This configuration can be useful when using HIP to develop an application which is portable to both AMD and NVIDIA. -## HIP detected my platform (HIP-Clang vs nvcc) incorrectly - what should I do? +## HIP detected my platform (HIP-Clang vs nvcc) incorrectly * what should I do? + HIP will set the platform to AMD and use HIP-Clang as compiler if it sees that the AMD graphics driver is installed and has detected an AMD GPU. -Sometimes this isn't what you want - you can force HIP to recognize the platform by setting the following, -``` +Sometimes this isn't what you want * you can force HIP to recognize the platform by setting the following, + +```bash export HIP_PLATFORM=amd ``` HIP then set and use correct AMD compiler and runtime, @@ -223,7 +255,8 @@ HIP_COMPILER=clang HIP_RUNTIME=rocclr To choose NVIDIA platform, you can set, -``` + +```bash export HIP_PLATFORM=nvidia ``` In this case, HIP will set and use the following, @@ -233,6 +266,7 @@ HIP_RUNTIME=nvcc One symptom of this problem is the message "error: 'unknown error'(11) at square.hipref.cpp:56". This can occur if you have a CUDA installation on an AMD platform, and HIP incorrectly detects the platform as nvcc. HIP may be able to compile the application using the nvcc tool-chain but will generate this error at runtime since the platform does not have a CUDA device. ## On CUDA, can I mix CUDA code with HIP code? + Yes. Most HIP data structures (hipStream_t, hipEvent_t) are typedefs to CUDA equivalents and can be intermixed. Both CUDA and HIP use integer device ids. One notable exception is that hipError_t is a new type, and cannot be used where a cudaError_t is expected. In these cases, refactor the code to remove the expectation. Alternatively, hip_runtime_api.h defines functions which convert between the error code spaces: @@ -243,19 +277,24 @@ hipCUResultTohipError If platform portability is important, use #ifdef __HIP_PLATFORM_NVIDIA__ to guard the CUDA-specific code. ## How do I trace HIP application flow? + See {doc}`/how-to/logging` for more information. ## What are the maximum limits of kernel launch parameters? + Product of block.x, block.y, and block.z should be less than 1024. Please note, HIP does not support kernel launch with total work items defined in dimension with size gridDim x blockDim >= 2^32, so gridDim.x * blockDim.x, gridDim.y * blockDim.y and gridDim.z * blockDim.z are always less than 2^32. ## Are __shfl_*_sync functions supported on HIP platform? + __shfl_*_sync is not supported on HIP but for nvcc path CUDA 9.0 and above all shuffle calls get redirected to it's sync version. ## How to create a guard for code that is specific to the host or the GPU? + The compiler defines the `__HIP_DEVICE_COMPILE__` macro only when compiling the code for the GPU. It could be used to guard code that is specific to the host or the GPU. ## Why _OpenMP is undefined when compiling with -fopenmp? + When compiling an OpenMP source file with `hipcc -fopenmp`, the compiler may generate error if there is a reference to the `_OPENMP` macro. This is due to a limitation in hipcc that treats any source file type (for example `.cpp`) as an HIP translation unit leading to some conflicts with the OpenMP language switch. If the OpenMP source file doesn't contain any HIP language constructs you could work around this issue by adding the `-x c++` switch to force the compiler to treat the file as regular C++. Another approach would be to guard the OpenMP code with `#ifdef _OPENMP` so that the code block is disabled when compiling for the GPU. The `__HIP_DEVICE_COMPILE__` macro defined by the HIP compiler when compiling GPU code could also be used for guarding code paths specific to the host or the GPU. ## Does the HIP-Clang compiler support extern shared declarations? @@ -273,8 +312,8 @@ If you have compiled the application yourself, make sure you have given the corr If you have a precompiled application/library (like rocblas, tensorflow etc) which gives you such error, there are one of two possibilities. - - The application/library does not ship code object bundles for *all* of your device(s): in this case you need to recompile the application/library yourself with correct `--offload-arch`. - - The application/library does not ship code object bundles for *some* of your device(s), for example you have a system with an APU + GPU and the library does not ship code objects for your APU. For this you can set the environment variable `HIP_VISIBLE_DEVICES` or `CUDA_VISIBLE_DEVICES` on NVdia platform, to only enable GPUs for which code object is available. This will limit the GPUs visible to your application and allow it to run. + * The application/library does not ship code object bundles for *all* of your device(s): in this case you need to recompile the application/library yourself with correct `--offload-arch`. + * The application/library does not ship code object bundles for *some* of your device(s), for example you have a system with an APU + GPU and the library does not ship code objects for your APU. For this you can set the environment variable `HIP_VISIBLE_DEVICES` or `CUDA_VISIBLE_DEVICES` on NVdia platform, to only enable GPUs for which code object is available. This will limit the GPUs visible to your application and allow it to run. Note: In previous releases, the error code is hipErrorNoBinaryForGpu with message "Unable to find code object for all current devices". The error code handling behavior is changed. HIP runtime shows the error code hipErrorSharedObjectInitFailed with message "Error: shared object initialization failed" on unsupported GPU. @@ -298,8 +337,9 @@ typedef float2 hipFloatComplex; typedef double2 hipDoubleComplex; Any application uses complex multiplication and division operations, need to replace '*' and '/' operators with the following, -- hipCmulf() and hipCdivf() for hipFloatComplex -- hipCmul() and hipCdiv() for hipDoubleComplex + +* hipCmulf() and hipCdivf() for hipFloatComplex +* hipCmul() and hipCdiv() for hipDoubleComplex Note: These complex operations are equivalent to corresponding types/functions on the NVIDIA platform.