File: dia_matrix_view.cu

package info (click to toggle)
python-escript 5.0-3
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 87,772 kB
  • ctags: 49,550
  • sloc: python: 585,488; cpp: 133,173; ansic: 18,675; xml: 3,283; sh: 690; makefile: 215
file content (193 lines) | stat: -rw-r--r-- 8,003 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#include <unittest/unittest.h>

#include <cusp/dia_matrix.h>
#include <cusp/multiply.h>

template <typename MemorySpace>
void TestDiaMatrixView(void)
{
  typedef int                                                           IndexType;
  typedef float                                                         ValueType;
  typedef typename cusp::dia_matrix<IndexType,ValueType,MemorySpace>    Matrix;
  typedef typename cusp::array1d<IndexType,MemorySpace>::iterator       IndexIterator;
  typedef typename cusp::array1d<ValueType,MemorySpace>::iterator       ValueIterator;
  typedef typename cusp::array1d_view<IndexIterator>                    IndexView1d;
  typedef typename cusp::array1d_view<ValueIterator>                    ValueView1d;
  typedef typename cusp::array2d_view<ValueView1d,cusp::column_major>   ValueView2d;
  typedef typename cusp::dia_matrix_view<IndexView1d,ValueView2d>       View;

  Matrix M(3, 2, 6, 4);

  View V(3, 2, 6,
      cusp::make_array1d_view(M.diagonal_offsets),
      cusp::make_array2d_view(M.values));

  ASSERT_EQUAL(V.num_rows,    3);
  ASSERT_EQUAL(V.num_cols,    2);
  ASSERT_EQUAL(V.num_entries, 6);
  
  ASSERT_EQUAL_QUIET(V.diagonal_offsets.begin(), M.diagonal_offsets.begin());
  ASSERT_EQUAL_QUIET(V.diagonal_offsets.end(),   M.diagonal_offsets.end());
  ASSERT_EQUAL_QUIET(V.values.num_rows,          M.values.num_rows);
  ASSERT_EQUAL_QUIET(V.values.num_cols,          M.values.num_cols);
  ASSERT_EQUAL_QUIET(V.values.num_entries,       M.values.num_entries);
  ASSERT_EQUAL_QUIET(V.values.pitch,             M.values.pitch);
  ASSERT_EQUAL_QUIET(V.values.values.begin(),    M.values.values.begin());
  ASSERT_EQUAL_QUIET(V.values.values.end(),      M.values.values.end());
  
  View W(M);
  
  ASSERT_EQUAL(W.num_rows,    3);
  ASSERT_EQUAL(W.num_cols,    2);
  ASSERT_EQUAL(W.num_entries, 6);

  ASSERT_EQUAL_QUIET(V.diagonal_offsets.begin(), M.diagonal_offsets.begin());
  ASSERT_EQUAL_QUIET(V.diagonal_offsets.end(),   M.diagonal_offsets.end());
  ASSERT_EQUAL_QUIET(V.values.num_rows,          M.values.num_rows);
  ASSERT_EQUAL_QUIET(V.values.num_cols,          M.values.num_cols);
  ASSERT_EQUAL_QUIET(V.values.num_entries,       M.values.num_entries);
  ASSERT_EQUAL_QUIET(V.values.pitch,             M.values.pitch);
  ASSERT_EQUAL_QUIET(V.values.values.begin(),    M.values.values.begin());
  ASSERT_EQUAL_QUIET(V.values.values.end(),      M.values.values.end());
}
DECLARE_HOST_DEVICE_UNITTEST(TestDiaMatrixView);


template <typename MemorySpace>
void TestDiaMatrixViewAssignment(void)
{
  typedef int                                                           IndexType;
  typedef float                                                         ValueType;
  typedef typename cusp::dia_matrix<IndexType,ValueType,MemorySpace>    Matrix;
  typedef typename cusp::array1d<IndexType,MemorySpace>::iterator       IndexIterator;
  typedef typename cusp::array1d<ValueType,MemorySpace>::iterator       ValueIterator;
  typedef typename cusp::array1d_view<IndexIterator>                    IndexView1d;
  typedef typename cusp::array1d_view<ValueIterator>                    ValueView1d;
  typedef typename cusp::array2d_view<ValueView1d,cusp::column_major>   ValueView2d;
  typedef typename cusp::dia_matrix_view<IndexView1d,ValueView2d>       View;

  Matrix M(3, 2, 6, 4);

  View V = M;
  
  ASSERT_EQUAL(V.num_rows,    3);
  ASSERT_EQUAL(V.num_cols,    2);
  ASSERT_EQUAL(V.num_entries, 6);

  ASSERT_EQUAL_QUIET(V.diagonal_offsets.begin(), M.diagonal_offsets.begin());
  ASSERT_EQUAL_QUIET(V.diagonal_offsets.end(),   M.diagonal_offsets.end());
  ASSERT_EQUAL_QUIET(V.values.num_rows,          M.values.num_rows);
  ASSERT_EQUAL_QUIET(V.values.num_cols,          M.values.num_cols);
  ASSERT_EQUAL_QUIET(V.values.num_entries,       M.values.num_entries);
  ASSERT_EQUAL_QUIET(V.values.pitch,             M.values.pitch);
  ASSERT_EQUAL_QUIET(V.values.values.begin(),    M.values.values.begin());
  ASSERT_EQUAL_QUIET(V.values.values.end(),      M.values.values.end());
  
  View W = V;
  
  ASSERT_EQUAL(W.num_rows,    3);
  ASSERT_EQUAL(W.num_cols,    2);
  ASSERT_EQUAL(W.num_entries, 6);
  
  ASSERT_EQUAL_QUIET(V.diagonal_offsets.begin(), M.diagonal_offsets.begin());
  ASSERT_EQUAL_QUIET(V.diagonal_offsets.end(),   M.diagonal_offsets.end());
  ASSERT_EQUAL_QUIET(V.values.num_rows,          M.values.num_rows);
  ASSERT_EQUAL_QUIET(V.values.num_cols,          M.values.num_cols);
  ASSERT_EQUAL_QUIET(V.values.num_entries,       M.values.num_entries);
  ASSERT_EQUAL_QUIET(V.values.pitch,             M.values.pitch);
  ASSERT_EQUAL_QUIET(V.values.values.begin(),    M.values.values.begin());
  ASSERT_EQUAL_QUIET(V.values.values.end(),      M.values.values.end());
}
DECLARE_HOST_DEVICE_UNITTEST(TestDiaMatrixViewAssignment);


