File: Tacho_Util.hpp

package info (click to toggle)
trilinos 12.10.1-3
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 471,528 kB
  • ctags: 314,491
  • sloc: cpp: 2,747,334; ansic: 420,675; fortran: 158,691; xml: 30,440; python: 25,109; f90: 24,661; sh: 14,133; makefile: 5,739; perl: 4,248; csh: 3,791; lex: 1,060; lisp: 810; yacc: 456; awk: 364; sed: 3
file content (495 lines) | stat: -rw-r--r-- 14,007 bytes parent folder | download | duplicates (3)
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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
#ifndef __TACHO_UTIL_HPP__
#define __TACHO_UTIL_HPP__

// standard C includes
#include <stdio.h>
#include <string.h>

// "std" includes
#include <algorithm>
#include <string>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <vector>
#include <set>
#include <map>
#include <memory>

#include <cmath>
//#include <complex>

#include <limits>

/// \file Tacho_Util.hpp
/// \brief Utility functions and constant integer class like an enum class.
/// \author Kyungjoo Kim (kyukim@sandia.gov)

namespace Tacho {

  /// \brief Error handling.
  //
#define MSG_NOT_YET_IMPLEMENTED "Not yet implemented"
#define MSG_INVALID_INPUT(what) "Invaid input argument: " #what
#define MSG_NOT_HAVE_PACKAGE(what) "Tacho does not have a package or library: " what
#define MSG_INVALID_TEMPLATE_ARGS "Invaid template arguments"

#define TACHO_TEST_FOR_ABORT(ierr, msg)                                 \
  if ((ierr) != 0) {                                                    \
    printf(">> Error in file %s, line %d, error %d \n   %s\n",__FILE__,__LINE__,ierr,msg); \
    Kokkos::abort(">> Tacho abort\n");                                  \
  }

#define TACHO_TEST_FOR_EXCEPTION(ierr, x, msg)                           \
  if ((ierr) != 0) {                                                    \
    fprintf(stderr, ">> Error in file %s, line %d, error %d \n",__FILE__,__LINE__,ierr); \
    fprintf(stderr, "   %s\n", msg);                                    \
    throw x(msg);                                                       \
  }

#define TACHO_TEST_FOR_WARNING(ierr, msg)                                 \
  if ((ierr) != 0) {                                                    \
    printf(">> Warning in file %s, line %d, error %d \n   %s\n",__FILE__,__LINE__,ierr,msg); \
  }

  /// \brief Control parameter decomposition.
  ///

  // control id
#undef  Ctrl
#define Ctrl(name,algo,variant) name<algo,variant>

  // control leaf
#undef CtrlComponent
#define CtrlComponent(name,algo,variant,component,id)   \
  Ctrl(name,algo,variant)::component[id]

  // control recursion
#undef CtrlDetail
#define CtrlDetail(name,algo,variant,component)                         \
  CtrlComponent(name,algo,variant,component,0),CtrlComponent(name,algo,variant,component,1),name

  // default value
  template<typename T>
  struct is_complex_type { enum : bool { value = false }; };

  // specialization
  template< typename T >
  struct is_complex_type< Kokkos::complex<T> >
    { enum : bool { value = true }; };

  // default value
  template<typename T>
  struct is_scalar_type { enum : bool { value = false }; };

  template<>
  struct is_scalar_type<int> { enum : bool { value = true }; };
  template<>
  struct is_scalar_type<unsigned int> { enum : bool { value = true }; };
  template<>
  struct is_scalar_type<long> { enum : bool { value = true }; };
  template<>
  struct is_scalar_type<unsigned long> { enum : bool { value = true }; };
  template<>
  struct is_scalar_type<float> { enum : bool { value = true }; };
  template<>
  struct is_scalar_type<double> { enum : bool { value = true }; };
  template<>
  struct is_scalar_type<Kokkos::complex<float> > { enum : bool { value = true }; };
  template<>
  struct is_scalar_type<Kokkos::complex<double> > { enum : bool { value = true }; };


  class Util {
  public:
    static constexpr size_t LabelSize = 64;

    template<typename T>
    KOKKOS_INLINE_FUNCTION
    static T min(const T a, const T b) {
      return (a < b ? a : b);
    }

    template<typename T>
    KOKKOS_INLINE_FUNCTION
    static T max(const T a, const T b) {
      return (a > b ? a : b);
    }

    template<typename T>
    KOKKOS_INLINE_FUNCTION
    static T abs(const T a) {
      return (a > 0 ? a : -a);
    }

    template<typename T>
    KOKKOS_INLINE_FUNCTION
    static T real(const T a) {
      return a;
    }

    template<typename T>
    KOKKOS_INLINE_FUNCTION
    static T imag(const T a) {
      return 0;
    }

    template<typename T>
    KOKKOS_INLINE_FUNCTION
    static T conj(const T a) {
      return a;
    }

