summaryrefslogtreecommitdiffstatshomepage
path: root/src/lib/netlist/solver/nld_matrix_solver.h
blob: 3a14c896d70b7addbc3227fae197a5cd6d7540e7 (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
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
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
// license:GPL-2.0+
// copyright-holders:Couriersud
/*
 * nld_matrix_solver.h
 *
 */

#ifndef NLD_MATRIX_SOLVER_H_
#define NLD_MATRIX_SOLVER_H_

#include "netlist/nl_base.h"
#include "netlist/nl_errstr.h"
#include "plib/mat_cr.h"
#include "plib/palloc.h"
#include "plib/pmatrix2d.h"
#include "plib/putil.h"
#include "plib/vector_ops.h"

#include <cmath>

namespace netlist
{
namespace solver
{
	P_ENUM(matrix_sort_type_e,
		NOSORT,
		ASCENDING,
		DESCENDING,
		PREFER_IDENTITY_TOP_LEFT,
		PREFER_BAND_MATRIX
	)

	P_ENUM(matrix_type_e,
		SOR_MAT,
		MAT_CR,
		MAT,
		SM,
		W,
		SOR,
		GMRES
	)

	struct solver_parameters_t
	{
		solver_parameters_t(device_t &parent)
		: m_freq(parent, "FREQ", 48000.0)

		/* iteration parameters */
		, m_gs_sor(parent, "SOR_FACTOR", 1.059)
		, m_method(parent, "METHOD", matrix_type_e::MAT_CR)
		, m_accuracy(parent, "ACCURACY", 1e-7)
		, m_gs_loops(parent, "GS_LOOPS", 9)              // Gauss-Seidel loops

		/* general parameters */
		, m_gmin(parent, "GMIN", 1e-9)
		, m_pivot(parent, "PIVOT", false)                    // use pivoting - on supported solvers
		, m_nr_loops(parent, "NR_LOOPS", 250)            // Newton-Raphson loops
		, m_nr_recalc_delay(parent, "NR_RECALC_DELAY", netlist_time::quantum().as_double()) // Delay to next solve attempt if nr loops exceeded
		, m_parallel(parent, "PARALLEL", 0)

		/* automatic time step */
		, m_dynamic_ts(parent, "DYNAMIC_TS", false)
		, m_dynamic_lte(parent, "DYNAMIC_LTE", 1e-5)                     // diff/timestep
		, m_dynamic_min_ts(parent, "DYNAMIC_MIN_TIMESTEP", 1e-6)   // nl_fptype timestep resolution

		/* matrix sorting */
		, m_sort_type(parent, "SORT_TYPE", matrix_sort_type_e::PREFER_IDENTITY_TOP_LEFT)

		/* special */
		, m_use_gabs(parent, "USE_GABS", true)
		, m_use_linear_prediction(parent, "USE_LINEAR_PREDICTION", false) // // savings are eaten up by effort

		{
			m_min_timestep = m_dynamic_min_ts();
			m_max_timestep = netlist_time::from_double(1.0 / m_freq()).as_double();

			if (m_dynamic_ts)
			{
				m_max_timestep *= 1;//NL_FCONST(1000.0);
			}
			else
			{
				m_min_timestep = m_max_timestep;
			}
		}

		param_fp_t m_freq;
		param_fp_t m_gs_sor;
		param_enum_t<matrix_type_e> m_method;
		param_fp_t m_accuracy;
		param_num_t<std::size_t> m_gs_loops;
		param_fp_t m_gmin;
		param_logic_t  m_pivot;
		param_num_t<std::size_t> m_nr_loops;
		param_fp_t m_nr_recalc_delay;
		param_int_t m_parallel;
		param_logic_t  m_dynamic_ts;
		param_fp_t m_dynamic_lte;
		param_fp_t m_dynamic_min_ts;
		param_enum_t<matrix_sort_type_e> m_sort_type;

		param_logic_t m_use_gabs;
		param_logic_t m_use_linear_prediction;

		nl_fptype m_min_timestep;
		nl_fptype m_max_timestep;
	};


	class terms_for_net_t
	{
	public:
		terms_for_net_t(analog_net_t * net = nullptr);

		void clear();

		void add_terminal(terminal_t *term, int net_other, bool sorted);

		std::size_t count() const noexcept { return m_terms.size(); }

		std::size_t railstart() const noexcept { return m_railstart; }

		terminal_t **terms() noexcept { return m_terms.data(); }

		nl_fptype getV() const noexcept { return m_net->Q_Analog(); }

		void setV(nl_fptype v) noexcept { m_net->set_Q_Analog(v); }

		bool isNet(const analog_net_t * net) const noexcept { return net == m_net; }

		void set_railstart(std::size_t val) noexcept { m_railstart = val; }

		PALIGNAS_VECTOROPT()

		plib::aligned_vector<unsigned> m_nz;   /* all non zero for multiplication */
		plib::aligned_vector<unsigned> m_nzrd; /* non zero right of the diagonal for elimination, may include RHS element */
		plib::aligned_vector<unsigned> m_nzbd; /* non zero below of the diagonal for elimination */

		plib::aligned_vector<int> m_connected_net_idx;
	private:
		analog_net_t * m_net;
		plib::aligned_vector<terminal_t *> m_terms;
		std::size_t m_railstart;
	};

	class proxied_analog_output_t : public analog_output_t
	{
	public:

		proxied_analog_output_t(core_device_t &dev, const pstring &aname, analog_net_t *pnet)
		: analog_output_t(dev, aname)
		, m_proxied_net(pnet)
		{ }

		analog_net_t *proxied_net() const { return m_proxied_net;}
	private:
		analog_net_t *m_proxied_net; // only for proxy nets in analog input logic
	};

	class matrix_solver_t : public device_t
	{
	public:
		using list_t = std::vector<matrix_solver_t *>;

		/* after every call to solve, update inputs must be called.
		 * this can be done as well as a batch to ease parallel processing.
		 */
		const netlist_time solve(netlist_time now);
		void update_inputs();

		bool has_dynamic_devices() const noexcept { return m_dynamic_devices.size() > 0; }
		bool has_timestep_devices() const noexcept { return m_step_devices.size() > 0; }

		void update_forced();
		void update_after(const netlist_time after)
		{
			m_Q_sync.net().toggle_and_push_to_queue(after);
		}

		/* netdevice functions */
		NETLIB_UPDATEI();
		NETLIB_RESETI();

	public:
		int get_net_idx(const analog_net_t *net) const noexcept;
		std::pair<int, int> get_left_right_of_diag(std::size_t row, std::size_t diag);
		nl_fptype get_weight_around_diag(std::size_t row, std::size_t diag);

		virtual void log_stats();

		virtual std::pair<pstring, pstring> create_solver_code()
		{
			return std::pair<pstring, pstring>("", plib::pfmt("/* solver doesn't support static compile */\n\n"));
		}

		/* return number of floating point operations for solve */
		std::size_t ops() { return m_ops; }

	protected:

		matrix_solver_t(netlist_state_t &anetlist, const pstring &name,
			const analog_net_t::list_t &nets,
			const solver_parameters_t *params);

		void sort_terms(matrix_sort_type_e sort);

		void update_dynamic();

		virtual unsigned vsolve_non_dynamic(const bool newton_raphson) = 0;
		virtual netlist_time compute_next_timestep(const nl_fptype cur_ts) = 0;

		void  add_term(std::size_t net_idx, terminal_t *term);

		std::size_t max_railstart() const noexcept
		{
			std::size_t max_rail = 0;
			for (std::size_t k = 0; k < m_terms.size(); k++)
				max_rail = std::max(max_rail, m_terms[k].railstart());
			return max_rail;
		}

		template <typename T>
		void store(const T & V)
		{
			const std::size_t iN = this->m_terms.size();
			for (std::size_t i = 0; i < iN; i++)
				this->m_terms[i].setV(V[i]);
		}


		template <typename T>
		auto delta(const T & V) -> typename std::decay<decltype(V[0])>::type
		{
			/* NOTE: Ideally we should also include currents (RHS) here. This would
			 * need a reevaluation of the right hand side after voltages have been updated
			 * and thus belong into a different calculation. This applies to all solvers.
			 */

			const std::size_t iN = this->m_terms.size();
			using vtype = typename std::decay<decltype(V[0])>::type;
			vtype cerr = 0;
			for (std::size_t i = 0; i < iN; i++)
				cerr = std::max(cerr, std::abs(V[i] - static_cast<vtype>(this->m_terms[i].getV())));
			return cerr;
		}

		void set_pointers()
		{
			const std::size_t iN = this->m_terms.size();

			std::size_t max_count = 0;
			std::size_t max_rail = 0;
			for (std::size_t k = 0; k < iN; k++)
			{
				max_count = std::max(max_count, m_terms[k].count());
				max_rail = std::max(max_rail, m_terms[k].railstart());
			}

			m_gtn.resize(iN, max_count);
			m_gonn.resize(iN, max_count);
			m_Idrn.resize(iN, max_count);
			m_connected_net_Vn.resize(iN, max_count);

			for (std::size_t k = 0; k < iN; k++)
			{
				auto count = m_terms[k].count();

				for (std::size_t i = 0; i < count; i++)
				{
					m_terms[k].terms()[i]->set_ptrs(&m_gtn[k][i], &m_gonn[k][i], &m_Idrn[k][i]);
					m_connected_net_Vn[k][i] = m_terms[k].terms()[i]->connected_terminal()->net().Q_Analog_state_ptr();
				}
			}
		}


		template <typename T, typename M>
		void log_fill(const T &fill, M &mat)
		{
			const std::size_t iN = fill.size();

			// FIXME: Not yet working, mat_cr.h needs some more work
#if 0
			auto mat_GE = dynamic_cast<plib::pGEmatrix_cr_t<typename M::base> *>(&mat);
#else
			plib::unused_var(mat);
#endif
			std::vector<unsigned> levL(iN, 0);
			std::vector<unsigned> levU(iN, 0);

			// parallel scheme for L x = y
			for (std::size_t k = 0; k < iN; k++)
			{
				unsigned lm=0;
				for (std::size_t j = 0; j<k; j++)
					if (fill[k][j] < M::FILL_INFINITY)
						lm = std::max(lm, levL[j]);
				levL[k] = 1+lm;
			}

			// parallel scheme for U x = y
			for (std::size_t k = iN; k-- > 0; )
			{
				unsigned lm=0;
				for (std::size_t j = iN; --j > k; )
					if (fill[k][j] < M::FILL_INFINITY)
						lm = std::max(lm, levU[j]);
				levU[k] = 1+lm;
			}
			for (std::size_t k = 0; k < iN; k++)
			{
				unsigned fm = 0;
				pstring ml = "";
				for (std::size_t j = 0; j < iN; j++)
				{
					ml += fill[k][j] == 0 ? 'X' : fill[k][j] < M::FILL_INFINITY ? '+' : '.';
					if (fill[k][j] < M::FILL_INFINITY)
						if (fill[k][j] > fm)
							fm = fill[k][j];
				}
#if 0
				this->log().verbose("{1:4} {2} {3:4} {4:4} {5:4} {6:4}", k, ml,
					levL[k], levU[k], mat_GE ? mat_GE->get_parallel_level(k) : 0, fm);
#else
				this->log().verbose("{1:4} {2} {3:4} {4:4} {5:4} {6:4}", k, ml,
					levL[k], levU[k], 0, fm);
#endif
			}
		}

		template <typename T>
		using aligned_alloc = plib::aligned_allocator<T, PALIGN_VECTOROPT>;

		plib::pmatrix2d<nl_fptype, aligned_alloc<nl_fptype>>        m_gonn;
		plib::pmatrix2d<nl_fptype, aligned_alloc<nl_fptype>>        m_gtn;
		plib::pmatrix2d<nl_fptype, aligned_alloc<nl_fptype>>        m_Idrn;
		plib::pmatrix2d<nl_fptype *, aligned_alloc<nl_fptype *>>    m_connected_net_Vn;

		plib::aligned_vector<terms_for_net_t> m_terms;
		plib::aligned_vector<terms_for_net_t> m_rails_temp;

		std::vector<unique_pool_ptr<proxied_analog_output_t>> m_inps;

		const solver_parameters_t &m_params;

		state_var<int> m_stat_calculations;
		state_var<int> m_stat_newton_raphson;
		state_var<int> m_stat_vsolver_calls;
		state_var<int> m_iterative_fail;
		state_var<int> m_iterative_total;

	private:

		state_var<netlist_time> m_last_step;
		std::vector<core_device_t *> m_step_devices;
		std::vector<core_device_t *> m_dynamic_devices;

		logic_input_t m_fb_sync;
		logic_output_t m_Q_sync;

		/* base setup - called from constructor */
		void setup_base(const analog_net_t::list_t &nets);

		/* calculate matrix */
		void setup_matrix();

		void step(const netlist_time &delta);

		std::size_t m_ops;
	};

	template <typename FT, int SIZE>
	class matrix_solver_ext_t: public matrix_solver_t
	{
		friend class matrix_solver_t;
	public:

		using float_type = FT;

		matrix_solver_ext_t(netlist_state_t &anetlist, const pstring &name,
			const analog_net_t::list_t &nets,
			const solver_parameters_t *params, const std::size_t size)
		: matrix_solver_t(anetlist, name, nets, params)
		, m_dim(size)
		, m_mat_ptr(size, this->max_railstart() + 1)
		, m_last_V(size, plib::constants<float_type>::zero())
		, m_DD_n_m_1(size, plib::constants<float_type>::zero())
		, m_h_n_m_1(size, plib::constants<float_type>::zero())
		{
			/*
			 * save states
			 */
			state().save(*this, m_last_V.as_base(), this->name(), "m_last_V");
			state().save(*this, m_DD_n_m_1.as_base(), this->name(), "m_DD_n_m_1");
			state().save(*this, m_h_n_m_1.as_base(), this->name(), "m_h_n_m_1");
		}


	private:
		const std::size_t m_dim;

	protected:
		static constexpr const std::size_t SIZEABS = plib::parray<FT, SIZE>::SIZEABS();
		static constexpr const std::size_t m_pitch_ABS = (((SIZEABS + 0) + 7) / 8) * 8;

		plib::parray2D<float_type *, SIZE, 0> m_mat_ptr;
		/* state - variable time_stepping */
		plib::parray<float_type, SIZE> m_last_V;
		plib::parray<float_type, SIZE> m_DD_n_m_1;
		plib::parray<float_type, SIZE> m_h_n_m_1;

		// FIXME: it should be like this, however dimensions are determined
		//        in vsetup.
		//state_container<std::vector<nl_fptype>> m_last_V;
		//state_container<std::vector<nl_fptype>> m_DD_n_m_1;
		//state_container<std::vector<nl_fptype>> m_h_n_m_1;

		constexpr std::size_t size() const noexcept { return (SIZE > 0) ? static_cast<std::size_t>(SIZE) : m_dim; }

		netlist_time compute_next_timestep(const nl_fptype cur_ts) override
		{
			nl_fptype new_solver_timestep = m_params.m_max_timestep;

			if (m_params.m_dynamic_ts)
			{
				for (std::size_t k = 0; k < m_terms.size(); k++)
				{
					auto &t = m_terms[k];
					//const nl_fptype DD_n = (n->Q_Analog() - t->m_last_V);
					// avoid floating point exceptions

					const nl_fptype DD_n = std::max(-fp_constants<nl_fptype>::TIMESTEP_MAXDIFF(),
						std::min(+fp_constants<nl_fptype>::TIMESTEP_MAXDIFF(),(t.getV() - m_last_V[k])));
					const nl_fptype hn = cur_ts;

					//printf("%g %g %g %g\n", DD_n, hn, t.m_DD_n_m_1, t.m_h_n_m_1);
					nl_fptype DD2 = (DD_n / hn - m_DD_n_m_1[k] / m_h_n_m_1[k]) / (hn + m_h_n_m_1[k]);
					nl_fptype new_net_timestep(0);

					m_h_n_m_1[k] = hn;
					m_DD_n_m_1[k] = DD_n;
					if (std::fabs(DD2) > fp_constants<nl_fptype>::TIMESTEP_MINDIV()) // avoid div-by-zero
						new_net_timestep = std::sqrt(m_params.m_dynamic_lte / std::fabs(plib::constants<nl_fptype>::cast(0.5)*DD2));
					else
						new_net_timestep = m_params.m_max_timestep;

					if (new_net_timestep < new_solver_timestep)
						new_solver_timestep = new_net_timestep;

					m_last_V[k] = t.getV();
				}
				if (new_solver_timestep < m_params.m_min_timestep)
				{
					new_solver_timestep = m_params.m_min_timestep;
				}
			}
			//if (new_solver_timestep > 10.0 * hn)
			//    new_solver_timestep = 10.0 * hn;
			/*
			 * FIXME: Factor 2 below is important. Without, we get timing issues. This must be a bug elsewhere.
			 */
			return std::max(netlist_time::from_double(new_solver_timestep), netlist_time::quantum() * 2);
		}


		template <typename M>
		void build_mat_ptr(M &mat)
		{
			const std::size_t iN = size();

			for (std::size_t k=0; k<iN; k++)
			{
				std::size_t cnt(0);
				/* build pointers into the compressed row format matrix for each terminal */
				for (std::size_t j=0; j< this->m_terms[k].railstart();j++)
				{
					int other = this->m_terms[k].m_connected_net_idx[j];
					if (other >= 0)
					{
						m_mat_ptr[k][j] = &(mat[k][static_cast<std::size_t>(other)]);
						cnt++;
					}
				}
				nl_assert_always(cnt == this->m_terms[k].railstart(), "Count and railstart mismatch");
				m_mat_ptr[k][this->m_terms[k].railstart()] = &(mat[k][k]);
			}
		}

		template <typename M>
		void clear_square_mat(M &m)
		{
			const std::size_t n = size();

			for (std::size_t k=0; k < n; k++)
			{
				auto *p = &(m[k][0]);
				for (std::size_t i=0; i < n; i++)
					p[i] = 0.0;
			}
		}

		template <typename RT>
		void fill_matrix(RT &RHS)
		{
			const std::size_t N = size();

			for (std::size_t k = 0; k < N; k++)
			{
				auto &net = m_terms[k];
				auto **tcr_r = &(m_mat_ptr[k][0]);

				const std::size_t term_count = net.count();
				const std::size_t railstart = net.railstart();
				const auto &go = m_gonn[k];
				const auto &gt = m_gtn[k];
				const auto &Idr = m_Idrn[k];
				const auto &cnV = m_connected_net_Vn[k];

				for (std::size_t i = 0; i < railstart; i++)
					*tcr_r[i]       += go[i];

				typename RT::value_type gtot_t = 0.0;
				typename RT::value_type RHS_t = 0.0;

				for (std::size_t i = 0; i < term_count; i++)
				{
					gtot_t        += gt[i];
					RHS_t         += Idr[i];
				}
				// FIXME: Code above is faster than vec_sum - Check this
		#if 0
				auto gtot_t = plib::vec_sum<FT>(term_count, m_gt);
				auto RHS_t = plib::vec_sum<FT>(term_count, m_Idr);
		#endif

				for (std::size_t i = railstart; i < term_count; i++)
				{
					RHS_t += (/*m_Idr[i]*/ (- go[i]) * *cnV[i]);
				}

				RHS[k] = RHS_t;
				// update diagonal element ...
				*tcr_r[railstart] += gtot_t; //mat.A[mat.diag[k]] += gtot_t;
			}

		}

	};

} // namespace solver
} // namespace netlist

#endif /* NLD_MS_DIRECT_H_ */