okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
bd34d0d87
main
1// Numerical parity for the CUDA intrinsics supplied by the HIP compatibility layer. 2#include <hip/hip_runtime.h> 3#include "strata/hip_compat/intrinsics.hpp" 4 5#include <cstdint> 6#include <cstdio> 7#include <limits> 8 9#define CHECK (call ) \ 10 do { \ 11 const hipError_t error = (call); \ 12 if (error != hipSuccess) { \ 13 std::fprintf(stderr, "%s: %s\n", #call, hipGetErrorString(error)); \ 14 return 2; \ 15 } \ 16 } while (0) 17 18namespace { 19 20constexpr int kLanes = 32 ; 21constexpr int kIntegerResults = 10 ; 22 23__global__ void intrinsic_probe (int * integer_results ,float * float_shuffle ,double * double_shuffle , 24unsigned * ballot ,const uint32_t * dot_inputs ) { 25const int lane = static_cast < int > (threadIdx .x ); 26constexpr unsigned mask = 0xffffffffu ; 27const uint32_t a = 0x80ff017fu ; 28const uint32_t b = 0x0203fe81u ; 29 30integer_results [lane * kIntegerResults + 0 ]= __dp4a (static_cast < int > (a ),static_cast < int > (b ),7 ); 31integer_results [lane * kIntegerResults + 1 ]= __dp4a (0x7f7f7f7f ,0x7f7f7f7f ,0x7fffffff ); 32integer_results [lane * kIntegerResults + 2 ]= __vsub4 (0x00ff0102 ,0x01010103 ); 33integer_results [lane * kIntegerResults + 3 ]= __vsub4 (0x00000000 ,0x01020304 ); 34integer_results [lane * kIntegerResults + 4 ]= __vsubss4 (0x807f0080 ,0x017f0180 ); 35integer_results [lane * kIntegerResults + 5 ]= __vcmpne4 (0x00ff0102 ,0x01010103 ); 36integer_results [lane * kIntegerResults + 6 ]= 37__dp4a (static_cast < int > (dot_inputs [lane * 2 ]),static_cast < int > (dot_inputs [lane * 2 + 1 ]), 380x7fffffff - lane ); 39 40__shared__ int exchanged [kLanes ]; 41int sum = 0 ; 42for (int iteration = 0 ;iteration < 64 ;++ iteration ) { 43exchanged [lane ]= static_cast < int > (dot_inputs [lane * 2 ]& 255u )+ iteration ; 44__syncwarp (); 45sum += exchanged [lane ^1 ]; 46__syncwarp (); 47 } 48integer_results [lane * kIntegerResults + 7 ]= sum ; 49const int input_a = static_cast < int > (dot_inputs [lane * 2 ]); 50const int input_b = static_cast < int > (dot_inputs [lane * 2 + 1 ]); 51integer_results [lane * kIntegerResults + 8 ]= __dp4a (input_a ,input_b ,input_a ); 52integer_results [lane * kIntegerResults + 9 ]= __dp4a (input_a ,input_b ,input_b ); 53 54float_shuffle [lane * 4 + 0 ]= __shfl_xor_sync (mask ,static_cast < float > (lane ),1 ); 55float_shuffle [lane * 4 + 1 ]= static_cast < float > (__shfl_down_sync (mask ,lane ,4 ,8 )); 56float_shuffle [lane * 4 + 2 ]= static_cast < float > (__shfl_up_sync (mask ,lane ,1 )); 57float_shuffle [lane * 4 + 3 ]= static_cast < float > (__shfl_sync (mask ,lane ,0 )); 58double_shuffle [lane ]= __shfl_xor_sync (mask ,static_cast < double > (lane )+ 0.25 ,1 ); 59 60const unsigned hit = __ballot_sync (mask , (lane & 1 )== 0 ); 61if (lane == 0 ) { 62* ballot = hit ; 63__nanosleep (100 ); 64 } 65} 66 67// The byte-lane intrinsics over many operands: __byte_perm with selector bits it ignores (bit 3 of each nibble and 68// the upper half), since the i-quant table lookups pass data-dependent selectors; the packed subtracts and compare 69// on every sign and overflow combination (the compare's second operand keeps two lanes of the first). 70constexpr int kPermCases = 1024 ; 71constexpr int kPermResults = 4 ; 72__global__ void byte_perm_probe (const uint32_t * inputs ,uint32_t * results ) { 73const int i = static_cast < int > (blockIdx .x * blockDim .x + threadIdx .x ); 74if (i >=kPermCases )return ; 75const uint32_t x = inputs [3 * i ],y = inputs [3 * i + 1 ],s = inputs [3 * i + 2 ]; 76results [kPermResults * i + 0 ]= __byte_perm (x ,y ,s ); 77results [kPermResults * i + 1 ]= static_cast < uint32_t > (__vsub4 (static_cast < int > (x ),static_cast < int > (y ))); 78results [kPermResults * i + 2 ]= static_cast < uint32_t > (__vsubss4 (static_cast < int > (x ),static_cast < int > (y ))); 79results [kPermResults * i + 3 ]= 80static_cast < uint32_t > (__vcmpne4 (static_cast < int > (x ),static_cast < int > (x ^ (s & 0xff00ff00u )))); 81} 82 83uint32_t byte_perm_reference (uint32_t x ,uint32_t y ,uint32_t s ) { 84const uint64_t pair = (static_cast < uint64_t > (y ) <<32 ) |x ; 85uint32_t out = 0 ; 86for (int i = 0 ;i < 4 ;++ i )out |=static_cast < uint32_t > ((pair >> (8 * ((s >> (4 * i ))& 7u )))& 0xffu ) << (8 * i ); 87return out ; 88} 89 90int signed_byte (uint32_t word ,int lane ) { 91const unsigned value = (word >> (lane * 8 ))& 0xffu ; 92return value < 0x80u ?static_cast < int > (value ) :static_cast < int > (value )- 0x100 ; 93} 94 95int dp4a_reference (uint32_t a ,uint32_t b ,int c ) { 96int64_t sum = c ; 97for (int lane = 0 ;lane < 4 ;++ lane )sum += signed_byte (a ,lane )* signed_byte (b ,lane ); 98return static_cast < int32_t > (static_cast < uint32_t > (sum )); 99} 100 101uint32_t byte_sub_reference (uint32_t a ,uint32_t b ) { 102uint32_t out = 0 ; 103for (int lane = 0 ;lane < 4 ;++ lane ) 104out |= ((((a >> (lane * 8 ))& 0xffu )- ((b >> (lane * 8 ))& 0xffu ))& 0xffu ) << (lane * 8 ); 105return out ; 106} 107 108uint32_t signed_saturating_sub_reference (uint32_t a ,uint32_t b ) { 109uint32_t out = 0 ; 110for (int lane = 0 ;lane < 4 ;++ lane ) { 111int value = signed_byte (a ,lane )- signed_byte (b ,lane ); 112if (value < -128 )value = -128 ; 113if (value > 127 )value = 127 ; 114out |= (static_cast < uint32_t > (value )& 0xffu ) << (lane * 8 ); 115 } 116return out ; 117} 118 119uint32_t not_equal_reference (uint32_t a ,uint32_t b ) { 120uint32_t out = 0 ; 121for (int lane = 0 ;lane < 4 ;++ lane ) 122if (((a >> (lane * 8 ))& 0xffu )!= ((b >> (lane * 8 ))& 0xffu ))out |=0xffu << (lane * 8 ); 123return out ; 124} 125 126}// namespace 127 128int main () { 129int device = 0 ; 130CHECK (hipGetDevice (& device )); 131hipDeviceProp_t properties {}; 132CHECK (hipGetDeviceProperties (& properties ,device )); 133if (properties .warpSize != kLanes ) { 134 std::fprintf (stderr ,"expected wave32, got wave%d\n" ,properties .warpSize ); 135return 1 ; 136 } 137 138int * device_integers = nullptr ; 139float * device_float_shuffle = nullptr ; 140double * device_double_shuffle = nullptr ; 141unsigned * device_ballot = nullptr ; 142uint32_t * device_dot_inputs = nullptr ; 143CHECK (hipMalloc (reinterpret_cast < void **> (& device_integers ),kLanes * kIntegerResults * sizeof (int ))); 144CHECK (hipMalloc (reinterpret_cast < void **> (& device_float_shuffle ),kLanes * 4 * sizeof (float ))); 145CHECK (hipMalloc (reinterpret_cast < void **> (& device_double_shuffle ),kLanes * sizeof (double ))); 146CHECK (hipMalloc (reinterpret_cast < void **> (& device_ballot ),sizeof (unsigned ))); 147CHECK (hipMalloc (reinterpret_cast < void **> (& device_dot_inputs ),kLanes * 2 * sizeof (uint32_t ))); 148 149uint32_t dot_inputs [kLanes * 2 ]{}; 150for (int lane = 0 ;lane < kLanes ;++ lane ) { 151dot_inputs [lane * 2 ]= 0x80ff017fu ^ (0x01010101u * static_cast < uint32_t > (lane )); 152dot_inputs [lane * 2 + 1 ]= 0x0203fe81u + (0x11111111u * static_cast < uint32_t > (lane )); 153 } 154CHECK (hipMemcpy (device_dot_inputs ,dot_inputs ,sizeof (dot_inputs ),hipMemcpyHostToDevice )); 155 156hipLaunchKernelGGL (intrinsic_probe ,dim3 (1 ),dim3 (kLanes ),0 ,0 , 157device_integers ,device_float_shuffle ,device_double_shuffle ,device_ballot ,device_dot_inputs ); 158CHECK (hipDeviceSynchronize ()); 159 160int integers [kLanes * kIntegerResults ]{}; 161float float_shuffle [kLanes * 4 ]{}; 162double double_shuffle [kLanes ]{}; 163unsigned ballot = 0 ; 164CHECK (hipMemcpy (integers ,device_integers ,sizeof (integers ),hipMemcpyDeviceToHost )); 165CHECK (hipMemcpy (float_shuffle ,device_float_shuffle ,sizeof (float_shuffle ),hipMemcpyDeviceToHost )); 166CHECK (hipMemcpy (double_shuffle ,device_double_shuffle ,sizeof (double_shuffle ),hipMemcpyDeviceToHost )); 167CHECK (hipMemcpy (& ballot ,device_ballot ,sizeof (ballot ),hipMemcpyDeviceToHost )); 168 169constexpr uint32_t a = 0x80ff017fu ,b = 0x0203fe81u ; 170const uint32_t expected_integer [6 ]= { 171static_cast < uint32_t > (dp4a_reference (a ,b ,7 )), 172static_cast < uint32_t > (dp4a_reference (0x7f7f7f7fu ,0x7f7f7f7fu , std::numeric_limits < int > ::max ())), 173byte_sub_reference (0x00ff0102u ,0x01010103u ), 174byte_sub_reference (0x00000000u ,0x01020304u ), 175signed_saturating_sub_reference (0x807f0080u ,0x017f0180u ), 176not_equal_reference (0x00ff0102u ,0x01010103u ), 177 }; 178bool ok = ballot == 0x55555555u ; 179for (int lane = 0 ;lane < kLanes ;++ lane ) { 180for (int i = 0 ;i < 6 ;++ i ) 181ok = ok && static_cast < uint32_t > (integers [lane * kIntegerResults + i ])== expected_integer [i ]; 182ok = ok && static_cast < uint32_t > (integers [lane * kIntegerResults + 6 ])== 183static_cast < uint32_t > (dp4a_reference (dot_inputs [lane * 2 ],dot_inputs [lane * 2 + 1 ], 1840x7fffffff - lane )); 185ok = ok && integers [lane * kIntegerResults + 7 ]== 18664 * static_cast < int > (dot_inputs [(lane ^1 )* 2 ]& 255u )+ 2016 ; 187for (int alias = 0 ;alias < 2 ;++ alias ) 188ok = ok && static_cast < uint32_t > (integers [lane * kIntegerResults + 8 + alias ])== 189static_cast < uint32_t > (dp4a_reference (dot_inputs [lane * 2 ],dot_inputs [lane * 2 + 1 ], 190static_cast < int > (dot_inputs [lane * 2 + alias ]))); 191ok = ok && float_shuffle [lane * 4 + 0 ]== static_cast < float > (lane ^1 ); 192const int down = lane %8 < 4 ?lane + 4 :lane ; 193ok = ok && float_shuffle [lane * 4 + 1 ]== static_cast < float > (down ); 194ok = ok && float_shuffle [lane * 4 + 2 ]== static_cast < float > (lane == 0 ?0 :lane - 1 ); 195ok = ok && float_shuffle [lane * 4 + 3 ]== 0.0f ; 196ok = ok && double_shuffle [lane ]== static_cast < double > (lane ^1 )+ 0.25 ; 197 } 198 199uint32_t perm_inputs [kPermCases * 3 ]{}; 200uint32_t state = 0x9e3779b9u ; 201for (uint32_t & v :perm_inputs ) { 202state ^=state <<13 ;state ^=state >>17 ;state ^=state <<5 ; 203v = state ; 204 } 205uint32_t * device_perm_inputs = nullptr ; 206uint32_t * device_perm_results = nullptr ; 207CHECK (hipMalloc (reinterpret_cast < void **> (& device_perm_inputs ),sizeof (perm_inputs ))); 208CHECK (hipMalloc (reinterpret_cast < void **> (& device_perm_results ),kPermCases * kPermResults * sizeof (uint32_t ))); 209CHECK (hipMemcpy (device_perm_inputs ,perm_inputs ,sizeof (perm_inputs ),hipMemcpyHostToDevice )); 210hipLaunchKernelGGL (byte_perm_probe ,dim3 (kPermCases /256 ),dim3 (256 ),0 ,0 ,device_perm_inputs ,device_perm_results ); 211CHECK (hipDeviceSynchronize ()); 212uint32_t perm_results [kPermCases * kPermResults ]{}; 213CHECK (hipMemcpy (perm_results ,device_perm_results ,sizeof (perm_results ),hipMemcpyDeviceToHost )); 214for (int i = 0 ;i < kPermCases ;++ i ) { 215const uint32_t x = perm_inputs [3 * i ],y = perm_inputs [3 * i + 1 ],s = perm_inputs [3 * i + 2 ]; 216const uint32_t * r = perm_results + kPermResults * i ; 217ok = ok && r [0 ]== byte_perm_reference (x ,y ,s ); 218ok = ok && r [1 ]== byte_sub_reference (x ,y ); 219ok = ok && r [2 ]== signed_saturating_sub_reference (x ,y ); 220ok = ok && r [3 ]== not_equal_reference (x ,x ^ (s & 0xff00ff00u )); 221 } 222CHECK (hipFree (device_perm_results )); 223CHECK (hipFree (device_perm_inputs )); 224 225CHECK (hipFree (device_ballot )); 226CHECK (hipFree (device_dot_inputs )); 227CHECK (hipFree (device_double_shuffle )); 228CHECK (hipFree (device_float_shuffle )); 229CHECK (hipFree (device_integers )); 230if (!ok ) { 231 std::fprintf (stderr ,"HIP intrinsic parity failed (ballot 0x%08x)\n" ,ballot ); 232return 1 ; 233 } 234 std::puts ("HIP intrinsics parity OK: dynamic signed dot4/overflow, byte permute, packed integer boundaries, wave32 shuffles, shared-memory exchange, ballot and sleep" ); 235return 0 ; 236}