Files
rocm-systems/src/device/common_kernel.h
T

242 líneas
9.3 KiB
C++
Original Vista normal Histórico

2018-09-24 16:06:59 -07:00
/*************************************************************************
2022-01-07 06:39:55 -08:00
* Copyright (c) 2015-2022, NVIDIA CORPORATION. All rights reserved.
2018-09-24 16:06:59 -07:00
*
* See LICENSE.txt for license information
************************************************************************/
#ifndef NCCL_COMMON_KERNEL_H_
#define NCCL_COMMON_KERNEL_H_
2023-09-26 05:47:28 -07:00
#include "device.h"
2023-02-27 02:48:21 -08:00
#include "op128.h"
#include "reduce_kernel.h"
2018-09-24 16:06:59 -07:00
#include <cstdio>
#include <cstdint>
#include <cuda_runtime.h>
// Define min for ssize_t
2023-02-27 02:48:21 -08:00
inline __device__ int min(int a, ssize_t b) { return (a < b) ? a : b; }
2018-09-24 16:06:59 -07:00
2022-01-07 06:39:55 -08:00
inline __device__ int loadInt(int* ptr) {
int v;
asm volatile("ld.volatile.global.u32 %0, [%1];"
: "=r"(v) : "l"(ptr));
return v;
2020-09-04 14:35:05 -07:00
}
2023-02-27 02:48:21 -08:00
template<typename RedFn, typename T, int Unroll, int BytePerPack,
2023-04-03 05:32:07 -07:00
int MultimemSrcs, int MinSrcs, int MaxSrcs,
int MultimemDsts, int MinDsts, int MaxDsts, int PreOpSrcs,
2023-02-27 02:48:21 -08:00
typename IntBytes>
__device__ __forceinline__ void reduceCopyPacks(
int nThreads, int &thread,
uint64_t redArg, uint64_t *preOpArgs, bool postOp,
int nSrcs, void **srcPtrs, int nDsts, void **dstPtrs,
IntBytes &nBytesBehind, IntBytes &nBytesAhead
2021-07-08 14:12:04 -07:00
) {
2023-02-27 02:48:21 -08:00
static_assert(std::is_signed<IntBytes>::value, "IntBytes must be a signed integral type.");
2023-04-03 05:32:07 -07:00
if (BytePerPack == 0) __trap();
2023-02-27 02:48:21 -08:00
// A hunk is the amount of contiguous data a warp consumes per loop iteration
// assuming all threads partake.
constexpr int BytePerHunk = Unroll*WARP_SIZE*BytePerPack;
int nWarps = nThreads/WARP_SIZE;
int warp = thread/WARP_SIZE;
int lane = thread%WARP_SIZE;
// This thread's initial position.
IntBytes threadBytesBehind = nBytesBehind + (warp*BytePerHunk + lane*BytePerPack);
IntBytes threadBytesAhead = nBytesAhead - (warp*BytePerHunk + lane*BytePerPack);
// Number of hunks to be consumed over all warps.
2023-04-03 05:32:07 -07:00
IntBytes nHunksAhead = nBytesAhead/(BytePerHunk + !BytePerHunk);
2023-02-27 02:48:21 -08:00
// Advance collective position.
nBytesBehind += nHunksAhead*BytePerHunk;
nBytesAhead -= nHunksAhead*BytePerHunk;
if (Unroll==1 && BytePerPack <= nBytesAhead) {
// Only Unroll=1 can do partial hunks (where not all threads partake).
nHunksAhead += 1;
2023-04-03 05:32:07 -07:00
nBytesBehind += nBytesAhead - (nBytesAhead%(BytePerPack + !BytePerPack));
nBytesAhead = nBytesAhead%(BytePerPack + !BytePerPack);
2023-02-27 02:48:21 -08:00
}
nHunksAhead -= warp;
RedFn redFn(redArg);
uintptr_t minSrcs[MinSrcs + !MinSrcs];
uintptr_t minDsts[MinDsts + !MinDsts];
#pragma unroll
for (int s=0; s < MinSrcs; s++)
minSrcs[s] = cvta_to_global(srcPtrs[s]) + threadBytesBehind;
#pragma unroll
for (int d=0; d < MinDsts; d++)
minDsts[d] = cvta_to_global(dstPtrs[d]) + threadBytesBehind;
// We dictate loop termination condition according to whether partial hunks
// can be handled or not.
while (Unroll==1 ? (BytePerPack <= threadBytesAhead) : (0 < nHunksAhead)) {
BytePack<BytePerPack> acc[Unroll];
{ RedFn preFn(0 < PreOpSrcs ? preOpArgs[0] : 0);
#pragma unroll Unroll
for (int u=0; u < Unroll; u++) {
2023-04-03 05:32:07 -07:00
if (0 < MultimemSrcs) {
// applyLoadMultimem uses relaxed semantics for same reason we use volatile below.
2023-09-26 05:47:28 -07:00
acc[u] = applyLoadMultimem<RedFn, BytePerPack>(redFn, minSrcs[0]);
2023-04-03 05:32:07 -07:00
} else {
// Use volatile loads in case credits are polled for with volatile (instead of acquire).
acc[u] = ld_volatile_global<BytePerPack>(minSrcs[0]);
2023-09-26 05:47:28 -07:00
if (0 < PreOpSrcs) acc[u] = applyPreOp(preFn, acc[u]);
2023-04-03 05:32:07 -07:00
}
2023-02-27 02:48:21 -08:00
minSrcs[0] += WARP_SIZE*BytePerPack;
}
2021-07-08 14:12:04 -07:00
}
2020-09-04 14:35:05 -07:00
2023-02-27 02:48:21 -08:00
#pragma unroll (MinSrcs-1 + !(MinSrcs-1))
for (int s=1; s < MinSrcs; s++) {
BytePack<BytePerPack> tmp[Unroll];
RedFn preFn(s < PreOpSrcs ? preOpArgs[s] : 0);
#pragma unroll Unroll
for (int u=0; u < Unroll; u++) {
2023-04-03 05:32:07 -07:00
if (s < MultimemSrcs) {
// applyLoadMultimem uses relaxed semantics for same reason we use volatile below.
2023-09-26 05:47:28 -07:00
acc[u] = applyLoadMultimem<RedFn, BytePerPack>(redFn, minSrcs[s]);
2023-04-03 05:32:07 -07:00
} else {
// Use volatile loads in case credits are polled for with volatile (instead of acquire).
tmp[u] = ld_volatile_global<BytePerPack>(minSrcs[s]);
}
2023-02-27 02:48:21 -08:00
minSrcs[s] += WARP_SIZE*BytePerPack;
2021-09-08 13:56:25 -07:00
}
2023-02-27 02:48:21 -08:00
#pragma unroll Unroll
for (int u=0; u < Unroll; u++) {
if (s < PreOpSrcs) tmp[u] = applyPreOp(preFn, tmp[u]);
acc[u] = applyReduce(redFn, acc[u], tmp[u]);
2020-09-04 14:35:05 -07:00
}
}
2018-12-13 15:56:12 -08:00
2023-02-27 02:48:21 -08:00
for (int s=MinSrcs; (MinSrcs < MaxSrcs) && (s < MaxSrcs) && (s < nSrcs); s++) {
uintptr_t src = cvta_to_global(srcPtrs[s]) + threadBytesBehind;
BytePack<BytePerPack> tmp[Unroll];
RedFn preFn(s < PreOpSrcs ? preOpArgs[s] : 0);
#pragma unroll Unroll
for (int u=0; u < Unroll; u++) {
// Use volatile loads in case credits are polled for with volatile (instead of acquire).
tmp[u] = ld_volatile_global<BytePerPack>(src);
src += WARP_SIZE*BytePerPack;
}
#pragma unroll Unroll
for (int u=0; u < Unroll; u++) {
if (s < PreOpSrcs) tmp[u] = applyPreOp(preFn, tmp[u]);
acc[u] = applyReduce(redFn, acc[u], tmp[u]);
2020-09-04 14:35:05 -07:00
}
}
2018-12-13 15:56:12 -08:00
2023-02-27 02:48:21 -08:00
if (postOp) {
#pragma unroll Unroll
for (int u=0; u < Unroll; u++)
acc[u] = applyPostOp(redFn, acc[u]);
2021-07-08 14:12:04 -07:00
}
2018-12-13 15:56:12 -08:00
2023-02-27 02:48:21 -08:00
#pragma unroll (MinDsts + !MinDsts)
for (int d=0; d < MinDsts; d++) {
#pragma unroll Unroll
for (int u=0; u < Unroll; u++) {
2023-04-03 05:32:07 -07:00
if (d < MultimemDsts) {
multimem_st_global(minDsts[d], acc[u]);
} else {
st_global<BytePerPack>(minDsts[d], acc[u]);
}
2023-02-27 02:48:21 -08:00
minDsts[d] += WARP_SIZE*BytePerPack;
2021-09-08 13:56:25 -07:00
}
2018-09-24 16:06:59 -07:00
}
2023-02-27 02:48:21 -08:00
for (int d=MinDsts; (MinDsts < MaxDsts) && (d < MaxDsts) && (d < nDsts); d++) {
uintptr_t dst = cvta_to_global(dstPtrs[d]) + threadBytesBehind;
#pragma unroll Unroll
for (int u=0; u < Unroll; u++) {
st_global<BytePerPack>(dst, acc[u]);
dst += WARP_SIZE*BytePerPack;
2020-09-04 14:35:05 -07:00
}
2018-09-24 16:06:59 -07:00
}
2018-12-13 15:56:12 -08:00
2023-02-27 02:48:21 -08:00
nWarps = nThreads/WARP_SIZE;
2020-09-04 14:35:05 -07:00
#pragma unroll
2023-02-27 02:48:21 -08:00
for (int s=0; s < MinSrcs; s++) minSrcs[s] += (nWarps-1)*BytePerHunk;
2020-09-04 14:35:05 -07:00
#pragma unroll
2023-02-27 02:48:21 -08:00
for (int d=0; d < MinDsts; d++) minDsts[d] += (nWarps-1)*BytePerHunk;
threadBytesBehind += nWarps*BytePerHunk;
threadBytesAhead -= nWarps*BytePerHunk;
nHunksAhead -= nWarps;
}
nWarps = nThreads/WARP_SIZE;
warp = thread/WARP_SIZE;
lane = thread%WARP_SIZE;
// The last loop iteration could have been partial, i.e. not taken by all
// threads. The threads that weren't included need an extra subtraction to
// make the value warp uniform.
if (Unroll==1 && nHunksAhead > 0) nHunksAhead -= nWarps;
// Rotate warps so the warp which got the least work here will be warp 0.
// This effectively assigns: warp = (warp-nHunks+nWarps)%nWarps;
warp = -nHunksAhead;
thread = warp*WARP_SIZE + lane;
2018-09-24 16:06:59 -07:00
}
2023-02-27 02:48:21 -08:00
template<int Unroll, typename RedFn, typename T,
2023-04-03 05:32:07 -07:00
int MultimemSrcs, int MinSrcs, int MaxSrcs,
int MultimemDsts, int MinDsts, int MaxDsts, int PreOpSrcs,
2023-02-27 02:48:21 -08:00
typename IntBytes>
2023-04-03 05:32:07 -07:00
__device__ __forceinline__ void reduceCopy(
2023-02-27 02:48:21 -08:00
int thread, int nThreads,
uint64_t redArg, uint64_t *preOpArgs, bool postOp,
int nSrcs, void **srcPtrs, int nDsts, void **dstPtrs,
IntBytes nElts
2021-07-08 14:12:04 -07:00
) {
2023-04-03 05:32:07 -07:00
static_assert(MultimemSrcs <= MinSrcs && MultimemDsts <= MinDsts, "Multimem pointers cannot exceed respective Min values.");
2023-02-27 02:48:21 -08:00
//int nWarps = nThreads/WARP_SIZE;
//int warp = thread/WARP_SIZE;
int lane = thread%WARP_SIZE;
2023-04-03 05:32:07 -07:00
// If a multimem src is present then our biggest pack size is limited to what
// is supported for this redfn/type.
constexpr int BigPackSize = (MultimemSrcs == 0) ? 16 : LoadMultimem_BigPackSize<RedFn>::BigPackSize;
2023-02-27 02:48:21 -08:00
IntBytes nBytesBehind = 0;
IntBytes nBytesAhead = nElts*sizeof(T);
2023-04-03 05:32:07 -07:00
#if __cpp_if_constexpr
if constexpr (BigPackSize > sizeof(T)) {
#else
if (BigPackSize > sizeof(T)) {
#endif
// Check that all pointers are BigPackSize aligned.
bool aligned = true;
if (lane < nSrcs) aligned &= 0 == cvta_to_global(srcPtrs[lane]) % (BigPackSize + !BigPackSize);
if (lane < nDsts) aligned &= 0 == cvta_to_global(dstPtrs[lane]) % (BigPackSize + !BigPackSize);
aligned = __all_sync(~0u, aligned);
if (aligned) {
reduceCopyPacks<RedFn, T, Unroll, BigPackSize,
MultimemSrcs, MinSrcs, MaxSrcs, MultimemDsts, MinDsts, MaxDsts, PreOpSrcs>
(nThreads, /*&*/thread, redArg, preOpArgs, postOp,
nSrcs, srcPtrs, nDsts, dstPtrs, /*&*/nBytesBehind, /*&*/nBytesAhead);
if (nBytesAhead == 0) return;
reduceCopyPacks<RedFn, T, /*Unroll=*/1, BigPackSize,
MultimemSrcs, MinSrcs, MaxSrcs, MultimemDsts, MinDsts, MaxDsts, PreOpSrcs>
(nThreads, /*&*/thread, redArg, preOpArgs, postOp,
nSrcs, srcPtrs, nDsts, dstPtrs, /*&*/nBytesBehind, /*&*/nBytesAhead);
if (nBytesAhead == 0) return;
}
2023-02-27 02:48:21 -08:00
}
reduceCopyPacks<RedFn, T, Unroll*(16/sizeof(T))/2, /*BytePerPack=*/sizeof(T),
2023-04-03 05:32:07 -07:00
MultimemSrcs, MinSrcs, MaxSrcs, MultimemDsts, MinDsts, MaxDsts, PreOpSrcs>
2023-02-27 02:48:21 -08:00
(nThreads, /*&*/thread, redArg, preOpArgs, postOp,
nSrcs, srcPtrs, nDsts, dstPtrs, /*&*/nBytesBehind, /*&*/nBytesAhead);
if (nBytesAhead == 0) return;
reduceCopyPacks<RedFn, T, /*Unroll=*/1, /*BytePerPack=*/sizeof(T),
2023-04-03 05:32:07 -07:00
MultimemSrcs, MinSrcs, MaxSrcs, MultimemDsts, MinDsts, MaxDsts, PreOpSrcs>
2023-02-27 02:48:21 -08:00
(nThreads, /*&*/thread, redArg, preOpArgs, postOp,
nSrcs, srcPtrs, nDsts, dstPtrs, /*&*/nBytesBehind, /*&*/nBytesAhead);
}
2020-09-04 14:35:05 -07:00
2018-09-24 16:06:59 -07:00
#endif // COMMON_KERNEL_H_