Sparse level 3 functions#

This module contains all sparse level 3 routines.

The sparse level 3 routines describe operations between a matrix in sparse format and multiple vectors in dense format that can also be seen as a dense matrix.

hipsparseXbsrmm()#

Warning

doxygenfunction: Cannot find file: /home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-hipsparse/checkouts/7.13.0-preview/projects/hipsparse/docs/doxygen/xml/index.xml

Warning

doxygenfunction: Cannot find file: /home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-hipsparse/checkouts/7.13.0-preview/projects/hipsparse/docs/doxygen/xml/index.xml

Warning

doxygenfunction: Cannot find file: /home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-hipsparse/checkouts/7.13.0-preview/projects/hipsparse/docs/doxygen/xml/index.xml

Warning

doxygenfunction: Cannot find file: /home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-hipsparse/checkouts/7.13.0-preview/projects/hipsparse/docs/doxygen/xml/index.xml

  1int main(int argc, char* argv[])
  2{
  3    // hipSPARSE handle
  4    hipsparseHandle_t handle;
  5    HIPSPARSE_CHECK(hipsparseCreate(&handle));
  6
  7    //     1 2 0 3 0 0
  8    // A = 0 4 5 0 0 0
  9    //     0 0 0 7 8 0
 10    //     0 0 1 2 4 1
 11
 12    const int                  blockDim = 2;
 13    const int                  mb       = 2;
 14    const int                  kb       = 3;
 15    const int                  nnzb     = 4;
 16    const hipsparseDirection_t dir      = HIPSPARSE_DIRECTION_ROW;
 17
 18    std::vector<int>   hbsrRowPtr = {0, 2, 4};
 19    std::vector<int>   hbsrColInd = {0, 1, 1, 2};
 20    std::vector<float> hbsrVal    = {1, 2, 0, 4, 0, 3, 5, 0, 0, 7, 1, 2, 8, 0, 4, 1};
 21
 22    // Set dimension n of B
 23    const int n = 3;
 24    const int m = mb * blockDim;
 25    const int k = kb * blockDim;
 26
 27    // Allocate and generate dense matrix B (k x n)
 28    std::vector<float> hB = {1.0f,
 29                             2.0f,
 30                             3.0f,
 31                             4.0f,
 32                             5.0f,
 33                             6.0f,
 34                             7.0f,
 35                             8.0f,
 36                             9.0f,
 37                             10.0f,
 38                             11.0f,
 39                             12.0f,
 40                             13.0f,
 41                             14.0f,
 42                             15.0f,
 43                             16.0f,
 44                             17.0f,
 45                             18.0f};
 46
 47    int*   dbsrRowPtr = NULL;
 48    int*   dbsrColInd = NULL;
 49    float* dbsrVal    = NULL;
 50    HIP_CHECK(hipMalloc((void**)&dbsrRowPtr, sizeof(int) * (mb + 1)));
 51    HIP_CHECK(hipMalloc((void**)&dbsrColInd, sizeof(int) * nnzb));
 52    HIP_CHECK(hipMalloc((void**)&dbsrVal, sizeof(float) * nnzb * blockDim * blockDim));
 53    HIP_CHECK(
 54        hipMemcpy(dbsrRowPtr, hbsrRowPtr.data(), sizeof(int) * (mb + 1), hipMemcpyHostToDevice));
 55    HIP_CHECK(hipMemcpy(dbsrColInd, hbsrColInd.data(), sizeof(int) * nnzb, hipMemcpyHostToDevice));
 56    HIP_CHECK(hipMemcpy(dbsrVal,
 57                        hbsrVal.data(),
 58                        sizeof(float) * nnzb * blockDim * blockDim,
 59                        hipMemcpyHostToDevice));
 60
 61    // Copy B to the device
 62    float* dB;
 63    HIP_CHECK(hipMalloc((void**)&dB, sizeof(float) * k * n));
 64    HIP_CHECK(hipMemcpy(dB, hB.data(), sizeof(float) * k * n, hipMemcpyHostToDevice));
 65
 66    // alpha and beta
 67    float alpha = 1.0f;
 68    float beta  = 0.0f;
 69
 70    // Allocate memory for the resulting matrix C
 71    float* dC;
 72    HIP_CHECK(hipMalloc((void**)&dC, sizeof(float) * m * n));
 73
 74    // Matrix descriptor
 75    hipsparseMatDescr_t descr;
 76    HIPSPARSE_CHECK(hipsparseCreateMatDescr(&descr));
 77
 78    // Perform the matrix multiplication
 79    HIPSPARSE_CHECK(hipsparseSbsrmm(handle,
 80                                    dir,
 81                                    HIPSPARSE_OPERATION_NON_TRANSPOSE,
 82                                    HIPSPARSE_OPERATION_NON_TRANSPOSE,
 83                                    mb,
 84                                    n,
 85                                    kb,
 86                                    nnzb,
 87                                    &alpha,
 88                                    descr,
 89                                    dbsrVal,
 90                                    dbsrRowPtr,
 91                                    dbsrColInd,
 92                                    blockDim,
 93                                    dB,
 94                                    k,
 95                                    &beta,
 96                                    dC,
 97                                    m));
 98
 99    // Copy results to host
100    std::vector<float> hC(m * n);
101    HIP_CHECK(hipMemcpy(hC.data(), dC, sizeof(float) * m * n, hipMemcpyDeviceToHost));
102
103    std::cout << "hC" << std::endl;
104    for(int i = 0; i < m * n; i++)
105    {
106        std::cout << hC[i] << " ";
107    }
108    std::cout << std::endl;
109
110    HIP_CHECK(hipFree(dbsrRowPtr));
111    HIP_CHECK(hipFree(dbsrColInd));
112    HIP_CHECK(hipFree(dbsrVal));
113    HIP_CHECK(hipFree(dB));
114    HIP_CHECK(hipFree(dC));
115
116    return 0;
117}

