Sparse extra functions#

This module contains all sparse extra routines.

The sparse extra routines describe operations that manipulate sparse matrices.

hipsparseXcsrgeamNnz()#

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

hipsparseXcsrgeam()#

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    const int m    = 4;
  4    const int n    = 4;
  5    const int nnzA = 9;
  6    const int nnzB = 6;
  7
  8    float alpha{1.0f};
  9    float beta{1.0f};
 10
 11    // A, B, and C are m×n
 12
 13    // A
 14    // 1 0 0 2
 15    // 3 4 0 0
 16    // 5 6 7 8
 17    // 0 0 9 0
 18    std::vector<int>   hcsrRowPtrA = {0, 2, 4, 8, 9};
 19    std::vector<int>   hcsrColIndA = {0, 3, 0, 1, 0, 1, 2, 3, 2};
 20    std::vector<float> hcsrValA    = {1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f, 9.0f};
 21
 22    // B
 23    // 0 1 0 0
 24    // 1 0 1 0
 25    // 0 1 0 1
 26    // 0 0 1 0
 27    std::vector<int>   hcsrRowPtrB = {0, 1, 3, 5, 6};
 28    std::vector<int>   hcsrColIndB = {1, 0, 2, 1, 3, 2};
 29    std::vector<float> hcsrValB    = {1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f};
 30
 31    // Device memory management: Allocate and copy A, B
 32    int*   dcsrRowPtrA;
 33    int*   dcsrColIndA;
 34    float* dcsrValA;
 35    int*   dcsrRowPtrB;
 36    int*   dcsrColIndB;
 37    float* dcsrValB;
 38    int*   dcsrRowPtrC;
 39    HIP_CHECK(hipMalloc((void**)&dcsrRowPtrA, (m + 1) * sizeof(int)));
 40    HIP_CHECK(hipMalloc((void**)&dcsrColIndA, nnzA * sizeof(int)));
 41    HIP_CHECK(hipMalloc((void**)&dcsrValA, nnzA * sizeof(float)));
 42    HIP_CHECK(hipMalloc((void**)&dcsrRowPtrB, (m + 1) * sizeof(int)));
 43    HIP_CHECK(hipMalloc((void**)&dcsrColIndB, nnzB * sizeof(int)));
 44    HIP_CHECK(hipMalloc((void**)&dcsrValB, nnzB * sizeof(float)));
 45    HIP_CHECK(hipMalloc((void**)&dcsrRowPtrC, (m + 1) * sizeof(int)));
 46
 47    HIP_CHECK(
 48        hipMemcpy(dcsrRowPtrA, hcsrRowPtrA.data(), (m + 1) * sizeof(int), hipMemcpyHostToDevice));
 49    HIP_CHECK(
 50        hipMemcpy(dcsrColIndA, hcsrColIndA.data(), nnzA * sizeof(int), hipMemcpyHostToDevice));
 51    HIP_CHECK(hipMemcpy(dcsrValA, hcsrValA.data(), nnzA * sizeof(float), hipMemcpyHostToDevice));
 52    HIP_CHECK(
 53        hipMemcpy(dcsrRowPtrB, hcsrRowPtrB.data(), (m + 1) * sizeof(int), hipMemcpyHostToDevice));
 54    HIP_CHECK(
 55        hipMemcpy(dcsrColIndB, hcsrColIndB.data(), nnzB * sizeof(int), hipMemcpyHostToDevice));
 56    HIP_CHECK(hipMemcpy(dcsrValB, hcsrValB.data(), nnzB * sizeof(float), hipMemcpyHostToDevice));
 57
 58    hipsparseHandle_t handle;
 59    HIPSPARSE_CHECK(hipsparseCreate(&handle));
 60
 61    hipsparseMatDescr_t descrA;
 62    HIPSPARSE_CHECK(hipsparseCreateMatDescr(&descrA));
 63
 64    hipsparseMatDescr_t descrB;
 65    HIPSPARSE_CHECK(hipsparseCreateMatDescr(&descrB));
 66
 67    hipsparseMatDescr_t descrC;
 68    HIPSPARSE_CHECK(hipsparseCreateMatDescr(&descrC));
 69
 70    int nnzC;
 71    HIPSPARSE_CHECK(hipsparseXcsrgeamNnz(handle,
 72                                         m,
 73                                         n,
 74                                         descrA,
 75                                         nnzA,
 76                                         dcsrRowPtrA,
 77                                         dcsrColIndA,
 78                                         descrB,
 79                                         nnzB,
 80                                         dcsrRowPtrB,
 81                                         dcsrColIndB,
 82                                         descrC,
 83                                         dcsrRowPtrC,
 84                                         &nnzC));
 85
 86    int*   dcsrColIndC = nullptr;
 87    float* dcsrValC    = nullptr;
 88    HIP_CHECK(hipMalloc((void**)&dcsrColIndC, sizeof(int) * nnzC));
 89    HIP_CHECK(hipMalloc((void**)&dcsrValC, sizeof(float) * nnzC));
 90
 91    HIPSPARSE_CHECK(hipsparseScsrgeam(handle,
 92                                      m,
 93                                      n,
 94                                      &alpha,
 95                                      descrA,
 96                                      nnzA,
 97                                      dcsrValA,
 98                                      dcsrRowPtrA,
 99                                      dcsrColIndA,
100                                      &beta,
101                                      descrB,
102                                      nnzB,
103                                      dcsrValB,
104                                      dcsrRowPtrB,
105                                      dcsrColIndB,
106                                      descrC,
107                                      dcsrValC,
108                                      dcsrRowPtrC,
109                                      dcsrColIndC));
110
111    std::vector<int>   hcsrRowPtrC(m + 1);
112    std::vector<int>   hcsrColIndC(nnzC);
113    std::vector<float> hcsrValC(nnzC);
114
115    // Copy back to the host
116    HIP_CHECK(
117        hipMemcpy(hcsrRowPtrC.data(), dcsrRowPtrC, sizeof(int) * (m + 1), hipMemcpyDeviceToHost));
118    HIP_CHECK(
119        hipMemcpy(hcsrColIndC.data(), dcsrColIndC, sizeof(int) * nnzC, hipMemcpyDeviceToHost));
120    HIP_CHECK(hipMemcpy(hcsrValC.data(), dcsrValC, sizeof(float) * nnzC, hipMemcpyDeviceToHost));
121
122    std::cout << "C" << std::endl;
123    for(int i = 0; i < m; i++)
124    {
125        int start = hcsrRowPtrC[i];
126        int end   = hcsrRowPtrC[i + 1];
127
128        std::vector<float> temp(n, 0.0f);
129        for(int j = start; j < end; j++)
130        {
131            temp[hcsrColIndC[j]] = hcsrValC[j];
132        }
133
134        for(int j = 0; j < n; j++)
135        {
136            std::cout << temp[j] << " ";
137        }
138        std::cout << std::endl;
139    }
140    std::cout << std::endl;
141
142    HIP_CHECK(hipFree(dcsrRowPtrA));
143    HIP_CHECK(hipFree(dcsrColIndA));
144    HIP_CHECK(hipFree(dcsrValA));
145    HIP_CHECK(hipFree(dcsrRowPtrB));
146    HIP_CHECK(hipFree(dcsrColIndB));
147    HIP_CHECK(hipFree(dcsrValB));
148    HIP_CHECK(hipFree(dcsrRowPtrC));
149    HIP_CHECK(hipFree(dcsrColIndC));
150    HIP_CHECK(hipFree(dcsrValC));
151
152    HIPSPARSE_CHECK(hipsparseDestroyMatDescr(descrA));
153    HIPSPARSE_CHECK(hipsparseDestroyMatDescr(descrB));
154    HIPSPARSE_CHECK(hipsparseDestroyMatDescr(descrC));
155    HIPSPARSE_CHECK(hipsparseDestroy(handle));
156
157    return 0;
158}

