okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
12d835dd6
main
1// The elastic K/V's VMM ranges (vmm.hpp): chunks map readable and writable, a chunk unmapped from one range and mapped 2// into another keeps its bytes (what the expert cache hands the K/V), and a range releases what it holds. 3#include "strata/core/vmm.hpp" 4 5#include <cuda_runtime.h> 6 7#include <chrono> 8#include <cstdio> 9#include <thread> 10#include <vector> 11 12using strata::core::VmmChunk ; 13using strata::core::VmmRange ; 14 15static int fails = 0 ; 16#define CHECK (c ) do { if (!(c)) { std::fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, #c); ++fails; } } while (0) 17 18int main () { 19if (cudaFree (nullptr )!= cudaSuccess || !strata::core::vmm_available ()) { 20 std::printf ("vmm_test: no CUDA device with virtual memory - skipped\n" ); 21return 0 ; 22 } 23const uint64_t G = strata::core::vmm_granularity (); 24CHECK (G >=4096 ); 25VmmRange a ,b ; 26CHECK (a .reserve (8 * G + 1 ));// rounds up 27CHECK (a .chunks ()== 9 ); 28CHECK (b .reserve (4 * G )); 29CHECK (a .mapped_count ()== 0 ); 30CHECK (a .map_range (0 ,6 , [] {return (VmmChunk )0 ; })); 31CHECK (a .mapped_count ()== 6 && a .mapped (5 )&& !a .mapped (6 )); 32 std::vector < uint8_t > h (6 * G ),back (6 * G ); 33for (size_t i = 0 ;i < h .size ();++ i )h [i ]= (uint8_t ) (i * 2654435761u >>13 ); 34CHECK (cudaMemcpy (a .base (),h .data (),h .size (),cudaMemcpyHostToDevice )== cudaSuccess ); 35// chunks 4 and 5 move to b's chunks 1 and 0. The host-to-device copy above may still be in flight (a pageable 36// cudaMemcpy returns once the data is staged), and a chunk unmapped under a copy faults it (an illegal memory access 37// that sticks to every later call), so the device is drained first, as generate.cpp does before it unmaps 38CHECK (cudaDeviceSynchronize ()== cudaSuccess ); 39const VmmChunk c4 = a .unmap (4 ),c5 = a .unmap (5 ); 40CHECK (c4 != 0 && c5 != 0 && !a .mapped (4 )&& a .unmap (4 )== 0 ); 41 std::vector < VmmChunk > give = {c4 ,c5 };// map_range takes them from the back: chunk 0 <- c5, chunk 1 <- c4 42CHECK (b .map_range (0 ,2 , [& ] {VmmChunk x = give .back ();give .pop_back ();return x ; })); 43CHECK (cudaMemcpy (back .data (),b .base (),G ,cudaMemcpyDeviceToHost )== cudaSuccess ); 44bool same = true; 45for (uint64_t i = 0 ;i < G ;++ i )same = same && back [i ]== h [5 * G + i ]; 46CHECK (same ); 47CHECK (cudaMemcpy (back .data (),b .base ()+ G ,G ,cudaMemcpyDeviceToHost )== cudaSuccess ); 48same = true; 49for (uint64_t i = 0 ;i < G ;++ i )same = same && back [i ]== h [4 * G + i ]; 50CHECK (same ); 51// a's first chunks are untouched, and map_range fills a hole with new memory, skipping mapped chunks 52CHECK (cudaMemcpy (back .data (),a .base (),4 * G ,cudaMemcpyDeviceToHost )== cudaSuccess ); 53same = true; 54for (uint64_t i = 0 ;i < 4 * G ;++ i )same = same && back [i ]== h [i ]; 55CHECK (same ); 56CHECK (a .map_range (3 ,7 , [] {return (VmmChunk )0 ; })&& a .mapped_count ()== 7 ); 57CHECK (cudaMemset (a .base ()+ 4 * G ,7 ,3 * G )== cudaSuccess && cudaDeviceSynchronize ()== cudaSuccess ); 58size_t f0 = 0 ,f1 = 0 ,t = 0 ; 59cudaMemGetInfo (& f0 ,& t ); 60a .release (); 61b .release (); 62for (int i = 0 ;i < 20 ;++ i ) {// the free-memory counter may lag a release by a moment on Windows (WDDM) 63cudaMemGetInfo (& f1 ,& t ); 64if (f1 >=f0 + 8 * G )break ; 65 std::this_thread::sleep_for (std::chrono::milliseconds (100 )); 66 } 67CHECK (f1 >=f0 + 8 * G );// 7 + 2 chunks went back (the driver may round) 68CHECK (cudaGetLastError ()== cudaSuccess ); 69 std::printf ("vmm_test: %s\n" ,fails ?"FAILED" :"ok" ); 70return fails ?1 :0 ; 71}