12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115 |
- // Copyright (c) Microsoft Corporation.
- // SPDX-License-Identifier: Apache-2.0
- // DeepSpeed Team
- #pragma once
- #include <cuda.h>
- #include "ds_kernel_utils.h"
- /////////////////////////////// Memory Access Utils ///////////////////////////////
- namespace mem_access {
- enum class LoadPolicy {
- CacheAll, // Cache at all levels
- CacheGlobal, // Cache at L2 only
- CacheStreaming // Cache with evict first policy
- };
- enum class StorePolicy {
- Writeback, // Cache in L1, write-back on eviction
- CacheGlobal, // Bypass L1, write-back on eviction
- CacheStreaming // Allocate cache line with evict first policy
- };
- template <int AccessSize, LoadPolicy policy = LoadPolicy::CacheAll>
- __device__ __forceinline__ void load_global(void* dst, const void* src);
- template <int AccessSize, LoadPolicy policy = LoadPolicy::CacheAll>
- __device__ __forceinline__ void load_global(void* dst, const void* src, bool do_access);
- // Shared accesses have no cache policy
- template <int AccessSize>
- __device__ __forceinline__ void load_shared(void* dst, const void* src);
- template <int AccessSize>
- __device__ __forceinline__ void load_shared(void* dst, const void* src, bool do_access);
- template <int AccessSize, StorePolicy policy = StorePolicy::Writeback>
- __device__ __forceinline__ void store_global(void* dst, const void* src);
- // Shared accesses have no cache policy
- template <int AccessSize>
- __device__ __forceinline__ void store_shared(void* dst, const void* src);
- #ifdef ASYNC_COPY_AVAILABLE
- template <int AccessSize>
- __device__ __forceinline__ void memcpy_async(void* shr, const void* gbl);
- template <int AccessSize>
- __device__ __forceinline__ void memcpy_async_nop(void* shr, const void* gbl, bool predicate);
- template <int AccessSize>
- __device__ __forceinline__ void memcpy_async_zero(void* shr, const void* gbl, bool predicate);
- __device__ __forceinline__ void memcpy_async_fence();
- template <int stages>
- __device__ __forceinline__ void memcpy_async_wait();
- template <int stages>
- __device__ __forceinline__ void tail_complete_wait(int remaining_stages);
- #endif
- // Util for tracking pipeline buffers
- // TODO: Evaluate whether this should also be guarded by ASYNC_COPY_AVAILABLE
- template <int max>
- class BufferTracker {
- public:
- int current_state;
- __device__ __forceinline__ BufferTracker() : current_state(0) {}
- __device__ __forceinline__ int get()
- {
- int return_val = current_state++;
- current_state = (current_state == max ? 0 : current_state);
- return return_val;
- }
- };
- __device__ __forceinline__ uint32_t lane_id()
- {
- #ifdef PTX_AVAILABLE
- unsigned int lane_id;
- asm volatile("mov.u32 %0, %%laneid;" : "=r"(lane_id));
- return lane_id;
- #else
- return threadIdx.x & (warpSize - 1); // Portable
- #endif
- }
- /////////// Load Global ///////////
- template <>
- __device__ __forceinline__ void load_global<16>(void* dst, const void* src)
- {
- uint4* data = reinterpret_cast<uint4*>(dst);
- #ifdef PTX_AVAILABLE
- asm volatile("ld.global.ca.v4.u32 {%0, %1, %2, %3}, [%4];\n"
- : "=r"(data[0].x), "=r"(data[0].y), "=r"(data[0].z), "=r"(data[0].w)
- : "l"(src));
- #else
- const uint4* src_cast = reinterpret_cast<const uint4*>(src);
- data[0] = src_cast[0];
- #endif
- }
- template <>
- __device__ __forceinline__ void load_global<16>(void* dst, const void* src, bool do_access)
- {
- uint4* data = reinterpret_cast<uint4*>(dst);
- #ifdef PTX_AVAILABLE
- asm volatile(
- "{\n"
- "\t.reg .pred p;\n"
- "\tsetp.ne.b32 p, %5, 0;\n"
- "\tmov.b32 %0, 0;\n"
- "\tmov.b32 %1, 0;\n"
- "\tmov.b32 %2, 0;\n"
- "\tmov.b32 %3, 0;\n"
- "\t@p ld.global.v4.u32 {%0, %1, %2, %3}, [%4];\n"
- "}\n"
- : "=r"(data[0].x), "=r"(data[0].y), "=r"(data[0].z), "=r"(data[0].w)
- : "l"(src), "r"((int)do_access));
- #else
- const uint4* src_cast = reinterpret_cast<const uint4*>(src);
- if (do_access) {
- data[0] = src_cast[0];
- } else {
- data[0].x = 0;
- data[0].y = 0;
- data[0].z = 0;
- data[0].w = 0;
- }
- #endif
- }
- template <>
- __device__ __forceinline__ void load_global<16, LoadPolicy::CacheGlobal>(void* dst, const void* src)
- {
- uint4* data = reinterpret_cast<uint4*>(dst);
- #ifdef PTX_AVAILABLE
- asm volatile("ld.global.cg.v4.u32 {%0, %1, %2, %3}, [%4];\n"
- : "=r"(data[0].x), "=r"(data[0].y), "=r"(data[0].z), "=r"(data[0].w)
- : "l"(src));
- #else
- const uint4* src_cast = reinterpret_cast<const uint4*>(src);
- data[0] = src_cast[0];
- #endif
- }
- template <>
- __device__ __forceinline__ void load_global<16, LoadPolicy::CacheGlobal>(void* dst,
- const void* src,
- bool do_access)
- {
- uint4* data = reinterpret_cast<uint4*>(dst);
- #ifdef PTX_AVAILABLE
- asm volatile(
- "{\n"
- "\t.reg .pred p;\n"
- "\tsetp.ne.b32 p, %5, 0;\n"
- "\tmov.b32 %0, 0;\n"
- "\tmov.b32 %1, 0;\n"
- "\tmov.b32 %2, 0;\n"
- "\tmov.b32 %3, 0;\n"
- "\t@p ld.global.cg.v4.u32 {%0, %1, %2, %3}, [%4];\n"
- "}\n"
- : "=r"(data[0].x), "=r"(data[0].y), "=r"(data[0].z), "=r"(data[0].w)
- : "l"(src), "r"((int)do_access));
- #else
- const uint4* src_cast = reinterpret_cast<const uint4*>(src);
- if (do_access) {
- data[0] = src_cast[0];
- } else {
- data[0].x = 0;
- data[0].y = 0;
- data[0].z = 0;
- data[0].w = 0;
- }
- #endif
- }
- template <>
- __device__ __forceinline__ void load_global<16, LoadPolicy::CacheStreaming>(void* dst,
- const void* src)
- {
- uint4* data = reinterpret_cast<uint4*>(dst);
- #ifdef PTX_AVAILABLE
- asm volatile("ld.global.cs.v4.u32 {%0, %1, %2, %3}, [%4];\n"
- : "=r"(data[0].x), "=r"(data[0].y), "=r"(data[0].z), "=r"(data[0].w)
- : "l"(src));
- #else
- const uint4* src_cast = reinterpret_cast<const uint4*>(src);
- data[0] = src_cast[0];
- #endif
- }
- template <>
- __device__ __forceinline__ void load_global<16, LoadPolicy::CacheStreaming>(void* dst,
- const void* src,
- bool do_access)
- {
- uint4* data = reinterpret_cast<uint4*>(dst);
- #ifdef PTX_AVAILABLE
- asm volatile(
- "{\n"
- "\t.reg .pred p;\n"
- "\tsetp.ne.b32 p, %5, 0;\n"
- "\tmov.b32 %0, 0;\n"
- "\tmov.b32 %1, 0;\n"
- "\tmov.b32 %2, 0;\n"
- "\tmov.b32 %3, 0;\n"
- "\t@p ld.global.cg.v4.u32 {%0, %1, %2, %3}, [%4];\n"
- "}\n"
- : "=r"(data[0].x), "=r"(data[0].y), "=r"(data[0].z), "=r"(data[0].w)
- : "l"(src), "r"((int)do_access));
- #else
- const uint4* src_cast = reinterpret_cast<const uint4*>(src);
- if (do_access) {
- data[0] = src_cast[0];
- } else {
- data[0].x = 0;
- data[0].y = 0;
- data[0].z = 0;
- data[0].w = 0;
- }
- #endif
- }
- template <>
- __device__ __forceinline__ void load_global<8>(void* dst, const void* src)
- {
- uint2* data = reinterpret_cast<uint2*>(dst);
- #ifdef PTX_AVAILABLE
- asm volatile("ld.global.ca.v2.u32 {%0, %1}, [%2];\n"
- : "=r"(data[0].x), "=r"(data[0].y)
- : "l"(src));
- #else
- const uint2* src_cast = reinterpret_cast<const uint2*>(src);
- data[0] = src_cast[0];
- #endif
- }
- template <>
- __device__ __forceinline__ void load_global<8>(void* dst, const void* src, bool do_access)
- {
- uint2* data = reinterpret_cast<uint2*>(dst);
- #ifdef PTX_AVAILABLE
- asm volatile(
- "{\n"
- "\t.reg .pred p;\n"
- "\tsetp.ne.b32 p, %3, 0;\n"
- "\tmov.b32 %0, 0;\n"
- "\tmov.b32 %1, 0;\n"
- "\t@p ld.global.v2.u32 {%0, %1}, [%2];\n"
- "}\n"
- : "=r"(data[0].x), "=r"(data[0].y)
- : "l"(src), "r"((int)do_access));
- #else
- const uint2* src_cast = reinterpret_cast<const uint2*>(src);
- if (do_access) {
- data[0] = src_cast[0];
- } else {
- data[0].x = 0;
- data[0].y = 0;
- }
- #endif
- }
- template <>
- __device__ __forceinline__ void load_global<8, LoadPolicy::CacheGlobal>(void* dst, const void* src)
- {
- uint2* data = reinterpret_cast<uint2*>(dst);
- #ifdef PTX_AVAILABLE
- asm volatile("ld.global.cg.v2.u32 {%0, %1}, [%2];\n"
- : "=r"(data[0].x), "=r"(data[0].y)
- : "l"(src));
- #else
- const uint2* src_cast = reinterpret_cast<const uint2*>(src);
- data[0] = src_cast[0];
- #endif
- }
- template <>
- __device__ __forceinline__ void load_global<8, LoadPolicy::CacheGlobal>(void* dst,
- const void* src,
- bool do_access)
- {
- uint2* data = reinterpret_cast<uint2*>(dst);
- #ifdef PTX_AVAILABLE
- asm volatile(
- "{\n"
- "\t.reg .pred p;\n"
- "\tsetp.ne.b32 p, %3, 0;\n"
- "\tmov.b32 %0, 0;\n"
- "\tmov.b32 %1, 0;\n"
- "\t@p ld.global.cg.v2.u32 {%0, %1}, [%2];\n"
- "}\n"
- : "=r"(data[0].x), "=r"(data[0].y)
- : "l"(src), "r"((int)do_access));
- #else
- const uint2* src_cast = reinterpret_cast<const uint2*>(src);
- if (do_access) {
- data[0] = src_cast[0];
- } else {
- data[0].x = 0;
- data[0].y = 0;
- }
- #endif
- }
- template <>
- __device__ __forceinline__ void load_global<8, LoadPolicy::CacheStreaming>(void* dst,
- const void* src)
- {
- uint2* data = reinterpret_cast<uint2*>(dst);
- #ifdef PTX_AVAILABLE
- asm volatile("ld.global.cs.v2.u32 {%0, %1}, [%2];\n"
- : "=r"(data[0].x), "=r"(data[0].y)
- : "l"(src));
- #else
- const uint2* src_cast = reinterpret_cast<const uint2*>(src);
- data[0] = src_cast[0];
- #endif
- }
- template <>
- __device__ __forceinline__ void load_global<8, LoadPolicy::CacheStreaming>(void* dst,
- const void* src,
- bool do_access)
- {
- uint2* data = reinterpret_cast<uint2*>(dst);
- #ifdef PTX_AVAILABLE
- asm volatile(
- "{\n"
- "\t.reg .pred p;\n"
- "\tsetp.ne.b32 p, %3, 0;\n"
- "\tmov.b32 %0, 0;\n"
- "\tmov.b32 %1, 0;\n"
- "\t@p ld.global.cs.v2.u32 {%0, %1}, [%2];\n"
- "}\n"
- : "=r"(data[0].x), "=r"(data[0].y)
- : "l"(src), "r"((int)do_access));
- #else
- const uint2* src_cast = reinterpret_cast<const uint2*>(src);
- if (do_access) {
- data[0] = src_cast[0];
- } else {
- data[0].x = 0;
- data[0].y = 0;
- }
- #endif
- }
- template <>
- __device__ __forceinline__ void load_global<4>(void* dst, const void* src)
- {
- int32_t* data = reinterpret_cast<int32_t*>(dst);
- #ifdef PTX_AVAILABLE
- asm volatile("ld.global.ca.u32 {%0}, [%1];\n" : "=r"(*data) : "l"(src));
- #else
- const int32_t* src_cast = reinterpret_cast<const int32_t*>(src);
- data[0] = src_cast[0];
- #endif
- }
- template <>
- __device__ __forceinline__ void load_global<4>(void* dst, const void* src, bool do_access)
- {
- int32_t* data = reinterpret_cast<int32_t*>(dst);
- #ifdef PTX_AVAILABLE
- asm volatile(
- "{\n"
- "\t.reg .pred p;\n"
- "\tsetp.ne.b32 p, %2, 0;\n"
- "\tmov.b32 %0, 0;\n"
- "\t@p ld.global.u32 {%0}, [%1];\n"
- "}\n"
- : "=r"(data[0])
- : "l"(src), "r"((int)do_access));
- #else
- const int32_t* src_cast = reinterpret_cast<const int32_t*>(src);
- if (do_access) {
- data[0] = src_cast[0];
- } else {
- data[0] = 0;
- }
- #endif
- }
- template <>
- __device__ __forceinline__ void load_global<4, LoadPolicy::CacheGlobal>(void* dst, const void* src)
- {
- int32_t* data = reinterpret_cast<int32_t*>(dst);
- #ifdef PTX_AVAILABLE
- asm volatile("ld.global.cg.u32 {%0}, [%1];\n" : "=r"(*data) : "l"(src));
- #else
- const int32_t* src_cast = reinterpret_cast<const int32_t*>(src);
- data[0] = src_cast[0];
- #endif
- }
- template <>
- __device__ __forceinline__ void load_global<4, LoadPolicy::CacheGlobal>(void* dst,
- const void* src,
- bool do_access)
- {
- int32_t* data = reinterpret_cast<int32_t*>(dst);
- #ifdef PTX_AVAILABLE
- asm volatile(
- "{\n"
- "\t.reg .pred p;\n"
- "\tsetp.ne.b32 p, %2, 0;\n"
- "\tmov.b32 %0, 0;\n"
- "\t@p ld.global.cg.u32 {%0}, [%1];\n"
- "}\n"
- : "=r"(data[0])
- : "l"(src), "r"((int)do_access));
- #else
- const int32_t* src_cast = reinterpret_cast<const int32_t*>(src);
- if (do_access) {
- data[0] = src_cast[0];
- } else {
- data[0] = 0;
- }
- #endif
- }
- template <>
- __device__ __forceinline__ void load_global<4, LoadPolicy::CacheStreaming>(void* dst,
- const void* src)
- {
- int32_t* data = reinterpret_cast<int32_t*>(dst);
- #ifdef PTX_AVAILABLE
- asm volatile("ld.global.cs.u32 {%0}, [%1];\n" : "=r"(*data) : "l"(src));
- #else
- const int32_t* src_cast = reinterpret_cast<const int32_t*>(src);
- data[0] = src_cast[0];
- #endif
- }
- template <>
- __device__ __forceinline__ void load_global<4, LoadPolicy::CacheStreaming>(void* dst,
- const void* src,
- bool do_access)
- {
- int32_t* data = reinterpret_cast<int32_t*>(dst);
- #ifdef PTX_AVAILABLE
- asm volatile(
- "{\n"
- "\t.reg .pred p;\n"
- "\tsetp.ne.b32 p, %2, 0;\n"
- "\tmov.b32 %0, 0;\n"
- "\t@p ld.global.cs.u32 {%0}, [%1];\n"
- "}\n"
- : "=r"(data[0])
- : "l"(src), "r"((int)do_access));
- #else
- const int32_t* src_cast = reinterpret_cast<const int32_t*>(src);
- if (do_access) {
- data[0] = src_cast[0];
- } else {
- data[0] = 0;
- }
- #endif
- }
- template <>
- __device__ __forceinline__ void load_global<2>(void* dst, const void* src)
- {
- int16_t* data = reinterpret_cast<int16_t*>(dst);
- #ifdef PTX_AVAILABLE
- asm volatile("ld.global.ca.u16 {%0}, [%1];\n" : "=h"(*data) : "l"(src));
- #else
- const int16_t* src_cast = reinterpret_cast<const int16_t*>(src);
- data[0] = src_cast[0];
- #endif
- }
- template <>
- __device__ __forceinline__ void load_global<2>(void* dst, const void* src, bool do_access)
- {
- int16_t* data = reinterpret_cast<int16_t*>(dst);
- #ifdef PTX_AVAILABLE
- asm volatile(
- "{\n"
- "\t.reg .pred p;\n"
- "\tsetp.ne.b32 p, %2, 0;\n"
- "\tmov.u16 %0, 0;\n"
- "\t@p ld.global.u16 {%0}, [%1];\n"
- "}\n"
- : "=h"(*data)
- : "l"(src), "r"((int)do_access));
- #else
- const int16_t* src_cast = reinterpret_cast<const int16_t*>(src);
- if (do_access) {
- data[0] = src_cast[0];
- } else {
- data[0] = 0;
- }
- #endif
- }
- template <>
- __device__ __forceinline__ void load_global<2, LoadPolicy::CacheGlobal>(void* dst, const void* src)
- {
- int16_t* data = reinterpret_cast<int16_t*>(dst);
- #ifdef PTX_AVAILABLE
- asm volatile("ld.global.cg.u16 {%0}, [%1];\n" : "=h"(*data) : "l"(src));
- #else
- const int16_t* src_cast = reinterpret_cast<const int16_t*>(src);
- data[0] = src_cast[0];
- #endif
- }
- template <>
- __device__ __forceinline__ void load_global<2, LoadPolicy::CacheGlobal>(void* dst,
- const void* src,
- bool do_access)
- {
- int16_t* data = reinterpret_cast<int16_t*>(dst);
- #ifdef PTX_AVAILABLE
- asm volatile(
- "{\n"
- "\t.reg .pred p;\n"
- "\tsetp.ne.b32 p, %2, 0;\n"
- "\tmov.u16 %0, 0;\n"
- "\t@p ld.global.cg.u16 {%0}, [%1];\n"
- "}\n"
- : "=h"(*data)
- : "l"(src), "r"((int)do_access));
- #else
- const int16_t* src_cast = reinterpret_cast<const int16_t*>(src);
- if (do_access) {
- data[0] = src_cast[0];
- } else {
- data[0] = 0;
- }
- #endif
- }
- template <>
- __device__ __forceinline__ void load_global<2, LoadPolicy::CacheStreaming>(void* dst,
- const void* src)
- {
- int16_t* data = reinterpret_cast<int16_t*>(dst);
- #ifdef PTX_AVAILABLE
- asm volatile("ld.global.cs.u16 {%0}, [%1];\n" : "=h"(*data) : "l"(src));
- #else
- const int16_t* src_cast = reinterpret_cast<const int16_t*>(src);
- data[0] = src_cast[0];
- #endif
- }
- template <>
- __device__ __forceinline__ void load_global<2, LoadPolicy::CacheStreaming>(void* dst,
- const void* src,
- bool do_access)
- {
- int16_t* data = reinterpret_cast<int16_t*>(dst);
- #ifdef PTX_AVAILABLE
- asm volatile(
- "{\n"
- "\t.reg .pred p;\n"
- "\tsetp.ne.b32 p, %2, 0;\n"
- "\tmov.u16 %0, 0;\n"
- "\t@p ld.global.cs.u16 {%0}, [%1];\n"
- "}\n"
- : "=h"(*data)
- : "l"(src), "r"((int)do_access));
- #else
- const int16_t* src_cast = reinterpret_cast<const int16_t*>(src);
- if (do_access) {
- data[0] = src_cast[0];
- } else {
- data[0] = 0;
- }
- #endif
- }
- /////////// Load Shared ///////////
- namespace internal {
- #ifdef PTX_AVAILABLE
- __device__ __forceinline__ unsigned convert_to_shared(const void* ptr)
- {
- #if __CUDACC_VER_MAJOR__ >= 11
- // In CUDA 11 we have a builtin intrinsic
- return __cvta_generic_to_shared(ptr);
- #else
- unsigned ret_val;
- asm volatile(
- "{\n"
- "\t.reg .u64 p1;\n"
- "\tcvta.to.shared.u64 p1, %1\n"
- "\tcvt.u32.u64 %0, p1;\n"
- "}\n"
- : "=r"(ret_val)
- : "l"(ptr));
- return ret_val;
- #endif
- }
- #endif
- } // namespace internal
- template <>
- __device__ __forceinline__ void load_shared<16>(void* dst, const void* src)
- {
- uint4* data = reinterpret_cast<uint4*>(dst);
- #ifdef PTX_AVAILABLE
- unsigned src_shr = internal::convert_to_shared(src);
- asm volatile("ld.shared.v4.u32 {%0, %1, %2, %3}, [%4];\n"
- : "=r"(data[0].x), "=r"(data[0].y), "=r"(data[0].z), "=r"(data[0].w)
- : "r"(src_shr));
- #else
- const uint4* src_cast = reinterpret_cast<const uint4*>(src);
- data[0] = src_cast[0];
- #endif
- }
- template <>
- __device__ __forceinline__ void load_shared<16>(void* dst, const void* src, bool do_access)
- {
- uint4* data = reinterpret_cast<uint4*>(dst);
- #ifdef PTX_AVAILABLE
- unsigned src_shr = internal::convert_to_shared(src);
- asm volatile(
- "{\n"
- "\t.reg .pred p;\n"
- "\tsetp.ne.b32 p, %5, 0;\n"
- "\tmov.b32 %0, 0;\n"
- "\tmov.b32 %1, 0;\n"
- "\tmov.b32 %2, 0;\n"
- "\tmov.b32 %3, 0;\n"
- "\t@p ld.shared.v4.u32 {%0, %1, %2, %3}, [%4];\n"
- "}\n"
- : "=r"(data[0].x), "=r"(data[0].y), "=r"(data[0].z), "=r"(data[0].w)
- : "r"(src_shr), "r"((int)do_access));
- #else
- const uint4* src_cast = reinterpret_cast<const uint4*>(src);
- if (do_access) {
- data[0] = src_cast[0];
- } else {
- data[0].x = 0;
- data[0].y = 0;
- data[0].z = 0;
- data[0].w = 0;
- }
- #endif
- }
- template <>
- __device__ __forceinline__ void load_shared<8>(void* dst, const void* src)
- {
- uint2* data = reinterpret_cast<uint2*>(dst);
- #ifdef PTX_AVAILABLE
- unsigned src_shr = internal::convert_to_shared(src);
- asm volatile("ld.shared.v2.u32 {%0, %1}, [%2];\n"
- : "=r"(data[0].x), "=r"(data[0].y)
- : "r"(src_shr));
- #else
- const uint2* src_cast = reinterpret_cast<const uint2*>(src);
- data[0] = src_cast[0];
- #endif
- }
- template <>
- __device__ __forceinline__ void load_shared<8>(void* dst, const void* src, bool do_access)
- {
- uint2* data = reinterpret_cast<uint2*>(dst);
- #ifdef PTX_AVAILABLE
- unsigned src_shr = internal::convert_to_shared(src);
- asm volatile(
- "{\n"
- "\t.reg .pred p;\n"
- "\tsetp.ne.b32 p, %3, 0;\n"
- "\tmov.b32 %0, 0;\n"
- "\tmov.b32 %1, 0;\n"
- "\t@p ld.shared.v2.u32 {%0, %1}, [%2];\n"
- "}\n"
- : "=r"(data[0].x), "=r"(data[0].y)
- : "r"(src_shr), "r"((int)do_access));
- #else
- const uint2* src_cast = reinterpret_cast<const uint2*>(src);
- if (do_access) {
- data[0] = src_cast[0];
- } else {
- data[0].x = 0;
- data[0].y = 0;
- }
- #endif
- }
- template <>
- __device__ __forceinline__ void load_shared<4>(void* dst, const void* src)
- {
- int32_t* data = reinterpret_cast<int32_t*>(dst);
- #ifdef PTX_AVAILABLE
- unsigned src_shr = internal::convert_to_shared(src);
- asm volatile("ld.shared.u32 {%0}, [%1];\n" : "=r"(*data) : "r"(src_shr));
- #else
- const int32_t* src_cast = reinterpret_cast<const int32_t*>(src);
- data[0] = src_cast[0];
- #endif
- }
- template <>
- __device__ __forceinline__ void load_shared<4>(void* dst, const void* src, bool do_access)
- {
- int32_t* data = reinterpret_cast<int32_t*>(dst);
- #ifdef PTX_AVAILABLE
- unsigned src_shr = internal::convert_to_shared(src);
- asm volatile(
- "{\n"
- "\t.reg .pred p;\n"
- "\tsetp.ne.b32 p, %2, 0;\n"
- "\tmov.b32 %0, 0;\n"
- "\t@p ld.shared.u32 %0, [%1];\n"
- "}\n"
- : "=r"(data[0])
- : "r"(src_shr), "r"((int)do_access));
- #else
- const int32_t* src_cast = reinterpret_cast<const int32_t*>(src);
- if (do_access) {
- data[0] = src_cast[0];
- } else {
- data[0] = 0;
- }
- #endif
- }
- /////////// Store Global ///////////
- template <>
- __device__ __forceinline__ void store_global<16>(void* dst, const void* src)
- {
- const uint4* data = reinterpret_cast<const uint4*>(src);
- #ifdef PTX_AVAILABLE
- asm volatile("st.global.wb.v4.u32 [%0], {%1, %2, %3, %4};\n"
- :
- : "l"(dst), "r"(data[0].x), "r"(data[0].y), "r"(data[0].z), "r"(data[0].w)
- : "memory");
- #else
- uint4* dst_cast = reinterpret_cast<uint4*>(dst);
- dst_cast[0] = data[0];
- #endif
- }
- template <>
- __device__ __forceinline__ void store_global<16, StorePolicy::CacheGlobal>(void* dst,
- const void* src)
- {
- const uint4* data = reinterpret_cast<const uint4*>(src);
- #ifdef PTX_AVAILABLE
- asm volatile("st.global.cg.v4.u32 [%0], {%1, %2, %3, %4};\n"
- :
- : "l"(dst), "r"(data[0].x), "r"(data[0].y), "r"(data[0].z), "r"(data[0].w)
- : "memory");
- #else
- uint4* dst_cast = reinterpret_cast<uint4*>(dst);
- dst_cast[0] = data[0];
- #endif
- }
- template <>
- __device__ __forceinline__ void store_global<16, StorePolicy::CacheStreaming>(void* dst,
- const void* src)
- {
- const uint4* data = reinterpret_cast<const uint4*>(src);
- #ifdef PTX_AVAILABLE
- asm volatile("st.global.cs.v4.u32 [%0], {%1, %2, %3, %4};\n"
- :
- : "l"(dst), "r"(data[0].x), "r"(data[0].y), "r"(data[0].z), "r"(data[0].w)
- : "memory");
- #else
- uint4* dst_cast = reinterpret_cast<uint4*>(dst);
- dst_cast[0] = data[0];
- #endif
- }
- template <>
- __device__ __forceinline__ void store_global<8>(void* dst, const void* src)
- {
- const uint2* data = reinterpret_cast<const uint2*>(src);
- #ifdef PTX_AVAILABLE
- asm volatile("st.global.wb.v2.u32 [%0], {%1, %2};\n"
- :
- : "l"(dst), "r"(data[0].x), "r"(data[0].y));
- #else
- uint2* dst_cast = reinterpret_cast<uint2*>(dst);
- dst_cast[0] = data[0];
- #endif
- }
- template <>
- __device__ __forceinline__ void store_global<8, StorePolicy::CacheGlobal>(void* dst,
- const void* src)
- {
- const uint2* data = reinterpret_cast<const uint2*>(src);
- #ifdef PTX_AVAILABLE
- asm volatile("st.global.cg.v2.u32 [%0], {%1, %2};\n"
- :
- : "l"(dst), "r"(data[0].x), "r"(data[0].y));
- #else
- uint2* dst_cast = reinterpret_cast<uint2*>(dst);
- dst_cast[0] = data[0];
- #endif
- }
- template <>
- __device__ __forceinline__ void store_global<8, StorePolicy::CacheStreaming>(void* dst,
- const void* src)
- {
- const uint2* data = reinterpret_cast<const uint2*>(src);
- #ifdef PTX_AVAILABLE
- asm volatile("st.global.cs.v2.u32 [%0], {%1, %2};\n"
- :
- : "l"(dst), "r"(data[0].x), "r"(data[0].y));
- #else
- uint2* dst_cast = reinterpret_cast<uint2*>(dst);
- dst_cast[0] = data[0];
- #endif
- }
- template <>
- __device__ __forceinline__ void store_global<4>(void* dst, const void* src)
- {
- const int32_t* data = reinterpret_cast<const int32_t*>(src);
- #ifdef PTX_AVAILABLE
- asm volatile("st.global.wb.u32 [%0], %1;\n" : : "l"(dst), "r"(*data));
- #else
- int32_t* dst_cast = reinterpret_cast<int32_t*>(dst);
- dst_cast[0] = data[0];
- #endif
- }
- template <>
- __device__ __forceinline__ void store_global<4, StorePolicy::CacheGlobal>(void* dst,
- const void* src)
- {
- const int32_t* data = reinterpret_cast<const int32_t*>(src);
- #ifdef PTX_AVAILABLE
- asm volatile("st.global.cg.u32 [%0], %1;\n" : : "l"(dst), "r"(*data));
- #else
- int32_t* dst_cast = reinterpret_cast<int32_t*>(dst);
- dst_cast[0] = data[0];
- #endif
- }
- template <>
- __device__ __forceinline__ void store_global<4, StorePolicy::CacheStreaming>(void* dst,
- const void* src)
- {
- const int32_t* data = reinterpret_cast<const int32_t*>(src);
- #ifdef PTX_AVAILABLE
- asm volatile("st.global.cs.u32 [%0], %1;\n" : : "l"(dst), "r"(*data));
- #else
- int32_t* dst_cast = reinterpret_cast<int32_t*>(dst);
- dst_cast[0] = data[0];
- #endif
- }
- /////////// Store Shared ///////////
- template <>
- __device__ __forceinline__ void store_shared<16>(void* dst, const void* src)
- {
- const uint4* data = reinterpret_cast<const uint4*>(src);
- #ifdef PTX_AVAILABLE
- unsigned dst_int = internal::convert_to_shared(dst);
- asm volatile("st.shared.v4.u32 [%0], {%1, %2, %3, %4};\n"
- :
- : "r"(dst_int), "r"(data[0].x), "r"(data[0].y), "r"(data[0].z), "r"(data[0].w));
- #else
- uint4* dst_cast = reinterpret_cast<uint4*>(dst);
- dst_cast[0] = data[0];
- #endif
- }
- template <>
- __device__ __forceinline__ void store_shared<8>(void* dst, const void* src)
- {
- const uint2* data = reinterpret_cast<const uint2*>(src);
- #ifdef PTX_AVAILABLE
- unsigned dst_int = internal::convert_to_shared(dst);
- asm volatile("st.shared.v2.u32 [%0], {%1, %2};\n"
- :
- : "r"(dst_int), "r"(data[0].x), "r"(data[0].y));
- #else
- uint2* dst_cast = reinterpret_cast<uint2*>(dst);
- dst_cast[0] = data[0];
- #endif
- }
- template <>
- __device__ __forceinline__ void store_shared<4>(void* dst, const void* src)
- {
- const int32_t* data = reinterpret_cast<const int32_t*>(src);
- #ifdef PTX_AVAILABLE
- unsigned dst_int = internal::convert_to_shared(dst);
- asm volatile("st.shared.u32 [%0], %1;\n" : : "r"(dst_int), "r"(*data));
- #else
- int32_t* dst_cast = reinterpret_cast<int32_t*>(dst);
- dst_cast[0] = data[0];
- #endif
- }
- /////////// Asynchronous Memory Copy ///////////
- #ifdef ASYNC_COPY_AVAILABLE
- template <int AccessSize>
- __device__ __forceinline__ void memcpy_async(void* shr, const void* gbl)
- {
- static_assert((AccessSize == 4 || AccessSize == 8 || AccessSize == 16));
- unsigned shr_int = internal::convert_to_shared(shr);
- asm volatile("cp.async.ca.shared.global [%0], [%1], %2;\n"
- :
- : "r"(shr_int), "l"(gbl), "n"(AccessSize));
- }
- template <int AccessSize>
- __device__ __forceinline__ void memcpy_async_nop(void* shr, const void* gbl, bool predicate)
- {
- static_assert((AccessSize == 4 || AccessSize == 8 || AccessSize == 16));
- unsigned shr_int = internal::convert_to_shared(shr);
- asm volatile(
- "{\n"
- " .reg .pred p;\n"
- " setp.ne.b32 p, %0, 0;\n"
- " @p cp.async.ca.shared.global [%1], [%2], %3;\n"
- "}\n"
- :
- : "r"((int)predicate), "r"(shr_int), "l"(gbl), "n"(AccessSize));
- }
- template <int AccessSize>
- __device__ __forceinline__ void memcpy_async_zero(void* shr, const void* gbl, bool predicate)
- {
- static_assert((AccessSize == 4 || AccessSize == 8 || AccessSize == 16));
- unsigned shr_int = internal::convert_to_shared(shr);
- int bytes_to_copy = (predicate ? AccessSize : 0);
- asm volatile("cp.async.ca.shared.global [%0], [%1], %2, %3;\n"
- :
- : "r"(shr_int), "l"(gbl), "n"(AccessSize), "r"(bytes_to_copy));
- }
- template <int AccessSize>
- __device__ __forceinline__ void memcpy_async_zero_nop(void* shr,
- const void* gbl,
- bool zero_predicate,
- bool nop_predicate)
- {
- static_assert((AccessSize == 4 || AccessSize == 8 || AccessSize == 16));
- unsigned shr_int = internal::convert_to_shared(shr);
- int bytes_to_copy = (zero_predicate ? AccessSize : 0);
- asm volatile(
- "{\n"
- " .reg .pred p;\n"
- " setp.ne.b32 p, %0, 0;\n"
- " @p cp.async.ca.shared.global [%1], [%2], %3, %4;\n"
- "}\n"
- :
- : "r"((int)nop_predicate), "r"(shr_int), "l"(gbl), "n"(AccessSize), "r"(bytes_to_copy));
- }
- // Cache global variants. Separate interface to require deliberate use of them.
- __device__ __forceinline__ void memcpy_async_cg(void* shr, const void* gbl)
- {
- unsigned shr_int = internal::convert_to_shared(shr);
- asm volatile("cp.async.cg.shared.global [%0], [%1], 16;\n" : : "r"(shr_int), "l"(gbl));
- }
- __device__ __forceinline__ void memcpy_async_nop_cg(void* shr, const void* gbl, bool predicate)
- {
- unsigned shr_int = internal::convert_to_shared(shr);
- asm volatile(
- "{\n"
- " .reg .pred p;\n"
- " setp.ne.b32 p, %0, 0;\n"
- " @p cp.async.cg.shared.global [%1], [%2], 16;\n"
- "}\n"
- :
- : "r"((int)predicate), "r"(shr_int), "l"(gbl));
- }
- __device__ __forceinline__ void memcpy_async_zero_cg(void* shr, const void* gbl, bool predicate)
- {
- unsigned shr_int = internal::convert_to_shared(shr);
- int bytes_to_copy = (predicate ? 16 : 0);
- asm volatile("cp.async.cg.shared.global [%0], [%1], 16, %2;\n"
- :
- : "r"(shr_int), "l"(gbl), "r"(bytes_to_copy));
- }
- __device__ __forceinline__ void memcpy_async_zero_nop_cg(void* shr,
- const void* gbl,
- bool zero_predicate,
- bool nop_predicate)
- {
- unsigned shr_int = internal::convert_to_shared(shr);
- int bytes_to_copy = (zero_predicate ? 16 : 0);
- asm volatile(
- "{\n"
- " .reg .pred p;\n"
- " setp.ne.b32 p, %0, 0;\n"
- " @p cp.async.cg.shared.global [%1], [%2], 16, %3;\n"
- "}\n"
- :
- : "r"((int)nop_predicate), "r"(shr_int), "l"(gbl), "r"(bytes_to_copy));
- }
- __device__ __forceinline__ void memcpy_async_fence() { asm volatile("cp.async.commit_group;\n"); }
- template <int stages>
- __device__ __forceinline__ void memcpy_async_wait()
- {
- static_assert(stages <= 8);
- asm volatile("cp.async.wait_group %0;\n" : : "n"(stages));
- }
- // TODO: The tail complete should be a known compile time artifact, should try and induce this
- // without all of the branches from the call-site. This is a hacky solution.
- template <>
- __device__ __forceinline__ void tail_complete_wait<1>(int remaining_stages)
- {
- if (remaining_stages == 0) memcpy_async_wait<0>();
- }
- template <>
- __device__ __forceinline__ void tail_complete_wait<2>(int remaining_stages)
- {
- if (remaining_stages == 1)
- memcpy_async_wait<1>();
- else if (remaining_stages == 0)
- memcpy_async_wait<0>();
- }
- template <>
- __device__ __forceinline__ void tail_complete_wait<3>(int remaining_stages)
- {
- if (remaining_stages == 2)
- memcpy_async_wait<2>();
- else if (remaining_stages == 1)
- memcpy_async_wait<1>();
- else if (remaining_stages == 0)
- memcpy_async_wait<0>();
- }
- template <>
- __device__ __forceinline__ void tail_complete_wait<4>(int remaining_stages)
- {
- if (remaining_stages == 3)
- memcpy_async_wait<3>();
- else if (remaining_stages == 2)
- memcpy_async_wait<2>();
- else if (remaining_stages == 1)
- memcpy_async_wait<1>();
- else if (remaining_stages == 0)
- memcpy_async_wait<0>();
- }
- template <>
- __device__ __forceinline__ void tail_complete_wait<5>(int remaining_stages)
- {
- if (remaining_stages == 4)
- memcpy_async_wait<4>();
- else if (remaining_stages == 3)
- memcpy_async_wait<3>();
- else if (remaining_stages == 2)
- memcpy_async_wait<2>();
- else if (remaining_stages == 1)
- memcpy_async_wait<1>();
- else if (remaining_stages == 0)
- memcpy_async_wait<0>();
- }
- template <>
- __device__ __forceinline__ void tail_complete_wait<6>(int remaining_stages)
- {
- if (remaining_stages == 5)
- memcpy_async_wait<5>();
- else if (remaining_stages == 4)
- memcpy_async_wait<4>();
- else if (remaining_stages == 3)
- memcpy_async_wait<3>();
- else if (remaining_stages == 2)
- memcpy_async_wait<2>();
- else if (remaining_stages == 1)
- memcpy_async_wait<1>();
- else if (remaining_stages == 0)
- memcpy_async_wait<0>();
- }
- #endif
- } // namespace mem_access
|