    template<typename T1, typename T2, typename T3>
    KOKKOS_FORCEINLINE_FUNCTION
    static void unrollIndex(Kokkos::pair<T1,T1> &idx,
                            const T2 k, const T3 stride) {
      idx.first  = k%stride;
      idx.second = k/stride;
    }

    template<typename T1, typename T2, typename T3, typename T4>
    KOKKOS_FORCEINLINE_FUNCTION
    static void unrollIndex(T1 &i, T2 &j,
                            const T3 k,
                            const T4 stride) {
      i = k%stride;
      j = k/stride;
    }

    template<size_t N, typename Lambda, typename IterT>
    KOKKOS_FORCEINLINE_FUNCTION
    static void unrollLoop(const Lambda &f, const IterT& iter) {
      if (N != 0) unrollLoop<N-1>(f, iter);
      f(iter + N);
    }

    template<typename T>
    KOKKOS_FORCEINLINE_FUNCTION
    static bool isComplex() {
      return is_complex_type<T>::value;
    }

    template<typename T>
    KOKKOS_FORCEINLINE_FUNCTION
    static bool isScalar() {
      return is_scalar_type<T>::value;
    }

    // uses range [first, last)
    template<typename ValueType, typename SpaceType, typename IterType>
    KOKKOS_FORCEINLINE_FUNCTION
    static IterType getLowerBound(const Kokkos::View<ValueType*,SpaceType> &data,
                                  IterType first,
                                  IterType last,
                                  const ValueType val) {
      IterType it, count = last - first, step = 0;
      while (count > 0) {
        it = first;
        it += ( step = (count >> 1) );
        if (data[it] < val) {
          first = ++it;
          count -= step+1;
        } else {
          count=step;
        }
      }
      return first;
    }

    template<typename ValueType, typename SpaceType, typename IterType>
    KOKKOS_FORCEINLINE_FUNCTION
    static IterType getUpperBound(const Kokkos::View<ValueType*,SpaceType> &data,
                                  IterType first,
                                  IterType last,
                                  const ValueType val) {
      IterType it, count = last - first, step;
      while (count > 0) {
        it = first;
        it += ( step = (count >> 1) );
        if (!(val < data[it])) {
          first = ++it;
          count -= step+1;
        } else {
          count = step;
        }
      }
      return first;
    }

    template<typename T>
    KOKKOS_FORCEINLINE_FUNCTION
    static void swap(T &a, T &b) {
      T c(a); a = b; b = c;
    }

    template<typename dataValueType, class ... dataProperties,
             typename idxValueType, class ... idxProperties,
             typename OrdinalType>
    KOKKOS_INLINE_FUNCTION
    static void sort(/**/  Kokkos::View<dataValueType*,dataProperties...> data,
                     /**/  Kokkos::View<idxValueType*,idxProperties...> idx,
                     const OrdinalType begin,
                     const OrdinalType end) {
      if (begin + 1 < end) {
        const auto piv = data[begin];
        OrdinalType left = (begin + 1), right = end;
        while (left < right) {
          if (data[left] <= piv) {
            ++left;
          } else {
            --right;
            Util::swap(data[left], data[right]);
            Util::swap(idx [left], idx [right]);
          }
        }

        --left;
        Util::swap(data[left], data[begin]);
        Util::swap(idx [left], idx [begin]);

        // recursion
        Util::sort(data, idx, begin, left);
        Util::sort(data, idx, right, end );
      }
    }

    template<typename dataValueType, class ...dataProperties,
             typename OrdinalType>
    KOKKOS_INLINE_FUNCTION
    static void sort(/**/  Kokkos::View<dataValueType*,dataProperties...> data,
                     const OrdinalType begin,
                     const OrdinalType end) {
      if (begin + 1 < end) {
        const auto piv = data[begin];
        OrdinalType left = (begin + 1), right = end;
        while (left < right) {
          if (data[left] <= piv) {
            ++left;
          } else {
            --right;
            Util::swap(data[left], data[right]);
          }
        }

        --left;
        Util::swap(data[left], data[begin]);

        // recursion
        Util::sort(data, begin, left);
        Util::sort(data, right, end );
      }
    }

  };

  /// \class Partition
  /// \brief Matrix partition parameters.
  ///
  class Partition {
  public:
    static constexpr int Top         = 101;
    static constexpr int Bottom      = 102;

    static constexpr int Left        = 201;
    static constexpr int Right       = 202;

    static constexpr int TopLeft     = 401;
    static constexpr int TopRight    = 402;
    static constexpr int BottomLeft  = 403;
    static constexpr int BottomRight = 404;
  };

  /// \class Uplo
  /// \brief Matrix upper/lower parameters.
  ///
  class Uplo {
  public:
    static constexpr int Upper = 501;
    static constexpr int Lower = 502;
  };

  /// \class Side
  /// \brief Matrix left/right parameters.
  class Side {
  public:
    static constexpr int Left  = 601;
    static constexpr int Right = 602;
  };

  /// \class Diag
  /// \brief Matrix unit/non-unit diag parameters.
  class Diag {
  public:
    static constexpr int Unit    = 701;
    static constexpr int NonUnit = 702;
  };

