summaryrefslogtreecommitdiffstatshomepage
path: root/src/emu/netlist/analog/nld_solver.h
blob: b6baa1d31dadb1cfd0966bf546dcf090eb2f6163 (plain) (blame)
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
/*
 * nld_solver.h
 *
 */

#ifndef NLD_SOLVER_H_
#define NLD_SOLVER_H_

#include "../nl_setup.h"
#include "../nl_base.h"

//#define ATTR_ALIGNED(N) __attribute__((aligned(N)))
#define ATTR_ALIGNED(N) ATTR_ALIGN

#define USE_PIVOT_SEARCH (0)
#define VECTALT 1
#define USE_GABS 1
#define USE_MATRIX_GS 0
// savings are eaten up by effort
#define USE_LINEAR_PREDICTION (0)

// ----------------------------------------------------------------------------------------
// Macros
// ----------------------------------------------------------------------------------------

#define SOLVER(_name, _freq)                                                 \
		NET_REGISTER_DEV(solver, _name)                                      \
		PARAM(_name.FREQ, _freq)

// ----------------------------------------------------------------------------------------
// solver
// ----------------------------------------------------------------------------------------

class NETLIB_NAME(solver);

/* FIXME: these should become proper devices */

struct netlist_solver_parameters_t
{
	nl_double m_accuracy;
	nl_double m_lte;
	nl_double m_min_timestep;
	nl_double m_max_timestep;
	nl_double m_sor;
	bool m_dynamic;
	int m_gs_loops;
	int m_nr_loops;
	netlist_time m_nt_sync_delay;
};

class vector_ops_t
{
public:

	vector_ops_t(int size)
	: m_dim(size)
	{
	}

	virtual ~vector_ops_t() {}

	virtual const nl_double sum(const nl_double * v) = 0;
	virtual void sum2(const nl_double * RESTRICT v1, const nl_double * RESTRICT v2, nl_double & RESTRICT  s1, nl_double & RESTRICT s2) = 0;
	virtual void addmult(nl_double * RESTRICT v1, const nl_double * RESTRICT v2, const nl_double &mult) = 0;
	virtual void sum2a(const nl_double * RESTRICT v1, const nl_double * RESTRICT v2, const nl_double * RESTRICT v3abs, nl_double & RESTRICT s1, nl_double & RESTRICT s2, nl_double & RESTRICT s3abs) = 0;

	virtual const nl_double sumabs(const nl_double * v) = 0;

	static vector_ops_t *create_ops(const int size);

protected:
	int m_dim;

private:

};

template <int m_N>
class vector_ops_impl_t : public vector_ops_t
{
public:

	vector_ops_impl_t()
	: vector_ops_t(m_N)
	{
	}

	vector_ops_impl_t(int size)
	: vector_ops_t(size)
	{
		nl_assert(m_N == 0);
	}

	virtual ~vector_ops_impl_t() {}

	ATTR_HOT inline const int N() const { if (m_N == 0) return m_dim; else return m_N; }

	const nl_double sum(const nl_double * v)
	{
		const nl_double *  RESTRICT vl = v;
		nl_double tmp = 0.0;
		for (int i=0; i < N(); i++)
			tmp += vl[i];
		return tmp;
	}

	void sum2(const nl_double * RESTRICT v1, const nl_double * RESTRICT v2, nl_double & RESTRICT s1, nl_double & RESTRICT s2)
	{
		const nl_double * RESTRICT v1l = v1;
		const nl_double * RESTRICT v2l = v2;
		for (int i=0; i < N(); i++)
		{
			s1 += v1l[i];
			s2 += v2l[i];
		}
	}

	void addmult(nl_double * RESTRICT v1, const nl_double * RESTRICT v2, const nl_double &mult)
	{
		nl_double * RESTRICT v1l = v1;
		const nl_double * RESTRICT v2l = v2;
		for (int i=0; i < N(); i++)
		{
			v1l[i] += v2l[i] * mult;
		}
	}

	void sum2a(const nl_double * RESTRICT v1, const nl_double * RESTRICT v2, const nl_double * RESTRICT v3abs, nl_double & RESTRICT s1, nl_double & RESTRICT s2, nl_double & RESTRICT s3abs)
	{
		const nl_double * RESTRICT v1l = v1;
		const nl_double * RESTRICT v2l = v2;
		const nl_double * RESTRICT v3l = v3abs;
		for (int i=0; i < N(); i++)
		{
			s1 += v1l[i];
			s2 += v2l[i];
			s3abs += fabs(v3l[i]);
		}
	}

	const nl_double sumabs(const nl_double * v)
	{
		const nl_double * RESTRICT vl = v;
		nl_double tmp = 0.0;
		for (int i=0; i < N(); i++)
			tmp += fabs(vl[i]);
		return tmp;
	}

private:
};

class ATTR_ALIGNED(64) terms_t
{
	NETLIST_PREVENT_COPYING(terms_t)

	public:
	ATTR_COLD terms_t() : m_railstart(0)
	{}

