|
| 1 | +/** |
| 2 | +* Copyright (c) NVIDIA CORPORATION & AFFILIATES, 2025. ALL RIGHTS RESERVED. |
| 3 | +* |
| 4 | +* See file LICENSE for terms. |
| 5 | +*/ |
| 6 | + |
| 7 | +#ifndef UCT_CUDA_IPC_CUH |
| 8 | +#define UCT_CUDA_IPC_CUH |
| 9 | + |
| 10 | +#include "ucs/type/status.h" |
| 11 | +#include "uct/api/uct_def.h" |
| 12 | +#ifdef HAVE_CONFIG_H |
| 13 | +#include "config.h" |
| 14 | +#endif |
| 15 | + |
| 16 | +#include <uct/api/device/uct_device_types.h> |
| 17 | +#include <uct/cuda/cuda_ipc/cuda_ipc_device.h> |
| 18 | + |
| 19 | +#define UCT_CUDA_IPC_IS_ALIGNED_POW2(_n, _p) (!((_n) & ((_p) - 1))) |
| 20 | +#define UCT_CUDA_IPC_WARP_SIZE 32 |
| 21 | +#define UCT_CUDA_IPC_COPY_LOOP_UNROLL 8 |
| 22 | + |
| 23 | +UCS_F_DEVICE int4 uct_cuda_ipc_ld_global_cg(const int4* p) { |
| 24 | + int4 v; |
| 25 | + asm volatile ("ld.global.cg.v4.s32 {%0,%1,%2,%3}, [%4];" |
| 26 | + : "=r"(v.x), "=r"(v.y), "=r"(v.z), "=r"(v.w) |
| 27 | + : "l"(p)); |
| 28 | + return v; |
| 29 | +} |
| 30 | + |
| 31 | +UCS_F_DEVICE void uct_cuda_ipc_st_global_cg(int4* p, const int4& v) { |
| 32 | + asm volatile ("st.global.cg.v4.s32 [%0], {%1,%2,%3,%4};" |
| 33 | + : |
| 34 | + : "l"(p), "r"(v.x), "r"(v.y), "r"(v.z), "r"(v.w)); |
| 35 | +} |
| 36 | + |
| 37 | +UCS_F_DEVICE int2 uct_cuda_ipc_ld_global_cg(const int2* p) { |
| 38 | + int2 v; |
| 39 | + asm volatile ("ld.global.cg.v2.s32 {%0,%1}, [%2];" |
| 40 | + : "=r"(v.x), "=r"(v.y) |
| 41 | + : "l"(p)); |
| 42 | + return v; |
| 43 | +} |
| 44 | + |
| 45 | +UCS_F_DEVICE void uct_cuda_ipc_st_global_cg(int2* p, const int2& v) { |
| 46 | + asm volatile ("st.global.cg.v2.s32 [%0], {%1,%2};" |
| 47 | + : |
| 48 | + : "l"(p), "r"(v.x), "r"(v.y)); |
| 49 | +} |
| 50 | + |
| 51 | +UCS_F_DEVICE void uct_cuda_ipc_copy_single_nv(void *dst, const void *src, size_t size) |
| 52 | +{ |
| 53 | + /* TODO: add vectorized version*/ |
| 54 | + auto s1 = reinterpret_cast<const char*>(src); |
| 55 | + auto d1 = reinterpret_cast<char *>(dst); |
| 56 | + |
| 57 | + for (size_t i = threadIdx.x; i < size; i += blockDim.x) { |
| 58 | + d1[i] = s1[i]; |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +template<int UNROLL> |
| 63 | +UCS_F_DEVICE void uct_cuda_ipc_copy_single(void *dst, const void *src, size_t size) |
| 64 | +{ |
| 65 | + using vec4 = int4; |
| 66 | + using vec2 = int2; |
| 67 | + auto s1 = reinterpret_cast<const char*>(src); |
| 68 | + auto d1 = reinterpret_cast<char *>(dst); |
| 69 | + const vec4 *s4; |
| 70 | + vec4 *d4; |
| 71 | + int warp, num_warps, idx; |
| 72 | + size_t num_lines; |
| 73 | + |
| 74 | + if (UCT_CUDA_IPC_IS_ALIGNED_POW2((intptr_t)s1, sizeof(vec4)) && |
| 75 | + UCT_CUDA_IPC_IS_ALIGNED_POW2((intptr_t)d1, sizeof(vec4))) { |
| 76 | + vec4 tmp[UNROLL]; |
| 77 | + warp = threadIdx.x / UCT_CUDA_IPC_WARP_SIZE; |
| 78 | + num_warps = blockDim.x / UCT_CUDA_IPC_WARP_SIZE; |
| 79 | + idx = threadIdx.x % UCT_CUDA_IPC_WARP_SIZE; |
| 80 | + s4 = reinterpret_cast<const vec4*>(s1); |
| 81 | + d4 = reinterpret_cast<vec4*>(d1); |
| 82 | + num_lines = (size / (UCT_CUDA_IPC_WARP_SIZE * UNROLL * sizeof(vec4))) * |
| 83 | + (UCT_CUDA_IPC_WARP_SIZE * UNROLL); |
| 84 | + |
| 85 | + for (size_t line = warp * UCT_CUDA_IPC_WARP_SIZE * UNROLL + idx; line < num_lines; |
| 86 | + line += num_warps * UCT_CUDA_IPC_WARP_SIZE * UNROLL) { |
| 87 | +#pragma unroll |
| 88 | + for (int i = 0; i < UNROLL; i++) { |
| 89 | + tmp[i] = uct_cuda_ipc_ld_global_cg(s4 + (line + UCT_CUDA_IPC_WARP_SIZE * i)); |
| 90 | + } |
| 91 | + |
| 92 | +#pragma unroll |
| 93 | + for (int i = 0; i < UNROLL; i++) { |
| 94 | + uct_cuda_ipc_st_global_cg(d4 + (line + UCT_CUDA_IPC_WARP_SIZE * i), tmp[i]); |
| 95 | + } |
| 96 | + } |
| 97 | + size = size - num_lines * sizeof(vec4); |
| 98 | + if (size == 0) { |
| 99 | + return; |
| 100 | + } |
| 101 | + |
| 102 | + s4 = s4 + num_lines; |
| 103 | + d4 = d4 + num_lines; |
| 104 | + num_lines = size / sizeof(vec4); |
| 105 | + for (size_t line = threadIdx.x; line < num_lines; line += blockDim.x) { |
| 106 | + vec4 v = uct_cuda_ipc_ld_global_cg(s4 + line); |
| 107 | + uct_cuda_ipc_st_global_cg(d4 + line, v); |
| 108 | + } |
| 109 | + |
| 110 | + size = size - num_lines * sizeof(vec4); |
| 111 | + if (size == 0) { |
| 112 | + return; |
| 113 | + } |
| 114 | + |
| 115 | + s1 = reinterpret_cast<const char*>(s4 + num_lines); |
| 116 | + d1 = reinterpret_cast<char*>(d4 + num_lines); |
| 117 | + } |
| 118 | + |
| 119 | + /* If not 16B-aligned, try 8B-aligned fast path using vec2 */ |
| 120 | + if (UCT_CUDA_IPC_IS_ALIGNED_POW2((intptr_t)s1, sizeof(vec2)) && |
| 121 | + UCT_CUDA_IPC_IS_ALIGNED_POW2((intptr_t)d1, sizeof(vec2))) { |
| 122 | + const vec2 *s2; |
| 123 | + vec2 *d2; |
| 124 | + vec2 tmp2[UNROLL]; |
| 125 | + |
| 126 | + warp = threadIdx.x / UCT_CUDA_IPC_WARP_SIZE; |
| 127 | + num_warps = blockDim.x / UCT_CUDA_IPC_WARP_SIZE; |
| 128 | + idx = threadIdx.x % UCT_CUDA_IPC_WARP_SIZE; |
| 129 | + s2 = reinterpret_cast<const vec2*>(s1); |
| 130 | + d2 = reinterpret_cast<vec2*>(d1); |
| 131 | + num_lines = (size / (UCT_CUDA_IPC_WARP_SIZE * UNROLL * sizeof(vec2))) * |
| 132 | + (UCT_CUDA_IPC_WARP_SIZE * UNROLL); |
| 133 | + |
| 134 | + for (size_t line = warp * UCT_CUDA_IPC_WARP_SIZE * UNROLL + idx; line < num_lines; |
| 135 | + line += num_warps * UCT_CUDA_IPC_WARP_SIZE * UNROLL) { |
| 136 | +#pragma unroll |
| 137 | + for (int i = 0; i < UNROLL; i++) { |
| 138 | + tmp2[i] = uct_cuda_ipc_ld_global_cg(s2 + (line + UCT_CUDA_IPC_WARP_SIZE * i)); |
| 139 | + } |
| 140 | + |
| 141 | +#pragma unroll |
| 142 | + for (int i = 0; i < UNROLL; i++) { |
| 143 | + uct_cuda_ipc_st_global_cg(d2 + (line + UCT_CUDA_IPC_WARP_SIZE * i), tmp2[i]); |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + size = size - num_lines * sizeof(vec2); |
| 148 | + if (size == 0) { |
| 149 | + return; |
| 150 | + } |
| 151 | + |
| 152 | + s2 = s2 + num_lines; |
| 153 | + d2 = d2 + num_lines; |
| 154 | + num_lines = size / sizeof(vec2); |
| 155 | + for (size_t line = threadIdx.x; line < num_lines; line += blockDim.x) { |
| 156 | + vec2 v2 = uct_cuda_ipc_ld_global_cg(s2 + line); |
| 157 | + uct_cuda_ipc_st_global_cg(d2 + line, v2); |
| 158 | + } |
| 159 | + |
| 160 | + size = size - num_lines * sizeof(vec2); |
| 161 | + if (size == 0) { |
| 162 | + return; |
| 163 | + } |
| 164 | + |
| 165 | + s1 = reinterpret_cast<const char*>(s2 + num_lines); |
| 166 | + d1 = reinterpret_cast<char*>(d2 + num_lines); |
| 167 | + } |
| 168 | + |
| 169 | + for (size_t line = threadIdx.x; line < size; line += blockDim.x) { |
| 170 | + d1[line] = s1[line]; |
| 171 | + } |
| 172 | +} |
| 173 | + |
| 174 | +template<uct_device_level_t level = UCT_DEVICE_LEVEL_BLOCK> |
| 175 | +UCS_F_DEVICE ucs_status_t |
| 176 | +uct_cuda_ipc_ep_put_single(uct_device_ep_h device_ep, |
| 177 | + const uct_device_mem_element_t *mem_elem, |
| 178 | + const void *address, uint64_t remote_address, |
| 179 | + size_t length, uint64_t flags, |
| 180 | + uct_device_completion_t *comp) |
| 181 | +{ |
| 182 | + auto cuda_ipc_mem_element = |
| 183 | + reinterpret_cast<const uct_cuda_ipc_device_mem_element_t *>(mem_elem); |
| 184 | + void *mapped_rem_addr; |
| 185 | + |
| 186 | + mapped_rem_addr = (void *)((uintptr_t)(remote_address) + cuda_ipc_mem_element->mapped_offset); |
| 187 | + |
| 188 | + switch (level) { |
| 189 | + case UCT_DEVICE_LEVEL_THREAD: |
| 190 | + /* TODO: add vectorized version*/ |
| 191 | + memcpy(mapped_rem_addr, address, length); |
| 192 | + break; |
| 193 | + case UCT_DEVICE_LEVEL_WARP: |
| 194 | + /* TODO: check if we can use uct_cuda_ipc_copy_single, need to see perf impact */ |
| 195 | + uct_cuda_ipc_copy_single_nv(mapped_rem_addr, address, length); |
| 196 | + break; |
| 197 | + case UCT_DEVICE_LEVEL_BLOCK: |
| 198 | + uct_cuda_ipc_copy_single<UCT_CUDA_IPC_COPY_LOOP_UNROLL>(mapped_rem_addr, address, length); |
| 199 | + break; |
| 200 | + case UCT_DEVICE_LEVEL_GRID: |
| 201 | + return UCS_ERR_UNSUPPORTED; |
| 202 | + default: |
| 203 | + return UCS_ERR_INVALID_PARAM; |
| 204 | + } |
| 205 | + |
| 206 | + __syncthreads(); |
| 207 | + if (threadIdx.x == 0) { |
| 208 | + comp->count = 0; |
| 209 | + } |
| 210 | + return UCS_OK; |
| 211 | +} |
| 212 | + |
| 213 | +#endif /* UCT_CUDA_IPC_CUH */ |
0 commit comments