hipsparseXcsrmm()#

Warning

doxygenfunction: Cannot find file: /home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-hipsparse/checkouts/7.13.0-preview/projects/hipsparse/docs/doxygen/xml/index.xml

Warning

doxygenfunction: Cannot find file: /home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-hipsparse/checkouts/7.13.0-preview/projects/hipsparse/docs/doxygen/xml/index.xml

Warning

doxygenfunction: Cannot find file: /home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-hipsparse/checkouts/7.13.0-preview/projects/hipsparse/docs/doxygen/xml/index.xml

Warning

doxygenfunction: Cannot find file: /home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-hipsparse/checkouts/7.13.0-preview/projects/hipsparse/docs/doxygen/xml/index.xml

  1int main(int argc, char* argv[])
  2{
  3    // hipSPARSE handle
  4    hipsparseHandle_t handle;
  5    HIPSPARSE_CHECK(hipsparseCreate(&handle));
  6
  7    //     1 2 0 3 0 0
  8    // A = 0 4 5 0 0 0
  9    //     0 0 0 7 8 0
 10    //     0 0 1 2 4 1
 11
 12    const int                  m   = 4;
 13    const int                  k   = 6;
 14    const int                  nnz = 11;
 15    const hipsparseDirection_t dir = HIPSPARSE_DIRECTION_ROW;
 16
 17    std::vector<int>   hcsrRowPtr = {0, 3, 5, 7, 11};
 18    std::vector<int>   hcsrColInd = {0, 1, 3, 1, 2, 3, 4, 2, 3, 4, 5};
 19    std::vector<float> hcsrVal    = {1, 2, 3, 4, 5, 7, 8, 1, 2, 4, 1};
 20
 21    // Set dimension n of B
 22    const int n = 3;
 23
 24    // Allocate and generate dense matrix B (k x n)
 25    std::vector<float> hB = {1.0f,
 26                             2.0f,
 27                             3.0f,
 28                             4.0f,
 29                             5.0f,
 30                             6.0f,
 31                             7.0f,
 32                             8.0f,
 33                             9.0f,
 34                             10.0f,
 35                             11.0f,
 36                             12.0f,
 37                             13.0f,
 38                             14.0f,
 39                             15.0f,
 40                             16.0f,
 41                             17.0f,
 42                             18.0f};
 43
 44    int*   dcsrRowPtr = NULL;
 45    int*   dcsrColInd = NULL;
 46    float* dcsrVal    = NULL;
 47    HIP_CHECK(hipMalloc((void**)&dcsrRowPtr, sizeof(int) * (m + 1)));
 48    HIP_CHECK(hipMalloc((void**)&dcsrColInd, sizeof(int) * nnz));
 49    HIP_CHECK(hipMalloc((void**)&dcsrVal, sizeof(float) * nnz));
 50    HIP_CHECK(
 51        hipMemcpy(dcsrRowPtr, hcsrRowPtr.data(), sizeof(int) * (m + 1), hipMemcpyHostToDevice));
 52    HIP_CHECK(hipMemcpy(dcsrColInd, hcsrColInd.data(), sizeof(int) * nnz, hipMemcpyHostToDevice));
 53    HIP_CHECK(hipMemcpy(dcsrVal, hcsrVal.data(), sizeof(float) * nnz, hipMemcpyHostToDevice));
 54
 55    // Copy B to the device
 56    float* dB;
 57    HIP_CHECK(hipMalloc((void**)&dB, sizeof(float) * k * n));
 58    HIP_CHECK(hipMemcpy(dB, hB.data(), sizeof(float) * k * n, hipMemcpyHostToDevice));
 59
 60    // alpha and beta
 61    float alpha = 1.0f;
 62    float beta  = 0.0f;
 63
 64    // Allocate memory for the resulting matrix C
 65    float* dC;
 66    HIP_CHECK(hipMalloc((void**)&dC, sizeof(float) * m * n));
 67
 68    // Matrix descriptor
 69    hipsparseMatDescr_t descr;
 70    HIPSPARSE_CHECK(hipsparseCreateMatDescr(&descr));
 71
 72    // Perform the matrix multiplication
 73    HIPSPARSE_CHECK(hipsparseScsrmm(handle,
 74                                    HIPSPARSE_OPERATION_NON_TRANSPOSE,
 75                                    m,
 76                                    n,
 77                                    k,
 78                                    nnz,
 79                                    &alpha,
 80                                    descr,
 81                                    dcsrVal,
 82                                    dcsrRowPtr,
 83                                    dcsrColInd,
 84                                    dB,
 85                                    k,
 86                                    &beta,
 87                                    dC,
 88                                    m));
 89
 90    // Copy results to host
 91    std::vector<float> hC(6 * 3);
 92    HIP_CHECK(hipMemcpy(hC.data(), dC, sizeof(float) * m * n, hipMemcpyDeviceToHost));
 93
 94    std::cout << "hC" << std::endl;
 95    for(int i = 0; i < m * n; i++)
 96    {
 97        std::cout << hC[i] << " ";
 98    }
 99    std::cout << std::endl;
100
101    HIP_CHECK(hipFree(dcsrRowPtr));
102    HIP_CHECK(hipFree(dcsrColInd));
103    HIP_CHECK(hipFree(dcsrVal));
104    HIP_CHECK(hipFree(dB));
105    HIP_CHECK(hipFree(dC));
106
107    HIPSPARSE_CHECK(hipsparseDestroy(handle));
108
109    return 0;
110}