hipsparseXcsrgeam2_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

hipsparseXcsrgeam2Nnz()#

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

hipsparseXcsrgeam2()#

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    const int m    = 4;
  4    const int n    = 4;
  5    const int nnzA = 9;
  6    const int nnzB = 6;
  7
  8    float alpha{1.0f};
  9    float beta{1.0f};
 10
 11    // A, B, and C are m×n
 12
 13    // A
 14    // 1 0 0 2
 15    // 3 4 0 0
 16    // 5 6 7 8
 17    // 0 0 9 0
 18    std::vector<int>   hcsrRowPtrA = {0, 2, 4, 8, 9};
 19    std::vector<int>   hcsrColIndA = {0, 3, 0, 1, 0, 1, 2, 3, 2};
 20    std::vector<float> hcsrValA    = {1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f, 9.0f};
 21
 22    // B
 23    // 0 1 0 0
 24    // 1 0 1 0
 25    // 0 1 0 1
 26    // 0 0 1 0
 27    std::vector<int>   hcsrRowPtrB = {0, 1, 3, 5, 6};
 28    std::vector<int>   hcsrColIndB = {1, 0, 2, 1, 3, 2};
 29    std::vector<float> hcsrValB    = {1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f};
 30
 31    // Device memory management: Allocate and copy A, B
 32    int*   dcsrRowPtrA;
 33    int*   dcsrColIndA;
 34    float* dcsrValA;
 35    int*   dcsrRowPtrB;
 36    int*   dcsrColIndB;
 37    float* dcsrValB;
 38    int*   dcsrRowPtrC;
 39    HIP_CHECK(hipMalloc((void**)&dcsrRowPtrA, (m + 1) * sizeof(int)));
 40    HIP_CHECK(hipMalloc((void**)&dcsrColIndA, nnzA * sizeof(int)));
 41    HIP_CHECK(hipMalloc((void**)&dcsrValA, nnzA * sizeof(float)));
 42    HIP_CHECK(hipMalloc((void**)&dcsrRowPtrB, (m + 1) * sizeof(int)));
 43    HIP_CHECK(hipMalloc((void**)&dcsrColIndB, nnzB * sizeof(int)));
 44    HIP_CHECK(hipMalloc((void**)&dcsrValB, nnzB * sizeof(float)));
 45    HIP_CHECK(hipMalloc((void**)&dcsrRowPtrC, (m + 1) * sizeof(int)));
 46
 47    HIP_CHECK(
 48        hipMemcpy(dcsrRowPtrA, hcsrRowPtrA.data(), (m + 1) * sizeof(int), hipMemcpyHostToDevice));
 49    HIP_CHECK(
 50        hipMemcpy(dcsrColIndA, hcsrColIndA.data(), nnzA * sizeof(int), hipMemcpyHostToDevice));
 51    HIP_CHECK(hipMemcpy(dcsrValA, hcsrValA.data(), nnzA * sizeof(float), hipMemcpyHostToDevice));
 52    HIP_CHECK(
 53        hipMemcpy(dcsrRowPtrB, hcsrRowPtrB.data(), (m + 1) * sizeof(int), hipMemcpyHostToDevice));
 54    HIP_CHECK(
 55        hipMemcpy(dcsrColIndB, hcsrColIndB.data(), nnzB * sizeof(int), hipMemcpyHostToDevice));
 56    HIP_CHECK(hipMemcpy(dcsrValB, hcsrValB.data(), nnzB * sizeof(float), hipMemcpyHostToDevice));
 57
 58    hipsparseHandle_t handle;
 59    HIPSPARSE_CHECK(hipsparseCreate(&handle));
 60
 61    hipsparseMatDescr_t descrA;
 62    HIPSPARSE_CHECK(hipsparseCreateMatDescr(&descrA));
 63
 64    hipsparseMatDescr_t descrB;
 65    HIPSPARSE_CHECK(hipsparseCreateMatDescr(&descrB));
 66
 67    hipsparseMatDescr_t descrC;
 68    HIPSPARSE_CHECK(hipsparseCreateMatDescr(&descrC));
 69
 70    size_t bufferSize;
 71    HIPSPARSE_CHECK(hipsparseScsrgeam2_bufferSizeExt(handle,
 72                                                     m,
 73                                                     n,
 74                                                     &alpha,
 75                                                     descrA,
 76                                                     nnzA,
 77                                                     dcsrValA,
 78                                                     dcsrRowPtrA,
 79                                                     dcsrColIndA,
 80                                                     &beta,
 81                                                     descrB,
 82                                                     nnzB,
 83                                                     dcsrValB,
 84                                                     dcsrRowPtrB,
 85                                                     dcsrColIndB,
 86                                                     descrC,
 87                                                     nullptr,
 88                                                     dcsrRowPtrC,
 89                                                     nullptr,
 90                                                     &bufferSize));
 91
 92    void* dbuffer = nullptr;
 93    HIP_CHECK(hipMalloc((void**)&dbuffer, bufferSize));
 94
 95    int nnzC;
 96    HIPSPARSE_CHECK(hipsparseXcsrgeam2Nnz(handle,
 97                                          m,
 98                                          n,
 99                                          descrA,
100                                          nnzA,
101                                          dcsrRowPtrA,
102                                          dcsrColIndA,
103                                          descrB,
104                                          nnzB,
105                                          dcsrRowPtrB,
106                                          dcsrColIndB,
107                                          descrC,
108                                          dcsrRowPtrC,
109                                          &nnzC,
110                                          dbuffer));
111
112    int*   dcsrColIndC = nullptr;
113    float* dcsrValC    = nullptr;
114    HIP_CHECK(hipMalloc((void**)&dcsrColIndC, sizeof(int) * nnzC));
115    HIP_CHECK(hipMalloc((void**)&dcsrValC, sizeof(float) * nnzC));
116
117    HIPSPARSE_CHECK(hipsparseScsrgeam2(handle,
118                                       m,
119                                       n,
120                                       &alpha,
121                                       descrA,
122                                       nnzA,
123                                       dcsrValA,
124                                       dcsrRowPtrA,
125                                       dcsrColIndA,
126                                       &beta,
127                                       descrB,
128                                       nnzB,
129                                       dcsrValB,
130                                       dcsrRowPtrB,
131                                       dcsrColIndB,
132                                       descrC,
133                                       dcsrValC,
134                                       dcsrRowPtrC,
135                                       dcsrColIndC,
136                                       dbuffer));
137
138    std::vector<int>   hcsrRowPtrC(m + 1);
139    std::vector<int>   hcsrColIndC(nnzC);
140    std::vector<float> hcsrValC(nnzC);
141
142    // Copy back to the host
143    HIP_CHECK(
144        hipMemcpy(hcsrRowPtrC.data(), dcsrRowPtrC, sizeof(int) * (m + 1), hipMemcpyDeviceToHost));
145    HIP_CHECK(
146        hipMemcpy(hcsrColIndC.data(), dcsrColIndC, sizeof(int) * nnzC, hipMemcpyDeviceToHost));
147    HIP_CHECK(hipMemcpy(hcsrValC.data(), dcsrValC, sizeof(float) * nnzC, hipMemcpyDeviceToHost));
148
149    std::cout << "C" << std::endl;
150    for(int i = 0; i < m; i++)
151    {
152        int start = hcsrRowPtrC[i];
153        int end   = hcsrRowPtrC[i + 1];
154
155        std::vector<float> temp(n, 0.0f);
156        for(int j = start; j < end; j++)
157        {
158            temp[hcsrColIndC[j]] = hcsrValC[j];
159        }
160
161        for(int j = 0; j < n; j++)
162        {
163            std::cout << temp[j] << " ";
164        }
165        std::cout << std::endl;
166    }
167    std::cout << std::endl;
168
169    HIP_CHECK(hipFree(dcsrRowPtrA));
170    HIP_CHECK(hipFree(dcsrColIndA));
171    HIP_CHECK(hipFree(dcsrValA));
172    HIP_CHECK(hipFree(dcsrRowPtrB));
173    HIP_CHECK(hipFree(dcsrColIndB));
174    HIP_CHECK(hipFree(dcsrValB));
175    HIP_CHECK(hipFree(dcsrRowPtrC));
176    HIP_CHECK(hipFree(dcsrColIndC));
177    HIP_CHECK(hipFree(dcsrValC));
178
179    HIP_CHECK(hipFree(dbuffer));
180
181    HIPSPARSE_CHECK(hipsparseDestroyMatDescr(descrA));
182    HIPSPARSE_CHECK(hipsparseDestroyMatDescr(descrB));
183    HIPSPARSE_CHECK(hipsparseDestroyMatDescr(descrC));
184    HIPSPARSE_CHECK(hipsparseDestroy(handle));
185
186    return 0;
187}

