This example illustrates the use of hipSOLVER's linear least-squares solver, gels
. The gels
functions solve an overdetermined (or underdetermined) linear system defined by an geqrf
(or the LQ factorization computed by gelqf
). The problem solved by this function is of the form
If
This example shows how
- Parse the user inputs, declare several constants for the sizes of the matrices.
- Allocate the input- and output matrices on the host and device, initialize the input data.
- Create a hipSOLVER handle.
- Query the size of the working space of the
gels
function and allocate the required amount of device memory. - Call the
gels
function to solve the linear least squares problem:$A\times X=B$ . - Copy the device result back to the host.
- Print the status value of the
gels
function. - Free device resources and the hipSOLVER handle.
- Validate that the result found is correct by calculating
$A\times X$ , and print the result.
The application provides the following optional command line arguments:
-
--n <n>
. Number of rows of input matrix$A$ , the default value is3
. -
--m <m>
. Number of columns of input matrix$A$ , the default value is2
.
-
hipSOLVER is initialized by calling
hipsolverCreate(hipsolverHandle_t*)
and it is terminated by callinghipsolverDestroy(hipsolverHandle_t)
. -
hipsolver(SS|DD|CC|ZZ)gels
solves the system of linear equations defined by$A\times X=B$ , where$A$ is anm
-by-n
matrix,$X$ is ann
-by-nrhs
matrix, and$B$ is anm
-by-nrhs
matrix. Depending on the character matched in(SS|DD|CC|ZZ)
, the solution can be obtained with different precisions:-
S
(single-precision:float
). -
D
(double-precision:double
). -
C
(single-precision complex:hipFloatComplex
). -
Z
(double-precision complex:hipDoubleComplex
).
The
gels
function also requires the specification of the leading dimension of all matrices. The leading dimension specifies the number of elements between the beginnings of successive matrix vectors. In other fields, this may be referred to as the stride. This concept allows the matrix used in thegels
function to be a sub-matrix of a larger one. Since hipSOLVER matrices are stored in column-major order, the leading dimension must be greater than or equal to the number of rows of the matrix. -
-
hipsolver(SS|DD|CC|ZZ)gels_bufferSize
allows to obtain the size needed for the working space for thehipsolver(SS|DD|CC|ZZ)gels
function.
hipsolverDDgels
hipsolverDDgels_bufferSize
hipsolverHandle_t
hipsolverCreate
hipsolverDestroy
hipFree
hipMalloc
hipMemcpy
hipMemcpyHostToDevice
hipMemcpyDeviceToHost