Sparse level 2 functions#
This module contains all sparse level 2 routines.
The sparse level 2 routines describe operations between a matrix in sparse format and a vector in dense format.
hipsparseXcsrmv()#
Warning
doxygenfunction: Cannot find file: /home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-hipsparse/checkouts/7.13.0-preview/projects/hipsparse/docs/doxygen/xml/index.xml
Warning
doxygenfunction: Cannot find file: /home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-hipsparse/checkouts/7.13.0-preview/projects/hipsparse/docs/doxygen/xml/index.xml
Warning
doxygenfunction: Cannot find file: /home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-hipsparse/checkouts/7.13.0-preview/projects/hipsparse/docs/doxygen/xml/index.xml
Warning
doxygenfunction: Cannot find file: /home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-hipsparse/checkouts/7.13.0-preview/projects/hipsparse/docs/doxygen/xml/index.xml
1int main(int argc, char* argv[])
2{
3 // hipSPARSE handle
4 hipsparseHandle_t handle;
5 HIPSPARSE_CHECK(hipsparseCreate(&handle));
6
7 // alpha * ( 1.0 0.0 2.0 ) * ( 1.0 ) + beta * ( 4.0 ) = ( 31.1 )
8 // ( 3.0 0.0 4.0 ) * ( 2.0 ) ( 5.0 ) = ( 62.0 )
9 // ( 5.0 6.0 0.0 ) * ( 3.0 ) ( 6.0 ) = ( 70.7 )
10 // ( 7.0 0.0 8.0 ) * ( 7.0 ) = ( 123.8 )
11
12 const int m = 4;
13 const int n = 3;
14 const int nnz = 8;
15
16 // CSR row pointers
17 std::vector<int> hcsrRowPtr = {0, 2, 4, 6, 8};
18
19 // CSR column indices
20 std::vector<int> hcsrColInd = {0, 2, 0, 2, 0, 1, 0, 2};
21
22 // CSR values
23 std::vector<double> hcsrVal = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0};
24
25 // Transposition of the matrix
26 hipsparseOperation_t trans = HIPSPARSE_OPERATION_NON_TRANSPOSE;
27
28 // Scalar alpha and beta
29 double alpha = 3.7;
30 double beta = 1.3;
31
32 // x and y
33 std::vector<double> hx = {1.0, 2.0, 3.0};
34 std::vector<double> hy = {4.0, 5.0, 6.0, 7.0};
35
36 // Matrix descriptor
37 hipsparseMatDescr_t descr;
38 HIPSPARSE_CHECK(hipsparseCreateMatDescr(&descr));
39
40 // Offload data to device
41 int* dcsrRowPtr;
42 int* dcsrColInd;
43 double* dcsrVal;
44 double* dx;
45 double* dy;
46
47 HIP_CHECK(hipMalloc((void**)&dcsrRowPtr, sizeof(int) * (m + 1)));
48 HIP_CHECK(hipMalloc((void**)&dcsrColInd, sizeof(int) * nnz));
49 HIP_CHECK(hipMalloc((void**)&dcsrVal, sizeof(double) * nnz));
50 HIP_CHECK(hipMalloc((void**)&dx, sizeof(double) * n));
51 HIP_CHECK(hipMalloc((void**)&dy, sizeof(double) * m));
52
53 HIP_CHECK(
54 hipMemcpy(dcsrRowPtr, hcsrRowPtr.data(), sizeof(int) * (m + 1), hipMemcpyHostToDevice));
55 HIP_CHECK(hipMemcpy(dcsrColInd, hcsrColInd.data(), sizeof(int) * nnz, hipMemcpyHostToDevice));
56 HIP_CHECK(hipMemcpy(dcsrVal, hcsrVal.data(), sizeof(double) * nnz, hipMemcpyHostToDevice));
57 HIP_CHECK(hipMemcpy(dx, hx.data(), sizeof(double) * n, hipMemcpyHostToDevice));
58 HIP_CHECK(hipMemcpy(dy, hy.data(), sizeof(double) * m, hipMemcpyHostToDevice));
59
60 // Call dcsrmv to perform y = alpha * A x + beta * y
61 HIPSPARSE_CHECK(hipsparseDcsrmv(
62 handle, trans, m, n, nnz, &alpha, descr, dcsrVal, dcsrRowPtr, dcsrColInd, dx, &beta, dy));
63
64 // Copy result back to host
65 HIP_CHECK(hipMemcpy(hy.data(), dy, sizeof(double) * m, hipMemcpyDeviceToHost));
66
67 std::cout << "hy" << std::endl;
68 for(int i = 0; i < m; i++)
69 {
70 std::cout << hy[i] << " ";
71 }
72 std::cout << std::endl;
73
74 // Clear hipSPARSE
75 HIPSPARSE_CHECK(hipsparseDestroyMatDescr(descr));
76 HIPSPARSE_CHECK(hipsparseDestroy(handle));
77
78 // Clear device memory
79 HIP_CHECK(hipFree(dcsrRowPtr));
80 HIP_CHECK(hipFree(dcsrColInd));
81 HIP_CHECK(hipFree(dcsrVal));
82 HIP_CHECK(hipFree(dx));
83 HIP_CHECK(hipFree(dy));
84
85 return 0;
86}
1int main(int argc, char* argv[])
2{
3 // hipSPARSE handle
4 hipsparseHandle_t handle;
5 HIPSPARSE_CHECK(hipsparseCreate(&handle));
6
7 // alpha * ( 1.0 0.0 2.0 ) * ( 1.0 ) + beta * ( 4.0 ) = ( 31.1 )
8 // ( 3.0 0.0 4.0 ) * ( 2.0 ) ( 5.0 ) = ( 62.0 )
9 // ( 5.0 6.0 0.0 ) * ( 3.0 ) ( 6.0 ) = ( 70.7 )
10 // ( 7.0 0.0 8.0 ) * ( 7.0 ) = ( 123.8 )
11
12 const int m = 4;
13 const int n = 3;
14 const int nnz = 8;
15
16 // CSR row pointers
17 int hcsrRowPtr[] = {0, 2, 4, 6, 8};
18
19 // CSR column indices
20 int hcsrColInd[] = {0, 2, 0, 2, 0, 1, 0, 2};
21
22 // CSR values
23 double hcsrVal[] = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0};
24
25 // Transposition of the matrix
26 hipsparseOperation_t trans = HIPSPARSE_OPERATION_NON_TRANSPOSE;
27
28 // Scalar alpha and beta
29 double alpha = 3.7;
30 double beta = 1.3;
31
32 // x and y
33 double hx[] = {1.0, 2.0, 3.0};
34 double hy[] = {4.0, 5.0, 6.0, 7.0};
35
36 // Matrix descriptor
37 hipsparseMatDescr_t descr;
38 HIPSPARSE_CHECK(hipsparseCreateMatDescr(&descr));
39
40 // Offload data to device
41 int* dcsrRowPtr;
42 int* dcsrColInd;
43 double* dcsrVal;
44 double* dx;
45 double* dy;
46
47 HIP_CHECK(hipMalloc((void**)&dcsrRowPtr, sizeof(int) * (m + 1)));
48 HIP_CHECK(hipMalloc((void**)&dcsrColInd, sizeof(int) * nnz));
49 HIP_CHECK(hipMalloc((void**)&dcsrVal, sizeof(double) * nnz));
50 HIP_CHECK(hipMalloc((void**)&dx, sizeof(double) * n));
51 HIP_CHECK(hipMalloc((void**)&dy, sizeof(double) * m));
52
53 HIP_CHECK(hipMemcpy(dcsrRowPtr, hcsrRowPtr, sizeof(int) * (m + 1), hipMemcpyHostToDevice));
54 HIP_CHECK(hipMemcpy(dcsrColInd, hcsrColInd, sizeof(int) * nnz, hipMemcpyHostToDevice));
55 HIP_CHECK(hipMemcpy(dcsrVal, hcsrVal, sizeof(double) * nnz, hipMemcpyHostToDevice));
56 HIP_CHECK(hipMemcpy(dx, hx, sizeof(double) * n, hipMemcpyHostToDevice));
57 HIP_CHECK(hipMemcpy(dy, hy, sizeof(double) * m, hipMemcpyHostToDevice));
58
59 // Call dcsrmv to perform y = alpha * A x + beta * y
60 HIPSPARSE_CHECK(hipsparseDcsrmv(
61 handle, trans, m, n, nnz, &alpha, descr, dcsrVal, dcsrRowPtr, dcsrColInd, dx, &beta, dy));
62
63 // Copy result back to host
64 HIP_CHECK(hipMemcpy(hy, dy, sizeof(double) * m, hipMemcpyDeviceToHost));
65
66 printf("hy\n");
67 for(int i = 0; i < m; i++)
68 {
69 printf("%f ", hy[i]);
70 }
71 printf("\n");
72
73 // Clear hipSPARSE
74 HIPSPARSE_CHECK(hipsparseDestroyMatDescr(descr));
75 HIPSPARSE_CHECK(hipsparseDestroy(handle));
76
77 // Clear device memory
78 HIP_CHECK(hipFree(dcsrRowPtr));
79 HIP_CHECK(hipFree(dcsrColInd));
80 HIP_CHECK(hipFree(dcsrVal));
81 HIP_CHECK(hipFree(dx));
82 HIP_CHECK(hipFree(dy));
83
84 return 0;
85}
1program example_fortran_csrmv
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_csr_row_ptr(5), h_csr_col_ind(8)
52 real(8), target :: h_csr_val(8), h_x(3), h_y(4)
53
54 type(c_ptr) :: d_csr_row_ptr
55 type(c_ptr) :: d_csr_col_ind
56 type(c_ptr) :: d_csr_val
57 type(c_ptr) :: d_x
58 type(c_ptr) :: d_y
59
60 integer :: i
61 integer(c_int) :: m, n, nnz
62 integer(c_int) :: trans
63
64 real(c_double), target :: alpha, beta
65
66 type(c_ptr) :: handle
67 type(c_ptr) :: descr
68
69 integer :: version
70
71! Input data
72 m = 4
73 n = 3
74 nnz = 8
75
76! Fill CSR structure
77 h_csr_row_ptr = (/0, 2, 4, 6, 8/)
78 h_csr_col_ind = (/0, 2, 0, 2, 0, 1, 0, 2/)
79 h_csr_val = (/1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0/)
80
81! Transposition
82 trans = HIPSPARSE_OPERATION_NON_TRANSPOSE
83
84! Scalar alpha and beta
85 alpha = 3.7
86 beta = 1.3
87
88! x and y vectors
89 h_x = (/1.0, 2.0, 3.0/)
90 h_y = (/4.0, 5.0, 6.0, 7.0/)
91
92! Allocate device memory
93 call HIP_CHECK(hipMalloc(d_csr_row_ptr, int(m + 1, c_size_t) * 4))
94 call HIP_CHECK(hipMalloc(d_csr_col_ind, int(nnz, c_size_t) * 4))
95 call HIP_CHECK(hipMalloc(d_csr_val, int(nnz, c_size_t) * 8))
96 call HIP_CHECK(hipMalloc(d_x, int(n, c_size_t) * 8))
97 call HIP_CHECK(hipMalloc(d_y, int(m, c_size_t) * 8))
98
99! Copy host data to device
100 call HIP_CHECK(hipMemcpy(d_csr_row_ptr, c_loc(h_csr_row_ptr), int(m + 1, c_size_t) * 4, 1))
101 call HIP_CHECK(hipMemcpy(d_csr_col_ind, c_loc(h_csr_col_ind), int(nnz, c_size_t) * 4, 1))
102 call HIP_CHECK(hipMemcpy(d_csr_val, c_loc(h_csr_val), int(nnz, c_size_t) * 8, 1))
103 call HIP_CHECK(hipMemcpy(d_x, c_loc(h_x), int(n, c_size_t) * 8, 1))
104 call HIP_CHECK(hipMemcpy(d_y, c_loc(h_y), int(m, c_size_t) * 8, 1))
105
106! Create hipSPARSE handle
107 call HIPSPARSE_CHECK(hipsparseCreate(handle))
108
109! Create matrix descriptor
110 call HIPSPARSE_CHECK(hipsparseCreateMatDescr(descr))
111
112! Get hipSPARSE version
113 call HIPSPARSE_CHECK(hipsparseGetVersion(handle, version))
114
115! Print version on screen
116 write(*,fmt='(A,I0,A,I0,A,I0)') 'hipSPARSE version: ', version / 100000, '.', &
117 mod(version / 100, 1000), '.', mod(version, 100)
118
119! Call dcsrmv to perform y = alpha * A * x + beta * y
120 call HIPSPARSE_CHECK(hipsparseDcsrmv(handle, &
121 trans, &
122 m, &
123 n, &
124 nnz, &
125 c_loc(alpha), &
126 descr, &
127 d_csr_val, &
128 d_csr_row_ptr, &
129 d_csr_col_ind, &
130 d_x, &
131 c_loc(beta), &
132 d_y))
133
134! Print result
135 call HIP_CHECK(hipMemcpy(c_loc(h_y), d_y, int(m, c_size_t) * 8, 2))
136
137 write(*,fmt='(A)',advance='no') 'hy:'
138 do i = 1, m
139 write(*,fmt='(A,F0.2)',advance='no') ' ', h_y(i)
140 end do
141 write(*,*)
142
143! Clear hipSPARSE
144 call HIPSPARSE_CHECK(hipsparseDestroyMatDescr(descr))
145 call HIPSPARSE_CHECK(hipsparseDestroy(handle))
146
147! Clear device memory
148 call HIP_CHECK(hipFree(d_csr_row_ptr))
149 call HIP_CHECK(hipFree(d_csr_col_ind))
150 call HIP_CHECK(hipFree(d_csr_val))
151 call HIP_CHECK(hipFree(d_x))
152 call HIP_CHECK(hipFree(d_y))
153
154end program example_fortran_csrmv
hipsparseXcsrsv2_zeroPivot()#
Warning
doxygenfunction: Cannot find file: /home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-hipsparse/checkouts/7.13.0-preview/projects/hipsparse/docs/doxygen/xml/index.xml
hipsparseXcsrsv2_bufferSize()#
Warning
doxygenfunction: Cannot find file: /home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-hipsparse/checkouts/7.13.0-preview/projects/hipsparse/docs/doxygen/xml/index.xml
Warning
doxygenfunction: Cannot find file: /home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-hipsparse/checkouts/7.13.0-preview/projects/hipsparse/docs/doxygen/xml/index.xml
Warning
doxygenfunction: Cannot find file: /home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-hipsparse/checkouts/7.13.0-preview/projects/hipsparse/docs/doxygen/xml/index.xml
Warning
doxygenfunction: Cannot find file: /home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-hipsparse/checkouts/7.13.0-preview/projects/hipsparse/docs/doxygen/xml/index.xml
hipsparseXcsrsv2_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
hipsparseXcsrsv2_analysis()#
Warning
doxygenfunction: Cannot find file: /home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-hipsparse/checkouts/7.13.0-preview/projects/hipsparse/docs/doxygen/xml/index.xml
Warning
doxygenfunction: Cannot find file: /home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-hipsparse/checkouts/7.13.0-preview/projects/hipsparse/docs/doxygen/xml/index.xml
Warning
doxygenfunction: Cannot find file: /home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-hipsparse/checkouts/7.13.0-preview/projects/hipsparse/docs/doxygen/xml/index.xml
Warning
doxygenfunction: Cannot find file: /home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-hipsparse/checkouts/7.13.0-preview/projects/hipsparse/docs/doxygen/xml/index.xml
hipsparseXcsrsv2_solve()#
Warning
doxygenfunction: Cannot find file: /home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-hipsparse/checkouts/7.13.0-preview/projects/hipsparse/docs/doxygen/xml/index.xml
Warning
doxygenfunction: Cannot find file: /home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-hipsparse/checkouts/7.13.0-preview/projects/hipsparse/docs/doxygen/xml/index.xml
Warning
doxygenfunction: Cannot find file: /home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-hipsparse/checkouts/7.13.0-preview/projects/hipsparse/docs/doxygen/xml/index.xml
Warning
doxygenfunction: Cannot find file: /home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-hipsparse/checkouts/7.13.0-preview/projects/hipsparse/docs/doxygen/xml/index.xml
1int main(int argc, char* argv[])
2{
3 // hipSPARSE handle
4 hipsparseHandle_t handle;
5 HIPSPARSE_CHECK(hipsparseCreate(&handle));
6
7 // alpha * ( 1.0 0.0 2.0 0.0 ) * ( x_0 ) = ( 32.0 )
8 // ( 3.0 2.0 4.0 1.0 ) * ( x_1 ) = ( 14.7 )
9 // ( 5.0 6.0 1.0 3.0 ) * ( x_2 ) = ( 33.6 )
10 // ( 7.0 0.0 8.0 0.6 ) * ( x_3 ) = ( 10.0 )
11
12 const int m = 4;
13 const int nnz = 13;
14
15 // CSR row pointers
16 std::vector<int> hcsrRowPtr = {0, 2, 6, 10, 13};
17
18 // CSR column indices
19 std::vector<int> hcsrColInd = {0, 2, 0, 1, 2, 3, 0, 1, 2, 3, 0, 2, 3};
20
21 // CSR values
22 std::vector<double> hcsrVal = {1.0, 2.0, 3.0, 2.0, 4.0, 1.0, 5.0, 6.0, 1.0, 3.0, 7.0, 8.0, 0.6};
23
24 // Transposition of the matrix
25 hipsparseOperation_t trans = HIPSPARSE_OPERATION_NON_TRANSPOSE;
26 hipsparseSolvePolicy_t policy = HIPSPARSE_SOLVE_POLICY_USE_LEVEL;
27
28 // Scalar alpha
29 double alpha = 1.0;
30
31 // f and x
32 std::vector<double> hf = {32.0, 14.7, 33.6, 10.0};
33 std::vector<double> hx(m);
34
35 // Matrix descriptor
36 hipsparseMatDescr_t descr;
37 HIPSPARSE_CHECK(hipsparseCreateMatDescr(&descr));
38
39 // Set index base on descriptor
40 HIPSPARSE_CHECK(hipsparseSetMatIndexBase(descr, HIPSPARSE_INDEX_BASE_ZERO));
41
42 // Set fill mode on descriptor
43 HIPSPARSE_CHECK(hipsparseSetMatFillMode(descr, HIPSPARSE_FILL_MODE_LOWER));
44
45 // Set diag type on descriptor
46 HIPSPARSE_CHECK(hipsparseSetMatDiagType(descr, HIPSPARSE_DIAG_TYPE_UNIT));
47
48 // Csrsv info
49 csrsv2Info_t info;
50 HIPSPARSE_CHECK(hipsparseCreateCsrsv2Info(&info));
51
52 // Offload data to device
53 int* dcsrRowPtr;
54 int* dcsrColInd;
55 double* dcsrVal;
56 double* df;
57 double* dx;
58
59 HIP_CHECK(hipMalloc((void**)&dcsrRowPtr, sizeof(int) * (m + 1)));
60 HIP_CHECK(hipMalloc((void**)&dcsrColInd, sizeof(int) * nnz));
61 HIP_CHECK(hipMalloc((void**)&dcsrVal, sizeof(double) * nnz));
62 HIP_CHECK(hipMalloc((void**)&df, sizeof(double) * m));
63 HIP_CHECK(hipMalloc((void**)&dx, sizeof(double) * m));
64
65 HIP_CHECK(
66 hipMemcpy(dcsrRowPtr, hcsrRowPtr.data(), sizeof(int) * (m + 1), hipMemcpyHostToDevice));
67 HIP_CHECK(hipMemcpy(dcsrColInd, hcsrColInd.data(), sizeof(int) * nnz, hipMemcpyHostToDevice));
68 HIP_CHECK(hipMemcpy(dcsrVal, hcsrVal.data(), sizeof(double) * nnz, hipMemcpyHostToDevice));
69 HIP_CHECK(hipMemcpy(df, hf.data(), sizeof(double) * m, hipMemcpyHostToDevice));
70
71 int bufferSize = 0;
72 HIPSPARSE_CHECK(hipsparseDcsrsv2_bufferSize(
73 handle, trans, m, nnz, descr, dcsrVal, dcsrRowPtr, dcsrColInd, info, &bufferSize));
74
75 void* dbuffer = nullptr;
76 HIP_CHECK(hipMalloc((void**)&dbuffer, bufferSize));
77
78 HIPSPARSE_CHECK(hipsparseDcsrsv2_analysis(
79 handle, trans, m, nnz, descr, dcsrVal, dcsrRowPtr, dcsrColInd, info, policy, dbuffer));
80
81 // Call dcsrsv to perform alpha * A * x = f
82 HIPSPARSE_CHECK(hipsparseDcsrsv2_solve(handle,
83 trans,
84 m,
85 nnz,
86 &alpha,
87 descr,
88 dcsrVal,
89 dcsrRowPtr,
90 dcsrColInd,
91 info,
92 df,
93 dx,
94 policy,
95 dbuffer));
96
97 // Copy result back to host
98 HIP_CHECK(hipMemcpy(hx.data(), dx, sizeof(double) * m, hipMemcpyDeviceToHost));
99
100 std::cout << "hx" << std::endl;
101 for(int i = 0; i < m; i++)
102 {
103 std::cout << hx[i] << " ";
104 }
105 std::cout << std::endl;
106
107 // Clear hipSPARSE
108 HIPSPARSE_CHECK(hipsparseDestroyMatDescr(descr));
109 HIPSPARSE_CHECK(hipsparseDestroyCsrsv2Info(info));
110 HIPSPARSE_CHECK(hipsparseDestroy(handle));
111
112 // Clear device memory
113 HIP_CHECK(hipFree(dcsrRowPtr));
114 HIP_CHECK(hipFree(dcsrColInd));
115 HIP_CHECK(hipFree(dcsrVal));
116 HIP_CHECK(hipFree(df));
117 HIP_CHECK(hipFree(dx));
118 HIP_CHECK(hipFree(dbuffer));
119
120 return 0;
121}
1int main(int argc, char* argv[])
2{
3 // hipSPARSE handle
4 hipsparseHandle_t handle;
5 HIPSPARSE_CHECK(hipsparseCreate(&handle));
6
7 // alpha * ( 1.0 0.0 2.0 0.0 ) * ( x_0 ) = ( 32.0 )
8 // ( 3.0 2.0 4.0 1.0 ) * ( x_1 ) = ( 14.7 )
9 // ( 5.0 6.0 1.0 3.0 ) * ( x_2 ) = ( 33.6 )
10 // ( 7.0 0.0 8.0 0.6 ) * ( x_3 ) = ( 10.0 )
11
12 const int m = 4;
13 const int nnz = 13;
14
15 // CSR row pointers
16 int hcsrRowPtr[] = {0, 2, 6, 10, 13};
17
18 // CSR column indices
19 int hcsrColInd[] = {0, 2, 0, 1, 2, 3, 0, 1, 2, 3, 0, 2, 3};
20
21 // CSR values
22 double hcsrVal[] = {1.0, 2.0, 3.0, 2.0, 4.0, 1.0, 5.0, 6.0, 1.0, 3.0, 7.0, 8.0, 0.6};
23
24 // Transposition of the matrix
25 hipsparseOperation_t trans = HIPSPARSE_OPERATION_NON_TRANSPOSE;
26 hipsparseSolvePolicy_t policy = HIPSPARSE_SOLVE_POLICY_USE_LEVEL;
27
28 // Scalar alpha
29 double alpha = 1.0;
30
31 // f and x
32 double hf[] = {32.0, 14.7, 33.6, 10.0};
33 double hx[m];
34
35 // Matrix descriptor
36 hipsparseMatDescr_t descr;
37 HIPSPARSE_CHECK(hipsparseCreateMatDescr(&descr));
38
39 // Set index base on descriptor
40 HIPSPARSE_CHECK(hipsparseSetMatIndexBase(descr, HIPSPARSE_INDEX_BASE_ZERO));
41
42 // Set fill mode on descriptor
43 HIPSPARSE_CHECK(hipsparseSetMatFillMode(descr, HIPSPARSE_FILL_MODE_LOWER));
44
45 // Set diag type on descriptor
46 HIPSPARSE_CHECK(hipsparseSetMatDiagType(descr, HIPSPARSE_DIAG_TYPE_UNIT));
47
48 // Csrsv info
49 csrsv2Info_t info;
50 HIPSPARSE_CHECK(hipsparseCreateCsrsv2Info(&info));
51
52 // Offload data to device
53 int* dcsrRowPtr;
54 int* dcsrColInd;
55 double* dcsrVal;
56 double* df;
57 double* dx;
58
59 HIP_CHECK(hipMalloc((void**)&dcsrRowPtr, sizeof(int) * (m + 1)));
60 HIP_CHECK(hipMalloc((void**)&dcsrColInd, sizeof(int) * nnz));
61 HIP_CHECK(hipMalloc((void**)&dcsrVal, sizeof(double) * nnz));
62 HIP_CHECK(hipMalloc((void**)&df, sizeof(double) * m));
63 HIP_CHECK(hipMalloc((void**)&dx, sizeof(double) * m));
64
65 HIP_CHECK(hipMemcpy(dcsrRowPtr, hcsrRowPtr, sizeof(int) * (m + 1), hipMemcpyHostToDevice));
66 HIP_CHECK(hipMemcpy(dcsrColInd, hcsrColInd, sizeof(int) * nnz, hipMemcpyHostToDevice));
67 HIP_CHECK(hipMemcpy(dcsrVal, hcsrVal, sizeof(double) * nnz, hipMemcpyHostToDevice));
68 HIP_CHECK(hipMemcpy(df, hf, sizeof(double) * m, hipMemcpyHostToDevice));
69
70 int bufferSize = 0;
71 HIPSPARSE_CHECK(hipsparseDcsrsv2_bufferSize(
72 handle, trans, m, nnz, descr, dcsrVal, dcsrRowPtr, dcsrColInd, info, &bufferSize));
73
74 void* dbuffer = NULL;
75 HIP_CHECK(hipMalloc((void**)&dbuffer, bufferSize));
76
77 HIPSPARSE_CHECK(hipsparseDcsrsv2_analysis(
78 handle, trans, m, nnz, descr, dcsrVal, dcsrRowPtr, dcsrColInd, info, policy, dbuffer));
79
80 // Call dcsrsv to perform alpha * A * x = f
81 HIPSPARSE_CHECK(hipsparseDcsrsv2_solve(handle,
82 trans,
83 m,
84 nnz,
85 &alpha,
86 descr,
87 dcsrVal,
88 dcsrRowPtr,
89 dcsrColInd,
90 info,
91 df,
92 dx,
93 policy,
94 dbuffer));
95
96 // Copy result back to host
97 HIP_CHECK(hipMemcpy(hx, dx, sizeof(double) * m, hipMemcpyDeviceToHost));
98
99 printf("hx\n");
100 for(int i = 0; i < m; i++)
101 {
102 printf("%f ", hx[i]);
103 }
104 printf("\n");
105
106 // Clear hipSPARSE
107 HIPSPARSE_CHECK(hipsparseDestroyMatDescr(descr));
108 HIPSPARSE_CHECK(hipsparseDestroyCsrsv2Info(info));
109 HIPSPARSE_CHECK(hipsparseDestroy(handle));
110
111 // Clear device memory
112 HIP_CHECK(hipFree(dcsrRowPtr));
113 HIP_CHECK(hipFree(dcsrColInd));
114 HIP_CHECK(hipFree(dcsrVal));
115 HIP_CHECK(hipFree(df));
116 HIP_CHECK(hipFree(dx));
117 HIP_CHECK(hipFree(dbuffer));
118
119 return 0;
120}
1program example_fortran_csrsv2
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_csr_row_ptr(5), h_csr_col_ind(13)
38 real(8), target :: h_csr_val(13), h_f(4), h_x(4)
39
40 type(c_ptr) :: d_csr_row_ptr, d_csr_col_ind, d_csr_val
41 type(c_ptr) :: d_f, d_x, d_buffer
42
43 integer :: i
44 integer(c_int) :: m, nnz, trans, policy
45 integer(c_int), target :: buffer_size
46
47 real(c_double), target :: alpha
48
49 type(c_ptr) :: handle, descr, info
50
51 integer :: version
52
53! Input data
54 m = 4
55 nnz = 13
56
57! Fill CSR structure
58 h_csr_row_ptr = (/0, 2, 6, 10, 13/)
59 h_csr_col_ind = (/0, 2, 0, 1, 2, 3, 0, 1, 2, 3, 0, 2, 3/)
60 h_csr_val = (/1.0d0, 2.0d0, 3.0d0, 2.0d0, 4.0d0, 1.0d0, 5.0d0, 6.0d0, 1.0d0, 3.0d0, 7.0d0, 8.0d0, 0.6d0/)
61
62! Transposition and policy
63 trans = HIPSPARSE_OPERATION_NON_TRANSPOSE
64 policy = HIPSPARSE_SOLVE_POLICY_USE_LEVEL
65
66! Scalar alpha
67 alpha = 1.0d0
68
69! f and x vectors
70 h_f = (/32.0d0, 14.7d0, 33.6d0, 10.0d0/)
71 h_x = 0.0d0
72
73! Allocate device memory
74 call HIP_CHECK(hipMalloc(d_csr_row_ptr, int(m + 1, c_size_t) * 4))
75 call HIP_CHECK(hipMalloc(d_csr_col_ind, int(nnz, c_size_t) * 4))
76 call HIP_CHECK(hipMalloc(d_csr_val, int(nnz, c_size_t) * 8))
77 call HIP_CHECK(hipMalloc(d_f, int(m, c_size_t) * 8))
78 call HIP_CHECK(hipMalloc(d_x, int(m, c_size_t) * 8))
79
80! Copy host data to device
81 call HIP_CHECK(hipMemcpy(d_csr_row_ptr, c_loc(h_csr_row_ptr), int(m + 1, c_size_t) * 4, 1))
82 call HIP_CHECK(hipMemcpy(d_csr_col_ind, c_loc(h_csr_col_ind), int(nnz, c_size_t) * 4, 1))
83 call HIP_CHECK(hipMemcpy(d_csr_val, c_loc(h_csr_val), int(nnz, c_size_t) * 8, 1))
84 call HIP_CHECK(hipMemcpy(d_f, c_loc(h_f), int(m, c_size_t) * 8, 1))
85
86! Create hipSPARSE handle
87 call HIPSPARSE_CHECK(hipsparseCreate(handle))
88
89! Create matrix descriptor
90 call HIPSPARSE_CHECK(hipsparseCreateMatDescr(descr))
91
92! Set matrix properties
93 call HIPSPARSE_CHECK(hipsparseSetMatIndexBase(descr, HIPSPARSE_INDEX_BASE_ZERO))
94 call HIPSPARSE_CHECK(hipsparseSetMatFillMode(descr, HIPSPARSE_FILL_MODE_LOWER))
95 call HIPSPARSE_CHECK(hipsparseSetMatDiagType(descr, HIPSPARSE_DIAG_TYPE_UNIT))
96
97! Create csrsv2 info
98 call HIPSPARSE_CHECK(hipsparseCreateCsrsv2Info(info))
99
100! Get hipSPARSE version
101 call HIPSPARSE_CHECK(hipsparseGetVersion(handle, version))
102 write(*,fmt='(A,I0,A,I0,A,I0)') 'hipSPARSE version: ', version / 100000, '.', &
103 mod(version / 100, 1000), '.', mod(version, 100)
104
105! Get buffer size
106 call HIPSPARSE_CHECK(hipsparseDcsrsv2_bufferSize(handle, &
107 trans, &
108 m, &
109 nnz, &
110 descr, &
111 d_csr_val, &
112 d_csr_row_ptr, &
113 d_csr_col_ind, &
114 info, &
115 c_loc(buffer_size)))
116
117! Allocate buffer
118 call HIP_CHECK(hipMalloc(d_buffer, int(buffer_size, c_size_t)))
119
120! Analysis step
121 call HIPSPARSE_CHECK(hipsparseDcsrsv2_analysis(handle, &
122 trans, &
123 m, &
124 nnz, &
125 descr, &
126 d_csr_val, &
127 d_csr_row_ptr, &
128 d_csr_col_ind, &
129 info, &
130 policy, &
131 d_buffer))
132
133! Call dcsrsv2_solve to perform alpha * A * x = f
134 call HIPSPARSE_CHECK(hipsparseDcsrsv2_solve(handle, &
135 trans, &
136 m, &
137 nnz, &
138 c_loc(alpha), &
139 descr, &
140 d_csr_val, &
141 d_csr_row_ptr, &
142 d_csr_col_ind, &
143 info, &
144 d_f, &
145 d_x, &
146 policy, &
147 d_buffer))
148
149! Copy result back to host
150 call HIP_CHECK(hipMemcpy(c_loc(h_x), d_x, int(m, c_size_t) * 8, 2))
151
152! Print result
153 write(*,fmt='(A)',advance='no') 'hx:'
154 do i = 1, m
155 write(*,fmt='(A,F0.2)',advance='no') ' ', h_x(i)
156 end do
157 write(*,*)
158
159! Clear hipSPARSE
160 call HIPSPARSE_CHECK(hipsparseDestroyCsrsv2Info(info))
161 call HIPSPARSE_CHECK(hipsparseDestroyMatDescr(descr))
162 call HIPSPARSE_CHECK(hipsparseDestroy(handle))
163
164! Clear device memory
165 call HIP_CHECK(hipFree(d_csr_row_ptr))
166 call HIP_CHECK(hipFree(d_csr_col_ind))
167 call HIP_CHECK(hipFree(d_csr_val))
168 call HIP_CHECK(hipFree(d_f))
169 call HIP_CHECK(hipFree(d_x))
170 call HIP_CHECK(hipFree(d_buffer))
171
172end program example_fortran_csrsv2
hipsparseXhybmv()#
Warning
doxygenfunction: Cannot find file: /home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-hipsparse/checkouts/7.13.0-preview/projects/hipsparse/docs/doxygen/xml/index.xml
Warning
doxygenfunction: Cannot find file: /home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-hipsparse/checkouts/7.13.0-preview/projects/hipsparse/docs/doxygen/xml/index.xml
Warning
doxygenfunction: Cannot find file: /home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-hipsparse/checkouts/7.13.0-preview/projects/hipsparse/docs/doxygen/xml/index.xml
Warning
doxygenfunction: Cannot find file: /home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-hipsparse/checkouts/7.13.0-preview/projects/hipsparse/docs/doxygen/xml/index.xml
1int main(int argc, char* argv[])
2{
3 // hipSPARSE handle
4 hipsparseHandle_t handle;
5 HIPSPARSE_CHECK(hipsparseCreate(&handle));
6
7 // A sparse matrix
8 // 1 0 3 4
9 // 0 0 5 1
10 // 0 2 0 0
11 // 4 0 0 8
12 std::vector<int> hAptr = {0, 3, 5, 6, 8};
13 std::vector<int> hAcol = {0, 2, 3, 2, 3, 1, 0, 3};
14 std::vector<double> hAval = {1.0, 3.0, 4.0, 5.0, 1.0, 2.0, 4.0, 8.0};
15
16 int m = 4;
17 int n = 4;
18 int nnz = 8;
19
20 double halpha = 1.0;
21 double hbeta = 0.0;
22
23 std::vector<double> hx = {1.0, 2.0, 3.0, 4.0};
24 std::vector<double> hy = {4.0, 5.0, 6.0, 7.0};
25
26 // Matrix descriptor
27 hipsparseMatDescr_t descrA;
28 HIPSPARSE_CHECK(hipsparseCreateMatDescr(&descrA));
29
30 // Offload data to device
31 int* dAptr = NULL;
32 int* dAcol = NULL;
33 double* dAval = NULL;
34 double* dx = NULL;
35 double* dy = NULL;
36
37 HIP_CHECK(hipMalloc((void**)&dAptr, sizeof(int) * (m + 1)));
38 HIP_CHECK(hipMalloc((void**)&dAcol, sizeof(int) * nnz));
39 HIP_CHECK(hipMalloc((void**)&dAval, sizeof(double) * nnz));
40 HIP_CHECK(hipMalloc((void**)&dx, sizeof(double) * n));
41 HIP_CHECK(hipMalloc((void**)&dy, sizeof(double) * m));
42
43 HIP_CHECK(hipMemcpy(dAptr, hAptr.data(), sizeof(int) * (m + 1), hipMemcpyHostToDevice));
44 HIP_CHECK(hipMemcpy(dAcol, hAcol.data(), sizeof(int) * nnz, hipMemcpyHostToDevice));
45 HIP_CHECK(hipMemcpy(dAval, hAval.data(), sizeof(double) * nnz, hipMemcpyHostToDevice));
46 HIP_CHECK(hipMemcpy(dx, hx.data(), sizeof(double) * n, hipMemcpyHostToDevice));
47
48 // Convert CSR matrix to HYB format
49 hipsparseHybMat_t hybA;
50 HIPSPARSE_CHECK(hipsparseCreateHybMat(&hybA));
51
52 HIPSPARSE_CHECK(hipsparseDcsr2hyb(
53 handle, m, n, descrA, dAval, dAptr, dAcol, hybA, 0, HIPSPARSE_HYB_PARTITION_AUTO));
54
55 // Clean up CSR structures
56 HIP_CHECK(hipFree(dAptr));
57 HIP_CHECK(hipFree(dAcol));
58 HIP_CHECK(hipFree(dAval));
59
60 // Call hipsparse hybmv
61 HIPSPARSE_CHECK(hipsparseDhybmv(
62 handle, HIPSPARSE_OPERATION_NON_TRANSPOSE, &halpha, descrA, hybA, dx, &hbeta, dy));
63
64 // Copy result back to host
65 HIP_CHECK(hipMemcpy(hy.data(), dy, sizeof(double) * m, hipMemcpyDeviceToHost));
66
67 std::cout << "hy" << std::endl;
68 for(int i = 0; i < m; i++)
69 {
70 std::cout << hy[i] << " ";
71 }
72 std::cout << std::endl;
73
74 // Clear up on device
75 HIPSPARSE_CHECK(hipsparseDestroyHybMat(hybA));
76 HIPSPARSE_CHECK(hipsparseDestroyMatDescr(descrA));
77 HIPSPARSE_CHECK(hipsparseDestroy(handle));
78
79 HIP_CHECK(hipFree(dx));
80 HIP_CHECK(hipFree(dy));
81
82 return 0;
83}
1int main(int argc, char* argv[])
2{
3 // hipSPARSE handle
4 hipsparseHandle_t handle;
5 HIPSPARSE_CHECK(hipsparseCreate(&handle));
6
7 // A sparse matrix
8 // 1 0 3 4
9 // 0 0 5 1
10 // 0 2 0 0
11 // 4 0 0 8
12 int hAptr[5] = {0, 3, 5, 6, 8};
13 int hAcol[8] = {0, 2, 3, 2, 3, 1, 0, 3};
14 double hAval[] = {1.0, 3.0, 4.0, 5.0, 1.0, 2.0, 4.0, 8.0};
15
16 int m = 4;
17 int n = 4;
18 int nnz = 8;
19
20 double halpha = 1.0;
21 double hbeta = 0.0;
22
23 double hx[] = {1.0, 2.0, 3.0, 4.0};
24 double hy[] = {4.0, 5.0, 6.0, 7.0};
25
26 // Matrix descriptor
27 hipsparseMatDescr_t descrA;
28 HIPSPARSE_CHECK(hipsparseCreateMatDescr(&descrA));
29
30 // Offload data to device
31 int* dAptr = NULL;
32 int* dAcol = NULL;
33 double* dAval = NULL;
34 double* dx = NULL;
35 double* dy = NULL;
36
37 HIP_CHECK(hipMalloc((void**)&dAptr, sizeof(int) * (m + 1)));
38 HIP_CHECK(hipMalloc((void**)&dAcol, sizeof(int) * nnz));
39 HIP_CHECK(hipMalloc((void**)&dAval, sizeof(double) * nnz));
40 HIP_CHECK(hipMalloc((void**)&dx, sizeof(double) * n));
41 HIP_CHECK(hipMalloc((void**)&dy, sizeof(double) * m));
42
43 HIP_CHECK(hipMemcpy(dAptr, hAptr, sizeof(int) * (m + 1), hipMemcpyHostToDevice));
44 HIP_CHECK(hipMemcpy(dAcol, hAcol, sizeof(int) * nnz, hipMemcpyHostToDevice));
45 HIP_CHECK(hipMemcpy(dAval, hAval, sizeof(double) * nnz, hipMemcpyHostToDevice));
46 HIP_CHECK(hipMemcpy(dx, hx, sizeof(double) * n, hipMemcpyHostToDevice));
47
48 // Convert CSR matrix to HYB format
49 hipsparseHybMat_t hybA;
50 HIPSPARSE_CHECK(hipsparseCreateHybMat(&hybA));
51
52 HIPSPARSE_CHECK(hipsparseDcsr2hyb(
53 handle, m, n, descrA, dAval, dAptr, dAcol, hybA, 0, HIPSPARSE_HYB_PARTITION_AUTO));
54
55 // Clean up CSR structures
56 HIP_CHECK(hipFree(dAptr));
57 HIP_CHECK(hipFree(dAcol));
58 HIP_CHECK(hipFree(dAval));
59
60 // Call hipsparse hybmv
61 HIPSPARSE_CHECK(hipsparseDhybmv(
62 handle, HIPSPARSE_OPERATION_NON_TRANSPOSE, &halpha, descrA, hybA, dx, &hbeta, dy));
63
64 // Copy result back to host
65 HIP_CHECK(hipMemcpy(hy, dy, sizeof(double) * m, hipMemcpyDeviceToHost));
66
67 printf("hy\n");
68 for(int i = 0; i < m; i++)
69 {
70 printf("%f ", hy[i]);
71 }
72 printf("\n");
73
74 // Clear up on device
75 HIPSPARSE_CHECK(hipsparseDestroyHybMat(hybA));
76 HIPSPARSE_CHECK(hipsparseDestroyMatDescr(descrA));
77 HIPSPARSE_CHECK(hipsparseDestroy(handle));
78
79 HIP_CHECK(hipFree(dx));
80 HIP_CHECK(hipFree(dy));
81
82 return 0;
83}
1program example_fortran_hybmv
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_a_ptr(5), h_a_col(8)
38 real(8), target :: h_a_val(8), h_x(4), h_y(4)
39
40 type(c_ptr) :: d_a_ptr, d_a_col, d_a_val, d_x, d_y
41
42 integer :: i
43 integer(c_int) :: m, n, nnz
44
45 real(c_double), target :: alpha, beta
46
47 type(c_ptr) :: handle, descr_a, hyb_a
48
49 integer :: version
50
51! Input data
52 m = 4
53 n = 4
54 nnz = 8
55
56! Fill CSR structure (to be converted to HYB)
57 h_a_ptr = (/0, 3, 5, 6, 8/)
58 h_a_col = (/0, 2, 3, 2, 3, 1, 0, 3/)
59 h_a_val = (/1.0d0, 3.0d0, 4.0d0, 5.0d0, 1.0d0, 2.0d0, 4.0d0, 8.0d0/)
60
61! Scalar alpha and beta
62 alpha = 1.0d0
63 beta = 0.0d0
64
65! x and y vectors
66 h_x = (/1.0d0, 2.0d0, 3.0d0, 4.0d0/)
67 h_y = (/4.0d0, 5.0d0, 6.0d0, 7.0d0/)
68
69! Allocate device memory for CSR matrix
70 call HIP_CHECK(hipMalloc(d_a_ptr, int(m + 1, c_size_t) * 4))
71 call HIP_CHECK(hipMalloc(d_a_col, int(nnz, c_size_t) * 4))
72 call HIP_CHECK(hipMalloc(d_a_val, int(nnz, c_size_t) * 8))
73 call HIP_CHECK(hipMalloc(d_x, int(n, c_size_t) * 8))
74 call HIP_CHECK(hipMalloc(d_y, int(m, c_size_t) * 8))
75
76! Copy host data to device
77 call HIP_CHECK(hipMemcpy(d_a_ptr, c_loc(h_a_ptr), int(m + 1, c_size_t) * 4, 1))
78 call HIP_CHECK(hipMemcpy(d_a_col, c_loc(h_a_col), int(nnz, c_size_t) * 4, 1))
79 call HIP_CHECK(hipMemcpy(d_a_val, c_loc(h_a_val), int(nnz, c_size_t) * 8, 1))
80 call HIP_CHECK(hipMemcpy(d_x, c_loc(h_x), int(n, c_size_t) * 8, 1))
81
82! Create hipSPARSE handle
83 call HIPSPARSE_CHECK(hipsparseCreate(handle))
84
85! Create matrix descriptor
86 call HIPSPARSE_CHECK(hipsparseCreateMatDescr(descr_a))
87
88! Get hipSPARSE version
89 call HIPSPARSE_CHECK(hipsparseGetVersion(handle, version))
90 write(*,fmt='(A,I0,A,I0,A,I0)') 'hipSPARSE version: ', version / 100000, '.', &
91 mod(version / 100, 1000), '.', mod(version, 100)
92
93! Create HYB matrix
94 call HIPSPARSE_CHECK(hipsparseCreateHybMat(hyb_a))
95
96! Convert CSR to HYB format
97 call HIPSPARSE_CHECK(hipsparseDcsr2hyb(handle, &
98 m, &
99 n, &
100 descr_a, &
101 d_a_val, &
102 d_a_ptr, &
103 d_a_col, &
104 hyb_a, &
105 0, &
106 HIPSPARSE_HYB_PARTITION_AUTO))
107
108! Free CSR structures (no longer needed after conversion)
109 call HIP_CHECK(hipFree(d_a_ptr))
110 call HIP_CHECK(hipFree(d_a_col))
111 call HIP_CHECK(hipFree(d_a_val))
112
113! Call dhybmv to perform y = alpha * A * x + beta * y
114 call HIPSPARSE_CHECK(hipsparseDhybmv(handle, &
115 HIPSPARSE_OPERATION_NON_TRANSPOSE, &
116 c_loc(alpha), &
117 descr_a, &
118 hyb_a, &
119 d_x, &
120 c_loc(beta), &
121 d_y))
122
123! Copy result back to host
124 call HIP_CHECK(hipMemcpy(c_loc(h_y), d_y, int(m, c_size_t) * 8, 2))
125
126! Print result
127 write(*,fmt='(A)',advance='no') 'hy:'
128 do i = 1, m
129 write(*,fmt='(A,F0.2)',advance='no') ' ', h_y(i)
130 end do
131 write(*,*)
132
133! Clear hipSPARSE
134 call HIPSPARSE_CHECK(hipsparseDestroyHybMat(hyb_a))
135 call HIPSPARSE_CHECK(hipsparseDestroyMatDescr(descr_a))
136 call HIPSPARSE_CHECK(hipsparseDestroy(handle))
137
138! Clear device memory
139 call HIP_CHECK(hipFree(d_x))
140 call HIP_CHECK(hipFree(d_y))
141
142end program example_fortran_hybmv
hipsparseXbsrmv()#
Warning
doxygenfunction: Cannot find file: /home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-hipsparse/checkouts/7.13.0-preview/projects/hipsparse/docs/doxygen/xml/index.xml
Warning
doxygenfunction: Cannot find file: /home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-hipsparse/checkouts/7.13.0-preview/projects/hipsparse/docs/doxygen/xml/index.xml
Warning
doxygenfunction: Cannot find file: /home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-hipsparse/checkouts/7.13.0-preview/projects/hipsparse/docs/doxygen/xml/index.xml
Warning
doxygenfunction: Cannot find file: /home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-hipsparse/checkouts/7.13.0-preview/projects/hipsparse/docs/doxygen/xml/index.xml
1int main(int argc, char* argv[])
2{
3 // hipSPARSE handle
4 hipsparseHandle_t handle;
5 HIPSPARSE_CHECK(hipsparseCreate(&handle));
6
7 // alpha * ( 1.0 0.0 2.0 ) * ( 1.0 ) + beta * ( 4.0 ) = ( 31.1 )
8 // ( 3.0 0.0 4.0 ) * ( 2.0 ) ( 5.0 ) = ( 62.0 )
9 // ( 5.0 6.0 0.0 ) * ( 3.0 ) ( 6.0 ) = ( 70.7 )
10 // ( 7.0 0.0 8.0 ) * ( 7.0 ) = ( 123.8 )
11
12 // BSR block dimension
13 const int bsr_dim = 2;
14
15 // Number of block rows and columns
16 const int mb = 2;
17 const int nb = 2;
18
19 // Number of non-zero blocks
20 const int nnzb = 4;
21
22 // Number of rows and columns
23 const int m = mb * bsr_dim;
24 const int n = nb * bsr_dim;
25
26 // BSR row pointers
27 std::vector<int> hbsrRowPtr = {0, 2, 4};
28
29 // BSR column indices
30 std::vector<int> hbsrColInd = {0, 1, 0, 1};
31
32 // BSR values
33 std::vector<double> hbsrVal
34 = {1.0, 3.0, 0.0, 0.0, 2.0, 4.0, 0.0, 0.0, 5.0, 7.0, 6.0, 0.0, 0.0, 8.0, 0.0, 0.0};
35
36 // Block storage in column major
37 hipsparseDirection_t dir = HIPSPARSE_DIRECTION_COLUMN;
38
39 // Transposition of the matrix
40 hipsparseOperation_t trans = HIPSPARSE_OPERATION_NON_TRANSPOSE;
41
42 // Scalar alpha and beta
43 double alpha = 3.7;
44 double beta = 1.3;
45
46 // x and y
47 std::vector<double> hx = {1.0, 2.0, 3.0, 0.0};
48 std::vector<double> hy = {4.0, 5.0, 6.0, 7.0};
49
50 // Matrix descriptor
51 hipsparseMatDescr_t descr;
52 HIPSPARSE_CHECK(hipsparseCreateMatDescr(&descr));
53
54 // Offload data to device
55 int* dbsrRowPtr;
56 int* dbsrColInd;
57 double* dbsrVal;
58 double* dx;
59 double* dy;
60
61 HIP_CHECK(hipMalloc((void**)&dbsrRowPtr, sizeof(int) * (mb + 1)));
62 HIP_CHECK(hipMalloc((void**)&dbsrColInd, sizeof(int) * nnzb));
63 HIP_CHECK(hipMalloc((void**)&dbsrVal, sizeof(double) * nnzb * bsr_dim * bsr_dim));
64 HIP_CHECK(hipMalloc((void**)&dx, sizeof(double) * n));
65 HIP_CHECK(hipMalloc((void**)&dy, sizeof(double) * m));
66
67 HIP_CHECK(
68 hipMemcpy(dbsrRowPtr, hbsrRowPtr.data(), sizeof(int) * (mb + 1), hipMemcpyHostToDevice));
69 HIP_CHECK(hipMemcpy(dbsrColInd, hbsrColInd.data(), sizeof(int) * nnzb, hipMemcpyHostToDevice));
70 HIP_CHECK(hipMemcpy(
71 dbsrVal, hbsrVal.data(), sizeof(double) * nnzb * bsr_dim * bsr_dim, hipMemcpyHostToDevice));
72 HIP_CHECK(hipMemcpy(dx, hx.data(), sizeof(double) * n, hipMemcpyHostToDevice));
73 HIP_CHECK(hipMemcpy(dy, hy.data(), sizeof(double) * m, hipMemcpyHostToDevice));
74
75 // Call dbsrmv to perform y = alpha * A x + beta * y
76 HIPSPARSE_CHECK(hipsparseDbsrmv(handle,
77 dir,
78 trans,
79 mb,
80 nb,
81 nnzb,
82 &alpha,
83 descr,
84 dbsrVal,
85 dbsrRowPtr,
86 dbsrColInd,
87 bsr_dim,
88 dx,
89 &beta,
90 dy));
91
92 // Copy result back to host
93 HIP_CHECK(hipMemcpy(hy.data(), dy, sizeof(double) * m, hipMemcpyDeviceToHost));
94
95 std::cout << "hy" << std::endl;
96 for(int i = 0; i < m; i++)
97 {
98 std::cout << hy[i] << " ";
99 }
100 std::cout << std::endl;
101
102 // Clear hipSPARSE
103 HIPSPARSE_CHECK(hipsparseDestroyMatDescr(descr));
104 HIPSPARSE_CHECK(hipsparseDestroy(handle));
105
106 // Clear device memory
107 HIP_CHECK(hipFree(dbsrRowPtr));
108 HIP_CHECK(hipFree(dbsrColInd));
109 HIP_CHECK(hipFree(dbsrVal));
110 HIP_CHECK(hipFree(dx));
111 HIP_CHECK(hipFree(dy));
112
113 return 0;
114}
1int main(int argc, char* argv[])
2{
3 // hipSPARSE handle
4 hipsparseHandle_t handle;
5 HIPSPARSE_CHECK(hipsparseCreate(&handle));
6
7 // alpha * ( 1.0 0.0 2.0 ) * ( 1.0 ) + beta * ( 4.0 ) = ( 31.1 )
8 // ( 3.0 0.0 4.0 ) * ( 2.0 ) ( 5.0 ) = ( 62.0 )
9 // ( 5.0 6.0 0.0 ) * ( 3.0 ) ( 6.0 ) = ( 70.7 )
10 // ( 7.0 0.0 8.0 ) * ( 7.0 ) = ( 123.8 )
11
12 // BSR block dimension
13 const int bsr_dim = 2;
14
15 // Number of block rows and columns
16 const int mb = 2;
17 const int nb = 2;
18
19 // Number of non-zero blocks
20 const int nnzb = 4;
21
22 // Number of rows and columns
23 const int m = mb * bsr_dim;
24 const int n = nb * bsr_dim;
25
26 // BSR row pointers
27 int hbsrRowPtr[] = {0, 2, 4};
28
29 // BSR column indices
30 int hbsrColInd[] = {0, 1, 0, 1};
31
32 // BSR values
33 double hbsrVal[]
34 = {1.0, 3.0, 0.0, 0.0, 2.0, 4.0, 0.0, 0.0, 5.0, 7.0, 6.0, 0.0, 0.0, 8.0, 0.0, 0.0};
35
36 // Block storage in column major
37 hipsparseDirection_t dir = HIPSPARSE_DIRECTION_COLUMN;
38
39 // Transposition of the matrix
40 hipsparseOperation_t trans = HIPSPARSE_OPERATION_NON_TRANSPOSE;
41
42 // Scalar alpha and beta
43 double alpha = 3.7;
44 double beta = 1.3;
45
46 // x and y
47 double hx[] = {1.0, 2.0, 3.0, 0.0};
48 double hy[] = {4.0, 5.0, 6.0, 7.0};
49
50 // Matrix descriptor
51 hipsparseMatDescr_t descr;
52 HIPSPARSE_CHECK(hipsparseCreateMatDescr(&descr));
53
54 // Offload data to device
55 int* dbsrRowPtr;
56 int* dbsrColInd;
57 double* dbsrVal;
58 double* dx;
59 double* dy;
60
61 HIP_CHECK(hipMalloc((void**)&dbsrRowPtr, sizeof(int) * (mb + 1)));
62 HIP_CHECK(hipMalloc((void**)&dbsrColInd, sizeof(int) * nnzb));
63 HIP_CHECK(hipMalloc((void**)&dbsrVal, sizeof(double) * nnzb * bsr_dim * bsr_dim));
64 HIP_CHECK(hipMalloc((void**)&dx, sizeof(double) * n));
65 HIP_CHECK(hipMalloc((void**)&dy, sizeof(double) * m));
66
67 HIP_CHECK(hipMemcpy(dbsrRowPtr, hbsrRowPtr, sizeof(int) * (mb + 1), hipMemcpyHostToDevice));
68 HIP_CHECK(hipMemcpy(dbsrColInd, hbsrColInd, sizeof(int) * nnzb, hipMemcpyHostToDevice));
69 HIP_CHECK(hipMemcpy(
70 dbsrVal, hbsrVal, sizeof(double) * nnzb * bsr_dim * bsr_dim, hipMemcpyHostToDevice));
71 HIP_CHECK(hipMemcpy(dx, hx, sizeof(double) * n, hipMemcpyHostToDevice));
72 HIP_CHECK(hipMemcpy(dy, hy, sizeof(double) * m, hipMemcpyHostToDevice));
73
74 // Call dbsrmv to perform y = alpha * A x + beta * y
75 HIPSPARSE_CHECK(hipsparseDbsrmv(handle,
76 dir,
77 trans,
78 mb,
79 nb,
80 nnzb,
81 &alpha,
82 descr,
83 dbsrVal,
84 dbsrRowPtr,
85 dbsrColInd,
86 bsr_dim,
87 dx,
88 &beta,
89 dy));
90
91 // Copy result back to host
92 HIP_CHECK(hipMemcpy(hy, dy, sizeof(double) * m, hipMemcpyDeviceToHost));
93
94 printf("hy\n");
95 for(int i = 0; i < m; i++)
96 {
97 printf("%f ", hy[i]);
98 }
99 printf("\n");
100
101 // Clear hipSPARSE
102 HIPSPARSE_CHECK(hipsparseDestroyMatDescr(descr));
103 HIPSPARSE_CHECK(hipsparseDestroy(handle));
104
105 // Clear device memory
106 HIP_CHECK(hipFree(dbsrRowPtr));
107 HIP_CHECK(hipFree(dbsrColInd));
108 HIP_CHECK(hipFree(dbsrVal));
109 HIP_CHECK(hipFree(dx));
110 HIP_CHECK(hipFree(dy));
111
112 return 0;
113}
1program example_fortran_bsrmv
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_bsr_row_ptr(3), h_bsr_col_ind(4)
38 real(8), target :: h_bsr_val(16), h_x(4), h_y(4)
39
40 type(c_ptr) :: d_bsr_row_ptr, d_bsr_col_ind, d_bsr_val, d_x, d_y
41
42 integer :: i
43 integer(c_int) :: bsr_dim, mb, nb, nnzb, m, n, dir, trans
44
45 real(c_double), target :: alpha, beta
46
47 type(c_ptr) :: handle, descr
48
49 integer :: version
50
51! BSR block dimension
52 bsr_dim = 2
53
54! Number of block rows and columns
55 mb = 2
56 nb = 2
57
58! Number of non-zero blocks
59 nnzb = 4
60
61! Number of rows and columns
62 m = mb * bsr_dim
63 n = nb * bsr_dim
64
65! Fill BSR structure
66 h_bsr_row_ptr = (/0, 2, 4/)
67 h_bsr_col_ind = (/0, 1, 0, 1/)
68 h_bsr_val = (/1.0d0, 3.0d0, 0.0d0, 0.0d0, 2.0d0, 4.0d0, 0.0d0, 0.0d0, &
69 5.0d0, 7.0d0, 6.0d0, 0.0d0, 0.0d0, 8.0d0, 0.0d0, 0.0d0/)
70
71! Block storage direction and transposition
72 dir = HIPSPARSE_DIRECTION_COLUMN
73 trans = HIPSPARSE_OPERATION_NON_TRANSPOSE
74
75! Scalar alpha and beta
76 alpha = 3.7d0
77 beta = 1.3d0
78
79! x and y vectors
80 h_x = (/1.0d0, 2.0d0, 3.0d0, 0.0d0/)
81 h_y = (/4.0d0, 5.0d0, 6.0d0, 7.0d0/)
82
83! Allocate device memory
84 call HIP_CHECK(hipMalloc(d_bsr_row_ptr, int(mb + 1, c_size_t) * 4))
85 call HIP_CHECK(hipMalloc(d_bsr_col_ind, int(nnzb, c_size_t) * 4))
86 call HIP_CHECK(hipMalloc(d_bsr_val, int(nnzb * bsr_dim * bsr_dim, c_size_t) * 8))
87 call HIP_CHECK(hipMalloc(d_x, int(n, c_size_t) * 8))
88 call HIP_CHECK(hipMalloc(d_y, int(m, c_size_t) * 8))
89
90! Copy host data to device
91 call HIP_CHECK(hipMemcpy(d_bsr_row_ptr, c_loc(h_bsr_row_ptr), int(mb + 1, c_size_t) * 4, 1))
92 call HIP_CHECK(hipMemcpy(d_bsr_col_ind, c_loc(h_bsr_col_ind), int(nnzb, c_size_t) * 4, 1))
93 call HIP_CHECK(hipMemcpy(d_bsr_val, c_loc(h_bsr_val), int(nnzb * bsr_dim * bsr_dim, c_size_t) * 8, 1))
94 call HIP_CHECK(hipMemcpy(d_x, c_loc(h_x), int(n, c_size_t) * 8, 1))
95 call HIP_CHECK(hipMemcpy(d_y, c_loc(h_y), int(m, c_size_t) * 8, 1))
96
97! Create hipSPARSE handle
98 call HIPSPARSE_CHECK(hipsparseCreate(handle))
99
100! Create matrix descriptor
101 call HIPSPARSE_CHECK(hipsparseCreateMatDescr(descr))
102
103! Get hipSPARSE version
104 call HIPSPARSE_CHECK(hipsparseGetVersion(handle, version))
105 write(*,fmt='(A,I0,A,I0,A,I0)') 'hipSPARSE version: ', version / 100000, '.', &
106 mod(version / 100, 1000), '.', mod(version, 100)
107
108! Call dbsrmv to perform y = alpha * A * x + beta * y
109 call HIPSPARSE_CHECK(hipsparseDbsrmv(handle, &
110 dir, &
111 trans, &
112 mb, &
113 nb, &
114 nnzb, &
115 c_loc(alpha), &
116 descr, &
117 d_bsr_val, &
118 d_bsr_row_ptr, &
119 d_bsr_col_ind, &
120 bsr_dim, &
121 d_x, &
122 c_loc(beta), &
123 d_y))
124
125! Copy result back to host
126 call HIP_CHECK(hipMemcpy(c_loc(h_y), d_y, int(m, c_size_t) * 8, 2))
127
128! Print result
129 write(*,fmt='(A)',advance='no') 'hy:'
130 do i = 1, m
131 write(*,fmt='(A,F0.2)',advance='no') ' ', h_y(i)
132 end do
133 write(*,*)
134
135! Clear hipSPARSE
136 call HIPSPARSE_CHECK(hipsparseDestroyMatDescr(descr))
137 call HIPSPARSE_CHECK(hipsparseDestroy(handle))
138
139! Clear device memory
140 call HIP_CHECK(hipFree(d_bsr_row_ptr))
141 call HIP_CHECK(hipFree(d_bsr_col_ind))
142 call HIP_CHECK(hipFree(d_bsr_val))
143 call HIP_CHECK(hipFree(d_x))
144 call HIP_CHECK(hipFree(d_y))
145
146end program example_fortran_bsrmv
hipsparseXbsrxmv()#
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
hipsparseXbsrsv2_zeroPivot()#
Warning
doxygenfunction: Cannot find file: /home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-hipsparse/checkouts/7.13.0-preview/projects/hipsparse/docs/doxygen/xml/index.xml
hipsparseXbsrsv2_bufferSize()#
Warning
doxygenfunction: Cannot find file: /home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-hipsparse/checkouts/7.13.0-preview/projects/hipsparse/docs/doxygen/xml/index.xml
Warning
doxygenfunction: Cannot find file: /home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-hipsparse/checkouts/7.13.0-preview/projects/hipsparse/docs/doxygen/xml/index.xml
Warning
doxygenfunction: Cannot find file: /home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-hipsparse/checkouts/7.13.0-preview/projects/hipsparse/docs/doxygen/xml/index.xml
Warning
doxygenfunction: Cannot find file: /home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-hipsparse/checkouts/7.13.0-preview/projects/hipsparse/docs/doxygen/xml/index.xml
hipsparseXbsrsv2_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
hipsparseXbsrsv2_analysis()#
Warning
doxygenfunction: Cannot find file: /home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-hipsparse/checkouts/7.13.0-preview/projects/hipsparse/docs/doxygen/xml/index.xml
Warning
doxygenfunction: Cannot find file: /home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-hipsparse/checkouts/7.13.0-preview/projects/hipsparse/docs/doxygen/xml/index.xml
Warning
doxygenfunction: Cannot find file: /home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-hipsparse/checkouts/7.13.0-preview/projects/hipsparse/docs/doxygen/xml/index.xml
Warning
doxygenfunction: Cannot find file: /home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-hipsparse/checkouts/7.13.0-preview/projects/hipsparse/docs/doxygen/xml/index.xml
hipsparseXbsrsv2_solve()#
Warning
doxygenfunction: Cannot find file: /home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-hipsparse/checkouts/7.13.0-preview/projects/hipsparse/docs/doxygen/xml/index.xml
Warning
doxygenfunction: Cannot find file: /home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-hipsparse/checkouts/7.13.0-preview/projects/hipsparse/docs/doxygen/xml/index.xml
Warning
doxygenfunction: Cannot find file: /home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-hipsparse/checkouts/7.13.0-preview/projects/hipsparse/docs/doxygen/xml/index.xml
Warning
doxygenfunction: Cannot find file: /home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-hipsparse/checkouts/7.13.0-preview/projects/hipsparse/docs/doxygen/xml/index.xml
1int main(int argc, char* argv[])
2{
3 // hipSPARSE handle
4 hipsparseHandle_t handle;
5 HIPSPARSE_CHECK(hipsparseCreate(&handle));
6
7 // A = ( 1.0 0.0 0.0 0.0 )
8 // ( 2.0 3.0 0.0 0.0 )
9 // ( 4.0 5.0 6.0 0.0 )
10 // ( 7.0 0.0 8.0 9.0 )
11 //
12 // with bsr_dim = 2
13 //
14 // -------------------
15 // = | 1.0 0.0 | 0.0 0.0 |
16 // | 2.0 3.0 | 0.0 0.0 |
17 // -------------------
18 // | 4.0 5.0 | 6.0 0.0 |
19 // | 7.0 0.0 | 8.0 9.0 |
20 // -------------------
21
22 // Number of rows (matrix must be square)
23 const int m = 4;
24
25 // Number of block rows and block columns
26 const int mb = 2;
27 const int nb = 2;
28
29 // BSR block dimension
30 const int bsr_dim = 2;
31
32 // Number of non-zero blocks
33 const int nnzb = 3;
34
35 // BSR row pointers
36 std::vector<int> hbsrRowPtr = {0, 1, 3};
37
38 // BSR column indices
39 std::vector<int> hbsrColInd = {0, 0, 1};
40
41 // BSR values
42 std::vector<double> hbsrVal = {1.0, 2.0, 0.0, 3.0, 4.0, 7.0, 5.0, 0.0, 6.0, 8.0, 0.0, 9.0};
43
44 // Storage scheme of the BSR blocks
45 hipsparseDirection_t dir = HIPSPARSE_DIRECTION_COLUMN;
46
47 // Transposition of the matrix and rhs matrix
48 hipsparseOperation_t trans = HIPSPARSE_OPERATION_NON_TRANSPOSE;
49
50 // Solve policy
51 hipsparseSolvePolicy_t solve_policy = HIPSPARSE_SOLVE_POLICY_USE_LEVEL;
52
53 // Scalar alpha and beta
54 double alpha = 3.7;
55
56 std::vector<double> hx = {1, 2, 3, 4};
57 std::vector<double> hy(m);
58
59 // Offload data to device
60 int* dbsrRowPtr;
61 int* dbsrColInd;
62 double* dbsrVal;
63 double* dx;
64 double* dy;
65
66 HIP_CHECK(hipMalloc((void**)&dbsrRowPtr, sizeof(int) * (mb + 1)));
67 HIP_CHECK(hipMalloc((void**)&dbsrColInd, sizeof(int) * nnzb));
68 HIP_CHECK(hipMalloc((void**)&dbsrVal, sizeof(double) * nnzb * bsr_dim * bsr_dim));
69 HIP_CHECK(hipMalloc((void**)&dx, sizeof(double) * m));
70 HIP_CHECK(hipMalloc((void**)&dy, sizeof(double) * m));
71
72 HIP_CHECK(
73 hipMemcpy(dbsrRowPtr, hbsrRowPtr.data(), sizeof(int) * (mb + 1), hipMemcpyHostToDevice));
74 HIP_CHECK(hipMemcpy(dbsrColInd, hbsrColInd.data(), sizeof(int) * nnzb, hipMemcpyHostToDevice));
75 HIP_CHECK(hipMemcpy(
76 dbsrVal, hbsrVal.data(), sizeof(double) * nnzb * bsr_dim * bsr_dim, hipMemcpyHostToDevice));
77 HIP_CHECK(hipMemcpy(dx, hx.data(), sizeof(double) * m, hipMemcpyHostToDevice));
78
79 // Matrix descriptor
80 hipsparseMatDescr_t descr;
81 HIPSPARSE_CHECK(hipsparseCreateMatDescr(&descr));
82
83 // Matrix fill mode
84 HIPSPARSE_CHECK(hipsparseSetMatFillMode(descr, HIPSPARSE_FILL_MODE_LOWER));
85
86 // Matrix diagonal type
87 HIPSPARSE_CHECK(hipsparseSetMatDiagType(descr, HIPSPARSE_DIAG_TYPE_UNIT));
88
89 // Matrix info structure
90 bsrsv2Info_t info;
91 HIPSPARSE_CHECK(hipsparseCreateBsrsv2Info(&info));
92
93 // Obtain required buffer size
94 int buffer_size;
95 HIPSPARSE_CHECK(hipsparseDbsrsv2_bufferSize(handle,
96 dir,
97 trans,
98 mb,
99 nnzb,
100 descr,
101 dbsrVal,
102 dbsrRowPtr,
103 dbsrColInd,
104 bsr_dim,
105 info,
106 &buffer_size));
107
108 // Allocate temporary buffer
109 void* dbuffer;
110 HIP_CHECK(hipMalloc(&dbuffer, buffer_size));
111
112 // Perform analysis step
113 HIPSPARSE_CHECK(hipsparseDbsrsv2_analysis(handle,
114 dir,
115 trans,
116 mb,
117 nnzb,
118 descr,
119 dbsrVal,
120 dbsrRowPtr,
121 dbsrColInd,
122 bsr_dim,
123 info,
124 solve_policy,
125 dbuffer));
126
127 // Call dbsrsm to perform lower triangular solve LX = B
128 HIPSPARSE_CHECK(hipsparseDbsrsv2_solve(handle,
129 dir,
130 trans,
131 mb,
132 nnzb,
133 &alpha,
134 descr,
135 dbsrVal,
136 dbsrRowPtr,
137 dbsrColInd,
138 bsr_dim,
139 info,
140 dx,
141 dy,
142 solve_policy,
143 dbuffer));
144
145 // Check for zero pivots
146 int pivot;
147 hipsparseStatus_t status = hipsparseXbsrsv2_zeroPivot(handle, info, &pivot);
148
149 if(status == HIPSPARSE_STATUS_ZERO_PIVOT)
150 {
151 std::cout << "Found zero pivot in matrix row " << pivot << std::endl;
152 }
153
154 // Copy results back to the host
155 HIP_CHECK(hipMemcpy(hy.data(), dy, sizeof(double) * m, hipMemcpyDeviceToHost));
156
157 std::cout << "hy" << std::endl;
158 for(int i = 0; i < m; i++)
159 {
160 std::cout << hy[i] << " ";
161 }
162 std::cout << std::endl;
163
164 // Clear hipSPARSE
165 HIPSPARSE_CHECK(hipsparseDestroyBsrsv2Info(info));
166 HIPSPARSE_CHECK(hipsparseDestroyMatDescr(descr));
167 HIPSPARSE_CHECK(hipsparseDestroy(handle));
168
169 // Clear device memory
170 HIP_CHECK(hipFree(dbsrRowPtr));
171 HIP_CHECK(hipFree(dbsrColInd));
172 HIP_CHECK(hipFree(dbsrVal));
173 HIP_CHECK(hipFree(dx));
174 HIP_CHECK(hipFree(dy));
175 HIP_CHECK(hipFree(dbuffer));
176
177 return 0;
178}
1int main(int argc, char* argv[])
2{
3 // hipSPARSE handle
4 hipsparseHandle_t handle;
5 HIPSPARSE_CHECK(hipsparseCreate(&handle));
6
7 // A = ( 1.0 0.0 0.0 0.0 )
8 // ( 2.0 3.0 0.0 0.0 )
9 // ( 4.0 5.0 6.0 0.0 )
10 // ( 7.0 0.0 8.0 9.0 )
11 //
12 // with bsr_dim = 2
13 //
14 // -------------------
15 // = | 1.0 0.0 | 0.0 0.0 |
16 // | 2.0 3.0 | 0.0 0.0 |
17 // -------------------
18 // | 4.0 5.0 | 6.0 0.0 |
19 // | 7.0 0.0 | 8.0 9.0 |
20 // -------------------
21
22 // Number of rows (matrix must be square)
23 const int m = 4;
24
25 // Number of block rows and block columns
26 const int mb = 2;
27 const int nb = 2;
28
29 // BSR block dimension
30 const int bsr_dim = 2;
31
32 // Number of non-zero blocks
33 const int nnzb = 3;
34
35 // BSR row pointers
36 int hbsrRowPtr[] = {0, 1, 3};
37
38 // BSR column indices
39 int hbsrColInd[] = {0, 0, 1};
40
41 // BSR values
42 double hbsrVal[] = {1.0, 2.0, 0.0, 3.0, 4.0, 7.0, 5.0, 0.0, 6.0, 8.0, 0.0, 9.0};
43
44 // Storage scheme of the BSR blocks
45 hipsparseDirection_t dir = HIPSPARSE_DIRECTION_COLUMN;
46
47 // Transposition of the matrix and rhs matrix
48 hipsparseOperation_t trans = HIPSPARSE_OPERATION_NON_TRANSPOSE;
49
50 // Solve policy
51 hipsparseSolvePolicy_t solve_policy = HIPSPARSE_SOLVE_POLICY_USE_LEVEL;
52
53 // Scalar alpha and beta
54 double alpha = 3.7;
55
56 double hx[] = {1, 2, 3, 4};
57 double hy[m];
58
59 // Offload data to device
60 int* dbsrRowPtr;
61 int* dbsrColInd;
62 double* dbsrVal;
63 double* dx;
64 double* dy;
65
66 HIP_CHECK(hipMalloc((void**)&dbsrRowPtr, sizeof(int) * (mb + 1)));
67 HIP_CHECK(hipMalloc((void**)&dbsrColInd, sizeof(int) * nnzb));
68 HIP_CHECK(hipMalloc((void**)&dbsrVal, sizeof(double) * nnzb * bsr_dim * bsr_dim));
69 HIP_CHECK(hipMalloc((void**)&dx, sizeof(double) * m));
70 HIP_CHECK(hipMalloc((void**)&dy, sizeof(double) * m));
71
72 HIP_CHECK(hipMemcpy(dbsrRowPtr, hbsrRowPtr, sizeof(int) * (mb + 1), hipMemcpyHostToDevice));
73 HIP_CHECK(hipMemcpy(dbsrColInd, hbsrColInd, sizeof(int) * nnzb, hipMemcpyHostToDevice));
74 HIP_CHECK(hipMemcpy(
75 dbsrVal, hbsrVal, sizeof(double) * nnzb * bsr_dim * bsr_dim, hipMemcpyHostToDevice));
76 HIP_CHECK(hipMemcpy(dx, hx, sizeof(double) * m, hipMemcpyHostToDevice));
77
78 // Matrix descriptor
79 hipsparseMatDescr_t descr;
80 HIPSPARSE_CHECK(hipsparseCreateMatDescr(&descr));
81
82 // Matrix fill mode
83 HIPSPARSE_CHECK(hipsparseSetMatFillMode(descr, HIPSPARSE_FILL_MODE_LOWER));
84
85 // Matrix diagonal type
86 HIPSPARSE_CHECK(hipsparseSetMatDiagType(descr, HIPSPARSE_DIAG_TYPE_UNIT));
87
88 // Matrix info structure
89 bsrsv2Info_t info;
90 HIPSPARSE_CHECK(hipsparseCreateBsrsv2Info(&info));
91
92 // Obtain required buffer size
93 int buffer_size;
94 HIPSPARSE_CHECK(hipsparseDbsrsv2_bufferSize(handle,
95 dir,
96 trans,
97 mb,
98 nnzb,
99 descr,
100 dbsrVal,
101 dbsrRowPtr,
102 dbsrColInd,
103 bsr_dim,
104 info,
105 &buffer_size));
106
107 // Allocate temporary buffer
108 void* dbuffer;
109 HIP_CHECK(hipMalloc(&dbuffer, buffer_size));
110
111 // Perform analysis step
112 HIPSPARSE_CHECK(hipsparseDbsrsv2_analysis(handle,
113 dir,
114 trans,
115 mb,
116 nnzb,
117 descr,
118 dbsrVal,
119 dbsrRowPtr,
120 dbsrColInd,
121 bsr_dim,
122 info,
123 solve_policy,
124 dbuffer));
125
126 // Call dbsrsm to perform lower triangular solve LX = B
127 HIPSPARSE_CHECK(hipsparseDbsrsv2_solve(handle,
128 dir,
129 trans,
130 mb,
131 nnzb,
132 &alpha,
133 descr,
134 dbsrVal,
135 dbsrRowPtr,
136 dbsrColInd,
137 bsr_dim,
138 info,
139 dx,
140 dy,
141 solve_policy,
142 dbuffer));
143
144 // Check for zero pivots
145 int pivot;
146 hipsparseStatus_t status = hipsparseXbsrsv2_zeroPivot(handle, info, &pivot);
147
148 if(status == HIPSPARSE_STATUS_ZERO_PIVOT)
149 {
150 printf("Found zero pivot in matrix row %d\n", pivot);
151 }
152
153 // Copy results back to the host
154 HIP_CHECK(hipMemcpy(hy, dy, sizeof(double) * m, hipMemcpyDeviceToHost));
155
156 printf("hy\n");
157 for(int i = 0; i < m; i++)
158 {
159 printf("%f ", hy[i]);
160 }
161 printf("\n");
162
163 // Clear hipSPARSE
164 HIPSPARSE_CHECK(hipsparseDestroyBsrsv2Info(info));
165 HIPSPARSE_CHECK(hipsparseDestroyMatDescr(descr));
166 HIPSPARSE_CHECK(hipsparseDestroy(handle));
167
168 // Clear device memory
169 HIP_CHECK(hipFree(dbsrRowPtr));
170 HIP_CHECK(hipFree(dbsrColInd));
171 HIP_CHECK(hipFree(dbsrVal));
172 HIP_CHECK(hipFree(dx));
173 HIP_CHECK(hipFree(dy));
174 HIP_CHECK(hipFree(dbuffer));
175
176 return 0;
177}
hipsparseXgemvi_bufferSize()#
Warning
doxygenfunction: Cannot find file: /home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-hipsparse/checkouts/7.13.0-preview/projects/hipsparse/docs/doxygen/xml/index.xml
Warning
doxygenfunction: Cannot find file: /home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-hipsparse/checkouts/7.13.0-preview/projects/hipsparse/docs/doxygen/xml/index.xml
Warning
doxygenfunction: Cannot find file: /home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-hipsparse/checkouts/7.13.0-preview/projects/hipsparse/docs/doxygen/xml/index.xml
Warning
doxygenfunction: Cannot find file: /home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-hipsparse/checkouts/7.13.0-preview/projects/hipsparse/docs/doxygen/xml/index.xml
hipsparseXgemvi()#
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 hipsparseOperation_t opA = HIPSPARSE_OPERATION_NON_TRANSPOSE;
4 hipsparseIndexBase_t idxBase = HIPSPARSE_INDEX_BASE_ZERO;
5
6 // Scalar alpha and beta
7 float alpha = 1.0f;
8 float beta = 1.0f;
9
10 const int m = 4; // Number of rows of A
11 const int n = 4; // Number of columns of A
12 const int lda = m; // leading dimension of A
13
14 // A = 1 2 3 4
15 // 5 6 7 8
16 // 2 4 6 8
17 // 4 3 2 1
18 std::vector<float> hA = {1.0f,
19 5.0f,
20 2.0f,
21 4.0f,
22 2.0f,
23 6.0f,
24 4.0f,
25 3.0f,
26 3.0f,
27 7.0f,
28 6.0f,
29 2.0f,
30 4.0f,
31 8.0f,
32 8.0f,
33 1.0f};
34
35 // Sparse vector x
36 int nnz = 2;
37 std::vector<int> hxInd = {0, 2};
38 std::vector<float> hx = {10.0f, 11.0f};
39
40 // Dense vector y
41 std::vector<float> hy = {1.0f, 2.0f, 3.0f, 4.0f};
42
43 // Device data
44 float* dA = nullptr;
45 HIP_CHECK(hipMalloc((void**)&dA, sizeof(float) * m * n));
46
47 int* dxInd = nullptr;
48 float* dx = nullptr;
49 HIP_CHECK(hipMalloc((void**)&dxInd, sizeof(int) * nnz));
50 HIP_CHECK(hipMalloc((void**)&dx, sizeof(float) * nnz));
51
52 float* dy = nullptr;
53 HIP_CHECK(hipMalloc((void**)&dy, sizeof(float) * m));
54
55 // Copy data from host to device
56 HIP_CHECK(hipMemcpy(dA, hA.data(), sizeof(float) * m * n, hipMemcpyHostToDevice));
57 HIP_CHECK(hipMemcpy(dxInd, hxInd.data(), sizeof(int) * nnz, hipMemcpyHostToDevice));
58 HIP_CHECK(hipMemcpy(dx, hx.data(), sizeof(float) * nnz, hipMemcpyHostToDevice));
59 HIP_CHECK(hipMemcpy(dy, hy.data(), sizeof(float) * m, hipMemcpyHostToDevice));
60
61 // hipSPARSE handle
62 hipsparseHandle_t handle;
63 HIPSPARSE_CHECK(hipsparseCreate(&handle));
64
65 // Call hipsparseSgemvi to perform y = alpha * A * x + beta * y
66 int bufferSize = 0;
67 HIPSPARSE_CHECK(hipsparseSgemvi_bufferSize(handle, opA, m, n, nnz, &bufferSize));
68
69 void* dbuffer = nullptr;
70 HIP_CHECK(hipMalloc((void**)&dbuffer, bufferSize));
71
72 HIPSPARSE_CHECK(hipsparseSgemvi(
73 handle, opA, m, n, &alpha, dA, lda, nnz, dx, dxInd, &beta, dy, idxBase, dbuffer));
74
75 // Copy result back to host
76 HIP_CHECK(hipMemcpy(hy.data(), dy, sizeof(float) * m, hipMemcpyDeviceToHost));
77
78 // Print the result (optional)
79 std::cout << "hy" << std::endl;
80 for(int i = 0; i < m; i++)
81 {
82 std::cout << hy[i] << " ";
83 }
84 std::cout << "" << std::endl;
85
86 // Clear hipSPARSE
87 HIPSPARSE_CHECK(hipsparseDestroy(handle));
88
89 // Clear device memory
90 HIP_CHECK(hipFree(dA));
91 HIP_CHECK(hipFree(dxInd));
92 HIP_CHECK(hipFree(dx));
93 HIP_CHECK(hipFree(dy));
94 HIP_CHECK(hipFree(dbuffer));
95
96 return 0;
97}
1int main(int argc, char* argv[])
2{
3 hipsparseOperation_t opA = HIPSPARSE_OPERATION_NON_TRANSPOSE;
4 hipsparseIndexBase_t idxBase = HIPSPARSE_INDEX_BASE_ZERO;
5
6 // Scalar alpha and beta
7 float alpha = 1.0f;
8 float beta = 1.0f;
9
10 const int m = 4; // Number of rows of A
11 const int n = 4; // Number of columns of A
12 const int lda = m; // leading dimension of A
13
14 // A = 1 2 3 4
15 // 5 6 7 8
16 // 2 4 6 8
17 // 4 3 2 1
18 float hA[16] = {1.0f,
19 5.0f,
20 2.0f,
21 4.0f,
22 2.0f,
23 6.0f,
24 4.0f,
25 3.0f,
26 3.0f,
27 7.0f,
28 6.0f,
29 2.0f,
30 4.0f,
31 8.0f,
32 8.0f,
33 1.0f};
34
35 // Sparse vector x
36 int nnz = 2;
37 int hxInd[2] = {0, 2};
38 float hx[2] = {10.0f, 11.0f};
39
40 // Dense vector y
41 float hy[4] = {1.0f, 2.0f, 3.0f, 4.0f};
42
43 // Device data
44 float* dA = NULL;
45 HIP_CHECK(hipMalloc((void**)&dA, sizeof(float) * m * n));
46
47 int* dxInd = NULL;
48 float* dx = NULL;
49 HIP_CHECK(hipMalloc((void**)&dxInd, sizeof(int) * nnz));
50 HIP_CHECK(hipMalloc((void**)&dx, sizeof(float) * nnz));
51
52 float* dy = NULL;
53 HIP_CHECK(hipMalloc((void**)&dy, sizeof(float) * m));
54
55 // Copy data from host to device
56 HIP_CHECK(hipMemcpy(dA, hA, sizeof(float) * m * n, hipMemcpyHostToDevice));
57 HIP_CHECK(hipMemcpy(dxInd, hxInd, sizeof(int) * nnz, hipMemcpyHostToDevice));
58 HIP_CHECK(hipMemcpy(dx, hx, sizeof(float) * nnz, hipMemcpyHostToDevice));
59 HIP_CHECK(hipMemcpy(dy, hy, sizeof(float) * m, hipMemcpyHostToDevice));
60
61 // hipSPARSE handle
62 hipsparseHandle_t handle;
63 HIPSPARSE_CHECK(hipsparseCreate(&handle));
64
65 // Call hipsparseSgemvi to perform y = alpha * A * x + beta * y
66 int bufferSize = 0;
67 HIPSPARSE_CHECK(hipsparseSgemvi_bufferSize(handle, opA, m, n, nnz, &bufferSize));
68
69 void* dbuffer = NULL;
70 HIP_CHECK(hipMalloc((void**)&dbuffer, bufferSize));
71
72 HIPSPARSE_CHECK(hipsparseSgemvi(
73 handle, opA, m, n, &alpha, dA, lda, nnz, dx, dxInd, &beta, dy, idxBase, dbuffer));
74
75 // Copy result back to host
76 HIP_CHECK(hipMemcpy(hy, dy, sizeof(float) * m, hipMemcpyDeviceToHost));
77
78 // Print the result (optional)
79 printf("hy\n");
80 for(int i = 0; i < m; i++)
81 {
82 printf("%f ", hy[i]);
83 }
84 printf("\n");
85
86 // Clear hipSPARSE
87 HIPSPARSE_CHECK(hipsparseDestroy(handle));
88
89 // Clear device memory
90 HIP_CHECK(hipFree(dA));
91 HIP_CHECK(hipFree(dxInd));
92 HIP_CHECK(hipFree(dx));
93 HIP_CHECK(hipFree(dy));
94 HIP_CHECK(hipFree(dbuffer));
95
96 return 0;
97}