hipsparseXcsrgemmNnz()#

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

hipsparseXcsrgemm()#

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    const int m    = 4;
  4    const int k    = 3;
  5    const int n    = 2;
  6    const int nnzA = 7;
  7    const int nnzB = 3;
  8
  9    hipsparseOperation_t transA = HIPSPARSE_OPERATION_NON_TRANSPOSE;
 10    hipsparseOperation_t transB = HIPSPARSE_OPERATION_NON_TRANSPOSE;
 11
 12    // A, B, and C are mxk, kxn, and m×n
 13
 14    // A
 15    // 1 0 0
 16    // 3 4 0
 17    // 5 6 7
 18    // 0 0 9
 19    std::vector<int>   hcsrRowPtrA = {0, 1, 3, 6, 7};
 20    std::vector<int>   hcsrColIndA = {0, 0, 1, 0, 1, 2, 2};
 21    std::vector<float> hcsrValA    = {1.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 9.0f};
 22
 23    // B
 24    // 0 1
 25    // 1 0
 26    // 0 1
 27    std::vector<int>   hcsrRowPtrB = {0, 1, 2, 3};
 28    std::vector<int>   hcsrColIndB = {1, 0, 1};
 29    std::vector<float> hcsrValB    = {1.0f, 1.0f, 1.0f};
 30
 31    // Device memory management: Allocate and copy A, B
 32    int*   dcsrRowPtrA;
 33    int*   dcsrColIndA;
 34    float* dcsrValA;
 35    int*   dcsrRowPtrB;
 36    int*   dcsrColIndB;
 37    float* dcsrValB;
 38    int*   dcsrRowPtrC;
 39    HIP_CHECK(hipMalloc((void**)&dcsrRowPtrA, (m + 1) * sizeof(int)));
 40    HIP_CHECK(hipMalloc((void**)&dcsrColIndA, nnzA * sizeof(int)));
 41    HIP_CHECK(hipMalloc((void**)&dcsrValA, nnzA * sizeof(float)));
 42    HIP_CHECK(hipMalloc((void**)&dcsrRowPtrB, (m + 1) * sizeof(int)));
 43    HIP_CHECK(hipMalloc((void**)&dcsrColIndB, nnzB * sizeof(int)));
 44    HIP_CHECK(hipMalloc((void**)&dcsrValB, nnzB * sizeof(float)));
 45    HIP_CHECK(hipMalloc((void**)&dcsrRowPtrC, (m + 1) * sizeof(int)));
 46
 47    HIP_CHECK(
 48        hipMemcpy(dcsrRowPtrA, hcsrRowPtrA.data(), (m + 1) * sizeof(int), hipMemcpyHostToDevice));
 49    HIP_CHECK(
 50        hipMemcpy(dcsrColIndA, hcsrColIndA.data(), nnzA * sizeof(int), hipMemcpyHostToDevice));
 51    HIP_CHECK(hipMemcpy(dcsrValA, hcsrValA.data(), nnzA * sizeof(float), hipMemcpyHostToDevice));
 52    HIP_CHECK(
 53        hipMemcpy(dcsrRowPtrB, hcsrRowPtrB.data(), (m + 1) * sizeof(int), hipMemcpyHostToDevice));
 54    HIP_CHECK(
 55        hipMemcpy(dcsrColIndB, hcsrColIndB.data(), nnzB * sizeof(int), hipMemcpyHostToDevice));
 56    HIP_CHECK(hipMemcpy(dcsrValB, hcsrValB.data(), nnzB * sizeof(float), hipMemcpyHostToDevice));
 57
 58    hipsparseHandle_t handle;
 59    HIPSPARSE_CHECK(hipsparseCreate(&handle));
 60
 61    hipsparseMatDescr_t descrA;
 62    HIPSPARSE_CHECK(hipsparseCreateMatDescr(&descrA));
 63
 64    hipsparseMatDescr_t descrB;
 65    HIPSPARSE_CHECK(hipsparseCreateMatDescr(&descrB));
 66
 67    hipsparseMatDescr_t descrC;
 68    HIPSPARSE_CHECK(hipsparseCreateMatDescr(&descrC));
 69
 70    int nnzC;
 71    HIPSPARSE_CHECK(hipsparseXcsrgemmNnz(handle,
 72                                         transA,
 73                                         transB,
 74                                         m,
 75                                         n,
 76                                         k,
 77                                         descrA,
 78                                         nnzA,
 79                                         dcsrRowPtrA,
 80                                         dcsrColIndA,
 81                                         descrB,
 82                                         nnzB,
 83                                         dcsrRowPtrB,
 84                                         dcsrColIndB,
 85                                         descrC,
 86                                         dcsrRowPtrC,
 87                                         &nnzC));
 88
 89    int*   dcsrColIndC = nullptr;
 90    float* dcsrValC    = nullptr;
 91    HIP_CHECK(hipMalloc((void**)&dcsrColIndC, sizeof(int) * nnzC));
 92    HIP_CHECK(hipMalloc((void**)&dcsrValC, sizeof(float) * nnzC));
 93
 94    HIPSPARSE_CHECK(hipsparseScsrgemm(handle,
 95                                      transA,
 96                                      transB,
 97                                      m,
 98                                      n,
 99                                      k,
100                                      descrA,
101                                      nnzA,
102                                      dcsrValA,
103                                      dcsrRowPtrA,
104                                      dcsrColIndA,
105                                      descrB,
106                                      nnzB,
107                                      dcsrValB,
108                                      dcsrRowPtrB,
109                                      dcsrColIndB,
110                                      descrC,
111                                      dcsrValC,
112                                      dcsrRowPtrC,
113                                      dcsrColIndC));
114
115    std::vector<int>   hcsrRowPtrC(m + 1);
116    std::vector<int>   hcsrColIndC(nnzC);
117    std::vector<float> hcsrValC(nnzC);
118
119    // Copy back to the host
120    HIP_CHECK(
121        hipMemcpy(hcsrRowPtrC.data(), dcsrRowPtrC, sizeof(int) * (m + 1), hipMemcpyDeviceToHost));
122    HIP_CHECK(
123        hipMemcpy(hcsrColIndC.data(), dcsrColIndC, sizeof(int) * nnzC, hipMemcpyDeviceToHost));
124    HIP_CHECK(hipMemcpy(hcsrValC.data(), dcsrValC, sizeof(float) * nnzC, hipMemcpyDeviceToHost));
125
126    std::cout << "C" << std::endl;
127    for(int i = 0; i < m; i++)
128    {
129        int start = hcsrRowPtrC[i];
130        int end   = hcsrRowPtrC[i + 1];
131
132        std::vector<float> temp(n, 0.0f);
133        for(int j = start; j < end; j++)
134        {
135            temp[hcsrColIndC[j]] = hcsrValC[j];
136        }
137
138        for(int j = 0; j < n; j++)
139        {
140            std::cout << temp[j] << " ";
141        }
142        std::cout << std::endl;
143    }
144    std::cout << std::endl;
145
146    HIP_CHECK(hipFree(dcsrRowPtrA));
147    HIP_CHECK(hipFree(dcsrColIndA));
148    HIP_CHECK(hipFree(dcsrValA));
149    HIP_CHECK(hipFree(dcsrRowPtrB));
150    HIP_CHECK(hipFree(dcsrColIndB));
151    HIP_CHECK(hipFree(dcsrValB));
152    HIP_CHECK(hipFree(dcsrRowPtrC));
153    HIP_CHECK(hipFree(dcsrColIndC));
154    HIP_CHECK(hipFree(dcsrValC));
155
156    HIPSPARSE_CHECK(hipsparseDestroyMatDescr(descrA));
157    HIPSPARSE_CHECK(hipsparseDestroyMatDescr(descrB));
158    HIPSPARSE_CHECK(hipsparseDestroyMatDescr(descrC));
159    HIPSPARSE_CHECK(hipsparseDestroy(handle));
160
161    return 0;
162}

