Compile and run a HIP kernel
This guide compiles and runs a minimal HIP program that adds 1 to every element of a 128-element integer array on an AMD GPU running ROCm.
HIP is the CUDA-shaped C++ source language, while ROCm is the underlying AMD driver, runtime, and library stack.
Compare this file directly with Compile and run a CUDA kernel and the conceptual overview in GPU compute to see how closely the two models align.
Verify ROCm and hipcc
Check that ROCm sees an AMD GPU agent and that hipcc is installed:
rocminfo
hipcc --version
rocminfo must list a GPU agent, not only the host CPU.
On Linux, installing ROCm provides both rocminfo and hipcc as the normal path for AMD compute.
If you are on Windows and rocminfo is unavailable or does not list your card, do not spend time wrestling with a partial ROCm setup — switch to Dispatch a Vulkan compute shader or the browser WebGPU path in Draw a triangle and run a compute shader, both of which drive AMD GPUs without ROCm.
Write the HIP program
Save the following listing as add_one.hip.
Aside from swapping the runtime header and changing the cuda* runtime prefix to hip*, the kernel body and launch syntax are identical to CUDA:
#include <cstdio>
#include <hip/hip_runtime.h>
__global__ void add_one(unsigned* data, int n) {
int i = blockIdx.x * blockDim.x + threadIdx.x;
if (i < n) data[i] += 1u;
}
int main() {
const int n = 128;
unsigned host[128];
for (int i = 0; i < n; ++i) host[i] = static_cast<unsigned>(i);
unsigned* device = nullptr;
hipMalloc(&device, sizeof(host));
hipMemcpy(device, host, sizeof(host), hipMemcpyHostToDevice);
add_one<<<(n + 63) / 64, 64>>>(device, n);
hipError_t err = hipDeviceSynchronize();
hipMemcpy(host, device, sizeof(host), hipMemcpyDeviceToHost);
hipFree(device);
if (err != hipSuccess) {
std::fprintf(stderr, "%s\n", hipGetErrorString(err));
return 1;
}
std::printf("%u %u %u\n", host[0], host[1], host[127]);
return host[0] == 1 && host[1] == 2 && host[127] == 128 ? 0 : 2;
}
Compile and run
hipcc add_one.hip -o add_one
./add_one
A working run prints 1 2 128 and exits with status 0.
hipcc can also compile this same HIP source against an NVIDIA CUDA installation to emit a CUDA-backed executable, which is why projects use HIP (and tools like hipify) for source-level portability across AMD and NVIDIA.
That portability stays at the source level: you compile fresh binaries for each vendor stack rather than executing a prebuilt NVIDIA .cubin on an AMD card.
This minimal example links none of ROCm’s math libraries; once this binary runs cleanly, you know the ROCm runtime can reach your card and you are ready to link libraries such as rocBLAS or write larger kernels. For cross-vendor native compute without ROCm, see Dispatch a Vulkan compute shader.