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()#
-
hipsparseStatus_t hipsparseSaxpyi(hipsparseHandle_t handle, int nnz, const float *alpha, const float *xVal, const int *xInd, float *y, hipsparseIndexBase_t idxBase)#
-
hipsparseStatus_t hipsparseDaxpyi(hipsparseHandle_t handle, int nnz, const double *alpha, const double *xVal, const int *xInd, double *y, hipsparseIndexBase_t idxBase)#
-
hipsparseStatus_t hipsparseCaxpyi(hipsparseHandle_t handle, int nnz, const hipComplex *alpha, const hipComplex *xVal, const int *xInd, hipComplex *y, hipsparseIndexBase_t idxBase)#
-
hipsparseStatus_t hipsparseZaxpyi(hipsparseHandle_t handle, int nnz, const hipDoubleComplex *alpha, const hipDoubleComplex *xVal, const int *xInd, hipDoubleComplex *y, hipsparseIndexBase_t idxBase)#
Scale a sparse vector and add it to a dense vector.
hipsparseXaxpyimultiplies the sparse vector \(x\) with scalar \(\alpha\) and adds the result to the dense vector \(y\), such that\[ y := y + \alpha \cdot x \]for(i = 0; i < nnz; ++i) { y[xInd[i]] = y[xInd[i]] + alpha * xVal[i]; }
- Deprecated:
This function is deprecated when using the CUDA backend (CUDA 11.0+) and will be removed in CUDA 12.0. This deprecation does not apply to the ROCm backend.
Note
This function is non blocking and executed asynchronously with respect to the host. It may return before the actual computation has finished.
Note
If
nnzis zero, the function returns successfully without modifyingy. Duplicate indices inxIndwill result in the corresponding values being added multiple times to the same location iny.- Parameters:
handle – [in] handle to the hipsparse library context queue.
nnz – [in] number of non-zero entries of vector \(x\). Must be non-negative.
alpha – [in] scalar \(\alpha\).
xVal – [in] array of
nnzelements containing the values of \(x\).xInd – [in] array of
nnzelements containing the indices of the non-zero values of \(x\).y – [inout] array of values in dense format. Must be pre-allocated with sufficient size to accommodate all indices specified in
xInd.idxBase – [in] index base. HIPSPARSE_INDEX_BASE_ZERO for zero-based indexing or HIPSPARSE_INDEX_BASE_ONE for one-based indexing.
- Return values:
HIPSPARSE_STATUS_SUCCESS – the operation completed successfully.
HIPSPARSE_STATUS_NOT_INITIALIZED –
handleis not initialized.HIPSPARSE_STATUS_INVALID_VALUE –
handleis nullptr,nnzis negative,alpha,xVal,xIndoryis nullptr whennnzis greater than zero, oridxBaseis neither HIPSPARSE_INDEX_BASE_ZERO nor HIPSPARSE_INDEX_BASE_ONE.
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}
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 int hxInd[] = {0, 3, 5};
11
12 // Sparse value vector
13 double hxVal[] = {1.0, 2.0, 3.0};
14
15 // Dense vector
16 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, sizeof(int) * nnz, hipMemcpyHostToDevice));
34 HIP_CHECK(hipMemcpy(dxVal, hxVal, sizeof(double) * nnz, hipMemcpyHostToDevice));
35 HIP_CHECK(hipMemcpy(dy, hy, 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, dy, sizeof(double) * size, hipMemcpyDeviceToHost));
46
47 printf("hy\n");
48 for(int i = 0; i < size; i++)
49 {
50 printf("%f ", hy[i]);
51 }
52 printf("\n");
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}
1program example_fortran_axpyi
2 use iso_c_binding
3 use hipsparse
4
5 implicit none
6
7 interface
8 function hipMalloc(ptr, size) &
9 bind(c, name = 'hipMalloc')
10 use iso_c_binding
11 implicit none
12 integer :: hipMalloc
13 type(c_ptr) :: ptr
14 integer(c_size_t), value :: size
15 end function hipMalloc
16
17 function hipFree(ptr) &
18 bind(c, name = 'hipFree')
19 use iso_c_binding
20 implicit none
21 integer :: hipFree
22 type(c_ptr), value :: ptr
23 end function hipFree
24
25 function hipMemcpy(dst, src, size, kind) &
26 bind(c, name = 'hipMemcpy')
27 use iso_c_binding
28 implicit none
29 integer :: hipMemcpy
30 type(c_ptr), value :: dst
31 type(c_ptr), intent(in), value :: src
32 integer(c_size_t), value :: size
33 integer(c_int), value :: kind
34 end function hipMemcpy
35
36 function hipDeviceSynchronize() &
37 bind(c, name = 'hipDeviceSynchronize')
38 use iso_c_binding
39 implicit none
40 integer :: hipDeviceSynchronize
41 end function hipDeviceSynchronize
42
43 function hipDeviceReset() &
44 bind(c, name = 'hipDeviceReset')
45 use iso_c_binding
46 implicit none
47 integer :: hipDeviceReset
48 end function hipDeviceReset
49 end interface
50
51 integer, target :: h_xind(3)
52 real(8), target :: h_xval(3), h_y(9)
53
54 type(c_ptr) :: d_xind
55 type(c_ptr) :: d_xval
56 type(c_ptr) :: d_y
57
58 integer :: i
59 integer(c_int) :: size, nnz
60
61 real(c_double), target :: alpha
62
63 type(c_ptr) :: handle
64
65 integer :: version
66
67! Input data
68
69! Number of entries in the dense vector
70 size = 9
71
72! Number of non-zero entries
73 nnz = 3
74
75! Fill structures
76 h_xind = (/0, 3, 5/)
77 h_xval = (/1.0, 2.0, 3.0/)
78 h_y = (/1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0/)
79
80! Scalar alpha
81 alpha = 3.7
82
83! Allocate device memory
84 call HIP_CHECK(hipMalloc(d_xind, int(nnz, c_size_t) * 4))
85 call HIP_CHECK(hipMalloc(d_xval, int(nnz, c_size_t) * 8))
86 call HIP_CHECK(hipMalloc(d_y, int(size, c_size_t) * 8))
87
88! Copy host data to device
89 call HIP_CHECK(hipMemcpy(d_xind, c_loc(h_xind), int(nnz, c_size_t) * 4, 1))
90 call HIP_CHECK(hipMemcpy(d_xval, c_loc(h_xval), int(nnz, c_size_t) * 8, 1))
91 call HIP_CHECK(hipMemcpy(d_y, c_loc(h_y), int(size, c_size_t) * 8, 1))
92
93! Create hipSPARSE handle
94 call HIPSPARSE_CHECK(hipsparseCreate(handle))
95
96! Get hipSPARSE version
97 call HIPSPARSE_CHECK(hipsparseGetVersion(handle, version))
98
99! Print version on screen
100 write(*,fmt='(A,I0,A,I0,A,I0)') 'hipSPARSE version: ', version / 100000, '.', &
101 mod(version / 100, 1000), '.', mod(version, 100)
102
103! Call daxpyi to perform y = y + alpha * x
104 call HIPSPARSE_CHECK(hipsparseDaxpyi(handle, &
105 nnz, &
106 c_loc(alpha), &
107 d_xval, &
108 d_xind, &
109 d_y, &
110 HIPSPARSE_INDEX_BASE_ZERO))
111
112! Print result
113 call HIP_CHECK(hipMemcpy(c_loc(h_y), d_y, int(size, c_size_t) * 8, 2))
114
115 write(*,fmt='(A)',advance='no') 'hy:'
116 do i = 1, size
117 write(*,fmt='(A,F0.2)',advance='no') ' ', h_y(i)
118 end do
119 write(*,*)
120
121! Clear hipSPARSE
122 call HIPSPARSE_CHECK(hipsparseDestroy(handle))
123
124! Clear device memory
125 call HIP_CHECK(hipFree(d_xind))
126 call HIP_CHECK(hipFree(d_xval))
127 call HIP_CHECK(hipFree(d_y))
128
129end program example_fortran_axpyi
hipsparseXdoti()#
-
hipsparseStatus_t hipsparseSdoti(hipsparseHandle_t handle, int nnz, const float *xVal, const int *xInd, const float *y, float *result, hipsparseIndexBase_t idxBase)#
-
hipsparseStatus_t hipsparseDdoti(hipsparseHandle_t handle, int nnz, const double *xVal, const int *xInd, const double *y, double *result, hipsparseIndexBase_t idxBase)#
-
hipsparseStatus_t hipsparseCdoti(hipsparseHandle_t handle, int nnz, const hipComplex *xVal, const int *xInd, const hipComplex *y, hipComplex *result, hipsparseIndexBase_t idxBase)#
-
hipsparseStatus_t hipsparseZdoti(hipsparseHandle_t handle, int nnz, const hipDoubleComplex *xVal, const int *xInd, const hipDoubleComplex *y, hipDoubleComplex *result, hipsparseIndexBase_t idxBase)#
Compute the dot product of a sparse vector with a dense vector.
hipsparseXdoticomputes the dot product of the sparse vector \(x\) with the dense vector \(y\), such that\[ result := y^T x \]result = 0 for(i = 0; i < nnz; ++i) { result += xVal[i] * y[xInd[i]]; }
- Deprecated:
This function is deprecated when using the CUDA backend (CUDA 10.0+) and will be removed in CUDA 11.0. This deprecation does not apply to the ROCm backend.
Note
This function is non blocking and executed asynchronously with respect to the host. It may return before the actual computation has finished.
Note
If
nnzis zero, the function returns successfully with result set to zero.- Parameters:
handle – [in] handle to the hipsparse library context queue.
nnz – [in] number of non-zero entries of vector \(x\). Must be non-negative.
xVal – [in] array of
nnzvalues containing the elements of \(x\).xInd – [in] array of
nnzelements containing the indices of the non-zero values of \(x\).y – [in] array of values in dense format. Must be pre-allocated with sufficient size to accommodate all indices specified in
xInd.result – [out] pointer to the result, can be host or device memory.
idxBase – [in] index base. HIPSPARSE_INDEX_BASE_ZERO for zero-based indexing or HIPSPARSE_INDEX_BASE_ONE for one-based indexing.
- Return values:
HIPSPARSE_STATUS_SUCCESS – the operation completed successfully.
HIPSPARSE_STATUS_NOT_INITIALIZED –
handleis not initialized.HIPSPARSE_STATUS_INVALID_VALUE –
handleorresultis nullptr,nnzis negative,xVal,xIndoryis nullptr whennnzis greater than zero, oridxBaseis neither HIPSPARSE_INDEX_BASE_ZERO nor HIPSPARSE_INDEX_BASE_ONE.HIPSPARSE_STATUS_ALLOC_FAILED – the buffer for the dot product reduction could not be allocated.
HIPSPARSE_STATUS_INTERNAL_ERROR – an internal error occurred.
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}
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 int hxInd[] = {0, 3, 5};
11
12 // Sparse value vector
13 float hxVal[] = {1.0, 2.0, 3.0};
14
15 // Dense vector
16 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, sizeof(int) * nnz, hipMemcpyHostToDevice));
31 HIP_CHECK(hipMemcpy(dxVal, hxVal, sizeof(float) * nnz, hipMemcpyHostToDevice));
32 HIP_CHECK(hipMemcpy(dy, hy, 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 printf("dot: %f\n", (double)dot);
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}
1program example_fortran_doti
2 use iso_c_binding
3 use hipsparse
4
5 implicit none
6
7 interface
8 function hipMalloc(ptr, size) &
9 bind(c, name = 'hipMalloc')
10 use iso_c_binding
11 implicit none
12 integer :: hipMalloc
13 type(c_ptr) :: ptr
14 integer(c_size_t), value :: size
15 end function hipMalloc
16
17 function hipFree(ptr) &
18 bind(c, name = 'hipFree')
19 use iso_c_binding
20 implicit none
21 integer :: hipFree
22 type(c_ptr), value :: ptr
23 end function hipFree
24
25 function hipMemcpy(dst, src, size, kind) &
26 bind(c, name = 'hipMemcpy')
27 use iso_c_binding
28 implicit none
29 integer :: hipMemcpy
30 type(c_ptr), value :: dst
31 type(c_ptr), intent(in), value :: src
32 integer(c_size_t), value :: size
33 integer(c_int), value :: kind
34 end function hipMemcpy
35 end interface
36
37 integer, target :: h_xind(3)
38 real(4), target :: h_xval(3), h_y(9)
39
40 type(c_ptr) :: d_xind
41 type(c_ptr) :: d_xval
42 type(c_ptr) :: d_y
43
44 integer(c_int) :: size, nnz
45 real(c_float), target :: dot
46
47 type(c_ptr) :: handle
48
49! Input data
50
51! Number of entries in the dense vector
52 size = 9
53
54! Number of non-zero entries
55 nnz = 3
56
57! Fill structures
58 h_xind = (/0, 3, 5/)
59 h_xval = (/1.0, 2.0, 3.0/)
60 h_y = (/1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0/)
61
62! Allocate device memory
63 call HIP_CHECK(hipMalloc(d_xind, int(nnz, c_size_t) * 4))
64 call HIP_CHECK(hipMalloc(d_xval, int(nnz, c_size_t) * 4))
65 call HIP_CHECK(hipMalloc(d_y, int(size, c_size_t) * 4))
66
67! Copy host data to device
68 call HIP_CHECK(hipMemcpy(d_xind, c_loc(h_xind), int(nnz, c_size_t) * 4, 1))
69 call HIP_CHECK(hipMemcpy(d_xval, c_loc(h_xval), int(nnz, c_size_t) * 4, 1))
70 call HIP_CHECK(hipMemcpy(d_y, c_loc(h_y), int(size, c_size_t) * 4, 1))
71
72! Create hipSPARSE handle
73 call HIPSPARSE_CHECK(hipsparseCreate(handle))
74
75! Call sdoti to compute the dot product
76 call HIPSPARSE_CHECK(hipsparseSdoti(handle, &
77 nnz, &
78 d_xval, &
79 d_xind, &
80 d_y, &
81 c_loc(dot), &
82 HIPSPARSE_INDEX_BASE_ZERO))
83
84 write(*,fmt='(A,F0.2)') 'dot: ', dot
85
86! Clear hipSPARSE
87 call HIPSPARSE_CHECK(hipsparseDestroy(handle))
88
89! Clear device memory
90 call HIP_CHECK(hipFree(d_xind))
91 call HIP_CHECK(hipFree(d_xval))
92 call HIP_CHECK(hipFree(d_y))
93
94end program example_fortran_doti
hipsparseXdotci()#
-
hipsparseStatus_t hipsparseCdotci(hipsparseHandle_t handle, int nnz, const hipComplex *xVal, const int *xInd, const hipComplex *y, hipComplex *result, hipsparseIndexBase_t idxBase)#
-
hipsparseStatus_t hipsparseZdotci(hipsparseHandle_t handle, int nnz, const hipDoubleComplex *xVal, const int *xInd, const hipDoubleComplex *y, hipDoubleComplex *result, hipsparseIndexBase_t idxBase)#
Compute the dot product of a complex conjugate sparse vector with a dense vector.
hipsparseXdotcicomputes the dot product of the complex conjugate sparse vector \(x\) with the dense vector \(y\), such that\[ result := \bar{x}^H y \]result = 0 for(i = 0; i < nnz; ++i) { result += conj(xVal[i]) * y[xInd[i]]; }
- Deprecated:
This function is deprecated when using the CUDA backend (CUDA 10.0+) and will be removed in CUDA 11.0. This deprecation does not apply to the ROCm backend.
Note
This function is non blocking and executed asynchronously with respect to the host. It may return before the actual computation has finished.
Note
If
nnzis zero, the function returns successfully with result set to zero.- Parameters:
handle – [in] handle to the hipsparse library context queue.
nnz – [in] number of non-zero entries of vector \(x\). Must be non-negative.
xVal – [in] array of
nnzvalues containing the elements of \(x\).xInd – [in] array of
nnzelements containing the indices of the non-zero values of \(x\).y – [in] array of values in dense format. Must be pre-allocated with sufficient size to accommodate all indices specified in
xInd.result – [out] pointer to the result, can be host or device memory.
idxBase – [in] index base. HIPSPARSE_INDEX_BASE_ZERO for zero-based indexing or HIPSPARSE_INDEX_BASE_ONE for one-based indexing.
- Return values:
HIPSPARSE_STATUS_SUCCESS – the operation completed successfully.
HIPSPARSE_STATUS_NOT_INITIALIZED –
handleis not initialized.HIPSPARSE_STATUS_INVALID_VALUE –
handleorresultis nullptr,nnzis negative,xVal,xIndoryis nullptr whennnzis greater than zero, oridxBaseis neither HIPSPARSE_INDEX_BASE_ZERO nor HIPSPARSE_INDEX_BASE_ONE.HIPSPARSE_STATUS_ALLOC_FAILED – the buffer for the dot product reduction could not be allocated.
HIPSPARSE_STATUS_INTERNAL_ERROR – an internal error occurred.
hipsparseXgthr()#
-
hipsparseStatus_t hipsparseSgthr(hipsparseHandle_t handle, int nnz, const float *y, float *xVal, const int *xInd, hipsparseIndexBase_t idxBase)#
-
hipsparseStatus_t hipsparseDgthr(hipsparseHandle_t handle, int nnz, const double *y, double *xVal, const int *xInd, hipsparseIndexBase_t idxBase)#
-
hipsparseStatus_t hipsparseCgthr(hipsparseHandle_t handle, int nnz, const hipComplex *y, hipComplex *xVal, const int *xInd, hipsparseIndexBase_t idxBase)#
-
hipsparseStatus_t hipsparseZgthr(hipsparseHandle_t handle, int nnz, const hipDoubleComplex *y, hipDoubleComplex *xVal, const int *xInd, hipsparseIndexBase_t idxBase)#
Gather elements from a dense vector and store them into a sparse vector.
hipsparseXgthrgathers the elements that are listed inxIndfrom the dense vector \(y\) and stores them in the sparse vector \(x\).for(i = 0; i < nnz; ++i) { xVal[i] = y[xInd[i]]; }
- Deprecated:
This function is deprecated when using the CUDA backend (CUDA 11.0+) and will be removed in CUDA 12.0. This deprecation does not apply to the ROCm backend.
Note
This function is non blocking and executed asynchronously with respect to the host. It may return before the actual computation has finished.
Note
If
nnzis zero, the function returns successfully without modifyingxVal.- Parameters:
handle – [in] handle to the hipsparse library context queue.
nnz – [in] number of non-zero entries of \(x\). Must be non-negative.
y – [in] array of values in dense format. Must be pre-allocated with sufficient size to accommodate all indices specified in
xInd.xVal – [out] array of
nnzelements that will contain the gathered values of \(x\).xInd – [in] array of
nnzelements containing the indices of the non-zero values of \(x\).idxBase – [in] index base. HIPSPARSE_INDEX_BASE_ZERO for zero-based indexing or HIPSPARSE_INDEX_BASE_ONE for one-based indexing.
- Return values:
HIPSPARSE_STATUS_SUCCESS – the operation completed successfully.
HIPSPARSE_STATUS_NOT_INITIALIZED –
handleis not initialized.HIPSPARSE_STATUS_INVALID_VALUE –
handleis nullptr,nnzis negative,y,xValorxIndis nullptr whennnzis greater than zero, oridxBaseis neither HIPSPARSE_INDEX_BASE_ZERO nor HIPSPARSE_INDEX_BASE_ONE.
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}
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 int hxInd[] = {0, 3, 5};
11
12 // Sparse value vector
13 float hxVal[nnz];
14
15 // Dense vector
16 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, sizeof(int) * nnz, hipMemcpyHostToDevice));
31 HIP_CHECK(hipMemcpy(dy, hy, 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, dxVal, sizeof(float) * nnz, hipMemcpyDeviceToHost));
42
43 printf("hxVal\n");
44 for(int i = 0; i < nnz; i++)
45 {
46 printf("%f ", hxVal[i]);
47 }
48 printf("\n");
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}
1program example_fortran_gthr
2 use iso_c_binding
3 use hipsparse
4
5 implicit none
6
7 interface
8 function hipMalloc(ptr, size) &
9 bind(c, name = 'hipMalloc')
10 use iso_c_binding
11 implicit none
12 integer :: hipMalloc
13 type(c_ptr) :: ptr
14 integer(c_size_t), value :: size
15 end function hipMalloc
16
17 function hipFree(ptr) &
18 bind(c, name = 'hipFree')
19 use iso_c_binding
20 implicit none
21 integer :: hipFree
22 type(c_ptr), value :: ptr
23 end function hipFree
24
25 function hipMemcpy(dst, src, size, kind) &
26 bind(c, name = 'hipMemcpy')
27 use iso_c_binding
28 implicit none
29 integer :: hipMemcpy
30 type(c_ptr), value :: dst
31 type(c_ptr), intent(in), value :: src
32 integer(c_size_t), value :: size
33 integer(c_int), value :: kind
34 end function hipMemcpy
35 end interface
36
37 integer, target :: h_xind(3)
38 real(4), target :: h_xval(3), h_y(9)
39
40 type(c_ptr) :: d_xind
41 type(c_ptr) :: d_xval
42 type(c_ptr) :: d_y
43
44 integer :: i
45 integer(c_int) :: size, nnz
46
47 type(c_ptr) :: handle
48
49! Input data
50
51! Number of entries in the dense vector
52 size = 9
53
54! Number of non-zero entries
55 nnz = 3
56
57! Fill structures
58 h_xind = (/0, 3, 5/)
59 h_y = (/1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0/)
60
61! Allocate device memory
62 call HIP_CHECK(hipMalloc(d_xind, int(nnz, c_size_t) * 4))
63 call HIP_CHECK(hipMalloc(d_xval, int(nnz, c_size_t) * 4))
64 call HIP_CHECK(hipMalloc(d_y, int(size, c_size_t) * 4))
65
66! Copy host data to device
67 call HIP_CHECK(hipMemcpy(d_xind, c_loc(h_xind), int(nnz, c_size_t) * 4, 1))
68 call HIP_CHECK(hipMemcpy(d_y, c_loc(h_y), int(size, c_size_t) * 4, 1))
69
70! Create hipSPARSE handle
71 call HIPSPARSE_CHECK(hipsparseCreate(handle))
72
73! Call sgthr
74 call HIPSPARSE_CHECK(hipsparseSgthr(handle, &
75 nnz, &
76 d_y, &
77 d_xval, &
78 d_xind, &
79 HIPSPARSE_INDEX_BASE_ZERO))
80
81! Copy result back to host
82 call HIP_CHECK(hipMemcpy(c_loc(h_xval), d_xval, int(nnz, c_size_t) * 4, 2))
83
84 write(*,fmt='(A)',advance='no') 'hxVal:'
85 do i = 1, nnz
86 write(*,fmt='(A,F0.2)',advance='no') ' ', h_xval(i)
87 end do
88 write(*,*)
89
90! Clear hipSPARSE
91 call HIPSPARSE_CHECK(hipsparseDestroy(handle))
92
93! Clear device memory
94 call HIP_CHECK(hipFree(d_xind))
95 call HIP_CHECK(hipFree(d_xval))
96 call HIP_CHECK(hipFree(d_y))
97
98end program example_fortran_gthr
hipsparseXgthrz()#
-
hipsparseStatus_t hipsparseSgthrz(hipsparseHandle_t handle, int nnz, float *y, float *xVal, const int *xInd, hipsparseIndexBase_t idxBase)#
-
hipsparseStatus_t hipsparseDgthrz(hipsparseHandle_t handle, int nnz, double *y, double *xVal, const int *xInd, hipsparseIndexBase_t idxBase)#
-
hipsparseStatus_t hipsparseCgthrz(hipsparseHandle_t handle, int nnz, hipComplex *y, hipComplex *xVal, const int *xInd, hipsparseIndexBase_t idxBase)#
-
hipsparseStatus_t hipsparseZgthrz(hipsparseHandle_t handle, int nnz, hipDoubleComplex *y, hipDoubleComplex *xVal, const int *xInd, hipsparseIndexBase_t idxBase)#
Gather and zero out elements from a dense vector and store them into a sparse vector.
hipsparseXgthrzgathers the elements that are listed inxIndfrom the dense vector \(y\) and stores them in the sparse vector \(x\). The gathered elements in \(y\) are replaced by zero.for(i = 0; i < nnz; ++i) { xVal[i] = y[xInd[i]]; y[xInd[i]] = 0; }
- Deprecated:
This function is deprecated when using the CUDA backend (CUDA 11.0+) and will be removed in CUDA 12.0. This deprecation does not apply to the ROCm backend.
Note
This function is non blocking and executed asynchronously with respect to the host. It may return before the actual computation has finished.
Note
If
nnzis zero, the function returns successfully without modifyingxValory.- Parameters:
handle – [in] handle to the hipsparse library context queue.
nnz – [in] number of non-zero entries of \(x\). Must be non-negative.
y – [inout] array of values in dense format. Must be pre-allocated with sufficient size to accommodate all indices specified in
xInd. Gathered elements are set to zero.xVal – [out] array of
nnzelements that will contain the gathered values of \(x\).xInd – [in] array of
nnzelements containing the indices of the non-zero values of \(x\).idxBase – [in] index base. HIPSPARSE_INDEX_BASE_ZERO for zero-based indexing or HIPSPARSE_INDEX_BASE_ONE for one-based indexing.
- Return values:
HIPSPARSE_STATUS_SUCCESS – the operation completed successfully.
HIPSPARSE_STATUS_NOT_INITIALIZED –
handleis not initialized.HIPSPARSE_STATUS_INVALID_VALUE –
handleis nullptr,nnzis negative,y,xValorxIndis nullptr whennnzis greater than zero, oridxBaseis neither HIPSPARSE_INDEX_BASE_ZERO nor HIPSPARSE_INDEX_BASE_ONE.
hipsparseXroti()#
-
hipsparseStatus_t hipsparseSroti(hipsparseHandle_t handle, int nnz, float *xVal, const int *xInd, float *y, const float *c, const float *s, hipsparseIndexBase_t idxBase)#
-
hipsparseStatus_t hipsparseDroti(hipsparseHandle_t handle, int nnz, double *xVal, const int *xInd, double *y, const double *c, const double *s, hipsparseIndexBase_t idxBase)#
Apply Givens rotation to a dense and a sparse vector.
hipsparseXrotiapplies the Givens rotation matrix \(G\) to the sparse vector \(x\) and the dense vector \(y\), where\[\begin{split} G = \begin{pmatrix} c & s \\ -s & c \end{pmatrix} \end{split}\]for(i = 0; i < nnz; ++i) { x_tmp = xVal[i]; y_tmp = y[xInd[i]]; xVal[i] = c * x_tmp + s * y_tmp; y[xInd[i]] = c * y_tmp - s * x_tmp; }
- Deprecated:
This function is deprecated when using the CUDA backend (CUDA 11.0+) and will be removed in CUDA 12.0. This deprecation does not apply to the ROCm backend.
Note
This function is non blocking and executed asynchronously with respect to the host. It may return before the actual computation has finished.
Note
If
nnzis zero, the function returns successfully without modifyingxValory.- Parameters:
handle – [in] handle to the hipsparse library context queue.
nnz – [in] number of non-zero entries of \(x\). Must be non-negative.
xVal – [inout] array of
nnzelements containing the non-zero values of \(x\).xInd – [in] array of
nnzelements containing the indices of the non-zero values of \(x\).y – [inout] array of values in dense format. Must be pre-allocated with sufficient size to accommodate all indices specified in
xInd.c – [in] pointer to the cosine element of \(G\), can be on host or device.
s – [in] pointer to the sine element of \(G\), can be on host or device.
idxBase – [in] index base. HIPSPARSE_INDEX_BASE_ZERO for zero-based indexing or HIPSPARSE_INDEX_BASE_ONE for one-based indexing.
- Return values:
HIPSPARSE_STATUS_SUCCESS – the operation completed successfully.
HIPSPARSE_STATUS_NOT_INITIALIZED –
handleis not initialized.HIPSPARSE_STATUS_INVALID_VALUE –
handle,corsis nullptr,nnzis negative,xVal,xIndoryis nullptr whennnzis greater than zero, oridxBaseis neither HIPSPARSE_INDEX_BASE_ZERO nor HIPSPARSE_INDEX_BASE_ONE.
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}
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 int hxInd[] = {0, 3, 5};
11
12 // Sparse value vector
13 float hxVal[] = {1.0, 2.0, 3.0};
14
15 // Dense vector
16 float hy[] = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0};
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, sizeof(int) * nnz, hipMemcpyHostToDevice));
35 HIP_CHECK(hipMemcpy(dxVal, hxVal, sizeof(float) * nnz, hipMemcpyHostToDevice));
36 HIP_CHECK(hipMemcpy(dy, hy, 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, dxVal, sizeof(float) * nnz, hipMemcpyDeviceToHost));
47 HIP_CHECK(hipMemcpy(hy, dy, sizeof(float) * size, hipMemcpyDeviceToHost));
48
49 printf("hxVal\n");
50 for(int i = 0; i < nnz; i++)
51 {
52 printf("%f ", hxVal[i]);
53 }
54 printf("\n");
55
56 printf("hy\n");
57 for(int i = 0; i < size; i++)
58 {
59 printf("%f ", hy[i]);
60 }
61 printf("\n");
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}
1program example_fortran_roti
2 use iso_c_binding
3 use hipsparse
4
5 implicit none
6
7 interface
8 function hipMalloc(ptr, size) &
9 bind(c, name = 'hipMalloc')
10 use iso_c_binding
11 implicit none
12 integer :: hipMalloc
13 type(c_ptr) :: ptr
14 integer(c_size_t), value :: size
15 end function hipMalloc
16
17 function hipFree(ptr) &
18 bind(c, name = 'hipFree')
19 use iso_c_binding
20 implicit none
21 integer :: hipFree
22 type(c_ptr), value :: ptr
23 end function hipFree
24
25 function hipMemcpy(dst, src, size, kind) &
26 bind(c, name = 'hipMemcpy')
27 use iso_c_binding
28 implicit none
29 integer :: hipMemcpy
30 type(c_ptr), value :: dst
31 type(c_ptr), intent(in), value :: src
32 integer(c_size_t), value :: size
33 integer(c_int), value :: kind
34 end function hipMemcpy
35 end interface
36
37 integer, target :: h_xind(3)
38 real(4), target :: h_xval(3), h_y(9)
39
40 type(c_ptr) :: d_xind
41 type(c_ptr) :: d_xval
42 type(c_ptr) :: d_y
43
44 integer :: i
45 integer(c_int) :: size, nnz
46
47 real(c_float), target :: c
48 real(c_float), target :: s
49
50 type(c_ptr) :: handle
51
52! Input data
53
54! Number of entries in the dense vector
55 size = 9
56
57! Number of non-zero entries
58 nnz = 3
59
60! Fill structures
61 h_xind = (/0, 3, 5/)
62 h_xval = (/1.0, 2.0, 3.0/)
63 h_y = (/1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0/)
64
65! c and s
66 c = 3.7
67 s = 1.3
68
69! Allocate device memory
70 call HIP_CHECK(hipMalloc(d_xind, int(nnz, c_size_t) * 4))
71 call HIP_CHECK(hipMalloc(d_xval, int(nnz, c_size_t) * 4))
72 call HIP_CHECK(hipMalloc(d_y, int(size, c_size_t) * 4))
73
74! Copy host data to device
75 call HIP_CHECK(hipMemcpy(d_xind, c_loc(h_xind), int(nnz, c_size_t) * 4, 1))
76 call HIP_CHECK(hipMemcpy(d_xval, c_loc(h_xval), int(nnz, c_size_t) * 4, 1))
77 call HIP_CHECK(hipMemcpy(d_y, c_loc(h_y), int(size, c_size_t) * 4, 1))
78
79! Create hipSPARSE handle
80 call HIPSPARSE_CHECK(hipsparseCreate(handle))
81
82! Call sroti
83 call HIPSPARSE_CHECK(hipsparseSroti(handle, &
84 nnz, &
85 d_xval, &
86 d_xind, &
87 d_y, &
88 c_loc(c), &
89 c_loc(s), &
90 HIPSPARSE_INDEX_BASE_ZERO))
91
92! Copy results back to host
93 call HIP_CHECK(hipMemcpy(c_loc(h_xval), d_xval, int(nnz, c_size_t) * 4, 2))
94 call HIP_CHECK(hipMemcpy(c_loc(h_y), d_y, int(size, c_size_t) * 4, 2))
95
96 do i = 1, nnz
97 write(*,fmt='(A,I0,A,F0.2)') 'x(', h_xind(i), ') = ', h_xval(i)
98 end do
99
100 write(*,fmt='(A)',advance='no') 'y:'
101 do i = 1, size
102 write(*,fmt='(A,F0.2)',advance='no') ' ', h_y(i)
103 end do
104 write(*,*)
105
106! Clear hipSPARSE
107 call HIPSPARSE_CHECK(hipsparseDestroy(handle))
108
109! Clear device memory
110 call HIP_CHECK(hipFree(d_xind))
111 call HIP_CHECK(hipFree(d_xval))
112 call HIP_CHECK(hipFree(d_y))
113
114end program example_fortran_roti
hipsparseXsctr()#
-
hipsparseStatus_t hipsparseSsctr(hipsparseHandle_t handle, int nnz, const float *xVal, const int *xInd, float *y, hipsparseIndexBase_t idxBase)#
-
hipsparseStatus_t hipsparseDsctr(hipsparseHandle_t handle, int nnz, const double *xVal, const int *xInd, double *y, hipsparseIndexBase_t idxBase)#
-
hipsparseStatus_t hipsparseCsctr(hipsparseHandle_t handle, int nnz, const hipComplex *xVal, const int *xInd, hipComplex *y, hipsparseIndexBase_t idxBase)#
-
hipsparseStatus_t hipsparseZsctr(hipsparseHandle_t handle, int nnz, const hipDoubleComplex *xVal, const int *xInd, hipDoubleComplex *y, hipsparseIndexBase_t idxBase)#
Scatter elements from a dense vector across a sparse vector.
hipsparseXsctrscatters the elements that are listed inxIndfrom the sparse vector \(x\) into the dense vector \(y\). Indices of \(y\) that are not listed inxIndremain unchanged.for(i = 0; i < nnz; ++i) { y[xInd[i]] = xVal[i]; }
- Deprecated:
This function is deprecated when using the CUDA backend (CUDA 11.0+) and will be removed in CUDA 12.0. This deprecation does not apply to the ROCm backend.
Note
This function is non blocking and executed asynchronously with respect to the host. It may return before the actual computation has finished.
Note
If
nnzis zero, the function returns successfully without modifyingy. Duplicate indices inxIndwill result in the last value being written toy.- Parameters:
handle – [in] handle to the hipsparse library context queue.
nnz – [in] number of non-zero entries of \(x\). Must be non-negative.
xVal – [in] array of
nnzelements containing the non-zero values of \(x\).xInd – [in] array of
nnzelements containing the indices of the non-zero values of \(x\).y – [inout] array of values in dense format. Must be pre-allocated with sufficient size to accommodate all indices specified in
xInd.idxBase – [in] index base. HIPSPARSE_INDEX_BASE_ZERO for zero-based indexing or HIPSPARSE_INDEX_BASE_ONE for one-based indexing.
- Return values:
HIPSPARSE_STATUS_SUCCESS – the operation completed successfully.
HIPSPARSE_STATUS_NOT_INITIALIZED –
handleis not initialized.HIPSPARSE_STATUS_INVALID_VALUE –
handleis nullptr,nnzis negative,xVal,xIndoryis nullptr whennnzis greater than zero, oridxBaseis neither HIPSPARSE_INDEX_BASE_ZERO nor HIPSPARSE_INDEX_BASE_ONE.
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}
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 int hxInd[] = {0, 3, 5};
11
12 // Sparse value vector
13 float hxVal[] = {9.0, 2.0, 3.0};
14
15 // Dense vector
16 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, sizeof(int) * nnz, hipMemcpyHostToDevice));
31 HIP_CHECK(hipMemcpy(dxVal, hxVal, sizeof(float) * nnz, hipMemcpyHostToDevice));
32 HIP_CHECK(hipMemcpy(dy, hy, 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, dy, sizeof(float) * size, hipMemcpyDeviceToHost));
43
44 printf("hy\n");
45 for(int i = 0; i < size; i++)
46 {
47 printf("%f ", hy[i]);
48 }
49 printf("\n");
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}
1program example_fortran_sctr
2 use iso_c_binding
3 use hipsparse
4
5 implicit none
6
7 interface
8 function hipMalloc(ptr, size) &
9 bind(c, name = 'hipMalloc')
10 use iso_c_binding
11 implicit none
12 integer :: hipMalloc
13 type(c_ptr) :: ptr
14 integer(c_size_t), value :: size
15 end function hipMalloc
16
17 function hipFree(ptr) &
18 bind(c, name = 'hipFree')
19 use iso_c_binding
20 implicit none
21 integer :: hipFree
22 type(c_ptr), value :: ptr
23 end function hipFree
24
25 function hipMemcpy(dst, src, size, kind) &
26 bind(c, name = 'hipMemcpy')
27 use iso_c_binding
28 implicit none
29 integer :: hipMemcpy
30 type(c_ptr), value :: dst
31 type(c_ptr), intent(in), value :: src
32 integer(c_size_t), value :: size
33 integer(c_int), value :: kind
34 end function hipMemcpy
35 end interface
36
37 integer, target :: h_xind(3)
38 real(4), target :: h_xval(3), h_y(9)
39
40 type(c_ptr) :: d_xind
41 type(c_ptr) :: d_xval
42 type(c_ptr) :: d_y
43
44 integer :: i
45 integer(c_int) :: size, nnz
46
47 type(c_ptr) :: handle
48
49! Input data
50
51! Number of entries in the dense vector
52 size = 9
53
54! Number of non-zero entries
55 nnz = 3
56
57! Fill structures
58 h_xind = (/0, 3, 5/)
59 h_xval = (/9.0, 2.0, 3.0/)
60 h_y = (/1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0/)
61
62! Allocate device memory
63 call HIP_CHECK(hipMalloc(d_xind, int(nnz, c_size_t) * 4))
64 call HIP_CHECK(hipMalloc(d_xval, int(nnz, c_size_t) * 4))
65 call HIP_CHECK(hipMalloc(d_y, int(size, c_size_t) * 4))
66
67! Copy host data to device
68 call HIP_CHECK(hipMemcpy(d_xind, c_loc(h_xind), int(nnz, c_size_t) * 4, 1))
69 call HIP_CHECK(hipMemcpy(d_xval, c_loc(h_xval), int(nnz, c_size_t) * 4, 1))
70 call HIP_CHECK(hipMemcpy(d_y, c_loc(h_y), int(size, c_size_t) * 4, 1))
71
72! Create hipSPARSE handle
73 call HIPSPARSE_CHECK(hipsparseCreate(handle))
74
75! Call ssctr
76 call HIPSPARSE_CHECK(hipsparseSsctr(handle, &
77 nnz, &
78 d_xval, &
79 d_xind, &
80 d_y, &
81 HIPSPARSE_INDEX_BASE_ZERO))
82
83! Copy result back to host
84 call HIP_CHECK(hipMemcpy(c_loc(h_y), d_y, int(size, c_size_t) * 4, 2))
85
86 write(*,fmt='(A)',advance='no') 'hy:'
87 do i = 1, size
88 write(*,fmt='(A,F0.2)',advance='no') ' ', h_y(i)
89 end do
90 write(*,*)
91
92! Clear hipSPARSE
93 call HIPSPARSE_CHECK(hipsparseDestroy(handle))
94
95! Clear device memory
96 call HIP_CHECK(hipFree(d_xind))
97 call HIP_CHECK(hipFree(d_xval))
98 call HIP_CHECK(hipFree(d_y))
99
100end program example_fortran_sctr