Sparse level 1 functions#

The sparse level 1 routines describe operations between a vector in sparse format and a vector in dense format. This section describes all hipSPARSE level 1 sparse linear algebra functions.

hipsparseXaxpyi()#

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    // Number of non-zeros of the sparse vector
 4    const int nnz = 3;
 5
 6    // Number of entries in the dense vector
 7    const int size = 9;
 8
 9    // Sparse index vector
10    std::vector<int> hxInd = {0, 3, 5};
11
12    // Sparse value vector
13    std::vector<double> hxVal = {1.0, 2.0, 3.0};
14
15    // Dense vector
16    std::vector<double> hy = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0};
17
18    // Scalar alpha
19    double alpha = 3.7;
20
21    // Index base
22    hipsparseIndexBase_t idxBase = HIPSPARSE_INDEX_BASE_ZERO;
23
24    // Offload data to device
25    int*    dxInd;
26    double* dxVal;
27    double* dy;
28
29    HIP_CHECK(hipMalloc((void**)&dxInd, sizeof(int) * nnz));
30    HIP_CHECK(hipMalloc((void**)&dxVal, sizeof(double) * nnz));
31    HIP_CHECK(hipMalloc((void**)&dy, sizeof(double) * size));
32
33    HIP_CHECK(hipMemcpy(dxInd, hxInd.data(), sizeof(int) * nnz, hipMemcpyHostToDevice));
34    HIP_CHECK(hipMemcpy(dxVal, hxVal.data(), sizeof(double) * nnz, hipMemcpyHostToDevice));
35    HIP_CHECK(hipMemcpy(dy, hy.data(), sizeof(double) * size, hipMemcpyHostToDevice));
36
37    // hipSPARSE handle
38    hipsparseHandle_t handle;
39    HIPSPARSE_CHECK(hipsparseCreate(&handle));
40
41    // Call daxpyi to perform y = y + alpha * x
42    HIPSPARSE_CHECK(hipsparseDaxpyi(handle, nnz, &alpha, dxVal, dxInd, dy, idxBase));
43
44    // Copy result back to host
45    HIP_CHECK(hipMemcpy(hy.data(), dy, sizeof(double) * size, hipMemcpyDeviceToHost));
46
47    std::cout << "hy" << std::endl;
48    for(int i = 0; i < size; i++)
49    {
50        std::cout << hy[i] << " ";
51    }
52    std::cout << std::endl;
53
54    // Clear hipSPARSE
55    HIPSPARSE_CHECK(hipsparseDestroy(handle));
56
57    // Clear device memory
58    HIP_CHECK(hipFree(dxInd));
59    HIP_CHECK(hipFree(dxVal));
60    HIP_CHECK(hipFree(dy));
61    return 0;
62}

hipsparseXdoti()#

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    // Number of non-zeros of the sparse vector
 4    const int nnz = 3;
 5
 6    // Number of entries in the dense vector
 7    const int size = 9;
 8
 9    // Sparse index vector
10    std::vector<int> hxInd = {0, 3, 5};
11
12    // Sparse value vector
13    std::vector<float> hxVal = {1.0f, 2.0f, 3.0f};
14
15    // Dense vector
16    std::vector<float> hy = {1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f, 9.0f};
17
18    // Index base
19    hipsparseIndexBase_t idxBase = HIPSPARSE_INDEX_BASE_ZERO;
20
21    // Offload data to device
22    int*   dxInd;
23    float* dxVal;
24    float* dy;
25
26    HIP_CHECK(hipMalloc((void**)&dxInd, sizeof(int) * nnz));
27    HIP_CHECK(hipMalloc((void**)&dxVal, sizeof(float) * nnz));
28    HIP_CHECK(hipMalloc((void**)&dy, sizeof(float) * size));
29
30    HIP_CHECK(hipMemcpy(dxInd, hxInd.data(), sizeof(int) * nnz, hipMemcpyHostToDevice));
31    HIP_CHECK(hipMemcpy(dxVal, hxVal.data(), sizeof(float) * nnz, hipMemcpyHostToDevice));
32    HIP_CHECK(hipMemcpy(dy, hy.data(), sizeof(float) * size, hipMemcpyHostToDevice));
33
34    // hipSPARSE handle
35    hipsparseHandle_t handle;
36    HIPSPARSE_CHECK(hipsparseCreate(&handle));
37
38    // Call sdoti to compute the dot product
39    float dot;
40    HIPSPARSE_CHECK(hipsparseSdoti(handle, nnz, dxVal, dxInd, dy, &dot, idxBase));
41
42    std::cout << "dot: " << dot << std::endl;
43
44    // Clear hipSPARSE
45    HIPSPARSE_CHECK(hipsparseDestroy(handle));
46
47    // Clear device memory
48    HIP_CHECK(hipFree(dxInd));
49    HIP_CHECK(hipFree(dxVal));
50    HIP_CHECK(hipFree(dy));
51
52    return 0;
53}

