Tinkering with HIPIFY

While HIP is designed for AMD GPUs. There is tool call HIPIFY which can be used to convert CUDA files to HIP files. Then it can be complied and run on NVIDIA GPUs as well as AMD GPUs. Sweeet!

1. Install RCOM 6.0 on via package manager. 
[DOC](https://rocm.docs.amd.com/projects/install-on-linux/en/docs-6.0.0/how-to/native-install/ubuntu.html) 

2. hipconfig

HIP version  : 6.0.32830-d62f6a171

== hipconfig
HIP_PATH     : /opt/rocm-6.0.0
ROCM_PATH    : /opt/rocm-6.0.0
HIP_COMPILER : clang
HIP_PLATFORM : amd
HIP_RUNTIME  : rocclr
CPP_CONFIG   :  -D__HIP_PLATFORM_HCC__= -D__HIP_PLATFORM_AMD__= -I/opt/rocm-6.0.0/include -I/opt/rocm-6.0.0/lib/llvm/lib/clang/17.0.0
 

== hip-clang
HIP_CLANG_PATH   : /opt/rocm-6.0.0/llvm/bin
AMD clang version 17.0.0 (https://github.com/RadeonOpenCompute/llvm-project roc-6.0.0 23483 7208e8d15fbf218deb74483ea8c549c67ca4985e)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /opt/rocm-6.0.0/llvm/bin
Configuration file: /opt/rocm-6.0.0/lib/llvm/bin/clang++.cfg
AMD LLVM version 17.0.0git
  Optimized build.
  Default target: x86_64-unknown-linux-gnu
  Host CPU: alderlake

  Registered Targets:
    amdgcn - AMD GCN GPUs
    r600   - AMD GPUs HD2XXX-HD6XXX
    x86    - 32-bit X86: Pentium-Pro and above
    x86-64 - 64-bit X86: EM64T and AMD64
hip-clang-cxxflags :  -isystem "/opt/rocm-6.0.0/include" -O3
hip-clang-ldflags  : --driver-mode=g++ -O3 --hip-link --rtlib=compiler-rt -unwindlib=libgcc

=== Environment Variables
PATH=/home/rajesh/install/miniconda3/condabin:/home/rajesh/install/cmake-3.28.3-linux-x86_64/bin:/home/rajesh/install/platform-tools:/home/rajesh/install/mono/bin:/home/rajesh/gems/bin:/usr/local/cuda-12.2/bin:/home/rajesh/.cargo/bin:/home/rajesh/.local/bin:/opt/nvidia/cudaq/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/opt/rocm-6.0/bin:/home/rajesh/install/cmake-3.28.3-linux-x86_64/bin:/home/rajesh/.local/bin:/usr/local/go/bin:/home/rajesh/.go/bin
LD_LIBRARY_PATH=/usr/local/cuda-12.2/lib64:/opt/nvidia/cudaq/lib
CUDA_QUANTUM_PATH=/opt/nvidia/cudaq

== Linux Kernel
Hostname     : g15warrior
Linux g15warrior 5.15.0-91-generic #101-Ubuntu SMP Tue Nov 14 13:30:08 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux
No LSB modules are available.
Distributor ID:	Linuxmint
Description:	Linux Mint 21.3
Release:	21.3
Codename:	virginia



3. export HIP_PLATFORM=nvidia and run `hipconfig` used cuda install dirs

IP version  : 6.0.32830-d62f6a171

== hipconfig
HIP_PATH     : /opt/rocm-6.0.0
ROCM_PATH    : /opt/rocm-6.0.0
HIP_COMPILER : clang
HIP_PLATFORM : nvidia
HIP_RUNTIME  : rocclr
CPP_CONFIG   :  -D__HIP_PLATFORM_NVCC__= -D__HIP_PLATFORM_NVIDIA__= -I/opt/rocm-6.0.0/include -I/usr/local/cuda/include

== nvcc
CUDA_PATH   : /usr/local/cuda
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2023 NVIDIA Corporation
Built on Tue_Aug_15_22:02:13_PDT_2023
Cuda compilation tools, release 12.2, V12.2.140
Build cuda_12.2.r12.2/compiler.33191640_0

=== Environment Variables
PATH=/home/rajesh/install/miniconda3/condabin:/home/rajesh/install/cmake-3.28.3-linux-x86_64/bin:/home/rajesh/install/platform-tools:/home/rajesh/install/mono/bin:/home/rajesh/gems/bin:/usr/local/cuda-12.2/bin:/home/rajesh/.cargo/bin:/home/rajesh/.local/bin:/opt/nvidia/cudaq/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/opt/rocm-6.0/bin:/home/rajesh/install/cmake-3.28.3-linux-x86_64/bin:/home/rajesh/.local/bin:/usr/local/go/bin:/home/rajesh/.go/bin
LD_LIBRARY_PATH=/usr/local/cuda-12.2/lib64:/opt/nvidia/cudaq/lib
HIP_PLATFORM=nvidia
CUDA_QUANTUM_PATH=/opt/nvidia/cudaq

create a cuda file. and hipfy it

cat a.cu
#include <stdio.h>
__global__ void k(){
	printf("hello %u!\n", threadIdx.x);
}
int main(void){
	k<<<2,32>>>();
	cudaDeviceSynchronize();
	return 0;
}

hipify-perl a.cu -o a-perl.hip hipify-clang a.cu -o a-clang.hip

cat a-perl.hip 
#include "hip/hip_runtime.h"
#include <stdio.h>
__global__ void k(){
	printf("hello %u!\n", threadIdx.x);
}
int main(void){
	k<<<2,32>>>();
	hipDeviceSynchronize();
	return 0;
}

run hipcc a-perl.hip -o a-perl.out && ./a-perl.out

both hipifiy-clang (AST-based translator w/ strict syntax checking) and hipifiy-perl (regular expression based. hipifys even erroring .cu files also) have their own advantages and disadvantages. // read adv and disadv sections.

bit of compiler knowledge would help them understand better. hipify tools are still under heavy development so there is no guarantee that it will produce correct code; sometimes one may have to edit as well. Compile hip files from both of them and see if it runs fine on AMD.

update:06-Mar-2024, 14:35:07 IST

★ 3 min read · Rajesh Pandian M · hipify , nvidia , amd , cuda