	ATTR_COLD void clear()
	{
		m_term.clear();
		m_net_other.clear();
		m_gt.clear();
	}

	ATTR_COLD void add(netlist_terminal_t *term, int net_other);

	ATTR_HOT inline int count() { return m_term.count(); }

	ATTR_HOT inline netlist_terminal_t **terms() { return m_term; }
	ATTR_HOT inline int *net_other() { return m_net_other; }
	ATTR_HOT inline nl_double *gt() { return m_gt; }
	ATTR_HOT inline nl_double *go() { return m_go; }
	ATTR_HOT inline nl_double *Idr() { return m_Idr; }
	ATTR_HOT inline nl_double **other_curanalog() { return m_other_curanalog; }

	ATTR_COLD void set_pointers();

	int m_railstart;

private:
	plinearlist_t<netlist_terminal_t *> m_term;
	plinearlist_t<int> m_net_other;
	plinearlist_t<nl_double> m_go;
	plinearlist_t<nl_double> m_gt;
	plinearlist_t<nl_double> m_Idr;
	plinearlist_t<nl_double *> m_other_curanalog;
};

class netlist_matrix_solver_t : public netlist_device_t
{
public:
	typedef plinearlist_t<netlist_matrix_solver_t *> list_t;
	typedef netlist_core_device_t::list_t dev_list_t;

	enum eSolverType
	{
	    GAUSSIAN_ELIMINATION,
	    GAUSS_SEIDEL
	};

	ATTR_COLD netlist_matrix_solver_t(const eSolverType type, const netlist_solver_parameters_t &params);
	ATTR_COLD virtual ~netlist_matrix_solver_t();

	ATTR_COLD virtual void vsetup(netlist_analog_net_t::list_t &nets) = 0;

	template<class C>
	void solve_base(C *p);

	ATTR_HOT nl_double solve();

	ATTR_HOT inline bool is_dynamic() { return m_dynamic_devices.count() > 0; }
	ATTR_HOT inline bool is_timestep() { return m_step_devices.count() > 0; }

	ATTR_HOT void update_forced();
	ATTR_HOT inline void update_after(const netlist_time after)
	{
		m_Q_sync.net().reschedule_in_queue(after);
	}

	/* netdevice functions */
	ATTR_HOT  virtual void update();
	ATTR_COLD virtual void start();
	ATTR_COLD virtual void reset();

	ATTR_COLD int get_net_idx(netlist_net_t *net);
	ATTR_COLD virtual void log_stats() {};

	inline const eSolverType type() const { return m_type; }

protected:

	ATTR_COLD void setup(netlist_analog_net_t::list_t &nets);
	ATTR_HOT void update_dynamic();

	// should return next time step
	ATTR_HOT virtual nl_double vsolve() = 0;

	ATTR_COLD virtual void  add_term(int net_idx, netlist_terminal_t *term) = 0;

	plinearlist_t<netlist_analog_net_t *> m_nets;
	plinearlist_t<netlist_analog_output_t *> m_inps;

    int m_stat_calculations;
    int m_stat_newton_raphson;
    int m_stat_vsolver_calls;

	const netlist_solver_parameters_t &m_params;

	ATTR_HOT inline const nl_double current_timestep() { return m_cur_ts; }
private:

	netlist_time m_last_step;
	nl_double m_cur_ts;
	dev_list_t m_step_devices;
	dev_list_t m_dynamic_devices;

	netlist_ttl_input_t m_fb_sync;
	netlist_ttl_output_t m_Q_sync;

	ATTR_HOT void step(const netlist_time delta);

	ATTR_HOT void update_inputs();

    const eSolverType m_type;
};



class ATTR_ALIGNED(64) NETLIB_NAME(solver) : public netlist_device_t
{
public:
	NETLIB_NAME(solver)()
	: netlist_device_t()    { }

	ATTR_COLD virtual ~NETLIB_NAME(solver)();

	ATTR_COLD void post_start();

	ATTR_HOT inline nl_double gmin() { return m_gmin.Value(); }

protected:
	ATTR_HOT void update();
	ATTR_HOT void start();
	ATTR_HOT void reset();
	ATTR_HOT void update_param();

	netlist_ttl_input_t m_fb_step;
	netlist_ttl_output_t m_Q_step;

	netlist_param_double_t m_freq;
	netlist_param_double_t m_sync_delay;
	netlist_param_double_t m_accuracy;
	netlist_param_double_t m_gmin;
	netlist_param_double_t m_lte;
	netlist_param_double_t m_sor;
	netlist_param_logic_t  m_dynamic;
	netlist_param_double_t m_min_timestep;

	netlist_param_int_t m_nr_loops;
	netlist_param_int_t m_gs_loops;
	netlist_param_int_t m_gs_threshold;
	netlist_param_int_t m_parallel;

	netlist_matrix_solver_t::list_t m_mat_solvers;
private:

	netlist_solver_parameters_t m_params;

	template <int m_N, int _storage_N>
	netlist_matrix_solver_t *create_solver(int size, int gs_threshold, bool use_specific);
};



#endif /* NLD_SOLVER_H_ */