  /// \class Trans
  /// \brief Matrix upper/lower parameters.
  class Trans {
  public:
    static constexpr int Transpose     = 801;
    static constexpr int ConjTranspose = 802;
    static constexpr int NoTranspose   = 803;
  };

  /// \class Variant
  /// \brief Algorithmic variants.
  class Variant {
  public:
    static constexpr int One   = 1;
    static constexpr int Two   = 2;
    static constexpr int Three = 3;
    static constexpr int Four  = 4;
    static constexpr int Five  = 5;
    static constexpr int Six   = 6;
    static constexpr int Seven = 7;
    static constexpr int Eight = 8;
    static constexpr int Nine  = 9;
  };

  class TaskWindow {
  public:
    static constexpr unsigned int CholByBlocks     = 4096;
    static constexpr unsigned int TriSolveByBlocks = 4096;
  };

  /// \class AlgoChol
  /// \brief Various Cholesky algorithms for sparse and dense factorization.
  class AlgoChol {
  public:
    // - Flat sparse matrix
    static constexpr int Dummy                  = 1000;
    static constexpr int Unblocked              = 1001;
    static constexpr int ExternalPardiso        = 1002;

    // - Block sparse matrix
    static constexpr int ByBlocks               = 1101;

    // - Flat dense matrix
    static constexpr int ExternalLapack         = 1202;

    // - Hier dense matrix
    static constexpr int DenseByBlocks          = 1211;

    // - Flat sparse with nested dense matrices
    static constexpr int SuperNodes             = 1301;
    static constexpr int SuperNodesByBlocks     = 1302;
  };

  /// \class AlgoTriSolve
  /// \brief Various Cholesky algorithms for sparse and dense factorization.
  class AlgoTriSolve {
  public:
    // - Flat sparse matrix
    static constexpr int Dummy                  = 1400;
    static constexpr int Unblocked              = 1401;
    static constexpr int ExternalPardiso        = 1402;

    // - Block sparse matrix
    static constexpr int ByBlocks               = 1501;

    // - Flat sparse with nested dense matrices
    static constexpr int SuperNodes             = 1601;
    static constexpr int SuperNodesByBlocks     = 1602;
  };

  /// \class AlgoBlas
  /// \brief Various matrix BLAS algorithms for sparse and dense operations.
  class AlgoBlas {
  public:
    // - Flat sparse-sparse matrix
    static constexpr int SparseSparseUnblocked          = 2001;
    static constexpr int SparseDenseUnblocked           = 2002;

    // - Flat dense matrix
    static constexpr int ExternalBlas                   = 2011;
    static constexpr int InternalBlas                   = 2012;

    // - Flat sparse with nested dense matrices
    static constexpr int SparseSparseSuperNodes         = 2021;
    static constexpr int SparseDenseSuperNodes          = 2022;
    static constexpr int SparseSparseSuperNodesByBlocks = 2033;
    static constexpr int SparseDenseSuperNodesByBlocks  = 2034;
  };

  class AlgoGemm : public AlgoBlas {
  public:
    static constexpr int DenseByBlocks = 2101;
  };

  class AlgoTrsm : public AlgoBlas {
  public:
    static constexpr int DenseByBlocks = 2201;
  };

  class AlgoHerk : public AlgoBlas {
  public:
    static constexpr int DenseByBlocks = 2301;
  };


  struct Stat {
    double flop;

    Stat() : flop(0.0) {}
    Stat& operator+=(const Stat &b) {
      flop += b.flop;
      return *this;
    }
  };
  inline Stat operator+(Stat a, const Stat &b) {
    return a += b;
  }
  
  /// \class Coo
  /// \brief Sparse coordinate format; (i, j, val).
  template<typename OrdinalType, typename ValueType>
  class Coo {
  public:
    typedef OrdinalType ordinal_type;
    typedef ValueType   value_type;

  public:
    ordinal_type _i,_j;
    value_type _val;

  public:
    ordinal_type& Row() { return _i;   }
    ordinal_type& Col() { return _j;   }
    value_type&   Val() { return _val; }

    ordinal_type  Row() const { return _i;   }
    ordinal_type  Col() const { return _j;   }
    value_type    Val() const { return _val; }

    Coo() = default;
    Coo(const ordinal_type i,
        const ordinal_type j,
        const value_type val)
      : _i(i), _j(j), _val(val) {}
    Coo(const Coo& b) = default;

    /// \brief Compare "less" index i and j only.
    bool operator<(const Coo &y) const {
      ordinal_type r_val = (this->_i - y._i);
      return (r_val == 0 ? this->_j < y._j : r_val < 0);
    }

    /// \brief Compare "equality" only index i and j.
    bool operator==(const Coo &y) const {
      return (this->_i == y._i) && (this->_j == y._j);
    }

    /// \brief Compare "in-equality" only index i and j.
    bool operator!=(const Coo &y) const {
      return !(*this == y);
    }
  };



}

#endif