ESBMC
Efficient SMT-based Context-Bounded Model Checker
Loading...
Searching...
No Matches
numpy_nditer.h
Go to the documentation of this file.
1#pragma once
2
3#include <nlohmann/json.hpp>
4#include <util/irep/expr.h>
5
6#include <optional>
7#include <string>
8#include <tuple>
9#include <vector>
10
12
18{
19 nlohmann::json array;
20 bool writable = false;
21 std::vector<long long> shape;
22 std::vector<long long> strides;
23 nlohmann::json extent;
24 nlohmann::json stride;
27 std::vector<long long> axes;
33 std::string buffer;
34};
35
40{
41 std::string state;
44 std::string alias;
45 std::vector<numpy_nditer_operand> operands;
46 std::vector<long long> shape;
48 std::vector<std::size_t> axes;
50 std::vector<bool> flipped;
52 bool runtime = false;
53 bool flip_negative = false;
54 bool multi_index = false;
56 std::string index;
57 bool external_loop = false;
58 bool buffered = false;
60 long long chunk = 1;
61 long long size = 0;
62};
63
68{
69 std::string iterator;
70 std::size_t operand = 0;
71 std::string position;
72};
73
79{
80public:
81 explicit numpy_nditer(python_converter &converter);
82
83 static bool
84 is_nditer_call(const nlohmann::json &node, const nlohmann::json &ast);
85
87 void create(const std::string &name, const nlohmann::json &call);
88
90 exprt as_list(const nlohmann::json &call);
91
94 bool bind(const nlohmann::json &target, const nlohmann::json &value);
95
96 std::optional<exprt> attribute(const nlohmann::json &node);
97 std::optional<exprt> call(const nlohmann::json &node);
98 std::optional<exprt> read_item(const nlohmann::json &name);
99 std::optional<exprt> subscript(const nlohmann::json &node);
101 bool write(const nlohmann::json &target, const nlohmann::json &value);
102
105 void keep_sources(const std::string &rebound_id, const std::string &heir_id);
106
107 static std::string key(const std::string &function, const std::string &name);
108
109private:
110 python_converter &converter_;
111
112 const numpy_nditer_state *find(const nlohmann::json &node) const;
113 const numpy_nditer_item *find_item(const nlohmann::json &node) const;
114
115 std::optional<numpy_nditer_state>
116 describe(const nlohmann::json &call, const std::string &state);
117 std::vector<nlohmann::json> evaluate_operands(
118 const std::vector<nlohmann::json> &ops,
119 const std::vector<std::vector<std::string>> &op_flags);
120 numpy_nditer_operand operand(const nlohmann::json &node, std::size_t index);
121 nlohmann::json named_operand(const nlohmann::json &node, std::size_t index);
124 template <class Info>
125 void view_layout(const Info &info, numpy_nditer_operand &result);
126 template <class Info>
127 void runtime_layout(const Info &info, numpy_nditer_operand &result);
128 std::string invalid_setup(
129 const std::vector<std::string> &flag_list,
130 const std::string &order_name) const;
131 std::string invalid_tracking(const std::vector<std::string> &flag_list) const;
132 nlohmann::json flat_index(
133 const numpy_nditer_state &it,
134 const std::vector<nlohmann::json> &axis_index) const;
135 bool writable_operands_fit(
136 const numpy_nditer_state &it,
137 const std::vector<std::vector<std::string>> &op_flags);
138 bool check_size(numpy_nditer_state &it, bool zerosize_ok);
139 std::vector<std::string> flags(const nlohmann::json &call);
140 std::string order(const nlohmann::json &call);
141 void order_axes(numpy_nditer_state &it, const std::string &order) const;
142 void set_chunk(numpy_nditer_state &it) const;
143 bool broadcast(numpy_nditer_state &it);
144 bool apply_op_axes(numpy_nditer_state &it, const nlohmann::json &call);
145 std::string invalid_op_axes(
146 const numpy_nditer_operand &o,
147 const std::vector<long long> &axes,
148 std::size_t op) const;
149 bool apply_op_dtypes(numpy_nditer_state &it, const nlohmann::json &call);
150 typet element_type(const numpy_nditer_operand &o) const;
151 nlohmann::json value(
152 const numpy_nditer_state &it,
153 std::size_t op,
154 const nlohmann::json &element_position);
155
156 nlohmann::json position(const numpy_nditer_state &it) const;
157 nlohmann::json items(const numpy_nditer_state &it) const;
158 nlohmann::json finished(const numpy_nditer_state &it) const;
159 static std::vector<long long> reported_shape(const numpy_nditer_state &it);
160 std::vector<nlohmann::json> indices(
161 const numpy_nditer_state &it,
162 const nlohmann::json &element_position) const;
163 nlohmann::json element(
164 const numpy_nditer_state &it,
165 std::size_t op,
166 const nlohmann::json &element_position) const;
167 exprt
168 chunk_value(const numpy_nditer_state &it, const numpy_nditer_item &item);
169 nlohmann::json prebuilt(const exprt &expr);
170 nlohmann::json materialize(const numpy_nditer_item &item);
171 bool substitute_chunks(nlohmann::json &node);
172
173 void assign(const std::string &name, const nlohmann::json &value);
174 void raise(const std::string &exception, const std::string &message);
175 void raise_if(
176 const nlohmann::json &cond,
177 const std::string &exception,
178 const std::string &message);
179 void advance(const numpy_nditer_state &it);
180 bool buffer_writes(numpy_nditer_state &it);
181 void fill_buffers(const numpy_nditer_state &it);
182 void flush_buffers(const numpy_nditer_state &it, const nlohmann::json &when);
183 void bind_name(
184 const nlohmann::json &target,
185 const numpy_nditer_state &it,
186 const std::string &iterator,
187 std::size_t op);
188 void bind_operand(
189 const nlohmann::json &target,
190 const numpy_nditer_state &it,
191 const std::string &iterator,
192 const nlohmann::json &index);
193 void bind_items(
194 const nlohmann::json &target,
195 const numpy_nditer_state &it,
196 const std::string &iterator);
197 nlohmann::json current_values(const numpy_nditer_state &it);
198 nlohmann::json
199 layout_attribute(const numpy_nditer_state &it, const std::string &attr) const;
200 nlohmann::json
201 position_attribute(const numpy_nditer_state &it, const std::string &attr);
202 void check_unpack(const numpy_nditer_state &it, long long expected);
203 std::optional<
204 std::tuple<const numpy_nditer_state *, std::size_t, nlohmann::json>>
205 written(const nlohmann::json &target);
206 nlohmann::json call_method(
207 const numpy_nditer_state &it,
208 const std::string &method,
209 const nlohmann::json &node);
210};
Definition expr.h:34
std::optional< exprt > read_item(const nlohmann::json &name)
Definition numpy_nditer.cpp:1718
std::optional< exprt > call(const nlohmann::json &node)
Definition numpy_nditer.cpp:1656
void keep_sources(const std::string &rebound_id, const std::string &heir_id)
Before the array rebound_id names is rebound: the iterators over it keep the old storage,...
Definition numpy_nditer.cpp:1850
std::optional< exprt > subscript(const nlohmann::json &node)
Definition numpy_nditer.cpp:1732
bool write(const nlohmann::json &target, const nlohmann::json &value)
x[...] = value on what the iterator yields; false for other targets.
Definition numpy_nditer.cpp:1815
std::optional< exprt > attribute(const nlohmann::json &node)
Definition numpy_nditer.cpp:1622
exprt as_list(const nlohmann::json &call)
The elements a bare np.nditer(...) yields, in visiting order.
Definition numpy_nditer.cpp:1887
bool bind(const nlohmann::json &target, const nlohmann::json &value)
target = <iterator>.
Definition numpy_nditer.cpp:1463
void create(const std::string &name, const nlohmann::json &call)
name = np.nditer(...): validates the call and creates the iterator.
Definition numpy_nditer.cpp:1184
static bool is_nditer_call(const nlohmann::json &node, const nlohmann::json &ast)
Definition numpy_nditer.cpp:279
numpy_nditer(python_converter &converter)
Definition numpy_nditer.cpp:275
Main converter for transforming Python AST into ESBMC's intermediate representation.
Definition python_converter.h:76
Definition type.h:9
pthread_cond_t cond
Definition cuda.h:50
void * value
Definition solidity_mapping.c:2
uint256_t key
Definition solidity_mapping.c:1
Definition module.h:12
A name bound to what an nditer yields: the element (or the external-loop chunk) of one operand at the...
Definition numpy_nditer.h:68
std::string position
Definition numpy_nditer.h:71
std::string iterator
Definition numpy_nditer.h:69
std::size_t operand
Definition numpy_nditer.h:70
One operand of an nditer: a named array or view and its layout in elements (a negative stride walks b...
Definition numpy_nditer.h:18
std::vector< long long > shape
Definition numpy_nditer.h:21
std::string buffer
A writable operand read through a cast is buffered: the hidden list its items live in,...
Definition numpy_nditer.h:33
nlohmann::json array
Definition numpy_nditer.h:19
nlohmann::json stride
Definition numpy_nditer.h:24
std::vector< long long > strides
Definition numpy_nditer.h:22
nlohmann::json extent
Definition numpy_nditer.h:23
typet dtype
The dtype reads are cast to (op_dtypes); empty for the operand's own.
Definition numpy_nditer.h:29
bool writable
Definition numpy_nditer.h:20
std::vector< long long > axes
The operand axis each iteration axis reads (op_axes), -1 for a new axis; empty for the default alignm...
Definition numpy_nditer.h:27
The static description of an np.nditer: its operands, the iteration shape, and the order the position...
Definition numpy_nditer.h:40
std::string state
Definition numpy_nditer.h:41
bool multi_index
Definition numpy_nditer.h:54
long long chunk
Elements per item: the chunk an external loop yields, 1 otherwise.
Definition numpy_nditer.h:60
std::vector< std::size_t > axes
Iteration axes, outermost first.
Definition numpy_nditer.h:48
std::string index
"C" or "F" for the index c_index or f_index tracks, empty for none.
Definition numpy_nditer.h:56
long long size
Definition numpy_nditer.h:61
std::vector< numpy_nditer_operand > operands
Definition numpy_nditer.h:45
bool flip_negative
Definition numpy_nditer.h:53
std::vector< bool > flipped
Axes visited from their last index down to 0.
Definition numpy_nditer.h:50
bool buffered
Definition numpy_nditer.h:58
bool external_loop
Definition numpy_nditer.h:57
std::vector< long long > shape
Definition numpy_nditer.h:46
bool runtime
A 1-D iteration whose extent and direction are run-time values.
Definition numpy_nditer.h:52
std::string alias
The name numpy is imported under, for the arrays an external loop yields.
Definition numpy_nditer.h:44