hipsparseXcsrmm2()#

Warning

doxygenfunction: Cannot find file: /home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-hipsparse/checkouts/7.13.0-preview/projects/hipsparse/docs/doxygen/xml/index.xml

Warning

doxygenfunction: Cannot find file: /home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-hipsparse/checkouts/7.13.0-preview/projects/hipsparse/docs/doxygen/xml/index.xml

Warning

doxygenfunction: Cannot find file: /home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-hipsparse/checkouts/7.13.0-preview/projects/hipsparse/docs/doxygen/xml/index.xml

Warning

doxygenfunction: Cannot find file: /home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-hipsparse/checkouts/7.13.0-preview/projects/hipsparse/docs/doxygen/xml/index.xml

hipsparseXbsrsm2_zeroPivot()#

Warning

doxygenfunction: Cannot find file: /home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-hipsparse/checkouts/7.13.0-preview/projects/hipsparse/docs/doxygen/xml/index.xml

hipsparseXbsrsm2_bufferSize()#

Warning

doxygenfunction: Cannot find file: /home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-hipsparse/checkouts/7.13.0-preview/projects/hipsparse/docs/doxygen/xml/index.xml

Warning

doxygenfunction: Cannot find file: /home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-hipsparse/checkouts/7.13.0-preview/projects/hipsparse/docs/doxygen/xml/index.xml

Warning

doxygenfunction: Cannot find file: /home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-hipsparse/checkouts/7.13.0-preview/projects/hipsparse/docs/doxygen/xml/index.xml

Warning

doxygenfunction: Cannot find file: /home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-hipsparse/checkouts/7.13.0-preview/projects/hipsparse/docs/doxygen/xml/index.xml

hipsparseXbsrsm2_analysis()#

Warning

doxygenfunction: Cannot find file: /home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-hipsparse/checkouts/7.13.0-preview/projects/hipsparse/docs/doxygen/xml/index.xml

Warning

doxygenfunction: Cannot find file: /home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-hipsparse/checkouts/7.13.0-preview/projects/hipsparse/docs/doxygen/xml/index.xml

Warning

doxygenfunction: Cannot find file: /home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-hipsparse/checkouts/7.13.0-preview/projects/hipsparse/docs/doxygen/xml/index.xml

Warning

