ViennaCL - The Vienna Computing Library: viennacl/tools/matrix_generation.hpp Source File

ViennaCL - The Vienna Computing Library 1.7.1 Free open-source GPU-accelerated linear algebra and solver library. |

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
matrix_generation.hpp
Go to the documentation of this file.
1 #ifndef VIENNACL_TOOLS_MATRIX_GENERATION_HPP_
2 #define VIENNACL_TOOLS_MATRIX_GENERATION_HPP_
3
4 /* =========================================================================
5 Copyright (c) 2010-2016, Institute for Microelectronics,
6 Institute for Analysis and Scientific Computing,
7 TU Wien.
8 Portions of this software are copyright by UChicago Argonne, LLC.
9
10 -----------------
11 ViennaCL - The Vienna Computing Library
12 -----------------
13
14 Project Head: Karl Rupp rupp@iue.tuwien.ac.at
15
16 (A list of authors and contributors can be found in the manual)
17
18 License: MIT (X11), see file LICENSE in the base directory
19 ============================================================================= */
20
25 #include <string>
26 #include <fstream>
27 #include <sstream>
28 #include "viennacl/forwards.h"
29 #include "viennacl/meta/result_of.hpp"
30 #include "viennacl/tools/adapter.hpp"
31
32 #include <vector>
33 #include <map>
34
35 namespace viennacl
36 {
37 namespace tools
38 {
39
47 template<typename MatrixType>
48 void generate_fdm_laplace(MatrixType & A, vcl_size_t points_x, vcl_size_t points_y)
49 {
50 vcl_size_t total_unknowns = points_x * points_y;
51
52 A.clear();
53 A.resize(total_unknowns, total_unknowns, false);
54
55 for (vcl_size_t i=0; i<points_x; ++i)
56 {
57 for (vcl_size_t j=0; j<points_y; ++j)
58 {
59 vcl_size_t row = i + j * points_x;
60
61 A(row, row) = 4.0;
62
63 if (i > 0)
64 {
65 vcl_size_t col = (i-1) + j * points_x;
66 A(row, col) = -1.0;
67 }
68
69 if (j > 0)
70 {
71 vcl_size_t col = i + (j-1) * points_x;
72 A(row, col) = -1.0;
73 }
74
75 if (i < points_x-1)
76 {
77 vcl_size_t col = (i+1) + j * points_x;
78 A(row, col) = -1.0;
79 }
80
81 if (j < points_y-1)
82 {
83 vcl_size_t col = i + (j+1) * points_x;
84 A(row, col) = -1.0;
85 }
86 }
87 }
88
89 }
90
91 template<typename NumericT>
92 void generate_fdm_laplace(viennacl::compressed_matrix<NumericT> & A, vcl_size_t points_x, vcl_size_t points_y)
93 {
94 // Assemble into temporary matrix on CPU, then copy over:
95 std::vector< std::map<unsigned int, NumericT> > temp_A;
96 viennacl::tools::sparse_matrix_adapter<NumericT> adapted_A(temp_A);
97 generate_fdm_laplace(adapted_A, points_x, points_y);
98 viennacl::copy(temp_A, A);
99 }
100
101 template<typename NumericT>
102 void generate_fdm_laplace(viennacl::coordinate_matrix<NumericT> & A, vcl_size_t points_x, vcl_size_t points_y)
103 {
104 // Assemble into temporary matrix on CPU, then copy over:
105 std::vector< std::map<unsigned int, NumericT> > temp_A;
106 viennacl::tools::sparse_matrix_adapter<NumericT> adapted_A(temp_A);
107 generate_fdm_laplace(adapted_A, points_x, points_y);
108 viennacl::copy(temp_A, A);
109 }
110
111 template<typename NumericT>
112 void generate_fdm_laplace(viennacl::ell_matrix<NumericT> & A, vcl_size_t points_x, vcl_size_t points_y)
113 {
114 // Assemble into temporary matrix on CPU, then copy over:
115 std::vector< std::map<unsigned int, NumericT> > temp_A;
116 viennacl::tools::sparse_matrix_adapter<NumericT> adapted_A(temp_A);
117 generate_fdm_laplace(adapted_A, points_x, points_y);
118 viennacl::copy(temp_A, A);
119 }
120
121 template<typename NumericT>
122 void generate_fdm_laplace(viennacl::sliced_ell_matrix<NumericT> & A, vcl_size_t points_x, vcl_size_t points_y)
123 {
124 // Assemble into temporary matrix on CPU, then copy over:
125 std::vector< std::map<unsigned int, NumericT> > temp_A;
126 viennacl::tools::sparse_matrix_adapter<NumericT> adapted_A(temp_A);
127 generate_fdm_laplace(adapted_A, points_x, points_y);
128 viennacl::copy(temp_A, A);
129 }
130
131 template<typename NumericT>
132 void generate_fdm_laplace(viennacl::hyb_matrix<NumericT> & A, vcl_size_t points_x, vcl_size_t points_y)
133 {
134 // Assemble into temporary matrix on CPU, then copy over:
135 std::vector< std::map<unsigned int, NumericT> > temp_A;
136 viennacl::tools::sparse_matrix_adapter<NumericT> adapted_A(temp_A);
137 generate_fdm_laplace(adapted_A, points_x, points_y);
138 viennacl::copy(temp_A, A);
139 }
140
141
142 } //namespace tools
143 } //namespace viennacl
144
145
146 #endif
viennacl::hyb_matrix
Sparse matrix class using a hybrid format composed of the ELL and CSR format for storing the nonzeros...
Definition: forwards.h:406
adapter.hpp
Adapter classes for sparse matrices made of the STL type std::vector >
forwards.h
This file provides the forward declarations for the main types used within ViennaCL.
viennacl::tools::generate_fdm_laplace
void generate_fdm_laplace(MatrixType &A, vcl_size_t points_x, vcl_size_t points_y)
Generates a sparse matrix obtained from a simple finite-difference discretization of the Laplace equa...
Definition: matrix_generation.hpp:48
viennacl::ell_matrix
Sparse matrix class using the ELLPACK format for storing the nonzeros.
Definition: ell_matrix.hpp:53
viennacl::sliced_ell_matrix
Sparse matrix class using the sliced ELLPACK with parameters C, .
Definition: forwards.h:403
viennacl::vcl_size_t
std::size_t vcl_size_t
Definition: forwards.h:75
viennacl::row
vector_expression< const matrix_base< NumericT, F >, const unsigned int, op_row > row(const matrix_base< NumericT, F > &A, unsigned int i)
Definition: matrix.hpp:910
viennacl::copy
void copy(std::vector< NumericT > &cpu_vec, circulant_matrix< NumericT, AlignmentV > &gpu_mat)
Copies a circulant matrix from the std::vector to the OpenCL device (either GPU or multi-core CPU) ...
Definition: circulant_matrix.hpp:150
viennacl::tools::sparse_matrix_adapter
Adapts a non-const sparse matrix type made up from std::vector > to basic u...
Definition: adapter.hpp:357
viennacl::compressed_matrix< NumericT >
result_of.hpp
A collection of compile time type deductions.
viennacl::coordinate_matrix
A sparse square matrix, where entries are stored as triplets (i,j, val), where i and j are the row an...
Definition: coordinate_matrix.hpp:186