ViennaCL - The Vienna Computing Library  1.7.0
Free open-source GPU-accelerated linear algebra and solver library.
scheduler_matrix.cpp
Go to the documentation of this file.
1 /* =========================================================================
2  Copyright (c) 2010-2015, Institute for Microelectronics,
3  Institute for Analysis and Scientific Computing,
4  TU Wien.
5  Portions of this software are copyright by UChicago Argonne, LLC.
6 
7  -----------------
8  ViennaCL - The Vienna Computing Library
9  -----------------
10 
11  Project Head: Karl Rupp rupp@iue.tuwien.ac.at
12 
13  (A list of authors and contributors can be found in the PDF manual)
14 
15  License: MIT (X11), see file LICENSE in the base directory
16 ============================================================================= */
17 
18 /*
19 *
20 * Tutorial: Show how to use the scheduler
21 *
22 */
23 
24 
25 // include necessary system headers
26 #include <iostream>
27 
28 //include basic scalar and vector types of ViennaCL
29 #include "viennacl/scalar.hpp"
30 #include "viennacl/vector.hpp"
31 #include "viennacl/matrix.hpp"
32 
35 
36 int main()
37 {
38  typedef float ScalarType;
39 
40  viennacl::matrix<ScalarType> vcl_A(10, 8);
41  viennacl::matrix<ScalarType> vcl_B(8, 10);
42 
43  //
44  // Let us fill the CPU vectors with random values:
45  //
46 
47  for (std::size_t i = 0; i < vcl_A.size1(); ++i)
48  for (std::size_t j = 0; j < vcl_A.size2(); ++j)
49  {
50  vcl_A(i, j) = ScalarType(i * j);
51  vcl_B(j, i) = ScalarType(i + j);
52  }
53 
54  std::cout << "vcl_A: " << vcl_A << std::endl;
55  std::cout << "vcl_B: " << vcl_B << std::endl;
56 
57  //
58  // Build expression graph for the operation vcl_vec3 = vcl_vec1 + vcl_vec2
59  //
60  // This requires the following expression graph:
61  //
62  // ( = )
63  // / |
64  // vcl_A ( trans )
65  // / |
66  // vcl_B vcl_B
67  //
68  // One expression node consists of two leaves and the operation connecting the two.
69  // Here we thus need two nodes: One for {vcl_vec3, = , link}, where 'link' points to the second node
70  // {vcl_vec1, +, vcl_vec2}.
71  //
72  // The following is the lowest level on which one could build the expression tree.
73  // Even for a C API one would introduce some additional convenience layer such as add_vector_float_to_lhs(...); etc.
74  //
75  typedef viennacl::scheduler::statement::container_type NodeContainerType; // this is just std::vector<viennacl::scheduler::statement_node>
76  NodeContainerType expression_nodes(2); //container with two nodes
77 
79 
80  // specify LHS of first node, i.e. vcl_vec3:
81  expression_nodes[0].lhs.type_family = viennacl::scheduler::MATRIX_TYPE_FAMILY; // family of vectors
82  expression_nodes[0].lhs.subtype = viennacl::scheduler::DENSE_ROW_MATRIX_TYPE; // a dense matrix
83  expression_nodes[0].lhs.numeric_type = viennacl::scheduler::FLOAT_TYPE; // vector consisting of floats
84  expression_nodes[0].lhs.matrix_row_float = &vcl_A; // provide pointer to vcl_vec3;
85 
86  // specify assignment operation for this node:
87  expression_nodes[0].op.type_family = viennacl::scheduler::OPERATION_BINARY_TYPE_FAMILY; // this is a binary operation, so both LHS and RHS operands are important
88  expression_nodes[0].op.type = viennacl::scheduler::OPERATION_BINARY_ASSIGN_TYPE; // assignment operation: '='
89 
90  // specify RHS: Just refer to the second node:
91  expression_nodes[0].rhs.type_family = viennacl::scheduler::COMPOSITE_OPERATION_FAMILY; // this links to another node (no need to set .subtype and .numeric_type)
92  expression_nodes[0].rhs.node_index = 1; // index of the other node
93 
95 
96  // LHS
97  expression_nodes[1].lhs.type_family = viennacl::scheduler::MATRIX_TYPE_FAMILY; // family of vectors
98  expression_nodes[1].lhs.subtype = viennacl::scheduler::DENSE_ROW_MATRIX_TYPE; // a dense vector
99  expression_nodes[1].lhs.numeric_type = viennacl::scheduler::FLOAT_TYPE; // vector consisting of floats
100  expression_nodes[1].lhs.matrix_row_float = &vcl_B; // provide pointer to vcl_vec1
101 
102  // OP
103  expression_nodes[1].op.type_family = viennacl::scheduler::OPERATION_UNARY_TYPE_FAMILY; // this is a binary operation, so both LHS and RHS operands are important
104  expression_nodes[1].op.type = viennacl::scheduler::OPERATION_UNARY_TRANS_TYPE; // addition operation: '+'
105 
106  // RHS
107  expression_nodes[1].rhs.type_family = viennacl::scheduler::MATRIX_TYPE_FAMILY; // family of vectors
108  expression_nodes[1].rhs.subtype = viennacl::scheduler::DENSE_ROW_MATRIX_TYPE; // a dense vector
109  expression_nodes[1].rhs.numeric_type = viennacl::scheduler::FLOAT_TYPE; // vector consisting of floats
110  expression_nodes[1].rhs.matrix_row_float = &vcl_B; // provide pointer to vcl_vec2
111 
112 
113  // create the full statement (aka. single line of code such as vcl_vec3 = vcl_vec1 + vcl_vec2):
114  viennacl::scheduler::statement vec_addition(expression_nodes);
115 
116  // print it
117  std::cout << vec_addition << std::endl;
118 
119  // run it
120  viennacl::scheduler::execute(vec_addition);
121 
122  // print vectors
123  std::cout << "vcl_A: " << vcl_A << std::endl;
124  std::cout << "vcl_B: " << vcl_B << std::endl;
125 
126 
127  std::cout << "!!!! TUTORIAL COMPLETED SUCCESSFULLY !!!!" << std::endl;
128 
129  return EXIT_SUCCESS;
130 }
131 
Implementation of the dense matrix class.
Some helper routines for reading/writing/printing scheduler expressions.
A dense matrix class.
Definition: forwards.h:375
void execute(statement const &s)
Definition: execute.hpp:279
std::vector< value_type > container_type
Definition: forwards.h:507
int main(int, const char **)
size_type size2() const
Returns the number of columns.
Definition: matrix_def.hpp:226
size_type size1() const
Returns the number of rows.
Definition: matrix_def.hpp:224
The vector type with operator-overloads and proxy classes is defined here. Linear algebra operations ...
float ScalarType
Definition: fft_1d.cpp:42
Provides the datastructures for dealing with a single statement such as 'x = y + z;'.
The main class for representing a statement such as x = inner_prod(y,z); at runtime.
Definition: forwards.h:502
Implementation of the ViennaCL scalar class.