File: verify.inl

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 (414 lines) | stat: -rw-r--r-- 13,857 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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
/*
 *  Copyright 2008-2009 NVIDIA Corporation
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

#include <cusp/format.h>
#include <cusp/exception.h>

#include <thrust/sort.h>
#include <thrust/count.h>
#include <thrust/extrema.h>
#include <thrust/functional.h>

#include <sstream>

namespace cusp
{
namespace detail
{

///////////////////////////////////
// Helper functions and functors //
///////////////////////////////////

template <typename IndexVector>
thrust::pair<typename IndexVector::value_type, typename IndexVector::value_type>
index_range(const IndexVector& indices)
{
//    // return a pair<> containing the min and max value in a range
//    thrust::pair<typename IndexVector::const_iterator, typename IndexVector::const_iterator> iter = thrust::minmax_element(indices.begin(), indices.end());
//    return thrust::make_pair(*iter.first, *iter.second);
   
    // WAR lack of const_iterator in array1d_view
    return thrust::make_pair
      (*thrust::min_element(indices.begin(), indices.end()),
       *thrust::max_element(indices.begin(), indices.end()));
}

template <typename IndexType>
struct is_ell_entry
{
    IndexType num_rows;
    IndexType pitch;
    IndexType invalid_index;

    is_ell_entry(IndexType num_rows, IndexType pitch, IndexType invalid_index)
        : num_rows(num_rows), pitch(pitch), invalid_index(invalid_index) {}

    template <typename Tuple>
    __host__ __device__
    bool operator()(const Tuple& t) const
    {
        IndexType n = thrust::get<0>(t);
        IndexType j = thrust::get<1>(t);
        return (n % pitch < num_rows) && (j != invalid_index);
    }
};

template <typename IndexType>
struct is_ell_entry_in_bounds
{
    IndexType num_rows;
    IndexType num_cols;
    IndexType pitch;
    IndexType invalid_index;

    is_ell_entry_in_bounds(IndexType num_rows, IndexType num_cols, IndexType pitch, IndexType invalid_index)
        : num_rows(num_rows), num_cols(num_cols), pitch(pitch), invalid_index(invalid_index) {}

    template <typename Tuple>
    __host__ __device__
    bool operator()(const Tuple& t) const
    {
        IndexType n = thrust::get<0>(t);
        IndexType j = thrust::get<1>(t);
        return (n % pitch < num_rows) && (j != invalid_index) && (j >= 0) && (j < num_cols);
    }
};


///////////////////////////////
// Matrix-Specific Functions //
///////////////////////////////

template <typename MatrixType, typename OutputStream>
bool is_valid_matrix(const MatrixType& A,
                     OutputStream& ostream,
                     cusp::coo_format)
{
    typedef typename MatrixType::index_type IndexType;

    // we could relax some of these conditions if necessary
    if (A.row_indices.size() != A.num_entries)
    {
        ostream << "size of row_indices (" << A.row_indices.size() << ") "
                << "should be equal to num_entries (" << A.num_entries << ")";
        return false;
    }
    
    if (A.column_indices.size() != A.num_entries)
    {
        ostream << "size of column_indices (" << A.column_indices.size() << ") "
                << "should be equal to num_entries (" << A.num_entries << ")";
        return false;
    }
    
    if (A.values.size() != A.num_entries)
    {
        ostream << "size of values (" << A.column_indices.size() << ") "
                << "should be equal to num_entries (" << A.num_entries << ")";
        return false;
    }
   
    if (A.num_entries > 0)
    {
        // check that row indices are within [0, num_rows)
        thrust::pair<IndexType,IndexType> min_max_row = index_range(A.row_indices);
        if (min_max_row.first < 0)
        {
            ostream << "row indices should be non-negative";
            return false;
        }
        if (static_cast<size_t>(min_max_row.second) >= A.num_rows)
        {
            ostream << "row indices should be less than num_row (" << A.num_rows << ")";
            return false;
        }
        
        // check that row_indices is a non-decreasing sequence
        if (!thrust::is_sorted(A.row_indices.begin(), A.row_indices.end()))
        {
            ostream << "row indices should form a non-decreasing sequence";
            return false;
        }

        // check that column indices are within [0, num_cols)
        thrust::pair<IndexType,IndexType> min_max_col = index_range(A.column_indices);
        if (min_max_col.first < 0)
        {
            ostream << "column indices should be non-negative";
            return false;
        }
        if (static_cast<size_t>(min_max_col.second) >= A.num_cols)
        {
            ostream << "column indices should be less than num_cols (" << A.num_cols << ")";
            return false;
        }
    }

    return true;
}


template <typename MatrixType, typename OutputStream>
bool is_valid_matrix(const MatrixType& A,
                     OutputStream& ostream,
                     cusp::csr_format)
{
    typedef typename MatrixType::index_type IndexType;

    // we could relax some of these conditions if necessary
    
    if (A.row_offsets.size() != A.num_rows + 1)
    {
        ostream << "size of row_offsets (" << A.row_offsets.size() << ") "
                << "should be equal to num_rows + 1 (" << (A.num_rows + 1) << ")";
        return false;
    }
    
    if (A.row_offsets.front() != IndexType(0))
    {
        ostream << "first value in row_offsets (" << A.row_offsets.front() << ") "
                << "should be equal to 0";
        return false;
    }

    // TODO is this overly strict?
    if (static_cast<size_t>(A.row_offsets.back()) != A.num_entries)
    {
        ostream << "last value in row_offsets (" << A.row_offsets.back() << ") "
                << "should be equal to num_entries (" << A.num_entries << ")";
        return false;
    }
    
    if (A.column_indices.size() != A.num_entries)
    {
        ostream << "size of column_indices (" << A.column_indices.size() << ") "
                << "should be equal to num_entries (" << A.num_entries << ")";
        return false;
    }
    
    if (A.values.size() != A.num_entries)
    {
        ostream << "size of values (" << A.column_indices.size() << ") "
                << "should be equal to num_entries (" << A.num_entries << ")";
        return false;
    }

    // check that row_offsets is a non-decreasing sequence
    if (!thrust::is_sorted(A.row_offsets.begin(), A.row_offsets.end()))
    {
        ostream << "row offsets should form a non-decreasing sequence";
        return false;
    }

    if (A.num_entries > 0)
    {
        // check that column indices are within [0, num_cols)
        thrust::pair<IndexType,IndexType> min_max = index_range(A.column_indices);

        if (min_max.first < 0)
        {
            ostream << "column indices should be non-negative";
            return false;
        }
        if (static_cast<size_t>(min_max.second) >= A.num_cols)
        {
            ostream << "column indices should be less than num_cols (" << A.num_cols << ")";
            return false;
        }
    }

    return true;
}


template <typename MatrixType, typename OutputStream>
bool is_valid_matrix(const MatrixType& A,
                     OutputStream& ostream,
                     cusp::dia_format)
{
    if (A.num_rows > A.values.num_rows)
    {
        ostream << "number of rows in values array (" << A.values.num_rows << ") ";
        ostream << "should be >= num_rows (" << A.num_rows << ")";
        return false;
    }
    
    if (A.num_rows > A.values.num_rows)
    {
        ostream << "number of rows in values array (" << A.values.num_rows << ") ";
        ostream << "should be >= num_rows (" << A.num_rows << ")";
        return false;
    }

    return true;
}

template <typename MatrixType, typename OutputStream>
bool is_valid_matrix(const MatrixType& A,
                     OutputStream& ostream,
                     cusp::ell_format)
{
    typedef typename MatrixType::index_type IndexType;

    const IndexType invalid_index = MatrixType::invalid_index;

    if (A.column_indices.num_rows != A.values.num_rows ||
        A.column_indices.num_cols != A.values.num_cols)
    {
        ostream << "shape of column_indices array (" << A.column_indices.num_rows << "," << A.column_indices.num_cols << ") ";
        ostream << "should agree with the values array (" << A.values.num_rows << "," << A.values.num_cols << ")";
        return false;
    }
    
    if (A.num_rows > A.values.num_rows)
    {
        ostream << "number of rows in values array (" << A.values.num_rows << ") ";
        ostream << "should be >= num_rows (" << A.num_rows << ")";
        return false;
    }

    // count true number of entries in ell structure
    size_t true_num_entries = 
      thrust::count_if(thrust::make_zip_iterator
                       (
                           thrust::make_tuple(thrust::counting_iterator<IndexType>(0), 
                                              A.column_indices.values.begin())
                       ),
                       thrust::make_zip_iterator
                       (
                           thrust::make_tuple(thrust::counting_iterator<IndexType>(0), 
                                              A.column_indices.values.begin())
                       ) + A.column_indices.values.size(),
                       is_ell_entry<IndexType>(A.num_rows, A.column_indices.pitch, invalid_index));

    if (A.num_entries != true_num_entries)
    {
        ostream << "number of valid column indices (" << true_num_entries << ") ";
        ostream << "should be == num_entries (" << A.num_entries << ")";
        return false;
    }

    if (A.num_entries > 0)
    {
        // check that column indices are in [0, num_cols)
        size_t num_entries_in_bounds =
          thrust::count_if(thrust::make_zip_iterator
                           (
                               thrust::make_tuple(thrust::counting_iterator<IndexType>(0), 
                                                  A.column_indices.values.begin())
                           ),
                           thrust::make_zip_iterator
                           (
                               thrust::make_tuple(thrust::counting_iterator<IndexType>(0), 
                                                  A.column_indices.values.begin())
                           ) + A.column_indices.values.size(),
                           is_ell_entry_in_bounds<IndexType>(A.num_rows, A.num_cols, A.column_indices.pitch, invalid_index));
        if (num_entries_in_bounds != true_num_entries)
        {
            ostream << "matrix contains (" << (true_num_entries - num_entries_in_bounds) << ") out-of-bounds column indices";
            return false;
        }
    }

    return true;
}

template <typename MatrixType, typename OutputStream>
bool is_valid_matrix(const MatrixType& A,
                     OutputStream& ostream,
                     cusp::hyb_format)
{
    // make sure redundant shapes values agree
    if (A.num_rows != A.ell.num_rows || A.num_rows != A.coo.num_rows ||
        A.num_cols != A.ell.num_cols || A.num_cols != A.coo.num_cols)
    {
        ostream << "matrix shape (" << A.num_rows << "," << A.num_cols << ") ";
        ostream << "should be equal to shape of ELL part (" << A.ell.num_rows << "," << A.ell.num_cols << ") and ";
        ostream << "COO part (" << A.coo.num_rows << "," << A.coo.num_cols << ")";
        return false;
    }

    // check that num_entries = A.ell.num_entries + A.coo.num_entries
    if (A.num_entries != A.ell.num_entries + A.coo.num_entries)
    {
        ostream << "num_entries (" << A.num_entries << ") ";
        ostream << "should be equal to sum of ELL num_entries (" << A.ell.num_entries << ") and ";
        ostream << "COO num_entries (" << A.coo.num_entries << ")";
        return false;
    }

    return cusp::is_valid_matrix(A.ell, ostream) && cusp::is_valid_matrix(A.coo, ostream);
}


template <typename MatrixType, typename OutputStream>
bool is_valid_matrix(const MatrixType& A,
                     OutputStream& ostream,
                     cusp::array2d_format)
{
    if (A.num_rows * A.num_cols != A.num_entries)
    {
        ostream << "product of matrix dimensions (" << A.num_rows << "," << A.num_cols << ") ";
        ostream << "should equal num_entries (" << A.num_entries << ")";
        return false;
    }
    
    if (A.num_entries != A.values.size())
    {
        ostream << "num_entries (" << A.num_entries << ") ";
        ostream << "should agree with size of values array (" << A.values.size() << ")";
        return false;
    }
    
    // TODO check .pitch

    return true;
}

} // end namespace detail


//////////////////
// Entry points //
//////////////////

template <typename MatrixType>
bool is_valid_matrix(const MatrixType& A)
{
    std::ostringstream oss;
    return cusp::is_valid_matrix(A, oss);
}

template <typename MatrixType, typename OutputStream>
bool is_valid_matrix(const MatrixType& A, OutputStream& ostream)
{
    // dispatch on matrix format
    return detail::is_valid_matrix(A, ostream, typename MatrixType::format());
}

template <typename MatrixType>
void assert_is_valid_matrix(const MatrixType& A)
{
    std::ostringstream oss;
    bool is_valid = cusp::is_valid_matrix(A, oss);

    if (!is_valid)
        throw cusp::format_exception(oss.str());
}

} // end namespace cusp