-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathexec_utils.hpp
More file actions
158 lines (136 loc) · 5.05 KB
/
exec_utils.hpp
File metadata and controls
158 lines (136 loc) · 5.05 KB
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
//////////////////////////////////////////////////////////////////////////////
// Copyright (c) 2018-2022, Lawrence Livermore National Security, LLC.
//
// Produced at the Lawrence Livermore National Laboratory
//
// LLNL-CODE-758885
//
// All rights reserved.
//
// This file is part of Comb.
//
// For details, see https://github.com/LLNL/Comb
// Please also see the LICENSE file for MIT license.
//////////////////////////////////////////////////////////////////////////////
#ifndef _UTILS_HPP
#define _UTILS_HPP
#include "config.hpp"
#include "print.hpp"
#include <cassert>
#include <cstdio>
using IdxT = int;
using LidxT = int;
using DataT = double;
namespace detail {
// std::exchange
// taken from https://en.cppreference.com/w/cpp/utility/exchange
// license http://creativecommons.org/licenses/by-sa/3.0/
template < typename T, typename U = T >
T exchange(T& obj, U&& new_value)
{
T old_value = std::move(obj);
obj = std::forward<U>(new_value);
return old_value;
}
template < typename T, typename ... types >
struct Count;
template < typename T >
struct Count<T> {
static const size_t value = 0;
};
template < typename T, typename ... types >
struct Count<T, T, types...> {
static const size_t value = 1 + Count<T, types...>::value;
};
template < typename T, typename T0, typename ... types >
struct Count<T, T0, types...> {
static const size_t value = Count<T, types...>::value;
};
struct indexer_kji {
IdxT ijlen, ilen;
indexer_kji(IdxT ijlen_, IdxT ilen_) : ijlen(ijlen_), ilen(ilen_) {}
COMB_HOST COMB_DEVICE IdxT operator()(IdxT k, IdxT j, IdxT i) const { return i + j * ilen + k * ijlen; }
};
struct indexer_ji {
IdxT ilen;
indexer_ji(IdxT ilen_) : ilen(ilen_) {}
COMB_HOST COMB_DEVICE IdxT operator()(IdxT j, IdxT i) const { return i + j * ilen; }
};
struct indexer_i {
indexer_i() {}
COMB_HOST COMB_DEVICE IdxT operator()(IdxT i) const { return i; }
};
struct indexer_offset_kji {
IdxT ijlen, ilen;
IdxT imin, jmin, kmin;
indexer_offset_kji(IdxT kmin_, IdxT jmin_, IdxT imin_, IdxT ijlen_, IdxT ilen_)
: ijlen(ijlen_), ilen(ilen_)
, imin(imin_), jmin(jmin_), kmin(kmin_) {}
COMB_HOST COMB_DEVICE IdxT operator()(IdxT k, IdxT j, IdxT i) const { return (i+imin) + (j+jmin) * ilen + (k+kmin) * ijlen; }
};
struct indexer_list_kji {
LidxT const* indices;
IdxT ijlen, ilen;
indexer_list_kji(LidxT const* indices_, IdxT ijlen_, IdxT ilen_) : indices(indices_), ijlen(ijlen_), ilen(ilen_) {}
COMB_HOST COMB_DEVICE IdxT operator()(IdxT k, IdxT j, IdxT i) const { return indices[i + j * ilen + k * ijlen]; }
};
struct indexer_list_ji {
LidxT const* indices;
IdxT ilen;
indexer_list_ji(LidxT const* indices_, IdxT ilen_) : indices(indices_), ilen(ilen_) {}
COMB_HOST COMB_DEVICE IdxT operator()(IdxT j, IdxT i) const { return indices[i + j * ilen]; }
};
struct indexer_list_i {
LidxT const* indices;
indexer_list_i(LidxT const* indices_) : indices(indices_) {}
COMB_HOST COMB_DEVICE IdxT operator()(IdxT i) const { return indices[i]; }
};
template < typename T_src, typename I_src, typename T_dst, typename I_dst >
struct copy_idxr_idxr {
T_src const* ptr_src;
T_dst* ptr_dst;
I_src idxr_src;
I_dst idxr_dst;
copy_idxr_idxr(T_src const* const& ptr_src_, I_src const& idxr_src_, T_dst* const& ptr_dst_, I_dst const& idxr_dst_) : ptr_src(ptr_src_), ptr_dst(ptr_dst_), idxr_src(idxr_src_), idxr_dst(idxr_dst_) {}
template < typename ... Ts >
COMB_HOST COMB_DEVICE void operator()(Ts... args) const
{
IdxT dst_i = idxr_dst(args...);
IdxT src_i = idxr_src(args...);
// LOGPRINTF("copy_idxr_idxr %p[%i]{%f} = %p[%i]{%f} (%i)\n",
// ptr_dst, dst_i, (double)ptr_dst[dst_i],
// ptr_src, src_i, (double)ptr_src[src_i], args...);
ptr_dst[dst_i] = ptr_src[src_i];
}
};
template < typename T_src, typename I_src, typename T_dst, typename I_dst >
copy_idxr_idxr<T_src, I_src, T_dst, I_dst> make_copy_idxr_idxr(T_src* const& ptr_src, I_src const& idxr_src, T_dst* const& ptr_dst, I_dst const& idxr_dst) {
return copy_idxr_idxr<T_src, I_src, T_dst, I_dst>(ptr_src, idxr_src, ptr_dst, idxr_dst);
}
template < typename I_src, typename T_dst, typename I_dst >
struct set_idxr_idxr {
T_dst* ptr_dst;
I_src idxr_src;
I_dst idxr_dst;
set_idxr_idxr(I_src const& idxr_src_, T_dst* const& ptr_dst_, I_dst const& idxr_dst_)
: ptr_dst(ptr_dst_)
, idxr_src(idxr_src_)
, idxr_dst(idxr_dst_)
{ }
template < typename ... Ts >
COMB_HOST COMB_DEVICE void operator()(Ts... args) const
{
IdxT dst_i = idxr_dst(args...);
IdxT src_i = idxr_src(args...);
// LOGPRINTF("set_idxr_idxr %p[%i]{%f} = %i (%i %i %i)\n",
// ptr_dst, dst_i, (double)ptr_dst[dst_i],
// src_i, args...);
ptr_dst[dst_i] = src_i;
}
};
template < typename I_src, typename T_dst, typename I_dst >
set_idxr_idxr<I_src, T_dst, I_dst> make_set_idxr_idxr(I_src const& idxr_src, T_dst* const& ptr_dst, I_dst const& idxr_dst) {
return set_idxr_idxr<I_src, T_dst, I_dst>(idxr_src, ptr_dst, idxr_dst);
}
} // namespace detail
#endif // _UTILS_HPP