hipsparseXdotci()#

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

hipsparseXgthr()#

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    // Number of non-zeros of the sparse vector
 4    const int nnz = 3;
 5
 6    // Number of entries in the dense vector
 7    const int size = 9;
 8
 9    // Sparse index vector
10    std::vector<int> hxInd = {0, 3, 5};
11
12    // Sparse value vector
13    std::vector<float> hxVal(nnz);
14
15    // Dense vector
16    std::vector<float> hy = {1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f, 9.0f};
17
18    // Index base
19    hipsparseIndexBase_t idxBase = HIPSPARSE_INDEX_BASE_ZERO;
20
21    // Offload data to device
22    int*   dxInd;
23    float* dxVal;
24    float* dy;
25
26    HIP_CHECK(hipMalloc((void**)&dxInd, sizeof(int) * nnz));
27    HIP_CHECK(hipMalloc((void**)&dxVal, sizeof(float) * nnz));
28    HIP_CHECK(hipMalloc((void**)&dy, sizeof(float) * size));
29
30    HIP_CHECK(hipMemcpy(dxInd, hxInd.data(), sizeof(int) * nnz, hipMemcpyHostToDevice));
31    HIP_CHECK(hipMemcpy(dy, hy.data(), sizeof(float) * size, hipMemcpyHostToDevice));
32
33    // hipSPARSE handle
34    hipsparseHandle_t handle;
35    HIPSPARSE_CHECK(hipsparseCreate(&handle));
36
37    // Call sgthr
38    HIPSPARSE_CHECK(hipsparseSgthr(handle, nnz, dy, dxVal, dxInd, idxBase));
39
40    // Copy result back to host
41    HIP_CHECK(hipMemcpy(hxVal.data(), dxVal, sizeof(float) * nnz, hipMemcpyDeviceToHost));
42
43    std::cout << "hxVal" << std::endl;
44    for(int i = 0; i < nnz; i++)
45    {
46        std::cout << hxVal[i] << " ";
47    }
48    std::cout << std::endl;
49
50    // Clear hipSPARSE
51    HIPSPARSE_CHECK(hipsparseDestroy(handle));
52
53    // Clear device memory
54    HIP_CHECK(hipFree(dxInd));
55    HIP_CHECK(hipFree(dxVal));
56    HIP_CHECK(hipFree(dy));
57
58    return 0;
59}

hipsparseXgthrz()#

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

hipsparseXroti()#

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    // Number of non-zeros of the sparse vector
 4    const int nnz = 3;
 5
 6    // Number of entries in the dense vector
 7    const int size = 9;
 8
 9    // Sparse index vector
