Example: Interfacing MTL 4
ViennaCL objects can directly initialized with data of objects from the MTL 4 library. The necessary wrappers are provided with ViennaCL, but must be enabled explicitly via the preprocessor constant VIENNACL_HAVE_MTL4. The following code snippet shows how transfer from MTL 4 to ViennaCL and vice versa is triggered:
Transfer from MTL 4 to ViennaCL and back
#define VIENNACL_HAVE_MTL4 // other ViennaCL and MTL 4 includes here // main code: mtl::dense2D<double> mtl4_densematrix(dim, dim); mtl::compressed2D<double> mtl4_sparsematrix; mtl::dense_vector<double> mtl4_vector(5, 0.0); // fill MTL 4 objects in a very sophisticated way with numbers here viennacl::matrix<double> viennacl_densematrix(dim, dim); viennacl::compressed_matrix<double> viennacl_sparsematrix(dim, dim); viennacl::vector<double> viennacl_vector(dim); // copy data from MTL4 objects to ViennaCL objects viennacl::copy(mtl4_densematrix, viennacl_densematrix); viennacl::copy(mtl4_sparsematrix, viennacl_sparsematrix); viennacl::copy(mtl4_vector, viennacl_vector); // do some heavy linear algebra with ViennaCL here // (iterative solvers, BLAS 3 routines, etc.) // copy back to MTL 4: viennacl::copy(viennacl_densematrix, mtl4_densematrix); viennacl::copy(viennacl_sparsematrix, mtl4_sparsematrix); viennacl::copy(viennacl_vector, mtl4_vector);
Similar to Eigen, the iterative solvers from ViennaCL can directly be called for MTL 4 objects. However, it should be noted that MTL 4 already provides its own set of iterative solvers.
Iterative Solvers from ViennaCL with MTL 4 objects
#define VIENNACL_HAVE_EIGEN //other ViennaCL and Eigen includes here //main code: using namespace viennacl::linalg; mtl::compressed2D<double> mtl4_matrix; mtl::dense_vector<double> mtl4_rhs(dim, 0.0); mtl::dense_vector<double> mtl4_result(dim, 0.0); // fill mtl4_matrix and mtl4_rhs here (e.g. by Finite Element Methods) // Solve system using Conjugate Gradients from ViennaCL mtl4_result = solve(mtl4_matrix, mtl4_rhs, cg_tag()); // Solve system using Stabilized BiConjugate Gradients from ViennaCL mtl4_result = solve(mtl4_matrix, mtl4_rhs, bicgstab_tag()); // Solve system using Generalized Minimum Residual Method from ViennaCL mtl4_result = solve(mtl4_matrix, mtl4_rhs, gmres_tag());