![]() |
cuLite v0.3.1
A lite CUDA C++ Interface
|
This section demonstrates how to compute the singular value decomposition (SVD) of a dense matrix on GPU. The SVD factorizes A = U * S * V*, where S contains the singular values in descending order. Policies control whether left (U) and right (V) singular vectors are computed. Available policies are Economy, Full, and NoCalculation.
| Source Code | Console Output |
|---|---|
#include <iostream>
#include <cla3p/dense.hpp>
#include <culite/dense.hpp>
#include <culite/svd.hpp>
int main()
{
/*
* Create a random (5x3) dense matrix on host and transfer to device
*/
HostA >> A;
std::cout << "A:\n" << A;
/*
* Set a simple svd object for A
*/
/*
* Decompose A into USV* product
*/
svdA.decompose(A);
std::cout << "SVD Decomposition of A\n";
std::cout << "----------------------\n";
std::cout
<< "S:\n" << svdA.singularValues()
<< "U:\n" << svdA.leftSingularVectors()
<< "V:\n" << svdA.rightSingularVectors();
/*
* Set a svd object with no calculation for V
*/
culite::svdPolicy_t policyU = culite::svdPolicy_t::Economy;
culite::svdPolicy_t policyV = culite::svdPolicy_t::NoCalculation;
culite::DefaultSVD<culite::dns::RdMatrix> svdB(policyU, policyV);
svdB.decompose(A);
std::cout << "SVD Decomposition of A (no V)\n";
std::cout << "-----------------------------\n";
std::cout
<< "S:\n" << svdB.singularValues()
<< "U:\n" << svdB.leftSingularVectors()
<< "V(empty):\n" << svdB.rightSingularVectors();
return 0;
}
static XxMatrix< T_Scalar > random(int_t nr, int_t nc, const Property &pr=Property::General(), T_RScalar lo=T_RScalar(0), T_RScalar hi=T_RScalar(1)) Singular Value Decomposition (SVD) solver using cuSOLVER. Definition default_svd.hpp:66 const T_Matrix & rightSingularVectors() const Gets the computed right singular vectors. Definition default_svd.hpp:185 const T_Matrix & leftSingularVectors() const Gets the computed left singular vectors. Definition default_svd.hpp:176 const T_RVector & singularValues() const Gets the computed singular values. Definition default_svd.hpp:167 void decompose(const T_Matrix &mat) Performs singular value decomposition on the input matrix. XxMatrix< real_t > RdMatrix ::cla3p::svdPolicy_t svdPolicy_t Singular vector computation policy enumeration. Definition enums.hpp:95 XxMatrix< real_t > RdMatrix Double precision real matrix. Definition dense.hpp:55 | A:
0 1 2
0 | 8.401877e-01 1.975514e-01 4.773971e-01
1 | 3.943829e-01 3.352228e-01 6.288709e-01
2 | 7.830992e-01 7.682296e-01 3.647845e-01
3 | 7.984400e-01 2.777747e-01 5.134009e-01
4 | 9.116474e-01 5.539700e-01 9.522297e-01
SVD Decomposition of A
----------------------
S:
0
0 | 2.374960e+00
1 | 4.570190e-01
2 | 3.826198e-01
U:
0 1 2
0 | -4.004597e-01 2.227839e-01 6.290916e-01
1 | -3.265902e-01 2.651950e-01 -5.671251e-01
2 | -4.560412e-01 -8.833325e-01 -5.565426e-02
3 | -4.104772e-01 1.444986e-01 4.008145e-01
4 | -5.970810e-01 2.808607e-01 -3.447655e-01
V:
0 1 2
0 | -7.134676e-01 -6.246771e-02 6.978981e-01
1 | -4.142054e-01 -7.657560e-01 -4.919875e-01
2 | -5.651530e-01 6.400903e-01 -5.204676e-01
SVD Decomposition of A (no V)
-----------------------------
S:
0
0 | 2.374960e+00
1 | 4.570190e-01
2 | 3.826198e-01
U:
0 1 2
0 | -4.004597e-01 2.227839e-01 6.290916e-01
1 | -3.265902e-01 2.651950e-01 -5.671251e-01
2 | -4.560412e-01 -8.833325e-01 -5.565426e-02
3 | -4.104772e-01 1.444986e-01 4.008145e-01
4 | -5.970810e-01 2.808607e-01 -3.447655e-01
V(empty):
|