10    std::vector<int> hxInd = {0, 3, 5};
11
12    // Sparse value vector
13    std::vector<float> hxVal = {1.0f, 2.0f, 3.0f};
14
15    // Dense vector
16    std::vector<float> hy = {1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f, 9.0f};
17
18    // c and s
19    float c = 3.7;
20    float s = 1.3;
21
22    // Index base
23    hipsparseIndexBase_t idxBase = HIPSPARSE_INDEX_BASE_ZERO;
24
25    // Offload data to device
26    int*   dxInd;
27    float* dxVal;
28    float* dy;
29
30    HIP_CHECK(hipMalloc((void**)&dxInd, sizeof(int) * nnz));
31    HIP_CHECK(hipMalloc((void**)&dxVal, sizeof(float) * nnz));
32    HIP_CHECK(hipMalloc((void**)&dy, sizeof(float) * size));
33
34    HIP_CHECK(hipMemcpy(dxInd, hxInd.data(), sizeof(int) * nnz, hipMemcpyHostToDevice));
35    HIP_CHECK(hipMemcpy(dxVal, hxVal.data(), sizeof(float) * nnz, hipMemcpyHostToDevice));
36    HIP_CHECK(hipMemcpy(dy, hy.data(), sizeof(float) * size, hipMemcpyHostToDevice));
37
38    // hipSPARSE handle
39    hipsparseHandle_t handle;
40    HIPSPARSE_CHECK(hipsparseCreate(&handle));
41
42    // Call sroti
43    HIPSPARSE_CHECK(hipsparseSroti(handle, nnz, dxVal, dxInd, dy, &c, &s, idxBase));
44
45    // Copy result back to host
46    HIP_CHECK(hipMemcpy(hxVal.data(), dxVal, sizeof(float) * nnz, hipMemcpyDeviceToHost));
47    HIP_CHECK(hipMemcpy(hy.data(), dy, sizeof(float) * size, hipMemcpyDeviceToHost));
48
49    std::cout << "hxVal" << std::endl;
50    for(int i = 0; i < nnz; i++)
51    {
52        std::cout << hxVal[i] << " ";
53    }
54    std::cout << std::endl;
55
56    std::cout << "hy" << std::endl;
57    for(int i = 0; i < size; i++)
58    {
59        std::cout << hy[i] << " ";
60    }
61    std::cout << std::endl;
62
63    // Clear hipSPARSE
64    HIPSPARSE_CHECK(hipsparseDestroy(handle));
65
66    // Clear device memory
67    HIP_CHECK(hipFree(dxInd));
68    HIP_CHECK(hipFree(dxVal));
69    HIP_CHECK(hipFree(dy));
70
71    return 0;
72}

hipsparseXsctr()#

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    // Number of non-zeros of the sparse vector
 4    const int nnz = 3;
 5
 6    // Number of entries in the dense vector
 7    const int size = 9;
 8
 9    // Sparse index vector
10    std::vector<int> hxInd = {0, 3, 5};
11
12    // Sparse value vector
13    std::vector<float> hxVal = {9.0, 2.0, 3.0};
14
15    // Dense vector
16    std::vector<float> hy = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0};
17
18    // Index base
19    hipsparseIndexBase_t idxBase = HIPSPARSE_INDEX_BASE_ZERO;
20
21    // Offload data to device
22    int*   dxInd;
23    float* dxVal;
24    float* dy;
25
26    HIP_CHECK(hipMalloc((void**)&dxInd, sizeof(int) * nnz));
27    HIP_CHECK(hipMalloc((void**)&dxVal, sizeof(float) * nnz));
28    HIP_CHECK(hipMalloc((void**)&dy, sizeof(float) * size));
29
30    HIP_CHECK(hipMemcpy(dxInd, hxInd.data(), sizeof(int) * nnz, hipMemcpyHostToDevice));
31    HIP_CHECK(hipMemcpy(dxVal, hxVal.data(), sizeof(float) * nnz, hipMemcpyHostToDevice));
32    HIP_CHECK(hipMemcpy(dy, hy.data(), sizeof(float) * size, hipMemcpyHostToDevice));
33
34    // hipSPARSE handle
35    hipsparseHandle_t handle;
36    HIPSPARSE_CHECK(hipsparseCreate(&handle));
37
38    // Call ssctr
39    HIPSPARSE_CHECK(hipsparseSsctr(handle, nnz, dxVal, dxInd, dy, idxBase));
40
41    // Copy result back to host
42    HIP_CHECK(hipMemcpy(hy.data(), dy, sizeof(float) * size, hipMemcpyDeviceToHost));
43
44    std::cout << "hy" << std::endl;
45    for(int i = 0; i < size; i++)
46    {
47        std::cout << hy[i] << " ";
48    }
49    std::cout << std::endl;
50
51    // Clear hipSPARSE
52    HIPSPARSE_CHECK(hipsparseDestroy(handle));
53
54    // Clear device memory
55    HIP_CHECK(hipFree(dxInd));
56    HIP_CHECK(hipFree(dxVal));
57    HIP_CHECK(hipFree(dy));
58
59    return 0;
60}