|
| 1 | +.. meta:: |
| 2 | + :description: Maps CUDA API syntax to HIP API syntax with an example |
| 3 | + :keywords: AMD, ROCm, HIP, CUDA, syntax, HIP syntax |
| 4 | + |
| 5 | +******************************************************************************** |
| 6 | +CUDA to HIP API Function Comparison |
| 7 | +******************************************************************************** |
| 8 | + |
| 9 | +This page introduces key syntax differences between CUDA and HIP APIs with a focused code |
| 10 | +example and comparison table. For a complete list of mappings, visit :ref:`HIPIFY <HIPIFY:index>`. |
| 11 | + |
| 12 | +The following CUDA code example illustrates several CUDA API syntaxes. |
| 13 | + |
| 14 | +.. code-block:: cpp |
| 15 | +
|
| 16 | + #include <iostream> |
| 17 | + #include <vector> |
| 18 | + #include <cuda_runtime.h> |
| 19 | +
|
| 20 | + __global__ void block_reduction(const float* input, float* output, int num_elements) |
| 21 | + { |
| 22 | + extern __shared__ float s_data[]; |
| 23 | +
|
| 24 | + int tid = threadIdx.x; |
| 25 | + int global_id = blockDim.x * blockIdx.x + tid; |
| 26 | +
|
| 27 | + if (global_id < num_elements) |
| 28 | + { |
| 29 | + s_data[tid] = input[global_id]; |
| 30 | + } |
| 31 | + else |
| 32 | + { |
| 33 | + s_data[tid] = 0.0f; |
| 34 | + } |
| 35 | + __syncthreads(); |
| 36 | +
|
| 37 | + for (int stride = blockDim.x / 2; stride > 0; stride >>= 1) |
| 38 | + { |
| 39 | + if (tid < stride) |
| 40 | + { |
| 41 | + s_data[tid] += s_data[tid + stride]; |
| 42 | + } |
| 43 | + __syncthreads(); |
| 44 | + } |
| 45 | +
|
| 46 | + if (tid == 0) |
| 47 | + { |
| 48 | + output[blockIdx.x] = s_data[0]; |
| 49 | + } |
| 50 | + } |
| 51 | +
|
| 52 | + int main() |
| 53 | + { |
| 54 | + int threads = 256; |
| 55 | + const int num_elements = 50000; |
| 56 | +
|
| 57 | + std::vector<float> h_a(num_elements); |
| 58 | + std::vector<float> h_b((num_elements + threads - 1) / threads); |
| 59 | +
|
| 60 | + for (int i = 0; i < num_elements; ++i) |
| 61 | + { |
| 62 | + h_a[i] = rand() / static_cast<float>(RAND_MAX); |
| 63 | + } |
| 64 | +
|
| 65 | + float *d_a, *d_b; |
| 66 | + cudaMalloc(&d_a, h_a.size() * sizeof(float)); |
| 67 | + cudaMalloc(&d_b, h_b.size() * sizeof(float)); |
| 68 | +
|
| 69 | + cudaStream_t stream; |
| 70 | + cudaStreamCreateWithFlags(&stream, cudaStreamNonBlocking); |
| 71 | +
|
| 72 | + cudaEvent_t start_event, stop_event; |
| 73 | + cudaEventCreate(&start_event); |
| 74 | + cudaEventCreate(&stop_event); |
| 75 | +
|
| 76 | + cudaMemcpyAsync(d_a, h_a.data(), h_a.size() * sizeof(float), cudaMemcpyHostToDevice, stream); |
| 77 | +
|
| 78 | + cudaEventRecord(start_event, stream); |
| 79 | +
|
| 80 | + int blocks = (num_elements + threads - 1) / threads; |
| 81 | + block_reduction<<<blocks, threads, threads * sizeof(float), stream>>>(d_a, d_b, num_elements); |
| 82 | +
|
| 83 | + cudaMemcpyAsync(h_b.data(), d_b, h_b.size() * sizeof(float), cudaMemcpyDeviceToHost, stream); |
| 84 | +
|
| 85 | + cudaEventRecord(stop_event, stream); |
| 86 | + cudaEventSynchronize(stop_event); |
| 87 | +
|
| 88 | + cudaEventElapsedTime(&milliseconds, start_event, stop_event); |
| 89 | + std::cout << "Kernel execution time: " << milliseconds << " ms\n"; |
| 90 | +
|
| 91 | + cudaFree(d_a); |
| 92 | + cudaFree(d_b); |
| 93 | +
|
| 94 | + cudaEventDestroy(start_event); |
| 95 | + cudaEventDestroy(stop_event); |
| 96 | + cudaStreamDestroy(stream); |
| 97 | +
|
| 98 | + return 0; |
| 99 | + } |
| 100 | +
|
| 101 | +The following table maps CUDA API functions to corresponding HIP API functions, as demonstrated in the |
| 102 | +preceding code examples. |
| 103 | + |
| 104 | +.. list-table:: |
| 105 | + :header-rows: 1 |
| 106 | + :name: syntax-mapping-table |
| 107 | + |
| 108 | + * |
| 109 | + - CUDA |
| 110 | + - HIP |
| 111 | + |
| 112 | + * |
| 113 | + - ``#include <cuda_runtime.h>`` |
| 114 | + - ``#include <hip/hip_runtime.h>`` |
| 115 | + |
| 116 | + * |
| 117 | + - ``cudaError_t`` |
| 118 | + - ``hipError_t`` |
| 119 | + |
| 120 | + * |
| 121 | + - ``cudaEvent_t`` |
| 122 | + - ``hipEvent_t`` |
| 123 | + |
| 124 | + * |
| 125 | + - ``cudaStream_t`` |
| 126 | + - ``hipStream_t`` |
| 127 | + |
| 128 | + * |
| 129 | + - ``cudaMalloc`` |
| 130 | + - ``hipMalloc`` |
| 131 | + |
| 132 | + * |
| 133 | + - ``cudaStreamCreateWithFlags`` |
| 134 | + - ``hipStreamCreateWithFlags`` |
| 135 | + |
| 136 | + * |
| 137 | + - ``cudaStreamNonBlocking`` |
| 138 | + - ``hipStreamNonBlocking`` |
| 139 | + |
| 140 | + * |
| 141 | + - ``cudaEventCreate`` |
| 142 | + - ``hipEventCreate`` |
| 143 | + |
| 144 | + * |
| 145 | + - ``cudaMemcpyAsync`` |
| 146 | + - ``hipMemcpyAsync`` |
| 147 | + |
| 148 | + * |
| 149 | + - ``cudaMemcpyHostToDevice`` |
| 150 | + - ``hipMemcpyHostToDevice`` |
| 151 | + |
| 152 | + * |
| 153 | + - ``cudaEventRecord`` |
| 154 | + - ``hipEventRecord`` |
| 155 | + |
| 156 | + * |
| 157 | + - ``cudaEventSynchronize`` |
| 158 | + - ``hipEventSynchronize`` |
| 159 | + |
| 160 | + * |
| 161 | + - ``cudaEventElapsedTime`` |
| 162 | + - ``hipEventElapsedTime`` |
| 163 | + |
| 164 | + * |
| 165 | + - ``cudaFree`` |
| 166 | + - ``hipFree`` |
| 167 | + |
| 168 | + * |
| 169 | + - ``cudaEventDestroy`` |
| 170 | + - ``hipEventDestroy`` |
| 171 | + |
| 172 | + * |
| 173 | + - ``cudaStreamDestroy`` |
| 174 | + - ``hipStreamDestroy`` |
| 175 | + |
| 176 | +In summary, this comparison highlights the primary differences between CUDA and HIP APIs. |
0 commit comments