doxygenfunction: Cannot find file: /home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-hipsparse/checkouts/7.13.0-preview/projects/hipsparse/docs/doxygen/xml/index.xml

hipsparseXbsrsm2_solve()#

Warning

doxygenfunction: Cannot find file: /home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-hipsparse/checkouts/7.13.0-preview/projects/hipsparse/docs/doxygen/xml/index.xml

Warning

doxygenfunction: Cannot find file: /home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-hipsparse/checkouts/7.13.0-preview/projects/hipsparse/docs/doxygen/xml/index.xml

Warning

doxygenfunction: Cannot find file: /home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-hipsparse/checkouts/7.13.0-preview/projects/hipsparse/docs/doxygen/xml/index.xml

Warning

doxygenfunction: Cannot find file: /home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-hipsparse/checkouts/7.13.0-preview/projects/hipsparse/docs/doxygen/xml/index.xml

  1int main(int argc, char* argv[])
  2{
  3    // hipSPARSE handle
  4    hipsparseHandle_t handle;
  5    HIPSPARSE_CHECK(hipsparseCreate(&handle));
  6
  7    // A = ( 1.0  0.0  0.0  0.0 )
  8    //     ( 2.0  3.0  0.0  0.0 )
  9    //     ( 4.0  5.0  6.0  0.0 )
 10    //     ( 7.0  0.0  8.0  9.0 )
 11    //
 12    // with bsr_dim = 2
 13    //
 14    //      -------------------
 15    //   = | 1.0 0.0 | 0.0 0.0 |
 16    //     | 2.0 3.0 | 0.0 0.0 |
 17    //      -------------------
 18    //     | 4.0 5.0 | 6.0 0.0 |
 19    //     | 7.0 0.0 | 8.0 9.0 |
 20    //      -------------------
 21
 22    // Number of rows and columns
 23    const int m = 4;
 24
 25    // Number of block rows and block columns
 26    const int mb = 2;
 27    const int nb = 2;
 28
 29    // BSR block dimension
 30    const int bsr_dim = 2;
 31
 32    // Number of right-hand-sides
 33    const int nrhs = 4;
 34
 35    // Number of non-zero blocks
 36    const int nnzb = 3;
 37
 38    // BSR row pointers
 39    std::vector<int> hbsrRowPtr = {0, 1, 3};
 40
 41    // BSR column indices
 42    std::vector<int> hbsrColInd = {0, 0, 1};
 43
 44    // BSR values
 45    std::vector<double> hbsrVal = {1.0, 2.0, 0.0, 3.0, 4.0, 7.0, 5.0, 0.0, 6.0, 8.0, 0.0, 9.0};
 46
 47    // Storage scheme of the BSR blocks
 48    hipsparseDirection_t dir = HIPSPARSE_DIRECTION_COLUMN;
 49
 50    // Transposition of the matrix and rhs matrix
 51    hipsparseOperation_t transA = HIPSPARSE_OPERATION_NON_TRANSPOSE;
 52    hipsparseOperation_t transX = HIPSPARSE_OPERATION_NON_TRANSPOSE;
 53
 54    // Solve policy
 55    hipsparseSolvePolicy_t solve_policy = HIPSPARSE_SOLVE_POLICY_NO_LEVEL;
 56
 57    // Scalar alpha and beta
 58    double alpha = 1.0;
 59
 60    // rhs and solution matrix
 61    const int ldb = nb * bsr_dim;
 62    const int ldx = mb * bsr_dim;
 63
 64    std::vector<double> hB = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16};
 65    std::vector<double> hX(ldx * nrhs);
 66
 67    // Offload data to device
 68    int*    dbsrRowPtr;
 69    int*    dbsrColInd;
 70    double* dbsrVal;
 71    double* dB;
 72    double* dX;
 73
 74    HIP_CHECK(hipMalloc((void**)&dbsrRowPtr, sizeof(int) * (mb + 1)));
 75    HIP_CHECK(hipMalloc((void**)&dbsrColInd, sizeof(int) * nnzb));
 76    HIP_CHECK(hipMalloc((void**)&dbsrVal, sizeof(double) * nnzb * bsr_dim * bsr_dim));
 77    HIP_CHECK(hipMalloc((void**)&dB, sizeof(double) * nb * bsr_dim * nrhs));
 78    HIP_CHECK(hipMalloc((void**)&dX, sizeof(double) * mb * bsr_dim * nrhs));
 79
 80    HIP_CHECK(
 81        hipMemcpy(dbsrRowPtr, hbsrRowPtr.data(), sizeof(int) * (mb + 1), hipMemcpyHostToDevice));
 82    HIP_CHECK(hipMemcpy(dbsrColInd, hbsrColInd.data(), sizeof(int) * nnzb, hipMemcpyHostToDevice));
 83    HIP_CHECK(hipMemcpy(
 84        dbsrVal, hbsrVal.data(), sizeof(double) * nnzb * bsr_dim * bsr_dim, hipMemcpyHostToDevice));
 85    HIP_CHECK(
 86        hipMemcpy(dB, hB.data(), sizeof(double) * nb * bsr_dim * nrhs, hipMemcpyHostToDevice));
 87
 88    // Matrix descriptor
 89    hipsparseMatDescr_t descr;
 90    HIPSPARSE_CHECK(hipsparseCreateMatDescr(&descr));
 91
 92    // Matrix fill mode
 93    HIPSPARSE_CHECK(hipsparseSetMatFillMode(descr, HIPSPARSE_FILL_MODE_LOWER));
 94
 95    // Matrix diagonal type
 96    HIPSPARSE_CHECK(hipsparseSetMatDiagType(descr, HIPSPARSE_DIAG_TYPE_NON_UNIT));
 97
 98    // Matrix info structure
 99    bsrsm2Info_t info;