hipsparseXcsrgemm2_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

hipsparseXcsrgemm2Nnz()#

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

hipsparseXcsrgemm2()#

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    int m    = 4;
  4    int k    = 3;
  5    int n    = 2;
  6    int nnzA = 7;
  7    int nnzB = 3;
  8    int nnzD = 6;
  9
 10    float alpha{1.0f};
 11    float beta{1.0f};
 12
 13    // A, B, and C are mxk, kxn, and m×n
 14
 15    // A
 16    // 1 0 0
 17    // 3 4 0
 18    // 5 6 7
 19    // 0 0 9
 20    std::vector<int>   hcsrRowPtrA = {0, 1, 3, 6, 7};
 21    std::vector<int>   hcsrColIndA = {0, 0, 1, 0, 1, 2, 2};
 22    std::vector<float> hcsrValA    = {1.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 9.0f};
 23
 24    // B
 25    // 0 1
 26    // 1 0
 27    // 0 1
 28    std::vector<int>   hcsrRowPtrB = {0, 1, 2, 3};
 29    std::vector<int>   hcsrColIndB = {1, 0, 1};
 30    std::vector<float> hcsrValB    = {1.0f, 1.0f, 1.0f};
 31
 32    // D
 33    // 0 1
 34    // 2 3
 35    // 4 5
 36    // 0 6
 37    std::vector<int>   hcsrRowPtrD = {0, 1, 3, 5, 6};
 38    std::vector<int>   hcsrColIndD = {1, 0, 1, 0, 1, 1};
 39    std::vector<float> hcsrValD    = {1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f};
 40
 41    // Device memory management: Allocate and copy A, B
 42    int*   dcsrRowPtrA;
 43    int*   dcsrColIndA;
 44    float* dcsrValA;
 45    int*   dcsrRowPtrB;
 46    int*   dcsrColIndB;
 47    float* dcsrValB;
 48    int*   dcsrRowPtrD;
 49    int*   dcsrColIndD;
 50    float* dcsrValD;
 51    int*   dcsrRowPtrC;
 52    HIP_CHECK(hipMalloc((void**)&dcsrRowPtrA, (m + 1) * sizeof(int)));
 53    HIP_CHECK(hipMalloc((void**)&dcsrColIndA, nnzA * sizeof(int)));
 54    HIP_CHECK(hipMalloc((void**)&dcsrValA, nnzA * sizeof(float)));
 55    HIP_CHECK(hipMalloc((void**)&dcsrRowPtrB, (k + 1) * sizeof(int)));
 56    HIP_CHECK(hipMalloc((void**)&dcsrColIndB, nnzB * sizeof(int)));
 57    HIP_CHECK(hipMalloc((void**)&dcsrValB, nnzB * sizeof(float)));
 58    HIP_CHECK(hipMalloc((void**)&dcsrRowPtrD, (m + 1) * sizeof(int)));
 59    HIP_CHECK(hipMalloc((void**)&dcsrColIndD, nnzD * sizeof(int)));
 60    HIP_CHECK(hipMalloc((void**)&dcsrValD, nnzD * sizeof(float)));
 61    HIP_CHECK(hipMalloc((void**)&dcsrRowPtrC, (m + 1) * sizeof(int)));
 62
 63    HIP_CHECK(
 64        hipMemcpy(dcsrRowPtrA, hcsrRowPtrA.data(), (m + 1) * sizeof(int), hipMemcpyHostToDevice));
 65    HIP_CHECK(
 66        hipMemcpy(dcsrColIndA, hcsrColIndA.data(), nnzA * sizeof(int), hipMemcpyHostToDevice));
 67    HIP_CHECK(hipMemcpy(dcsrValA, hcsrValA.data(), nnzA * sizeof(float), hipMemcpyHostToDevice));
 68    HIP_CHECK(
 69        hipMemcpy(dcsrRowPtrB, hcsrRowPtrB.data(), (k + 1) * sizeof(int), hipMemcpyHostToDevice));
 70    HIP_CHECK(
 71        hipMemcpy(dcsrColIndB, hcsrColIndB.data(), nnzB * sizeof(int), hipMemcpyHostToDevice));
 72    HIP_CHECK(hipMemcpy(dcsrValB, hcsrValB.data(), nnzB * sizeof(float), hipMemcpyHostToDevice));
 73    HIP_CHECK(
 74        hipMemcpy(dcsrRowPtrD, hcsrRowPtrD.data(), (m + 1) * sizeof(int), hipMemcpyHostToDevice));
 75    HIP_CHECK(
 76        hipMemcpy(dcsrColIndD, hcsrColIndD.data(), nnzD * sizeof(int), hipMemcpyHostToDevice));
 77    HIP_CHECK(hipMemcpy(dcsrValD, hcsrValD.data(), nnzD * sizeof(float), hipMemcpyHostToDevice));
 78
 79    hipsparseHandle_t handle;
 80    HIPSPARSE_CHECK(hipsparseCreate(&handle));
 81
 82    hipsparseMatDescr_t descrA;
 83    HIPSPARSE_CHECK(hipsparseCreateMatDescr(&descrA));
 84
 85    hipsparseMatDescr_t descrB;
 86    HIPSPARSE_CHECK(hipsparseCreateMatDescr(&descrB));
 87
 88    hipsparseMatDescr_t descrC;
 89    HIPSPARSE_CHECK(hipsparseCreateMatDescr(&descrC));
 90
 91    hipsparseMatDescr_t descrD;
 92    HIPSPARSE_CHECK(hipsparseCreateMatDescr(&descrD));
 93
 94    csrgemm2Info_t info;
 95    HIPSPARSE_CHECK(hipsparseCreateCsrgemm2Info(&info));
 96
 97    size_t bufferSize;
 98    HIPSPARSE_CHECK(hipsparseScsrgemm2_bufferSizeExt(handle,
 99                                                     m,
100                                                     n,
101                                                     k,
102                                                     &alpha,
103                                                     descrA,
104                                                     nnzA,
105                                                     dcsrRowPtrA,
106                                                     dcsrColIndA,
107                                                     descrB,
108                                                     nnzB,
109                                                     dcsrRowPtrB,
110                                                     dcsrColIndB,
111                                                     &beta,
112                                                     descrD,
113                                                     nnzD,
114                                                     dcsrRowPtrD,
115                                                     dcsrColIndD,
116                                                     info,
117                                                     &bufferSize));
118
119    void* dbuffer = nullptr;
120    HIP_CHECK(hipMalloc((void**)&dbuffer, bufferSize));
121
122    int nnzC;
123    HIPSPARSE_CHECK(hipsparseXcsrgemm2Nnz(handle,
124                                          m,
125                                          n,
126                                          k,
127                                          descrA,
128                                          nnzA,
129                                          dcsrRowPtrA,
130                                          dcsrColIndA,
131                                          descrB,
132                                          nnzB,
133                                          dcsrRowPtrB,
134                                          dcsrColIndB,
135                                          descrD,
136                                          nnzD,
137                                          dcsrRowPtrD,
138                                          dcsrColIndD,
139                                          descrC,
140                                          dcsrRowPtrC,
141                                          &nnzC,
142                                          info,
143                                          dbuffer));
144
145    int*   dcsrColIndC = nullptr;
146    float* dcsrValC    = nullptr;
147    HIP_CHECK(hipMalloc((void**)&dcsrColIndC, sizeof(int) * nnzC));
148    HIP_CHECK(hipMalloc((void**)&dcsrValC, sizeof(float) * nnzC));
149
150    HIPSPARSE_CHECK(hipsparseScsrgemm2(handle,
151                                       m,
152                                       n,
153                                       k,
154                                       &alpha,
155                                       descrA,
156                                       nnzA,
157                                       dcsrValA,
158                                       dcsrRowPtrA,
159                                       dcsrColIndA,
160                                       descrB,
161                                       nnzB,
162                                       dcsrValB,
163                                       dcsrRowPtrB,
164                                       dcsrColIndB,
165                                       &beta,
166                                       descrD,
167                                       nnzD,
168                                       dcsrValD,
169                                       dcsrRowPtrD,
170                                       dcsrColIndD,
171                                       descrC,
172                                       dcsrValC,
173                                       dcsrRowPtrC,
174                                       dcsrColIndC,
175                                       info,
176                                       dbuffer));
177
178    std::vector<int>   hcsrRowPtrC(m + 1);
179    std::vector<int>   hcsrColIndC(nnzC);
180    std::vector<float> hcsrValC(nnzC);
181
182    // Copy back to the host
183    HIP_CHECK(
184        hipMemcpy(hcsrRowPtrC.data(), dcsrRowPtrC, sizeof(int) * (m + 1), hipMemcpyDeviceToHost));
185    HIP_CHECK(
186        hipMemcpy(hcsrColIndC.data(), dcsrColIndC, sizeof(int) * nnzC, hipMemcpyDeviceToHost));
187    HIP_CHECK(hipMemcpy(hcsrValC.data(), dcsrValC, sizeof(float) * nnzC, hipMemcpyDeviceToHost));
188
189    std::cout << "C" << std::endl;
190    for(int i = 0; i < m; i++)
191    {
192        int start = hcsrRowPtrC[i];
193        int end   = hcsrRowPtrC[i + 1];
194
195        std::vector<float> temp(n, 0.0f);
196        for(int j = start; j < end; j++)
197        {
198            temp[hcsrColIndC[j]] = hcsrValC[j];
199        }
200
201        for(int j = 0; j < n; j++)
202        {
203            std::cout << temp[j] << " ";
204        }
205        std::cout << std::endl;
206    }
207    std::cout << std::endl;
208
209    HIP_CHECK(hipFree(dcsrRowPtrA));
210    HIP_CHECK(hipFree(dcsrColIndA));
211    HIP_CHECK(hipFree(dcsrValA));
212    HIP_CHECK(hipFree(dcsrRowPtrB));
213    HIP_CHECK(hipFree(dcsrColIndB));
214    HIP_CHECK(hipFree(dcsrValB));
215    HIP_CHECK(hipFree(dcsrRowPtrC));
216    HIP_CHECK(hipFree(dcsrColIndC));
217    HIP_CHECK(hipFree(dcsrValC));
218    HIP_CHECK(hipFree(dcsrRowPtrD));
219    HIP_CHECK(hipFree(dcsrColIndD));
220    HIP_CHECK(hipFree(dcsrValD));
221
222    HIP_CHECK(hipFree(dbuffer));
223
224    HIPSPARSE_CHECK(hipsparseDestroyMatDescr(descrA));
225    HIPSPARSE_CHECK(hipsparseDestroyMatDescr(descrB));
226    HIPSPARSE_CHECK(hipsparseDestroyMatDescr(descrC));
227    HIPSPARSE_CHECK(hipsparseDestroyMatDescr(descrD));
228    HIPSPARSE_CHECK(hipsparseDestroyCsrgemm2Info(info));
229
230    HIPSPARSE_CHECK(hipsparseDestroy(handle));
231
232    return 0;
233}