1// NOTE: this test requires gpu-sm80 2// 3// DEFINE: %{compile} = mlir-opt %s \ 4// DEFINE: --sparsifier="enable-gpu-libgen gpu-triple=nvptx64-nvidia-cuda gpu-chip=sm_80 gpu-features=+ptx71 gpu-format=%gpu_compilation_format 5// DEFINE: %{run} = mlir-runner \ 6// DEFINE: --shared-libs=%mlir_cuda_runtime \ 7// DEFINE: --shared-libs=%mlir_c_runner_utils \ 8// DEFINE: --e main --entry-point-result=void \ 9// DEFINE: | FileCheck %s 10// 11// with RT lib (SoA COO): 12// 13// RUN: %{compile} enable-runtime-library=true" | %{run} 14// 15// without RT lib (AoS COO): note, may fall back to CPU 16// 17// RUN: %{compile} enable-runtime-library=false" | %{run} 18 19#SortedCOO = #sparse_tensor.encoding<{ 20 map = (d0, d1) -> (d0 : compressed(nonunique), d1 : singleton) 21}> 22 23#CSR = #sparse_tensor.encoding<{ 24 map = (d0, d1) -> (d0 : dense, d1 : compressed), 25 posWidth = 32, 26 crdWidth = 32 27}> 28 29#CSC = #sparse_tensor.encoding<{ 30 map = (d0, d1) -> (d1 : dense, d0 : compressed), 31 posWidth = 64, 32 crdWidth = 64 33}> 34 35module { 36 llvm.func @mgpuCreateSparseEnv() 37 llvm.func @mgpuDestroySparseEnv() 38 39 // Compute matrix vector y = Ax on COO with default index coordinates. 40 func.func @matvecCOO(%A: tensor<?x?xf64, #SortedCOO>, %x: tensor<?xf64>, %y_in: tensor<?xf64>) -> tensor<?xf64> { 41 %y_out = linalg.matvec 42 ins(%A, %x: tensor<?x?xf64, #SortedCOO>, tensor<?xf64>) 43 outs(%y_in: tensor<?xf64>) -> tensor<?xf64> 44 return %y_out : tensor<?xf64> 45 } 46 47 // Compute matrix vector y = Ax on CSR with 32-bit positions and coordinates. 48 func.func @matvecCSR(%A: tensor<?x?xf64, #CSR>, %x: tensor<?xf64>, %y_in: tensor<?xf64>) -> tensor<?xf64> { 49 %y_out = linalg.matvec 50 ins(%A, %x: tensor<?x?xf64, #CSR>, tensor<?xf64>) 51 outs(%y_in: tensor<?xf64>) -> tensor<?xf64> 52 return %y_out : tensor<?xf64> 53 } 54 55 // Compute matrix vector y = Ax on CSC with 64-bit positions and coordinates. 56 func.func @matvecCSC(%A: tensor<?x?xf64, #CSC>, %x: tensor<?xf64>, %y_in: tensor<?xf64>) -> tensor<?xf64> { 57 %y_out = linalg.matvec 58 ins(%A, %x: tensor<?x?xf64, #CSC>, tensor<?xf64>) 59 outs(%y_in: tensor<?xf64>) -> tensor<?xf64> 60 return %y_out : tensor<?xf64> 61 } 62 63 func.func @main() { 64 llvm.call @mgpuCreateSparseEnv() : () -> () 65 %f0 = arith.constant 0.0 : f64 66 %f1 = arith.constant 1.0 : f64 67 %c0 = arith.constant 0 : index 68 %c1 = arith.constant 1 : index 69 70 // Stress test with a dense matrix DA. 71 %DA = tensor.generate { 72 ^bb0(%i: index, %j: index): 73 %k = arith.addi %i, %j : index 74 %l = arith.index_cast %k : index to i64 75 %f = arith.uitofp %l : i64 to f64 76 tensor.yield %f : f64 77 } : tensor<64x64xf64> 78 79 // Convert to a "sparse" m x n matrix A. 80 %Acoo = sparse_tensor.convert %DA : tensor<64x64xf64> to tensor<?x?xf64, #SortedCOO> 81 %Acsr = sparse_tensor.convert %DA : tensor<64x64xf64> to tensor<?x?xf64, #CSR> 82 %Acsc = sparse_tensor.convert %DA : tensor<64x64xf64> to tensor<?x?xf64, #CSC> 83 84 // Initialize dense vector with n elements: 85 // (1, 2, 3, 4, ..., n) 86 %d1 = tensor.dim %Acoo, %c1 : tensor<?x?xf64, #SortedCOO> 87 %x = tensor.generate %d1 { 88 ^bb0(%i : index): 89 %k = arith.addi %i, %c1 : index 90 %j = arith.index_cast %k : index to i64 91 %f = arith.uitofp %j : i64 to f64 92 tensor.yield %f : f64 93 } : tensor<?xf64> 94 95 // Initialize dense vectors to m zeros and m ones. 96 %d0 = tensor.dim %Acoo, %c0 : tensor<?x?xf64, #SortedCOO> 97 %y0 = tensor.generate %d0 { 98 ^bb0(%i : index): 99 tensor.yield %f0 : f64 100 } : tensor<?xf64> 101 %y1 = tensor.generate %d0 { 102 ^bb0(%i : index): 103 tensor.yield %f1 : f64 104 } : tensor<?xf64> 105 106 // Call the kernels. 107 %0 = call @matvecCOO(%Acoo, %x, %y0) : (tensor<?x?xf64, #SortedCOO>, 108 tensor<?xf64>, 109 tensor<?xf64>) -> tensor<?xf64> 110 %1 = call @matvecCSR(%Acsr, %x, %y0) : (tensor<?x?xf64, #CSR>, 111 tensor<?xf64>, 112 tensor<?xf64>) -> tensor<?xf64> 113 %2 = call @matvecCSC(%Acsc, %x, %y0) : (tensor<?x?xf64, #CSC>, 114 tensor<?xf64>, 115 tensor<?xf64>) -> tensor<?xf64> 116 %3 = call @matvecCOO(%Acoo, %x, %y1) : (tensor<?x?xf64, #SortedCOO>, 117 tensor<?xf64>, 118 tensor<?xf64>) -> tensor<?xf64> 119 %4 = call @matvecCSR(%Acsr, %x, %y1) : (tensor<?x?xf64, #CSR>, 120 tensor<?xf64>, 121 tensor<?xf64>) -> tensor<?xf64> 122 %5 = call @matvecCSC(%Acsc, %x, %y1) : (tensor<?x?xf64, #CSC>, 123 tensor<?xf64>, 124 tensor<?xf64>) -> tensor<?xf64> 125 126 // 127 // Sanity check on the results. 128 // 129 // CHECK-COUNT-3: ( 87360, 89440, 91520, 93600, 95680, 97760, 99840, 101920, 104000, 106080, 108160, 110240, 112320, 114400, 116480, 118560, 120640, 122720, 124800, 126880, 128960, 131040, 133120, 135200, 137280, 139360, 141440, 143520, 145600, 147680, 149760, 151840, 153920, 156000, 158080, 160160, 162240, 164320, 166400, 168480, 170560, 172640, 174720, 176800, 178880, 180960, 183040, 185120, 187200, 189280, 191360, 193440, 195520, 197600, 199680, 201760, 203840, 205920, 208000, 210080, 212160, 214240, 216320, 218400 ) 130 // 131 // CHECK-COUNT-3: ( 87361, 89441, 91521, 93601, 95681, 97761, 99841, 101921, 104001, 106081, 108161, 110241, 112321, 114401, 116481, 118561, 120641, 122721, 124801, 126881, 128961, 131041, 133121, 135201, 137281, 139361, 141441, 143521, 145601, 147681, 149761, 151841, 153921, 156001, 158081, 160161, 162241, 164321, 166401, 168481, 170561, 172641, 174721, 176801, 178881, 180961, 183041, 185121, 187201, 189281, 191361, 193441, 195521, 197601, 199681, 201761, 203841, 205921, 208001, 210081, 212161, 214241, 216321, 218401 ) 132 // 133 %pb0 = vector.transfer_read %0[%c0], %f0 : tensor<?xf64>, vector<64xf64> 134 vector.print %pb0 : vector<64xf64> 135 %pb1 = vector.transfer_read %1[%c0], %f0 : tensor<?xf64>, vector<64xf64> 136 vector.print %pb1 : vector<64xf64> 137 %pb2 = vector.transfer_read %2[%c0], %f0 : tensor<?xf64>, vector<64xf64> 138 vector.print %pb2 : vector<64xf64> 139 %pb3 = vector.transfer_read %3[%c0], %f0 : tensor<?xf64>, vector<64xf64> 140 vector.print %pb3 : vector<64xf64> 141 %pb4 = vector.transfer_read %4[%c0], %f0 : tensor<?xf64>, vector<64xf64> 142 vector.print %pb4 : vector<64xf64> 143 %pb5 = vector.transfer_read %5[%c0], %f0 : tensor<?xf64>, vector<64xf64> 144 vector.print %pb5 : vector<64xf64> 145 146 // Release the resources. 147 bufferization.dealloc_tensor %Acoo : tensor<?x?xf64, #SortedCOO> 148 bufferization.dealloc_tensor %Acsr : tensor<?x?xf64, #CSR> 149 bufferization.dealloc_tensor %Acsc : tensor<?x?xf64, #CSC> 150 151 llvm.call @mgpuDestroySparseEnv() : () -> () 152 return 153 } 154} 155