100    HIPSPARSE_CHECK(hipsparseCreateBsrsm2Info(&info));
101
102    // Obtain required buffer size
103    int buffer_size;
104    HIPSPARSE_CHECK(hipsparseDbsrsm2_bufferSize(handle,
105                                                dir,
106                                                transA,
107                                                transX,
108                                                mb,
109                                                nrhs,
110                                                nnzb,
111                                                descr,
112                                                dbsrVal,
113                                                dbsrRowPtr,
114                                                dbsrColInd,
115                                                bsr_dim,
116                                                info,
117                                                &buffer_size));
118
119    // Allocate temporary buffer
120    void* dbuffer;
121    HIP_CHECK(hipMalloc(&dbuffer, buffer_size));
122
123    // Perform analysis step
124    HIPSPARSE_CHECK(hipsparseDbsrsm2_analysis(handle,
125                                              dir,
126                                              transA,
127                                              transX,
128                                              mb,
129                                              nrhs,
130                                              nnzb,
131                                              descr,
132                                              dbsrVal,
133                                              dbsrRowPtr,
134                                              dbsrColInd,
135                                              bsr_dim,
136                                              info,
137                                              solve_policy,
138                                              dbuffer));
139
140    // Call dbsrsm to perform lower triangular solve LX = B
141    HIPSPARSE_CHECK(hipsparseDbsrsm2_solve(handle,
142                                           dir,
143                                           transA,
144                                           transX,
145                                           mb,
146                                           nrhs,
147                                           nnzb,
148                                           &alpha,
149                                           descr,
150                                           dbsrVal,
151                                           dbsrRowPtr,
152                                           dbsrColInd,
153                                           bsr_dim,
154                                           info,
155                                           dB,
156                                           ldb,
157                                           dX,
158                                           ldx,
159                                           solve_policy,
160                                           dbuffer));
161
162    // Check for zero pivots
163    int               pivot;
164    hipsparseStatus_t status = hipsparseXbsrsm2_zeroPivot(handle, info, &pivot);
165
166    if(status == HIPSPARSE_STATUS_ZERO_PIVOT)
167    {
168        std::cout << "Found zero pivot in matrix row " << pivot << std::endl;
169    }
170
171    // Copy result back to host
172    HIP_CHECK(
173        hipMemcpy(hX.data(), dX, sizeof(double) * mb * bsr_dim * nrhs, hipMemcpyDeviceToHost));
174
175    std::cout << "hX" << std::endl;
176    for(int i = 0; i < ldx * nrhs; i++)
177    {
178        std::cout << hX[i] << " ";
179    }
180    std::cout << std::endl;
181
182    // Clear hipSPARSE
183    HIPSPARSE_CHECK(hipsparseDestroyBsrsm2Info(info));
184    HIPSPARSE_CHECK(hipsparseDestroyMatDescr(descr));
185    HIPSPARSE_CHECK(hipsparseDestroy(handle));
186
187    // Clear device memory
188    HIP_CHECK(hipFree(dbsrRowPtr));
189    HIP_CHECK(hipFree(dbsrColInd));
190    HIP_CHECK(hipFree(dbsrVal));
191    HIP_CHECK(hipFree(dB));
192    HIP_CHECK(hipFree(dX));
193    HIP_CHECK(hipFree(dbuffer));
194
195    return 0;
196}

hipsparseXcsrsm2_zeroPivot()#

Warning