template <typename MemorySpace>
void TestMakeDiaMatrixView(void)
{
  typedef int                                                               IndexType;
  typedef float                                                             ValueType;
  typedef typename cusp::dia_matrix<IndexType,ValueType,MemorySpace>        Matrix;
  typedef typename cusp::array1d<IndexType,MemorySpace>::iterator           IndexIterator;
  typedef typename cusp::array1d<ValueType,MemorySpace>::iterator           ValueIterator;
  typedef typename cusp::array1d_view<IndexIterator>                        IndexView1d;
  typedef typename cusp::array1d_view<ValueIterator>                        ValueView1d;
  typedef typename cusp::array2d_view<ValueView1d,cusp::column_major>       ValueView2d;
  typedef typename cusp::dia_matrix_view<IndexView1d,ValueView2d>           View;
  typedef typename cusp::array1d<IndexType,MemorySpace>::const_iterator     ConstIndexIterator;
  typedef typename cusp::array1d<ValueType,MemorySpace>::const_iterator     ConstValueIterator;
  typedef typename cusp::array1d_view<ConstIndexIterator>                   ConstIndexView1d;
  typedef typename cusp::array1d_view<ConstValueIterator>                   ConstValueView1d;
  typedef typename cusp::array2d_view<ConstValueView1d,cusp::column_major>  ConstValueView2d;
  typedef typename cusp::dia_matrix_view<ConstIndexView1d,ConstValueView2d> ConstView;

  // construct view from parts
  {
    Matrix M(3, 2, 6, 4);

    View V =
      cusp::make_dia_matrix_view(3, 2, 6,
          cusp::make_array1d_view(M.diagonal_offsets),
          cusp::make_array2d_view(M.values));
    
    ASSERT_EQUAL(V.num_rows,    3);
    ASSERT_EQUAL(V.num_cols,    2);
    ASSERT_EQUAL(V.num_entries, 6);

    V.diagonal_offsets[0] = 1;
    V.values(0,0) = 20;

    ASSERT_EQUAL_QUIET(V.diagonal_offsets, M.diagonal_offsets);
    ASSERT_EQUAL_QUIET(V.values,           M.values);
  }
  
  // construct view from matrix
  {
    Matrix M(3, 2, 6, 4);

    View V = cusp::make_dia_matrix_view(M);
    
    ASSERT_EQUAL(V.num_rows,    3);
    ASSERT_EQUAL(V.num_cols,    2);
    ASSERT_EQUAL(V.num_entries, 6);
    
    V.diagonal_offsets[0] = 1;
    V.values(0,0) = 20;
    
    ASSERT_EQUAL_QUIET(V.diagonal_offsets, M.diagonal_offsets);
    ASSERT_EQUAL_QUIET(V.values,           M.values);
  }
  
  // construct view from view
  {
    Matrix M(3, 2, 6, 4);

    View X = cusp::make_dia_matrix_view(M);
    View V = cusp::make_dia_matrix_view(X);
    
    ASSERT_EQUAL(V.num_rows,    3);
    ASSERT_EQUAL(V.num_cols,    2);
    ASSERT_EQUAL(V.num_entries, 6);

    V.diagonal_offsets[0] = 1;
    V.values(0,0) = 20;

    ASSERT_EQUAL_QUIET(V.diagonal_offsets, M.diagonal_offsets);
    ASSERT_EQUAL_QUIET(V.values,           M.values);
  }
 
  // construct view from const matrix
  {
    const Matrix M(3, 2, 6, 4);
    
    ConstView V = cusp::make_dia_matrix_view(M);
    
    ASSERT_EQUAL(V.num_rows,    3);
    ASSERT_EQUAL(V.num_cols,    2);
    ASSERT_EQUAL(V.num_entries, 6);
    
    ASSERT_EQUAL_QUIET(V.diagonal_offsets, M.diagonal_offsets);
    ASSERT_EQUAL_QUIET(V.values,           M.values);
  }
}
DECLARE_HOST_DEVICE_UNITTEST(TestMakeDiaMatrixView);