doxygenfunction: Cannot find file: /home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-hipsparse/checkouts/7.13.0-preview/projects/hipsparse/docs/doxygen/xml/index.xml

hipsparseXcsrsm2_bufferSizeExt()#

Warning

doxygenfunction: Cannot find file: /home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-hipsparse/checkouts/7.13.0-preview/projects/hipsparse/docs/doxygen/xml/index.xml

Warning

doxygenfunction: Cannot find file: /home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-hipsparse/checkouts/7.13.0-preview/projects/hipsparse/docs/doxygen/xml/index.xml

Warning

doxygenfunction: Cannot find file: /home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-hipsparse/checkouts/7.13.0-preview/projects/hipsparse/docs/doxygen/xml/index.xml

Warning

doxygenfunction: Cannot find file: /home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-hipsparse/checkouts/7.13.0-preview/projects/hipsparse/docs/doxygen/xml/index.xml

hipsparseXcsrsm2_analysis()#

Warning

doxygenfunction: Cannot find file: /home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-hipsparse/checkouts/7.13.0-preview/projects/hipsparse/docs/doxygen/xml/index.xml

Warning

doxygenfunction: Cannot find file: /home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-hipsparse/checkouts/7.13.0-preview/projects/hipsparse/docs/doxygen/xml/index.xml

Warning

doxygenfunction: Cannot find file: /home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-hipsparse/checkouts/7.13.0-preview/projects/hipsparse/docs/doxygen/xml/index.xml

Warning

doxygenfunction: Cannot find file: /home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-hipsparse/checkouts/7.13.0-preview/projects/hipsparse/docs/doxygen/xml/index.xml

hipsparseXcsrsm2_solve()#

Warning

doxygenfunction: Cannot find file: /home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-hipsparse/checkouts/7.13.0-preview/projects/hipsparse/docs/doxygen/xml/index.xml

Warning

doxygenfunction: Cannot find file: /home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-hipsparse/checkouts/7.13.0-preview/projects/hipsparse/docs/doxygen/xml/index.xml

Warning

doxygenfunction: Cannot find file: /home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-hipsparse/checkouts/7.13.0-preview/projects/hipsparse/docs/doxygen/xml/index.xml

Warning

doxygenfunction: Cannot find file: /home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-hipsparse/checkouts/7.13.0-preview/projects/hipsparse/docs/doxygen/xml/index.xml

  1int main(int argc, char* argv[])
  2{
  3    // hipSPARSE handle
  4    hipsparseHandle_t handle;
  5    HIPSPARSE_CHECK(hipsparseCreate(&handle));
  6
  7    // A = ( 1.0  0.0  0.0  0.0 )
  8    //     ( 2.0  3.0  0.0  0.0 )
  9    //     ( 4.0  5.0  6.0  0.0 )
 10    //     ( 7.0  0.0  8.0  9.0 )
 11
 12    // Number of rows and columns
 13    int m = 4;
 14    int n = 4;
 15
 16    // Number of right-hand-sides
 17    int nrhs = 4;
 18
 19    // Number of non-zeros
 20    int nnz = 9;
 21
 22    // CSR row pointers
 23    std::vector<int> hcsrRowPtr = {0, 1, 3, 6, 9};
 24
 25    // CSR column indices
 26    std::vector<int> hcsrColInd = {0, 0, 1, 0, 1, 2, 0, 2, 3};
 27
 28    // CSR values
 29    std::vector<double> hcsrVal = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0};
 30
 31    // Transposition of the matrix and rhs matrix
 32    hipsparseOperation_t transA = HIPSPARSE_OPERATION_NON_TRANSPOSE;
 33    hipsparseOperation_t transB = HIPSPARSE_OPERATION_NON_TRANSPOSE;
 34
 35    // Solve policy
 36    hipsparseSolvePolicy_t solve_policy = HIPSPARSE_SOLVE_POLICY_NO_LEVEL;
 37
 38    // Scalar alpha and beta
 39    double alpha = 1.0;
 40
 41    // rhs and solution matrix
 42    int ldb = n;
 43
 44    std::vector<double> hB = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16};
 45
 46    // Offload data to device
 47    int*    dcsrRowPtr;
 48    int*    dcsrColInd;
 49    double* dcsrVal;
 50    double* dB;
 51
 52    HIP_CHECK(hipMalloc((void**)&dcsrRowPtr, sizeof(int) * (m + 1)));
 53    HIP_CHECK(hipMalloc((void**)&dcsrColInd, sizeof(int) * nnz));
 54    HIP_CHECK(hipMalloc((void**)&dcsrVal, sizeof(double) * nnz));
 55    HIP_CHECK(hipMalloc((void**)&dB, sizeof(double) * n * nrhs));
 56
 57    HIP_CHECK(
 58        hipMemcpy(dcsrRowPtr, hcsrRowPtr.data(), sizeof(int) * (m + 1), hipMemcpyHostToDevice));
 59    HIP_CHECK(hipMemcpy(dcsrColInd, hcsrColInd.data(), sizeof(int) * nnz, hipMemcpyHostToDevice));
 60    HIP_CHECK(hipMemcpy(dcsrVal, hcsrVal.data(), sizeof(double) * nnz, hipMemcpyHostToDevice));
 61    HIP_CHECK(hipMemcpy(dB, hB.data(), sizeof(double) * n * nrhs, hipMemcpyHostToDevice));
 62
 63    // Matrix descriptor
 64    hipsparseMatDescr_t descr;
 65    HIPSPARSE_CHECK(hipsparseCreateMatDescr(&descr));
 66
 67    // Matrix fill mode
 68    HIPSPARSE_CHECK(hipsparseSetMatFillMode(descr, HIPSPARSE_FILL_MODE_LOWER));
 69
 70    // Matrix diagonal type
 71    HIPSPARSE_CHECK(hipsparseSetMatDiagType(descr, HIPSPARSE_DIAG_TYPE_NON_UNIT));
 72
 73    // Matrix info structure
 74    csrsm2Info_t info;
 75    HIPSPARSE_CHECK(hipsparseCreateCsrsm2Info(&info));
 76
 77    // Obtain required buffer size
 78    size_t buffer_size;
 79    HIPSPARSE_CHECK(hipsparseDcsrsm2_bufferSizeExt(handle,
 80                                                   0,
 81                                                   transA,
 82                                                   transB,
 83                                                   m,
 84                                                   nrhs,
 85                                                   nnz,
 86                                                   &alpha,
 87                                                   descr,
 88                                                   dcsrVal,
 89                                                   dcsrRowPtr,
 90                                                   dcsrColInd,
 91                                                   dB,
 92                                                   ldb,
 93                                                   info,
 94                                                   solve_policy,
 95                                                   &buffer_size));
 96
 97    // Allocate temporary buffer
 98    void* dbuffer;
 99    HIP_CHECK(hipMalloc(&dbuffer, buffer_size));
100
101    // Perform analysis step
102    HIPSPARSE_CHECK(hipsparseDcsrsm2_analysis(handle,
103                                              0,
104                                              transA,
105                                              transB,
106                                              m,
107                                              nrhs,
108                                              nnz,
109                                              &alpha,
110                                              descr,
111                                              dcsrVal,
112                                              dcsrRowPtr,
113                                              dcsrColInd,
114                                              dB,
115                                              ldb,
116                                              info,
117                                              solve_policy,
118                                              dbuffer));
119
120    // Call dcsrsm to perform lower triangular solve LB = B
121    HIPSPARSE_CHECK(hipsparseDcsrsm2_solve(handle,
122                                           0,
123                                           transA,
124                                           transB,
125                                           m,
126                                           nrhs,
127                                           nnz,
128                                           &alpha,
129                                           descr,
130                                           dcsrVal,
131                                           dcsrRowPtr,
132                                           dcsrColInd,
133                                           dB,
134                                           ldb,
135                                           info,
136                                           solve_policy,
137                                           dbuffer));
138
139    // Check for zero pivots
140    int               pivot;
141    hipsparseStatus_t status = hipsparseXcsrsm2_zeroPivot(handle, info, &pivot);
142
143    if(status == HIPSPARSE_STATUS_ZERO_PIVOT)
144    {
145        std::cout << "Found zero pivot in matrix row " << pivot << std::endl;
146    }
147
148    // Copy result back to host
149    HIP_CHECK(hipMemcpy(hB.data(), dB, sizeof(double) * m * nrhs, hipMemcpyDeviceToHost));
150
151    std::cout << "hB" << std::endl;
152    for(size_t i = 0; i < hB.size(); i++)
153    {
154        std::cout << hB[i] << " ";
155    }
156    std::cout << "" << std::endl;
157
158    // Clear hipSPARSE
159    HIPSPARSE_CHECK(hipsparseDestroyCsrsm2Info(info));
160    HIPSPARSE_CHECK(hipsparseDestroyMatDescr(descr));
161    HIPSPARSE_CHECK(hipsparseDestroy(handle));
162
163    // Clear device memory
164    HIP_CHECK(hipFree(dcsrRowPtr));
165    HIP_CHECK(hipFree(dcsrColInd));
166    HIP_CHECK(hipFree(dcsrVal));
167    HIP_CHECK(hipFree(dB));
168    HIP_CHECK(hipFree(dbuffer));
169
170    return 0;
171}

hipsparseXgemmi()#

Warning

doxygenfunction: Cannot find file: /home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-hipsparse/checkouts/7.13.0-preview/projects/hipsparse/docs/doxygen/xml/index.xml

Warning

doxygenfunction: Cannot find file: /home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-hipsparse/checkouts/7.13.0-preview/projects/hipsparse/docs/doxygen/xml/index.xml

Warning

doxygenfunction: Cannot find file: /home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-hipsparse/checkouts/7.13.0-preview/projects/hipsparse/docs/doxygen/xml/index.xml

Warning

doxygenfunction: Cannot find file: /home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-hipsparse/checkouts/7.13.0-preview/projects/hipsparse/docs/doxygen/xml/index.xml

 1int main(int argc, char* argv[])
 2{
 3    // A, B, and C are m×k, k×n, and m×n
 4    const int m = 3, n = 5, k = 4;
 5    const int lda = m, ldc = m;
 6    const int nnz_A = m * k, nnz_B = 10, nnz_C = m * n;
 7
 8    // alpha and beta
 9    float alpha = 0.5f;
10    float beta  = 0.25f;
11
12    std::vector<int>   hcscColPtr = {0, 2, 5, 7, 8, 10};
13    std::vector<int>   hcscRowInd = {0, 2, 0, 1, 3, 1, 3, 2, 0, 2};
14    std::vector<float> hcsc_val   = {1, 6, 2, 4, 9, 5, 2, 7, 3, 8};
15
16    std::vector<float> hA(nnz_A, 1.0f);
17    std::vector<float> hC(nnz_C, 1.0f);
18
19    int*   dcscColPtr;
20    int*   dcscRowInd;
21    float* dcsc_val;
22    HIP_CHECK(hipMalloc((void**)&dcscColPtr, sizeof(int) * (n + 1)));
23    HIP_CHECK(hipMalloc((void**)&dcscRowInd, sizeof(int) * nnz_B));
24    HIP_CHECK(hipMalloc((void**)&dcsc_val, sizeof(float) * nnz_B));
25
26    HIP_CHECK(
27        hipMemcpy(dcscColPtr, hcscColPtr.data(), sizeof(int) * (n + 1), hipMemcpyHostToDevice));
28    HIP_CHECK(hipMemcpy(dcscRowInd, hcscRowInd.data(), sizeof(int) * nnz_B, hipMemcpyHostToDevice));
29    HIP_CHECK(hipMemcpy(dcsc_val, hcsc_val.data(), sizeof(float) * nnz_B, hipMemcpyHostToDevice));
30
31    hipsparseHandle_t handle;
32    HIPSPARSE_CHECK(hipsparseCreate(&handle));
33
34    // Allocate memory for the matrix A
35    float* dA;
36    HIP_CHECK(hipMalloc((void**)&dA, sizeof(float) * nnz_A));
37    HIP_CHECK(hipMemcpy(dA, hA.data(), sizeof(float) * nnz_A, hipMemcpyHostToDevice));
38
39    // Allocate memory for the resulting matrix C
40    float* dC;
41    HIP_CHECK(hipMalloc((void**)&dC, sizeof(float) * nnz_C));
42    HIP_CHECK(hipMemcpy(dC, hC.data(), sizeof(float) * nnz_C, hipMemcpyHostToDevice));
43
44    // Perform operation
45    HIPSPARSE_CHECK(hipsparseSgemmi(
46        handle, m, n, k, nnz_B, &alpha, dA, lda, dcsc_val, dcscColPtr, dcscRowInd, &beta, dC, ldc));
47
48    // Copy device to host
49    HIP_CHECK(hipMemcpy(hC.data(), dC, sizeof(float) * nnz_C, hipMemcpyDeviceToHost));
50
51    std::cout << "hC" << std::endl;
52    for(int i = 0; i < nnz_C; i++)
53    {
54        std::cout << hC[i] << " ";
55    }
56    std::cout << std::endl;
57
58    // Destroy matrix descriptors and handles
59    HIPSPARSE_CHECK(hipsparseDestroy(handle));
60
61    HIP_CHECK(hipFree(dcscColPtr));
62    HIP_CHECK(hipFree(dcscRowInd));
63    HIP_CHECK(hipFree(dcsc_val));
64    HIP_CHECK(hipFree(dA));
65    HIP_CHECK(hipFree(dC));
66
67    return 0;
68}