GetFEM  5.6
getfem_generic_assembly_compile_and_exec.cc
1 /*===========================================================================
2 
3  Copyright (C) 2013-2026 Yves Renard
4 
5  This file is a part of GetFEM
6 
7  GetFEM is free software; you can redistribute it and/or modify it
8  under the terms of the GNU Lesser General Public License as published
9  by the Free Software Foundation; either version 3 of the License, or
10  (at your option) any later version along with the GCC Runtime Library
11  Exception either version 3.1 or (at your option) any later version.
12  This program is distributed in the hope that it will be useful, but
13  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14  or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
15  License and GCC Runtime Library Exception for more details.
16  You should have received a copy of the GNU Lesser General Public License
17  along with this program. If not, see https://www.gnu.org/licenses/.
18 
19 ===========================================================================*/
20 
21 #include <array>
25 #include "getfem/getfem_generic_assembly_compile_and_exec.h"
26 #include "getfem/getfem_generic_assembly_functions_and_operators.h"
27 
28 #if defined(GMM_USES_BLAS)
29 #define GA_USES_BLAS
30 #endif
31 
32 // #define GA_DEBUG_INFO(a) { cout << a << endl; }
33 #define GA_DEBUG_INFO(a)
34 
35 
36 
37 namespace getfem {
38 
39 
40  template <class VEC1, class VEC2>
41  inline void copy_scaled_4(const VEC1 &v1, const scalar_type a, VEC2 &v2) {
42  auto it1 = v1.begin();
43  auto it2 = v2.begin(), it2e = v2.end();
44  size_type nd = v1.size() >> 2;
45  for (size_type i = 0; i < nd; ++i) {
46  *it2++ = (*it1++) * a;
47  *it2++ = (*it1++) * a;
48  *it2++ = (*it1++) * a;
49  *it2++ = (*it1++) * a;
50  }
51  for (; it2 != it2e;)
52  *it2++ = (*it1++) * a;
53  }
54 
55  template <class VEC1, class VEC2>
56  inline void add_scaled_4(const VEC1 &v1, const scalar_type a, VEC2 &v2) {
57  auto it1 = v1.begin();
58  auto it2 = v2.begin(), it2e = v2.end();
59  size_type nd = v1.size() >> 2;
60  for (size_type i = 0; i < nd; ++i) {
61  *it2++ += (*it1++) * a;
62  *it2++ += (*it1++) * a;
63  *it2++ += (*it1++) * a;
64  *it2++ += (*it1++) * a;
65  }
66  for (; it2 != it2e;)
67  *it2++ += (*it1++) * a;
68  }
69 
70  template <class VEC1, class VEC2>
71  inline void copy_scaled_8(const VEC1 &v1, const scalar_type a, VEC2 &v2) {
72  auto it1 = v1.begin();
73  auto it2 = v2.begin(), it2e = v2.end();
74  size_type nd = v1.size() >> 3;
75  for (size_type i = 0; i < nd; ++i) {
76  *it2++ = (*it1++) * a;
77  *it2++ = (*it1++) * a;
78  *it2++ = (*it1++) * a;
79  *it2++ = (*it1++) * a;
80  *it2++ = (*it1++) * a;
81  *it2++ = (*it1++) * a;
82  *it2++ = (*it1++) * a;
83  *it2++ = (*it1++) * a;
84  }
85  for (; it2 != it2e;)
86  *it2++ = (*it1++) * a;
87  }
88 
89  template <class VEC1, class VEC2>
90  inline void add_scaled_8(const VEC1 &v1, const scalar_type a, VEC2 &v2) {
91  auto it1 = v1.begin();
92  auto it2 = v2.begin(), it2e = v2.end();
93  size_type nd = v1.size() >> 3;
94  for (size_type i = 0; i < nd; ++i) {
95  *it2++ += (*it1++) * a;
96  *it2++ += (*it1++) * a;
97  *it2++ += (*it1++) * a;
98  *it2++ += (*it1++) * a;
99  *it2++ += (*it1++) * a;
100  *it2++ += (*it1++) * a;
101  *it2++ += (*it1++) * a;
102  *it2++ += (*it1++) * a;
103  }
104  for (; it2 != it2e;)
105  *it2++ += (*it1++) * a;
106  }
107 
108  bool operator <(const gauss_pt_corresp &gpc1,
109  const gauss_pt_corresp &gpc2) {
110  if (gpc1.pai != gpc2.pai)
111  return (gpc1.pai < gpc2.pai );
112  if (gpc1.nodes.size() != gpc2.nodes.size())
113  return (gpc1.nodes.size() < gpc2.nodes.size());
114  for (size_type i = 0; i < gpc1.nodes.size(); ++i)
115  if (gpc1.nodes[i] != gpc2.nodes[i])
116  return (gpc1.nodes[i] < gpc2.nodes[i]);
117  if (gpc1.pgt1 != gpc2.pgt1)
118  return (gpc1.pgt1 < gpc2.pgt1);
119  if (gpc1.pgt2 != gpc2.pgt2)
120  return (gpc1.pgt2 < gpc2.pgt2);
121  return false;
122  }
123 
124  bool operator <(const ga_instruction_set::region_mim &rm1,
125  const ga_instruction_set::region_mim &rm2) {
126  if (rm1.mim() != rm2.mim()) return (rm1.mim() < rm2.mim());
127  if (rm1.region() != rm2.region()) return (rm1.region() < rm2.region());
128  return (rm1.psd() < rm2.psd());
129  }
130 
131  //=========================================================================
132  // Instructions for compilation: basic optimized operations on tensors
133  //=========================================================================
134 
135  struct ga_instruction_extract_local_im_data : public ga_instruction {
136  base_tensor &t;
137  const im_data &imd;
138  papprox_integration &pai;
139  const base_vector &U;
140  const fem_interpolation_context &ctx;
141  size_type qdim, cv_old;
142  virtual int exec() {
143  GA_DEBUG_INFO("Instruction: extract local im data");
144  size_type cv = ctx.convex_num();
145  if (cv != cv_old) {
146  cv_old = cv;
147  GMM_ASSERT1(imd.linked_mesh_im().int_method_of_element(cv)
148  ->approx_method() == pai, "Im data have to be used only "
149  "on their original integration method.");
150  }
151  size_type ipt = imd.filtered_index_of_point(cv, ctx.ii());
152  GMM_ASSERT1(ipt != size_type(-1),
153  "Im data with no data on the current integration point.");
154  auto it = U.begin()+ipt*qdim;
155  std::copy(it, it+qdim, t.begin());
156  return 0;
157  }
158  ga_instruction_extract_local_im_data
159  (base_tensor &t_, const im_data &imd_, const base_vector &U_,
160  papprox_integration &pai_, const fem_interpolation_context &ctx_,
161  size_type qdim_)
162  : t(t_), imd(imd_), pai(pai_), U(U_), ctx(ctx_), qdim(qdim_),
163  cv_old(-1)
164  {}
165  };
166 
167  struct ga_instruction_slice_local_dofs : public ga_instruction {
168  const mesh_fem &mf;
169  const base_vector &U;
170  const fem_interpolation_context &ctx;
171  base_vector &coeff;
172  size_type qmult1, qmult2;
173  virtual int exec() {
174  GA_DEBUG_INFO("Instruction: Slice local dofs");
175  GMM_ASSERT1(qmult1 != 0 && qmult2 != 0, "Internal error");
176  slice_vector_on_basic_dof_of_element(mf, U, ctx.convex_num(),
177  coeff, qmult1, qmult2);
178  return 0;
179  }
180  ga_instruction_slice_local_dofs(const mesh_fem &mf_, const base_vector &U_,
181  const fem_interpolation_context &ctx_,
182  base_vector &coeff_,
183  size_type qmult1_, size_type qmult2_)
184  : mf(mf_), U(U_), ctx(ctx_), coeff(coeff_),
185  qmult1(qmult1_), qmult2(qmult2_) {}
186  };
187 
188  struct ga_instruction_update_pfp : public ga_instruction {
189  const mesh_fem &mf;
190  const fem_interpolation_context &ctx;
191  fem_precomp_pool &fp_pool;
192  pfem_precomp &pfp;
193 
194  virtual int exec() {
195  GA_DEBUG_INFO("Instruction: Pfp update");
196  if (ctx.have_pgp()) {
197  size_type cv = ctx.is_convex_num_valid()
198  ? ctx.convex_num() : mf.convex_index().first_true();
199  pfem pf = mf.fem_of_element(cv);
200  if (!pfp || pf != pfp->get_pfem() ||
201  ctx.pgp()->get_ppoint_tab() != pfp->get_ppoint_tab()) {
202  pfp = fp_pool(pf, ctx.pgp()->get_ppoint_tab());
203  }
204  } else {
205  pfp = 0;
206  }
207  return 0;
208  }
209 
210  ga_instruction_update_pfp(const mesh_fem &mf_, pfem_precomp &pfp_,
211  const fem_interpolation_context &ctx_,
212  fem_precomp_pool &fp_pool_)
213  : mf(mf_), ctx(ctx_), fp_pool(fp_pool_), pfp(pfp_) {}
214  };
215 
216  struct ga_instruction_first_ind_tensor : public ga_instruction {
217  base_tensor &t;
218  const fem_interpolation_context &ctx;
219  size_type qdim;
220  const mesh_fem *mfn, **mfg;
221 
222  virtual int exec() {
223  GA_DEBUG_INFO("Instruction: adapt first index of tensor");
224  const mesh_fem &mf = *(mfg ? *mfg : mfn);
225  GA_DEBUG_ASSERT(mfg ? *mfg : mfn, "Internal error");
226  size_type cv_1 = ctx.is_convex_num_valid()
227  ? ctx.convex_num() : mf.convex_index().first_true();
228  pfem pf = mf.fem_of_element(cv_1);
229  GMM_ASSERT1(pf, "An element without finite element method defined");
230  size_type Qmult = qdim / pf->target_dim();
231  size_type s = pf->nb_dof(cv_1) * Qmult;
232  if (t.sizes()[0] != s)
233  { bgeot::multi_index mi = t.sizes(); mi[0] = s; t.adjust_sizes(mi); }
234  return 0;
235  }
236 
237  ga_instruction_first_ind_tensor(base_tensor &t_,
238  const fem_interpolation_context &ctx_,
239  size_type qdim_, const mesh_fem *mfn_,
240  const mesh_fem **mfg_)
241  : t(t_), ctx(ctx_), qdim(qdim_), mfn(mfn_), mfg(mfg_) {}
242  };
243 
244  struct ga_instruction_second_ind_tensor
245  : public ga_instruction_first_ind_tensor {
246 
247  virtual int exec() {
248  GA_DEBUG_INFO("Instruction: adapt second index of tensor");
249  const mesh_fem &mf = *(mfg ? *mfg : mfn);
250  size_type cv_1 = ctx.is_convex_num_valid()
251  ? ctx.convex_num() : mf.convex_index().first_true();
252  pfem pf = mf.fem_of_element(cv_1);
253  GMM_ASSERT1(pf, "An element without finite element methode defined");
254  size_type Qmult = qdim / pf->target_dim();
255  size_type s = pf->nb_dof(cv_1) * Qmult;
256  if (t.sizes()[1] != s)
257  { bgeot::multi_index mi = t.sizes(); mi[1] = s; t.adjust_sizes(mi); }
258  return 0;
259  }
260 
261  ga_instruction_second_ind_tensor(base_tensor &t_,
262  fem_interpolation_context &ctx_,
263  size_type qdim_, const mesh_fem *mfn_,
264  const mesh_fem **mfg_)
265  : ga_instruction_first_ind_tensor(t_, ctx_, qdim_, mfn_, mfg_) {}
266 
267  };
268 
269  struct ga_instruction_two_first_ind_tensor : public ga_instruction {
270  base_tensor &t;
271  const fem_interpolation_context &ctx1, &ctx2;
272  size_type qdim1;
273  const mesh_fem *mfn1, **mfg1;
274  size_type qdim2;
275  const mesh_fem *mfn2, **mfg2;
276 
277  virtual int exec() {
278  GA_DEBUG_INFO("Instruction: adapt two first indices of tensor");
279  const mesh_fem &mf1 = *(mfg1 ? *mfg1 : mfn1);
280  const mesh_fem &mf2 = *(mfg2 ? *mfg2 : mfn2);
281  size_type cv_1 = ctx1.is_convex_num_valid()
282  ? ctx1.convex_num() : mf1.convex_index().first_true();
283  size_type cv_2 = ctx2.is_convex_num_valid()
284  ? ctx2.convex_num() : mf2.convex_index().first_true();
285  pfem pf1 = mf1.fem_of_element(cv_1);
286  GMM_ASSERT1(pf1, "An element without finite element method defined");
287  pfem pf2 = mf2.fem_of_element(cv_2);
288  GMM_ASSERT1(pf2, "An element without finite element method defined");
289  size_type Qmult1 = qdim1 / pf1->target_dim();
290  size_type s1 = pf1->nb_dof(cv_1) * Qmult1;
291  size_type Qmult2 = qdim2 / pf2->target_dim();
292  size_type s2 = pf2->nb_dof(cv_2) * Qmult2;
293  GMM_ASSERT1(s1 > 0 && s2 >0, "Element without degrees of freedom");
294  if (t.sizes()[0] != s1 || t.sizes()[1] != s2) {
295  bgeot::multi_index mi = t.sizes();
296  mi[0] = s1; mi[1] = s2;
297  t.adjust_sizes(mi);
298  }
299  return 0;
300  }
301 
302  ga_instruction_two_first_ind_tensor
303  (base_tensor &t_, const fem_interpolation_context &ctx1_,
304  const fem_interpolation_context &ctx2_,
305  size_type qdim1_, const mesh_fem *mfn1_, const mesh_fem **mfg1_,
306  size_type qdim2_, const mesh_fem *mfn2_, const mesh_fem **mfg2_)
307  : t(t_), ctx1(ctx1_), ctx2(ctx2_), qdim1(qdim1_), mfn1(mfn1_),
308  mfg1(mfg1_), qdim2(qdim2_), mfn2(mfn2_), mfg2(mfg2_) {}
309  };
310 
311 
312  struct ga_instruction_X_component : public ga_instruction {
313  scalar_type &t;
314  const fem_interpolation_context &ctx;
315  size_type n;
316 
317  virtual int exec() {
318  GA_DEBUG_INFO("Instruction: X component");
319  t = ctx.xreal()[n];
320  return 0;
321  }
322 
323  ga_instruction_X_component
324  (scalar_type &t_, const fem_interpolation_context &ctx_, size_type n_)
325  : t(t_), ctx(ctx_), n(n_) {}
326  };
327 
328  struct ga_instruction_X : public ga_instruction {
329  base_tensor &t;
330  const fem_interpolation_context &ctx;
331 
332  virtual int exec() {
333  GA_DEBUG_INFO("Instruction: X");
334  GA_DEBUG_ASSERT(t.size() == ctx.xreal().size(), "dimensions mismatch");
335  gmm::copy(ctx.xreal(), t.as_vector());
336  return 0;
337  }
338 
339  ga_instruction_X(base_tensor &t_, const fem_interpolation_context &ctx_)
340  : t(t_), ctx(ctx_) {}
341  };
342 
343  struct ga_instruction_copy_small_vect : public ga_instruction {
344  base_tensor &t;
345  const base_small_vector &vec;
346 
347  virtual int exec() {
348  GA_DEBUG_INFO("Instruction: copy small vector");
349  GMM_ASSERT1(t.size() == vec.size(), "Invalid vector size.");
350  gmm::copy(vec, t.as_vector());
351  return 0;
352  }
353  ga_instruction_copy_small_vect(base_tensor &t_,
354  const base_small_vector &vec_)
355  : t(t_), vec(vec_) {}
356  };
357 
358  struct ga_instruction_copy_Normal : public ga_instruction_copy_small_vect {
359 
360  virtual int exec() {
361  GA_DEBUG_INFO("Instruction: unit normal vector");
362  GMM_ASSERT1(t.size() == vec.size(), "Invalid outward unit normal "
363  "vector. Possible reasons: not on boundary (Normal is "
364  "defined on faces, and inside the elements of curves in "
365  "2D and surfaces in 3D) or transformation failed.");
366  gmm::copy(vec, t.as_vector());
367  return 0;
368  }
369  ga_instruction_copy_Normal(base_tensor &t_,
370  const base_small_vector &Normal_)
371  : ga_instruction_copy_small_vect(t_, Normal_) {}
372  };
373 
374  struct ga_instruction_level_set_normal_vector : public ga_instruction {
375  base_tensor &t;
376  const mesh_im_level_set *mimls;
377  const fem_interpolation_context &ctx;
378  base_small_vector vec;
379 
380  virtual int exec() {
381  GA_DEBUG_INFO("Instruction: unit normal vector to a level-set");
382  mimls->compute_normal_vector(ctx, vec);
383  GMM_ASSERT1(t.size() == vec.size(), "Invalid outward unit normal "
384  "vector. Possible reasons: not on boundary or "
385  "transformation failed.");
386  gmm::copy(vec, t.as_vector());
387  return 0;
388  }
389  ga_instruction_level_set_normal_vector
390  (base_tensor &t_, const mesh_im_level_set *mimls_,
391  const fem_interpolation_context &ctx_)
392  : t(t_), mimls(mimls_), ctx(ctx_), vec(t.size()) {}
393  };
394 
395  struct ga_instruction_element_size : public ga_instruction {
396  base_tensor &t;
397  scalar_type &es;
398 
399  virtual int exec() {
400  GA_DEBUG_INFO("Instruction: element_size");
401  GMM_ASSERT1(t.size() == 1, "Invalid element size.");
402  t[0] = es;
403  return 0;
404  }
405  ga_instruction_element_size(base_tensor &t_, scalar_type &es_)
406  : t(t_), es(es_) {}
407  };
408 
409  struct ga_instruction_element_K : public ga_instruction {
410  base_tensor &t;
411  const fem_interpolation_context &ctx;
412 
413  virtual int exec() {
414  GA_DEBUG_INFO("Instruction: element_K");
415  GMM_ASSERT1(t.size() == (ctx.K()).size(), "Invalid tensor size.");
416  gmm::copy(ctx.K().as_vector(), t.as_vector());
417  return 0;
418  }
419  ga_instruction_element_K(base_tensor &t_,
420  const fem_interpolation_context &ct)
421  : t(t_), ctx(ct) {}
422  };
423 
424  struct ga_instruction_element_B : public ga_instruction {
425  base_tensor &t;
426  const fem_interpolation_context &ctx;
427 
428  virtual int exec() {
429  GA_DEBUG_INFO("Instruction: element_B");
430  GMM_ASSERT1(t.size() == (ctx.B()).size(), "Invalid tensor size.");
431  gmm::copy(ctx.B().as_vector(), t.as_vector());
432  return 0;
433  }
434  ga_instruction_element_B(base_tensor &t_,
435  const fem_interpolation_context &ct)
436  : t(t_), ctx(ct) {}
437  };
438 
439  // Curvature tensor of a hypersurface (curve in 2D, surface in 3D)
440  // computed elementwise from the geometric transformation:
441  // element_curvature = grad_s n = - B b B^T,
442  // where n is the unit normal oriented by the element parametrization
443  // (n = K_1 x K_2 in 3D, n = (K_12, -K_11) in 2D) and
444  // b_ab = n . d^2x/dxi_a dxi_b is the second fundamental form.
445  // The result is a symmetric N x N matrix with n in its kernel; its
446  // nonzero eigenvalues are the principal curvatures (positive for a
447  // sphere/circle with outward normal). Vanishes for linear transformations.
448  struct ga_instruction_element_curvature : public ga_instruction {
449  base_tensor &t;
450  const fem_interpolation_context &ctx;
451  base_matrix PC, Htau, bB;
452  base_small_vector n, bab;
453 
454  virtual int exec() {
455  GA_DEBUG_INFO("Instruction: element_curvature");
456  bgeot::pgeometric_trans pgt = ctx.pgt();
457  size_type N = ctx.N(), P = pgt->dim();
458  GMM_ASSERT1(P+1 == N && (N == 2 || N == 3), "element_curvature is "
459  "only defined for curves in 2D and surfaces in 3D");
460  GMM_ASSERT1(t.size() == N*N, "Invalid tensor size.");
461  gmm::clear(t.as_vector());
462  if (pgt->is_linear()) return 0;
463 
464  const base_matrix &K = ctx.K(), &B = ctx.B();
465  n.resize(N);
466  if (N == 2) {
467  n[0] = K(1,0); n[1] = -K(0,0);
468  } else {
469  n[0] = K(1,0)*K(2,1) - K(2,0)*K(1,1);
470  n[1] = K(2,0)*K(0,1) - K(0,0)*K(2,1);
471  n[2] = K(0,0)*K(1,1) - K(1,0)*K(0,1);
472  }
473  gmm::scale(n, scalar_type(1)/gmm::vect_norm2(n));
474 
475  // Second derivatives of the geometric transformation (N x P*P)
476  Htau.base_resize(N, P*P);
477  if (ctx.have_pgp() && ctx.ii() != size_type(-1))
478  gmm::mult(ctx.G(), ctx.pgp()->hessian(ctx.ii()), Htau);
479  else {
480  PC.base_resize(pgt->nb_points(), P*P);
481  pgt->poly_vector_hess(ctx.xref(), PC);
482  gmm::mult(ctx.G(), PC, Htau);
483  }
484 
485  // b_ab = n . Htau(:, a + P*b)
486  bab.resize(P*P);
487  gmm::mult(gmm::transposed(Htau), n, bab);
488 
489  // t = - B b B^T
490  bB.base_resize(P, N);
491  gmm::clear(bB);
492  for (size_type a = 0; a < P; ++a)
493  for (size_type c = 0; c < P; ++c)
494  for (size_type j = 0; j < N; ++j)
495  bB(a, j) += bab[a + P*c] * B(j, c);
496  for (size_type j = 0; j < N; ++j)
497  for (size_type i = 0; i < N; ++i) {
498  scalar_type s(0);
499  for (size_type a = 0; a < P; ++a) s += B(i, a) * bB(a, j);
500  t[i + N*j] = -s;
501  }
502  return 0;
503  }
504  ga_instruction_element_curvature(base_tensor &t_,
505  const fem_interpolation_context &ct)
506  : t(t_), ctx(ct) {}
507  };
508 
509  struct ga_instruction_val_base : public ga_instruction {
510  base_tensor &t;
511  fem_interpolation_context &ctx;
512  const mesh_fem &mf;
513  const pfem_precomp &pfp;
514 
515  virtual int exec() { // --> t(ndof,target_dim)
516  GA_DEBUG_INFO("Instruction: compute value of base functions");
517  // if (ctx.have_pgp()) ctx.set_pfp(pfp);
518  // else ctx.set_pf(mf.fem_of_element(ctx.convex_num()));
519  // GMM_ASSERT1(ctx.pf(), "Undefined finite element method");
520  // ctx.base_value(t);
521  if (ctx.have_pgp()) ctx.pfp_base_value(t, pfp);
522  else {
523  ctx.set_pf(mf.fem_of_element(ctx.convex_num()));
524  GMM_ASSERT1(ctx.pf(), "Undefined finite element method");
525  ctx.base_value(t);
526  }
527  return 0;
528  }
529 
530  ga_instruction_val_base(base_tensor &tt, fem_interpolation_context &ct,
531  const mesh_fem &mf_, const pfem_precomp &pfp_)
532  : t(tt), ctx(ct), mf(mf_), pfp(pfp_) {}
533  };
534 
535  struct ga_instruction_xfem_plus_val_base : public ga_instruction {
536  base_tensor &t;
537  fem_interpolation_context &ctx;
538  const mesh_fem &mf;
539  pfem_precomp &pfp;
540 
541  virtual int exec() { // --> t(ndof,target_dim)
542  GA_DEBUG_INFO("Instruction: compute value of base functions");
543  if (ctx.have_pgp()) ctx.set_pfp(pfp);
544  else ctx.set_pf(mf.fem_of_element(ctx.convex_num()));
545  GMM_ASSERT1(ctx.pf(), "Undefined finite element method");
546  int old_xfem_side = ctx.xfem_side();
547  ctx.set_xfem_side(1);
548  ctx.base_value(t);
549  ctx.set_xfem_side(old_xfem_side);
550  return 0;
551  }
552 
553  ga_instruction_xfem_plus_val_base(base_tensor &tt,
554  fem_interpolation_context &ct,
555  const mesh_fem &mf_, pfem_precomp &pfp_)
556  : t(tt), ctx(ct), mf(mf_), pfp(pfp_) {}
557  };
558 
559  struct ga_instruction_xfem_minus_val_base : public ga_instruction {
560  base_tensor &t;
561  fem_interpolation_context &ctx;
562  const mesh_fem &mf;
563  pfem_precomp &pfp;
564 
565  virtual int exec() { // --> t(ndof,target_dim)
566  GA_DEBUG_INFO("Instruction: compute value of base functions");
567  if (ctx.have_pgp()) ctx.set_pfp(pfp);
568  else ctx.set_pf(mf.fem_of_element(ctx.convex_num()));
569  GMM_ASSERT1(ctx.pf(), "Undefined finite element method");
570  int old_xfem_side = ctx.xfem_side();
571  ctx.set_xfem_side(-1);
572  ctx.base_value(t);
573  ctx.set_xfem_side(old_xfem_side);
574  return 0;
575  }
576 
577  ga_instruction_xfem_minus_val_base
578  (base_tensor &tt, fem_interpolation_context &ct,
579  const mesh_fem &mf_, pfem_precomp &pfp_)
580  : t(tt), ctx(ct), mf(mf_), pfp(pfp_) {}
581  };
582 
583  struct ga_instruction_grad_base : public ga_instruction_val_base {
584 
585  virtual int exec() { // --> t(ndof,target_dim,N)
586  GA_DEBUG_INFO("Instruction: compute gradient of base functions");
587  // if (ctx.have_pgp()) ctx.set_pfp(pfp);
588  // else ctx.set_pf(mf.fem_of_element(ctx.convex_num()));
589  // GMM_ASSERT1(ctx.pf(), "Undefined finite element method");
590  // ctx.grad_base_value(t);
591  if (ctx.have_pgp()) ctx.pfp_grad_base_value(t, pfp);
592  else {
593  ctx.set_pf(mf.fem_of_element(ctx.convex_num()));
594  GMM_ASSERT1(ctx.pf(), "Undefined finite element method");
595  ctx.grad_base_value(t);
596  }
597  return 0;
598  }
599 
600  ga_instruction_grad_base(base_tensor &tt, fem_interpolation_context &ct,
601  const mesh_fem &mf_, pfem_precomp &pfp_)
602  : ga_instruction_val_base(tt, ct, mf_, pfp_)
603  {}
604  };
605 
606  struct ga_instruction_xfem_plus_grad_base : public ga_instruction_val_base {
607 
608  virtual int exec() { // --> t(ndof,target_dim,N)
609  GA_DEBUG_INFO("Instruction: compute gradient of base functions");
610  if (ctx.have_pgp()) ctx.set_pfp(pfp);
611  else ctx.set_pf(mf.fem_of_element(ctx.convex_num()));
612  GMM_ASSERT1(ctx.pf(), "Undefined finite element method");
613  int old_xfem_side = ctx.xfem_side();
614  ctx.set_xfem_side(1);
615  ctx.grad_base_value(t);
616  ctx.set_xfem_side(old_xfem_side);
617  return 0;
618  }
619 
620  ga_instruction_xfem_plus_grad_base
621  (base_tensor &tt, fem_interpolation_context &ct,
622  const mesh_fem &mf_, pfem_precomp &pfp_)
623  : ga_instruction_val_base(tt, ct, mf_, pfp_)
624  {}
625  };
626 
627  struct ga_instruction_xfem_minus_grad_base : public ga_instruction_val_base {
628 
629  virtual int exec() { // --> t(ndof,target_dim,N)
630  GA_DEBUG_INFO("Instruction: compute gradient of base functions");
631  if (ctx.have_pgp()) ctx.set_pfp(pfp);
632  else ctx.set_pf(mf.fem_of_element(ctx.convex_num()));
633  GMM_ASSERT1(ctx.pf(), "Undefined finite element method");
634  int old_xfem_side = ctx.xfem_side();
635  ctx.set_xfem_side(-1);
636  ctx.grad_base_value(t);
637  ctx.set_xfem_side(old_xfem_side);
638  return 0;
639  }
640 
641  ga_instruction_xfem_minus_grad_base
642  (base_tensor &tt, fem_interpolation_context &ct,
643  const mesh_fem &mf_, pfem_precomp &pfp_)
644  : ga_instruction_val_base(tt, ct, mf_, pfp_)
645  {}
646  };
647 
648 
649  struct ga_instruction_hess_base : public ga_instruction_val_base {
650 
651  virtual int exec() { // --> t(ndof,target_dim,N*N)
652  GA_DEBUG_INFO("Instruction: compute Hessian of base functions");
653  if (ctx.have_pgp()) ctx.set_pfp(pfp);
654  else ctx.set_pf(mf.fem_of_element(ctx.convex_num()));
655  GMM_ASSERT1(ctx.pf(), "Undefined finite element method");
656  ctx.hess_base_value(t);
657  return 0;
658  }
659 
660  ga_instruction_hess_base(base_tensor &tt, fem_interpolation_context &ct,
661  const mesh_fem &mf_, pfem_precomp &pfp_)
662  : ga_instruction_val_base(tt, ct, mf_, pfp_)
663  {}
664  };
665 
666  struct ga_instruction_xfem_plus_hess_base : public ga_instruction_val_base {
667 
668  virtual int exec() { // --> t(ndof,target_dim,N*N)
669  GA_DEBUG_INFO("Instruction: compute Hessian of base functions");
670  if (ctx.have_pgp()) ctx.set_pfp(pfp);
671  else ctx.set_pf(mf.fem_of_element(ctx.convex_num()));
672  GMM_ASSERT1(ctx.pf(), "Undefined finite element method");
673  int old_xfem_side = ctx.xfem_side();
674  ctx.set_xfem_side(1);
675  ctx.hess_base_value(t);
676  ctx.set_xfem_side(old_xfem_side);
677  return 0;
678  }
679 
680  ga_instruction_xfem_plus_hess_base
681  (base_tensor &tt, fem_interpolation_context &ct,
682  const mesh_fem &mf_, pfem_precomp &pfp_)
683  : ga_instruction_val_base(tt, ct, mf_, pfp_)
684  {}
685  };
686 
687  struct ga_instruction_xfem_minus_hess_base : public ga_instruction_val_base {
688 
689  virtual int exec() { // --> t(ndof,target_dim,N*N)
690  GA_DEBUG_INFO("Instruction: compute Hessian of base functions");
691  if (ctx.have_pgp()) ctx.set_pfp(pfp);
692  else ctx.set_pf(mf.fem_of_element(ctx.convex_num()));
693  GMM_ASSERT1(ctx.pf(), "Undefined finite element method");
694  int old_xfem_side = ctx.xfem_side();
695  ctx.set_xfem_side(-1);
696  ctx.hess_base_value(t);
697  ctx.set_xfem_side(old_xfem_side);
698  return 0;
699  }
700 
701  ga_instruction_xfem_minus_hess_base
702  (base_tensor &tt, fem_interpolation_context &ct,
703  const mesh_fem &mf_, pfem_precomp &pfp_)
704  : ga_instruction_val_base(tt, ct, mf_, pfp_)
705  {}
706  };
707 
708  struct ga_instruction_val : public ga_instruction {
709  scalar_type &a;
710  base_tensor &t;
711  const base_tensor &Z;
712  const base_vector &coeff;
713  size_type qdim;
714  // Z(ndof,target_dim), coeff(Qmult,ndof) --> t(target_dim*Qmult)
715  virtual int exec() {
716  GA_DEBUG_INFO("Instruction: variable value");
717  size_type ndof = Z.sizes()[0];
718  if (!ndof) { gmm::clear(t.as_vector()); return 0; }
719  GA_DEBUG_ASSERT(t.size() == qdim, "dimensions mismatch");
720 
721  if (qdim == 1) {
722  GA_DEBUG_ASSERT(gmm::vect_size(coeff) == ndof,
723  "Wrong size for coeff vector");
724  auto itc = coeff.begin(); auto itZ = Z.begin();
725  a = (*itc++) * (*itZ++);
726  while (itc != coeff.end()) a += (*itc++) * (*itZ++);
727  } else {
728  size_type target_dim = Z.sizes()[1];
729  if (target_dim == 1) {
730  GA_DEBUG_ASSERT(gmm::vect_size(coeff) == ndof*qdim,
731  "Wrong size for coeff vector");
732  auto itc = coeff.begin(); auto itZ = Z.begin();
733  for (auto it = t.begin(); it != t.end(); ++it)
734  *it = (*itc++) * (*itZ);
735  ++itZ;
736  for (size_type j = 1; j < ndof; ++j, ++itZ) {
737  for (auto it = t.begin(); it != t.end(); ++it)
738  *it += (*itc++) * (*itZ);
739  }
740  } else {
741  size_type Qmult = qdim / target_dim;
742  GA_DEBUG_ASSERT(gmm::vect_size(coeff) == ndof*Qmult,
743  "Wrong size for coeff vector");
744 
745  gmm::clear(t.as_vector());
746  auto itc = coeff.begin();
747  for (size_type j = 0; j < ndof; ++j) {
748  auto it = t.begin();
749  for (size_type q = 0; q < Qmult; ++q, ++itc) {
750  for (size_type r = 0; r < target_dim; ++r)
751  *it++ += (*itc) * Z[j + r*ndof];
752  }
753  }
754  }
755  }
756  return 0;
757  }
758 
759  ga_instruction_val(base_tensor &tt, const base_tensor &Z_,
760  const base_vector &co, size_type q)
761  : a(tt[0]), t(tt), Z(Z_), coeff(co), qdim(q) {}
762  };
763 
764  struct ga_instruction_grad : public ga_instruction_val {
765  // Z(ndof,target_dim,N), coeff(Qmult,ndof) --> t(target_dim*Qmult,N)
766  virtual int exec() {
767  GA_DEBUG_INFO("Instruction: gradient");
768  size_type ndof = Z.sizes()[0];
769  if (!ndof) { gmm::clear(t.as_vector()); return 0; }
770  size_type N = Z.sizes()[2];
771  if (qdim == 1) {
772  GA_DEBUG_ASSERT(t.size() == N, "dimensions mismatch");
773  GA_DEBUG_ASSERT(coeff.size() == ndof, "Wrong size for coeff vector");
774  auto itZ = Z.begin();
775  for (auto it = t.begin(); it != t.end(); ++it) {
776  auto itc = coeff.begin();
777  *it = (*itc++) * (*itZ++);
778  while (itc != coeff.end()) *it += (*itc++) * (*itZ++);
779  }
780  } else {
781  size_type target_dim = Z.sizes()[1];
782  if (target_dim == 1) {
783  GA_DEBUG_ASSERT(t.size() == N*qdim, "dimensions mismatch");
784  GA_DEBUG_ASSERT(coeff.size() == ndof*qdim,
785  "Wrong size for coeff vector");
786  for (size_type q = 0; q < qdim; ++q) {
787  auto itZ = Z.begin(); auto it = t.begin() + q;
788  for (size_type k = 0; k < N; ++k) {
789  if (k) it += qdim;
790  auto itc = coeff.begin() + q;
791  *it = (*itc) * (*itZ++);
792  for (size_type j = 1; j < ndof; ++j)
793  { itc += qdim; *it += (*itc) * (*itZ++); }
794  }
795  }
796  } else {
797  size_type Qmult = qdim / target_dim;
798  GA_DEBUG_ASSERT(t.size() == N*qdim, "dimensions mismatch");
799  GA_DEBUG_ASSERT(coeff.size() == ndof*Qmult,
800  "Wrong size for coeff vector");
801  gmm::clear(t.as_vector());
802  for (size_type q = 0; q < Qmult; ++q) {
803  auto itZ = Z.begin();
804  for (size_type k = 0; k < N; ++k)
805  for (size_type r = 0; r < target_dim; ++r)
806  for (size_type j = 0; j < ndof; ++j)
807  t[r + q*target_dim + k*qdim] += coeff[j*Qmult+q] * (*itZ++);
808  }
809  }
810  }
811  return 0;
812  }
813 
814  ga_instruction_grad(base_tensor &tt, const base_tensor &Z_,
815  const base_vector &co, size_type q)
816  : ga_instruction_val(tt, Z_, co, q)
817  {}
818 
819  };
820 
821  struct ga_instruction_hess : public ga_instruction_val {
822  // Z(ndof,target_dim,N*N), coeff(Qmult,ndof) --> t(target_dim*Qmult,N,N)
823  virtual int exec() {
824  GA_DEBUG_INFO("Instruction: Hessian");
825  size_type ndof = Z.sizes()[0];
826  if (!ndof) { gmm::clear(t.as_vector()); return 0; }
827  size_type NN = gmm::sqr(t.sizes().back());
828  GA_DEBUG_ASSERT(NN == Z.sizes()[2], "Internal error");
829  if (qdim == 1) {
830  GA_DEBUG_ASSERT(gmm::vect_size(coeff) == ndof,
831  "Wrong size for coeff vector");
832  auto it = Z.begin(); auto itt = t.begin();
833  for (size_type kl = 0; kl < NN; ++kl, ++itt) {
834  *itt = scalar_type(0);
835  for (auto itc = coeff.begin(); itc != coeff.end(); ++itc, ++it)
836  *itt += (*itc) * (*it);
837  }
838  GMM_ASSERT1(itt == t.end(), "dimensions mismatch");
839  } else {
840  size_type target_dim = Z.sizes()[1];
841  if (target_dim == 1) {
842  GA_DEBUG_ASSERT(t.size() == NN*qdim, "dimensions mismatch");
843  GA_DEBUG_ASSERT(gmm::vect_size(coeff) == ndof*qdim,
844  "Wrong size for coeff vector");
845  gmm::clear(t.as_vector());
846  for (size_type q = 0; q < qdim; ++q) {
847  base_tensor::const_iterator it = Z.begin();
848  for (size_type kl = 0; kl < NN; ++kl)
849  for (size_type j = 0; j < ndof; ++j, ++it)
850  t[q + kl*qdim] += coeff[j*qdim+q] * (*it);
851  }
852  } else {
853  size_type Qmult = qdim / target_dim;
854  GA_DEBUG_ASSERT(t.size() == NN*qdim, "dimensions mismatch");
855  GA_DEBUG_ASSERT(gmm::vect_size(coeff) == ndof*Qmult,
856  "Wrong size for coeff vector");
857  gmm::clear(t.as_vector());
858  for (size_type q = 0; q < Qmult; ++q) {
859  base_tensor::const_iterator it = Z.begin();
860  for (size_type kl = 0; kl < NN; ++kl)
861  for (size_type r = 0; r < target_dim; ++r)
862  for (size_type j = 0; j < ndof; ++j, ++it)
863  t[r + q*target_dim + kl*qdim] += coeff[j*Qmult+q] * (*it);
864  }
865  }
866  }
867  return 0;
868  }
869 
870  ga_instruction_hess(base_tensor &tt, const base_tensor &Z_,
871  const base_vector &co, size_type q)
872  : ga_instruction_val(tt, Z_, co, q)
873  {}
874  };
875 
876  struct ga_instruction_diverg : public ga_instruction_val {
877  // Z(ndof,target_dim,N), coeff(Qmult,ndof) --> t(1)
878  virtual int exec() {
879  GA_DEBUG_INFO("Instruction: divergence");
880  size_type ndof = Z.sizes()[0];
881  if (!ndof) { gmm::clear(t.as_vector()); return 0; }
882  size_type target_dim = Z.sizes()[1];
883  size_type N = Z.sizes()[2];
884  size_type Qmult = qdim / target_dim;
885  GA_DEBUG_ASSERT(Qmult*target_dim == N && (Qmult == 1 || target_dim == 1),
886  "Dimensions mismatch for divergence operator");
887  GA_DEBUG_ASSERT(gmm::vect_size(coeff) == ndof*Qmult,
888  "Wrong size for coeff vector");
889 
890  t[0] = scalar_type(0);
891  base_tensor::const_iterator it = Z.begin();
892  if (Qmult == 1)
893  for (size_type k = 0; k < N; ++k) {
894  if (k) it += (N*ndof + 1);
895  for (size_type j = 0; j < ndof; ++j) {
896  if (j) ++it;
897  t[0] += coeff[j] * (*it);
898  }
899  }
900  else // if (target_dim() == 1)
901  for (size_type k = 0; k < N; ++k) {
902  if (k) ++it;
903  for (size_type j = 0; j < ndof; ++j) {
904  if (j) ++it;
905  t[0] += coeff[j*N+k] * (*it);
906  }
907  }
908  return 0;
909  }
910 
911  ga_instruction_diverg(base_tensor &tt, const base_tensor &Z_,
912  const base_vector &co, size_type q)
913  : ga_instruction_val(tt, Z_, co, q)
914  {}
915  };
916 
917  struct ga_instruction_copy_val_base : public ga_instruction {
918  base_tensor &t;
919  const base_tensor &Z;
920  size_type qdim;
921  // Z(ndof,target_dim) --> t(Qmult*ndof,Qmult*target_dim)
922  virtual int exec() {
923  GA_DEBUG_INFO("Instruction: value of test functions");
924  if (qdim == 1) {
925  GA_DEBUG_ASSERT(t.size() == Z.size(), "Wrong size for base vector");
926  std::copy(Z.begin(), Z.end(), t.begin());
927  } else {
928  size_type target_dim = Z.sizes()[1];
929  size_type Qmult = qdim / target_dim;
930  if (Qmult == 1) {
931  std::copy(Z.begin(), Z.end(), t.begin());
932  } else {
933  if (target_dim == 1) {
934  size_type ndof = Z.sizes()[0];
935  GA_DEBUG_ASSERT(t.size() == Z.size() * Qmult * Qmult,
936  "Wrong size for base vector");
937  std::fill(t.begin(), t.end(), scalar_type(0));
938  auto itZ = Z.begin();
939  size_type s = t.sizes()[0], sss = s+1;
940 
941  // Performs t(i*Qmult+j, k*Qmult + j) = Z(i,k);
942  auto it = t.begin();
943  for (size_type i = 0; i < ndof; ++i, ++itZ) {
944  if (i) it += Qmult;
945  auto it2 = it;
946  *it2 = *itZ;
947  for (size_type j = 1; j < Qmult; ++j) { it2 += sss; *it2 = *itZ; }
948  }
949  } else {
950  size_type ndof = Z.sizes()[0];
951  GA_DEBUG_ASSERT(t.size() == Z.size() * Qmult * Qmult,
952  "Wrong size for base vector");
953  std::fill(t.begin(), t.end(), scalar_type(0));
954  auto itZ = Z.begin();
955  size_type s = t.sizes()[0];
956 
957  // Performs t(i*Qmult+j, j*target_dim + k) = Z(i,k);
958  for (size_type k = 0; k < target_dim; ++k)
959  for (size_type i = 0; i < ndof; ++i, ++itZ)
960  for (size_type j = 0; j < Qmult; ++j)
961  t[i*Qmult + j + s*(j*target_dim + k)] = *itZ;
962  }
963  }
964  }
965  return 0;
966  }
967 
968  ga_instruction_copy_val_base(base_tensor &tt, const base_tensor &Z_,
969  size_type q) : t(tt), Z(Z_), qdim(q) {}
970  };
971 
972  struct ga_instruction_copy_grad_base : public ga_instruction_copy_val_base {
973  // Z(ndof,target_dim,N) --> t(Qmult*ndof,Qmult*target_dim,N)
974  virtual int exec() {
975  GA_DEBUG_INFO("Instruction: gradient of test functions");
976  if (qdim == 1) {
977  std::copy(Z.begin(), Z.end(), t.begin());
978  } else {
979  size_type target_dim = Z.sizes()[1];
980  size_type Qmult = qdim / target_dim;
981  if (Qmult == 1) {
982  std::copy(Z.begin(), Z.end(), t.begin());
983  } else {
984  if (target_dim == 1) {
985  size_type ndof = Z.sizes()[0];
986  size_type N = Z.sizes()[2];
987  GA_DEBUG_ASSERT(t.size() == Z.size() * Qmult * Qmult,
988  "Wrong size for gradient vector");
989  std::fill(t.begin(), t.end(), scalar_type(0));
990  base_tensor::const_iterator itZ = Z.begin();
991  size_type s = t.sizes()[0], sss = s+1, ssss = s*target_dim*Qmult;
992 
993  // Performs t(i*Qmult+j, k*Qmult + j, l) = Z(i,k,l);
994  for (size_type l = 0; l < N; ++l) {
995  base_tensor::iterator it = t.begin() + (ssss*l);
996  for (size_type i = 0; i < ndof; ++i, ++itZ) {
997  if (i) it += Qmult;
998  base_tensor::iterator it2 = it;
999  *it2 = *itZ;
1000  for (size_type j = 1; j < Qmult; ++j) { it2+=sss; *it2=*itZ; }
1001  }
1002  }
1003  } else {
1004  size_type ndof = Z.sizes()[0];
1005  size_type N = Z.sizes()[2];
1006  GA_DEBUG_ASSERT(t.size() == Z.size() * Qmult * Qmult,
1007  "Wrong size for gradient vector");
1008  std::fill(t.begin(), t.end(), scalar_type(0));
1009  base_tensor::const_iterator itZ = Z.begin();
1010  size_type s = t.sizes()[0], ss = s * qdim;
1011 
1012  // Performs t(i*Qmult+j, j*target_dim + k, l) = Z(i,k,l);
1013  for (size_type l = 0; l < N; ++l)
1014  for (size_type k = 0; k < target_dim; ++k)
1015  for (size_type i = 0; i < ndof; ++i, ++itZ)
1016  for (size_type j = 0; j < Qmult; ++j)
1017  t[i*Qmult + j + s*(j*target_dim + k) + ss*l] = *itZ;
1018  }
1019  }
1020  }
1021  return 0;
1022  }
1023 
1024  ga_instruction_copy_grad_base(base_tensor &tt, const base_tensor &Z_,
1025  size_type q)
1026  : ga_instruction_copy_val_base(tt,Z_,q) {}
1027  };
1028 
1029  struct ga_instruction_copy_vect_val_base : public ga_instruction {
1030  base_tensor &t;
1031  const base_tensor &Z;
1032  size_type qdim;
1033  // Z(ndof) --> t(qdim*ndof,qdim*target_dim)
1034  virtual int exec() {
1035  GA_DEBUG_INFO("Instruction: vectorized value of test functions");
1036 
1037  size_type ndof = Z.sizes()[0];
1038  GA_DEBUG_ASSERT(t.size() == Z.size() * qdim * qdim,
1039  "Wrong size for base vector");
1040  // std::fill(t.begin(), t.end(), scalar_type(0)); // Factorized
1041  auto itZ = Z.begin();
1042  size_type s = t.sizes()[0], sss = s+1;
1043 
1044  // Performs t(i*qdim+j, k*qdim + j) = Z(i,k);
1045  auto it = t.begin();
1046  for (size_type i = 0; i < ndof; ++i, ++itZ) {
1047  if (i) it += qdim;
1048  auto it2 = it;
1049  *it2 = *itZ;
1050  for (size_type j = 1; j < qdim; ++j) { it2 += sss; *it2 = *itZ; }
1051  }
1052  return 0;
1053  }
1054 
1055  ga_instruction_copy_vect_val_base(base_tensor &tt, const base_tensor &Z_,
1056  size_type q) : t(tt), Z(Z_), qdim(q) {}
1057  };
1058 
1059  struct ga_instruction_copy_vect_grad_base
1060  : public ga_instruction_copy_vect_val_base {
1061  // Z(ndof,N) --> t(qdim*ndof,qdim,N)
1062  virtual int exec() {
1063  GA_DEBUG_INFO("Instruction: vectorized gradient of test functions");
1064  size_type ndof = Z.sizes()[0];
1065  size_type N = Z.sizes()[2];
1066  GA_DEBUG_ASSERT(t.size() == Z.size() * qdim * qdim,
1067  "Wrong size for gradient vector");
1068  // std::fill(t.begin(), t.end(), scalar_type(0)); // Factorized
1069  base_tensor::const_iterator itZ = Z.begin();
1070  size_type s = t.sizes()[0], sss = s+1, ssss = s*qdim;
1071 
1072  // Performs t(i*qdim+j, k*qdim + j, l) = Z(i,k,l);
1073  for (size_type l = 0; l < N; ++l) {
1074  base_tensor::iterator it = t.begin() + (ssss*l);
1075  for (size_type i = 0; i < ndof; ++i, ++itZ) {
1076  if (i) it += qdim;
1077  base_tensor::iterator it2 = it;
1078  *it2 = *itZ;
1079  for (size_type j = 1; j < qdim; ++j) { it2+=sss; *it2=*itZ; }
1080  }
1081  }
1082  return 0;
1083  }
1084 
1085  ga_instruction_copy_vect_grad_base(base_tensor &tt, const base_tensor &Z_,
1086  size_type q)
1087  : ga_instruction_copy_vect_val_base(tt,Z_,q) {}
1088  };
1089 
1090  struct ga_instruction_copy_hess_base : public ga_instruction_copy_val_base {
1091  // Z(ndof,target_dim,N*N) --> t(Qmult*ndof,Qmult*target_dim,N,N)
1092  virtual int exec() {
1093  GA_DEBUG_INFO("Instruction: Hessian of test functions");
1094  size_type target_dim = Z.sizes()[1];
1095  size_type Qmult = qdim / target_dim;
1096  if (Qmult == 1) {
1097  gmm::copy(Z.as_vector(), t.as_vector());
1098  } else {
1099  size_type ndof = Z.sizes()[0];
1100  GA_DEBUG_ASSERT(t.size() == Z.size() * Qmult * Qmult,
1101  "Wrong size for Hessian vector");
1102  gmm::clear(t.as_vector());
1103  base_tensor::const_iterator itZ = Z.begin();
1104  size_type s = t.sizes()[0], ss = s * qdim;
1105 
1106  // Performs t(i*Qmult+j, j*target_dim + k, l, m) = Z(i,k,l*N+m)
1107  size_type NN = Z.sizes()[2];
1108  for (size_type lm = 0; lm < NN; ++lm)
1109  for (size_type k = 0; k < target_dim; ++k)
1110  for (size_type i = 0; i < ndof; ++i, ++itZ)
1111  for (size_type j = 0; j < Qmult; ++j)
1112  t[i*Qmult + j + s*(j*target_dim + k) + ss*lm] = *itZ;
1113  }
1114  return 0;
1115  }
1116 
1117  ga_instruction_copy_hess_base(base_tensor &tt, const base_tensor &Z_,
1118  size_type q)
1119  : ga_instruction_copy_val_base(tt, Z_, q) {}
1120  };
1121 
1122  struct ga_instruction_copy_diverg_base : public ga_instruction_copy_val_base {
1123  // Z(ndof,target_dim,N) --> t(Qmult*ndof)
1124  virtual int exec() {
1125  GA_DEBUG_INFO("Instruction: divergence of test functions");
1126  size_type ndof = Z.sizes()[0];
1127  size_type target_dim = Z.sizes()[1];
1128  size_type N = Z.sizes()[2];
1129  size_type Qmult = qdim / target_dim;
1130  GA_DEBUG_ASSERT(Qmult*target_dim == N && (Qmult == 1 || target_dim == 1),
1131  "Dimensions mismatch for divergence operator");
1132  GA_DEBUG_ASSERT(t.size() == ndof * Qmult,
1133  "Wrong size for divergence vector");
1134  gmm::clear(t.as_vector());
1135  base_tensor::const_iterator itZ = Z.begin();
1136  if (Qmult == 1) { // target_dim == N
1137  // Performs t(i) = Trace(Z(i,:,:))
1138  for (size_type l = 0; l < N; ++l) {
1139  base_tensor::iterator it = t.begin();
1140  if (l) itZ += target_dim*ndof+1;
1141  for (size_type i = 0; i < ndof; ++i) {
1142  if (i) { ++it; ++itZ; }
1143  *it += *itZ;
1144  }
1145  }
1146  } else { // Qmult == N
1147  // Performs t(i*Qmult+j) = Z(i,1,j)
1148  for (size_type j = 0; j < N; ++j) {
1149  base_tensor::iterator it = t.begin() + j;
1150  if (j) ++itZ;
1151  for (size_type i = 0; i < ndof; ++i) {
1152  if (i) { it += Qmult; ++itZ; }
1153  *it += *itZ;
1154  }
1155  }
1156  }
1157  return 0;
1158  }
1159 
1160  ga_instruction_copy_diverg_base(base_tensor &tt, const base_tensor &Z_,
1161  size_type q)
1162  : ga_instruction_copy_val_base(tt, Z_, q) {}
1163  };
1164 
1165  struct ga_instruction_elementary_trans {
1166  const base_vector &coeff_in;
1167  base_vector coeff_out;
1168  pelementary_transformation elemtrans;
1169  const mesh_fem &mf1, &mf2;
1170  const fem_interpolation_context &ctx;
1171  base_matrix &M;
1172  size_type &icv;
1173 
1174  void do_transformation(size_type n, size_type m) {
1175  if (icv != ctx.convex_num() || M.size() == 0) {
1176  M.base_resize(m, n);
1177  icv = ctx.convex_num();
1178  elemtrans->give_transformation(mf1, mf2, icv, M);
1179  }
1180  coeff_out.resize(gmm::mat_nrows(M));
1181  gmm::mult(M, coeff_in, coeff_out); // remember: coeff == coeff_out
1182  }
1183 
1184  ga_instruction_elementary_trans
1185  (const base_vector &co, pelementary_transformation e,
1186  const mesh_fem &mf1_, const mesh_fem &mf2_,
1187  const fem_interpolation_context &ctx_, base_matrix &M_,
1188  size_type &icv_)
1189  : coeff_in(co), elemtrans(e), mf1(mf1_), mf2(mf2_), ctx(ctx_),
1190  M(M_), icv(icv_) {}
1191  ~ga_instruction_elementary_trans() {};
1192  };
1193 
1194  struct ga_instruction_elementary_trans_val
1195  : public ga_instruction_val, ga_instruction_elementary_trans {
1196  // Z(ndof,target_dim), coeff_in(Qmult,ndof) --> t(target_dim*Qmult)
1197  virtual int exec() {
1198  GA_DEBUG_INFO("Instruction: variable value with elementary "
1199  "transformation");
1200  size_type ndof = Z.sizes()[0];
1201  size_type Qmult = qdim / Z.sizes()[1];
1202  do_transformation(coeff_in.size(), ndof*Qmult);
1203  return ga_instruction_val::exec();
1204  }
1205 
1206  ga_instruction_elementary_trans_val
1207  (base_tensor &tt, const base_tensor &Z_, const base_vector &co, size_type q,
1208  pelementary_transformation e, const mesh_fem &mf1_, const mesh_fem &mf2_,
1209  fem_interpolation_context &ctx_, base_matrix &M_, size_type &icv_)
1210  : ga_instruction_val(tt, Z_, coeff_out, q),
1211  ga_instruction_elementary_trans(co, e, mf1_, mf2_, ctx_, M_, icv_) {}
1212  };
1213 
1214  struct ga_instruction_elementary_trans_grad
1215  : public ga_instruction_grad, ga_instruction_elementary_trans {
1216  // Z(ndof,target_dim,N), coeff_in(Qmult,ndof) --> t(target_dim*Qmult,N)
1217  virtual int exec() {
1218  GA_DEBUG_INFO("Instruction: gradient with elementary transformation");
1219  size_type ndof = Z.sizes()[0];
1220  size_type Qmult = qdim / Z.sizes()[1];
1221  do_transformation(coeff_in.size(), ndof*Qmult);
1222  return ga_instruction_grad::exec();
1223  }
1224 
1225  ga_instruction_elementary_trans_grad
1226  (base_tensor &tt, const base_tensor &Z_, const base_vector &co, size_type q,
1227  pelementary_transformation e, const mesh_fem &mf1_, const mesh_fem &mf2_,
1228  fem_interpolation_context &ctx_, base_matrix &M_, size_type &icv_)
1229  : ga_instruction_grad(tt, Z_, coeff_out, q),
1230  ga_instruction_elementary_trans(co, e, mf1_, mf2_, ctx_, M_, icv_) {}
1231  };
1232 
1233  struct ga_instruction_elementary_trans_hess
1234  : public ga_instruction_hess, ga_instruction_elementary_trans {
1235  // Z(ndof,target_dim,N,N), coeff_in(Qmult,ndof) --> t(target_dim*Qmult,N,N)
1236  virtual int exec() {
1237  GA_DEBUG_INFO("Instruction: Hessian with elementary transformation");
1238  size_type ndof = Z.sizes()[0];
1239  size_type Qmult = qdim / Z.sizes()[1];
1240  do_transformation(coeff_in.size(), ndof*Qmult);
1241  return ga_instruction_hess::exec();
1242  }
1243 
1244  ga_instruction_elementary_trans_hess
1245  (base_tensor &tt, const base_tensor &Z_, const base_vector &co, size_type q,
1246  pelementary_transformation e, const mesh_fem &mf1_, const mesh_fem &mf2_,
1247  fem_interpolation_context &ctx_, base_matrix &M_, size_type &icv_)
1248  : ga_instruction_hess(tt, Z_, coeff_out, q),
1249  ga_instruction_elementary_trans(co, e, mf1_, mf2_, ctx_, M_, icv_) {}
1250  };
1251 
1252  struct ga_instruction_elementary_trans_diverg
1253  : public ga_instruction_diverg, ga_instruction_elementary_trans {
1254  // Z(ndof,target_dim,N), coeff_in(Qmult,ndof) --> t(1)
1255  virtual int exec() {
1256  GA_DEBUG_INFO("Instruction: divergence with elementary transformation");
1257  size_type ndof = Z.sizes()[0];
1258  size_type Qmult = qdim / Z.sizes()[1];
1259  do_transformation(coeff_in.size(), ndof*Qmult);
1260  return ga_instruction_diverg::exec();
1261  }
1262 
1263  ga_instruction_elementary_trans_diverg
1264  (base_tensor &tt, const base_tensor &Z_, const base_vector &co, size_type q,
1265  pelementary_transformation e, const mesh_fem &mf1_, const mesh_fem &mf2_,
1266  fem_interpolation_context &ctx_, base_matrix &M_, size_type &icv_)
1267  : ga_instruction_diverg(tt, Z_, coeff_out, q),
1268  ga_instruction_elementary_trans(co, e, mf1_, mf2_, ctx_, M_, icv_) {}
1269  };
1270 
1271  struct ga_instruction_update_group_info : public ga_instruction {
1272  const ga_workspace &workspace;
1273  const ga_instruction_set &gis;
1274  const ga_instruction_set::interpolate_info &inin;
1275  const std::string gname;
1276  ga_instruction_set::variable_group_info &vgi;
1277 
1278  virtual int exec() {
1279  GA_DEBUG_INFO("Instruction: Update group info for "+gname);
1280  if (vgi.cached_mesh && vgi.cached_mesh == inin.m)
1281  return 0;
1282 
1283  vgi.cached_mesh = inin.m;
1284  const std::string &varname
1285  = inin.m ? workspace.variable_in_group(gname, *(inin.m))
1286  : workspace.first_variable_of_group(gname);
1287  vgi.varname = &varname;
1288  vgi.mf = workspace.associated_mf(varname);
1289  GA_DEBUG_ASSERT(vgi.mf, "Group variable should always have a mesh_fem");
1290  vgi.reduced_mf = vgi.mf->is_reduced();
1291  if (vgi.reduced_mf) {
1292  const auto it = gis.really_extended_vars.find(varname);
1293  GA_DEBUG_ASSERT(it != gis.really_extended_vars.end(),
1294  "Variable " << varname << " not in extended variables");
1295  vgi.U = &(it->second);
1296  vgi.I = &(workspace.temporary_interval_of_variable(varname));
1297  } else {
1298  vgi.U = &(workspace.value(varname));
1299  vgi.I = &(workspace.interval_of_variable(varname));
1300  }
1301  vgi.alpha = workspace.factor_of_variable(varname);
1302  return 0;
1303  }
1304 
1305  ga_instruction_update_group_info
1306  (const ga_workspace &workspace_, const ga_instruction_set &gis_,
1307  const ga_instruction_set::interpolate_info &inin_,
1308  const std::string &gname_, ga_instruction_set::variable_group_info &vgi_)
1309  : workspace(workspace_), gis(gis_), inin(inin_), gname(gname_), vgi(vgi_)
1310  {}
1311  };
1312 
1313  struct ga_instruction_interpolate_filter : public ga_instruction {
1314  base_tensor &t;
1315  const ga_instruction_set::interpolate_info &inin;
1316  const size_type pt_type;
1317  const int nb;
1318 
1319  virtual int exec() {
1320  GA_DEBUG_INFO("Instruction: interpolated filter");
1321  if ((pt_type == size_type(-1) && inin.pt_type) ||
1322  (pt_type != size_type(-1) && inin.pt_type == pt_type)) {
1323  GA_DEBUG_INFO("Instruction: interpolated filter: pass");
1324  return 0;
1325  }
1326  else {
1327  GA_DEBUG_INFO("Instruction: interpolated filter: filtered");
1328  gmm::clear(t.as_vector());
1329  return nb;
1330  }
1331  return 0;
1332  }
1333 
1334  ga_instruction_interpolate_filter
1335  (base_tensor &t_, const ga_instruction_set::interpolate_info &inin_,
1336  size_type ind_, int nb_)
1337  : t(t_), inin(inin_), pt_type(ind_), nb(nb_) {}
1338  };
1339 
1340  struct ga_instruction_copy_interpolated_small_vect : public ga_instruction {
1341  base_tensor &t;
1342  const base_small_vector &vec;
1343  const ga_instruction_set::interpolate_info &inin;
1344 
1345  virtual int exec() {
1346  GA_DEBUG_INFO("Instruction: copy small vector");
1347  GMM_ASSERT1(!(inin.has_ctx) || inin.ctx.is_convex_num_valid(),
1348  "Invalid element, probably transformation failed");
1349  GMM_ASSERT1(t.size() == vec.size(),
1350  "Invalid vector size: " << t.size() << "!=" << vec.size());
1351  gmm::copy(vec, t.as_vector());
1352  return 0;
1353  }
1354  ga_instruction_copy_interpolated_small_vect
1355  (base_tensor &t_, const base_small_vector &vec_,
1356  const ga_instruction_set::interpolate_info &inin_)
1357  : t(t_), vec(vec_), inin(inin_) {}
1358  };
1359 
1360  struct ga_instruction_interpolate : public ga_instruction {
1361  base_tensor &t;
1362  const mesh **m;
1363  const mesh_fem *mfn, **mfg;
1364  const base_vector *Un, **Ug;
1365  fem_interpolation_context &ctx;
1366  base_vector coeff;
1367  size_type qdim;
1368  const size_type &ipt;
1369  fem_precomp_pool &fp_pool;
1370  ga_instruction_set::interpolate_info &inin;
1371 
1372  virtual int exec() {
1373  GMM_ASSERT1(ctx.is_convex_num_valid(), "No valid element for the "
1374  "transformation. Probably transformation failed");
1375  const mesh_fem &mf = *(mfg ? *mfg : mfn);
1376  const base_vector &U = *(Ug ? *Ug : Un);
1377  GMM_ASSERT1(&(mf.linked_mesh()) == *m, "Interpolation of a variable "
1378  "on another mesh than the one it is defined on");
1379  slice_vector_on_basic_dof_of_element(mf, U, ctx.convex_num(), coeff);
1380  pfem pf = mf.fem_of_element(ctx.convex_num());
1381  GMM_ASSERT1(pf, "Undefined finite element method");
1382  if (ctx.have_pgp()) {
1383  if (ipt == 0)
1384  inin.pfps[&mf] = fp_pool(pf, ctx.pgp()->get_ppoint_tab());
1385  ctx.set_pfp(inin.pfps[&mf]);
1386  } else {
1387  ctx.set_pf(pf);
1388  }
1389  return 0;
1390  }
1391 
1392  ga_instruction_interpolate
1393  (base_tensor &tt, const mesh **m_, const mesh_fem *mfn_,
1394  const mesh_fem **mfg_, const base_vector *Un_, const base_vector **Ug_,
1395  fem_interpolation_context &ctx_, size_type q, const size_type &ipt_,
1396  fem_precomp_pool &fp_pool_, ga_instruction_set::interpolate_info &inin_)
1397  : t(tt), m(m_), mfn(mfn_), mfg(mfg_), Un(Un_), Ug(Ug_),
1398  ctx(ctx_), qdim(q), ipt(ipt_), fp_pool(fp_pool_), inin(inin_) {}
1399  };
1400 
1401  struct ga_instruction_interpolate_val : public ga_instruction_interpolate {
1402  // --> t(target_dim*Qmult)
1403  virtual int exec() {
1404  GA_DEBUG_INFO("Instruction: interpolated variable value");
1405  ga_instruction_interpolate::exec();
1406  ctx.pf()->interpolation(ctx, coeff, t.as_vector(), dim_type(qdim));
1407  // cout << "interpolate " << &U << " result : " << t.as_vector() << endl;
1408  return 0;
1409  }
1410 
1411  ga_instruction_interpolate_val
1412  (base_tensor &tt, const mesh **m_, const mesh_fem *mfn_,
1413  const mesh_fem **mfg_, const base_vector *Un_, const base_vector **Ug_,
1414  fem_interpolation_context &ctx_, size_type q, size_type &ipt_,
1415  fem_precomp_pool &fp_pool_, ga_instruction_set::interpolate_info &inin_)
1416  : ga_instruction_interpolate(tt, m_, mfn_, mfg_, Un_, Ug_,ctx_, q, ipt_,
1417  fp_pool_, inin_)
1418  {}
1419  };
1420 
1421  struct ga_instruction_interpolate_grad : public ga_instruction_interpolate {
1422  // --> t(target_dim*Qmult,N)
1423  virtual int exec() {
1424  GA_DEBUG_INFO("Instruction: interpolated variable grad");
1425  ga_instruction_interpolate::exec();
1426  base_matrix v(qdim, ctx.N());
1427  ctx.pf()->interpolation_grad(ctx, coeff, v, dim_type(qdim));
1428  gmm::copy(v.as_vector(), t.as_vector());
1429  return 0;
1430  }
1431 
1432  ga_instruction_interpolate_grad
1433  (base_tensor &tt, const mesh **m_, const mesh_fem *mfn_,
1434  const mesh_fem **mfg_, const base_vector *Un_, const base_vector **Ug_,
1435  fem_interpolation_context &ctx_, size_type q, size_type &ipt_,
1436  fem_precomp_pool &fp_pool_, ga_instruction_set::interpolate_info &inin_)
1437  : ga_instruction_interpolate(tt, m_, mfn_, mfg_, Un_, Ug_, ctx_, q, ipt_,
1438  fp_pool_, inin_)
1439  {}
1440  };
1441 
1442  struct ga_instruction_interpolate_hess : public ga_instruction_interpolate {
1443  // --> t(target_dim*Qmult,N,N)
1444  virtual int exec() {
1445  GA_DEBUG_INFO("Instruction: interpolated variable hessian");
1446  ga_instruction_interpolate::exec();
1447  base_matrix v(qdim, ctx.N()*ctx.N()); // To be optimized
1448  ctx.pf()->interpolation_hess(ctx, coeff, v, dim_type(qdim));
1449  gmm::copy(v.as_vector(), t.as_vector());
1450  return 0;
1451  }
1452 
1453  ga_instruction_interpolate_hess
1454  (base_tensor &tt, const mesh **m_, const mesh_fem *mfn_,
1455  const mesh_fem **mfg_, const base_vector *Un_, const base_vector **Ug_,
1456  fem_interpolation_context &ctx_, size_type q, size_type &ipt_,
1457  fem_precomp_pool &fp_pool_, ga_instruction_set::interpolate_info &inin_)
1458  : ga_instruction_interpolate(tt, m_, mfn_, mfg_, Un_, Ug_, ctx_, q, ipt_,
1459  fp_pool_, inin_)
1460  {}
1461  };
1462 
1463  struct ga_instruction_interpolate_diverg : public ga_instruction_interpolate {
1464  // --> t(1)
1465  virtual int exec() {
1466  GA_DEBUG_INFO("Instruction: interpolated variable divergence");
1467  ga_instruction_interpolate::exec();
1468  ctx.pf()->interpolation_diverg(ctx, coeff, t[0]);
1469  return 0;
1470  }
1471 
1472  ga_instruction_interpolate_diverg
1473  (base_tensor &tt, const mesh **m_, const mesh_fem *mfn_,
1474  const mesh_fem **mfg_, const base_vector *Un_, const base_vector **Ug_,
1475  fem_interpolation_context &ctx_, size_type q, size_type &ipt_,
1476  fem_precomp_pool &fp_pool_, ga_instruction_set::interpolate_info &inin_)
1477  : ga_instruction_interpolate(tt, m_, mfn_, mfg_, Un_, Ug_, ctx_, q, ipt_,
1478  fp_pool_, inin_)
1479  {}
1480  };
1481 
1482  struct ga_instruction_interpolate_base {
1483  base_tensor ZZ;
1484  const mesh **m;
1485  const mesh_fem *mfn, **mfg;
1486  const size_type &ipt;
1487  ga_instruction_set::interpolate_info &inin;
1488  fem_precomp_pool &fp_pool;
1489 
1490  virtual int exec() {
1491  GMM_ASSERT1(inin.ctx.is_convex_num_valid(), "No valid element for "
1492  "the transformation. Probably transformation failed");
1493  const mesh_fem &mf = *(mfg ? *mfg : mfn);
1494  GMM_ASSERT1(&(mf.linked_mesh()) == *m, "Interpolation of a variable "
1495  "on another mesh than the one it is defined on");
1496 
1497  pfem pf = mf.fem_of_element(inin.ctx.convex_num());
1498  GMM_ASSERT1(pf, "Undefined finite element method");
1499 
1500  if (inin.ctx.have_pgp()) {
1501  if (ipt == 0)
1502  inin.pfps[&mf] = fp_pool(pf, inin.ctx.pgp()->get_ppoint_tab());
1503  inin.ctx.set_pfp(inin.pfps[&mf]);
1504  } else {
1505  inin.ctx.set_pf(pf);
1506  }
1507  return 0;
1508  }
1509 
1510  ga_instruction_interpolate_base
1511  (const mesh **m_, const mesh_fem *mfn_, const mesh_fem **mfg_,
1512  const size_type &ipt_, ga_instruction_set::interpolate_info &inin_,
1513  fem_precomp_pool &fp_pool_)
1514  : m(m_), mfn(mfn_), mfg(mfg_), ipt(ipt_), inin(inin_),
1515  fp_pool(fp_pool_) {}
1516  };
1517 
1518  struct ga_instruction_interpolate_val_base
1519  : public ga_instruction_copy_val_base, ga_instruction_interpolate_base {
1520  // ctx --> Z(ndof,target_dim) --> t(Qmult*ndof,Qmult*target_dim)
1521  virtual int exec() {
1522  GA_DEBUG_INFO("Instruction: interpolated base value");
1523  ga_instruction_interpolate_base::exec();
1524  inin.ctx.pf()->real_base_value(inin.ctx, ZZ); // remember Z == ZZ
1525  return ga_instruction_copy_val_base::exec();
1526  }
1527 
1528  ga_instruction_interpolate_val_base
1529  (base_tensor &t_, const mesh **m_, const mesh_fem *mfn_,
1530  const mesh_fem **mfg_, const size_type &ipt_, size_type q,
1531  ga_instruction_set::interpolate_info &inin_, fem_precomp_pool &fp_pool_)
1532  : ga_instruction_copy_val_base(t_, ZZ, q),
1533  ga_instruction_interpolate_base(m_, mfn_, mfg_, ipt_,
1534  inin_, fp_pool_) {}
1535  };
1536 
1537  struct ga_instruction_interpolate_grad_base
1538  : public ga_instruction_copy_grad_base, ga_instruction_interpolate_base {
1539  // ctx --> Z(ndof,target_dim,N) --> t(Qmult*ndof,Qmult*target_dim,N)
1540  virtual int exec() {
1541  GA_DEBUG_INFO("Instruction: interpolated base grad");
1542  ga_instruction_interpolate_base::exec();
1543  inin.ctx.pf()->real_grad_base_value(inin.ctx, ZZ); // remember Z == ZZ
1544  return ga_instruction_copy_grad_base::exec();
1545  }
1546 
1547  ga_instruction_interpolate_grad_base
1548  (base_tensor &t_, const mesh **m_, const mesh_fem *mfn_,
1549  const mesh_fem **mfg_, const size_type &ipt_, size_type q,
1550  ga_instruction_set::interpolate_info &inin_, fem_precomp_pool &fp_pool_)
1551  : ga_instruction_copy_grad_base(t_, ZZ, q),
1552  ga_instruction_interpolate_base(m_, mfn_, mfg_, ipt_,
1553  inin_, fp_pool_) {}
1554  };
1555 
1556  struct ga_instruction_interpolate_hess_base
1557  : public ga_instruction_copy_hess_base, ga_instruction_interpolate_base {
1558  // ctx --> Z(ndof,target_dim,N*N) --> t(Qmult*ndof,Qmult*target_dim,N,N)
1559  virtual int exec() {
1560  GA_DEBUG_INFO("Instruction: interpolated base hessian");
1561  ga_instruction_interpolate_base::exec();
1562  inin.ctx.pf()->real_hess_base_value(inin.ctx, ZZ); // remember Z == ZZ
1563  return ga_instruction_copy_hess_base::exec();
1564  }
1565 
1566  ga_instruction_interpolate_hess_base
1567  (base_tensor &t_, const mesh **m_, const mesh_fem *mfn_,
1568  const mesh_fem **mfg_, const size_type &ipt_, size_type q,
1569  ga_instruction_set::interpolate_info &inin_, fem_precomp_pool &fp_pool_)
1570  : ga_instruction_copy_hess_base(t_, ZZ, q),
1571  ga_instruction_interpolate_base(m_, mfn_, mfg_, ipt_,
1572  inin_, fp_pool_) {}
1573  };
1574 
1575  struct ga_instruction_interpolate_diverg_base
1576  : public ga_instruction_copy_diverg_base, ga_instruction_interpolate_base {
1577  // ctx --> Z(ndof,target_dim,N*N) --> t(Qmult*ndof)
1578  virtual int exec() {
1579  GA_DEBUG_INFO("Instruction: interpolated base divergence");
1580  ga_instruction_interpolate_base::exec();
1581  inin.ctx.pf()->real_grad_base_value(inin.ctx, ZZ); // remember Z == ZZ
1582  return ga_instruction_copy_diverg_base::exec();
1583  }
1584 
1585  ga_instruction_interpolate_diverg_base
1586  (base_tensor &t_, const mesh **m_, const mesh_fem *mfn_,
1587  const mesh_fem **mfg_, const size_type &ipt_, size_type q,
1588  ga_instruction_set::interpolate_info &inin_, fem_precomp_pool &fp_pool_)
1589  : ga_instruction_copy_diverg_base(t_, ZZ, q),
1590  ga_instruction_interpolate_base(m_, mfn_, mfg_, ipt_,
1591  inin_, fp_pool_) {}
1592  };
1593 
1594 
1595  struct ga_instruction_elementary_trans_base {
1596  base_tensor t_in;
1597  base_tensor &t_out;
1598  pelementary_transformation elemtrans;
1599  const mesh_fem &mf1, &mf2;
1600  const fem_interpolation_context &ctx;
1601  base_matrix &M;
1602  size_type &icv;
1603 
1604  void do_transformation(size_type n, size_type m) {
1605  if (icv != ctx.convex_num() || M.size() == 0) {
1606  M.base_resize(m, n);
1607  icv = ctx.convex_num();
1608  elemtrans->give_transformation(mf1, mf2, icv, M);
1609  }
1610  t_out.mat_reduction(t_in, M, 0);
1611  }
1612 
1613  ga_instruction_elementary_trans_base
1614  (base_tensor &t_, pelementary_transformation e, const mesh_fem &mf1_,
1615  const mesh_fem &mf2_,
1616  const fem_interpolation_context &ctx_, base_matrix &M_, size_type &icv_)
1617  : t_out(t_), elemtrans(e), mf1(mf1_), mf2(mf2_), ctx(ctx_),
1618  M(M_), icv(icv_) {}
1619  };
1620 
1621  struct ga_instruction_elementary_trans_val_base
1622  : public ga_instruction_copy_val_base,
1623  ga_instruction_elementary_trans_base {
1624  // Z(ndof,target_dim) --> t_in --> t_out(Qmult*ndof,Qmult*target_dim)
1625  virtual int exec() {
1626  GA_DEBUG_INFO("Instruction: value of test functions with elementary "
1627  "transformation");
1628  size_type ndof = Z.sizes()[0];
1629  size_type Qmult = qdim / Z.sizes()[1];
1630  t_in.adjust_sizes(Qmult*ndof, Qmult*Z.sizes()[1]);
1631  ga_instruction_copy_val_base::exec();
1632  do_transformation(t_out.sizes()[0], ndof*Qmult);
1633  return 0;
1634  }
1635 
1636  ga_instruction_elementary_trans_val_base
1637  (base_tensor &t_, const base_tensor &Z_, size_type q,
1638  pelementary_transformation e, const mesh_fem &mf1_, const mesh_fem &mf2_,
1639  fem_interpolation_context &ctx_, base_matrix &M_, size_type &icv_)
1640  : ga_instruction_copy_val_base(t_in, Z_, q),
1641  ga_instruction_elementary_trans_base(t_, e, mf1_, mf2_, ctx_,
1642  M_, icv_) {}
1643  };
1644 
1645  struct ga_instruction_elementary_trans_grad_base
1646  : public ga_instruction_copy_grad_base,
1647  ga_instruction_elementary_trans_base {
1648  // Z(ndof,target_dim,N) --> t_in --> t_out(Qmult*ndof,Qmult*target_dim,N)
1649  virtual int exec() {
1650  GA_DEBUG_INFO("Instruction: gradient of test functions with elementary "
1651  "transformation");
1652  size_type ndof = Z.sizes()[0];
1653  size_type Qmult = qdim / Z.sizes()[1];
1654  t_in.adjust_sizes(Qmult*ndof, Qmult*Z.sizes()[1], Z.sizes()[2]);
1655  ga_instruction_copy_grad_base::exec();
1656  do_transformation(t_out.sizes()[0], ndof*Qmult);
1657  return 0;
1658  }
1659 
1660  ga_instruction_elementary_trans_grad_base
1661  (base_tensor &t_, const base_tensor &Z_, size_type q,
1662  pelementary_transformation e, const mesh_fem &mf1_, const mesh_fem &mf2_,
1663  fem_interpolation_context &ctx_, base_matrix &M_, size_type &icv_)
1664  : ga_instruction_copy_grad_base(t_in, Z_, q),
1665  ga_instruction_elementary_trans_base(t_, e, mf1_, mf2_, ctx_,
1666  M_, icv_) {}
1667  };
1668 
1669  struct ga_instruction_elementary_trans_hess_base
1670  : public ga_instruction_copy_hess_base,
1671  ga_instruction_elementary_trans_base {
1672  // Z(ndof,target_dim,N*N) --> t_out(Qmult*ndof,Qmult*target_dim,N,N)
1673  virtual int exec() {
1674  GA_DEBUG_INFO("Instruction: Hessian of test functions with elementary "
1675  "transformation");
1676  size_type ndof = Z.sizes()[0];
1677  size_type Qmult = qdim / Z.sizes()[1];
1678  t_in.adjust_sizes(Qmult*ndof, Qmult*Z.sizes()[1], Z.sizes()[2]);
1679  ga_instruction_copy_hess_base::exec();
1680  do_transformation(t_out.sizes()[0], ndof*Qmult);
1681  return 0;
1682  }
1683 
1684  ga_instruction_elementary_trans_hess_base
1685  (base_tensor &t_, const base_tensor &Z_, size_type q,
1686  pelementary_transformation e, const mesh_fem &mf1_, const mesh_fem &mf2_,
1687  fem_interpolation_context &ctx_, base_matrix &M_, size_type &icv_)
1688  : ga_instruction_copy_hess_base(t_in, Z_, q),
1689  ga_instruction_elementary_trans_base(t_, e, mf1_, mf2_, ctx_,
1690  M_, icv_) {}
1691  };
1692 
1693  struct ga_instruction_elementary_trans_diverg_base
1694  : public ga_instruction_copy_diverg_base,
1695  ga_instruction_elementary_trans_base {
1696  // Z(ndof,target_dim,N) --> t_out(Qmult*ndof)
1697  virtual int exec() {
1698  GA_DEBUG_INFO("Instruction: divergence of test functions with elementary "
1699  "transformation");
1700  size_type ndof = Z.sizes()[0];
1701  size_type Qmult = qdim / Z.sizes()[1];
1702  t_in.adjust_sizes(Qmult*ndof);
1703  ga_instruction_copy_diverg_base::exec();
1704  do_transformation(t_out.sizes()[0], ndof*Qmult);
1705  return 0;
1706  }
1707 
1708  ga_instruction_elementary_trans_diverg_base
1709  (base_tensor &t_, const base_tensor &Z_, size_type q,
1710  pelementary_transformation e, const mesh_fem &mf1_, const mesh_fem &mf2_,
1711  fem_interpolation_context &ctx_, base_matrix &M_, size_type &icv_)
1712  : ga_instruction_copy_diverg_base(t_in, Z_, q),
1713  ga_instruction_elementary_trans_base(t_, e, mf1_, mf2_, ctx_,
1714  M_, icv_) {}
1715  };
1716 
1717 
1718  struct ga_instruction_add : public ga_instruction {
1719  base_tensor &t;
1720  const base_tensor &tc1, &tc2;
1721  virtual int exec() {
1722  GA_DEBUG_INFO("Instruction: addition");
1723  GA_DEBUG_ASSERT(t.size() == tc1.size(),
1724  "internal error " << t.size() << " != " << tc1.size());
1725  GA_DEBUG_ASSERT(t.size() == tc2.size(),
1726  "internal error " << t.size() << " != " << tc2.size());
1727  gmm::add(tc1.as_vector(), tc2.as_vector(), t.as_vector());
1728  return 0;
1729  }
1730  ga_instruction_add(base_tensor &t_,
1731  const base_tensor &tc1_, const base_tensor &tc2_)
1732  : t(t_), tc1(tc1_), tc2(tc2_) {}
1733  };
1734 
1735  struct ga_instruction_add_to : public ga_instruction {
1736  base_tensor &t;
1737  const base_tensor &tc1;
1738  virtual int exec() {
1739  GA_DEBUG_INFO("Instruction: addition");
1740  GA_DEBUG_ASSERT(t.size() == tc1.size(), "internal error " << t.size()
1741  << " incompatible with " << tc1.size());
1742  gmm::add(tc1.as_vector(), t.as_vector());
1743  return 0;
1744  }
1745  ga_instruction_add_to(base_tensor &t_, const base_tensor &tc1_)
1746  : t(t_), tc1(tc1_) {}
1747  };
1748 
1749  struct ga_instruction_add_to_coeff : public ga_instruction {
1750  base_tensor &t;
1751  const base_tensor &tc1;
1752  scalar_type &coeff;
1753  virtual int exec() {
1754  GA_DEBUG_INFO("Instruction: addition with scale");
1755  GA_DEBUG_ASSERT(t.size() == tc1.size(), "internal error " << t.size()
1756  << " incompatible with " << tc1.size());
1757  gmm::add(gmm::scaled(tc1.as_vector(), coeff), t.as_vector());
1758  return 0;
1759  }
1760  ga_instruction_add_to_coeff(base_tensor &t_, const base_tensor &tc1_,
1761  scalar_type &coeff_)
1762  : t(t_), tc1(tc1_), coeff(coeff_) {}
1763  };
1764 
1765  struct ga_instruction_sub : public ga_instruction {
1766  base_tensor &t;
1767  const base_tensor &tc1, &tc2;
1768  virtual int exec() {
1769  GA_DEBUG_INFO("Instruction: subtraction");
1770  GA_DEBUG_ASSERT(t.size() == tc1.size() && t.size() == tc2.size(),
1771  "internal error");
1772  gmm::add(tc1.as_vector(), gmm::scaled(tc2.as_vector(), scalar_type(-1)),
1773  t.as_vector());
1774  return 0;
1775  }
1776  ga_instruction_sub(base_tensor &t_,
1777  const base_tensor &tc1_, const base_tensor &tc2_)
1778  : t(t_), tc1(tc1_), tc2(tc2_) {}
1779  };
1780 
1781  struct ga_instruction_opposite : public ga_instruction {
1782  base_tensor &t;
1783  virtual int exec() {
1784  GA_DEBUG_INFO("Instruction: multiplication with -1");
1785  gmm::scale(t.as_vector(), scalar_type(-1));
1786  return 0;
1787  }
1788  ga_instruction_opposite(base_tensor &t_) : t(t_) {}
1789  };
1790 
1791  struct ga_instruction_print_tensor : public ga_instruction {
1792  base_tensor &t;
1793  pga_tree_node pnode;
1794  const fem_interpolation_context &ctx;
1795  size_type &nbpt, &ipt;
1796  virtual int exec() {
1797  GA_DEBUG_INFO("Instruction: tensor print");
1798  cout << "Print term "; ga_print_node(pnode, cout);
1799  cout << " on Gauss point " << ipt << "/" << nbpt << " of element "
1800  << ctx.convex_num() << ": " << t << endl;
1801  return 0;
1802  }
1803  ga_instruction_print_tensor(base_tensor &t_, pga_tree_node pnode_,
1804  const fem_interpolation_context &ctx_,
1805  size_type &nbpt_, size_type &ipt_)
1806  : t(t_), pnode(pnode_), ctx(ctx_), nbpt(nbpt_), ipt(ipt_) {}
1807  };
1808 
1809  struct ga_instruction_copy_tensor : public ga_instruction {
1810  base_tensor &t;
1811  const base_tensor &tc1;
1812  virtual int exec() {
1813  GA_DEBUG_INFO("Instruction: tensor copy");
1814  std::copy(tc1.begin(), tc1.end(), t.begin());
1815  // gmm::copy(tc1.as_vector(), t.as_vector());
1816  return 0;
1817  }
1818  ga_instruction_copy_tensor(base_tensor &t_, const base_tensor &tc1_)
1819  : t(t_), tc1(tc1_) {}
1820  };
1821 
1822  struct ga_instruction_clear_tensor : public ga_instruction {
1823  base_tensor &t;
1824  virtual int exec() {
1825  GA_DEBUG_INFO("Instruction: clear tensor");
1826  std::fill(t.begin(), t.end(), scalar_type(0));
1827  return 0;
1828  }
1829  ga_instruction_clear_tensor(base_tensor &t_) : t(t_) {}
1830  };
1831 
1832  struct ga_instruction_copy_tensor_possibly_void : public ga_instruction {
1833  base_tensor &t;
1834  const base_tensor &tc1;
1835  virtual int exec() {
1836  GA_DEBUG_INFO("Instruction: tensor copy possibly void");
1837  if (tc1.size())
1838  gmm::copy(tc1.as_vector(), t.as_vector());
1839  else
1840  gmm::clear(t.as_vector());
1841  return 0;
1842  }
1843  ga_instruction_copy_tensor_possibly_void(base_tensor &t_,
1844  const base_tensor &tc1_)
1845  : t(t_), tc1(tc1_) {}
1846  };
1847 
1848  struct ga_instruction_copy_scalar : public ga_instruction {
1849  scalar_type &t; const scalar_type &t1;
1850  virtual int exec() {
1851  GA_DEBUG_INFO("Instruction: scalar copy");
1852  t = t1;
1853  return 0;
1854  }
1855  ga_instruction_copy_scalar(scalar_type &t_, const scalar_type &t1_)
1856  : t(t_), t1(t1_) {}
1857  };
1858 
1859  struct ga_instruction_copy_vect : public ga_instruction {
1860  base_vector &t;
1861  const base_vector &t1;
1862  virtual int exec() {
1863  GA_DEBUG_INFO("Instruction: fixed size tensor copy");
1864  gmm::copy(t1, t);
1865  return 0;
1866  }
1867  ga_instruction_copy_vect(base_vector &t_, const base_vector &t1_)
1868  : t(t_), t1(t1_) {}
1869  };
1870 
1871  struct ga_instruction_trace : public ga_instruction {
1872  base_tensor &t;
1873  const base_tensor &tc1;
1874  size_type n;
1875  // tc1(:,:,...,n,n) --> t(:,:,...)
1876  virtual int exec() {
1877  GA_DEBUG_INFO("Instruction: Trace");
1878  GA_DEBUG_ASSERT(t.size()*n*n == tc1.size(), "Wrong sizes");
1879  size_type s = t.size() * (n+1);
1880  auto it = t.begin();
1881  auto it1 = tc1.begin();
1882  for (; it != t.end(); ++it, ++it1) {
1883  auto it2 = it1;
1884  *it = *it2;
1885  for (size_type i = 1; i < n; ++i) { it2 += s; *it += *it2; }
1886  }
1887  return 0;
1888  }
1889 
1890  ga_instruction_trace(base_tensor &t_, const base_tensor &tc1_, size_type n_)
1891  : t(t_), tc1(tc1_), n(n_) {}
1892  };
1893 
1894  struct ga_instruction_deviator : public ga_instruction {
1895  base_tensor &t;
1896  const base_tensor &tc1;
1897  size_type n;
1898  // tc1(:,:,...,n,n) --> t(:,:,...,n,n)
1899  virtual int exec() {
1900  GA_DEBUG_INFO("Instruction: Deviator");
1901  GA_DEBUG_ASSERT(t.size() == tc1.size(), "Wrong sizes");
1902 
1903  gmm::copy(tc1.as_vector(), t.as_vector());
1904 
1905  size_type nb = t.size()/(n*n);
1906  size_type s = nb * (n+1), j = 0;
1907  base_tensor::iterator it = t.begin();
1908  base_tensor::const_iterator it1 = tc1.begin();
1909  for (; j < nb; ++it, ++it1, ++j) {
1910  scalar_type tr(0);
1911  base_tensor::const_iterator it2 = it1;
1912  tr += *it2;
1913  for (size_type i = 1; i < n; ++i) { it2 += s; tr += *it2; }
1914  tr /= scalar_type(n);
1915 
1916  base_tensor::iterator it3 = it;
1917  *it3 -= tr;
1918  for (size_type i = 1; i < n; ++i) { it3 += s; *it3 -= tr; }
1919  }
1920  return 0;
1921  }
1922 
1923  ga_instruction_deviator(base_tensor &t_, const base_tensor &tc1_,
1924  size_type n_)
1925  : t(t_), tc1(tc1_), n(n_) {}
1926  };
1927 
1928  struct ga_instruction_transpose : public ga_instruction { // To be optimized
1929  base_tensor &t;
1930  const base_tensor &tc1;
1931  size_type J, K, I;
1932  virtual int exec() {
1933  GA_DEBUG_INFO("Instruction: transpose");
1934  GA_DEBUG_ASSERT(t.size() == tc1.size(), "Wrong sizes");
1935 
1936  size_type L = tc1.size() / (J*K*I);
1937  auto it = t.begin();
1938  for (size_type i = 0; i < I; ++i) {
1939  size_type s1 = i*J*K*L;
1940  for (size_type j = 0; j < J; ++j) {
1941  size_type s2 = s1 + j*L;
1942  for (size_type k = 0; k < K; ++k) {
1943  size_type s3 = s2 + k*J*L;
1944  for (size_type l = 0; l < L; ++l, ++it)
1945  *it = tc1[s3+l];
1946  }
1947  }
1948  }
1949  GA_DEBUG_ASSERT(it == t.end(), "Wrong sizes");
1950  return 0;
1951  }
1952  ga_instruction_transpose(base_tensor &t_, const base_tensor &tc1_,
1953  size_type J_, size_type K_, size_type I_)
1954  : t(t_), tc1(tc1_), J(J_), K(K_), I(I_) {}
1955  };
1956 
1957  struct ga_instruction_swap_indices : public ga_instruction {// To be optimized
1958  base_tensor &t;
1959  const base_tensor &tc1;
1960  size_type nn1, nn2, ii2, ii3;
1961  virtual int exec() {
1962  GA_DEBUG_INFO("Instruction: swap indices");
1963  GA_DEBUG_ASSERT(t.size() == tc1.size(), "Wrong sizes");
1964  size_type ii1 = t.size() / (nn1*nn2*ii2*ii3);
1965 
1966  auto it = t.begin();
1967  for (size_type i = 0; i < ii3; ++i)
1968  for (size_type j = 0; j < nn1; ++j)
1969  for (size_type k = 0; k < ii2; ++k)
1970  for (size_type l = 0; l < nn2; ++l) {
1971  size_type ind = j*ii1+k*ii1*nn1+l*ii1*nn1*ii2+i*ii1*nn1*ii2*nn2;
1972  for (size_type m = 0; m < ii1; ++m, ++it)
1973  *it = tc1[m+ind];
1974  }
1975  GA_DEBUG_ASSERT(it == t.end(), "Wrong sizes");
1976  return 0;
1977  }
1978  ga_instruction_swap_indices(base_tensor &t_, const base_tensor &tc1_,
1979  size_type n1_, size_type n2_,
1980  size_type i2_, size_type i3_)
1981  : t(t_), tc1(tc1_), nn1(n1_), nn2(n2_), ii2(i2_), ii3(i3_) {}
1982  };
1983 
1984  struct ga_instruction_index_move_last : public ga_instruction {// To be optimized
1985  base_tensor &t;
1986  const base_tensor &tc1;
1987  size_type nn, ii2;
1988  virtual int exec() {
1989  GA_DEBUG_INFO("Instruction: swap indices");
1990  GA_DEBUG_ASSERT(t.size() == tc1.size(), "Wrong sizes");
1991  size_type ii1 = t.size() / (nn*ii2);
1992 
1993  auto it = t.begin();
1994  for (size_type i = 0; i < nn; ++i)
1995  for (size_type j = 0; j < ii2; ++j) {
1996  size_type ind = i*ii1+j*ii1*nn;
1997  for (size_type k = 0; k < ii1; ++k, ++it)
1998  *it = tc1[k+ind];
1999  }
2000  GA_DEBUG_ASSERT(it == t.end(), "Wrong sizes");
2001  return 0;
2002  }
2003  ga_instruction_index_move_last(base_tensor &t_, const base_tensor &tc1_,
2004  size_type n_, size_type i2_)
2005  : t(t_), tc1(tc1_), nn(n_), ii2(i2_) {}
2006  };
2007 
2008  struct ga_instruction_transpose_no_test : public ga_instruction {
2009  base_tensor &t;
2010  const base_tensor &tc1;
2011  size_type n1, n2, nn;
2012  virtual int exec() {
2013  GA_DEBUG_INFO("Instruction: transpose");
2014  GA_DEBUG_ASSERT(t.size() == tc1.size(), "Wrong sizes");
2015 
2016  auto it = t.begin();
2017  for (size_type i = 0; i < nn; ++i) {
2018  size_type s1 = i*n1*n2;
2019  for (size_type j = 0; j < n1; ++j) {
2020  size_type s2 = s1 + j;
2021  for (size_type k = 0; k < n2; ++k, ++it)
2022  *it = tc1[s2 + k*n1];
2023  }
2024  }
2025  GA_DEBUG_ASSERT(it == t.end(), "Wrong sizes");
2026  return 0;
2027  }
2028  ga_instruction_transpose_no_test(base_tensor &t_, const base_tensor &tc1_,
2029  size_type n1_, size_type n2_,
2030  size_type nn_)
2031  : t(t_), tc1(tc1_), n1(n1_), n2(n2_), nn(nn_) {}
2032  };
2033 
2034  struct ga_instruction_transpose_test : public ga_instruction {
2035  base_tensor &t;
2036  const base_tensor &tc1;
2037  virtual int exec() {
2038  GA_DEBUG_INFO("Instruction: copy tensor and transpose test functions");
2039  GA_DEBUG_ASSERT(t.size() == tc1.size(), "Wrong sizes");
2040  GA_DEBUG_ASSERT(t.sizes().size() >= 2, "Wrong sizes");
2041 
2042  size_type s1 = t.sizes()[0], s2 = t.sizes()[1], s3 = s1*s2;
2043  size_type s = t.size() / s3;
2044  base_tensor::iterator it = t.begin();
2045  for (size_type k = 0; k < s; ++k)
2046  for (size_type j = 0; j < s2; ++j)
2047  for (size_type i = 0; i < s1; ++i, ++it)
2048  *it = tc1[j+s2*i+k*s3];
2049  return 0;
2050  }
2051  ga_instruction_transpose_test(base_tensor &t_, const base_tensor &tc1_)
2052  : t(t_), tc1(tc1_) {}
2053  };
2054 
2055  struct ga_instruction_sym : public ga_instruction {
2056  base_tensor &t;
2057  const base_tensor &tc1;
2058  virtual int exec() {
2059  GA_DEBUG_INFO("Instruction: symmetric part");
2060  GA_DEBUG_ASSERT(t.size() == tc1.size(), "Wrong sizes");
2061  size_type order = t.sizes().size();
2062  size_type s1 = t.sizes()[order-2], s2 = t.sizes()[order-1];
2063  size_type s = t.size() / (s1*s2);
2064  for (size_type i = 0; i < s1; ++i)
2065  for (size_type j = 0; j < s2; ++j) {
2066  base_tensor::iterator it = t.begin() + s*(i + s1*j);
2067  base_tensor::const_iterator it1 = tc1.begin() + s*(i + s1*j),
2068  it1T = tc1.begin() + s*(j + s2*i);
2069  for (size_type k = 0; k < s; ++k) *it++ = 0.5*(*it1++ + *it1T++);
2070  }
2071  return 0;
2072  }
2073  ga_instruction_sym(base_tensor &t_, const base_tensor &tc1_)
2074  : t(t_), tc1(tc1_) {}
2075  };
2076 
2077  struct ga_instruction_skew : public ga_instruction {
2078  base_tensor &t;
2079  const base_tensor &tc1;
2080  virtual int exec() {
2081  GA_DEBUG_INFO("Instruction: skew-symmetric part");
2082  GA_DEBUG_ASSERT(t.size() == tc1.size(), "Wrong sizes");
2083  size_type order = t.sizes().size();
2084  size_type s1 = t.sizes()[order-2], s2 = t.sizes()[order-1];
2085  size_type s = t.size() / (s1*s2);
2086  for (size_type i = 0; i < s1; ++i)
2087  for (size_type j = 0; j < s2; ++j) {
2088  base_tensor::iterator it = t.begin() + s*(i + s1*j);
2089  base_tensor::const_iterator it1 = tc1.begin() + s*(i + s1*j),
2090  it1T = tc1.begin() + s*(j + s2*i);
2091  for (size_type k = 0; k < s; ++k) *it++ = 0.5*(*it1++ - *it1T++);
2092  }
2093  return 0;
2094  }
2095  ga_instruction_skew(base_tensor &t_, const base_tensor &tc1_)
2096  : t(t_), tc1(tc1_) {}
2097  };
2098 
2099  struct ga_instruction_scalar_add : public ga_instruction {
2100  scalar_type &t;
2101  const scalar_type &c, &d;
2102  virtual int exec() {
2103  GA_DEBUG_INFO("Instruction: scalar addition");
2104  t = c + d;
2105  return 0;
2106  }
2107  ga_instruction_scalar_add(scalar_type &t_, const scalar_type &c_,
2108  const scalar_type &d_)
2109  : t(t_), c(c_), d(d_) {}
2110  };
2111 
2112  struct ga_instruction_scalar_sub : public ga_instruction {
2113  scalar_type &t;
2114  const scalar_type &c, &d;
2115  virtual int exec() {
2116  GA_DEBUG_INFO("Instruction: scalar subtraction");
2117  t = c - d;
2118  return 0;
2119  }
2120  ga_instruction_scalar_sub(scalar_type &t_, const scalar_type &c_,
2121  const scalar_type &d_)
2122  : t(t_), c(c_), d(d_) {}
2123  };
2124 
2125  struct ga_instruction_scalar_scalar_mult : public ga_instruction {
2126  scalar_type &t;
2127  const scalar_type &c, &d;
2128  virtual int exec() {
2129  GA_DEBUG_INFO("Instruction: scalar multiplication");
2130  t = c * d;
2131  return 0;
2132  }
2133  ga_instruction_scalar_scalar_mult(scalar_type &t_, const scalar_type &c_,
2134  const scalar_type &d_)
2135  : t(t_), c(c_), d(d_) {}
2136  };
2137 
2138  struct ga_instruction_scalar_scalar_div : public ga_instruction {
2139  scalar_type &t;
2140  const scalar_type &c, &d;
2141  virtual int exec() {
2142  GA_DEBUG_INFO("Instruction: scalar division");
2143  t = c / d;
2144  return 0;
2145  }
2146  ga_instruction_scalar_scalar_div(scalar_type &t_, const scalar_type &c_,
2147  const scalar_type &d_)
2148  : t(t_), c(c_), d(d_) {}
2149  };
2150 
2151  template<int I> inline void dax__(base_tensor::iterator &it,
2152  base_tensor::const_iterator &itx,
2153  const scalar_type &a) {
2154  constexpr int I1 = I/8;
2155  constexpr int I2 = I - I1*8;
2156  for (int i=0; i < I1; ++i)
2157  dax__<8>(it, itx , a);
2158  dax__<I2>(it, itx , a);
2159  }
2160  template<> inline void dax__<8>(base_tensor::iterator &it,
2161  base_tensor::const_iterator &itx,
2162  const scalar_type &a) {
2163  *it++ = *itx++ * a;
2164  *it++ = *itx++ * a;
2165  *it++ = *itx++ * a;
2166  *it++ = *itx++ * a;
2167  *it++ = *itx++ * a;
2168  *it++ = *itx++ * a;
2169  *it++ = *itx++ * a;
2170  *it++ = *itx++ * a;
2171  }
2172  template<> inline void dax__<7>(base_tensor::iterator &it,
2173  base_tensor::const_iterator &itx,
2174  const scalar_type &a) {
2175  *it++ = *itx++ * a;
2176  *it++ = *itx++ * a;
2177  *it++ = *itx++ * a;
2178  *it++ = *itx++ * a;
2179  *it++ = *itx++ * a;
2180  *it++ = *itx++ * a;
2181  *it++ = *itx++ * a;
2182  }
2183  template<> inline void dax__<6>(base_tensor::iterator &it,
2184  base_tensor::const_iterator &itx,
2185  const scalar_type &a) {
2186  *it++ = *itx++ * a;
2187  *it++ = *itx++ * a;
2188  *it++ = *itx++ * a;
2189  *it++ = *itx++ * a;
2190  *it++ = *itx++ * a;
2191  *it++ = *itx++ * a;
2192  }
2193  template<> inline void dax__<5>(base_tensor::iterator &it,
2194  base_tensor::const_iterator &itx,
2195  const scalar_type &a) {
2196  *it++ = *itx++ * a;
2197  *it++ = *itx++ * a;
2198  *it++ = *itx++ * a;
2199  *it++ = *itx++ * a;
2200  *it++ = *itx++ * a;
2201  }
2202  template<> inline void dax__<4>(base_tensor::iterator &it,
2203  base_tensor::const_iterator &itx,
2204  const scalar_type &a) {
2205  *it++ = *itx++ * a;
2206  *it++ = *itx++ * a;
2207  *it++ = *itx++ * a;
2208  *it++ = *itx++ * a;
2209  }
2210  template<> inline void dax__<3>(base_tensor::iterator &it,
2211  base_tensor::const_iterator &itx,
2212  const scalar_type &a) {
2213  *it++ = *itx++ * a;
2214  *it++ = *itx++ * a;
2215  *it++ = *itx++ * a;
2216  }
2217  template<> inline void dax__<2>(base_tensor::iterator &it,
2218  base_tensor::const_iterator &itx,
2219  const scalar_type &a) {
2220  *it++ = *itx++ * a;
2221  *it++ = *itx++ * a;
2222  }
2223  template<> inline void dax__<1>(base_tensor::iterator &it,
2224  base_tensor::const_iterator &itx,
2225  const scalar_type &a) {
2226  *it++ = *itx++ * a;
2227  }
2228  template<> inline void dax__<0>(base_tensor::iterator &,
2229  base_tensor::const_iterator &,
2230  const scalar_type &) {}
2231 
2232 
2233  template<int I> inline
2234  void reduc_elem_unrolled__(base_tensor::iterator &it,
2235  base_tensor::const_iterator &it1, base_tensor::const_iterator &it2,
2236  const size_type s1, const size_type s2) {
2237  *it = it1[0] * it2[0];
2238  for (int i=1; i < I; ++i)
2239  *it += it1[i*s1] * it2[i*s2];
2240  }
2241  template<> inline
2242  void reduc_elem_unrolled__<9>(base_tensor::iterator &it,
2243  base_tensor::const_iterator &it1, base_tensor::const_iterator &it2,
2244  const size_type s1, const size_type s2) {
2245  *it = it1[0] * it2[0] // (*it1) * (*it2)
2246  + it1[s1] * it2[s2] // (*(it1+s1)) * (*(it2+s2))
2247  + it1[2*s1] * it2[2*s2] // (*(it1+2*s1)) * (*(it2+2*s2))
2248  + it1[3*s1] * it2[3*s2] // (*(it1+3*s1)) * (*(it2+3*s2))
2249  + it1[4*s1] * it2[4*s2] // (*(it1+4*s1)) * (*(it2+4*s2))
2250  + it1[5*s1] * it2[5*s2] // (*(it1+5*s1)) * (*(it2+5*s2))
2251  + it1[6*s1] * it2[6*s2] // (*(it1+6*s1)) * (*(it2+6*s2))
2252  + it1[7*s1] * it2[7*s2] // (*(it1+7*s1)) * (*(it2+7*s2))
2253  + it1[8*s1] * it2[8*s2]; // (*(it1+8*s1)) * (*(it2+8*s2));
2254  }
2255  template<> inline
2256  void reduc_elem_unrolled__<8>(base_tensor::iterator &it,
2257  base_tensor::const_iterator &it1, base_tensor::const_iterator &it2,
2258  const size_type s1, const size_type s2) {
2259  *it = it1[0] * it2[0]
2260  + it1[s1] * it2[s2]
2261  + it1[2*s1] * it2[2*s2]
2262  + it1[3*s1] * it2[3*s2]
2263  + it1[4*s1] * it2[4*s2]
2264  + it1[5*s1] * it2[5*s2]
2265  + it1[6*s1] * it2[6*s2]
2266  + it1[7*s1] * it2[7*s2];
2267  }
2268  template<> inline
2269  void reduc_elem_unrolled__<7>(base_tensor::iterator &it,
2270  base_tensor::const_iterator &it1, base_tensor::const_iterator &it2,
2271  const size_type s1, const size_type s2) {
2272  *it = it1[0] * it2[0]
2273  + it1[s1] * it2[s2]
2274  + it1[2*s1] * it2[2*s2]
2275  + it1[3*s1] * it2[3*s2]
2276  + it1[4*s1] * it2[4*s2]
2277  + it1[5*s1] * it2[5*s2]
2278  + it1[6*s1] * it2[6*s2];
2279  }
2280  template<> inline
2281  void reduc_elem_unrolled__<6>(base_tensor::iterator &it,
2282  base_tensor::const_iterator &it1, base_tensor::const_iterator &it2,
2283  const size_type s1, const size_type s2) {
2284  *it = it1[0] * it2[0]
2285  + it1[s1] * it2[s2]
2286  + it1[2*s1] * it2[2*s2]
2287  + it1[3*s1] * it2[3*s2]
2288  + it1[4*s1] * it2[4*s2]
2289  + it1[5*s1] * it2[5*s2];
2290  }
2291  template<> inline
2292  void reduc_elem_unrolled__<5>(base_tensor::iterator &it,
2293  base_tensor::const_iterator &it1, base_tensor::const_iterator &it2,
2294  const size_type s1, const size_type s2) {
2295  *it = it1[0] * it2[0]
2296  + it1[s1] * it2[s2]
2297  + it1[2*s1] * it2[2*s2]
2298  + it1[3*s1] * it2[3*s2]
2299  + it1[4*s1] * it2[4*s2];
2300  }
2301  template<> inline
2302  void reduc_elem_unrolled__<4>(base_tensor::iterator &it,
2303  base_tensor::const_iterator &it1, base_tensor::const_iterator &it2,
2304  const size_type s1, const size_type s2) {
2305  *it = it1[0] * it2[0]
2306  + it1[s1] * it2[s2]
2307  + it1[2*s1] * it2[2*s2]
2308  + it1[3*s1] * it2[3*s2];
2309  }
2310  template<> inline
2311  void reduc_elem_unrolled__<3>(base_tensor::iterator &it,
2312  base_tensor::const_iterator &it1, base_tensor::const_iterator &it2,
2313  const size_type s1, const size_type s2) {
2314  *it = it1[0] * it2[0]
2315  + it1[s1] * it2[s2]
2316  + it1[2*s1] * it2[2*s2];
2317  }
2318  template<> inline
2319  void reduc_elem_unrolled__<2>(base_tensor::iterator &it,
2320  base_tensor::const_iterator &it1, base_tensor::const_iterator &it2,
2321  const size_type s1, const size_type s2) {
2322  *it = it1[0] * it2[0]
2323  + it1[s1] * it2[s2];
2324  }
2325  template<> inline
2326  void reduc_elem_unrolled__<1>(base_tensor::iterator &it,
2327  base_tensor::const_iterator &it1, base_tensor::const_iterator &it2,
2328  const size_type /*s1*/, const size_type /*s2*/)
2329  { *it = it1[0] * it2[0]; }
2330 
2331 
2332  struct ga_instruction_scalar_mult : public ga_instruction {
2333  base_tensor &t;
2334  const base_tensor &tc1;
2335  const scalar_type &c;
2336  virtual int exec() {
2337  GA_DEBUG_INFO("Instruction: multiplication of a tensor by a scalar " << c);
2338  gmm::copy(gmm::scaled(tc1.as_vector(), c), t.as_vector());
2339  return 0;
2340  }
2341  ga_instruction_scalar_mult(base_tensor &t_,
2342  const base_tensor &tc1_, const scalar_type &c_)
2343  : t(t_), tc1(tc1_), c(c_) {}
2344  };
2345 
2346  struct ga_instruction_scalar_div : public ga_instruction {
2347  base_tensor &t;
2348  const base_tensor &tc1;
2349  const scalar_type &c;
2350  virtual int exec() {
2351  GA_DEBUG_INFO("Instruction: division of a tensor by a scalar");
2352  GA_DEBUG_ASSERT(t.size() == tc1.size(), "Wrong sizes");
2353  base_tensor::iterator it = t.begin();
2354  base_tensor::const_iterator it1 = tc1.cbegin();
2355  for (; it != t.end(); ++it, ++it1) *it = *it1/c;
2356  return 0;
2357  }
2358  ga_instruction_scalar_div(base_tensor &t_,
2359  const base_tensor &tc1_, const scalar_type &c_)
2360  : t(t_), tc1(tc1_), c(c_) {}
2361  };
2362 
2363  // Performs Cross product in the presence of test functions
2364  struct ga_instruction_cross_product_tf : public ga_instruction {
2365  base_tensor &t;
2366  const base_tensor &tc1, &tc2;
2367  bool inv;
2368  virtual int exec() {
2369  GA_DEBUG_INFO("Instruction: Cross product with test functions");
2370 
2371  size_type n1 = tc1.size() / 3, n2 = tc2.size() / 3, nn=n1*n2;
2372  GA_DEBUG_ASSERT(t.size() == nn*3, "Bad tensor size for cross product");
2373  size_type mm=2*nn, n1_2 = 2*n1, n2_2 = 2*n2;
2374  base_tensor::iterator it = t.begin();
2375  base_tensor::const_iterator it2 = tc2.cbegin();
2376  if (inv) {
2377  for (size_type i = 0; i < n2; ++i, ++it2) {
2378  base_tensor::const_iterator it1 = tc1.cbegin();
2379  for (size_type j = 0; j < n1; ++j, ++it, ++it1) {
2380  *it = - it1[n1] *it2[n2_2] + it1[n1_2]*it2[n2];
2381  it[nn] = - it1[n1_2]*it2[0] + it1[0] *it2[n2_2];
2382  it[mm] = - it1[0] *it2[n2] + it1[n1] *it2[0];
2383  }
2384  }
2385  } else {
2386  for (size_type i = 0; i < n2; ++i, ++it2) {
2387  base_tensor::const_iterator it1 = tc1.cbegin();
2388  for (size_type j = 0; j < n1; ++j, ++it, ++it1) {
2389  *it = it1[n1] *it2[n2_2] - it1[n1_2]*it2[n2];
2390  it[nn] = it1[n1_2]*it2[0] - it1[0] *it2[n2_2];
2391  it[mm] = it1[0] *it2[n2] - it1[n1] *it2[0];
2392  }
2393  }
2394  }
2395  return 0;
2396  }
2397  ga_instruction_cross_product_tf(base_tensor &t_,
2398  const base_tensor &tc1_,
2399  const base_tensor &tc2_, bool inv_)
2400  : t(t_), tc1(tc1_), tc2(tc2_), inv(inv_) {}
2401  };
2402 
2403  // Performs Cross product in the absence of test functions
2404  struct ga_instruction_cross_product : public ga_instruction {
2405  base_tensor &t;
2406  const base_tensor &tc1, &tc2;
2407  virtual int exec() {
2408  GA_DEBUG_INFO("Instruction: Cross product with test functions");
2409  GA_DEBUG_ASSERT(t.size() == 3 && tc1.size() == 3 && tc2.size() == 3,
2410  "Bad tensor size for cross product");
2411  t[0] = tc1[1]*tc2[2] - tc1[2]*tc2[1];
2412  t[1] = tc1[2]*tc2[0] - tc1[0]*tc2[2];
2413  t[2] = tc1[0]*tc2[1] - tc1[1]*tc2[0];
2414  return 0;
2415  }
2416  ga_instruction_cross_product(base_tensor &t_,
2417  const base_tensor &tc1_, const base_tensor &tc2_)
2418  : t(t_), tc1(tc1_), tc2(tc2_) {}
2419  };
2420 
2421 
2422 
2423 
2424  struct ga_instruction_dotmult : public ga_instruction {
2425  base_tensor &t;
2426  const base_tensor &tc1, &tc2;
2427  virtual int exec() {
2428  GA_DEBUG_INFO("Instruction: componentwise multiplication");
2429  size_type s2 = tc2.size(), s1_1 = tc1.size() / s2;
2430  GA_DEBUG_ASSERT(t.size() == s1_1*s2, "Wrong sizes");
2431 
2432  base_tensor::iterator it = t.begin();
2433  for (size_type i = 0; i < s2; ++i)
2434  for (size_type m = 0; m < s1_1; ++m, ++it)
2435  *it = tc1[m+s1_1*i] * tc2[i];
2436  return 0;
2437  }
2438  ga_instruction_dotmult(base_tensor &t_,
2439  const base_tensor &tc1_, const base_tensor &tc2_)
2440  : t(t_), tc1(tc1_), tc2(tc2_) {}
2441  };
2442 
2443  struct ga_instruction_dotdiv : public ga_instruction {
2444  base_tensor &t;
2445  const base_tensor &tc1, &tc2;
2446  virtual int exec() {
2447  GA_DEBUG_INFO("Instruction: componentwise division");
2448  size_type s2 = tc2.size(), s1_1 = tc1.size() / s2;
2449  GA_DEBUG_ASSERT(t.size() == s1_1*s2, "Wrong sizes");
2450 
2451  base_tensor::iterator it = t.begin();
2452  for (size_type i = 0; i < s2; ++i)
2453  for (size_type m = 0; m < s1_1; ++m, ++it)
2454  *it = tc1[m+s1_1*i] / tc2[i];
2455  return 0;
2456  }
2457  ga_instruction_dotdiv(base_tensor &t_,
2458  const base_tensor &tc1_, const base_tensor &tc2_)
2459  : t(t_), tc1(tc1_), tc2(tc2_) {}
2460  };
2461 
2462  // Performs Ami Bni -> Cmni
2463  struct ga_instruction_dotmult_spec : public ga_instruction {
2464  base_tensor &t;
2465  const base_tensor &tc1, &tc2;
2466  virtual int exec() {
2467  GA_DEBUG_INFO("Instruction: specific componentwise multiplication");
2468  size_type s2_1 = tc2.sizes()[0], s2_2 = tc2.size() / s2_1;
2469  size_type s1_1 = tc1.size() / s2_2;
2470 
2471  base_tensor::iterator it = t.begin();
2472  for (size_type i = 0; i < s2_2; ++i)
2473  for (size_type n = 0; n < s2_1; ++n)
2474  for (size_type m = 0; m < s1_1; ++m, ++it)
2475  *it = tc1[m+s1_1*i] * tc2[n+s2_1*i];
2476  GA_DEBUG_ASSERT(it == t.end(), "Wrong sizes");
2477  return 0;
2478  }
2479  ga_instruction_dotmult_spec(base_tensor &t_,
2480  const base_tensor &tc1_, const base_tensor &tc2_)
2481  : t(t_), tc1(tc1_), tc2(tc2_) {}
2482  };
2483 
2484  // Performs Amijik -> Cmjk. To be optimized
2485  struct ga_instruction_contract_1_1 : public ga_instruction {
2486  base_tensor &t;
2487  const base_tensor &tc1;
2488  size_type nn, ii2, ii3;
2489  virtual int exec() {
2490  GA_DEBUG_INFO("Instruction: single contraction on a single tensor");
2491 
2492  size_type ii1 = tc1.size() / (nn*nn*ii2*ii3);
2493 
2494  base_tensor::iterator it = t.begin();
2495  for (size_type i = 0; i < ii3; ++i)
2496  for (size_type j = 0; j < ii2; ++j)
2497  for (size_type k = 0; k < ii1; ++k, ++it) {
2498  *it = scalar_type(0);
2499  size_type pre_ind = k+j*ii1*nn+i*ii1*nn*ii2*nn;
2500  for (size_type n = 0; n < nn; ++n)
2501  *it += tc1[pre_ind+n*ii1+n*ii1*nn*ii2];
2502  }
2503 
2504  GA_DEBUG_ASSERT(it == t.end(), "Wrong sizes");
2505  return 0;
2506  }
2507  ga_instruction_contract_1_1(base_tensor &t_, const base_tensor &tc1_,
2508  size_type n_, size_type i2_, size_type i3_)
2509  : t(t_), tc1(tc1_), nn(n_), ii2(i2_), ii3(i3_) {}
2510  };
2511 
2512  // Performs Amijk Bnljp -> Cmniklp. To be optimized
2513  struct ga_instruction_contract_2_1 : public ga_instruction {
2514  base_tensor &t;
2515  const base_tensor &tc1, &tc2;
2516  size_type nn, ii1, ii2, ii3, ii4;
2517  virtual int exec() {
2518  GA_DEBUG_INFO("Instruction: single contraction on two tensors");
2519 
2520  size_type ift1 = tc1.size() / (nn*ii1*ii2);
2521  size_type ift2 = tc2.size() / (nn*ii3*ii4);
2522 
2523  base_tensor::iterator it = t.begin();
2524  for (size_type i = 0; i < ii4; ++i)
2525  for (size_type j = 0; j < ii3; ++j)
2526  for (size_type k = 0; k < ii2; ++k)
2527  for (size_type l = 0; l < ii1; ++l)
2528  for (size_type p = 0; p < ift2; ++p)
2529  for (size_type q = 0; q < ift1; ++q, ++it) {
2530  *it = scalar_type(0);
2531  size_type ind1 = q+l*ift1+k*ift1*ii1*nn;
2532  size_type ind2 = p+j*ift2+i*ift2*ii3*nn;
2533  for (size_type n = 0; n < nn; ++n)
2534  *it += tc1[ind1+n*ift1*ii1] * tc2[ind2+n*ift2*ii3];
2535  }
2536 
2537  GA_DEBUG_ASSERT(it == t.end(), "Wrong sizes");
2538  return 0;
2539  }
2540  ga_instruction_contract_2_1(base_tensor &t_,
2541  const base_tensor &tc1_, const base_tensor &tc2_,
2542  size_type n_, size_type i1_, size_type i2_,
2543  size_type i3_, size_type i4_)
2544  : t(t_), tc1(tc1_), tc2(tc2_), nn(n_),
2545  ii1(i1_), ii2(i2_), ii3(i3_), ii4(i4_) {}
2546  };
2547 
2548  // Performs Amijk Bnljp -> Cnmiklp. To be optimized
2549  struct ga_instruction_contract_2_1_rev : public ga_instruction {
2550  base_tensor &t;
2551  const base_tensor &tc1, &tc2;
2552  size_type nn, ii1, ii2, ii3, ii4;
2553  virtual int exec() {
2554  GA_DEBUG_INFO("Instruction: single contraction on two tensors");
2555 
2556  size_type ift1 = tc1.size() / (nn*ii1*ii2);
2557  size_type ift2 = tc2.size() / (nn*ii3*ii4);
2558 
2559  base_tensor::iterator it = t.begin();
2560  for (size_type i = 0; i < ii4; ++i)
2561  for (size_type j = 0; j < ii3; ++j)
2562  for (size_type k = 0; k < ii2; ++k)
2563  for (size_type l = 0; l < ii1; ++l)
2564  for (size_type q = 0; q < ift1; ++q)
2565  for (size_type p = 0; p < ift2; ++p, ++it) {
2566  *it = scalar_type(0);
2567  size_type ind1 = q+l*ift1+k*ift1*ii1*nn;
2568  size_type ind2 = p+j*ift2+i*ift2*ii3*nn;
2569  for (size_type n = 0; n < nn; ++n)
2570  *it += tc1[ind1+n*ift1*ii1] * tc2[ind2+n*ift2*ii3];
2571  }
2572 
2573  GA_DEBUG_ASSERT(it == t.end(), "Wrong sizes");
2574  return 0;
2575  }
2576  ga_instruction_contract_2_1_rev(base_tensor &t_,
2577  const base_tensor &tc1_, const base_tensor &tc2_,
2578  size_type n_, size_type i1_, size_type i2_,
2579  size_type i3_, size_type i4_)
2580  : t(t_), tc1(tc1_), tc2(tc2_), nn(n_),
2581  ii1(i1_), ii2(i2_), ii3(i3_), ii4(i4_) {}
2582  };
2583 
2584  // Performs Amijklp Bnqjrls -> Cmnikpqrs. To be optimized
2585  struct ga_instruction_contract_2_2 : public ga_instruction {
2586  base_tensor &t;
2587  const base_tensor &tc1, &tc2;
2588  size_type nn1, nn2, ii1, ii2, ii3, ii4, ii5, ii6;
2589  bool inv_tc2;
2590  virtual int exec() {
2591  GA_DEBUG_INFO("Instruction: single contraction on two tensors");
2592 
2593  size_type ift1 = tc1.size() / (nn1*nn2*ii1*ii2*ii3);
2594  size_type ift2 = tc2.size() / (nn1*nn2*ii3*ii4*ii5);
2595 
2596  size_type sn1 = ift2*ii4, sn2 = ift2*ii4*nn1*ii5;
2597  if (inv_tc2) std::swap(sn1, sn2);
2598 
2599  base_tensor::iterator it = t.begin();
2600  for (size_type i = 0; i < ii6; ++i)
2601  for (size_type j = 0; j < ii5; ++j)
2602  for (size_type k = 0; k < ii4; ++k)
2603  for (size_type l = 0; l < ii3; ++l)
2604  for (size_type p = 0; p < ii2; ++p)
2605  for (size_type q = 0; q < ii1; ++q)
2606  for (size_type r = 0; r < ift2; ++r)
2607  for (size_type s = 0; s < ift1; ++s, ++it) {
2608  *it = scalar_type(0);
2609  size_type ind1
2610  = s+q*ift1+p*ift1*ii1*nn1+l*ift1*ii1*nn1*ii2*nn2;
2611  size_type ind2
2612  = r+k*ift2+j*ift2*ii4*nn1+i*ift2*ii4*nn1*ii5*nn2;
2613  for (size_type n1 = 0; n1 < nn1; ++n1)
2614  for (size_type n2 = 0; n2 < nn2; ++n2)
2615  *it += tc1[ind1+n1*ift1*ii1+n2*ift1*ii1*nn1*ii2]
2616  * tc2[ind2+n1*sn1+n2*sn2];
2617  }
2618 
2619  GA_DEBUG_ASSERT(it == t.end(), "Wrong sizes");
2620  return 0;
2621  }
2622  ga_instruction_contract_2_2(base_tensor &t_,
2623  const base_tensor &tc1_, const base_tensor &tc2_,
2624  size_type n1_, size_type n2_,
2625  size_type i1_, size_type i2_, size_type i3_,
2626  size_type i4_, size_type i5_, size_type i6_,
2627  bool intc2)
2628  : t(t_), tc1(tc1_), tc2(tc2_), nn1(n1_), nn2(n2_),
2629  ii1(i1_), ii2(i2_), ii3(i3_), ii4(i4_), ii5(i5_), ii6(i6_),
2630  inv_tc2(intc2) {}
2631  };
2632 
2633  // Performs Amijklp Bnqjrls -> Cnmikpqrs. To be optimized
2634  struct ga_instruction_contract_2_2_rev : public ga_instruction {
2635  base_tensor &t;
2636  const base_tensor &tc1, &tc2;
2637  size_type nn1, nn2, ii1, ii2, ii3, ii4, ii5, ii6;
2638  bool inv_tc2;
2639  virtual int exec() {
2640  GA_DEBUG_INFO("Instruction: single contraction on two tensors");
2641 
2642  size_type ift1 = tc1.size() / (nn1*nn2*ii1*ii2*ii3);
2643  size_type ift2 = tc2.size() / (nn1*nn2*ii3*ii4*ii5);
2644 
2645  size_type sn1 = ift2*ii4, sn2 = ift2*ii4*nn1*ii5;
2646  if (inv_tc2) std::swap(sn1, sn2);
2647 
2648  base_tensor::iterator it = t.begin();
2649  for (size_type i = 0; i < ii6; ++i)
2650  for (size_type j = 0; j < ii5; ++j)
2651  for (size_type k = 0; k < ii4; ++k)
2652  for (size_type l = 0; l < ii3; ++l)
2653  for (size_type p = 0; p < ii2; ++p)
2654  for (size_type q = 0; q < ii1; ++q)
2655  for (size_type s = 0; s < ift1; ++s)
2656  for (size_type r = 0; r < ift2; ++r, ++it) {
2657  *it = scalar_type(0);
2658  size_type ind1
2659  = s+q*ift1+p*ift1*ii1*nn1+l*ift1*ii1*nn1*ii2*nn2;
2660  size_type ind2
2661  = r+k*ift2+j*ift2*ii4*nn1+i*ift2*ii4*nn1*ii5*nn2;
2662  for (size_type n1 = 0; n1 < nn1; ++n1)
2663  for (size_type n2 = 0; n2 < nn2; ++n2)
2664  *it += tc1[ind1+n1*ift1*ii1+n2*ift1*ii1*nn1*ii2]
2665  * tc2[ind2+n1*sn1+n2*sn2];
2666  }
2667 
2668  GA_DEBUG_ASSERT(it == t.end(), "Wrong sizes");
2669  return 0;
2670  }
2671  ga_instruction_contract_2_2_rev(base_tensor &t_,
2672  const base_tensor &tc1_, const base_tensor &tc2_,
2673  size_type n1_, size_type n2_,
2674  size_type i1_, size_type i2_, size_type i3_,
2675  size_type i4_, size_type i5_, size_type i6_,
2676  bool intc2)
2677  : t(t_), tc1(tc1_), tc2(tc2_), nn1(n1_), nn2(n2_),
2678  ii1(i1_), ii2(i2_), ii3(i3_), ii4(i4_), ii5(i5_), ii6(i6_),
2679  inv_tc2(intc2) {}
2680  };
2681 
2682 
2683  // Performs Amj Bjk -> Cmk. To be optimized
2684  struct ga_instruction_matrix_mult : public ga_instruction {
2685  base_tensor &t;
2686  const base_tensor &tc1, &tc2;
2687  const size_type J;
2688  virtual int exec() {
2689  GA_DEBUG_INFO("Instruction: order one contraction "
2690  "(dot product or matrix multiplication)");
2691  size_type M = tc1.size() / J,
2692  K = tc2.size() / J;
2693 #if defined(GA_USES_BLAS)
2694  if (M*J*K > 27) {
2695  const BLAS_INT M_=BLAS_INT(M), J_=BLAS_INT(J), K_=BLAS_INT(K);
2696  constexpr char notrans = 'N';
2697  constexpr scalar_type one(1), zero(0);
2698  gmm::dgemm_(&notrans, &notrans, &M_, &K_, &J_, &one,
2699  &(tc1[0]), &M_, &(tc2[0]), &J_, &zero, &(t[0]), &M_);
2700  } else
2701 #endif
2702  {
2703  auto it = t.begin();
2704  if (M==2 && J==2 && K == 2) {
2705  *it++ = tc1[0]*tc2[0] + tc1[2]*tc2[1]; // k=0,m=0
2706  *it++ = tc1[1]*tc2[0] + tc1[3]*tc2[1]; // k=0,m=1
2707  *it++ = tc1[0]*tc2[2] + tc1[2]*tc2[3]; // k=1,m=0
2708  *it++ = tc1[1]*tc2[2] + tc1[3]*tc2[3]; // k=1,m=1
2709  } else if (M==3 && J==3 && K == 3) {
2710  *it++ = tc1[0]*tc2[0] + tc1[3]*tc2[1] + tc1[6]*tc2[2]; // k=0,m=0
2711  *it++ = tc1[1]*tc2[0] + tc1[4]*tc2[1] + tc1[7]*tc2[2]; // k=0,m=1
2712  *it++ = tc1[2]*tc2[0] + tc1[5]*tc2[1] + tc1[8]*tc2[2]; // k=0,m=2
2713  *it++ = tc1[0]*tc2[3] + tc1[3]*tc2[4] + tc1[6]*tc2[5]; // k=1,m=0
2714  *it++ = tc1[1]*tc2[3] + tc1[4]*tc2[4] + tc1[7]*tc2[5]; // k=1,m=1
2715  *it++ = tc1[2]*tc2[3] + tc1[5]*tc2[4] + tc1[8]*tc2[5]; // k=1,m=2
2716  *it++ = tc1[0]*tc2[6] + tc1[3]*tc2[7] + tc1[6]*tc2[8]; // k=2,m=0
2717  *it++ = tc1[1]*tc2[6] + tc1[4]*tc2[7] + tc1[7]*tc2[8]; // k=2,m=1
2718  *it++ = tc1[2]*tc2[6] + tc1[5]*tc2[7] + tc1[8]*tc2[8]; // k=2,m=2
2719  } else {
2720  for (size_type k = 0; k < K; ++k)
2721  for (size_type m = 0; m < M; ++m, ++it) {
2722  *it = scalar_type(0);
2723  for (size_type j = 0; j < J; ++j)
2724  *it += tc1[m+M*j] * tc2[j+J*k];
2725  }
2726  }
2727  GA_DEBUG_ASSERT(it == t.end(), "Wrong sizes");
2728  }
2729  return 0;
2730  }
2731  ga_instruction_matrix_mult(base_tensor &t_,
2732  const base_tensor &tc1_,
2733  const base_tensor &tc2_, size_type J_)
2734  : t(t_), tc1(tc1_), tc2(tc2_), J(J_) {}
2735  };
2736 
2737  // Performs Amij Bnjk -> Cmnik. To be optimized
2738  struct ga_instruction_matrix_mult_spec : public ga_instruction {
2739  base_tensor &t;
2740  const base_tensor &tc1, &tc2;
2741  size_type J, I, K; // tc1 of size M*I*J, tc2 of size N*J*K
2742  // t of size M*N*I*K
2743  virtual int exec() {
2744  GA_DEBUG_INFO("Instruction: specific order one contraction "
2745  "(dot product or matrix multiplication)");
2746  const size_type MI = tc1.size() / J, M = MI / I,
2747  NJ = tc2.size() / K, N = NJ / J;
2748 #if defined(GA_USES_BLAS)
2749  const BLAS_INT J_ = BLAS_INT(J), M_ = BLAS_INT(M), N_ = BLAS_INT(N),
2750  MI_ = BLAS_INT(MI);
2751  constexpr char notrans = 'N', trans = 'T';
2752  constexpr scalar_type one(1), zero(0);
2753  size_type MN = M*N;
2754  auto it = t.begin();
2755  for (size_type k = 0; k < K; ++k)
2756  for (size_type i = 0; i < I; ++i, it += MN) // => t[M*N*(i+I*k)]
2757  gmm::dgemm_(&notrans, &trans, &M_, &N_, &J_, &one,
2758  &(tc1[M*i]), &MI_, &(tc2[NJ*k]), &N_, &zero,
2759  &(*it), &M_);
2760  GA_DEBUG_ASSERT(it == t.end(), "Wrong sizes");
2761 #else
2762  auto it = t.begin();
2763  for (size_type k = 0; k < K; ++k)
2764  for (size_type i = 0; i < I; ++i)
2765  for (size_type n = 0; n < N; ++n)
2766  for (size_type m = 0; m < M; ++m, ++it) {
2767  *it = scalar_type(0);
2768  for (size_type j = 0; j < J; ++j)
2769  *it += tc1[m+M*i+MI*j] * tc2[n+N*j+NJ*k];
2770  }
2771  GA_DEBUG_ASSERT(it == t.end(), "Wrong sizes");
2772 #endif
2773  return 0;
2774  }
2775  ga_instruction_matrix_mult_spec(base_tensor &t_,
2776  const base_tensor &tc1_,
2777  const base_tensor &tc2_,
2778  size_type J_, size_type I_, size_type K_)
2779  : t(t_), tc1(tc1_), tc2(tc2_), J(J_), I(I_), K(K_) {}
2780  };
2781 
2782  // Performs Amij Bnjk -> Cnmik. To be optimized
2783  struct ga_instruction_matrix_mult_spec2 : public ga_instruction {
2784  base_tensor &t;
2785  const base_tensor &tc1, &tc2;
2786  size_type J, I, K; // tc1 of size M*I*J, tc2 of size N*J*K
2787  // t of size N*M*I*K
2788  virtual int exec() {
2789  GA_DEBUG_INFO("Instruction: specific order one contraction "
2790  "(dot product or matrix multiplication)");
2791  const size_type MI = tc1.size() / J,
2792  NJ = tc2.size() / K, N = NJ / J;
2793 #if defined(GA_USES_BLAS)
2794  const BLAS_INT J_ = BLAS_INT(J), MI_ = BLAS_INT(MI), N_ = BLAS_INT(N);
2795  constexpr char notrans = 'N', trans = 'T';
2796  constexpr scalar_type one(1), zero(0);
2797  size_type NMI = N*MI;
2798  auto it = t.begin();
2799  for (size_type k = 0; k < K; ++k, it += NMI) // => it[N*M*I*k]
2800  gmm::dgemm_(&notrans, &trans, &N_, &MI_, &J_, &one,
2801  &(tc2[NJ*k]), &N_, &(tc1[0]), &MI_, &zero,
2802  &(*it), &N_);
2803  GA_DEBUG_ASSERT(it == t.end(), "Wrong sizes");
2804 #else
2805  auto it = t.begin();
2806  for (size_type k = 0; k < K; ++k)
2807  for (size_type mi = 0; mi < MI; ++mi)
2808  for (size_type n = 0; n < N; ++n, ++it) {
2809  *it = scalar_type(0);
2810  for (size_type j = 0; j < J; ++j)
2811  *it += tc1[mi+MI*j] * tc2[n+N*j+NJ*k];
2812  }
2813  GA_DEBUG_ASSERT(it == t.end(), "Wrong sizes");
2814 #endif
2815  return 0;
2816  }
2817  ga_instruction_matrix_mult_spec2(base_tensor &t_,
2818  const base_tensor &tc1_,
2819  const base_tensor &tc2_,
2820  size_type J_, size_type I_, size_type K_)
2821  : t(t_), tc1(tc1_), tc2(tc2_), J(J_), I(I_), K(K_) {}
2822  };
2823 
2824  // Performs Ani Bmi -> Cmn
2825  struct ga_instruction_contraction : public ga_instruction {
2826  base_tensor &t;
2827  const base_tensor &tc1, &tc2;
2828  const size_type I;
2829  virtual int exec() {
2830  GA_DEBUG_INFO("Instruction: contraction operation of size " << I);
2831  size_type N = tc1.size()/I,
2832  M = tc2.size()/I;
2833  GA_DEBUG_ASSERT(t.size() == N*M, "Internal error");
2834 #if defined(GA_USES_BLAS)
2835  if (M*N*I > 27) {
2836  BLAS_INT N_ = BLAS_INT(N), I_ = BLAS_INT(I), M_ = BLAS_INT(M);
2837  char notrans = 'N', trans = 'T';
2838  static const scalar_type one(1), zero(0);
2839  gmm::dgemm_(&notrans, &trans, &M_, &N_, &I_, &one,
2840  &(tc2[0]), &M_, &(tc1[0]), &N_, &zero, &(t[0]), &M_);
2841  } else
2842 #endif
2843  {
2844  auto it1=tc1.cbegin(), it2=tc2.cbegin(), it2end=it2+M;
2845  if (I==7) {
2846  for (auto it = t.begin(); it != t.end(); ++it) {
2847  reduc_elem_unrolled__<7>(it, it1, it2, N, M);
2848  if (++it2 == it2end) { it2 = tc2.cbegin(), ++it1; }
2849  }
2850  } else if (I==8) {
2851  for (auto it = t.begin(); it != t.end(); ++it) {
2852  reduc_elem_unrolled__<8>(it, it1, it2, N, M);
2853  if (++it2 == it2end) { it2 = tc2.cbegin(), ++it1; }
2854  }
2855  } else if (I==9) {
2856  for (auto it = t.begin(); it != t.end(); ++it) {
2857  reduc_elem_unrolled__<9>(it, it1, it2, N, M);
2858  if (++it2 == it2end) { it2 = tc2.cbegin(), ++it1; }
2859  }
2860  } else if (I==10) {
2861  for (auto it = t.begin(); it != t.end(); ++it) {
2862  reduc_elem_unrolled__<10>(it, it1, it2, N, M);
2863  if (++it2 == it2end) { it2 = tc2.cbegin(), ++it1; }
2864  }
2865  } else {
2866  for (auto it = t.begin(); it != t.end(); ++it) {
2867  auto it11 = it1, it22 = it2;
2868  scalar_type a = (*it11) * (*it22);
2869  for (size_type i = 1; i < I; ++i)
2870  { it11 += N; it22 += M; a += (*it11) * (*it22); }
2871  *it = a;
2872  if (++it2 == it2end) { it2 = tc2.cbegin(), ++it1; }
2873  }
2874  }
2875  }
2876  // auto it = t.begin(); // Unoptimized version.
2877  // for (size_type n = 0; n < N; ++n)
2878  // for (size_type m = 0; m < M; ++m, ++it) {
2879  // *it = scalar_type(0);
2880  // for (size_type i = 0; i < I; ++i)
2881  // *it += tc1[n+N*i] * tc2[m+M*i];
2882  // }
2883  return 0;
2884  }
2885  ga_instruction_contraction(base_tensor &t_,
2886  const base_tensor &tc1_,
2887  const base_tensor &tc2_, size_type I_)
2888  : t(t_), tc1(tc1_), tc2(tc2_), I(I_) {}
2889  };
2890 
2891  // Performs Ani Bmi -> Cmn
2892  struct ga_instruction_contraction_opt0_2 : public ga_instruction {
2893  base_tensor &t;
2894  const base_tensor &tc1, &tc2;
2895  size_type n, q;
2896  virtual int exec() {
2897  GA_DEBUG_INFO("Instruction: contraction operation of size " << n*q <<
2898  " optimized for vectorized second tensor of type 2");
2899  size_type nn = n*q, s1 = tc1.size()/nn, s2 = tc2.size()/nn, s2_q = s2/q;
2900  size_type s1_qq = s1*q, s2_qq = s2*q;
2901  GA_DEBUG_ASSERT(t.size() == s1*s2, "Internal error");
2902 
2903  auto it = t.begin();
2904  auto it1 = tc1.cbegin();
2905  for (size_type i = 0; i < s1; ++i, ++it1) {
2906  auto it2 = tc2.cbegin();
2907  for (size_type j = 0; j < s2_q; ++j) {
2908  if (j) it2+=q;
2909  auto itt1 = it1;
2910  for (size_type l = 0; l < q; ++l, ++it) {
2911  if (l) itt1 += s1;
2912  auto ittt1 = itt1, ittt2 = it2;
2913  *it = *ittt1 * (*ittt2);
2914  for (size_type m = 1; m < n; ++m) {
2915  ittt1 += s1_qq, ittt2 += s2_qq; *it += *ittt1 * (*ittt2);
2916  }
2917  }
2918  }
2919  }
2920  // base_tensor u = t;
2921  // ga_instruction_contraction toto(t, tc1, tc2, n*q);
2922  // toto.exec();
2923  // GMM_ASSERT1(gmm::vect_dist2(t.as_vector(), u.as_vector()) < 1E-9, "Erroneous");
2924  return 0;
2925  }
2926  ga_instruction_contraction_opt0_2(base_tensor &t_,
2927  const base_tensor &tc1_,
2928  const base_tensor &tc2_,
2929  size_type n_, size_type q_)
2930  : t(t_), tc1(tc1_), tc2(tc2_), n(n_), q(q_) {}
2931  };
2932 
2933  // Performs Ani Bmi -> Cmn
2934  template <int N>
2935  struct ga_instruction_contraction_opt0_2_unrolled : public ga_instruction {
2936  base_tensor &t;
2937  const base_tensor &tc1, &tc2;
2938  size_type q;
2939  virtual int exec() {
2940  GA_DEBUG_INFO("Instruction: unrolled contraction of size " << N*q <<
2941  " optimized for vectorized second tensor of type 2");
2942  size_type nn = N*q, s1 = tc1.size()/nn, s2 = tc2.size()/nn, s2_q = s2/q;
2943  size_type s1_qq = s1*q, s2_qq = s2*q;
2944  GA_DEBUG_ASSERT(t.size() == s1*s2, "Internal error");
2945 
2946  auto it = t.begin();
2947  auto it1 = tc1.cbegin();
2948  for (size_type i = 0; i < s1; ++i, ++it1) {
2949  auto it2 = tc2.cbegin();
2950  for (size_type j = 0; j < s2_q; ++j) {
2951  if (j) it2+=q;
2952  auto itt1 = it1;
2953  for (size_type l = 0; l < q; ++l, ++it) {
2954  if (l) itt1 += s1;
2955  auto ittt1 = itt1, ittt2 = it2;
2956  *it = *ittt1 * (*ittt2);
2957  for (size_type m = 1; m < N; ++m) {
2958  ittt1 += s1_qq, ittt2 += s2_qq; *it += *ittt1 * (*ittt2);
2959  }
2960  }
2961  }
2962  }
2963  return 0;
2964  }
2965  ga_instruction_contraction_opt0_2_unrolled(base_tensor &t_,
2966  const base_tensor &tc1_,
2967  const base_tensor &tc2_,
2968  size_type q_)
2969  : t(t_), tc1(tc1_), tc2(tc2_), q(q_) {}
2970  };
2971 
2972  // Performs Ani Bmi -> Cmn
2973  template <int N, int Q>
2974  struct ga_instruction_contraction_opt0_2_dunrolled : public ga_instruction {
2975  base_tensor &t;
2976  const base_tensor &tc1, &tc2;
2977  virtual int exec() {
2978  GA_DEBUG_INFO("Instruction: unrolled contraction of size " << N*Q
2979  << " optimized for vectorized second tensor of type 2");
2980  size_type s1 = tc1.size()/(N*Q), s2 = tc2.size()/(N*Q), s2_q = s2/Q;
2981  size_type s1_qq = s1*Q, s2_qq = s2*Q;
2982  GA_DEBUG_ASSERT(t.size() == s1*s2, "Internal error");
2983 
2984  auto it = t.begin();
2985  auto it1 = tc1.cbegin();
2986  for (size_type i = 0; i < s1; ++i, ++it1) {
2987  auto it2 = tc2.cbegin();
2988  for (size_type j = 0; j < s2_q; ++j) {
2989  if (j) it2+=Q;
2990  auto itt1 = it1;
2991  for (size_type l = 0; l < Q; ++l, ++it) {
2992  if (l) itt1 += s1;
2993  auto ittt1 = itt1, ittt2 = it2;
2994  *it = *ittt1 * (*ittt2);
2995  for (size_type m = 1; m < N; ++m) {
2996  ittt1 += s1_qq, ittt2 += s2_qq; *it += *ittt1 * (*ittt2);
2997  }
2998  }
2999  }
3000  }
3001  return 0;
3002  }
3003  ga_instruction_contraction_opt0_2_dunrolled(base_tensor &t_,
3004  const base_tensor &tc1_,
3005  const base_tensor &tc2_)
3006  : t(t_), tc1(tc1_), tc2(tc2_) {}
3007  };
3008 
3009  // Performs Ani Bmi -> Cmn
3010  struct ga_instruction_contraction_opt2_0 : public ga_instruction {
3011  base_tensor &t;
3012  const base_tensor &tc1, &tc2;
3013  size_type n, q;
3014  virtual int exec() {
3015  GA_DEBUG_INFO("Instruction: contraction operation of size " << n*q <<
3016  " optimized for vectorized second tensor of type 2");
3017  size_type nn = n*q, s1 = tc1.size()/nn, s2 = tc2.size()/nn;
3018  size_type s1_q = s1/q, s1_qq = s1*q, s2_qq = s2*q;
3019  GA_DEBUG_ASSERT(t.size() == s1*s2, "Internal error");
3020 
3021  auto it = t.begin();
3022  for (size_type i = 0; i < s1_q; ++i) {
3023  auto it1 = tc1.cbegin() + i*q;
3024  for (size_type l = 0; l < q; ++l) {
3025  auto it2 = tc2.cbegin() + l*s2;
3026  for (size_type j = 0; j < s2; ++j, ++it, ++it2) {
3027  auto itt1 = it1, itt2 = it2;
3028  *it = *itt1 * (*itt2);
3029  for (size_type m = 1; m < n; ++m) {
3030  itt1 += s1_qq, itt2 += s2_qq; *it += *itt1 * (*itt2);
3031  }
3032  }
3033  }
3034  }
3035  return 0;
3036  }
3037  ga_instruction_contraction_opt2_0(base_tensor &t_,
3038  const base_tensor &tc1_,
3039  const base_tensor &tc2_,
3040  size_type n_, size_type q_)
3041  : t(t_), tc1(tc1_), tc2(tc2_), n(n_), q(q_) { }
3042  };
3043 
3044  // Performs Ani Bmi -> Cmn
3045  template <int N>
3046  struct ga_instruction_contraction_opt2_0_unrolled : public ga_instruction {
3047  base_tensor &t;
3048  const base_tensor &tc1, &tc2;
3049  size_type q;
3050  virtual int exec() {
3051  GA_DEBUG_INFO("Instruction: unrolled contraction of size " << N*q
3052  << " optimized for vectorized second tensor of type 2");
3053  size_type nn = N*q, s1 = tc1.size()/nn, s2 = tc2.size()/nn;
3054  size_type s1_q = s1/q, s1_qq = s1*q, s2_qq = s2*q;
3055  GA_DEBUG_ASSERT(t.size() == s1*s2, "Internal error");
3056 
3057  auto it = t.begin();
3058  auto it1 = tc1.cbegin();
3059  for (size_type i = 0; i < s1_q; ++i, it1 += q) {
3060  for (size_type l = 0; l < q; ++l) {
3061  auto it2 = tc2.cbegin() + l*s2;
3062  for (size_type j = 0; j < s2; ++j, ++it, ++it2) {
3063  auto itt1 = it1, itt2 = it2;
3064  *it = *itt1 * (*itt2);
3065  for (size_type m = 1; m < N; ++m) {
3066  itt1 += s1_qq, itt2 += s2_qq; *it += *itt1 * (*itt2);
3067  }
3068  }
3069  }
3070  }
3071  return 0;
3072  }
3073  ga_instruction_contraction_opt2_0_unrolled(base_tensor &t_,
3074  const base_tensor &tc1_,
3075  const base_tensor &tc2_,
3076  size_type q_)
3077  : t(t_), tc1(tc1_), tc2(tc2_), q(q_) {}
3078  };
3079 
3080  // Performs Ani Bmi -> Cmn
3081  template <int N, int Q>
3082  struct ga_instruction_contraction_opt2_0_dunrolled : public ga_instruction {
3083  base_tensor &t;
3084  const base_tensor &tc1, &tc2;
3085  virtual int exec() {
3086  GA_DEBUG_INFO("Instruction: unrolled contraction of size " << N*Q
3087  << " optimized for vectorized second tensor of type 2");
3088  size_type s1 = tc1.size()/(N*Q), s2 = tc2.size()/(N*Q);
3089  size_type s1_q = s1/Q, s1_qq = s1*Q, s2_qq = s2*Q;
3090  GA_DEBUG_ASSERT(t.size() == s1*s2, "Internal error");
3091 
3092  auto it = t.begin();
3093  auto it1 = tc1.cbegin();
3094  for (size_type i = 0; i < s1_q; ++i, it1 += Q) {
3095  for (size_type l = 0; l < Q; ++l) {
3096  auto it2 = tc2.cbegin() + l*s2;
3097  for (size_type j = 0; j < s2; ++j, ++it, ++it2) {
3098  auto itt1 = it1, itt2 = it2;
3099  *it = *itt1 * (*itt2);
3100  for (size_type m = 1; m < N; ++m) {
3101  itt1 += s1_qq, itt2 += s2_qq; *it += *itt1 * (*itt2);
3102  }
3103  }
3104  }
3105  }
3106  return 0;
3107  }
3108  ga_instruction_contraction_opt2_0_dunrolled(base_tensor &t_,
3109  const base_tensor &tc1_,
3110  const base_tensor &tc2_)
3111  : t(t_), tc1(tc1_), tc2(tc2_) {}
3112  };
3113 
3114  // Performs Ani Bmi -> Cmn
3115  struct ga_instruction_contraction_opt0_1 : public ga_instruction {
3116  base_tensor &t;
3117  const base_tensor &tc1, &tc2;
3118  size_type nn;
3119  virtual int exec() {
3120  GA_DEBUG_INFO("Instruction: contraction operation of size " << nn <<
3121  " optimized for vectorized second tensor of type 1");
3122  size_type ss1=tc1.size(), s1 = ss1/nn, s2=tc2.size()/nn, s2_n=s2/nn;
3123 
3124  auto it = t.begin();
3125  auto it1 = tc1.cbegin();
3126  for (size_type i = 0; i < s1; ++i, ++it1) {
3127  auto it2 = tc2.cbegin();
3128  for (size_type j = 0; j < s2_n; ++j) {
3129  if (j) it2 += nn;
3130  auto itt1 = it1;
3131  *it++ = (*itt1) * (*it2);
3132  for (size_type k = 1; k < nn; ++k)
3133  { itt1 += s1; *it++ = (*itt1) * (*it2); }
3134  }
3135  }
3136  return 0;
3137  }
3138  ga_instruction_contraction_opt0_1(base_tensor &t_,
3139  const base_tensor &tc1_,
3140  const base_tensor &tc2_,
3141  size_type n_)
3142  : t(t_), tc1(tc1_), tc2(tc2_), nn(n_) {}
3143  };
3144 
3145  template<int N> inline void reduc_elem_unrolled_opt1_
3146  (const base_vector::iterator &it, const base_vector::const_iterator &it1,
3147  scalar_type a, size_type s1) {
3148  it[N-1] = it1[(N-1)*s1] * a;
3149  reduc_elem_unrolled_opt1_<N-1>(it, it1, a, s1);
3150  }
3151  template<> inline void reduc_elem_unrolled_opt1_<1>
3152  (const base_vector::iterator &it, const base_vector::const_iterator &it1,
3153  scalar_type a, size_type /* s1 */)
3154  { *it = (*it1) * a; }
3155 
3156  // Performs Ani Bmi -> Cmn
3157  template <int N>
3158  struct ga_instruction_contraction_opt0_1_unrolled : public ga_instruction {
3159  base_tensor &t;
3160  const base_tensor &tc1, &tc2;
3161  virtual int exec() {
3162  GA_DEBUG_INFO("Instruction: unrolled contraction operation of size " << N
3163  << " optimized for vectorized second tensor of type 1");
3164  size_type s1 = tc1.size()/N, s2 = tc2.size()/N;
3165  auto it = t.begin();
3166  auto it1 = tc1.cbegin();
3167  for (size_type i = 0; i < s1; ++i, ++it1) {
3168  auto it2 = tc2.cbegin(), it2e = it2 + s2;
3169  for (; it2 != it2e; it2 += N, it += N)
3170  reduc_elem_unrolled_opt1_<N>(it, it1, *it2, s1);
3171  }
3172  return 0;
3173  }
3174  ga_instruction_contraction_opt0_1_unrolled(base_tensor &t_,
3175  const base_tensor &tc1_,
3176  const base_tensor &tc2_)
3177  : t(t_), tc1(tc1_), tc2(tc2_) {}
3178  };
3179 
3180  // Performs Ani Bmi -> Cmn
3181  struct ga_instruction_contraction_opt1_1 : public ga_instruction {
3182  base_tensor &t;
3183  const base_tensor &tc1, &tc2;
3184  size_type nn;
3185  virtual int exec() {
3186  GA_DEBUG_INFO("Instruction: contraction operation of size " << nn <<
3187  " optimized for both vectorized tensor of type 1");
3188  size_type s1 = tc1.size()/nn, s2 = tc2.size()/nn, s2_1 = s2+1;
3189  GA_DEBUG_ASSERT(t.size() == s2*s1, "Internal error");
3190  size_type ss1 = s1/nn, ss2 = s2/nn;
3191 
3192  // std::fill(t.begin(), t.end(), scalar_type(0)); // Factorized
3193  auto it2 = tc2.cbegin();
3194  for (size_type j = 0; j < ss2; ++j) {
3195  if (j) it2 += nn;
3196  auto it1 = tc1.cbegin();
3197  auto it = t.begin() + j*nn;
3198  for (size_type i = 0; i < ss1; ++i) {
3199  if (i) { it1 += nn, it += s2*nn; }
3200  scalar_type a = (*it1) * (*it2);
3201  auto itt = it;
3202  *itt = a; itt += s2_1; *itt = a;
3203  for (size_type k = 2; k < nn; ++k) { itt += s2_1; *itt = a; }
3204  }
3205  }
3206  return 0;
3207  }
3208  ga_instruction_contraction_opt1_1(base_tensor &t_,
3209  const base_tensor &tc1_,
3210  const base_tensor &tc2_, size_type n_)
3211  : t(t_), tc1(tc1_), tc2(tc2_), nn(n_) {}
3212  };
3213 
3214 
3215 
3216  // Performs Ani Bmi -> Cmn where both tensors are vectorized gradients of
3217  // type 2. The result has the sparsity 10: a scalar block repeated on the
3218  // qdim diagonal blocks.
3219  struct ga_instruction_contraction_opt2_2 : public ga_instruction {
3220  base_tensor &t;
3221  const base_tensor &tc1, &tc2;
3222  size_type q, n;
3223  virtual int exec() {
3224  GA_DEBUG_INFO("Instruction: contraction operation of size " << n*q <<
3225  " optimized for both vectorized tensor of type 2");
3226  size_type nn = n*q, s1 = tc1.size()/nn, s2 = tc2.size()/nn;
3227  size_type ss1 = s1/q, ss2 = s2/q, s1_qq = s1*q, s2_qq = s2*q;
3228  size_type s2_1 = s2+1;
3229  GA_DEBUG_ASSERT(t.size() == s2*s1, "Internal error");
3230 
3231  // std::fill(t.begin(), t.end(), scalar_type(0)); // Factorized
3232  for (size_type j = 0; j < ss2; ++j) {
3233  auto it2 = tc2.cbegin() + j*q;
3234  auto it = t.begin() + j*q;
3235  for (size_type i = 0; i < ss1; ++i, it += s2_qq) {
3236  auto it1 = tc1.cbegin() + i*q;
3237  scalar_type a = scalar_type(0);
3238  for (size_type m = 0; m < n; ++m)
3239  a += it1[m*s1_qq] * it2[m*s2_qq];
3240  auto itt = it;
3241  *itt = a;
3242  for (size_type k = 1; k < q; ++k) { itt += s2_1; *itt = a; }
3243  }
3244  }
3245  return 0;
3246  }
3247  ga_instruction_contraction_opt2_2(base_tensor &t_,
3248  const base_tensor &tc1_,
3249  const base_tensor &tc2_,
3250  size_type q_, size_type n_)
3251  : t(t_), tc1(tc1_), tc2(tc2_), q(q_), n(n_) {}
3252  };
3253 
3254 
3255  // Performs Ani Bmi -> Cmn. Unrolled operation.
3256  template<int I>
3257  struct ga_instruction_contraction_unrolled
3258  : public ga_instruction {
3259  base_tensor &t;
3260  const base_tensor &tc1, &tc2;
3261  virtual int exec() {
3262  GA_DEBUG_INFO("Instruction: unrolled contraction operation of size " << I);
3263  size_type N = tc1.size()/I, M = tc2.size()/I;
3264  GA_DEBUG_ASSERT(t.size() == N*M, "Internal error, " << t.size()
3265  << " != " << N << "*" << M);
3266  auto it1=tc1.cbegin(), it2=tc2.cbegin(), it2end=it2+M;
3267  for (auto it = t.begin(); it != t.end(); ++it) {
3268  reduc_elem_unrolled__<I>(it, it1, it2, N, M);
3269  if (++it2 == it2end) { it2 = tc2.cbegin(), ++it1; }
3270  }
3271  return 0;
3272  }
3273  ga_instruction_contraction_unrolled(base_tensor &t_,
3274  const base_tensor &tc1_,
3275  const base_tensor &tc2_)
3276  : t(t_), tc1(tc1_), tc2(tc2_) {}
3277  };
3278 
3279  // Performs An Bm -> Cmn. Unrolled operation.
3280  template<>
3281  struct ga_instruction_contraction_unrolled<1> : public ga_instruction {
3282  base_tensor &t;
3283  const base_tensor &tc1, &tc2;
3284  virtual int exec() {
3285  GA_DEBUG_INFO("Instruction: unrolled contraction operation of size 1");
3286  size_type N = tc1.size(), M = tc2.size();
3287  GA_DEBUG_ASSERT(t.size() == N*M, "Internal error, " << t.size()
3288  << " != " << N << "*" << M);
3289 
3290  base_tensor::iterator it = t.begin();
3291  base_tensor::const_iterator it1 = tc1.cbegin();
3292  switch(M) {
3293  case(1):
3294  for (size_type n = 0; n < N; ++n, ++it1)
3295  *it++ = tc2[0] * (*it1);
3296  break;
3297  case(2):
3298  for (size_type n = 0; n < N; ++n, ++it1) {
3299  base_tensor::const_iterator it2 = tc2.cbegin();
3300  dax__<2>(it, it2, *it1);
3301  }
3302  break;
3303  case(3):
3304  for (size_type n = 0; n < N; ++n, ++it1) {
3305  base_tensor::const_iterator it2 = tc2.cbegin();
3306  dax__<4>(it, it2, *it1);
3307  }
3308  break;
3309  case(4):
3310  for (size_type n = 0; n < N; ++n, ++it1) {
3311  base_tensor::const_iterator it2 = tc2.cbegin();
3312  dax__<4>(it, it2, *it1);
3313  }
3314  break;
3315  default:
3316  const int M1 = int(M)/4;
3317  const int M2 = int(M) - M1*4;
3318  for (size_type n = 0; n < N; ++n, ++it1) {
3319  base_tensor::const_iterator it2 = tc2.cbegin();
3320  for (int mm=0; mm < M1; ++mm)
3321  dax__<4>(it, it2, *it1);
3322  for (int mm=0; mm < M2; ++mm)
3323  *it++ = (*it2++) * (*it1);
3324  }
3325  }
3326  return 0;
3327  }
3328  ga_instruction_contraction_unrolled(base_tensor &t_,
3329  const base_tensor &tc1_,
3330  const base_tensor &tc2_)
3331  : t(t_), tc1(tc1_), tc2(tc2_) {}
3332  };
3333 
3334  template<int N, int S2>
3335  inline void reduc_elem_d_unrolled__(base_tensor::iterator &it,
3336  base_tensor::const_iterator &it1,
3337  base_tensor::const_iterator &it2,
3338  size_type s1, size_type s2) {
3339  reduc_elem_unrolled__<N>(it, it1, it2, s1, s2);
3340  reduc_elem_d_unrolled__<N, S2-1>(++it, it1, ++it2, s1, s2);
3341  }
3342  // A Repeated definition is following because partial specialization
3343  // of functions is not allowed in C++ for the moment.
3344  // The gain in assembly time is small compared to the simply unrolled version
3345  template<> inline void reduc_elem_d_unrolled__<1, 0>
3346  (base_tensor::iterator &/* it */, base_tensor::const_iterator &/* it1 */,
3347  base_tensor::const_iterator &/* it2 */, size_type /* s1 */, size_type /* s2 */) { }
3348  template<> inline void reduc_elem_d_unrolled__<2, 0>
3349  (base_tensor::iterator &/* it */, base_tensor::const_iterator &/* it1 */,
3350  base_tensor::const_iterator &/* it2 */, size_type /* s1 */, size_type /* s2 */) { }
3351  template<> inline void reduc_elem_d_unrolled__<3, 0>
3352  (base_tensor::iterator &/* it */, base_tensor::const_iterator &/* it1 */,
3353  base_tensor::const_iterator &/* it2 */, size_type /* s1 */, size_type /* s2 */) { }
3354  template<> inline void reduc_elem_d_unrolled__<4, 0>
3355  (base_tensor::iterator &/* it */, base_tensor::const_iterator &/* it1 */,
3356  base_tensor::const_iterator &/* it2 */, size_type /* s1 */, size_type /* s2 */) { }
3357  template<> inline void reduc_elem_d_unrolled__<5, 0>
3358  (base_tensor::iterator &/* it */, base_tensor::const_iterator &/* it1 */,
3359  base_tensor::const_iterator &/* it2 */, size_type /* s1 */, size_type /* s2 */) { }
3360  template<> inline void reduc_elem_d_unrolled__<6, 0>
3361  (base_tensor::iterator &/* it */, base_tensor::const_iterator &/* it1 */,
3362  base_tensor::const_iterator &/* it2 */, size_type /* s1 */, size_type /* s2 */) { }
3363  template<> inline void reduc_elem_d_unrolled__<7, 0>
3364  (base_tensor::iterator &/* it */, base_tensor::const_iterator &/* it1 */,
3365  base_tensor::const_iterator &/* it2 */, size_type /* s1 */, size_type /* s2 */) { }
3366  template<> inline void reduc_elem_d_unrolled__<8, 0>
3367  (base_tensor::iterator &/* it */, base_tensor::const_iterator &/* it1 */,
3368  base_tensor::const_iterator &/* it2 */, size_type /* s1 */, size_type /* s2 */) { }
3369  template<> inline void reduc_elem_d_unrolled__<9, 0>
3370  (base_tensor::iterator &/* it */, base_tensor::const_iterator &/* it1 */,
3371  base_tensor::const_iterator &/* it2 */, size_type /* s1 */, size_type /* s2 */) { }
3372  template<> inline void reduc_elem_d_unrolled__<10, 0>
3373  (base_tensor::iterator &/* it */, base_tensor::const_iterator &/* it1 */,
3374  base_tensor::const_iterator &/* it2 */, size_type /* s1 */, size_type /* s2 */) { }
3375  template<> inline void reduc_elem_d_unrolled__<11, 0>
3376  (base_tensor::iterator &/* it */, base_tensor::const_iterator &/* it1 */,
3377  base_tensor::const_iterator &/* it2 */, size_type /* s1 */, size_type /* s2 */) { }
3378  template<> inline void reduc_elem_d_unrolled__<12, 0>
3379  (base_tensor::iterator &/* it */, base_tensor::const_iterator &/* it1 */,
3380  base_tensor::const_iterator &/* it2 */, size_type /* s1 */, size_type /* s2 */) { }
3381  template<> inline void reduc_elem_d_unrolled__<13, 0>
3382  (base_tensor::iterator &/* it */, base_tensor::const_iterator &/* it1 */,
3383  base_tensor::const_iterator &/* it2 */, size_type /* s1 */, size_type /* s2 */) { }
3384  template<> inline void reduc_elem_d_unrolled__<14, 0>
3385  (base_tensor::iterator &/* it */, base_tensor::const_iterator &/* it1 */,
3386  base_tensor::const_iterator &/* it2 */, size_type /* s1 */, size_type /* s2 */) { }
3387  template<> inline void reduc_elem_d_unrolled__<15, 0>
3388  (base_tensor::iterator &/* it */, base_tensor::const_iterator &/* it1 */,
3389  base_tensor::const_iterator &/* it2 */, size_type /* s1 */, size_type /* s2 */) { }
3390  template<> inline void reduc_elem_d_unrolled__<16, 0>
3391  (base_tensor::iterator &/* it */, base_tensor::const_iterator &/* it1 */,
3392  base_tensor::const_iterator &/* it2 */, size_type /* s1 */, size_type /* s2 */) { }
3393 
3394  // Performs Ani Bmi -> Cmn. Automatically doubly unrolled operation
3395  // (for uniform meshes).
3396  template<int I, int M>
3397  struct ga_ins_red_d_unrolled : public ga_instruction {
3398  base_tensor &t;
3399  const base_tensor &tc1, &tc2;
3400  virtual int exec() {
3401  GA_DEBUG_INFO("Instruction: doubly unrolled contraction operation of size "
3402  << M << "x" << I);
3403  size_type N = tc1.size()/I, M_ = tc2.size()/I;
3404  GA_DEBUG_ASSERT(M_ == M, "Internal error");
3405  GA_DEBUG_ASSERT(t.size() == N*M, "Internal error, " << t.size()
3406  << " != " << N << "*" << M);
3407  auto it = t.begin();
3408  auto it1 = tc1.cbegin();
3409  for (size_type n = 0; n < N; ++n, ++it1) {
3410  auto it2 = tc2.cbegin();
3411  reduc_elem_d_unrolled__<I, M>(it, it1, it2, N, M); // M argument is known at compile time it can be optimized
3412  }
3413  GA_DEBUG_ASSERT(it == t.end(), "Internal error");
3414  return 0;
3415  }
3416  ga_ins_red_d_unrolled(base_tensor &t_,
3417  const base_tensor &tc1_, const base_tensor &tc2_)
3418  : t(t_), tc1(tc1_), tc2(tc2_) {}
3419  };
3420 
3421 
3422  // Extracts the pattern of a single test function vectorized tensor.
3423  static bool ga_structure_mask(const assembly_tensor &t, size_type &Q,
3424  std::vector<bool> &mask) {
3425  if (t.sparsity() != 2 && t.sparsity() != 4) return false;
3426  Q = t.qdim();
3427  if (t.sparsity() == 4) { mask = *(t.mask()); return true; }
3428  const bgeot::multi_index &sz = t.tensor().sizes();
3429  if (sz.size() < 2 || sz.size() > 3 || sz[1] != Q) return false;
3430  size_type nd = (sz.size() > 2) ? sz[2] : 1;
3431  mask.assign(Q*Q*nd, false);
3432  for (size_type l = 0; l < nd; ++l)
3433  for (size_type q = 0; q < Q; ++q)
3434  mask[q + Q*(q + Q*l)] = true;
3435  return true;
3436  }
3437 
3438  // Tests if a sum has the form A + A' or A' + A.
3439  static bool ga_is_sum_with_transpose(const pga_tree_node child0,
3440  const pga_tree_node child1,
3441  const ga_workspace &workspace) {
3442  auto is_transpose = [&workspace](const pga_tree_node a,
3443  const pga_tree_node b) {
3444  return a->node_type == GA_NODE_OP && a->op_type == GA_QUOTE
3445  && sub_tree_are_equal(a->children[0], b, workspace, 1);
3446  };
3447  return is_transpose(child0, child1) || is_transpose(child1, child0);
3448  }
3449 
3450  // A sum is nonzero where one of its terms is.
3451  static void ga_propagate_sum_structure(assembly_tensor &t,
3452  const assembly_tensor &tc1,
3453  const assembly_tensor &tc2,
3454  bool symmetric = false) {
3455  if (tc1.sparsity() == tc2.sparsity() && tc1.qdim() == tc2.qdim()
3456  && tc1.sparsity() != 4) { t.copy_structure(tc1); return; }
3457  size_type Q1 = 0, Q2 = 0;
3458  std::vector<bool> m1, m2;
3459  if (ga_structure_mask(tc1, Q1, m1) && ga_structure_mask(tc2, Q2, m2)
3460  && Q1 == Q2 && m1.size() == m2.size()) {
3461  for (size_type i = 0; i < m1.size(); ++i) m1[i] = m1[i] || m2[i];
3462  t.set_mask(Q1, std::make_shared<std::vector<bool>>(std::move(m1)),
3463  symmetric || (tc1.mask_symmetric() && tc2.mask_symmetric()));
3464  }
3465  }
3466 
3467  // Pattern of the transpose of a tensor having two proper indices of size d
3468  // or, if keep_original is true, of its symmetric part (union of the two).
3469  static bool ga_matrix_op_mask(const assembly_tensor &tc, size_type d,
3470  bool keep_original, size_type &Q,
3471  std::vector<bool> &res) {
3472  std::vector<bool> m;
3473  if (!ga_structure_mask(tc, Q, m) || m.size() != Q*d*d) return false;
3474  res.assign(m.size(), false);
3475  for (size_type j = 0; j < d; ++j)
3476  for (size_type i = 0; i < d; ++i)
3477  for (size_type q = 0; q < Q; ++q)
3478  res[q + Q*(i + d*j)] = m[q + Q*(j + d*i)]
3479  || (keep_original && m[q + Q*(i + d*j)]);
3480  return true;
3481  }
3482 
3483  // Symmetric part of a masked tensor: only the stored entries are computed.
3484  struct ga_instruction_sym_masked : public ga_instruction {
3485  base_tensor &t;
3486  const base_tensor &tc1;
3487  size_type Q, d;
3488  std::vector<std::array<size_type, 3>> entries; // q, direct, transposed
3489  virtual int exec() {
3490  GA_DEBUG_INFO("Instruction: symmetric part with a mask");
3491  GA_DEBUG_ASSERT(t.size() == tc1.size(), "Wrong sizes");
3492  size_type s = t.size() / (d*d), nb = s / Q;
3493  for (const auto &e : entries) {
3494  auto it = t.begin() + e[0] + s*e[1];
3495  auto it1 = tc1.cbegin() + e[0] + s*e[1];
3496  auto it1T = tc1.cbegin() + e[0] + s*e[2];
3497  for (size_type a = 0; a < nb; ++a, it += Q, it1 += Q, it1T += Q)
3498  *it = 0.5*(*it1 + *it1T);
3499  }
3500  return 0;
3501  }
3502  ga_instruction_sym_masked(base_tensor &t_, const base_tensor &tc1_,
3503  size_type Q_, size_type d_,
3504  const std::vector<bool> &mask)
3505  : t(t_), tc1(tc1_), Q(Q_), d(d_) {
3506  for (size_type j = 0; j < d; ++j)
3507  for (size_type i = 0; i < d; ++i)
3508  for (size_type q = 0; q < Q; ++q)
3509  if (mask[q + Q*(i + d*j)])
3510  entries.push_back({{q, i + d*j, j + d*i}});
3511  }
3512  };
3513 
3514  // Performs Ani Bmi -> Cmn for two masked tensors. The nonzero slices of
3515  // the tensors are first gathered in contiguous arrays, then each block of
3516  // components (q1, q2) is computed in a contiguous buffer and stored in t.
3517  // If both tensors are symmetric in their two proper indices of size d,
3518  // only the entries i <= j are visited, with a weight 2 for i < j.
3519  struct ga_instruction_contraction_masked : public ga_instruction {
3520  struct slice { size_type q, p; };
3521  struct term { size_type k1, k2; scalar_type w; };
3522  struct block { size_type q1, q2, first, last; };
3523  base_tensor &t;
3524  const base_tensor &tc1, &tc2;
3525  size_type Q, n;
3526  std::vector<slice> slices1, slices2;
3527  std::vector<term> terms;
3528  std::vector<block> blocks;
3529  std::vector<scalar_type> packed1, packed2, buffer;
3530 
3531  static void gather(const base_tensor &tc, size_type s, size_type Q_,
3532  const std::vector<slice> &slices,
3533  std::vector<scalar_type> &packed) {
3534  size_type ss = s/Q_;
3535  packed.resize(ss*slices.size());
3536  auto it = packed.begin();
3537  for (const slice &sl : slices) {
3538  auto itc = tc.cbegin() + sl.q + s*sl.p;
3539  for (size_type i = 0; i < ss; ++i, itc += Q_) *it++ = *itc;
3540  }
3541  }
3542 
3543  virtual int exec() {
3544  GA_DEBUG_INFO("Instruction: contraction of size " << n
3545  << " of masked tensors");
3546  size_type s1 = tc1.size()/n, s2 = tc2.size()/n;
3547  size_type ss1 = s1/Q, ss2 = s2/Q;
3548  GA_DEBUG_ASSERT(t.size() == s1*s2, "Internal error");
3549  gather(tc1, s1, Q, slices1, packed1);
3550  gather(tc2, s2, Q, slices2, packed2);
3551  buffer.resize(ss1*ss2);
3552 
3553  for (const block &b : blocks) {
3554  std::fill(buffer.begin(), buffer.end(), scalar_type(0));
3555  for (size_type l = b.first; l < b.last; ++l) {
3556  const term &tm = terms[l];
3557  const scalar_type *p1 = packed1.data() + tm.k1*ss1;
3558  const scalar_type *p2 = packed2.data() + tm.k2*ss2;
3559  scalar_type *pb = buffer.data();
3560  for (size_type i = 0; i < ss1; ++i, pb += ss2) {
3561  scalar_type a = tm.w * p1[i];
3562  for (size_type j = 0; j < ss2; ++j) pb[j] += a * p2[j];
3563  }
3564  }
3565  const scalar_type *pb = buffer.data();
3566  auto itt = t.begin() + b.q2 + s2*b.q1;
3567  for (size_type i = 0; i < ss1; ++i, itt += s2*Q) {
3568  auto it = itt;
3569  for (size_type j = 0; j < ss2; ++j, it += Q) *it = *pb++;
3570  }
3571  }
3572  return 0;
3573  }
3574 
3575  static size_type slice_index(std::vector<slice> &slices,
3576  std::vector<size_type> &index,
3577  size_type q, size_type p, size_type Q_) {
3578  size_type &k = index[q + Q_*p];
3579  if (k == size_type(-1)) { k = slices.size(); slices.push_back({q, p}); }
3580  return k;
3581  }
3582 
3583  ga_instruction_contraction_masked(base_tensor &t_, const base_tensor &tc1_,
3584  const base_tensor &tc2_, size_type Q_,
3585  size_type n_,
3586  const std::vector<bool> &m1,
3587  const std::vector<bool> &m2,
3588  size_type sym_d)
3589  : t(t_), tc1(tc1_), tc2(tc2_), Q(Q_), n(n_) {
3590  std::vector<size_type> ind1(Q*n, size_type(-1)), ind2(Q*n, size_type(-1));
3591  for (size_type q2 = 0; q2 < Q; ++q2)
3592  for (size_type q1 = 0; q1 < Q; ++q1) {
3593  size_type first = terms.size();
3594  for (size_type p = 0; p < n; ++p) {
3595  scalar_type w(1);
3596  if (sym_d) {
3597  size_type i = p % sym_d, j = p / sym_d;
3598  if (i > j) continue;
3599  if (i < j) w = scalar_type(2);
3600  }
3601  if (m1[q1 + Q*p] && m2[q2 + Q*p])
3602  terms.push_back(term{slice_index(slices1, ind1, q1, p, Q),
3603  slice_index(slices2, ind2, q2, p, Q), w});
3604  }
3605  if (terms.size() > first)
3606  blocks.push_back(block{q1, q2, first, terms.size()});
3607  }
3608  }
3609  };
3610 
3611 #if defined(GA_USES_BLAS)
3612  // Performs Ani Bmi -> Cmn for two tensors symmetric in their two proper
3613  // indices of size d, keeping only the entries i <= j (with a weight 2 for
3614  // i < j). A single dgemm is much faster here than a loop on the nonzero
3615  // entries, the number of which is not small enough to compensate.
3616  struct ga_instruction_contraction_sym : public ga_instruction {
3617  base_tensor &t;
3618  const base_tensor &tc1, &tc2;
3619  size_type n;
3620  std::vector<size_type> ind;
3621  std::vector<scalar_type> weight, packed1, packed2;
3622  virtual int exec() {
3623  GA_DEBUG_INFO("Instruction: contraction of size " << n
3624  << " of symmetric tensors");
3625  size_type s1 = tc1.size()/n, s2 = tc2.size()/n, nb = ind.size();
3626  GA_DEBUG_ASSERT(t.size() == s1*s2, "Internal error");
3627  packed1.resize(s1*nb);
3628  packed2.resize(s2*nb);
3629  auto it1 = packed1.begin(), it2 = packed2.begin();
3630  for (size_type k = 0; k < nb; ++k) {
3631  auto itc1 = tc1.cbegin() + s1*ind[k], itc2 = tc2.cbegin() + s2*ind[k];
3632  scalar_type w = weight[k];
3633  for (size_type i = 0; i < s1; ++i) *it1++ = w * itc1[i];
3634  it2 = std::copy(itc2, itc2 + s2, it2);
3635  }
3636  BLAS_INT N_ = BLAS_INT(s1), M_ = BLAS_INT(s2), K_ = BLAS_INT(nb);
3637  char notrans = 'N', trans = 'T';
3638  static const scalar_type one(1), zero(0);
3639  gmm::dgemm_(&notrans, &trans, &M_, &N_, &K_, &one, packed2.data(), &M_,
3640  packed1.data(), &N_, &zero, &(t[0]), &M_);
3641  return 0;
3642  }
3643  ga_instruction_contraction_sym(base_tensor &t_, const base_tensor &tc1_,
3644  const base_tensor &tc2_, size_type n_,
3645  size_type d)
3646  : t(t_), tc1(tc1_), tc2(tc2_), n(n_) {
3647  for (size_type j = 0; j < d; ++j)
3648  for (size_type i = 0; i <= j; ++i) {
3649  ind.push_back(i + d*j);
3650  weight.push_back(i == j ? scalar_type(1) : scalar_type(2));
3651  }
3652  }
3653  };
3654 #endif
3655 
3656  // Selects the instruction for two masked tensors. The masked loop has to
3657  // compete with dgemm when a BLAS is used, which requires a very small
3658  // proportion of visited entries (11% for the transpose of a gradient in 3D
3659  // wins, 19% for a symmetrized gradient loses).
3660  static pga_instruction ga_masked_contraction_switch
3661  (assembly_tensor &t_, assembly_tensor &tc1_, assembly_tensor &tc2_,
3662  size_type n, bool &to_clear) {
3663  // With a gradient of type 2 as operand, the opt0_2 and opt2_0 instructions
3664  // are faster despite visiting more entries.
3665  if (tc1_.sparsity() != 4 || tc2_.sparsity() != 4)
3666  return pga_instruction();
3667  // In dimension 2, the unrolled dense instructions are faster.
3668  if (n <= 4) return pga_instruction();
3669  size_type Q1 = 0, Q2 = 0;
3670  std::vector<bool> m1, m2;
3671  if (!ga_structure_mask(tc1_, Q1, m1) || !ga_structure_mask(tc2_, Q2, m2)
3672  || Q1 != Q2 || m1.size() != Q1*n || m2.size() != Q1*n)
3673  return pga_instruction();
3674  size_type sym_d = 0;
3675  if (tc1_.mask_symmetric() && tc2_.mask_symmetric())
3676  sym_d = size_type(round(sqrt(scalar_type(n))));
3677 #if defined(GA_USES_BLAS)
3678  if (sym_d)
3679  return std::make_shared<ga_instruction_contraction_sym>
3680  (t_.tensor(), tc1_.tensor(), tc2_.tensor(), n, sym_d);
3681  const size_type max_percent = 15;
3682 #else
3683  const size_type max_percent = 70;
3684 #endif
3685  auto pgai = std::make_shared<ga_instruction_contraction_masked>
3686  (t_.tensor(), tc1_.tensor(), tc2_.tensor(), Q1, n, m1, m2, sym_d);
3687  if (100*pgai->terms.size() > max_percent*Q1*Q1*n)
3688  return pga_instruction();
3689  to_clear = true;
3690  return pgai;
3691  }
3692 
3693  // Returns true if tc1 and tc2 are vectorized gradients of type 2, with the
3694  // same qdim, fully contracted on their component and derivative indices.
3695  static bool ga_contraction_is_opt2_2(const assembly_tensor &tc1_,
3696  const assembly_tensor &tc2_,
3697  size_type n) {
3698  if (tc1_.sparsity() != 2 || tc2_.sparsity() != 2
3699  || tc1_.qdim() != tc2_.qdim()) return false;
3700  const bgeot::multi_index &sz1 = tc1_.tensor().sizes();
3701  const bgeot::multi_index &sz2 = tc2_.tensor().sizes();
3702  size_type q = tc1_.qdim();
3703  if (sz1.size() != sz2.size() || sz1.size() < 2 || sz1.size() > 3
3704  || sz1[1] != q || sz2[1] != q) return false;
3705  size_type n1 = (sz1.size() > 2) ? sz1[2] : 1;
3706  size_type n2 = (sz2.size() > 2) ? sz2[2] : 1;
3707  return n1 == n2 && n1*q == n;
3708  }
3709 
3710  pga_instruction ga_instruction_contraction_switch
3711  (assembly_tensor &t_, assembly_tensor &tc1_, assembly_tensor &tc2_,
3712  size_type n, bool &to_clear) {
3713  base_tensor &t = t_.tensor(), &tc1 = tc1_.tensor(), &tc2 = tc2_.tensor();
3714 
3715  if (tc1_.sparsity() == 1 && tc2_.sparsity() == 1 &&
3716  tc1_.qdim() == n && tc2_.qdim() == n) {
3717  to_clear = true;
3718  t_.set_sparsity(10, tc1_.qdim());
3719  return std::make_shared<ga_instruction_contraction_opt1_1>(t, tc1, tc2, n);
3720  }
3721 
3722  if (ga_contraction_is_opt2_2(tc1_, tc2_, n)) {
3723  to_clear = true;
3724  t_.set_sparsity(10, tc1_.qdim());
3725  return std::make_shared<ga_instruction_contraction_opt2_2>
3726  (t, tc1, tc2, tc1_.qdim(), n/tc1_.qdim());
3727  }
3728  {
3729  pga_instruction pgai_masked
3730  = ga_masked_contraction_switch(t_, tc1_, tc2_, n, to_clear);
3731  if (pgai_masked) return pgai_masked;
3732  }
3733 
3734  if (tc2_.sparsity() == 1) {
3735  switch(n) {
3736  case 2:
3737  return std::make_shared<ga_instruction_contraction_opt0_1_unrolled<2>>
3738  (t, tc1, tc2);
3739  case 3:
3740  return std::make_shared<ga_instruction_contraction_opt0_1_unrolled<3>>
3741  (t, tc1, tc2);
3742  case 4:
3743  return std::make_shared<ga_instruction_contraction_opt0_1_unrolled<4>>
3744  (t, tc1, tc2);
3745  case 5:
3746  return std::make_shared<ga_instruction_contraction_opt0_1_unrolled<5>>
3747  (t, tc1, tc2);
3748  default:
3749  return std::make_shared<ga_instruction_contraction_opt0_1>(t,tc1,tc2,n);
3750  }
3751  }
3752  if (tc2_.sparsity() == 2) {
3753  size_type q2 = tc2.sizes()[1];
3754  size_type n2 = (tc2.sizes().size() > 2) ? tc2.sizes()[2] : 1;
3755  if (n2*q2 == n) {
3756  switch (n2) {
3757  case 1:
3758  switch (q2) {
3759  case 2:
3760  return
3761  std::make_shared<ga_instruction_contraction_opt0_2_dunrolled<1,2>>
3762  (t, tc1, tc2);
3763  case 3:
3764  return
3765  std::make_shared<ga_instruction_contraction_opt0_2_dunrolled<1,3>>
3766  (t, tc1, tc2);
3767  case 4:
3768  return
3769  std::make_shared<ga_instruction_contraction_opt0_2_dunrolled<1,4>>
3770  (t, tc1, tc2);
3771  default :
3772  return std::make_shared<ga_instruction_contraction_opt0_2_unrolled<1>>
3773  (t, tc1, tc2, q2);
3774  }
3775  case 2:
3776  switch (q2) {
3777  case 2:
3778  return
3779  std::make_shared<ga_instruction_contraction_opt0_2_dunrolled<2,2>>
3780  (t, tc1, tc2);
3781  case 3:
3782  return
3783  std::make_shared<ga_instruction_contraction_opt0_2_dunrolled<2,3>>
3784  (t, tc1, tc2);
3785  case 4:
3786  return
3787  std::make_shared<ga_instruction_contraction_opt0_2_dunrolled<2,4>>
3788  (t, tc1, tc2);
3789  default :
3790  return std::make_shared<ga_instruction_contraction_opt0_2_unrolled<2>>
3791  (t, tc1, tc2, q2);
3792  }
3793  case 3:
3794  switch (q2) {
3795  case 2:
3796  return
3797  std::make_shared<ga_instruction_contraction_opt0_2_dunrolled<3,2>>
3798  (t, tc1, tc2);
3799  case 3:
3800  return
3801  std::make_shared<ga_instruction_contraction_opt0_2_dunrolled<3,3>>
3802  (t, tc1, tc2);
3803  case 4:
3804  return
3805  std::make_shared<ga_instruction_contraction_opt0_2_dunrolled<3,4>>
3806  (t, tc1, tc2);
3807  default :
3808  return std::make_shared<ga_instruction_contraction_opt0_2_unrolled<3>>
3809  (t, tc1, tc2, q2);
3810  }
3811  case 4:
3812  return std::make_shared<ga_instruction_contraction_opt0_2_unrolled<4>>
3813  (t, tc1, tc2, q2);
3814  case 5:
3815  return std::make_shared<ga_instruction_contraction_opt0_2_unrolled<5>>
3816  (t, tc1, tc2, q2);
3817  default:
3818  return std::make_shared<ga_instruction_contraction_opt0_2>
3819  (t,tc1,tc2,n2,q2);
3820  }
3821  }
3822  }
3823  if (tc1_.sparsity() == 2) {
3824  size_type q1 = tc1.sizes()[1];
3825  size_type n1 = (tc1.sizes().size() > 2) ? tc1.sizes()[2] : 1;
3826  if (n1*q1 == n) {
3827  switch (n1) {
3828  case 1:
3829  switch (q1) {
3830  case 2:
3831  return
3832  std::make_shared<ga_instruction_contraction_opt2_0_dunrolled<1,2>>
3833  (t, tc1, tc2);
3834  case 3:
3835  return
3836  std::make_shared<ga_instruction_contraction_opt2_0_dunrolled<1,3>>
3837  (t, tc1, tc2);
3838  case 4:
3839  return
3840  std::make_shared<ga_instruction_contraction_opt2_0_dunrolled<1,4>>
3841  (t, tc1, tc2);
3842  default :
3843  return std::make_shared<ga_instruction_contraction_opt2_0_unrolled<1>>
3844  (t, tc1, tc2, q1);
3845  }
3846  case 2:
3847  switch (q1) {
3848  case 2:
3849  return
3850  std::make_shared<ga_instruction_contraction_opt2_0_dunrolled<2,2>>
3851  (t, tc1, tc2);
3852  case 3:
3853  return
3854  std::make_shared<ga_instruction_contraction_opt2_0_dunrolled<2,3>>
3855  (t, tc1, tc2);
3856  case 4:
3857  return
3858  std::make_shared<ga_instruction_contraction_opt2_0_dunrolled<2,4>>
3859  (t, tc1, tc2);
3860  default :
3861  return std::make_shared<ga_instruction_contraction_opt2_0_unrolled<2>>
3862  (t, tc1, tc2, q1);
3863  }
3864  case 3:
3865  switch (q1) {
3866  case 2:
3867  return
3868  std::make_shared<ga_instruction_contraction_opt2_0_dunrolled<3,2>>
3869  (t, tc1, tc2);
3870  case 3:
3871  return
3872  std::make_shared<ga_instruction_contraction_opt2_0_dunrolled<3,3>>
3873  (t, tc1, tc2);
3874  case 4:
3875  return
3876  std::make_shared<ga_instruction_contraction_opt2_0_dunrolled<3,4>>
3877  (t, tc1, tc2);
3878  default :
3879  return std::make_shared<ga_instruction_contraction_opt2_0_unrolled<3>>
3880  (t, tc1, tc2, q1);
3881  }
3882  return std::make_shared<ga_instruction_contraction_opt2_0_unrolled<3>>
3883  (t, tc1, tc2, q1);
3884  case 4:
3885  return std::make_shared<ga_instruction_contraction_opt2_0_unrolled<4>>
3886  (t, tc1, tc2, q1);
3887  case 5:
3888  return std::make_shared<ga_instruction_contraction_opt2_0_unrolled<5>>
3889  (t, tc1, tc2, q1);
3890  default:
3891  return std::make_shared<ga_instruction_contraction_opt2_0>
3892  (t,tc1,tc2, n1, q1);
3893  }
3894  }
3895  }
3896 
3897  switch(n) {
3898  case 1 : return std::make_shared<ga_instruction_contraction_unrolled< 1>>
3899  (t, tc1, tc2);
3900  case 2 : return std::make_shared<ga_instruction_contraction_unrolled< 2>>
3901  (t, tc1, tc2);
3902  case 3 : return std::make_shared<ga_instruction_contraction_unrolled< 3>>
3903  (t, tc1, tc2);
3904  case 4 : return std::make_shared<ga_instruction_contraction_unrolled< 4>>
3905  (t, tc1, tc2);
3906  case 5 : return std::make_shared<ga_instruction_contraction_unrolled< 5>>
3907  (t, tc1, tc2);
3908  case 6 : return std::make_shared<ga_instruction_contraction_unrolled< 6>>
3909  (t, tc1, tc2);
3910  // above 6 it is decided inside ga_instruction_contraction::exec() whether
3911  // an unrolled loop or dgemm is used
3912  default : return std::make_shared<ga_instruction_contraction>
3913  (t, tc1, tc2, n);
3914  }
3915  }
3916 
3917  pga_instruction ga_uniform_instruction_contraction_switch
3918  (assembly_tensor &t_, assembly_tensor &tc1_, assembly_tensor &tc2_,
3919  size_type n, bool &to_clear) {
3920  base_tensor &t = t_.tensor(), &tc1 = tc1_.tensor(), &tc2 = tc2_.tensor();
3921 
3922  if (tc1_.sparsity() == 1 && tc2_.sparsity() == 1 &&
3923  tc1_.qdim() == n && tc2_.qdim() == n) {
3924  to_clear = true;
3925  t_.set_sparsity(10, tc1_.qdim());
3926  return std::make_shared<ga_instruction_contraction_opt1_1>(t,tc1,tc2,n);
3927  }
3928 
3929  if (ga_contraction_is_opt2_2(tc1_, tc2_, n)) {
3930  to_clear = true;
3931  t_.set_sparsity(10, tc1_.qdim());
3932  return std::make_shared<ga_instruction_contraction_opt2_2>
3933  (t, tc1, tc2, tc1_.qdim(), n/tc1_.qdim());
3934  }
3935  {
3936  pga_instruction pgai_masked
3937  = ga_masked_contraction_switch(t_, tc1_, tc2_, n, to_clear);
3938  if (pgai_masked) return pgai_masked;
3939  }
3940  if (tc2_.sparsity() == 1) {
3941  switch(n) {
3942  case 2:
3943  return std::make_shared<ga_instruction_contraction_opt0_1_unrolled<2>>
3944  (t, tc1, tc2);
3945  case 3:
3946  return std::make_shared<ga_instruction_contraction_opt0_1_unrolled<3>>
3947  (t, tc1, tc2);
3948  case 4:
3949  return std::make_shared<ga_instruction_contraction_opt0_1_unrolled<4>>
3950  (t, tc1, tc2);
3951  case 5:
3952  return std::make_shared<ga_instruction_contraction_opt0_1_unrolled<5>>
3953  (t, tc1, tc2);
3954  default:
3955  return std::make_shared<ga_instruction_contraction_opt0_1>(t,tc1,tc2, n);
3956  }
3957  }
3958  if (tc2_.sparsity() == 2) {
3959  size_type q2 = tc2.sizes()[1];
3960  size_type n2 = (tc2.sizes().size() > 2) ? tc2.sizes()[2] : 1;
3961  if (n2*q2 == n) {
3962  switch (n2) {
3963  case 1:
3964  switch (q2) {
3965  case 2:
3966  return
3967  std::make_shared<ga_instruction_contraction_opt0_2_dunrolled<1,2>>
3968  (t, tc1, tc2);
3969  case 3:
3970  return
3971  std::make_shared<ga_instruction_contraction_opt0_2_dunrolled<1,3>>
3972  (t, tc1, tc2);
3973  case 4:
3974  return
3975  std::make_shared<ga_instruction_contraction_opt0_2_dunrolled<1,4>>
3976  (t, tc1, tc2);
3977  default :
3978  return std::make_shared<ga_instruction_contraction_opt0_2_unrolled<1>>
3979  (t, tc1, tc2, q2);
3980  }
3981  case 2:
3982  switch (q2) {
3983  case 2:
3984  return
3985  std::make_shared<ga_instruction_contraction_opt0_2_dunrolled<2,2>>
3986  (t, tc1, tc2);
3987  case 3:
3988  return
3989  std::make_shared<ga_instruction_contraction_opt0_2_dunrolled<2,3>>
3990  (t, tc1, tc2);
3991  case 4:
3992  return
3993  std::make_shared<ga_instruction_contraction_opt0_2_dunrolled<2,4>>
3994  (t, tc1, tc2);
3995  default :
3996  return std::make_shared<ga_instruction_contraction_opt0_2_unrolled<2>>
3997  (t, tc1, tc2, q2);
3998  }
3999  case 3:
4000  switch (q2) {
4001  case 2:
4002  return
4003  std::make_shared<ga_instruction_contraction_opt0_2_dunrolled<3,2>>
4004  (t, tc1, tc2);
4005  case 3:
4006  return
4007  std::make_shared<ga_instruction_contraction_opt0_2_dunrolled<3,3>>
4008  (t, tc1, tc2);
4009  case 4:
4010  return
4011  std::make_shared<ga_instruction_contraction_opt0_2_dunrolled<3,4>>
4012  (t, tc1, tc2);
4013  default :
4014  return std::make_shared<ga_instruction_contraction_opt0_2_unrolled<3>>
4015  (t, tc1, tc2, q2);
4016  }
4017  case 4:
4018  return std::make_shared<ga_instruction_contraction_opt0_2_unrolled<4>>
4019  (t, tc1, tc2, q2);
4020  case 5:
4021  return std::make_shared<ga_instruction_contraction_opt0_2_unrolled<5>>
4022  (t, tc1, tc2, q2);
4023  default:
4024  return std::make_shared<ga_instruction_contraction_opt0_2>
4025  (t,tc1,tc2,n2,q2);
4026  }
4027  }
4028  }
4029  if (tc1_.sparsity() == 2) {
4030  size_type q1 = tc1.sizes()[1];
4031  size_type n1 = (tc1.sizes().size() > 2) ? tc1.sizes()[2] : 1;
4032  if (n1*q1 == n) {
4033  switch (n1) {
4034  case 1:
4035  switch (q1) {
4036  case 2:
4037  return
4038  std::make_shared<ga_instruction_contraction_opt2_0_dunrolled<1,2>>
4039  (t, tc1, tc2);
4040  case 3:
4041  return
4042  std::make_shared<ga_instruction_contraction_opt2_0_dunrolled<1,3>>
4043  (t, tc1, tc2);
4044  case 4:
4045  return
4046  std::make_shared<ga_instruction_contraction_opt2_0_dunrolled<1,4>>
4047  (t, tc1, tc2);
4048  default :
4049  return std::make_shared<ga_instruction_contraction_opt2_0_unrolled<1>>
4050  (t, tc1, tc2, q1);
4051  }
4052  case 2:
4053  switch (q1) {
4054  case 2:
4055  return
4056  std::make_shared<ga_instruction_contraction_opt2_0_dunrolled<2,2>>
4057  (t, tc1, tc2);
4058  case 3:
4059  return
4060  std::make_shared<ga_instruction_contraction_opt2_0_dunrolled<2,3>>
4061  (t, tc1, tc2);
4062  case 4:
4063  return
4064  std::make_shared<ga_instruction_contraction_opt2_0_dunrolled<2,4>>
4065  (t, tc1, tc2);
4066  default :
4067  return std::make_shared<ga_instruction_contraction_opt2_0_unrolled<2>>
4068  (t, tc1, tc2, q1);
4069  }
4070  case 3:
4071  switch (q1) {
4072  case 2:
4073  return
4074  std::make_shared<ga_instruction_contraction_opt2_0_dunrolled<3,2>>
4075  (t, tc1, tc2);
4076  case 3:
4077  return
4078  std::make_shared<ga_instruction_contraction_opt2_0_dunrolled<3,3>>
4079  (t, tc1, tc2);
4080  case 4:
4081  return
4082  std::make_shared<ga_instruction_contraction_opt2_0_dunrolled<3,4>>
4083  (t, tc1, tc2);
4084  default :
4085  return std::make_shared<ga_instruction_contraction_opt2_0_unrolled<3>>
4086  (t, tc1, tc2, q1);
4087  }
4088  return std::make_shared<ga_instruction_contraction_opt2_0_unrolled<3>>
4089  (t, tc1, tc2, q1);
4090  case 4:
4091  return std::make_shared<ga_instruction_contraction_opt2_0_unrolled<4>>
4092  (t, tc1, tc2, q1);
4093  case 5:
4094  return std::make_shared<ga_instruction_contraction_opt2_0_unrolled<5>>
4095  (t, tc1, tc2, q1);
4096  default:
4097  return std::make_shared<ga_instruction_contraction_opt2_0>
4098  (t,tc1,tc2, n1, q1);
4099  }
4100  }
4101  }
4102 
4103  // Only specialized for certain values
4104  size_type s2 = tc2.size()/n;
4105  switch(s2) {
4106  case 1 :
4107  switch(n) {
4108  case 2: return std::make_shared<ga_ins_red_d_unrolled<2,1>>(t, tc1, tc2);
4109  case 3: return std::make_shared<ga_ins_red_d_unrolled<3,1>>(t, tc1, tc2);
4110  case 4: return std::make_shared<ga_ins_red_d_unrolled<4,1>>(t, tc1, tc2);
4111  default: return ga_instruction_contraction_switch(t_,tc1_,tc2_,n,to_clear);
4112  }
4113  case 2 :
4114  switch(n) {
4115  case 2: return std::make_shared<ga_ins_red_d_unrolled<2,2>>(t, tc1, tc2);
4116  case 3: return std::make_shared<ga_ins_red_d_unrolled<3,2>>(t, tc1, tc2);
4117  case 4: return std::make_shared<ga_ins_red_d_unrolled<4,2>>(t, tc1, tc2);
4118  default: return ga_instruction_contraction_switch(t_,tc1_,tc2_,n,to_clear);
4119  }
4120  case 3 :
4121  switch(n) {
4122  case 2: return std::make_shared<ga_ins_red_d_unrolled<2,3>>(t, tc1, tc2);
4123  case 3: return std::make_shared<ga_ins_red_d_unrolled<3,3>>(t, tc1, tc2);
4124  case 4: return std::make_shared<ga_ins_red_d_unrolled<4,3>>(t, tc1, tc2);
4125  default: return ga_instruction_contraction_switch(t_,tc1_,tc2_,n,to_clear);
4126  }
4127  case 4 :
4128  switch(n) {
4129  case 2: return std::make_shared<ga_ins_red_d_unrolled<2,4>>(t, tc1, tc2);
4130  case 3: return std::make_shared<ga_ins_red_d_unrolled<3,4>>(t, tc1, tc2);
4131  case 4: return std::make_shared<ga_ins_red_d_unrolled<4,4>>(t, tc1, tc2);
4132  default: return ga_instruction_contraction_switch(t_,tc1_,tc2_,n,to_clear);
4133  }
4134  case 5 :
4135  switch(n) {
4136  case 2: return std::make_shared<ga_ins_red_d_unrolled<2,5>>(t, tc1, tc2);
4137  case 3: return std::make_shared<ga_ins_red_d_unrolled<3,5>>(t, tc1, tc2);
4138  case 4: return std::make_shared<ga_ins_red_d_unrolled<4,5>>(t, tc1, tc2);
4139  default: return ga_instruction_contraction_switch(t_,tc1_,tc2_,n,to_clear);
4140  }
4141  case 6 :
4142  switch(n) {
4143  case 2: return std::make_shared<ga_ins_red_d_unrolled<2,6>>(t, tc1, tc2);
4144  case 3: return std::make_shared<ga_ins_red_d_unrolled<3,6>>(t, tc1, tc2);
4145  case 4: return std::make_shared<ga_ins_red_d_unrolled<4,6>>(t, tc1, tc2);
4146  default: return ga_instruction_contraction_switch(t_,tc1_,tc2_,n,to_clear);
4147  }
4148  case 7 :
4149  switch(n) {
4150  case 2: return std::make_shared<ga_ins_red_d_unrolled<2,7>>(t, tc1, tc2);
4151  case 3: return std::make_shared<ga_ins_red_d_unrolled<3,7>>(t, tc1, tc2);
4152  case 4: return std::make_shared<ga_ins_red_d_unrolled<4,7>>(t, tc1, tc2);
4153  default: return ga_instruction_contraction_switch(t_,tc1_,tc2_,n,to_clear);
4154  }
4155  case 8 :
4156  switch(n) {
4157  case 2: return std::make_shared<ga_ins_red_d_unrolled<2,8>>(t, tc1, tc2);
4158  case 3: return std::make_shared<ga_ins_red_d_unrolled<3,8>>(t, tc1, tc2);
4159  case 4: return std::make_shared<ga_ins_red_d_unrolled<4,8>>(t, tc1, tc2);
4160  default: return ga_instruction_contraction_switch(t_,tc1_,tc2_,n,to_clear);
4161  }
4162  case 9 :
4163  switch(n) {
4164  case 2: return std::make_shared<ga_ins_red_d_unrolled<2,9>>(t, tc1, tc2);
4165  case 3: return std::make_shared<ga_ins_red_d_unrolled<3,9>>(t, tc1, tc2);
4166  case 4: return std::make_shared<ga_ins_red_d_unrolled<4,9>>(t, tc1, tc2);
4167  default: return ga_instruction_contraction_switch(t_,tc1_,tc2_,n,to_clear);
4168  }
4169  case 10:
4170  switch(n) {
4171  case 2: return std::make_shared<ga_ins_red_d_unrolled<2,10>>(t, tc1, tc2);
4172  case 3: return std::make_shared<ga_ins_red_d_unrolled<3,10>>(t, tc1, tc2);
4173  case 4: return std::make_shared<ga_ins_red_d_unrolled<4,10>>(t, tc1, tc2);
4174  default: return ga_instruction_contraction_switch(t_,tc1_,tc2_,n,to_clear);
4175  }
4176  default: return ga_instruction_contraction_switch(t_,tc1_,tc2_,n,to_clear);
4177  }
4178  }
4179 
4180 
4181  // Performs Amij Bnj -> Cmni. To be optimized.
4182  struct ga_instruction_spec_contraction : public ga_instruction {
4183  base_tensor &t;
4184  const base_tensor &tc1, &tc2;
4185  size_type nn;
4186  virtual int exec() {
4187  GA_DEBUG_INFO("Instruction: specific contraction operation of "
4188  "size " << nn);
4189  size_type s1 = tc1.sizes()[0], s11 = tc1.size() / (s1*nn), s111 = s1*s11;
4190  size_type s2 = tc2.sizes()[0];
4191  base_tensor::iterator it = t.begin();
4192  for (size_type i = 0; i < s11; ++i)
4193  for (size_type n = 0; n < s2; ++n)
4194  for (size_type m = 0; m < s1; ++m, ++it) {
4195  *it = scalar_type(0);
4196  for (size_type j = 0; j < nn; ++j)
4197  *it += tc1[m+i*s1+j*s111] * tc2[n+j*s2];
4198  }
4199  GA_DEBUG_ASSERT(it == t.end(), "Wrong sizes");
4200  return 0;
4201  }
4202  ga_instruction_spec_contraction(base_tensor &t_,
4203  const base_tensor &tc1_,
4204  const base_tensor &tc2_, size_type n_)
4205  : t(t_), tc1(tc1_), tc2(tc2_), nn(n_) {}
4206  };
4207 
4208  // Performs Amik Bnjk -> Cmnij. To be optimized.
4209  struct ga_instruction_spec2_contraction : public ga_instruction {
4210  base_tensor &t;
4211  const base_tensor &tc1, &tc2;
4212  size_type nn;
4213  virtual int exec() {
4214  GA_DEBUG_INFO("Instruction: second specific contraction operation of "
4215  "size " << nn);
4216  size_type s1 = tc1.sizes()[0], s11 = tc1.size() / (s1*nn), s111 = s1*s11;
4217  size_type s2 = tc2.sizes()[0], s22 = tc2.size() / (s2*nn), s222 = s2*s22;
4218  base_tensor::iterator it = t.begin();
4219  for (size_type j = 0; j < s22; ++j)
4220  for (size_type i = 0; i < s11; ++i)
4221  for (size_type m = 0; m < s1; ++m)
4222  for (size_type n = 0; n < s2; ++n, ++it) {
4223  *it = scalar_type(0);
4224  for (size_type k = 0; k < nn; ++k)
4225  *it += tc1[m+i*s1+k*s111] * tc2[n+j*s2+k*s222];
4226  }
4227  GA_DEBUG_ASSERT(it == t.end(), "Wrong sizes");
4228  return 0;
4229  }
4230  ga_instruction_spec2_contraction(base_tensor &t_,
4231  const base_tensor &tc1_,
4232  const base_tensor &tc2_, size_type n_)
4233  : t(t_), tc1(tc1_), tc2(tc2_), nn(n_) {}
4234  };
4235 
4236  // Performs Aij Bkl -> Cijkl
4237  struct ga_instruction_simple_tmult : public ga_instruction {
4238  base_tensor &t;
4239  const base_tensor &tc1, &tc2;
4240  virtual int exec() {
4241  GA_DEBUG_INFO("Instruction: simple tensor product");
4242  size_type s1 = tc1.size();
4243  GA_DEBUG_ASSERT(t.size() == s1 * tc2.size(), "Wrong sizes");
4244  base_tensor::const_iterator it2=tc2.cbegin(), it1=tc1.cbegin(), it1end=it1 + s1;
4245  for (base_tensor::iterator it = t.begin(); it != t.end(); ++it) {
4246  *it = *(it2) * (*it1);
4247  if (++it1 == it1end) { it1 = tc1.cbegin(), ++it2; }
4248  }
4249  return 0;
4250  }
4251  ga_instruction_simple_tmult(base_tensor &t_,
4252  const base_tensor &tc1_, const base_tensor &tc2_)
4253  : t(t_), tc1(tc1_), tc2(tc2_) {}
4254  };
4255 
4256 
4257  // Performs Aij Bkl -> Cijkl, partially unrolled version
4258  template<int IJ> struct ga_instruction_simple_tmult_unrolled
4259  : public ga_instruction {
4260  base_tensor &t;
4261  const base_tensor &tc1, &tc2;
4262  virtual int exec() {
4263  size_type KL = tc2.size();
4264  GA_DEBUG_ASSERT(tc1.size() == IJ,
4265  "Wrong sizes " << tc1.size() << " != " << IJ);
4266  GA_DEBUG_INFO("Instruction: simple tensor product, unrolled with "
4267  << IJ << " operations");
4268  GA_DEBUG_ASSERT(t.size() == IJ * KL,
4269  "Wrong sizes " << t.size() << " != " << IJ << "*" << KL);
4270 #if 0 // too slow, how can this be? that's what dger should be good at. (it is slower even without the std::fill overhead)
4271  const BLAS_INT IJ_=BLAS_INT(IJ), KL_=BLAS_INT(KL), INC(1);
4272  const scalar_type one(1);
4273  std::fill(t.begin(), t.end(), scalar_type(0));
4274  gmm::dger_(&IJ_, &KL_, &one, &tc1[0], &INC, &tc2[0], &INC, &(t[0]), &IJ_);
4275 #else
4276  base_tensor::iterator it = t.begin();
4277  base_tensor::const_iterator it2 = tc2.cbegin();
4278  for (size_type kl = 0; kl < KL; ++kl, ++it2) {
4279  base_tensor::const_iterator it1 = tc1.cbegin();
4280  dax__<IJ>(it, it1, *it2);
4281  }
4282  GA_DEBUG_ASSERT(it == t.end(), "Internal error");
4283 #endif
4284  return 0;
4285  }
4286  ga_instruction_simple_tmult_unrolled(base_tensor &t_,
4287  const base_tensor &tc1_,
4288  const base_tensor &tc2_)
4289  : t(t_), tc1(tc1_), tc2(tc2_) {}
4290  };
4291 
4292  pga_instruction ga_uniform_instruction_simple_tmult
4293  (base_tensor &t, const base_tensor &tc1, const base_tensor &tc2) {
4294  switch(tc1.size()) {
4295  case 1 : GMM_ASSERT1(false, "size 1 should not happen");
4296  case 2 : return std::make_shared<ga_instruction_simple_tmult_unrolled< 2>>
4297  (t, tc1, tc2);
4298  case 3 : return std::make_shared<ga_instruction_simple_tmult_unrolled< 3>>
4299  (t, tc1, tc2);
4300  case 4 : return std::make_shared<ga_instruction_simple_tmult_unrolled< 4>>
4301  (t, tc1, tc2);
4302  case 5 : return std::make_shared<ga_instruction_simple_tmult_unrolled< 5>>
4303  (t, tc1, tc2);
4304  case 6 : return std::make_shared<ga_instruction_simple_tmult_unrolled< 6>>
4305  (t, tc1, tc2);
4306  case 7 : return std::make_shared<ga_instruction_simple_tmult_unrolled< 7>>
4307  (t, tc1, tc2);
4308  case 8 : return std::make_shared<ga_instruction_simple_tmult_unrolled< 8>>
4309  (t, tc1, tc2);
4310  case 9 : return std::make_shared<ga_instruction_simple_tmult_unrolled< 9>>
4311  (t, tc1, tc2);
4312  case 10 : return std::make_shared<ga_instruction_simple_tmult_unrolled<10>>
4313  (t, tc1, tc2);
4314  case 11 : return std::make_shared<ga_instruction_simple_tmult_unrolled<11>>
4315  (t, tc1, tc2);
4316  case 12 : return std::make_shared<ga_instruction_simple_tmult_unrolled<12>>
4317  (t, tc1, tc2);
4318  case 13 : return std::make_shared<ga_instruction_simple_tmult_unrolled<13>>
4319  (t, tc1, tc2);
4320  case 14 : return std::make_shared<ga_instruction_simple_tmult_unrolled<14>>
4321  (t, tc1, tc2);
4322  case 15 : return std::make_shared<ga_instruction_simple_tmult_unrolled<15>>
4323  (t, tc1, tc2);
4324  case 16 : return std::make_shared<ga_instruction_simple_tmult_unrolled<16>>
4325  (t, tc1, tc2);
4326  default : return std::make_shared<ga_instruction_simple_tmult>
4327  (t, tc1, tc2);
4328  }
4329  }
4330 
4331 
4332  // Performs Ami Bnj -> Cmnij. To be optimized.
4333  struct ga_instruction_spec_tmult : public ga_instruction {
4334  base_tensor &t;
4335  const base_tensor &tc1, &tc2;
4336  const size_type I, J;
4337  virtual int exec() {
4338  GA_DEBUG_INFO("Instruction: specific tensor product");
4339  GA_DEBUG_ASSERT(t.size() == tc1.size() * tc2.size(), "Wrong sizes");
4340  const size_type M = tc1.size() / I,
4341  N = tc2.size() / J;
4342  auto it = t.begin();
4343 #if 1 // there could be a smarter way to implement this, but this hardcoded version is fast and robust
4344  switch(M) {
4345  case 1:
4346  for (size_type j = 0; j < J; ++j)
4347  for (auto it1 = tc1.cbegin(); it1 != tc1.end(); ++it1)
4348  for (size_type n = 0; n < N; ++n)
4349  *it++ = (*it1) * tc2[n+N*j];
4350  break;
4351  case 2:
4352  for (size_type j = 0; j < J; ++j)
4353  for (size_type i = 0; i < I; ++i)
4354  for (size_type n = 0; n < N; ++n) {
4355  auto it1 = tc1.cbegin() + M*i;
4356  dax__<2>(it, it1, tc2[n+N*j]);
4357  }
4358  break;
4359  case 3:
4360  for (size_type j = 0; j < J; ++j)
4361  for (size_type i = 0; i < I; ++i)
4362  for (size_type n = 0; n < N; ++n) {
4363  auto it1 = tc1.cbegin() + M*i;
4364  dax__<3>(it, it1, tc2[n+N*j]);
4365  }
4366  break;
4367  case 4:
4368  for (size_type j = 0; j < J; ++j)
4369  for (size_type i = 0; i < I; ++i)
4370  for (size_type n = 0; n < N; ++n) {
4371  auto it1 = tc1.cbegin() + M*i;
4372  dax__<4>(it, it1, tc2[n+N*j]);
4373  }
4374  break;
4375  case 5:
4376  for (size_type j = 0; j < J; ++j)
4377  for (size_type i = 0; i < I; ++i)
4378  for (size_type n = 0; n < N; ++n) {
4379  auto it1 = tc1.cbegin() + M*i;
4380  dax__<5>(it, it1, tc2[n+N*j]);
4381  }
4382  break;
4383  case 6:
4384  for (size_type j = 0; j < J; ++j)
4385  for (size_type i = 0; i < I; ++i)
4386  for (size_type n = 0; n < N; ++n) {
4387  auto it1 = tc1.cbegin() + M*i;
4388  dax__<6>(it, it1, tc2[n+N*j]);
4389  }
4390  break;
4391  case 7:
4392  for (size_type j = 0; j < J; ++j)
4393  for (size_type i = 0; i < I; ++i)
4394  for (size_type n = 0; n < N; ++n) {
4395  auto it1 = tc1.cbegin() + M*i;
4396  dax__<7>(it, it1, tc2[n+N*j]);
4397  }
4398  break;
4399  case 8:
4400  for (size_type j = 0; j < J; ++j)
4401  for (size_type i = 0; i < I; ++i)
4402  for (size_type n = 0; n < N; ++n) {
4403  auto it1 = tc1.cbegin() + M*i;
4404  dax__<8>(it, it1, tc2[n+N*j]);
4405  }
4406  break;
4407  default:
4408  const int M1 = int(M)/8;
4409  const int M2 = int(M) - M1*8;
4410  switch(M2) {
4411  case 0:
4412  for (size_type j = 0; j < J; ++j)
4413  for (size_type i = 0; i < I; ++i)
4414  for (size_type n = 0; n < N; ++n) {
4415  auto it1 = tc1.cbegin() + M*i;
4416  for (int mm=0; mm < M1; ++mm)
4417  dax__<8>(it, it1, tc2[n+N*j]);
4418  }
4419  break;
4420  case 1:
4421  for (size_type j = 0; j < J; ++j)
4422  for (size_type i = 0; i < I; ++i)
4423  for (size_type n = 0; n < N; ++n) {
4424  auto it1 = tc1.cbegin() + M*i;
4425  for (int mm=0; mm < M1; ++mm)
4426  dax__<8>(it, it1, tc2[n+N*j]);
4427  dax__<1>(it, it1, tc2[n+N*j]);
4428  }
4429  break;
4430  case 2:
4431  for (size_type j = 0; j < J; ++j)
4432  for (size_type i = 0; i < I; ++i)
4433  for (size_type n = 0; n < N; ++n) {
4434  auto it1 = tc1.cbegin() + M*i;
4435  for (int mm=0; mm < M1; ++mm)
4436  dax__<8>(it, it1, tc2[n+N*j]);
4437  dax__<2>(it, it1, tc2[n+N*j]);
4438  }
4439  break;
4440  case 3:
4441  for (size_type j = 0; j < J; ++j)
4442  for (size_type i = 0; i < I; ++i)
4443  for (size_type n = 0; n < N; ++n) {
4444  auto it1 = tc1.cbegin() + M*i;
4445  for (int mm=0; mm < M1; ++mm)
4446  dax__<8>(it, it1, tc2[n+N*j]);
4447  dax__<3>(it, it1, tc2[n+N*j]);
4448  }
4449  break;
4450  case 4:
4451  for (size_type j = 0; j < J; ++j)
4452  for (size_type i = 0; i < I; ++i)
4453  for (size_type n = 0; n < N; ++n) {
4454  auto it1 = tc1.cbegin() + M*i;
4455  for (int mm=0; mm < M1; ++mm)
4456  dax__<8>(it, it1, tc2[n+N*j]);
4457  dax__<4>(it, it1, tc2[n+N*j]);
4458  }
4459  break;
4460  case 5:
4461  for (size_type j = 0; j < J; ++j)
4462  for (size_type i = 0; i < I; ++i)
4463  for (size_type n = 0; n < N; ++n) {
4464  auto it1 = tc1.cbegin() + M*i;
4465  for (int mm=0; mm < M1; ++mm)
4466  dax__<8>(it, it1, tc2[n+N*j]);
4467  dax__<5>(it, it1, tc2[n+N*j]);
4468  }
4469  break;
4470  case 6:
4471  for (size_type j = 0; j < J; ++j)
4472  for (size_type i = 0; i < I; ++i)
4473  for (size_type n = 0; n < N; ++n) {
4474  auto it1 = tc1.cbegin() + M*i;
4475  for (int mm=0; mm < M1; ++mm)
4476  dax__<8>(it, it1, tc2[n+N*j]);
4477  dax__<6>(it, it1, tc2[n+N*j]);
4478  }
4479  break;
4480  case 7:
4481  for (size_type j = 0; j < J; ++j)
4482  for (size_type i = 0; i < I; ++i)
4483  for (size_type n = 0; n < N; ++n) {
4484  auto it1 = tc1.cbegin() + M*i;
4485  for (int mm=0; mm < M1; ++mm)
4486  dax__<8>(it, it1, tc2[n+N*j]);
4487  dax__<7>(it, it1, tc2[n+N*j]);
4488  }
4489  break;
4490  default:
4491  GMM_ASSERT1(false, "should not happen");
4492  }
4493  }
4494  GA_DEBUG_ASSERT(it == t.end(), "Wrong sizes");
4495 #else // runtime performance of this implementation often affected by totally unrelated changes
4496  // even if it actually compiles to the same assembly instructions
4497  for (size_type j = 0; j < J; ++j)
4498  for (size_type i = 0; i < I; ++i)
4499  for (size_type n = 0; n < N; ++n) {
4500  scalar_type val = tc2[n+N*j];
4501  for (size_type m = 0; m < M; ++m, ++it)
4502  *it = tc1[m+M*i] * val;
4503  }
4504  GA_DEBUG_ASSERT(it == t.end(), "Wrong sizes");
4505 #endif
4506  return 0;
4507  }
4508  ga_instruction_spec_tmult(base_tensor &t_,
4509  const base_tensor &tc1_,
4510  const base_tensor &tc2_,
4511  size_type I_, size_type J_)
4512  : t(t_), tc1(tc1_), tc2(tc2_), I(I_), J(J_) {}
4513  };
4514 
4515  // Performs Ai Bmj -> Cmij. To be optimized.
4516  struct ga_instruction_spec2_tmult : public ga_instruction {
4517  base_tensor &t;
4518  const base_tensor &tc1, &tc2;
4519  virtual int exec() {
4520  GA_DEBUG_INFO("Instruction: second specific tensor product");
4521  GA_DEBUG_ASSERT(t.size() == tc1.size() * tc2.size(), "Wrong sizes");
4522  size_type I = tc1.size();
4523  size_type M = tc2.sizes()[0], J = tc2.size() / M;
4524 
4525  base_tensor::iterator it = t.begin();
4526  for (size_type j = 0; j < J; ++j)
4527  for (size_type i = 0; i < I; ++i)
4528  for (size_type m = 0; m < M; ++m, ++it)
4529  *it = tc1[i] * tc2[m+M*j];
4530  GA_DEBUG_ASSERT(it == t.end(), "Wrong sizes");
4531  return 0;
4532  }
4533  ga_instruction_spec2_tmult(base_tensor &t_,
4534  const base_tensor &tc1_, const base_tensor &tc2_)
4535  : t(t_), tc1(tc1_), tc2(tc2_) {}
4536  };
4537 
4538 
4539 
4540  struct ga_instruction_simple_c_matrix : public ga_instruction {
4541  base_tensor &t;
4542  std::vector<scalar_type *> components;
4543  virtual int exec() {
4544  GA_DEBUG_INFO("Instruction: gathering components for explicit "
4545  "matrix");
4546  GA_DEBUG_ASSERT(t.size() == components.size(), "Wrong sizes");
4547  for (size_type i = 0; i < components.size(); ++i)
4548  t[i] = *(components[i]);
4549  return 0;
4550  }
4551  ga_instruction_simple_c_matrix(base_tensor &t_,
4552  std::vector<scalar_type *> &components_)
4553  : t(t_), components(components_) {}
4554  };
4555 
4556  struct ga_instruction_c_matrix_with_tests : public ga_instruction {
4557  base_tensor &t;
4558  const std::vector<const base_tensor *> components;
4559  virtual int exec() {
4560  GA_DEBUG_INFO("Instruction: gathering components for explicit "
4561  "matrix with tests functions");
4562  size_type s = t.size() / components.size();
4563  GA_DEBUG_ASSERT(s, "Wrong sizes");
4564  base_tensor::iterator it = t.begin();
4565  for (size_type i = 0; i < components.size(); ++i) {
4566  const base_tensor &t1 = *(components[i]);
4567  if (t1.size() > 1) {
4568  GA_DEBUG_ASSERT(t1.size() == s, "Wrong sizes, " << t1.size()
4569  << " != " << s);
4570  for (size_type j = 0; j < s; ++j) *it++ = t1[j];
4571  } else {
4572  for (size_type j = 0; j < s; ++j) *it++ = t1[0];
4573  }
4574  }
4575  return 0;
4576  }
4577  ga_instruction_c_matrix_with_tests
4578  (base_tensor &t_, const std::vector<const base_tensor *> &components_)
4579  : t(t_), components(components_) {}
4580  };
4581 
4582  struct ga_instruction_eval_func_1arg_1res : public ga_instruction {
4583  scalar_type &t;
4584  const scalar_type &c;
4585  pscalar_func_onearg f1;
4586  virtual int exec() {
4587  GA_DEBUG_INFO("Instruction: evaluation of a one argument "
4588  "predefined function on a scalar");
4589  t = (*f1)(c);
4590  return 0;
4591  }
4592  ga_instruction_eval_func_1arg_1res(scalar_type &t_, const scalar_type &c_,
4593  pscalar_func_onearg f1_)
4594  : t(t_), c(c_), f1(f1_) {}
4595  };
4596 
4597  struct ga_instruction_eval_func_1arg_1res_expr : public ga_instruction {
4598  scalar_type &t;
4599  const scalar_type &c;
4600  const ga_predef_function &F;
4601  virtual int exec() {
4602  GA_DEBUG_INFO("Instruction: evaluation of a one argument "
4603  "predefined function on a scalar");
4604  t = F(c);
4605  return 0;
4606  }
4607  ga_instruction_eval_func_1arg_1res_expr(scalar_type &t_,
4608  const scalar_type &c_,
4609  const ga_predef_function &F_)
4610  : t(t_), c(c_), F(F_) {}
4611  };
4612 
4613  struct ga_instruction_eval_func_1arg : public ga_instruction {
4614  base_tensor &t;
4615  const base_tensor &tc1;
4616  pscalar_func_onearg f1;
4617  virtual int exec() {
4618  GA_DEBUG_INFO("Instruction: evaluation of a one argument "
4619  "predefined function on tensor");
4620  GA_DEBUG_ASSERT(t.size() == tc1.size(), "Wrong sizes");
4621  for (size_type i = 0; i < t.size(); ++i)
4622  t[i] = (*f1)(tc1[i]);
4623  return 0;
4624  }
4625  ga_instruction_eval_func_1arg(base_tensor &t_,
4626  const base_tensor &c_, pscalar_func_onearg f1_)
4627  : t(t_), tc1(c_), f1(f1_) {}
4628  };
4629 
4630  struct ga_instruction_eval_func_1arg_expr : public ga_instruction {
4631  base_tensor &t;
4632  const base_tensor &tc1;
4633  const ga_predef_function &F;
4634  virtual int exec() {
4635  GA_DEBUG_INFO("Instruction: evaluation of a one argument "
4636  "predefined function on tensor");
4637  GA_DEBUG_ASSERT(t.size() == tc1.size(), "Wrong sizes");
4638  for (size_type i = 0; i < t.size(); ++i)
4639  t[i] = F(tc1[i]);
4640  return 0;
4641  }
4642  ga_instruction_eval_func_1arg_expr(base_tensor &t_,
4643  const base_tensor &c_,
4644  const ga_predef_function &F_)
4645  : t(t_), tc1(c_), F(F_) {}
4646  };
4647 
4648  struct ga_instruction_eval_func_2arg_1res : public ga_instruction {
4649  scalar_type &t;
4650  const scalar_type &c, &d;
4651  pscalar_func_twoargs f2;
4652  virtual int exec() {
4653  GA_DEBUG_INFO("Instruction: evaluation of a two arguments "
4654  "predefined function on two scalar");
4655  t = (*f2)(c, d);
4656  return 0;
4657  }
4658  ga_instruction_eval_func_2arg_1res(scalar_type &t_, const scalar_type &c_,
4659  const scalar_type &d_,
4660  pscalar_func_twoargs f2_)
4661  : t(t_), c(c_), d(d_), f2(f2_) {}
4662  };
4663 
4664  struct ga_instruction_eval_func_2arg_1res_expr : public ga_instruction {
4665  scalar_type &t;
4666  const scalar_type &c, &d;
4667  const ga_predef_function &F;
4668  virtual int exec() {
4669  GA_DEBUG_INFO("Instruction: evaluation of a two arguments "
4670  "predefined function on two scalar");
4671  t = F(c, d);
4672  return 0;
4673  }
4674  ga_instruction_eval_func_2arg_1res_expr(scalar_type &t_,
4675  const scalar_type &c_,
4676  const scalar_type &d_,
4677  const ga_predef_function &F_)
4678  : t(t_), c(c_), d(d_), F(F_) {}
4679  };
4680 
4681  struct ga_instruction_eval_func_2arg_first_scalar : public ga_instruction {
4682  base_tensor &t;
4683  const base_tensor &tc1, &tc2;
4684  pscalar_func_twoargs f2;
4685  virtual int exec() {
4686  GA_DEBUG_INFO("Instruction: evaluation of a two arguments "
4687  "predefined function on one scalar and one tensor");
4688  GA_DEBUG_ASSERT(t.size() == tc2.size(), "Wrong sizes");
4689  for (size_type i = 0; i < t.size(); ++i)
4690  t[i] = (*f2)(tc1[0], tc2[i]);
4691  return 0;
4692  }
4693  ga_instruction_eval_func_2arg_first_scalar(base_tensor &t_,
4694  const base_tensor &c_,
4695  const base_tensor &d_,
4696  pscalar_func_twoargs f2_)
4697  : t(t_), tc1(c_), tc2(d_), f2(f2_) {}
4698  };
4699 
4700  struct ga_instruction_eval_func_2arg_first_scalar_expr
4701  : public ga_instruction {
4702  base_tensor &t;
4703  const base_tensor &tc1, &tc2;
4704  const ga_predef_function &F;
4705  virtual int exec() {
4706  GA_DEBUG_INFO("Instruction: evaluation of a two arguments "
4707  "predefined function on one scalar and one tensor");
4708  GA_DEBUG_ASSERT(t.size() == tc2.size(), "Wrong sizes");
4709  for (size_type i = 0; i < t.size(); ++i)
4710  t[i] = F(tc1[0], tc2[i]);
4711  return 0;
4712  }
4713  ga_instruction_eval_func_2arg_first_scalar_expr(base_tensor &t_,
4714  const base_tensor &c_,
4715  const base_tensor &d_,
4716  const ga_predef_function &F_)
4717  : t(t_), tc1(c_), tc2(d_), F(F_) {}
4718  };
4719 
4720  struct ga_instruction_eval_func_2arg_second_scalar : public ga_instruction {
4721  base_tensor &t;
4722  const base_tensor &tc1, &tc2;
4723  pscalar_func_twoargs f2;
4724  virtual int exec() {
4725  GA_DEBUG_INFO("Instruction: evaluation of a two arguments "
4726  "predefined function on one tensor and one scalar");
4727  GA_DEBUG_ASSERT(t.size() == tc1.size(), "Wrong sizes");
4728  for (size_type i = 0; i < t.size(); ++i)
4729  t[i] = (*f2)(tc1[i], tc2[0]);
4730  return 0;
4731  }
4732  ga_instruction_eval_func_2arg_second_scalar(base_tensor &t_,
4733  const base_tensor &c_,
4734  const base_tensor &d_,
4735  pscalar_func_twoargs f2_)
4736  : t(t_), tc1(c_), tc2(d_), f2(f2_) {}
4737  };
4738 
4739  struct ga_instruction_eval_func_2arg_second_scalar_expr
4740  : public ga_instruction {
4741  base_tensor &t;
4742  const base_tensor &tc1, &tc2;
4743  const ga_predef_function &F;
4744  virtual int exec() {
4745  GA_DEBUG_INFO("Instruction: evaluation of a two arguments "
4746  "predefined function on one tensor and one scalar");
4747  GA_DEBUG_ASSERT(t.size() == tc1.size(), "Wrong sizes");
4748  for (size_type i = 0; i < t.size(); ++i)
4749  t[i] = F(tc1[i], tc2[0]);
4750  return 0;
4751  }
4752  ga_instruction_eval_func_2arg_second_scalar_expr(base_tensor &t_,
4753  const base_tensor &c_,
4754  const base_tensor &d_,
4755  const ga_predef_function &F_)
4756  : t(t_), tc1(c_), tc2(d_), F(F_) {}
4757  };
4758 
4759  struct ga_instruction_eval_func_2arg : public ga_instruction {
4760  base_tensor &t;
4761  const base_tensor &tc1, &tc2;
4762  pscalar_func_twoargs f2;
4763  virtual int exec() {
4764  GA_DEBUG_INFO("Instruction: evaluation of a two arguments "
4765  "predefined function on two tensors");
4766  GA_DEBUG_ASSERT(t.size() == tc1.size() && t.size() == tc2.size(),
4767  "Wrong sizes");
4768  for (size_type i = 0; i < t.size(); ++i)
4769  t[i] = (*f2)(tc1[i], tc2[i]);
4770  return 0;
4771  }
4772  ga_instruction_eval_func_2arg(base_tensor &t_,
4773  const base_tensor &c_,
4774  const base_tensor &d_, pscalar_func_twoargs f2_)
4775  : t(t_), tc1(c_), tc2(d_), f2(f2_) {}
4776  };
4777 
4778  struct ga_instruction_eval_func_2arg_expr : public ga_instruction {
4779  base_tensor &t;
4780  const base_tensor &tc1, &tc2;
4781  const ga_predef_function &F;
4782  virtual int exec() {
4783  GA_DEBUG_INFO("Instruction: evaluation of a two arguments "
4784  "predefined function on two tensors");
4785  GA_DEBUG_ASSERT(t.size() == tc1.size() && t.size() == tc2.size(),
4786  "Wrong sizes");
4787  for (size_type i = 0; i < t.size(); ++i)
4788  t[i] = F(tc1[i], tc2[i]);
4789  return 0;
4790  }
4791  ga_instruction_eval_func_2arg_expr(base_tensor &t_,
4792  const base_tensor &c_,
4793  const base_tensor &d_,
4794  const ga_predef_function &F_)
4795  : t(t_), tc1(c_), tc2(d_), F(F_) {}
4796  };
4797 
4798  struct ga_instruction_eval_OP : public ga_instruction {
4799  base_tensor &t;
4800  const ga_nonlinear_operator &OP;
4801  ga_nonlinear_operator::arg_list args;
4802  virtual int exec() {
4803  GA_DEBUG_INFO("Instruction: operator evaluation");
4804  OP.value(args, t);
4805  return 0;
4806  }
4807  ga_instruction_eval_OP(base_tensor &t_, const ga_nonlinear_operator &OP_,
4808  ga_nonlinear_operator::arg_list &args_)
4809  : t(t_), OP(OP_), args(args_) {}
4810  };
4811 
4812  struct ga_instruction_eval_derivative_OP : public ga_instruction {
4813  base_tensor &t;
4814  const ga_nonlinear_operator &OP;
4815  ga_nonlinear_operator::arg_list args;
4816  size_type der1;
4817  virtual int exec() {
4818  GA_DEBUG_INFO("Instruction: operator derivative evaluation");
4819  OP.derivative(args, der1, t);
4820  return 0;
4821  }
4822  ga_instruction_eval_derivative_OP(base_tensor &t_,
4823  const ga_nonlinear_operator &OP_,
4824  ga_nonlinear_operator::arg_list &args_,
4825  size_type der1_)
4826  : t(t_), OP(OP_), args(args_), der1(der1_) {}
4827  };
4828 
4829  struct ga_instruction_eval_second_derivative_OP : public ga_instruction {
4830  base_tensor &t;
4831  const ga_nonlinear_operator &OP;
4832  ga_nonlinear_operator::arg_list args;
4833  size_type der1, der2;
4834  virtual int exec() {
4835  GA_DEBUG_INFO("Instruction: operator second derivative evaluation");
4836  OP.second_derivative(args, der1, der2, t);
4837  return 0;
4838  }
4839  ga_instruction_eval_second_derivative_OP
4840  (base_tensor &t_, const ga_nonlinear_operator &OP_,
4841  ga_nonlinear_operator::arg_list &args_, size_type der1_, size_type der2_)
4842  : t(t_), OP(OP_), args(args_), der1(der1_), der2(der2_) {}
4843  };
4844 
4845  struct ga_instruction_tensor_slice : public ga_instruction {
4846  base_tensor &t;
4847  const base_tensor &tc1;
4848  bgeot::multi_index mi, indices;
4849  virtual int exec() {
4850  GA_DEBUG_INFO("Instruction: tensor slice");
4851  size_type order = t.sizes().size();
4852  for (bgeot::multi_index mi3(order); !mi3.finished(t.sizes());
4853  mi3.incrementation(t.sizes())) {
4854  for (size_type j = 0; j < order; ++j)
4855  mi[indices[j]] = mi3[j];
4856  t(mi3) = tc1(mi);
4857  }
4858  return 0;
4859  }
4860  ga_instruction_tensor_slice(base_tensor &t_,
4861  const base_tensor &tc1_,
4862  bgeot::multi_index &mi_,
4863  bgeot::multi_index &indices_)
4864  : t(t_), tc1(tc1_), mi(mi_), indices(indices_) {}
4865  };
4866 
4867  struct ga_instruction_transformation_call : public ga_instruction {
4868  const ga_workspace &workspace;
4869  ga_instruction_set::interpolate_info &inin;
4870  pinterpolate_transformation trans;
4871  fem_interpolation_context &ctx;
4872  const base_small_vector &Normal;
4873  const mesh &m;
4874  bool compute_der;
4875 
4876  virtual int exec() {
4877  GA_DEBUG_INFO("Instruction: call interpolate transformation");
4878  base_node P_ref;
4879  size_type cv;
4880  short_type face_num;
4881  inin.pt_type = trans->transform(workspace, m, ctx, Normal, &(inin.m), cv,
4882  face_num, P_ref, inin.Normal,
4883  inin.derivatives, compute_der);
4884  if (inin.pt_type) {
4885  if (cv != size_type(-1)) {
4886  inin.m->points_of_convex(cv, inin.G);
4887  inin.ctx.change((inin.m)->trans_of_convex(cv),
4888  0, P_ref, inin.G, cv, face_num);
4889  inin.has_ctx = true;
4890  if (face_num != short_type(-1)) {
4891  inin.Normal = bgeot::compute_normal(inin.ctx, face_num);
4892  gmm::scale(inin.Normal, 1.0/gmm::vect_norm2(inin.Normal));
4893  } else // unit normal of a curve in 2D or a surface in 3D
4894  inin.Normal = bgeot::compute_unit_element_normal(inin.ctx);
4895  inin.pt_y = inin.ctx.xreal();
4896  } else {
4897  inin.ctx.invalid_convex_num();
4898  inin.Normal.resize(0);
4899  inin.pt_y = P_ref;
4900  inin.has_ctx = false;
4901  }
4902  } else {
4903  inin.ctx.invalid_convex_num();
4904  inin.Normal.resize(0);
4905  inin.pt_y.resize(0);
4906  inin.has_ctx = false;
4907  }
4908  GA_DEBUG_INFO("Instruction: end of call interpolate transformation");
4909  return 0;
4910  }
4911  ga_instruction_transformation_call
4912  (const ga_workspace &w, ga_instruction_set::interpolate_info &i,
4913  pinterpolate_transformation t, fem_interpolation_context &ctxx,
4914  const base_small_vector &No, const mesh &mm, bool compute_der_)
4915  : workspace(w), inin(i), trans(t), ctx(ctxx), Normal(No), m(mm),
4916  compute_der(compute_der_) {}
4917  };
4918 
4919  struct ga_instruction_neighbor_transformation_call : public ga_instruction {
4920  const ga_workspace &workspace;
4921  ga_instruction_set::interpolate_info &inin;
4922  pinterpolate_transformation trans;
4923  fem_interpolation_context &ctx;
4924  base_small_vector dummy_normal;
4925  const mesh &m;
4926  size_type &ipt;
4927  papprox_integration &pai;
4929  std::map<gauss_pt_corresp, bgeot::pstored_point_tab> &neighbor_corresp;
4930 
4931  virtual int exec() {
4932  bool cancel_optimization = false;
4933  GA_DEBUG_INFO("Instruction: call interpolate neighbor transformation");
4934  if (ipt == 0) {
4935  if (!(ctx.have_pgp()) || !pai || pai->is_built_on_the_fly()
4936  || cancel_optimization) {
4937  inin.ctx.invalid_convex_num();
4938  } else {
4939  // Test if the situation has already been encountered
4940  size_type cv = ctx.convex_num();
4941  short_type f = ctx.face_num();
4942  auto adj_face = m.adjacent_face(cv, f);
4943  if (adj_face.cv == size_type(-1)) {
4944  GMM_WARNING2("Adjacent face not found, "
4945  "probably an non-interior face");
4946  inin.ctx.invalid_convex_num();
4947  } else {
4948  gauss_pt_corresp gpc;
4949  gpc.pgt1 = m.trans_of_convex(cv);
4950  gpc.pgt2 = m.trans_of_convex(adj_face.cv);
4951  gpc.pai = pai;
4952  auto inds_pt1 = m.ind_points_of_face_of_convex(cv, f);
4953  auto inds_pt2 = m.ind_points_of_face_of_convex(adj_face.cv,
4954  adj_face.f);
4955  auto str1 = gpc.pgt1->structure();
4956  auto str2 = gpc.pgt2->structure();
4957  size_type nbptf1 = str1->nb_points_of_face(f);
4958  size_type nbptf2 = str2->nb_points_of_face(adj_face.f);
4959  gpc.nodes.resize(nbptf1*2);
4960  for (size_type i = 0; i < nbptf1; ++i) {
4961  gpc.nodes[2*i] = str1->ind_points_of_face(f)[i];
4962  bool found = false;
4963  for (size_type j = 0; j < nbptf2; ++j) {
4964  if (inds_pt2[j] == inds_pt1[i]) {
4965  gpc.nodes[2*i+1] = str2->ind_points_of_face(adj_face.f)[j];
4966  found = true;
4967  break;
4968  }
4969  }
4970  GMM_ASSERT1(found, "Internal error");
4971  }
4972  bgeot::pstored_point_tab pspt = 0;
4973  auto itm = neighbor_corresp.find(gpc);
4974  if (itm != neighbor_corresp.end()) {
4975  pspt = itm->second;
4976  } else {
4977  size_type nbpt = pai->nb_points_on_face(f);
4979  gic.init(m.points_of_convex(adj_face.cv), gpc.pgt2);
4980  size_type first_ind = pai->ind_first_point_on_face(f);
4982  &spt = *(pai->pintegration_points());
4983  base_matrix G;
4984  m.points_of_convex(cv, G);
4985  fem_interpolation_context ctx_x(gpc.pgt1, 0, spt[0], G, cv, f);
4986  std::vector<base_node> P_ref(nbpt);
4987 
4988  for (size_type i = 0; i < nbpt; ++i) {
4989  ctx_x.set_xref(spt[first_ind+i]);
4990  bool converged = true;
4991  gic.invert(ctx_x.xreal(), P_ref[i], converged);
4992  bool is_in = (gpc.pgt2->convex_ref()->is_in(P_ref[i]) < 1E-4);
4993  GMM_ASSERT1(is_in && converged,"Geometric transformation "
4994  "inversion has failed in neighbor transformation");
4995  }
4996  pspt = store_point_tab(P_ref);
4997  neighbor_corresp[gpc] = pspt;
4998  }
4999  m.points_of_convex(adj_face.cv, inin.G);
5000  bgeot::pgeotrans_precomp pgp = gp_pool(gpc.pgt2, pspt);
5001  inin.ctx.change(pgp, 0, 0, inin.G, adj_face.cv, adj_face.f);
5002  }
5003  }
5004  }
5005 
5006  if (inin.ctx.have_pgp() && inin.ctx.is_convex_num_valid()) {
5007  inin.ctx.set_ii(ipt);
5008  inin.pt_type = 1;
5009  inin.has_ctx = true;
5010  inin.pt_y = inin.ctx.xreal();
5011  inin.Normal = bgeot::compute_normal(inin.ctx, inin.ctx.face_num());
5012  gmm::scale(inin.Normal, 1.0/gmm::vect_norm2(inin.Normal));
5013  inin.m = &m;
5014  } else {
5015  base_node P_ref;
5016  size_type cv;
5017  short_type face_num;
5018  gmm::clear(inin.Normal);
5019  inin.pt_type = trans->transform(workspace, m, ctx, dummy_normal,
5020  &(inin.m), cv, face_num, P_ref,
5021  dummy_normal, inin.derivatives,
5022  false);
5023  if (inin.pt_type) {
5024  if (cv != size_type(-1)) {
5025  inin.m->points_of_convex(cv, inin.G);
5026  inin.ctx.change((inin.m)->trans_of_convex(cv),
5027  0, P_ref, inin.G, cv, face_num);
5028  inin.has_ctx = true;
5029  if (face_num != short_type(-1)) {
5030  inin.Normal = bgeot::compute_normal(inin.ctx, face_num);
5031  gmm::scale(inin.Normal, 1.0/gmm::vect_norm2(inin.Normal));
5032  } else // unit normal of a curve in 2D or a surface in 3D
5033  inin.Normal = bgeot::compute_unit_element_normal(inin.ctx);
5034  inin.pt_y = inin.ctx.xreal();
5035  } else {
5036  inin.ctx.invalid_convex_num();
5037  inin.pt_y = P_ref;
5038  inin.has_ctx = false;
5039  }
5040  } else {
5041  inin.ctx.invalid_convex_num();
5042  inin.Normal.resize(0);
5043  inin.pt_y.resize(0);
5044  inin.has_ctx = false;
5045  }
5046  }
5047  GA_DEBUG_INFO("Instruction: end of call neighbor interpolate "
5048  "transformation");
5049  return 0;
5050  }
5051  ga_instruction_neighbor_transformation_call
5052  (const ga_workspace &w, ga_instruction_set::interpolate_info &i,
5053  pinterpolate_transformation t, fem_interpolation_context &ctxx,
5054  const mesh &mm, size_type &ipt_, papprox_integration &pai_,
5055  bgeot::geotrans_precomp_pool &gp_pool_,
5056  std::map<gauss_pt_corresp, bgeot::pstored_point_tab> &neighbor_corresp_)
5057  : workspace(w), inin(i), trans(t), ctx(ctxx), m(mm),
5058  ipt(ipt_), pai(pai_), gp_pool(gp_pool_),
5059  neighbor_corresp(neighbor_corresp_) {}
5060  };
5061 
5062 
5063  struct ga_instruction_scalar_assembly : public ga_instruction {
5064  const base_tensor &t;
5065  scalar_type &E, &coeff;
5066  virtual int exec() {
5067  GA_DEBUG_INFO("Instruction: scalar term assembly");
5068  E += t[0] * coeff;
5069  return 0;
5070  }
5071  ga_instruction_scalar_assembly(const base_tensor &t_, scalar_type &E_,
5072  scalar_type &coeff_)
5073  : t(t_), E(E_), coeff(coeff_) {}
5074  };
5075 
5076  struct ga_instruction_vector_assembly_mf : public ga_instruction
5077  {
5078  const base_tensor &t;
5079  base_vector &VI, &Vi;
5080  const fem_interpolation_context &ctx;
5081  const gmm::sub_interval *const&I, *const I__;
5082  const mesh_fem *const&mf, *const mf__;
5083  const bool &reduced_mf;
5084  const scalar_type &coeff;
5085  const size_type &nbpt, &ipt;
5086  base_vector elem;
5087  const bool interpolate;
5088  virtual int exec() {
5089  GA_DEBUG_INFO("Instruction: vector term assembly for fem variable");
5090  bool empty_weight = (coeff == scalar_type(0));
5091  if (ipt == 0 || interpolate) {
5092  if (empty_weight) elem.resize(0);
5093  elem.resize(t.size());
5094  if (!empty_weight)
5095  copy_scaled_4(t, coeff, elem);
5096  } else if (!empty_weight)
5097  // gmm::add(gmm::scaled(t.as_vector(), coeff), elem);
5098  add_scaled_4(t, coeff, elem);
5099 
5100  if (ipt == nbpt-1 || interpolate) { // finalize
5101  GA_DEBUG_ASSERT(mf, "Internal error");
5102  if (!ctx.is_convex_num_valid()) return 0;
5103  size_type cv_1 = ctx.convex_num();
5104  size_type qmult = mf->get_qdim();
5105  if (qmult > 1) qmult /= mf->fem_of_element(cv_1)->target_dim();
5106  base_vector &V = reduced_mf ? Vi : VI;
5107  GA_DEBUG_ASSERT(V.size() >= I->first() + mf->nb_basic_dof(),
5108  "Bad assembly vector size " << V.size() << ">=" <<
5109  I->first() << "+"<< mf->nb_basic_dof());
5110  auto itr = elem.cbegin();
5111  auto itw = V.begin() + I->first();
5112  for (const auto &dof : mf->ind_scalar_basic_dof_of_element(cv_1))
5113  for (size_type q = 0; q < qmult; ++q)
5114  *(itw+dof+q) += *itr++;
5115  GMM_ASSERT1(itr == elem.end(), "Internal error");
5116  }
5117  return 0;
5118  }
5119 
5120  ga_instruction_vector_assembly_mf
5121  (const base_tensor &t_, base_vector &VI_, base_vector &Vi_,
5122  const fem_interpolation_context &ctx_,
5123  const gmm::sub_interval *&I_, const mesh_fem *&mf_,
5124  const bool &reduced_mf_,
5125  const scalar_type &coeff_, const size_type &nbpt_, const size_type &ipt_,
5126  bool interpolate_)
5127  : t(t_), VI(VI_), Vi(Vi_), ctx(ctx_),
5128  I(I_), I__(nullptr), mf(mf_), mf__(nullptr), reduced_mf(reduced_mf_),
5129  coeff(coeff_), nbpt(nbpt_), ipt(ipt_), interpolate(interpolate_) {}
5130 
5131  ga_instruction_vector_assembly_mf
5132  (const base_tensor &t_, base_vector &V_,
5133  const fem_interpolation_context &ctx_,
5134  const gmm::sub_interval &I_, const mesh_fem &mf_,
5135  const scalar_type &coeff_, const size_type &nbpt_, const size_type &ipt_,
5136  bool interpolate_)
5137  : t(t_), VI(V_), Vi(V_), ctx(ctx_),
5138  I(I__), I__(&I_), mf(mf__), mf__(&mf_), reduced_mf(false_),
5139  coeff(coeff_), nbpt(nbpt_), ipt(ipt_), interpolate(interpolate_) {}
5140  protected:
5141  const bool false_=false;
5142  };
5143 
5144  struct ga_instruction_vector_assembly_imd : public ga_instruction {
5145  const base_tensor &t;
5146  base_vector &V;
5147  const fem_interpolation_context &ctx;
5148  const gmm::sub_interval &I;
5149  const im_data &imd;
5150  scalar_type &coeff;
5151  const size_type &ipt;
5152  const bool initialize;
5153  virtual int exec() {
5154  GA_DEBUG_INFO("Instruction: vector term assembly for im_data variable");
5155  size_type cv = ctx.convex_num();
5156  size_type i = t.size() * imd.filtered_index_of_point(cv, ctx.ii());
5157  GMM_ASSERT1(i+t.size() <= I.size(),
5158  "Internal error "<<i<<"+"<<t.size()<<" <= "<<I.size());
5159  auto itw = V.begin() + I.first() + i;
5160  if (initialize)
5161  for (const auto &val : t.as_vector())
5162  *itw++ = coeff*val;
5163  else
5164  for (const auto &val : t.as_vector())
5165  *itw++ += coeff*val;
5166  return 0;
5167  }
5168  ga_instruction_vector_assembly_imd
5169  (const base_tensor &t_, base_vector &V_,
5170  const fem_interpolation_context &ctx_, const gmm::sub_interval &I_,
5171  const im_data &imd_, scalar_type &coeff_, const size_type &ipt_,
5172  bool initialize_=false)
5173  : t(t_), V(V_), ctx(ctx_), I(I_), imd(imd_), coeff(coeff_), ipt(ipt_),
5174  initialize(initialize_)
5175  {}
5176  };
5177 
5178  struct ga_instruction_vector_assembly : public ga_instruction {
5179  const base_tensor &t;
5180  base_vector &V;
5181  const gmm::sub_interval &I;
5182  scalar_type &coeff;
5183  virtual int exec() {
5184  GA_DEBUG_INFO("Instruction: vector term assembly for "
5185  "fixed size variable");
5186  gmm::add(gmm::scaled(t.as_vector(), coeff), gmm::sub_vector(V, I));
5187  return 0;
5188  }
5189  ga_instruction_vector_assembly(const base_tensor &t_, base_vector &V_,
5190  const gmm::sub_interval &I_,
5191  scalar_type &coeff_)
5192  : t(t_), V(V_), I(I_), coeff(coeff_) {}
5193  };
5194 
5195  struct ga_instruction_assignment : public ga_instruction {
5196  const base_tensor &t;
5197  base_vector &V;
5198  const fem_interpolation_context &ctx;
5199  const im_data *imd;
5200  virtual int exec() {
5201  GA_DEBUG_INFO("Instruction: Assignment to im_data");
5202  imd->set_tensor(V, ctx.convex_num(), ctx.ii(), t);
5203  return 0;
5204  }
5205  ga_instruction_assignment(const base_tensor &t_, base_vector &V_,
5206  const fem_interpolation_context &ctx_,
5207  const im_data *imd_)
5208  : t(t_), V(V_), ctx(ctx_), imd(imd_) {}
5209  };
5210 
5211  struct ga_instruction_extract_residual_on_imd_dofs : public ga_instruction {
5212  base_tensor &t;
5213  const base_vector &V;
5214  const fem_interpolation_context &ctx;
5215  const gmm::sub_interval &I;
5216  const im_data &imd;
5217  const size_type &ipt;
5218  virtual int exec() {
5219  GA_DEBUG_INFO("Instruction: extract residual for im_data variable");
5220  size_type ifirst = I.first();
5221  size_type cv = ctx.convex_num();
5222  size_type i = t.size() * imd.filtered_index_of_point(cv, ctx.ii());
5223  GMM_ASSERT1(i+t.size() <= I.size(),
5224  "Internal error "<<i<<"+"<<t.size()<<" <= "<<I.size());
5225  for (auto &&val : t.as_vector())
5226  val = V[ifirst+(i++)];
5227  return 0;
5228  }
5229  ga_instruction_extract_residual_on_imd_dofs
5230  (base_tensor &t_, const base_vector &V_,
5231  const fem_interpolation_context &ctx_, const gmm::sub_interval &I_,
5232  const im_data &imd_, const size_type &ipt_)
5233  : t(t_), V(V_), ctx(ctx_), I(I_), imd(imd_), ipt(ipt_)
5234  {}
5235  };
5236 
5237 
5238  template <class MAT>
5239  inline void add_elem_matrix
5240  (MAT &K, const std::vector<size_type> &dofs1,
5241  const std::vector<size_type> &dofs2, std::vector<size_type> &/*dofs1_sort*/,
5242  const base_vector &elem, scalar_type threshold, size_type /* N */) {
5243 
5244  base_vector::const_iterator it = elem.cbegin();
5245  for (const size_type &dof2 : dofs2)
5246  for (const size_type &dof1 : dofs1) {
5247  if (gmm::abs(*it) > threshold)
5248  K(dof1, dof2) += *it;
5249  ++it;
5250  }
5251  }
5252 
5253  // static const std::vector<size_type> *the_indto_sort;
5254  // int compare_my_indices(const void *a, const void *b) {
5255  // size_type aa = *((const size_type *)(a));
5256  // size_type bb = *((const size_type *)(b));
5257  // return int((*the_indto_sort)[aa]) - int((*the_indto_sort)[bb]);
5258  // }
5259 
5260  inline void add_elem_matrix
5261  (gmm::col_matrix<gmm::rsvector<scalar_type>> &K,
5262  const std::vector<size_type> &dofs1, const std::vector<size_type> &dofs2,
5263  std::vector<size_type> &dofs1_sort,
5264  const base_vector &elem, scalar_type threshold, size_type N) {
5265 
5266  size_type s1 = dofs1.size();
5267 
5268  dofs1_sort.resize(s1);
5269  for (size_type i = 0; i < s1; ++i) { // insertion sort
5270  size_type j = i, k = j-1;
5271  while (j > 0 && dofs1[i] < dofs1[dofs1_sort[k]])
5272  { dofs1_sort[j] = dofs1_sort[k]; j--; k--; }
5273  dofs1_sort[j] = i;
5274  }
5275 
5276  // dofs1_sort.resize(s1); // test with qsort: not faster in the tested cases
5277  // for (size_type i = 0; i < s1; ++i) dofs1_sort[i] = i;
5278  // the_indto_sort = &dofs1;
5279  // qsort(&(dofs1_sort[0]), s1, sizeof(size_type), compare_my_indices);
5280 
5281  gmm::elt_rsvector_<scalar_type> ev;
5282 
5283  size_type maxest = (N+1) * s1;
5284  base_vector::const_iterator it = elem.cbegin();
5285  bool first(true);
5286  for (const size_type &dof2 : dofs2) { // Iteration on columns
5287  if (first) first = false;
5288  else it += s1;
5289  std::vector<gmm::elt_rsvector_<scalar_type>> &col = K[dof2];
5290  size_type nb = col.size();
5291 
5292  if (nb == 0) {
5293  col.reserve(maxest);
5294  for (size_type k : dofs1_sort) {
5295  ev.e = *(it+k);
5296  if (gmm::abs(ev.e) > threshold) {
5297  ev.c=dofs1[k];
5298  col.push_back(ev);
5299  }
5300  }
5301  } else { // column merge
5302  size_type ind = 0;
5303  for (size_type k : dofs1_sort) {
5304  ev.e = *(it+k);
5305  if (gmm::abs(ev.e) > threshold) {
5306  ev.c = dofs1[k];
5307 
5308  size_type count = nb - ind, step, l;
5309  while (count > 0) {
5310  step = count / 2;
5311  l = ind + step;
5312  if (col[l].c < ev.c) {
5313  ind = ++l;
5314  count -= step + 1;
5315  }
5316  else
5317  count = step;
5318  }
5319 
5320  auto itc = col.begin() + ind;
5321  if (ind != nb && itc->c == ev.c)
5322  itc->e += ev.e;
5323  else {
5324  if (nb - ind > 1300)
5325  GMM_WARNING2("Inefficient addition of element in rsvector with "
5326  << col.size() - ind << " non-zero entries");
5327  col.push_back(ev);
5328  if (ind != nb) {
5329  itc = col.begin() + ind;
5330  auto ite = col.end();
5331  --ite;
5332  auto itee = ite;
5333  for (; ite != itc; --ite) { --itee; *ite = *itee; }
5334  *itc = ev;
5335  }
5336  ++nb;
5337  }
5338  ++ind;
5339  }
5340  }
5341  }
5342  }
5343  }
5344 
5345 
5346  inline void add_elem_matrix_contiguous_rows
5347  (gmm::col_matrix<gmm::rsvector<scalar_type>> &K,
5348  const size_type &i1, const size_type &s1,
5349  const std::vector<size_type> &dofs2,
5350  const base_vector &elem, scalar_type threshold) {
5351 
5352  gmm::elt_rsvector_<scalar_type> ev;
5353 
5354  base_vector::const_iterator it = elem.cbegin();
5355  bool first(true);
5356  for (const size_type &dof2 : dofs2) { // Iteration on columns
5357  if (first) first = false;
5358  else it += s1;
5359  std::vector<gmm::elt_rsvector_<scalar_type>> &col = K[dof2];
5360  size_type nb = col.size();
5361 
5362  if (nb == 0) {
5363  col.reserve(s1);
5364  for (size_type i = 0; i < s1; ++i) {
5365  ev.e = *(it+i);
5366  if (gmm::abs(ev.e) > threshold) {
5367  ev.c = i1 + i;
5368  col.push_back(ev);
5369  }
5370  }
5371  } else { // column merge (can be optimized for a contiguous range)
5372  size_type ind = 0;
5373  for (size_type i = 0; i < s1; ++i) {
5374  ev.e = *(it+i);
5375  if (gmm::abs(ev.e) > threshold) {
5376  ev.c = i1 + i;
5377 
5378  size_type count = nb - ind, step, l;
5379  while (count > 0) {
5380  step = count / 2;
5381  l = ind + step;
5382  if (col[l].c < ev.c) {
5383  ind = ++l;
5384  count -= step + 1;
5385  }
5386  else
5387  count = step;
5388  }
5389 
5390  auto itc = col.begin() + ind;
5391  if (ind != nb && itc->c == ev.c)
5392  itc->e += ev.e;
5393  else {
5394  if (nb - ind > 1300)
5395  GMM_WARNING2("Inefficient addition of element in rsvector with "
5396  << col.size() - ind << " non-zero entries");
5397  col.push_back(ev);
5398  if (ind != nb) {
5399  itc = col.begin() + ind;
5400  auto ite = col.end();
5401  --ite;
5402  auto itee = ite;
5403  for (; ite != itc; --ite) { --itee; *ite = *itee; }
5404  *itc = ev;
5405  }
5406  ++nb;
5407  }
5408  ++ind;
5409  }
5410  }
5411  }
5412  }
5413  }
5414 
5415  inline void populate_dofs_vector
5416  (std::vector<size_type> &dofs,
5417  const size_type &size, const size_type &ifirst, const size_type &qmult,
5418  const getfem::mesh::ind_set &mfdofs)
5419  {
5420  dofs.assign(size, ifirst);
5421  auto itd = dofs.begin();
5422  if (qmult == 1)
5423  for (const auto &dof : mfdofs) *itd++ += dof;
5424  else
5425  for (const auto &dof : mfdofs)
5426  for (size_type q = 0; q < qmult; ++q) *itd++ += dof + q;
5427  }
5428 
5429  inline void populate_dofs_vector // special case for qmult == 1
5430  (std::vector<size_type> &dofs, const size_type &size, const size_type &ifirst,
5431  const getfem::mesh::ind_set &mfdofs)
5432  {
5433  dofs.assign(size, ifirst);
5434  auto itd = dofs.begin();
5435  for (const auto &dof : mfdofs) *itd++ += dof;
5436  }
5437 
5438 
5439  inline void populate_contiguous_dofs_vector
5440  (std::vector<size_type> &dofs, const size_type &size, const size_type &ifirst)
5441  {
5442  dofs.assign(size, ifirst);
5443  for (size_type i=0; i < size; ++i) dofs[i] += i;
5444  }
5445 
5446  struct ga_instruction_matrix_assembly_base : public ga_instruction {
5447  const base_tensor &t;
5448  const fem_interpolation_context &ctx1, &ctx2;
5449  const scalar_type &alpha1, &alpha2, &coeff;
5450  const size_type &nbpt, &ipt;
5451  base_vector elem;
5452  bool interpolate;
5453  std::vector<size_type> dofs1, dofs2, dofs1_sort;
5454  void add_tensor_to_element_matrix(bool initialize, bool empty_weight) {
5455  if (initialize) {
5456  if (empty_weight) elem.resize(0);
5457  elem.resize(t.size());
5458  if (!empty_weight)
5459  copy_scaled_4(t, coeff*alpha1*alpha2, elem);
5460  } else if (!empty_weight)
5461  // gmm::add(gmm::scaled(t.as_vector(), coeff*alpha1*alpha2), elem);
5462  // Faster than a daxpy blas call on my config
5463  add_scaled_4(t, coeff*alpha1*alpha2, elem);
5464  }
5465  ga_instruction_matrix_assembly_base
5466  (const base_tensor &t_,
5467  const fem_interpolation_context &ctx1_,
5468  const fem_interpolation_context &ctx2_,
5469  const scalar_type &a1, const scalar_type &a2, const scalar_type &coeff_,
5470  const size_type &nbpt_, const size_type &ipt_, bool interpolate_)
5471  : t(t_), ctx1(ctx1_), ctx2(ctx2_), alpha1(a1), alpha2(a2),
5472  coeff(coeff_), nbpt(nbpt_), ipt(ipt_), interpolate(interpolate_),
5473  dofs1(0), dofs2(0), dofs1_sort(0)
5474  {}
5475  protected:
5476  const bool false_=false;
5477  const size_type zero_=0;
5478  };
5479 
5480 
5481  struct ga_instruction_matrix_assembly_mf_mf
5482  : public ga_instruction_matrix_assembly_base
5483  {
5484  model_real_sparse_matrix &Krr, &Kru, &Kur, &Kuu;
5485  const gmm::sub_interval *const&I1, *const&I2, *const I1__, *const I2__;
5486  const mesh_fem *const&mf1, *const&mf2, *const mf1__, *const mf2__;
5487  const bool &reduced_mf1, &reduced_mf2; // refs to mf1/2->is_reduced()
5488  virtual int exec() {
5489  GA_DEBUG_INFO("Instruction: matrix term assembly mf-mf");
5490  if (!ctx1.is_convex_num_valid() || !ctx2.is_convex_num_valid()) return 0;
5491 
5492  bool initialize = (ipt == 0 || interpolate);
5493  bool empty_weight = (coeff == scalar_type(0));
5494  add_tensor_to_element_matrix(initialize, empty_weight); // t --> elem
5495 
5496  if (ipt == nbpt-1 || interpolate) { // finalize
5497  model_real_sparse_matrix &K = reduced_mf1 ? (reduced_mf2 ? Kuu : Kur)
5498  : (reduced_mf2 ? Kru : Krr);
5499  GA_DEBUG_ASSERT(I1->size() && I2->size(), "Internal error");
5500 
5501  scalar_type ninf = gmm::vect_norminf(elem);
5502  if (ninf == scalar_type(0)) return 0;
5503 
5504  size_type s1 = t.sizes()[0], s2 = t.sizes()[1];
5505  size_type cv1 = ctx1.convex_num(), cv2 = ctx2.convex_num();
5506  size_type ifirst1 = I1->first(), ifirst2 = I2->first();
5507 
5508  size_type N = ctx1.N();
5509  size_type qmult1 = mf1->get_qdim();
5510  if (qmult1 > 1) qmult1 /= mf1->fem_of_element(cv1)->target_dim();
5511  populate_dofs_vector(dofs1, s1, ifirst1, qmult1, // --> dofs1
5512  mf1->ind_scalar_basic_dof_of_element(cv1));
5513  if (mf1 == mf2 && cv1 == cv2) {
5514  if (ifirst1 == ifirst2) {
5515  add_elem_matrix(K, dofs1, dofs1, dofs1_sort, elem, ninf*1E-14, N);
5516  } else {
5517  populate_dofs_vector(dofs2, dofs1.size(), ifirst2 - ifirst1, dofs1);
5518  add_elem_matrix(K, dofs1, dofs2, dofs1_sort, elem, ninf*1E-14, N);
5519  }
5520  } else {
5521  N = std::max(N, ctx2.N());
5522  size_type qmult2 = mf2->get_qdim();
5523  if (qmult2 > 1) qmult2 /= mf2->fem_of_element(cv2)->target_dim();
5524  populate_dofs_vector(dofs2, s2, ifirst2, qmult2, // --> dofs2
5525  mf2->ind_scalar_basic_dof_of_element(cv2));
5526  add_elem_matrix(K, dofs1, dofs2, dofs1_sort, elem, ninf*1E-14, N);
5527  }
5528  }
5529  return 0;
5530  }
5531 
5532  ga_instruction_matrix_assembly_mf_mf
5533  (const base_tensor &t_,
5534  model_real_sparse_matrix &Krr_, model_real_sparse_matrix &Kru_,
5535  model_real_sparse_matrix &Kur_, model_real_sparse_matrix &Kuu_,
5536  const fem_interpolation_context &ctx1_,
5537  const fem_interpolation_context &ctx2_,
5538  const ga_instruction_set::variable_group_info &vgi1,
5539  const ga_instruction_set::variable_group_info &vgi2,
5540  const scalar_type &coeff_, const size_type &nbpt_, const size_type &ipt_,
5541  bool interpolate_)
5542  : ga_instruction_matrix_assembly_base
5543  (t_, ctx1_, ctx2_, vgi1.alpha, vgi2.alpha, coeff_, nbpt_, ipt_,
5544  interpolate_),
5545  Krr(Krr_), Kru(Kru_), Kur(Kur_), Kuu(Kuu_),
5546  I1(vgi1.I), I2(vgi2.I), I1__(nullptr), I2__(nullptr),
5547  mf1(vgi1.mf), mf2(vgi2.mf), mf1__(nullptr), mf2__(nullptr),
5548  reduced_mf1(vgi1.reduced_mf), reduced_mf2(vgi2.reduced_mf) {}
5549 
5550  ga_instruction_matrix_assembly_mf_mf
5551  (const base_tensor &t_,
5552  model_real_sparse_matrix &Kxr_, model_real_sparse_matrix &Kxu_,
5553  const fem_interpolation_context &ctx1_,
5554  const fem_interpolation_context &ctx2_,
5555  const gmm::sub_interval &I1_, const mesh_fem &mf1_, const scalar_type &a1,
5556  const ga_instruction_set::variable_group_info &vgi2,
5557  const scalar_type &coeff_, const size_type &nbpt_, const size_type &ipt_,
5558  bool interpolate_)
5559  : ga_instruction_matrix_assembly_base
5560  (t_, ctx1_, ctx2_, a1, vgi2.alpha, coeff_, nbpt_, ipt_, interpolate_),
5561  Krr(Kxr_), Kru(Kxu_), Kur(Kxr_), Kuu(Kxu_),
5562  I1(I1__), I2(vgi2.I), I1__(&I1_), I2__(nullptr),
5563  mf1(mf1__), mf2(vgi2.mf), mf1__(&mf1_), mf2__(nullptr),
5564  reduced_mf1(false_), reduced_mf2(vgi2.reduced_mf) {}
5565 
5566  ga_instruction_matrix_assembly_mf_mf
5567  (const base_tensor &t_,
5568  model_real_sparse_matrix &Krx_, model_real_sparse_matrix &Kux_,
5569  const fem_interpolation_context &ctx1_,
5570  const fem_interpolation_context &ctx2_,
5571  const ga_instruction_set::variable_group_info &vgi1,
5572  const gmm::sub_interval &I2_, const mesh_fem &mf2_, const scalar_type &a2,
5573  const scalar_type &coeff_, const size_type &nbpt_, const size_type &ipt_,
5574  bool interpolate_)
5575  : ga_instruction_matrix_assembly_base
5576  (t_, ctx1_, ctx2_, vgi1.alpha, a2, coeff_, nbpt_, ipt_, interpolate_),
5577  Krr(Krx_), Kru(Krx_), Kur(Kux_), Kuu(Kux_),
5578  I1(vgi1.I), I2(I2__), I1__(nullptr), I2__(&I2_),
5579  mf1(vgi1.mf), mf2(mf2__), mf1__(nullptr), mf2__(&mf2_),
5580  reduced_mf1(vgi1.reduced_mf), reduced_mf2(false_) {}
5581 
5582  ga_instruction_matrix_assembly_mf_mf
5583  (const base_tensor &t_, model_real_sparse_matrix &K_,
5584  const fem_interpolation_context &ctx1_,
5585  const fem_interpolation_context &ctx2_,
5586  const gmm::sub_interval &I1_, const mesh_fem &mf1_, const scalar_type &a1,
5587  const gmm::sub_interval &I2_, const mesh_fem &mf2_, const scalar_type &a2,
5588  const scalar_type &coeff_, const size_type &nbpt_, const size_type &ipt_,
5589  bool interpolate_)
5590  : ga_instruction_matrix_assembly_base
5591  (t_, ctx1_, ctx2_, a1, a2, coeff_, nbpt_, ipt_, interpolate_),
5592  Krr(K_), Kru(K_), Kur(K_), Kuu(K_),
5593  I1(I1__), I2(I2__), I1__(&I1_), I2__(&I2_),
5594  mf1(mf1__), mf2(mf2__), mf1__(&mf1_), mf2__(&mf2_),
5595  reduced_mf1(false_), reduced_mf2(false_) {}
5596  };
5597 
5598 
5599  struct ga_instruction_matrix_assembly_imd_mf
5600  : public ga_instruction_matrix_assembly_base
5601  {
5602  model_real_sparse_matrix &Kxr, &Kxu;
5603  const gmm::sub_interval *I1, *I2__, * const &I2;
5604  const im_data *imd1;
5605  const mesh_fem * const mf2__, * const &mf2;
5606  const bool &reduced_mf2; // ref to mf2->is_reduced()
5607  virtual int exec() {
5608  GA_DEBUG_INFO("Instruction: matrix term assembly "
5609  "(imdata or fixed size)-mf");
5610  if (!ctx1.is_convex_num_valid() || !ctx2.is_convex_num_valid()) return 0;
5611 
5612  bool empty_weight = (coeff == scalar_type(0));
5613  add_tensor_to_element_matrix(true, empty_weight); // t --> elem
5614 
5615  scalar_type ninf = gmm::vect_norminf(elem);
5616  if (ninf == scalar_type(0)) return 0;
5617 
5618  model_real_sparse_matrix &K = reduced_mf2 ? Kxu : Kxr;
5619  GA_DEBUG_ASSERT(I1->size() && I2->size(), "Internal error");
5620  size_type s1 = t.sizes()[0], s2 = t.sizes()[1];
5621  size_type cv1 = ctx1.convex_num(), cv2 = ctx2.convex_num();
5622  size_type ifirst1 = I1->first(), ifirst2 = I2->first();
5623  if (imd1) ifirst1 += s1 * imd1->filtered_index_of_point(cv1, ctx1.ii());
5624 
5625  populate_contiguous_dofs_vector(dofs1, s1, ifirst1); // --> dofs1
5626  size_type qmult2 = mf2->get_qdim();
5627  if (qmult2 > 1) qmult2 /= mf2->fem_of_element(cv2)->target_dim();
5628  populate_dofs_vector(dofs2, s2, ifirst2, qmult2, // --> dofs2
5629  mf2->ind_scalar_basic_dof_of_element(cv2));
5630  add_elem_matrix(K, dofs1, dofs2, dofs1_sort, elem, ninf*1E-14, ctx2.N());
5631  return 0;
5632  }
5633 
5634  ga_instruction_matrix_assembly_imd_mf
5635  (const base_tensor &t_,
5636  model_real_sparse_matrix &Kxr_, model_real_sparse_matrix &Kxu_,
5637  const fem_interpolation_context &ctx1_,
5638  const fem_interpolation_context &ctx2_,
5639  const gmm::sub_interval &I1_, const im_data *imd1_, const scalar_type &a1,
5640  const ga_instruction_set::variable_group_info &vgi2,
5641  const scalar_type &coeff_, const size_type &ipt_)
5642  : ga_instruction_matrix_assembly_base
5643  (t_, ctx1_, ctx2_, a1, vgi2.alpha, coeff_, zero_, ipt_, false),
5644  Kxr(Kxr_), Kxu(Kxu_), I1(&I1_), I2__(nullptr), I2(vgi2.I),
5645  imd1(imd1_), mf2__(nullptr), mf2(vgi2.mf), reduced_mf2(vgi2.reduced_mf)
5646  {}
5647 
5648  ga_instruction_matrix_assembly_imd_mf
5649  (const base_tensor &t_, model_real_sparse_matrix &K_,
5650  const fem_interpolation_context &ctx1_,
5651  const fem_interpolation_context &ctx2_,
5652  const gmm::sub_interval &I1_, const im_data *imd1_, const scalar_type &a1,
5653  const gmm::sub_interval &I2_, const mesh_fem &mf2_, const scalar_type &a2,
5654  const scalar_type &coeff_, const size_type &ipt_)
5655  : ga_instruction_matrix_assembly_base
5656  (t_, ctx1_, ctx2_, a1, a2, coeff_, zero_, ipt_, false),
5657  Kxr(K_), Kxu(K_), I1(&I1_), I2__(&I2_), I2(I2__),
5658  imd1(imd1_), mf2__(&mf2_), mf2(mf2__), reduced_mf2(false_) {}
5659  };
5660 
5661  struct ga_instruction_matrix_assembly_mf_imd
5662  : public ga_instruction_matrix_assembly_base
5663  {
5664  model_real_sparse_matrix &Krx, &Kux;
5665  const gmm::sub_interval * const &I1, *const I1__, *I2;
5666  const mesh_fem * const &mf1, *const mf1__;
5667  const bool &reduced_mf1; // ref to mf1->is_reduced()
5668  const im_data *imd2;
5669  virtual int exec() {
5670  GA_DEBUG_INFO("Instruction: matrix term assembly "
5671  "mf-(imdata or fixed size)");
5672  if (!ctx1.is_convex_num_valid() || !ctx2.is_convex_num_valid()) return 0;
5673 
5674  bool empty_weight = (coeff == scalar_type(0));
5675  add_tensor_to_element_matrix(true, empty_weight); // t --> elem
5676 
5677  scalar_type ninf = gmm::vect_norminf(elem);
5678  if (ninf == scalar_type(0)) return 0;
5679 
5680  model_real_sparse_matrix &K = reduced_mf1 ? Kux : Krx;
5681  GA_DEBUG_ASSERT(I1->size() && I2->size(), "Internal error");
5682  size_type s1 = t.sizes()[0], s2 = t.sizes()[1];
5683  size_type cv1 = ctx1.convex_num(), cv2 = ctx2.convex_num();
5684  size_type ifirst1 = I1->first(), ifirst2 = I2->first();
5685  if (imd2) ifirst2 += s2 * imd2->filtered_index_of_point(cv2, ctx2.ii());
5686 
5687  size_type qmult1 = mf1->get_qdim();
5688  if (qmult1 > 1) qmult1 /= mf1->fem_of_element(cv1)->target_dim();
5689  populate_dofs_vector(dofs1, s1, ifirst1, qmult1, // --> dofs1
5690  mf1->ind_scalar_basic_dof_of_element(cv1));
5691  populate_contiguous_dofs_vector(dofs2, s2, ifirst2); // --> dofs2
5692  add_elem_matrix(K, dofs1, dofs2, dofs1_sort, elem, ninf*1E-14, ctx1.N());
5693  return 0;
5694  }
5695 
5696  ga_instruction_matrix_assembly_mf_imd
5697  (const base_tensor &t_,
5698  model_real_sparse_matrix &Krx_, model_real_sparse_matrix &Kux_,
5699  const fem_interpolation_context &ctx1_,
5700  const fem_interpolation_context &ctx2_,
5701  const ga_instruction_set::variable_group_info &vgi1,
5702  const gmm::sub_interval &I2_, const im_data *imd2_, const scalar_type &a2,
5703  const scalar_type &coeff_, const size_type &ipt_)
5704  : ga_instruction_matrix_assembly_base
5705  (t_, ctx1_, ctx2_, vgi1.alpha, a2, coeff_, zero_, ipt_, false),
5706  Krx(Krx_), Kux(Kux_), I1(vgi1.I), I1__(nullptr), I2(&I2_),
5707  mf1(vgi1.mf), mf1__(nullptr), reduced_mf1(vgi1.reduced_mf), imd2(imd2_)
5708  {}
5709 
5710  ga_instruction_matrix_assembly_mf_imd
5711  (const base_tensor &t_, model_real_sparse_matrix &K_,
5712  const fem_interpolation_context &ctx1_,
5713  const fem_interpolation_context &ctx2_,
5714  const gmm::sub_interval &I1_, const mesh_fem &mf1_, const scalar_type &a1,
5715  const gmm::sub_interval &I2_, const im_data *imd2_, const scalar_type &a2,
5716  const scalar_type &coeff_, const size_type &ipt_)
5717  : ga_instruction_matrix_assembly_base
5718  (t_, ctx1_, ctx2_, a1, a2, coeff_, zero_, ipt_, false),
5719  Krx(K_), Kux(K_), I1(I1__), I1__(&I1_), I2(&I2_),
5720  mf1(mf1__), mf1__(&mf1_), reduced_mf1(false_), imd2(imd2_) {}
5721  };
5722 
5723 
5724 
5725  struct ga_instruction_matrix_assembly_imd_imd
5726  : public ga_instruction_matrix_assembly_base
5727  {
5728  model_real_sparse_matrix &K;
5729  const gmm::sub_interval &I1, &I2;
5730  const im_data *imd1, *imd2;
5731  virtual int exec() {
5732  GA_DEBUG_INFO("Instruction: matrix term assembly "
5733  "(imdata or fixed size)-(imdata or fixed size)");
5734  GA_DEBUG_ASSERT(I1.size() && I2.size(), "Internal error");
5735 
5736  bool empty_weight = (coeff == scalar_type(0));
5737  add_tensor_to_element_matrix(true, empty_weight); // t --> elem
5738 
5739  scalar_type ninf = gmm::vect_norminf(elem);
5740  if (ninf == scalar_type(0)) return 0;
5741 
5742  size_type s1 = t.sizes()[0], s2 = t.sizes()[1];
5743  size_type ifirst1 = I1.first(), ifirst2 = I2.first();
5744  if (imd1)
5745  ifirst1 += s1 * imd1->filtered_index_of_point(ctx1.convex_num(), ctx1.ii());
5746  if (imd2)
5747  ifirst2 += s2 * imd2->filtered_index_of_point(ctx2.convex_num(), ctx2.ii());
5748 
5749  populate_contiguous_dofs_vector(dofs2, s2, ifirst2);
5750  add_elem_matrix_contiguous_rows(K, ifirst1, s1, dofs2, elem, ninf*1E-14);
5751  return 0;
5752  }
5753  ga_instruction_matrix_assembly_imd_imd
5754  (const base_tensor &t_, model_real_sparse_matrix &K_,
5755  const fem_interpolation_context &ctx1_,
5756  const fem_interpolation_context &ctx2_,
5757  const gmm::sub_interval &I1_, const im_data *imd1_, const scalar_type &a1,
5758  const gmm::sub_interval &I2_, const im_data *imd2_, const scalar_type &a2,
5759  const scalar_type &coeff_, const size_type &ipt_)
5760  : ga_instruction_matrix_assembly_base
5761  (t_, ctx1_, ctx2_, a1, a2, coeff_, zero_, ipt_, false),
5762  K(K_), I1(I1_), I2(I2_), imd1(imd1_), imd2(imd2_) {}
5763  };
5764 
5765 
5766  struct ga_instruction_matrix_assembly_standard_scalar
5767  : public ga_instruction_matrix_assembly_base
5768  {
5769  model_real_sparse_matrix &K;
5770  const gmm::sub_interval &I1, &I2;
5771  const mesh_fem *pmf1, *pmf2;
5772  virtual int exec() {
5773  GA_DEBUG_INFO("Instruction: matrix term assembly for standard "
5774  "scalar fems");
5775  if (ipt == 0) {
5776  elem.resize(t.size());
5777  // gmm::copy(gmm::scaled(t.as_vector(), coeff*alpha1*alpha2), elem);
5778  copy_scaled_4(t, coeff*alpha1*alpha2, elem);
5779  } else
5780  // gmm::add(gmm::scaled(t.as_vector(), coeff*alpha1*alpha2), elem);
5781  // Faster than a daxpy blas call on my config
5782  add_scaled_4(t, coeff*alpha1*alpha2, elem);
5783 
5784  if (ipt == nbpt-1) { // finalize
5785  GA_DEBUG_ASSERT(I1.size() && I2.size(), "Internal error");
5786 
5787  scalar_type ninf = gmm::vect_norminf(elem);
5788  if (ninf == scalar_type(0)) return 0;
5789 
5790  size_type cv1 = ctx1.convex_num(), cv2 = ctx2.convex_num(), N=ctx1.N();
5791  if (cv1 == size_type(-1)) return 0;
5792  auto &ct1 = pmf1->ind_scalar_basic_dof_of_element(cv1);
5793  GA_DEBUG_ASSERT(ct1.size() == t.sizes()[0], "Internal error");
5794  populate_dofs_vector(dofs1, ct1.size(), I1.first(), ct1);
5795 
5796  if (pmf2 == pmf1 && cv1 == cv2) {
5797  if (I1.first() == I2.first()) {
5798  add_elem_matrix(K, dofs1, dofs1, dofs1_sort, elem, ninf*1E-14, N);
5799  } else {
5800  populate_dofs_vector(dofs2, dofs1.size(), I2.first() - I1.first(),
5801  dofs1);
5802  add_elem_matrix(K, dofs1, dofs2, dofs1_sort, elem, ninf*1E-14, N);
5803  }
5804  } else {
5805  if (cv2 == size_type(-1)) return 0;
5806  auto &ct2 = pmf2->ind_scalar_basic_dof_of_element(cv2);
5807  GA_DEBUG_ASSERT(ct2.size() == t.sizes()[1], "Internal error");
5808  populate_dofs_vector(dofs2, ct2.size(), I2.first(), ct2);
5809  add_elem_matrix(K, dofs1, dofs2, dofs1_sort, elem, ninf*1E-14, N);
5810  }
5811  }
5812  return 0;
5813  }
5814  ga_instruction_matrix_assembly_standard_scalar
5815  (const base_tensor &t_, model_real_sparse_matrix &K_,
5816  const fem_interpolation_context &ctx1_,
5817  const fem_interpolation_context &ctx2_,
5818  const gmm::sub_interval &I1_, const gmm::sub_interval &I2_,
5819  const mesh_fem *mfn1_, const mesh_fem *mfn2_,
5820  const scalar_type &a1, const scalar_type &a2, const scalar_type &coeff_,
5821  const size_type &nbpt_, const size_type &ipt_)
5822  : ga_instruction_matrix_assembly_base
5823  (t_, ctx1_, ctx2_, a1, a2, coeff_, nbpt_, ipt_, false),
5824  K(K_), I1(I1_), I2(I2_), pmf1(mfn1_), pmf2(mfn2_) {}
5825  };
5826 
5827  struct ga_instruction_matrix_assembly_standard_vector
5828  : public ga_instruction_matrix_assembly_base
5829  {
5830  model_real_sparse_matrix &K;
5831  const gmm::sub_interval &I1, &I2;
5832  const mesh_fem *pmf1, *pmf2;
5833  virtual int exec() {
5834  GA_DEBUG_INFO("Instruction: matrix term assembly for standard "
5835  "vector fems");
5836  if (ipt == 0) {
5837  elem.resize(t.size());
5838  copy_scaled_8(t, coeff*alpha1*alpha2, elem);
5839  // gmm::copy(gmm::scaled(t.as_vector(), coeff*alpha1*alpha2), elem);
5840  } else
5841  // gmm::add(gmm::scaled(t.as_vector(), coeff*alpha1*alpha2), elem);
5842  // (Far) faster than a daxpy blas call on my config.
5843  add_scaled_8(t, coeff*alpha1*alpha2, elem);
5844 
5845  if (ipt == nbpt-1) { // finalize
5846  GA_DEBUG_ASSERT(I1.size() && I2.size(), "Internal error");
5847 
5848  scalar_type ninf = gmm::vect_norminf(elem);
5849  if (ninf == scalar_type(0)) return 0;
5850  size_type s1 = t.sizes()[0], s2 = t.sizes()[1], N = ctx1.N();
5851 
5852  size_type cv1 = ctx1.convex_num(), cv2 = ctx2.convex_num();
5853  if (cv1 == size_type(-1)) return 0;
5854  size_type qmult1 = pmf1->get_qdim();
5855  if (qmult1 > 1) qmult1 /= pmf1->fem_of_element(cv1)->target_dim();
5856  populate_dofs_vector(dofs1, s1, I1.first(), qmult1, // --> dofs1
5857  pmf1->ind_scalar_basic_dof_of_element(cv1));
5858 
5859  if (pmf2 == pmf1 && cv1 == cv2 && I1.first() == I2.first()) {
5860  add_elem_matrix(K, dofs1, dofs1, dofs1_sort, elem, ninf*1E-14, N);
5861  } else {
5862  if (pmf2 == pmf1 && cv1 == cv2) {
5863  populate_dofs_vector(dofs2, dofs1.size(), I2.first() - I1.first(),
5864  dofs1);
5865  } else {
5866  if (cv2 == size_type(-1)) return 0;
5867  size_type qmult2 = pmf2->get_qdim();
5868  if (qmult2 > 1) qmult2 /= pmf2->fem_of_element(cv2)->target_dim();
5869  populate_dofs_vector(dofs2, s2, I2.first(), qmult2, // --> dofs2
5870  pmf2->ind_scalar_basic_dof_of_element(cv2));
5871  }
5872  add_elem_matrix(K, dofs1, dofs2, dofs1_sort, elem, ninf*1E-14, N);
5873  }
5874  }
5875  return 0;
5876  }
5877  ga_instruction_matrix_assembly_standard_vector
5878  (const base_tensor &t_, model_real_sparse_matrix &K_,
5879  const fem_interpolation_context &ctx1_,
5880  const fem_interpolation_context &ctx2_,
5881  const gmm::sub_interval &I1_, const gmm::sub_interval &I2_,
5882  const mesh_fem *mfn1_, const mesh_fem *mfn2_,
5883  const scalar_type &a1, const scalar_type &a2, const scalar_type &coeff_,
5884  const size_type &nbpt_, const size_type &ipt_)
5885  : ga_instruction_matrix_assembly_base
5886  (t_, ctx1_, ctx2_, a1, a2, coeff_, nbpt_, ipt_, false),
5887  K(K_), I1(I1_), I2(I2_), pmf1(mfn1_), pmf2(mfn2_) {}
5888  };
5889 
5890  template<int QQ>
5891  struct ga_instruction_matrix_assembly_standard_vector_opt10
5892  : public ga_instruction_matrix_assembly_base
5893  {
5894  model_real_sparse_matrix &K;
5895  const gmm::sub_interval &I1, &I2;
5896  const mesh_fem *pmf1, *pmf2;
5897  virtual int exec() {
5898  GA_DEBUG_INFO("Instruction: matrix term assembly for standard "
5899  "vector fems optimized for format 10 qdim " << QQ);
5900  size_type s1_q = QQ*t.sizes()[0];
5901  size_type ss1 = t.sizes()[0]/QQ, ss2 = t.sizes()[1]/QQ;
5902  scalar_type e = coeff*alpha1*alpha2;
5903  if (ipt == 0) {
5904  elem.resize(ss1*ss2);
5905  auto itel = elem.begin();
5906  for (size_type j = 0; j < ss2; ++j) {
5907  auto it = t.begin() + j*s1_q;
5908  for (size_type i = 0; i < ss1; ++i, it += QQ)
5909  *itel++ = (*it) * e;
5910  }
5911  } else {
5912  auto itel = elem.begin();
5913  for (size_type j = 0; j < ss2; ++j) {
5914  auto it = t.begin() + j*s1_q;
5915  for (size_type i = 0; i < ss1; ++i, it += QQ)
5916  *itel++ += (*it) * e;
5917  }
5918  }
5919  if (ipt == nbpt-1) { // finalize
5920  GA_DEBUG_ASSERT(I1.size() && I2.size(), "Internal error");
5921 
5922  scalar_type ninf = gmm::vect_norminf(elem) * 1E-14;
5923  if (ninf == scalar_type(0)) return 0;
5924  size_type N = ctx1.N();
5925  size_type cv1 = ctx1.convex_num(), cv2 = ctx2.convex_num();
5926  size_type i1 = I1.first(), i2 = I2.first();
5927  if (cv1 == size_type(-1)) return 0;
5928  populate_dofs_vector(dofs1, ss1, i1,
5929  pmf1->ind_scalar_basic_dof_of_element(cv1));
5930  bool same_dofs(pmf2 == pmf1 && cv1 == cv2 && i1 == i2);
5931 
5932  if (!same_dofs) {
5933  if (cv2 == size_type(-1)) return 0;
5934  populate_dofs_vector(dofs2, ss2, i2,
5935  pmf2->ind_scalar_basic_dof_of_element(cv2));
5936  }
5937  std::vector<size_type> &dofs2_ = same_dofs ? dofs1 : dofs2;
5938  add_elem_matrix(K, dofs1, dofs2_, dofs1_sort, elem, ninf, N);
5939  for (size_type i = 0; i < ss1; ++i) (dofs1[i])++;
5940  if (!same_dofs) for (size_type i = 0; i < ss2; ++i) (dofs2[i])++;
5941  add_elem_matrix(K, dofs1, dofs2_, dofs1_sort, elem, ninf, N);
5942  if (QQ >= 3) {
5943  for (size_type i = 0; i < ss1; ++i) (dofs1[i])++;
5944  if (!same_dofs) for (size_type i = 0; i < ss2; ++i) (dofs2[i])++;
5945  add_elem_matrix(K, dofs1, dofs2_, dofs1_sort, elem, ninf, N);
5946  }
5947  }
5948  return 0;
5949  }
5950 
5951  ga_instruction_matrix_assembly_standard_vector_opt10
5952  (const base_tensor &t_, model_real_sparse_matrix &Kn_,
5953  const fem_interpolation_context &ctx1_,
5954  const fem_interpolation_context &ctx2_,
5955  const gmm::sub_interval &In1_, const gmm::sub_interval &In2_,
5956  const mesh_fem *mfn1_, const mesh_fem *mfn2_,
5957  const scalar_type &a1, const scalar_type &a2, const scalar_type &coeff_,
5958  const size_type &nbpt_, const size_type &ipt_)
5959  : ga_instruction_matrix_assembly_base
5960  (t_, ctx1_, ctx2_, a1, a2, coeff_, nbpt_, ipt_, false),
5961  K(Kn_), I1(In1_), I2(In2_), pmf1(mfn1_), pmf2(mfn2_)
5962  {
5963  static_assert(QQ >= 2 && QQ <=3,
5964  "Template implemented only for QQ=2 and QQ=3");
5965  }
5966  };
5967 
5968 
5969  struct ga_instruction_condensation_sub : public ga_instruction {
5970  // one such instruction is used for every cluster of intercoupled
5971  // condensed variables
5972  gmm::dense_matrix<base_tensor *> KQJprime;
5973  std::vector<base_tensor *> RQprime;
5974  gmm::dense_matrix<base_tensor const *> KQQloc, KQJloc;
5975  base_tensor invKqqqq, Kqqjj;
5976  base_vector Rqq;
5977  std::vector<std::array<size_type,3>> partQ, partJ;
5978  const scalar_type &coeff; // &alpha1, &alpha2 ?
5979  virtual int exec() { // implementation can be optimized
5980  GA_DEBUG_INFO("Instruction: variable cluster subdiagonal condensation");
5981  // copy from KQQ to invKqqqq
5982  for (const auto &qqq1 : partQ) {
5983  size_type q1 = qqq1[0], qq1start = qqq1[1], qq1end = qqq1[2];
5984  for (const auto &qqq2 : partQ) {
5985  size_type q2 = qqq2[0], qq2start = qqq2[1], qq2end = qqq2[2];
5986  if (KQQloc(q1,q2)) {
5987  auto itr = KQQloc(q1,q2)->cbegin();
5988  GMM_ASSERT1(KQQloc(q1,q2)->size()
5989  == (qq1end-qq1start)*(qq2end-qq2start),
5990  "Internal error");
5991  for (size_type qq2=qq2start; qq2 < qq2end; ++qq2)
5992  for (size_type qq1=qq1start; qq1 < qq1end; ++qq1)
5993  invKqqqq(qq1,qq2) = *itr++;
5994  }
5995  }
5996  }
5997  // calculate inverse matrix invKqqqq
5998  bgeot::lu_inverse(&(invKqqqq[0]), invKqqqq.size(0));
5999 
6000  // Resize Kqqjj as primary variable sizes may change dynamically
6001  size_type prev_j(0);
6002  for (auto &&jjj : partJ) {
6003  size_type j=jjj[0];
6004  size_type new_j(0);
6005  for (const auto &qqq : partQ) {
6006  size_type q=qqq[0];
6007  if (KQJloc(q,j)) {
6008  if (new_j) {
6009  GMM_ASSERT1(new_j == KQJloc(q,j)->size(1), "Internal error");
6010  } else
6011  new_j = KQJloc(q,j)->size(1);
6012  }
6013  }
6014  // Resize KQJprime submatrices to match KQJloc sizes
6015  for (const auto &qqq : partQ) {
6016  size_type q=qqq[0];
6017  KQJprime(q,j)->adjust_sizes(qqq[2]-qqq[1], new_j);
6018  }
6019  jjj[1] = prev_j;
6020  prev_j += new_j;
6021  jjj[2] = prev_j;
6022  }
6023 
6024  Kqqjj.adjust_sizes(partQ.back()[2], partJ.back()[2]);
6025  gmm::clear(Kqqjj.as_vector());
6026  gmm::clear(Rqq);
6027 
6028  // multiply invKqqqq with all submatrices in KQJloc and RQprime and store
6029  // the results in Kqqjj and Rqq
6030  for (const auto &jjj : partJ) {
6031  size_type j = jjj[0], jjstart = jjj[1], jjend = jjj[2];
6032  for (const auto &qqq2 : partQ) {
6033  size_type q2 = qqq2[0], qq2start = qqq2[1], qq2end = qqq2[2];
6034  if (KQJloc(q2,j)) {
6035  auto itr = KQJloc(q2,j)->begin(); // auto &mat = KQJloc(q2,j);
6036  for (size_type jj=jjstart; jj < jjend; ++jj) {
6037  for (size_type qq2=qq2start; qq2 < qq2end; ++qq2, ++itr) {
6038  for (size_type qq1=0; qq1 < partQ.back()[2]; ++qq1) {
6039  Kqqjj(qq1,jj) += invKqqqq(qq1,qq2)*(*itr);
6040  // Kqqjj(qq1,jj) += invKqq(qq1,qq2)*mat(qq2-qqstart,jj-jjstart);
6041  } // for qq1
6042  } // for qq2
6043  } // for jj
6044  GMM_ASSERT1(itr == KQJloc(q2,j)->cend(), "Internal error");
6045  }
6046  } // in partQ
6047  } // in partJ
6048  for (const auto &qqq2 : partQ) {
6049  size_type q2 = qqq2[0], qq2start = qqq2[1], qq2end = qqq2[2];
6050  if (RQprime[q2]) {
6051  auto itr = RQprime[q2]->cbegin();
6052  for (size_type qq2=qq2start; qq2 < qq2end; ++qq2, ++itr) {
6053  for (size_type qq1=0; qq1 < invKqqqq.size(0); ++qq1)
6054  Rqq[qq1] += invKqqqq(qq1,qq2)*(*itr);
6055  } // for qq2
6056  GMM_ASSERT1(itr == RQprime[q2]->cend(), "Internal error");
6057  }
6058  } // in partQ
6059 
6060  // distribute the results from Kqqjj/Rqq to KQJprime/RQprime
6061  // submatrices/subvectors
6062  for (const auto &qqq1 : partQ) {
6063  size_type q1 = qqq1[0], qq1start = qqq1[1], qq1end = qqq1[2];
6064  { // writing into RQprime
6065  auto itw = RQprime[q1]->begin();
6066  for (size_type qq1=qq1start; qq1 < qq1end; ++qq1)
6067  *itw++ = Rqq[qq1]/coeff;
6068  }
6069  for (const auto &jjj2 : partJ) {
6070  size_type j2 = jjj2[0], jj2start = jjj2[1], jj2end = jjj2[2];
6071  auto itw = KQJprime(q1,j2)->begin();
6072  for (size_type jj2=jj2start; jj2 < jj2end; ++jj2)
6073  for (size_type qq1=qq1start; qq1 < qq1end; ++qq1)
6074  *itw++ = Kqqjj(qq1,jj2);
6075  }
6076  }
6077  return 0;
6078  }
6079 
6080  ga_instruction_condensation_sub(gmm::dense_matrix<base_tensor *> &KQJpr,
6081  std::vector<base_tensor *> &RQpr, // input/output
6082  const gmm::dense_matrix<base_tensor *> &KQQ,
6083  const gmm::dense_matrix<base_tensor *> &KQJ,
6084  const std::set<size_type> &Qset,
6085  const scalar_type &coeff_)
6086  : KQJprime(KQJpr), RQprime(RQpr), coeff(coeff_)
6087  {
6088  // * to const *
6089  KQQloc.resize(KQQ.nrows(), KQQ.ncols());
6090  KQJloc.resize(KQJ.nrows(), KQJ.ncols());
6091  for (size_type i=0; i < KQQ.as_vector().size(); ++i) KQQloc[i] = KQQ[i];
6092  for (size_type i=0; i < KQJ.as_vector().size(); ++i) KQJloc[i] = KQJ[i];
6093 
6094  for (size_type j=0; j < KQJ.ncols(); ++j)
6095  for (const size_type &q : Qset)
6096  if (KQJ(q,j)) {
6097  partJ.push_back(std::array<size_type,3>{j,0,0});
6098  break;
6099  }
6100 
6101  partQ.resize(0);
6102  for (const size_type &q : Qset)
6103  partQ.push_back(std::array<size_type,3>{q,0,0});
6104  size_type prev_q(0);
6105  for (auto &qqq1 : partQ) {
6106  size_type q1 = qqq1[0];
6107  size_type new_q(0);
6108  for (const size_type &q2 : Qset)
6109  if (new_q) {
6110  GMM_ASSERT1(new_q == KQQ(q1,q2)->size(0) &&
6111  new_q == KQQ(q2,q1)->size(1), "Internal error");
6112  } else
6113  new_q = KQQ(q1,q2)->size(0);
6114  qqq1[1] = prev_q;
6115  prev_q += new_q;
6116  qqq1[2] = prev_q;
6117  }
6118  invKqqqq.adjust_sizes(partQ.back()[2], partQ.back()[2]);
6119  Rqq.resize(partQ.back()[2]);
6120  // Kqqjj will be resized dynamically due to possible changes in j interval
6121  }
6122  };
6123 
6124 
6125  struct ga_instruction_condensation_super_K : public ga_instruction {
6126  base_tensor &Kij;
6127  std::vector<base_tensor *> KiQ, KQj; // indexed wrt q in Q
6128  size_type Qsize;
6129 
6130  virtual int exec() {
6131  GA_DEBUG_INFO("Instruction: contribution of condensation to kept part");
6132 
6133  size_type m = KiQ[0]->size(0);
6134  size_type n = KQj[0]->size(1);
6135  Kij.adjust_sizes(m,n);
6136  gmm::clear(Kij.as_vector());
6137  for (size_type k=0; k < Qsize; ++k) {
6138  const base_tensor &K1 = *KiQ[k], &K2 = *KQj[k];
6139  size_type qqsize = K1.size(1);
6140  GMM_ASSERT1(K1.size(0) == m && K2.size(1) == n && K2.size(0) == qqsize,
6141  "Internal error");
6142 
6143  base_tensor::iterator it = Kij.begin();
6144  for (size_type jj = 0; jj < n; ++jj)
6145  for (size_type ii = 0; ii < m; ++ii, ++it)
6146  for (size_type qq = 0; qq < qqsize; ++qq)
6147  *it -= K1[ii+qq*m] * K2[qq+jj*qqsize];
6148  GA_DEBUG_ASSERT(it == Kij.end(), "Wrong sizes");
6149  }
6150  return 0;
6151  }
6152  ga_instruction_condensation_super_K(base_tensor &Kij_,
6153  const std::vector<base_tensor *> KiQ_,
6154  const std::vector<base_tensor *> KQj_)
6155  : Kij(Kij_), KiQ(KiQ_), KQj(KQj_)
6156  {
6157  Qsize = KiQ.size();
6158  GMM_ASSERT1(KiQ.size() == KQj.size(), "Internal error");
6159  }
6160  };
6161 
6162  struct ga_instruction_condensation_super_R : public ga_instruction {
6163  base_tensor &Ri;
6164  std::vector<base_tensor *> KiQ, RQpr; // indexed wrt q in Q
6165  size_type Qsize;
6166 
6167  virtual int exec() {
6168  GA_DEBUG_INFO("Instruction: contribution of condensation to primary rhs");
6169 
6170  size_type m = KiQ[0]->size(0);
6171  Ri.adjust_sizes(m);
6172  gmm::clear(Ri.as_vector());
6173  for (size_type k=0; k < Qsize; ++k) {
6174  const base_tensor &K1 = *KiQ[k], &R2 = *RQpr[k];
6175  size_type qqsize = K1.size(1);
6176  GMM_ASSERT1(K1.size(0) == m && R2.size(0) == qqsize, "Internal error");
6177  base_tensor::iterator it = Ri.begin();
6178  for (size_type ii = 0; ii < m; ++ii, ++it)
6179  for (size_type qq = 0; qq < qqsize; ++qq)
6180  *it -= K1[ii+qq*m] * R2[qq];
6181  GA_DEBUG_ASSERT(it == Ri.end(), "Wrong sizes");
6182  }
6183  return 0;
6184  }
6185  ga_instruction_condensation_super_R(base_tensor &Ri_,
6186  const std::vector<base_tensor *> KiQ_,
6187  const std::vector<base_tensor *> RQpr_)
6188  : Ri(Ri_), KiQ(KiQ_), RQpr(RQpr_)
6189  {
6190  Qsize = KiQ.size();
6191  GMM_ASSERT1(KiQ.size() == RQpr.size(), "Internal error");
6192  }
6193  };
6194 
6195  //=========================================================================
6196  // Compilation of assembly trees into a list of basic instructions
6197  //=========================================================================
6198 
6199  static void extend_variable_in_gis(const ga_workspace &workspace,
6200  const std::string &varname,
6201  ga_instruction_set &gis) {
6202  if (workspace.variable_group_exists(varname)) {
6203  for (const std::string &v : workspace.variable_group(varname))
6204  extend_variable_in_gis(workspace, v, gis);
6205  } else if (gis.extended_vars.count(varname) == 0) {
6206  const mesh_fem *mf = workspace.associated_mf(varname);
6207  if (mf->is_reduced()) {
6208  auto n = (mf->get_qdim() == 1) ? workspace.qdim(varname) : 1;
6209  base_vector &U = gis.really_extended_vars[varname];
6210  gmm::resize(U, mf->nb_basic_dof() * n);
6211  mf->extend_vector(workspace.value(varname), U);
6212  gis.extended_vars[varname] = &(gis.really_extended_vars[varname]);
6213  } else {
6214  gis.extended_vars[varname] = &(workspace.value(varname));
6215  }
6216  }
6217  }
6218 
6219  static void ga_clear_node_list
6220  (pga_tree_node pnode, std::map<scalar_type,
6221  std::list<pga_tree_node> > &node_list) {
6222  std::list<pga_tree_node> &loc_node_list = node_list[pnode->hash_value];
6223  for (std::list<pga_tree_node>::iterator it = loc_node_list.begin();
6224  it != loc_node_list.end(); ) {
6225  if (*it == pnode) it = loc_node_list.erase(it); else ++it;
6226  }
6227  for (size_type i = 0; i < pnode->children.size(); ++i)
6228  ga_clear_node_list(pnode->children[i], node_list);
6229  }
6230 
6231  // workspace argument is not const because of declaration of temporary
6232  // unreduced variables
6233  static void ga_compile_node(const pga_tree_node pnode,
6234  ga_workspace &workspace,
6235  ga_instruction_set &gis,
6236  ga_instruction_set::region_mim_instructions &rmi,
6237  const mesh &m, bool function_case,
6238  ga_if_hierarchy &if_hierarchy) {
6239 
6240  if (pnode->node_type == GA_NODE_PREDEF_FUNC ||
6241  pnode->node_type == GA_NODE_OPERATOR ||
6242  pnode->node_type == GA_NODE_SPEC_FUNC ||
6243  pnode->node_type == GA_NODE_CONSTANT ||
6244  pnode->node_type == GA_NODE_ALLINDICES ||
6245  pnode->node_type == GA_NODE_RESHAPE ||
6246  pnode->node_type == GA_NODE_SWAP_IND ||
6247  pnode->node_type == GA_NODE_IND_MOVE_LAST ||
6248  pnode->node_type == GA_NODE_CONTRACT) return;
6249 
6250  // cout << "compiling "; ga_print_node(pnode, cout); cout << endl;
6251 
6252  pga_instruction pgai;
6253  ga_if_hierarchy *pif_hierarchy = &if_hierarchy;
6254  ga_if_hierarchy new_if_hierarchy;
6255 
6256  const mesh_fem *mf1 = 0, *mf2 = 0;
6257  const mesh_fem **mfg1 = 0, **mfg2 = 0;
6258  fem_interpolation_context *pctx1 = 0, *pctx2 = 0;
6259  bool tensor_to_clear = false;
6260  bool tensor_to_adapt = false;
6261 
6262  if (pnode->test_function_type) {
6263  if (pnode->name_test1.size())
6264  mf1 = workspace.associated_mf(pnode->name_test1);
6265  if (mf1) {
6266  pctx1 = &(gis.ctx);
6267  const std::string &intn1 = pnode->interpolate_name_test1;
6268  if (intn1.size()) {
6269  if (workspace.secondary_domain_exists(intn1)) {
6270  pctx1 = &(rmi.secondary_domain_infos.ctx);
6271  } else {
6272  tensor_to_adapt = true;
6273  pctx1 = &(rmi.interpolate_infos[intn1].ctx);
6274  if (workspace.variable_group_exists(pnode->name_test1)) {
6275  ga_instruction_set::variable_group_info &vgi =
6276  rmi.interpolate_infos[intn1].groups_info[pnode->name_test1];
6277  mfg1 = &(vgi.mf);
6278  mf1 = 0;
6279  }
6280  }
6281  }
6282  }
6283  if (pnode->name_test2.size())
6284  mf2 = workspace.associated_mf(pnode->name_test2);
6285  if (mf2) {
6286  pctx2 = &(gis.ctx);
6287  const std::string &intn2 = pnode->interpolate_name_test2;
6288  if (intn2.size()) {
6289  if (workspace.secondary_domain_exists(intn2)) {
6290  pctx2 = &(rmi.secondary_domain_infos.ctx);
6291  } else {
6292  tensor_to_adapt = true;
6293  pctx2 = &(rmi.interpolate_infos[intn2].ctx);
6294  if (workspace.variable_group_exists(pnode->name_test2)) {
6295  ga_instruction_set::variable_group_info &vgi =
6296  rmi.interpolate_infos[intn2].groups_info[pnode->name_test2];
6297  mfg2 = &(vgi.mf);
6298  mf2 = 0;
6299  }
6300  }
6301  }
6302  }
6303  }
6304 
6305  // Produce a resize instruction which is stored if no equivalent node is
6306  // detected and if the mesh is not uniform.
6307  pnode->t.set_to_original();
6308  pnode->t.set_sparsity(0, 0);
6309  bool is_uniform = false;
6310  if (pnode->test_function_type == 1) {
6311  if (mf1 || mfg1)
6312  pgai = std::make_shared<ga_instruction_first_ind_tensor>
6313  (pnode->tensor(), *pctx1, pnode->qdim1, mf1, mfg1);
6314  if (mf1 && mf1->is_uniform())
6315  { is_uniform = true; pctx1->invalid_convex_num(); }
6316  } else if (pnode->test_function_type == 2) {
6317  if (mf2 || mfg2)
6318  pgai = std::make_shared<ga_instruction_first_ind_tensor>
6319  (pnode->tensor(), *pctx2, pnode->qdim2, mf2, mfg2);
6320  if (mf2 && mf2->is_uniform())
6321  { is_uniform = true; pctx2->invalid_convex_num(); }
6322  } else if (pnode->test_function_type == 3) {
6323  if ((mf1 || mfg1) && (mf2 || mfg2)) {
6324  pgai = std::make_shared<ga_instruction_two_first_ind_tensor>
6325  (pnode->tensor(), *pctx1, *pctx2, pnode->qdim1, mf1, mfg1,
6326  pnode->qdim2, mf2, mfg2);
6327  if (mf1 && mf1->is_uniform() && mf2 && mf2->is_uniform()) {
6328  is_uniform = true;
6329  pctx1->invalid_convex_num();
6330  pctx2->invalid_convex_num();
6331  }
6332  } else if (mf1 || mfg1) {
6333  pgai = std::make_shared<ga_instruction_first_ind_tensor>
6334  (pnode->tensor(), *pctx1, pnode->qdim1, mf1, mfg1);
6335  if (mf1 && mf1->is_uniform())
6336  { is_uniform = true; pctx1->invalid_convex_num(); }
6337  } else if (mf2 || mfg2) {
6338  pgai = std::make_shared<ga_instruction_second_ind_tensor>
6339  (pnode->tensor(), *pctx2, pnode->qdim2, mf2, mfg2);
6340  if (mf2 && mf2->is_uniform())
6341  { is_uniform = true; pctx2->invalid_convex_num(); }
6342  }
6343  }
6344 
6345  // Optimization: detects if an equivalent node has already been compiled
6346  pnode->t.set_to_original();
6347  if (rmi.node_list.count(pnode->hash_value) != 0) {
6348  for (pga_tree_node &pnode1 : rmi.node_list[pnode->hash_value]) {
6349  // cout << "found potential equivalent nodes ";
6350  // ga_print_node(pnode, cout);
6351  // cout << " and "; ga_print_node(pnode1, cout); cout << endl;
6352  if (sub_tree_are_equal(pnode, pnode1, workspace, 1)) {
6353  pnode->t.set_to_copy(pnode1->t);
6354  return;
6355  }
6356  if (sub_tree_are_equal(pnode, pnode1, workspace, 2)) {
6357  // cout << "confirmed with transpose" << endl;
6358  if (pnode->nb_test_functions() == 2) {
6359  if (pgai) { // resize instruction if needed
6360  if (is_uniform)
6361  { pgai->exec(); }
6362  else { rmi.instructions.push_back(std::move(pgai)); }
6363  }
6364  pgai = std::make_shared<ga_instruction_transpose_test>
6365  (pnode->tensor(), pnode1->tensor());
6366  rmi.instructions.push_back(std::move(pgai));
6367  } else {
6368  pnode->t.set_to_copy(pnode1->t);
6369  }
6370  return;
6371  }
6372  // cout << "sub_tree_are_equal = " << int(sub_tree_are_equal(pnode, pnode1, workspace, 1)) << endl;
6373  std::stringstream ss;
6374  ss << "Detected wrong equivalent nodes:" << endl;
6375  ga_print_node(pnode, ss);
6376  ss << endl << " and " << endl;
6377  ga_print_node(pnode1, ss);
6378  ss << endl << "No problem, but hash values could be adapted." << endl;
6379  GMM_TRACE2(ss.str());
6380  }
6381  }
6382 
6383  if (pgai) { // resize instruction if needed and no equivalent node detected
6384  if (is_uniform) { pgai->exec(); }
6385  else {
6386  if (tensor_to_adapt)
6387  rmi.instructions.push_back(std::move(pgai));
6388  else
6389  rmi.elt_instructions.push_back(std::move(pgai));
6390  }
6391  }
6392 
6393  size_type interpolate_filter_inst = rmi.instructions.size();
6394  if (pnode->node_type == GA_NODE_INTERPOLATE_FILTER) {
6395  pgai = pga_instruction();
6396  rmi.instructions.push_back(std::move(pgai));
6397  if_hierarchy.increment();
6398  new_if_hierarchy.child_of(if_hierarchy);
6399  pif_hierarchy = &new_if_hierarchy;
6400  }
6401 
6402  for (size_type i = 0; i < pnode->children.size(); ++i)
6403  ga_compile_node(pnode->children[i], workspace, gis, rmi, m,
6404  function_case, *pif_hierarchy);
6405 
6406  if (pnode->node_type == GA_NODE_INTERPOLATE_FILTER) {
6407  const std::string &intn = pnode->interpolate_name;
6408  ga_instruction_set::interpolate_info &inin = rmi.interpolate_infos[intn];
6409  pgai = std::make_shared<ga_instruction_interpolate_filter>
6410  (pnode->tensor(), inin, pnode->nbc1,
6411  int(rmi.instructions.size() - interpolate_filter_inst));
6412  rmi.instructions[interpolate_filter_inst].swap(pgai);
6413  pgai = std::make_shared<ga_instruction_copy_tensor>
6414  (pnode->tensor(), pnode->children[0]->tensor());
6415  rmi.instructions.push_back(std::move(pgai));
6416  ga_clear_node_list(pnode->children[0], rmi.node_list);
6417  }
6418 
6419  static scalar_type minus = -scalar_type(1);
6420  size_type nbch = pnode->children.size();
6421  pga_tree_node child0 = (nbch > 0) ? pnode->children[0] : 0;
6422  pga_tree_node child1 = (nbch > 1) ? pnode->children[1] : 0;
6423  bgeot::multi_index mi;
6424  const bgeot::multi_index &size0 = child0 ? child0->t.sizes() : mi;
6425  // const bgeot::multi_index &size1 = child1 ? child1->t.sizes() : mi;
6426  size_type dim0 = child0 ? child0->tensor_order() : 0;
6427  size_type dim1 = child1 ? child1->tensor_order() : 0;
6428 
6429  switch (pnode->node_type) {
6430 
6431  case GA_NODE_PREDEF_FUNC: case GA_NODE_OPERATOR: case GA_NODE_SPEC_FUNC:
6432  case GA_NODE_CONSTANT: case GA_NODE_ALLINDICES: case GA_NODE_ZERO:
6433  case GA_NODE_RESHAPE: case GA_NODE_CROSS_PRODUCT:
6434  case GA_NODE_SWAP_IND: case GA_NODE_IND_MOVE_LAST:
6435  case GA_NODE_CONTRACT: case GA_NODE_INTERPOLATE_FILTER:
6436  break;
6437 
6438  case GA_NODE_X:
6439  GMM_ASSERT1(!function_case,
6440  "No use of X is allowed in scalar functions");
6441  if (pnode->nbc1) {
6442  GA_DEBUG_ASSERT(pnode->tensor().size() == 1, "dimensions mismatch");
6443  GMM_ASSERT1(pnode->nbc1 <= m.dim(),
6444  "Bad index for X in expression");
6445  pgai = std::make_shared<ga_instruction_X_component>
6446  (pnode->tensor()[0], gis.ctx, pnode->nbc1-1);
6447  } else {
6448  if (pnode->tensor().size() != m.dim())
6449  pnode->init_vector_tensor(m.dim());
6450  pgai = std::make_shared<ga_instruction_X>(pnode->tensor(), gis.ctx);
6451  }
6452  rmi.instructions.push_back(std::move(pgai));
6453  break;
6454 
6455  case GA_NODE_ELT_SIZE:
6456  GMM_ASSERT1(!function_case,
6457  "No use of element_size is allowed in functions");
6458  if (pnode->tensor().size() != 1) pnode->init_scalar_tensor(0);
6459  pgai = std::make_shared<ga_instruction_element_size>
6460  (pnode->tensor(), gis.elt_size);
6461  gis.need_elt_size = true;
6462  rmi.instructions.push_back(std::move(pgai));
6463  break;
6464 
6465  case GA_NODE_ELT_K:
6466  GMM_ASSERT1(!function_case,
6467  "No use of element_K is allowed in functions");
6468  pgai = std::make_shared<ga_instruction_element_K>(pnode->tensor(),
6469  gis.ctx);
6470  rmi.instructions.push_back(std::move(pgai));
6471  break;
6472 
6473  case GA_NODE_ELT_B:
6474  GMM_ASSERT1(!function_case,
6475  "No use of element_B is allowed in functions");
6476  pgai = std::make_shared<ga_instruction_element_B>(pnode->tensor(),
6477  gis.ctx);
6478  rmi.instructions.push_back(std::move(pgai));
6479  break;
6480 
6481  case GA_NODE_ELT_CURVATURE:
6482  GMM_ASSERT1(!function_case,
6483  "No use of element_curvature is allowed in functions");
6484  pgai = std::make_shared<ga_instruction_element_curvature>
6485  (pnode->tensor(), gis.ctx);
6486  rmi.instructions.push_back(std::move(pgai));
6487  break;
6488 
6489  case GA_NODE_NORMAL:
6490  {
6491  GMM_ASSERT1(!function_case,
6492  "No use of Normal is allowed in functions");
6493  if (pnode->tensor().size() != m.dim())
6494  pnode->init_vector_tensor(m.dim());
6495  const mesh_im_level_set *mimls
6496  = dynamic_cast<const mesh_im_level_set *>(rmi.im);
6497  if (mimls && mimls->location()==mesh_im_level_set::INTEGRATE_BOUNDARY) {
6498  // Appel avec ctx (pt de Gauss)
6499  pgai = std::make_shared<ga_instruction_level_set_normal_vector>
6500  (pnode->tensor(), mimls, gis.ctx);
6501  rmi.instructions.push_back(std::move(pgai));
6502  } else {
6503  pgai = std::make_shared<ga_instruction_copy_Normal>
6504  (pnode->tensor(), gis.Normal);
6505  rmi.instructions.push_back(std::move(pgai));
6506  }
6507  }
6508  break;
6509 
6510  case GA_NODE_INTERPOLATE_X:
6511  case GA_NODE_INTERPOLATE_NORMAL:
6512  GMM_ASSERT1(!function_case,
6513  "No use of Interpolate is allowed in functions");
6514  if (pnode->tensor().size() != m.dim())
6515  pnode->init_vector_tensor(m.dim());
6516  if (pnode->node_type == GA_NODE_INTERPOLATE_X)
6517  pgai = std::make_shared<ga_instruction_copy_interpolated_small_vect>
6518  (pnode->tensor(),
6519  rmi.interpolate_infos[pnode->interpolate_name].pt_y,
6520  rmi.interpolate_infos[pnode->interpolate_name]);
6521  else if (pnode->node_type == GA_NODE_INTERPOLATE_NORMAL)
6522  pgai = std::make_shared<ga_instruction_copy_Normal>
6523  (pnode->tensor(),
6524  rmi.interpolate_infos[pnode->interpolate_name].Normal);
6525  rmi.instructions.push_back(std::move(pgai));
6526  break;
6527 
6528  case GA_NODE_INTERPOLATE_ELT_K:
6529  case GA_NODE_INTERPOLATE_ELT_B:
6530  GMM_ASSERT1(!function_case,
6531  "No use of Interpolate is allowed in functions");
6532  if (pnode->node_type == GA_NODE_INTERPOLATE_ELT_K)
6533  pgai = std::make_shared<ga_instruction_element_K>
6534  (pnode->tensor(),
6535  rmi.interpolate_infos[pnode->interpolate_name].ctx);
6536  else if (pnode->node_type == GA_NODE_INTERPOLATE_ELT_B)
6537  pgai = std::make_shared<ga_instruction_element_B>
6538  (pnode->tensor(),
6539  rmi.interpolate_infos[pnode->interpolate_name].ctx);
6540  rmi.instructions.push_back(std::move(pgai));
6541  break;
6542 
6543  case GA_NODE_SECONDARY_DOMAIN_X:
6544  case GA_NODE_SECONDARY_DOMAIN_NORMAL:
6545  {
6546  GMM_ASSERT1(!function_case,
6547  "No use of Secondary_domain is allowed in functions");
6548  auto psd = workspace.secondary_domain(pnode->interpolate_name);
6549  size_type sddim = psd->mim().linked_mesh().dim();
6550  if (pnode->tensor().size() != sddim)
6551  pnode->init_vector_tensor(sddim);
6552  if (pnode->node_type == GA_NODE_SECONDARY_DOMAIN_X)
6553  pgai = std::make_shared<ga_instruction_X>
6554  (pnode->tensor(), rmi.secondary_domain_infos.ctx);
6555  else if (pnode->node_type == GA_NODE_SECONDARY_DOMAIN_NORMAL)
6556  pgai = std::make_shared<ga_instruction_copy_Normal>
6557  (pnode->tensor(), rmi.secondary_domain_infos.Normal);
6558  rmi.instructions.push_back(std::move(pgai));
6559  }
6560  break;
6561 
6562  case GA_NODE_VAL: case GA_NODE_GRAD:
6563  case GA_NODE_HESS: case GA_NODE_DIVERG:
6564  case GA_NODE_ELEMENTARY_VAL: case GA_NODE_ELEMENTARY_GRAD:
6565  case GA_NODE_ELEMENTARY_HESS: case GA_NODE_ELEMENTARY_DIVERG:
6566  case GA_NODE_XFEM_PLUS_VAL: case GA_NODE_XFEM_PLUS_GRAD:
6567  case GA_NODE_XFEM_PLUS_HESS: case GA_NODE_XFEM_PLUS_DIVERG:
6568  case GA_NODE_XFEM_MINUS_VAL: case GA_NODE_XFEM_MINUS_GRAD:
6569  case GA_NODE_XFEM_MINUS_HESS: case GA_NODE_XFEM_MINUS_DIVERG:
6570  {
6571  bool is_elementary = (pnode->node_type == GA_NODE_ELEMENTARY_VAL ||
6572  pnode->node_type == GA_NODE_ELEMENTARY_GRAD ||
6573  pnode->node_type == GA_NODE_ELEMENTARY_HESS ||
6574  pnode->node_type == GA_NODE_ELEMENTARY_DIVERG);
6575  if (function_case) {
6576  GMM_ASSERT1(!is_elementary,
6577  "No elementary transformation is allowed in functions");
6578  GMM_ASSERT1(pnode->node_type != GA_NODE_XFEM_PLUS_VAL &&
6579  pnode->node_type != GA_NODE_XFEM_PLUS_GRAD &&
6580  pnode->node_type != GA_NODE_XFEM_PLUS_HESS &&
6581  pnode->node_type != GA_NODE_XFEM_PLUS_DIVERG,
6582  "Xfem_plus not allowed in functions");
6583  GMM_ASSERT1(pnode->node_type != GA_NODE_XFEM_MINUS_VAL &&
6584  pnode->node_type != GA_NODE_XFEM_MINUS_GRAD &&
6585  pnode->node_type != GA_NODE_XFEM_MINUS_HESS &&
6586  pnode->node_type != GA_NODE_XFEM_MINUS_DIVERG,
6587  "Xfem_plus not allowed in functions");
6588  const mesh_fem *mf = workspace.associated_mf(pnode->name);
6589  const im_data *imd = workspace.associated_im_data(pnode->name);
6590  GMM_ASSERT1(!mf, "No fem expression is allowed in "
6591  "function expression");
6592  GMM_ASSERT1(!imd, "No integration method data is allowed in "
6593  "function expression");
6594  if (gmm::vect_size(workspace.value(pnode->name)) == 1)
6595  pgai = std::make_shared<ga_instruction_copy_scalar>
6596  (pnode->tensor()[0], (workspace.value(pnode->name))[0]);
6597  else
6598  pgai = std::make_shared<ga_instruction_copy_vect>
6599  (pnode->tensor().as_vector(), workspace.value(pnode->name));
6600  rmi.instructions.push_back(std::move(pgai));
6601  } else {
6602  const mesh_fem *mf = workspace.associated_mf(pnode->name), *mfo=mf;
6603  const im_data *imd = workspace.associated_im_data(pnode->name);
6604 
6605  if (is_elementary) {
6606  mf = workspace.associated_mf(pnode->elementary_target);
6607  GMM_ASSERT1(mf && mfo,
6608  "Wrong context for elementary transformation");
6609  GMM_ASSERT1(&(mfo->linked_mesh()) == &(m),
6610  "The finite element of variable " << pnode->name
6611  << " has to be defined on the same mesh as the "
6612  << "integration method or interpolation used");
6613  }
6614 
6615  if (imd) {
6616  GMM_ASSERT1(pnode->node_type == GA_NODE_VAL,
6617  "Only values can be extracted on im_data (no " <<
6618  "gradient, Hessian, xfem or elementary tranformation" <<
6619  " allowed)");
6620  pgai = std::make_shared<ga_instruction_extract_local_im_data>
6621  (pnode->tensor(), *imd, workspace.value(pnode->name),
6622  gis.pai, gis.ctx, workspace.qdim(pnode->name));
6623  rmi.instructions.push_back(std::move(pgai));
6624  } else {
6625  GMM_ASSERT1(mf, "Internal error");
6626 
6627  GMM_ASSERT1(&(mf->linked_mesh()) == &(m),
6628  "The finite element of variable " <<
6629  (is_elementary ? pnode->elementary_target : pnode->name)
6630  << " has to be defined on the same mesh as the "
6631  << "integration method or interpolation used");
6632 
6633  // An instruction for extracting local dofs of the variable.
6634  if (rmi.local_dofs.count(pnode->name) == 0) {
6635  rmi.local_dofs[pnode->name] = base_vector(1);
6636  extend_variable_in_gis(workspace, pnode->name, gis);
6637  // cout << "local dof of " << pnode->name << endl;
6638  size_type qmult2 = mfo->get_qdim();
6639  if (qmult2 > 1 && !(mfo->is_uniformly_vectorized()))
6640  qmult2 = size_type(-1);
6641  pgai = std::make_shared<ga_instruction_slice_local_dofs>
6642  (*mfo, *(gis.extended_vars[pnode->name]), gis.ctx,
6643  rmi.local_dofs[pnode->name],
6644  workspace.qdim(pnode->name) / mfo->get_qdim(), qmult2);
6645  rmi.elt_instructions.push_back(std::move(pgai));
6646  }
6647 
6648  // An instruction for pfp update
6649  if (mf->is_uniform()) {
6650  if (rmi.pfps.count(mf) == 0) {
6651  rmi.pfps[mf] = 0;
6652  pgai = std::make_shared<ga_instruction_update_pfp>
6653  (*mf, rmi.pfps[mf], gis.ctx, gis.fp_pool);
6654  rmi.begin_instructions.push_back(std::move(pgai));
6655  }
6656  } else if (rmi.pfps.count(mf) == 0 ||
6657  !if_hierarchy.is_compatible(rmi.pfp_hierarchy[mf])) {
6658  rmi.pfp_hierarchy[mf].push_back(if_hierarchy);
6659  rmi.pfps[mf] = 0;
6660  pgai = std::make_shared<ga_instruction_update_pfp>
6661  (*mf, rmi.pfps[mf], gis.ctx, gis.fp_pool);
6662  rmi.instructions.push_back(std::move(pgai));
6663  }
6664 
6665  // An instruction for the base value
6666  pgai = pga_instruction();
6667  switch (pnode->node_type) {
6668  case GA_NODE_VAL: case GA_NODE_ELEMENTARY_VAL:
6669  if (rmi.base.count(mf) == 0 ||
6670  !if_hierarchy.is_compatible(rmi.base_hierarchy[mf])) {
6671  rmi.base_hierarchy[mf].push_back(if_hierarchy);
6672  pgai = std::make_shared<ga_instruction_val_base>
6673  (rmi.base[mf], gis.ctx, *mf, rmi.pfps[mf]);
6674  }
6675  break;
6676  case GA_NODE_XFEM_PLUS_VAL:
6677  if (rmi.xfem_plus_base.count(mf) == 0 ||
6678  !if_hierarchy.is_compatible(rmi.xfem_plus_base_hierarchy[mf]))
6679  {
6680  rmi.xfem_plus_base_hierarchy[mf].push_back(if_hierarchy);
6681  pgai = std::make_shared<ga_instruction_xfem_plus_val_base>
6682  (rmi.xfem_plus_base[mf], gis.ctx, *mf, rmi.pfps[mf]);
6683  }
6684  break;
6685  case GA_NODE_XFEM_MINUS_VAL:
6686  if (rmi.xfem_minus_base.count(mf) == 0 ||
6687  !if_hierarchy.is_compatible(rmi.xfem_minus_base_hierarchy[mf]))
6688  {
6689  rmi.xfem_minus_base_hierarchy[mf].push_back(if_hierarchy);
6690  pgai = std::make_shared<ga_instruction_xfem_minus_val_base>
6691  (rmi.xfem_minus_base[mf], gis.ctx, *mf, rmi.pfps[mf]);
6692  }
6693  break;
6694  case GA_NODE_GRAD: case GA_NODE_DIVERG:
6695  case GA_NODE_ELEMENTARY_GRAD: case GA_NODE_ELEMENTARY_DIVERG:
6696  if (rmi.grad.count(mf) == 0 ||
6697  !if_hierarchy.is_compatible(rmi.grad_hierarchy[mf])) {
6698  rmi.grad_hierarchy[mf].push_back(if_hierarchy);
6699  pgai = std::make_shared<ga_instruction_grad_base>
6700  (rmi.grad[mf], gis.ctx, *mf, rmi.pfps[mf]);
6701  }
6702  break;
6703  case GA_NODE_XFEM_PLUS_GRAD: case GA_NODE_XFEM_PLUS_DIVERG:
6704  if (rmi.xfem_plus_grad.count(mf) == 0 ||
6705  !if_hierarchy.is_compatible(rmi.xfem_plus_grad_hierarchy[mf]))
6706  {
6707  rmi.xfem_plus_grad_hierarchy[mf].push_back(if_hierarchy);
6708  pgai = std::make_shared<ga_instruction_xfem_plus_grad_base>
6709  (rmi.xfem_plus_grad[mf], gis.ctx, *mf, rmi.pfps[mf]);
6710  }
6711  break;
6712  case GA_NODE_XFEM_MINUS_GRAD: case GA_NODE_XFEM_MINUS_DIVERG:
6713  if (rmi.xfem_minus_grad.count(mf) == 0 ||
6714  !if_hierarchy.is_compatible(rmi.xfem_minus_grad_hierarchy[mf]))
6715  {
6716  rmi.xfem_minus_grad_hierarchy[mf].push_back(if_hierarchy);
6717  pgai = std::make_shared<ga_instruction_xfem_minus_grad_base>
6718  (rmi.xfem_minus_grad[mf], gis.ctx, *mf, rmi.pfps[mf]);
6719  }
6720  break;
6721  case GA_NODE_HESS: case GA_NODE_ELEMENTARY_HESS:
6722  if (rmi.hess.count(mf) == 0 ||
6723  !if_hierarchy.is_compatible(rmi.hess_hierarchy[mf])) {
6724  rmi.hess_hierarchy[mf].push_back(if_hierarchy);
6725  pgai = std::make_shared<ga_instruction_hess_base>
6726  (rmi.hess[mf], gis.ctx, *mf, rmi.pfps[mf]);
6727  }
6728  break;
6729  case GA_NODE_XFEM_PLUS_HESS:
6730  if (rmi.xfem_plus_hess.count(mf) == 0 ||
6731  !if_hierarchy.is_compatible(rmi.xfem_plus_hess_hierarchy[mf]))
6732  {
6733  rmi.xfem_plus_hess_hierarchy[mf].push_back(if_hierarchy);
6734  pgai = std::make_shared<ga_instruction_xfem_plus_hess_base>
6735  (rmi.xfem_plus_hess[mf], gis.ctx, *mf, rmi.pfps[mf]);
6736  }
6737  break;
6738  case GA_NODE_XFEM_MINUS_HESS:
6739  if (rmi.xfem_minus_hess.count(mf) == 0 ||
6740  !if_hierarchy.is_compatible(rmi.xfem_minus_hess_hierarchy[mf]))
6741  {
6742  rmi.xfem_minus_hess_hierarchy[mf].push_back(if_hierarchy);
6743  pgai = std::make_shared<ga_instruction_xfem_minus_hess_base>
6744  (rmi.xfem_minus_hess[mf], gis.ctx, *mf, rmi.pfps[mf]);
6745  }
6746  break;
6747 
6748  default : GMM_ASSERT1(false, "Internal error");
6749  }
6750  if (pgai) rmi.instructions.push_back(std::move(pgai));
6751 
6752  // The eval instruction
6753  switch (pnode->node_type) {
6754  case GA_NODE_VAL: // --> t(target_dim*Qmult)
6755  pgai = std::make_shared<ga_instruction_val>
6756  (pnode->tensor(), rmi.base[mf], rmi.local_dofs[pnode->name],
6757  workspace.qdim(pnode->name));
6758  break;
6759  case GA_NODE_GRAD: // --> t(target_dim*Qmult,N)
6760  pgai = std::make_shared<ga_instruction_grad>
6761  (pnode->tensor(), rmi.grad[mf],
6762  rmi.local_dofs[pnode->name], workspace.qdim(pnode->name));
6763  break;
6764  case GA_NODE_HESS: // --> t(target_dim*Qmult,N,N)
6765  pgai = std::make_shared<ga_instruction_hess>
6766  (pnode->tensor(), rmi.hess[mf],
6767  rmi.local_dofs[pnode->name], workspace.qdim(pnode->name));
6768  break;
6769  case GA_NODE_DIVERG: // --> t(1)
6770  pgai = std::make_shared<ga_instruction_diverg>
6771  (pnode->tensor(), rmi.grad[mf],
6772  rmi.local_dofs[pnode->name], workspace.qdim(pnode->name));
6773  break;
6774  case GA_NODE_XFEM_PLUS_VAL: // --> t(target_dim*Qmult)
6775  pgai = std::make_shared<ga_instruction_val>
6776  (pnode->tensor(), rmi.xfem_plus_base[mf],
6777  rmi.local_dofs[pnode->name], workspace.qdim(pnode->name));
6778  break;
6779  case GA_NODE_XFEM_PLUS_GRAD: // --> t(target_dim*Qmult,N)
6780  pgai = std::make_shared<ga_instruction_grad>
6781  (pnode->tensor(), rmi.xfem_plus_grad[mf],
6782  rmi.local_dofs[pnode->name], workspace.qdim(pnode->name));
6783  break;
6784  case GA_NODE_XFEM_PLUS_HESS: // --> t(target_dim*Qmult,N,N)
6785  pgai = std::make_shared<ga_instruction_hess>
6786  (pnode->tensor(), rmi.xfem_plus_hess[mf],
6787  rmi.local_dofs[pnode->name], workspace.qdim(pnode->name));
6788  break;
6789  case GA_NODE_XFEM_PLUS_DIVERG: // --> t(1)
6790  pgai = std::make_shared<ga_instruction_diverg>
6791  (pnode->tensor(), rmi.xfem_plus_grad[mf],
6792  rmi.local_dofs[pnode->name], workspace.qdim(pnode->name));
6793  break;
6794  case GA_NODE_XFEM_MINUS_VAL: // --> t(target_dim*Qmult)
6795  pgai = std::make_shared<ga_instruction_val>
6796  (pnode->tensor(), rmi.xfem_minus_base[mf],
6797  rmi.local_dofs[pnode->name], workspace.qdim(pnode->name));
6798  break;
6799  case GA_NODE_XFEM_MINUS_GRAD: // --> t(target_dim*Qmult,N)
6800  pgai = std::make_shared<ga_instruction_grad>
6801  (pnode->tensor(), rmi.xfem_minus_grad[mf],
6802  rmi.local_dofs[pnode->name], workspace.qdim(pnode->name));
6803  break;
6804  case GA_NODE_XFEM_MINUS_HESS: // --> t(target_dim*Qmult,N,N)
6805  pgai = std::make_shared<ga_instruction_hess>
6806  (pnode->tensor(), rmi.xfem_minus_hess[mf],
6807  rmi.local_dofs[pnode->name], workspace.qdim(pnode->name));
6808  break;
6809  case GA_NODE_XFEM_MINUS_DIVERG: // --> t(1)
6810  pgai = std::make_shared<ga_instruction_diverg>
6811  (pnode->tensor(), rmi.xfem_minus_grad[mf],
6812  rmi.local_dofs[pnode->name], workspace.qdim(pnode->name));
6813  break;
6814  case GA_NODE_ELEMENTARY_VAL:
6815  { // --> t(target_dim*Qmult)
6816  ga_instruction_set::elementary_trans_info &eti
6817  = rmi.elementary_trans_infos
6818  [std::make_tuple(pnode->elementary_name, mfo, mf)];
6819  pgai =
6820  std::make_shared<ga_instruction_elementary_trans_val>
6821  (pnode->tensor(), rmi.base[mf],
6822  rmi.local_dofs[pnode->name],
6823  workspace.qdim(pnode->elementary_target),
6824  workspace.elementary_transformation(pnode->elementary_name),
6825  *mfo, *mf, gis.ctx, eti.M, eti.icv);
6826  }
6827  break;
6828  case GA_NODE_ELEMENTARY_GRAD:
6829  { // --> t(target_dim*Qmult,N)
6830  ga_instruction_set::elementary_trans_info &eti
6831  = rmi.elementary_trans_infos
6832  [std::make_tuple(pnode->elementary_name, mfo, mf)];
6833  pgai =
6834  std::make_shared<ga_instruction_elementary_trans_grad>
6835  (pnode->tensor(), rmi.grad[mf],
6836  rmi.local_dofs[pnode->name],
6837  workspace.qdim(pnode->elementary_target),
6838  workspace.elementary_transformation(pnode->elementary_name),
6839  *mfo, *mf, gis.ctx, eti.M, eti.icv);
6840  }
6841  break;
6842  case GA_NODE_ELEMENTARY_HESS:
6843  { // --> t(target_dim*Qmult,N,N)
6844  ga_instruction_set::elementary_trans_info &eti
6845  = rmi.elementary_trans_infos
6846  [std::make_tuple(pnode->elementary_name, mfo, mf)];
6847  pgai =
6848  std::make_shared<ga_instruction_elementary_trans_hess>
6849  (pnode->tensor(), rmi.hess[mf],
6850  rmi.local_dofs[pnode->name],
6851  workspace.qdim(pnode->elementary_target),
6852  workspace.elementary_transformation(pnode->elementary_name),
6853  *mfo, *mf, gis.ctx, eti.M, eti.icv);
6854  }
6855  break;
6856  case GA_NODE_ELEMENTARY_DIVERG:
6857  { // --> t(1)
6858  ga_instruction_set::elementary_trans_info &eti
6859  = rmi.elementary_trans_infos
6860  [std::make_tuple(pnode->elementary_name, mfo, mf)];
6861  pgai =
6862  std::make_shared<ga_instruction_elementary_trans_diverg>
6863  (pnode->tensor(), rmi.grad[mf],
6864  rmi.local_dofs[pnode->name],
6865  workspace.qdim(pnode->elementary_target),
6866  workspace.elementary_transformation(pnode->elementary_name),
6867  *mfo, *mf, gis.ctx, eti.M, eti.icv);
6868  }
6869  break;
6870  default: break;
6871  }
6872  rmi.instructions.push_back(std::move(pgai));
6873  }
6874  }
6875  }
6876  break;
6877 
6878  case GA_NODE_SECONDARY_DOMAIN_VAL: case GA_NODE_SECONDARY_DOMAIN_GRAD:
6879  case GA_NODE_SECONDARY_DOMAIN_HESS: case GA_NODE_SECONDARY_DOMAIN_DIVERG:
6880  {
6881  GMM_ASSERT1(!function_case, "internal error");
6882  const mesh_fem *mf = workspace.associated_mf(pnode->name);
6883  const im_data *imd = workspace.associated_im_data(pnode->name);
6884  const std::string &intn = pnode->interpolate_name;
6885  auto &sdi = rmi.secondary_domain_infos;
6886 
6887  fem_interpolation_context *pctx = &(sdi.ctx);
6888  papprox_integration pai = sdi.pai;
6889  psecondary_domain psd = workspace.secondary_domain(intn);
6890 
6891  if (imd) {
6892  pgai = std::make_shared<ga_instruction_extract_local_im_data>
6893  (pnode->tensor(), *imd, workspace.value(pnode->name),
6894  pai, *pctx, workspace.qdim(pnode->name));
6895  rmi.instructions.push_back(std::move(pgai));
6896  } else {
6897  GMM_ASSERT1(mf, "Internal error");
6898  GMM_ASSERT1(&(mf->linked_mesh()) == &(psd->mim().linked_mesh()),
6899  "The finite element of variable " << pnode->name <<
6900  " has to be defined on the same mesh as the "
6901  "integration method or interpolation used on the "
6902  "secondary domain");
6903 
6904  // An instruction for extracting local dofs of the variable.
6905  if (sdi.local_dofs.count(pnode->name) == 0) {
6906  sdi.local_dofs[pnode->name] = base_vector(1);
6907  extend_variable_in_gis(workspace, pnode->name, gis);
6908  size_type qmult2 = mf->get_qdim();
6909  if (qmult2 > 1 && !(mf->is_uniformly_vectorized()))
6910  qmult2 = size_type(-1);
6911  pgai = std::make_shared<ga_instruction_slice_local_dofs>
6912  (*mf, *(gis.extended_vars[pnode->name]), *pctx,
6913  sdi.local_dofs[pnode->name],
6914  workspace.qdim(pnode->name) / mf->get_qdim(), qmult2);
6915  rmi.elt_instructions.push_back(std::move(pgai));
6916  }
6917 
6918  // An instruction for pfp update
6919  if (mf->is_uniform()) {
6920  if (sdi.pfps.count(mf) == 0) {
6921  sdi.pfps[mf] = 0;
6922  pgai = std::make_shared<ga_instruction_update_pfp>
6923  (*mf, sdi.pfps[mf], *pctx, gis.fp_pool);
6924  rmi.begin_instructions.push_back(std::move(pgai));
6925  }
6926  } else if (sdi.pfps.count(mf) == 0 ||
6927  !if_hierarchy.is_compatible(rmi.pfp_hierarchy[mf])) {
6928  rmi.pfp_hierarchy[mf].push_back(if_hierarchy);
6929  sdi.pfps[mf] = 0;
6930  pgai = std::make_shared<ga_instruction_update_pfp>
6931  (*mf, sdi.pfps[mf], *pctx, gis.fp_pool);
6932  rmi.instructions.push_back(std::move(pgai));
6933  }
6934 
6935  // An instruction for the base value
6936  pgai = pga_instruction();
6937  switch (pnode->node_type) {
6938  case GA_NODE_SECONDARY_DOMAIN_VAL:
6939  if (sdi.base.count(mf) == 0 ||
6940  !(if_hierarchy.is_compatible(rmi.base_hierarchy[mf]))) {
6941  rmi.base_hierarchy[mf].push_back(if_hierarchy);
6942  pgai = std::make_shared<ga_instruction_val_base>
6943  (sdi.base[mf], *pctx, *mf, sdi.pfps[mf]);
6944  }
6945  break;
6946  case GA_NODE_SECONDARY_DOMAIN_GRAD:
6947  case GA_NODE_SECONDARY_DOMAIN_DIVERG:
6948  if (sdi.grad.count(mf) == 0 ||
6949  !(if_hierarchy.is_compatible(rmi.grad_hierarchy[mf]))) {
6950  rmi.grad_hierarchy[mf].push_back(if_hierarchy);
6951  pgai = std::make_shared<ga_instruction_grad_base>
6952  (sdi.grad[mf], *pctx, *mf, sdi.pfps[mf]);
6953  }
6954  break;
6955  case GA_NODE_SECONDARY_DOMAIN_HESS:
6956  if (sdi.hess.count(mf) == 0 ||
6957  !(if_hierarchy.is_compatible(rmi.hess_hierarchy[mf]))) {
6958  rmi.hess_hierarchy[mf].push_back(if_hierarchy);
6959  pgai = std::make_shared<ga_instruction_hess_base>
6960  (sdi.hess[mf], *pctx, *mf, sdi.pfps[mf]);
6961  }
6962  break;
6963  default : GMM_ASSERT1(false, "Internal error");
6964  }
6965  if (pgai) rmi.instructions.push_back(std::move(pgai));
6966 
6967  // The eval instruction
6968  switch (pnode->node_type) {
6969  case GA_NODE_SECONDARY_DOMAIN_VAL: // --> t(target_dim*Qmult)
6970  pgai = std::make_shared<ga_instruction_val>
6971  (pnode->tensor(), sdi.base[mf], sdi.local_dofs[pnode->name],
6972  workspace.qdim(pnode->name));
6973  break;
6974  case GA_NODE_SECONDARY_DOMAIN_GRAD: // --> t(target_dim*Qmult,N)
6975  pgai = std::make_shared<ga_instruction_grad>
6976  (pnode->tensor(), sdi.grad[mf],
6977  sdi.local_dofs[pnode->name], workspace.qdim(pnode->name));
6978  break;
6979  case GA_NODE_SECONDARY_DOMAIN_HESS: // --> t(target_dim*Qmult,N,N)
6980  pgai = std::make_shared<ga_instruction_hess>
6981  (pnode->tensor(), sdi.hess[mf],
6982  sdi.local_dofs[pnode->name], workspace.qdim(pnode->name));
6983  break;
6984  case GA_NODE_SECONDARY_DOMAIN_DIVERG: // --> t(1)
6985  pgai = std::make_shared<ga_instruction_diverg>
6986  (pnode->tensor(), sdi.grad[mf],
6987  sdi.local_dofs[pnode->name], workspace.qdim(pnode->name));
6988  break;
6989  default: break;
6990  }
6991  rmi.instructions.push_back(std::move(pgai));
6992  }
6993  }
6994  break;
6995 
6996  case GA_NODE_INTERPOLATE_VAL: case GA_NODE_INTERPOLATE_GRAD:
6997  case GA_NODE_INTERPOLATE_HESS: case GA_NODE_INTERPOLATE_DIVERG:
6998  {
6999  extend_variable_in_gis(workspace, pnode->name, gis);
7000 
7001  const mesh_fem *mfn = workspace.associated_mf(pnode->name), **mfg = 0;
7002  const std::string &intn = pnode->interpolate_name;
7003  const base_vector *Un = gis.extended_vars[pnode->name], **Ug = 0;
7004  fem_interpolation_context *pctx = &(rmi.interpolate_infos[intn].ctx);
7005  const mesh **m2 = &(rmi.interpolate_infos[intn].m);
7006  if (workspace.variable_group_exists(pnode->name)) {
7007  ga_instruction_set::variable_group_info &vgi =
7008  rmi.interpolate_infos[intn].groups_info[pnode->name];
7009  mfg = &(vgi.mf); mfn = 0; Ug = &(vgi.U); Un = 0;
7010  }
7011 
7012  if (pnode->node_type == GA_NODE_INTERPOLATE_VAL) {
7013  // --> t(target_dim*Qmult)
7014  pgai = std::make_shared<ga_instruction_interpolate_val>
7015  (pnode->tensor(), m2, mfn, mfg, Un, Ug, *pctx,
7016  workspace.qdim(pnode->name),
7017  gis.ipt, gis.fp_pool, rmi.interpolate_infos[intn]);
7018  } else if (pnode->node_type == GA_NODE_INTERPOLATE_GRAD) {
7019  // --> t(target_dim*Qmult,N)
7020  pgai = std::make_shared<ga_instruction_interpolate_grad>
7021  (pnode->tensor(), m2, mfn, mfg, Un, Ug, *pctx,
7022  workspace.qdim(pnode->name),
7023  gis.ipt, gis.fp_pool, rmi.interpolate_infos[intn]);
7024  } else if (pnode->node_type == GA_NODE_INTERPOLATE_HESS) {
7025  // --> t(target_dim*Qmult,N,N)
7026  pgai = std::make_shared<ga_instruction_interpolate_hess>
7027  (pnode->tensor(), m2, mfn, mfg, Un, Ug, *pctx,
7028  workspace.qdim(pnode->name),
7029  gis.ipt, gis.fp_pool, rmi.interpolate_infos[intn]);
7030  } else { // --> t(1)
7031  pgai = std::make_shared<ga_instruction_interpolate_diverg>
7032  (pnode->tensor(), m2, mfn, mfg, Un, Ug, *pctx,
7033  workspace.qdim(pnode->name),
7034  gis.ipt, gis.fp_pool, rmi.interpolate_infos[intn]);
7035  }
7036  rmi.instructions.push_back(std::move(pgai));
7037  }
7038  break;
7039 
7040  case GA_NODE_INTERPOLATE_DERIVATIVE:
7041  GMM_ASSERT1(!function_case,
7042  "No use of Interpolate is allowed in functions");
7043  pgai = std::make_shared<ga_instruction_copy_tensor_possibly_void>
7044  (pnode->tensor(),
7045  rmi.interpolate_infos[pnode->interpolate_name_der]
7046  .derivatives[var_trans_pair(pnode->name, pnode->interpolate_name)]);
7047  rmi.instructions.push_back(std::move(pgai));
7048  break;
7049 
7050  case GA_NODE_VAL_TEST: case GA_NODE_GRAD_TEST:
7051  case GA_NODE_HESS_TEST: case GA_NODE_DIVERG_TEST:
7052  case GA_NODE_ELEMENTARY_VAL_TEST: case GA_NODE_ELEMENTARY_GRAD_TEST:
7053  case GA_NODE_ELEMENTARY_HESS_TEST: case GA_NODE_ELEMENTARY_DIVERG_TEST:
7054  case GA_NODE_XFEM_PLUS_VAL_TEST: case GA_NODE_XFEM_PLUS_GRAD_TEST:
7055  case GA_NODE_XFEM_PLUS_HESS_TEST: case GA_NODE_XFEM_PLUS_DIVERG_TEST:
7056  case GA_NODE_XFEM_MINUS_VAL_TEST: case GA_NODE_XFEM_MINUS_GRAD_TEST:
7057  case GA_NODE_XFEM_MINUS_HESS_TEST: case GA_NODE_XFEM_MINUS_DIVERG_TEST:
7058  // GMM_ASSERT1(!function_case,
7059  // "Test functions not allowed in functions");
7060  {
7061  bool is_elementary = (pnode->node_type==GA_NODE_ELEMENTARY_VAL_TEST ||
7062  pnode->node_type==GA_NODE_ELEMENTARY_GRAD_TEST ||
7063  pnode->node_type==GA_NODE_ELEMENTARY_HESS_TEST ||
7064  pnode->node_type==GA_NODE_ELEMENTARY_DIVERG_TEST);
7065  const mesh_fem *mf = workspace.associated_mf(pnode->name), *mfo=mf;
7066  if (is_elementary) {
7067  mf = workspace.associated_mf(pnode->elementary_target);
7068  GMM_ASSERT1(mf && mfo,
7069  "Wrong context for elementary transformation");
7070  GMM_ASSERT1(&(mfo->linked_mesh()) == &(m),
7071  "The finite element of variable " << pnode->name
7072  << " has to be defined on the same mesh as the "
7073  << "integration method or interpolation used");
7074  }
7075 
7076  if (mf) {
7077  GMM_ASSERT1(&(mf->linked_mesh()) == &(m),
7078  "The finite element of variable " <<
7079  (is_elementary ? pnode->elementary_target : pnode->name)
7080  << " and the applied integration method have to be"
7081  << " defined on the same mesh");
7082 
7083  // An instruction for pfp update
7084  if (is_uniform) {
7085  if (rmi.pfps.count(mf) == 0) {
7086  rmi.pfps[mf] = 0;
7087  pgai = std::make_shared<ga_instruction_update_pfp>
7088  (*mf, rmi.pfps[mf], gis.ctx, gis.fp_pool);
7089  rmi.begin_instructions.push_back(std::move(pgai));
7090  }
7091  } else if (rmi.pfps.count(mf) == 0 ||
7092  !if_hierarchy.is_compatible(rmi.pfp_hierarchy[mf])) {
7093  rmi.pfp_hierarchy[mf].push_back(if_hierarchy);
7094  rmi.pfps[mf] = 0;
7095  pgai = std::make_shared<ga_instruction_update_pfp>
7096  (*mf, rmi.pfps[mf], gis.ctx, gis.fp_pool);
7097  rmi.instructions.push_back(std::move(pgai));
7098  }
7099 
7100  // An instruction for the base value
7101  pgai = pga_instruction();
7102  switch (pnode->node_type) {
7103  case GA_NODE_VAL_TEST: case GA_NODE_ELEMENTARY_VAL_TEST:
7104  if (rmi.base.count(mf) == 0 ||
7105  !if_hierarchy.is_compatible(rmi.base_hierarchy[mf])) {
7106  rmi.base_hierarchy[mf].push_back(if_hierarchy);
7107  pgai = std::make_shared<ga_instruction_val_base>
7108  (rmi.base[mf], gis.ctx, *mf, rmi.pfps[mf]);
7109  }
7110  break;
7111  case GA_NODE_XFEM_PLUS_VAL_TEST:
7112  if (rmi.xfem_plus_base.count(mf) == 0 ||
7113  !if_hierarchy.is_compatible(rmi.xfem_plus_base_hierarchy[mf]))
7114  {
7115  rmi.xfem_plus_base_hierarchy[mf].push_back(if_hierarchy);
7116  pgai = std::make_shared<ga_instruction_xfem_plus_val_base>
7117  (rmi.xfem_plus_base[mf], gis.ctx, *mf, rmi.pfps[mf]);
7118  }
7119  break;
7120  case GA_NODE_XFEM_MINUS_VAL_TEST:
7121  if (rmi.xfem_minus_base.count(mf) == 0 ||
7122  !if_hierarchy.is_compatible(rmi.xfem_minus_base_hierarchy[mf]))
7123  {
7124  rmi.xfem_minus_base_hierarchy[mf].push_back(if_hierarchy);
7125  pgai = std::make_shared<ga_instruction_xfem_minus_val_base>
7126  (rmi.xfem_minus_base[mf], gis.ctx, *mf, rmi.pfps[mf]);
7127  }
7128  break;
7129  case GA_NODE_GRAD_TEST: case GA_NODE_DIVERG_TEST:
7130  case GA_NODE_ELEMENTARY_GRAD_TEST:
7131  case GA_NODE_ELEMENTARY_DIVERG_TEST:
7132  if (rmi.grad.count(mf) == 0 ||
7133  !if_hierarchy.is_compatible(rmi.grad_hierarchy[mf])) {
7134  rmi.grad_hierarchy[mf].push_back(if_hierarchy);
7135  pgai = std::make_shared<ga_instruction_grad_base>
7136  (rmi.grad[mf], gis.ctx, *mf, rmi.pfps[mf]);
7137  }
7138  break;
7139  case GA_NODE_XFEM_PLUS_GRAD_TEST: case GA_NODE_XFEM_PLUS_DIVERG_TEST:
7140  if (rmi.xfem_plus_grad.count(mf) == 0 ||
7141  !if_hierarchy.is_compatible(rmi.xfem_plus_grad_hierarchy[mf]))
7142  {
7143  rmi.xfem_plus_grad_hierarchy[mf].push_back(if_hierarchy);
7144  pgai = std::make_shared<ga_instruction_xfem_plus_grad_base>
7145  (rmi.xfem_plus_grad[mf], gis.ctx, *mf, rmi.pfps[mf]);
7146  }
7147  break;
7148  case GA_NODE_XFEM_MINUS_GRAD_TEST:
7149  case GA_NODE_XFEM_MINUS_DIVERG_TEST:
7150  if (rmi.xfem_minus_grad.count(mf) == 0 ||
7151  !if_hierarchy.is_compatible(rmi.xfem_minus_grad_hierarchy[mf]))
7152  {
7153  rmi.xfem_minus_grad_hierarchy[mf].push_back(if_hierarchy);
7154  pgai = std::make_shared<ga_instruction_xfem_minus_grad_base>
7155  (rmi.xfem_minus_grad[mf], gis.ctx, *mf, rmi.pfps[mf]);
7156  }
7157  break;
7158  case GA_NODE_HESS_TEST: case GA_NODE_ELEMENTARY_HESS_TEST:
7159  if (rmi.hess.count(mf) == 0 ||
7160  !if_hierarchy.is_compatible(rmi.hess_hierarchy[mf])) {
7161  rmi.hess_hierarchy[mf].push_back(if_hierarchy);
7162  pgai = std::make_shared<ga_instruction_hess_base>
7163  (rmi.hess[mf], gis.ctx, *mf, rmi.pfps[mf]);
7164  }
7165  break;
7166  case GA_NODE_XFEM_PLUS_HESS_TEST:
7167  if (rmi.xfem_plus_hess.count(mf) == 0 ||
7168  !if_hierarchy.is_compatible(rmi.xfem_plus_hess_hierarchy[mf]))
7169  {
7170  rmi.xfem_plus_hess_hierarchy[mf].push_back(if_hierarchy);
7171  pgai = std::make_shared<ga_instruction_xfem_plus_hess_base>
7172  (rmi.xfem_plus_hess[mf], gis.ctx, *mf, rmi.pfps[mf]);
7173  }
7174  break;
7175  case GA_NODE_XFEM_MINUS_HESS_TEST:
7176  if (rmi.xfem_minus_hess.count(mf) == 0 ||
7177  !if_hierarchy.is_compatible(rmi.xfem_minus_hess_hierarchy[mf]))
7178  {
7179  rmi.xfem_minus_hess_hierarchy[mf].push_back(if_hierarchy);
7180  pgai = std::make_shared<ga_instruction_xfem_minus_hess_base>
7181  (rmi.xfem_minus_hess[mf], gis.ctx, *mf, rmi.pfps[mf]);
7182  }
7183  break;
7184 
7185  default : GMM_ASSERT1(false, "Internal error");
7186  }
7187  if (pgai) rmi.instructions.push_back(std::move(pgai));
7188 
7189  // The copy of the real_base_value
7190  switch(pnode->node_type) {
7191  case GA_NODE_VAL_TEST:
7192  // --> t(Qmult*ndof,Qmult*target_dim)
7193  if (mf->get_qdim() > 1 && mf->is_uniformly_vectorized()) {
7194  pnode->t.set_sparsity(1, mf->get_qdim());
7195  tensor_to_clear = true;
7196  pgai = std::make_shared<ga_instruction_copy_vect_val_base>
7197  (pnode->tensor(), rmi.base[mf], mf->get_qdim());
7198  } else {
7199  pgai = std::make_shared<ga_instruction_copy_val_base>
7200  (pnode->tensor(), rmi.base[mf], mf->get_qdim());
7201  }
7202  break;
7203  case GA_NODE_GRAD_TEST:
7204  // --> t(Qmult*ndof,Qmult*target_dim,N)
7205  if (mf->get_qdim() > 1 && mf->is_uniformly_vectorized()) {
7206  pnode->t.set_sparsity(2, mf->get_qdim());
7207  tensor_to_clear = true;
7208  pgai = std::make_shared<ga_instruction_copy_vect_grad_base>
7209  (pnode->tensor(), rmi.grad[mf], mf->get_qdim());
7210  } else {
7211  pgai = std::make_shared<ga_instruction_copy_grad_base>
7212  (pnode->tensor(), rmi.grad[mf], mf->get_qdim());
7213  }
7214  break;
7215  case GA_NODE_HESS_TEST:
7216  // --> t(Qmult*ndof,Qmult*target_dim,N,N)
7217  pgai = std::make_shared<ga_instruction_copy_hess_base>
7218  (pnode->tensor(), rmi.hess[mf], mf->get_qdim());
7219  if (mf->get_qdim() > 1 && mf->is_uniformly_vectorized())
7220  pnode->t.set_sparsity(3, mf->get_qdim());
7221  break;
7222  case GA_NODE_DIVERG_TEST:
7223  // --> t(Qmult*ndof)
7224  pgai = std::make_shared<ga_instruction_copy_diverg_base>
7225  (pnode->tensor(), rmi.grad[mf], mf->get_qdim());
7226  break;
7227  case GA_NODE_XFEM_PLUS_VAL_TEST:
7228  // -->t(Qmult*ndof,Qmult*target_dim)
7229  pgai = std::make_shared<ga_instruction_copy_val_base>
7230  (pnode->tensor(), rmi.xfem_plus_base[mf], mf->get_qdim());
7231  if (mf->get_qdim() > 1 && mf->is_uniformly_vectorized())
7232  pnode->t.set_sparsity(1, mf->get_qdim());
7233  break;
7234  case GA_NODE_XFEM_PLUS_GRAD_TEST:
7235  // --> t(Qmult*ndof,Qmult*target_dim,N)
7236  pgai = std::make_shared<ga_instruction_copy_grad_base>
7237  (pnode->tensor(), rmi.xfem_plus_grad[mf], mf->get_qdim());
7238  if (mf->get_qdim() > 1 && mf->is_uniformly_vectorized())
7239  pnode->t.set_sparsity(2, mf->get_qdim());
7240  break;
7241  case GA_NODE_XFEM_PLUS_HESS_TEST:
7242  // --> t(Qmult*ndof,Qmult*target_dim,N,N)
7243  pgai = std::make_shared<ga_instruction_copy_hess_base>
7244  (pnode->tensor(), rmi.xfem_plus_hess[mf], mf->get_qdim());
7245  if (mf->get_qdim() > 1 && mf->is_uniformly_vectorized())
7246  pnode->t.set_sparsity(3, mf->get_qdim());
7247  break;
7248  case GA_NODE_XFEM_PLUS_DIVERG_TEST:
7249  // --> t(Qmult*ndof)
7250  pgai = std::make_shared<ga_instruction_copy_diverg_base>
7251  (pnode->tensor(), rmi.xfem_plus_grad[mf], mf->get_qdim());
7252  break;
7253  case GA_NODE_XFEM_MINUS_VAL_TEST:
7254  // -->t(Qmult*ndof,Qmult*target_dim)
7255  pgai = std::make_shared<ga_instruction_copy_val_base>
7256  (pnode->tensor(), rmi.xfem_minus_base[mf], mf->get_qdim());
7257  if (mf->get_qdim() > 1 && mf->is_uniformly_vectorized())
7258  pnode->t.set_sparsity(1, mf->get_qdim());
7259  break;
7260  case GA_NODE_XFEM_MINUS_GRAD_TEST:
7261  // --> t(Qmult*ndof,Qmult*target_dim,N)
7262  pgai = std::make_shared<ga_instruction_copy_grad_base>
7263  (pnode->tensor(), rmi.xfem_minus_grad[mf], mf->get_qdim());
7264  if (mf->get_qdim() > 1 && mf->is_uniformly_vectorized())
7265  pnode->t.set_sparsity(2, mf->get_qdim());
7266  break;
7267  case GA_NODE_XFEM_MINUS_HESS_TEST:
7268  // --> t(Qmult*ndof,Qmult*target_dim,N,N)
7269  pgai = std::make_shared<ga_instruction_copy_hess_base>
7270  (pnode->tensor(), rmi.xfem_minus_hess[mf], mf->get_qdim());
7271  if (mf->get_qdim() > 1 && mf->is_uniformly_vectorized())
7272  pnode->t.set_sparsity(3, mf->get_qdim());
7273  break;
7274  case GA_NODE_XFEM_MINUS_DIVERG_TEST:
7275  // --> t(Qmult*ndof)
7276  pgai = std::make_shared<ga_instruction_copy_diverg_base>
7277  (pnode->tensor(), rmi.xfem_minus_grad[mf], mf->get_qdim());
7278  break;
7279  case GA_NODE_ELEMENTARY_VAL_TEST:
7280  { // --> t(Qmult*ndof,Qmult*target_dim)
7281  ga_instruction_set::elementary_trans_info &eti
7282  = rmi.elementary_trans_infos
7283  [std::make_tuple(pnode->elementary_name, mfo, mf)];
7284  pgai =
7285  std::make_shared<ga_instruction_elementary_trans_val_base>
7286  (pnode->tensor(), rmi.base[mf], mf->get_qdim(),
7287  workspace.elementary_transformation(pnode->elementary_name),
7288  *mfo, *mf, gis.ctx, eti.M, eti.icv);
7289  }
7290  break;
7291  case GA_NODE_ELEMENTARY_GRAD_TEST:
7292  { // --> t(Qmult*ndof,Qmult*target_dim,N)
7293  ga_instruction_set::elementary_trans_info &eti
7294  = rmi.elementary_trans_infos
7295  [std::make_tuple(pnode->elementary_name, mfo, mf)];
7296  pgai =
7297  std::make_shared<ga_instruction_elementary_trans_grad_base>
7298  (pnode->tensor(), rmi.grad[mf], mf->get_qdim(),
7299  workspace.elementary_transformation(pnode->elementary_name),
7300  *mfo, *mf, gis.ctx, eti.M, eti.icv);
7301  }
7302  break;
7303  case GA_NODE_ELEMENTARY_HESS_TEST:
7304  { // --> t(Qmult*ndof,Qmult*target_dim,N,N)
7305  ga_instruction_set::elementary_trans_info &eti
7306  = rmi.elementary_trans_infos
7307  [std::make_tuple(pnode->elementary_name, mfo, mf)];
7308  pgai =
7309  std::make_shared<ga_instruction_elementary_trans_hess_base>
7310  (pnode->tensor(), rmi.hess[mf], mf->get_qdim(),
7311  workspace.elementary_transformation(pnode->elementary_name),
7312  *mfo, *mf, gis.ctx, eti.M, eti.icv);
7313  }
7314  break;
7315  case GA_NODE_ELEMENTARY_DIVERG_TEST:
7316  { // --> t(Qmult*ndof)
7317  ga_instruction_set::elementary_trans_info &eti
7318  = rmi.elementary_trans_infos
7319  [std::make_tuple(pnode->elementary_name, mfo, mf)];
7320  pgai =
7321  std::make_shared<ga_instruction_elementary_trans_diverg_base>
7322  (pnode->tensor(), rmi.grad[mf], mf->get_qdim(),
7323  workspace.elementary_transformation(pnode->elementary_name),
7324  *mfo, *mf, gis.ctx, eti.M, eti.icv);
7325  }
7326  break;
7327  default: break;
7328  }
7329  if (pgai) rmi.instructions.push_back(std::move(pgai));
7330  }
7331  workspace.add_temporary_interval_for_unreduced_variable(pnode->name);
7332  }
7333  break;
7334 
7335  case GA_NODE_SECONDARY_DOMAIN_VAL_TEST:
7336  case GA_NODE_SECONDARY_DOMAIN_GRAD_TEST:
7337  case GA_NODE_SECONDARY_DOMAIN_HESS_TEST:
7338  case GA_NODE_SECONDARY_DOMAIN_DIVERG_TEST:
7339  {
7340  GMM_ASSERT1(!function_case, "internal error");
7341  const mesh_fem *mf = workspace.associated_mf(pnode->name);
7342  const std::string &intn = pnode->interpolate_name;
7343  auto &sdi = rmi.secondary_domain_infos;
7344 
7345  fem_interpolation_context *pctx = &(sdi.ctx);
7346  papprox_integration pai = sdi.pai;
7347  psecondary_domain psd = workspace.secondary_domain(intn);
7348  if (mf) {
7349  GMM_ASSERT1(&(mf->linked_mesh()) == &(psd->mim().linked_mesh()),
7350  "The finite element of variable " << pnode->name <<
7351  " and the applied integration method have to be"
7352  " defined on the same mesh for secondary domain");
7353 
7354  // An instruction for pfp update
7355  if (is_uniform) {
7356  if (sdi.pfps.count(mf) == 0) {
7357  sdi.pfps[mf] = 0;
7358  pgai = std::make_shared<ga_instruction_update_pfp>
7359  (*mf, sdi.pfps[mf], *pctx, gis.fp_pool);
7360  rmi.begin_instructions.push_back(std::move(pgai));
7361  }
7362  } else if (sdi.pfps.count(mf) == 0 ||
7363  !if_hierarchy.is_compatible(rmi.pfp_hierarchy[mf])) {
7364  rmi.pfp_hierarchy[mf].push_back(if_hierarchy);
7365  sdi.pfps[mf] = 0;
7366  pgai = std::make_shared<ga_instruction_update_pfp>
7367  (*mf, sdi.pfps[mf], *pctx, gis.fp_pool);
7368  rmi.instructions.push_back(std::move(pgai));
7369  }
7370 
7371  // An instruction for the base value
7372  pgai = pga_instruction();
7373  switch (pnode->node_type) {
7374  case GA_NODE_SECONDARY_DOMAIN_VAL_TEST:
7375  if (sdi.base.count(mf) == 0 ||
7376  !(if_hierarchy.is_compatible(rmi.base_hierarchy[mf]))) {
7377  rmi.base_hierarchy[mf].push_back(if_hierarchy);
7378  pgai = std::make_shared<ga_instruction_val_base>
7379  (sdi.base[mf], *pctx, *mf, sdi.pfps[mf]);
7380  }
7381  break;
7382  case GA_NODE_SECONDARY_DOMAIN_GRAD_TEST:
7383  case GA_NODE_SECONDARY_DOMAIN_DIVERG_TEST:
7384  if (sdi.grad.count(mf) == 0 ||
7385  !(if_hierarchy.is_compatible(rmi.grad_hierarchy[mf]))) {
7386  rmi.grad_hierarchy[mf].push_back(if_hierarchy);
7387  pgai = std::make_shared<ga_instruction_grad_base>
7388  (sdi.grad[mf], *pctx, *mf, sdi.pfps[mf]);
7389  }
7390  break;
7391  case GA_NODE_SECONDARY_DOMAIN_HESS_TEST:
7392  if (sdi.hess.count(mf) == 0 ||
7393  !(if_hierarchy.is_compatible(rmi.hess_hierarchy[mf]))) {
7394  rmi.hess_hierarchy[mf].push_back(if_hierarchy);
7395  pgai = std::make_shared<ga_instruction_hess_base>
7396  (sdi.hess[mf], *pctx, *mf, sdi.pfps[mf]);
7397  }
7398  break;
7399  default : GMM_ASSERT1(false, "Internal error");
7400  }
7401  if (pgai) rmi.instructions.push_back(std::move(pgai));
7402 
7403  // The copy of the real_base_value
7404  switch(pnode->node_type) {
7405  case GA_NODE_SECONDARY_DOMAIN_VAL_TEST:
7406  // --> t(Qmult*ndof,Qmult*target_dim)
7407  if (mf->get_qdim() > 1 && mf->is_uniformly_vectorized()) {
7408  pnode->t.set_sparsity(1, mf->get_qdim());
7409  tensor_to_clear = true;
7410  pgai = std::make_shared<ga_instruction_copy_vect_val_base>
7411  (pnode->tensor(), sdi.base[mf], mf->get_qdim());
7412  } else {
7413  pgai = std::make_shared<ga_instruction_copy_val_base>
7414  (pnode->tensor(), sdi.base[mf], mf->get_qdim());
7415  }
7416  break;
7417  case GA_NODE_SECONDARY_DOMAIN_GRAD_TEST:
7418  // --> t(Qmult*ndof,Qmult*target_dim,N)
7419  if (mf->get_qdim() > 1 && mf->is_uniformly_vectorized()) {
7420  pnode->t.set_sparsity(2, mf->get_qdim());
7421  tensor_to_clear = true;
7422  pgai = std::make_shared<ga_instruction_copy_vect_grad_base>
7423  (pnode->tensor(), sdi.grad[mf], mf->get_qdim());
7424  } else {
7425  pgai = std::make_shared<ga_instruction_copy_grad_base>
7426  (pnode->tensor(), sdi.grad[mf], mf->get_qdim());
7427  }
7428  break;
7429  case GA_NODE_SECONDARY_DOMAIN_HESS_TEST:
7430  // --> t(Qmult*ndof,Qmult*target_dim,N,N)
7431  pgai = std::make_shared<ga_instruction_copy_hess_base>
7432  (pnode->tensor(), sdi.hess[mf], mf->get_qdim());
7433  if (mf->get_qdim() > 1 && mf->is_uniformly_vectorized())
7434  pnode->t.set_sparsity(3, mf->get_qdim());
7435  break;
7436  case GA_NODE_SECONDARY_DOMAIN_DIVERG_TEST:
7437  // --> t(Qmult*ndof)
7438  pgai = std::make_shared<ga_instruction_copy_diverg_base>
7439  (pnode->tensor(), sdi.grad[mf], mf->get_qdim());
7440  break;
7441  default: break;
7442  }
7443  if (pgai) rmi.instructions.push_back(std::move(pgai));
7444  }
7445  workspace.add_temporary_interval_for_unreduced_variable(pnode->name);
7446  }
7447  break;
7448 
7449  case GA_NODE_INTERPOLATE_VAL_TEST: case GA_NODE_INTERPOLATE_GRAD_TEST:
7450  case GA_NODE_INTERPOLATE_HESS_TEST: case GA_NODE_INTERPOLATE_DIVERG_TEST:
7451  {
7452  const mesh_fem *mfn = workspace.associated_mf(pnode->name), **mfg = 0;
7453  const std::string &intn = pnode->interpolate_name;
7454  const mesh **m2 = &(rmi.interpolate_infos[intn].m);
7455  if (workspace.variable_group_exists(pnode->name)) {
7456  ga_instruction_set::variable_group_info &vgi =
7457  rmi.interpolate_infos[intn].groups_info[pnode->name];
7458  mfg = &(vgi.mf); mfn = 0;
7459  }
7460 
7461  if (pnode->node_type == GA_NODE_INTERPOLATE_VAL_TEST) {
7462  // --> t(Qmult*ndof,Qmult*target_dim)
7463  pgai = std::make_shared<ga_instruction_interpolate_val_base>
7464  (pnode->tensor(), m2, mfn, mfg, gis.ipt,
7465  workspace.qdim(pnode->name), rmi.interpolate_infos[intn],
7466  gis.fp_pool);
7467  } else if (pnode->node_type == GA_NODE_INTERPOLATE_GRAD_TEST) {
7468  // --> t(Qmult*ndof,Qmult*target_dim,N)
7469  pgai = std::make_shared<ga_instruction_interpolate_grad_base>
7470  (pnode->tensor(), m2, mfn, mfg, gis.ipt,
7471  workspace.qdim(pnode->name),
7472  rmi.interpolate_infos[intn], gis.fp_pool);
7473  } else if (pnode->node_type == GA_NODE_INTERPOLATE_HESS_TEST) {
7474  // --> t(Qmult*ndof,Qmult*target_dim,N,N)
7475  pgai = std::make_shared<ga_instruction_interpolate_hess_base>
7476  (pnode->tensor(), m2, mfn, mfg, gis.ipt,
7477  workspace.qdim(pnode->name),
7478  rmi.interpolate_infos[intn], gis.fp_pool);
7479  } else { // if (pnode->node_type == GA_NODE_INTERPOLATE_DIVERG_TEST) {
7480  // --> t(Qmult*ndof)
7481  pgai = std::make_shared<ga_instruction_interpolate_diverg_base>
7482  (pnode->tensor(), m2, mfn, mfg, gis.ipt,
7483  workspace.qdim(pnode->name),
7484  rmi.interpolate_infos[intn], gis.fp_pool);
7485  }
7486  rmi.instructions.push_back(std::move(pgai));
7487  workspace.add_temporary_interval_for_unreduced_variable(pnode->name);
7488  }
7489  break;
7490 
7491  case GA_NODE_OP:
7492  switch(pnode->op_type) {
7493 
7494  case GA_PLUS:
7495  if (pnode->tensor().size() == 1) {
7496  GA_DEBUG_ASSERT(child0->tensor().size() == 1,
7497  "Internal error: child0 not scalar");
7498  GA_DEBUG_ASSERT(child1->tensor().size() == 1,
7499  "Internal error: child1 not scalar");
7500  pgai = std::make_shared<ga_instruction_scalar_add>
7501  (pnode->tensor()[0], child0->tensor()[0], child1->tensor()[0]);
7502  } else {
7503  pgai = std::make_shared<ga_instruction_add>
7504  (pnode->tensor(), child0->tensor(), child1->tensor());
7505  }
7506  ga_propagate_sum_structure
7507  (pnode->t, child0->t, child1->t,
7508  pnode->nb_test_functions() == 1 && pnode->tensor_order() == 2
7509  && ga_is_sum_with_transpose(child0, child1, workspace));
7510  rmi.instructions.push_back(std::move(pgai));
7511  break;
7512 
7513  case GA_MINUS:
7514  if (pnode->tensor().size() == 1) {
7515  GA_DEBUG_ASSERT(child0->tensor().size() == 1,
7516  "Internal error: child0 not scalar");
7517  GA_DEBUG_ASSERT(child1->tensor().size() == 1,
7518  "Internal error: child1 not scalar");
7519  pgai = std::make_shared<ga_instruction_scalar_sub>
7520  (pnode->tensor()[0], child0->tensor()[0], child1->tensor()[0]);
7521  } else {
7522  pgai = std::make_shared<ga_instruction_sub>
7523  (pnode->tensor(), child0->tensor(), child1->tensor());
7524  }
7525  ga_propagate_sum_structure(pnode->t, child0->t, child1->t);
7526  rmi.instructions.push_back(std::move(pgai));
7527  break;
7528 
7529  case GA_UNARY_MINUS:
7530  if (pnode->tensor().size() == 1) {
7531  GA_DEBUG_ASSERT(child0->tensor().size() == 1, "Internal error");
7532  pgai = std::make_shared<ga_instruction_scalar_scalar_mult>
7533  (pnode->tensor()[0], child0->tensor()[0], minus);
7534  } else {
7535  pgai = std::make_shared<ga_instruction_scalar_mult>
7536  (pnode->tensor(), child0->tensor(), minus);
7537  }
7538  pnode->t.copy_structure(child0->t);
7539  rmi.instructions.push_back(std::move(pgai));
7540  break;
7541 
7542 
7543  case GA_DOT: case GA_COLON: case GA_MULT:
7544  {
7545  size_type tps0 = child0->tensor_proper_size();
7546  size_type tps1 = child1->tensor_proper_size();
7547  size_type s1 = (tps0 * tps1) / pnode->tensor_proper_size();
7548  size_type s2 = size_type(round(sqrt(scalar_type(s1))));
7549 
7550  pgai = pga_instruction();
7551  if ((pnode->op_type == GA_DOT && dim1 <= 1) ||
7552  (pnode->op_type == GA_COLON && dim1 <= 2) ||
7553  (pnode->op_type == GA_MULT && dim0 == 4) ||
7554  (pnode->op_type == GA_MULT && dim1 <= 1) ||
7555  child0->tensor().size() == 1 || tps1 == 1) {
7556 
7557  if (child0->tensor().size() == 1 && child1->tensor().size() == 1) {
7558  pgai = std::make_shared<ga_instruction_scalar_scalar_mult>
7559  (pnode->tensor()[0], child0->tensor()[0], child1->tensor()[0]);
7560  }
7561  else if (child0->tensor().size() == 1) {
7562  pnode->t.copy_structure(child1->t);
7563  pgai = std::make_shared<ga_instruction_scalar_mult>
7564  (pnode->tensor(), child1->tensor(), child0->tensor()[0]);
7565  }
7566  else if (child1->tensor().size() == 1) {
7567  pnode->t.copy_structure(child0->t);
7568  pgai = std::make_shared<ga_instruction_scalar_mult>
7569  (pnode->tensor(), child0->tensor(), child1->tensor()[0]);
7570  }
7571  else if (pnode->test_function_type < 3) {
7572  if (tps0 == 1) {
7573  if (is_uniform) // Unrolled instruction
7574  pgai = ga_uniform_instruction_simple_tmult
7575  (pnode->tensor(), child0->tensor(), child1->tensor());
7576  else
7577  pgai = std::make_shared<ga_instruction_simple_tmult>
7578  (pnode->tensor(), child0->tensor(), child1->tensor());
7579  } else {
7580  if (tps1 == 1) {
7581  if (is_uniform) // Unrolled instruction
7582  pgai = ga_uniform_instruction_simple_tmult
7583  (pnode->tensor(), child1->tensor(), child0->tensor());
7584  else
7585  pgai = std::make_shared<ga_instruction_simple_tmult>
7586  (pnode->tensor(), child1->tensor(), child0->tensor());
7587  } else if (is_uniform) // Unrolled instruction
7588  pgai = ga_uniform_instruction_contraction_switch
7589  (pnode->t, child0->t, child1->t, s2, tensor_to_clear);
7590  else // Unrolled instruction
7591  pgai = ga_instruction_contraction_switch
7592  (pnode->t, child0->t, child1->t, s2, tensor_to_clear);
7593  }
7594  } else {
7595  if (child1->test_function_type == 1 ||
7596  child1->test_function_type == 3) {
7597  if (child1->test_function_type == 3 ||
7598  child1->tensor_proper_size() <= s2) {
7599  if (tps0 == 1) {
7600  if (is_uniform) { // Unrolled instruction
7601  pgai = ga_uniform_instruction_simple_tmult
7602  (pnode->tensor(), child1->tensor(), child0->tensor());
7603  } else
7604  pgai = std::make_shared<ga_instruction_simple_tmult>
7605  (pnode->tensor(), child1->tensor(), child0->tensor());
7606  } else if (is_uniform) // Unrolled instruction
7607  pgai = ga_uniform_instruction_contraction_switch
7608  (pnode->t, child0->t, child1->t, s2, tensor_to_clear);
7609  else // Unrolled instruction
7610  pgai = ga_instruction_contraction_switch
7611  (pnode->t, child0->t, child1->t, s2, tensor_to_clear);
7612  } else
7613  pgai = std::make_shared<ga_instruction_spec_contraction>
7614  (pnode->tensor(), child1->tensor(), child0->tensor(), s2);
7615  } else if (child1->test_function_type == 0 ||
7616  (child0->tensor_proper_size() == s2 &&
7617  child1->tensor_proper_size() == s2)) {
7618  if (tps0 == 1) {
7619  if (is_uniform) { // Unrolled instruction
7620  pgai = ga_uniform_instruction_simple_tmult
7621  (pnode->tensor(), child0->tensor(), child1->tensor());
7622  } else
7623  pgai = std::make_shared<ga_instruction_simple_tmult>
7624  (pnode->tensor(), child0->tensor(), child1->tensor());
7625  } else {
7626  if (is_uniform) // Unrolled instruction
7627  pgai = ga_uniform_instruction_contraction_switch
7628  (pnode->t, child1->t, child0->t, s2, tensor_to_clear);
7629  else // Unrolled instruction
7630  pgai = ga_instruction_contraction_switch
7631  (pnode->t, child1->t, child0->t, s2, tensor_to_clear);
7632  }
7633  } else {
7634  if (child0->tensor_proper_size() == s2)
7635  pgai = ga_uniform_instruction_contraction_switch
7636  (pnode->t, child1->t, child0->t, s2, tensor_to_clear);
7637  else if (child1->tensor_proper_size() == s2)
7638  pgai = std::make_shared<ga_instruction_spec_contraction>
7639  (pnode->tensor(), child0->tensor(), child1->tensor(), s2);
7640  else
7641  pgai = std::make_shared<ga_instruction_spec2_contraction>
7642  (pnode->tensor(), child0->tensor(), child1->tensor(), s2);
7643  }
7644  }
7645  } else { // GA_MULT or GA_DOT for dim1 > 1 or GA_COLON for dim1 > 2
7646  // and child1->tensor_proper_size() > 1
7647  if (pnode->test_function_type < 3) {
7648  if (tps0 == 1) {
7649  if (is_uniform) // Unrolled instruction
7650  pgai = ga_uniform_instruction_simple_tmult
7651  (pnode->tensor(), child0->tensor(), child1->tensor());
7652  else
7653  pgai = std::make_shared<ga_instruction_simple_tmult>
7654  (pnode->tensor(), child0->tensor(), child1->tensor());
7655  } else {
7656  if (child1->test_function_type == 0)
7657  pgai = std::make_shared<ga_instruction_matrix_mult>
7658  (pnode->tensor(), child0->tensor(), child1->tensor(), s2);
7659  else
7660  pgai = std::make_shared<ga_instruction_matrix_mult_spec>
7661  (pnode->tensor(), child0->tensor(), child1->tensor(),
7662  s2, tps0/s2, tps1/s2);
7663  }
7664  } else {
7665  if (child0->tensor_proper_size() == 1) {
7666  if (child0->test_function_type == 0 ||
7667  child0->test_function_type == 1) {
7668  if (is_uniform) // Unrolled instruction
7669  pgai = ga_uniform_instruction_simple_tmult
7670  (pnode->tensor(), child0->tensor(), child1->tensor());
7671  else
7672  pgai = std::make_shared<ga_instruction_simple_tmult>
7673  (pnode->tensor(), child0->tensor(), child1->tensor());
7674  } else
7675  pgai = std::make_shared<ga_instruction_spec_tmult>
7676  (pnode->tensor(), child1->tensor(), child0->tensor(),
7677  tps1, tps0);
7678  } else {
7679  if (child1->test_function_type == 0)
7680  pgai = std::make_shared<ga_instruction_matrix_mult>
7681  (pnode->tensor(), child0->tensor(), child1->tensor(), s2);
7682  else if (child1->test_function_type == 2)
7683  pgai = std::make_shared<ga_instruction_matrix_mult_spec>
7684  (pnode->tensor(), child0->tensor(), child1->tensor(),
7685  s2, tps0/s2, tps1/s2);
7686  else
7687  pgai = std::make_shared<ga_instruction_matrix_mult_spec2>
7688  (pnode->tensor(), child0->tensor(), child1->tensor(),
7689  s2, tps0/s2, tps1/s2);
7690  }
7691  }
7692  }
7693  rmi.instructions.push_back(std::move(pgai));
7694  }
7695  break;
7696 
7697  case GA_DIV:
7698  if (child0->tensor().size() == 1 && child1->tensor().size() == 1) {
7699  pgai = std::make_shared<ga_instruction_scalar_scalar_div>
7700  (pnode->tensor()[0], child0->tensor()[0], child1->tensor()[0]);
7701  } else if (child1->tensor().size() == 1) {
7702  pnode->t.copy_structure(child0->t);
7703  pgai = std::make_shared<ga_instruction_scalar_div>
7704  (pnode->tensor(), child0->tensor(), child1->tensor()[0]);
7705  } else GMM_ASSERT1(false, "Internal error");
7706  rmi.instructions.push_back(std::move(pgai));
7707  break;
7708 
7709  case GA_PRINT:
7710  pnode->t.set_to_copy(child0->t);
7711  pgai = std::make_shared<ga_instruction_print_tensor>
7712  (pnode->tensor(), child0, gis.ctx, gis.nbpt, gis.ipt);
7713  rmi.instructions.push_back(std::move(pgai));
7714  break;
7715 
7716  case GA_QUOTE:
7717  if (pnode->tensor_proper_size() > 1) {
7718  size_type n1 = child0->tensor_proper_size(0);
7719  size_type n2 = (child0->tensor_order() > 1) ?
7720  child0->tensor_proper_size(1) : 1;
7721  size_type nn = 1;
7722  for (size_type i = 2; i < child0->tensor_order(); ++i)
7723  nn *= child0->tensor_proper_size(i);
7724  if (child0->nb_test_functions() == 0)
7725  pgai = std::make_shared<ga_instruction_transpose_no_test>
7726  (pnode->tensor(), child0->tensor(), n1, n2, nn);
7727  else
7728  pgai = std::make_shared<ga_instruction_transpose>
7729  (pnode->tensor(), child0->tensor(), n1, n2, nn);
7730  if (child0->nb_test_functions() == 1
7731  && child0->tensor_order() == 2 && n1 == n2) {
7732  size_type Q = 0;
7733  std::vector<bool> mask;
7734  if (ga_matrix_op_mask(child0->t, n1, false, Q, mask))
7735  pnode->t.set_mask
7736  (Q, std::make_shared<std::vector<bool>>(std::move(mask)),
7737  child0->t.mask_symmetric());
7738  }
7739  rmi.instructions.push_back(std::move(pgai));
7740  } else {
7741  pnode->t.set_to_copy(child0->t);
7742  }
7743  break;
7744 
7745  case GA_SYM:
7746  if (pnode->tensor_proper_size() != 1) {
7747  size_type Q = 0, d = size0.back();
7748  std::vector<bool> mask;
7749  if (pnode->nb_test_functions() == 1 && child0->tensor_order() == 2
7750  && ga_matrix_op_mask(child0->t, d, true, Q, mask)) {
7751  pgai = std::make_shared<ga_instruction_sym_masked>
7752  (pnode->tensor(), child0->tensor(), Q, d, mask);
7753  pnode->t.set_mask
7754  (Q, std::make_shared<std::vector<bool>>(std::move(mask)), true);
7755  tensor_to_clear = true;
7756  } else
7757  pgai = std::make_shared<ga_instruction_sym>
7758  (pnode->tensor(), child0->tensor());
7759  rmi.instructions.push_back(std::move(pgai));
7760  } else {
7761  pnode->t.set_to_copy(child0->t);
7762  }
7763  break;
7764 
7765  case GA_SKEW:
7766  {
7767  pgai = std::make_shared<ga_instruction_skew>
7768  (pnode->tensor(), child0->tensor());
7769  rmi.instructions.push_back(std::move(pgai));
7770  }
7771  break;
7772 
7773  case GA_TRACE:
7774  {
7775  size_type N = (child0->tensor_proper_size() == 1) ? 1:size0.back();
7776  if (N == 1) {
7777  pnode->t.set_to_copy(child0->t);
7778  } else {
7779  pgai = std::make_shared<ga_instruction_trace>
7780  (pnode->tensor(), child0->tensor(), N);
7781  rmi.instructions.push_back(std::move(pgai));
7782  }
7783  }
7784  break;
7785 
7786  case GA_DEVIATOR:
7787  {
7788  size_type N = (child0->tensor_proper_size() == 1) ? 1:size0.back();
7789  pgai = std::make_shared<ga_instruction_deviator>
7790  (pnode->tensor(), child0->tensor(), N);
7791  rmi.instructions.push_back(std::move(pgai));
7792  }
7793  break;
7794 
7795  case GA_DOTMULT:
7796 
7797  if (child0->tensor().size() == 1 && child1->tensor().size() == 1) {
7798  pgai = std::make_shared<ga_instruction_scalar_scalar_mult>
7799  (pnode->tensor()[0], child0->tensor()[0], child1->tensor()[0]);
7800  } else if (child0->tensor().size() == 1) {
7801  pnode->t.copy_structure(child1->t);
7802  pgai = std::make_shared<ga_instruction_scalar_mult>
7803  (pnode->tensor(), child1->tensor(), child0->tensor()[0]);
7804  }
7805  else if (child1->tensor().size() == 1) {
7806  pnode->t.copy_structure(child0->t);
7807  pgai = std::make_shared<ga_instruction_scalar_mult>
7808  (pnode->tensor(), child0->tensor(), child1->tensor()[0]);
7809  }
7810  else if (child1->test_function_type == 0)
7811  pgai = std::make_shared<ga_instruction_dotmult>
7812  (pnode->tensor(), child0->tensor(), child1->tensor());
7813  else if (child0->test_function_type == 0)
7814  pgai = std::make_shared<ga_instruction_dotmult>
7815  (pnode->tensor(), child1->tensor(), child0->tensor());
7816  else if (child0->test_function_type == 1)
7817  pgai = std::make_shared<ga_instruction_dotmult_spec>
7818  (pnode->tensor(), child0->tensor(), child1->tensor());
7819  else
7820  pgai = std::make_shared<ga_instruction_dotmult_spec>
7821  (pnode->tensor(), child1->tensor(), child0->tensor());
7822 
7823  rmi.instructions.push_back(std::move(pgai));
7824  break;
7825 
7826 
7827  case GA_DOTDIV:
7828  if (child0->tensor().size() == 1 && child1->tensor().size() == 1) {
7829  pgai = std::make_shared<ga_instruction_scalar_scalar_div>
7830  (pnode->tensor()[0], child0->tensor()[0], child1->tensor()[0]);
7831  } else if (child1->tensor().size() == 1) {
7832  pnode->t.copy_structure(child0->t);
7833  pgai = std::make_shared<ga_instruction_scalar_div>
7834  (pnode->tensor(), child0->tensor(), child1->tensor()[0]);
7835  } else if (child1->test_function_type == 0) {
7836  pgai = std::make_shared<ga_instruction_dotdiv>
7837  (pnode->tensor(), child0->tensor(), child1->tensor());
7838  } else GMM_ASSERT1(false, "Internal error");
7839  rmi.instructions.push_back(std::move(pgai));
7840  break;
7841 
7842 
7843  case GA_TMULT:
7844  if (child0->tensor().size() == 1 && child1->tensor().size() == 1) {
7845  pgai = std::make_shared<ga_instruction_scalar_scalar_mult>
7846  (pnode->tensor()[0], child0->tensor()[0], child1->tensor()[0]);
7847  } else if (child0->tensor().size() == 1) {
7848  pnode->t.copy_structure(child1->t);
7849  pgai = std::make_shared<ga_instruction_scalar_mult>
7850  (pnode->tensor(), child1->tensor(), child0->tensor()[0]);
7851  }
7852  else if (child1->tensor().size() == 1) {
7853  pnode->t.copy_structure(child0->t);
7854  pgai = std::make_shared<ga_instruction_scalar_mult>
7855  (pnode->tensor(), child0->tensor(), child1->tensor()[0]);
7856  }
7857  else if (child1->test_function_type == 0) {
7858  if (is_uniform) // Unrolled instruction
7859  pgai = ga_uniform_instruction_simple_tmult
7860  (pnode->tensor(), child0->tensor(), child1->tensor());
7861  else
7862  pgai = std::make_shared<ga_instruction_simple_tmult>
7863  (pnode->tensor(), child0->tensor(), child1->tensor());
7864  } else if (child1->tensor_proper_size() == 1)
7865  pgai = std::make_shared<ga_instruction_spec2_tmult>
7866  (pnode->tensor(), child0->tensor(), child1->tensor());
7867  else
7868  pgai = std::make_shared<ga_instruction_spec_tmult>
7869  (pnode->tensor(), child0->tensor(), child1->tensor(),
7870  child0->tensor_proper_size(),
7871  child1->tensor_proper_size());
7872 
7873  rmi.instructions.push_back(std::move(pgai));
7874  break;
7875 
7876  default:GMM_ASSERT1(false, "Unexpected operation. Internal error.");
7877  }
7878  break;
7879 
7880  case GA_NODE_C_MATRIX:
7881  {
7882  if (pnode->test_function_type) {
7883  std::vector<const base_tensor *> components(pnode->children.size());
7884  for (size_type i = 0; i < pnode->children.size(); ++i)
7885  components[i] = &(pnode->children[i]->tensor());
7886  pgai = std::make_shared<ga_instruction_c_matrix_with_tests>
7887  (pnode->tensor(), components);
7888  } else {
7889  std::vector<scalar_type *> components(pnode->children.size());
7890  for (size_type i = 0; i < pnode->children.size(); ++i)
7891  components[i] = &(pnode->children[i]->tensor()[0]);
7892  pgai = std::make_shared<ga_instruction_simple_c_matrix>
7893  (pnode->tensor(), components);
7894  }
7895  rmi.instructions.push_back(std::move(pgai));
7896  }
7897  break;
7898 
7899  case GA_NODE_PARAMS:
7900  if (child0->node_type == GA_NODE_RESHAPE) {
7901  pgai = std::make_shared<ga_instruction_copy_tensor>(pnode->tensor(),
7902  child1->tensor());
7903  rmi.instructions.push_back(std::move(pgai));
7904  } else if (child0->node_type == GA_NODE_CROSS_PRODUCT) {
7905  pga_tree_node child2 = pnode->children[2];
7906  if (child1->test_function_type==2 && child2->test_function_type==1)
7907  pgai = std::make_shared<ga_instruction_cross_product_tf>
7908  (pnode->tensor(), child2->tensor(), child1->tensor(), true);
7909  else if (child1->test_function_type || child2->test_function_type)
7910  pgai = std::make_shared<ga_instruction_cross_product_tf>
7911  (pnode->tensor(), child1->tensor(), child2->tensor(), false);
7912  else
7913  pgai = std::make_shared<ga_instruction_cross_product>
7914  (pnode->tensor(), child1->tensor(), child2->tensor());
7915  rmi.instructions.push_back(std::move(pgai));
7916  } else if (child0->node_type == GA_NODE_IND_MOVE_LAST) {
7917  size_type ind;
7918  ind = size_type(round(pnode->children[2]->tensor()[0])-1);
7919  size_type ii2 = 1;
7920  for (size_type i = 0; i < child1->tensor_order(); ++i)
7921  if (i>ind) ii2 *= child1->tensor_proper_size(i);
7922  size_type nn = child1->tensor_proper_size(ind);
7923  pgai = std::make_shared<ga_instruction_index_move_last>
7924  (pnode->tensor(), child1->tensor(), nn, ii2);
7925  rmi.instructions.push_back(std::move(pgai));
7926  } else if (child0->node_type == GA_NODE_SWAP_IND) {
7927  size_type ind[4];
7928  for (size_type i = 2; i < 4; ++i)
7929  ind[i] = size_type(round(pnode->children[i]->tensor()[0])-1);
7930  if (ind[2] > ind[3]) std::swap(ind[2], ind[3]);
7931  size_type ii2 = 1, ii3 = 1;
7932  for (size_type i = 0; i < child1->tensor_order(); ++i) {
7933  if (i>ind[2] && i<ind[3]) ii2 *= child1->tensor_proper_size(i);
7934  if (i>ind[3]) ii3 *= child1->tensor_proper_size(i);
7935  }
7936  size_type nn1 = child1->tensor_proper_size(ind[2]);
7937  size_type nn2 = child1->tensor_proper_size(ind[3]);
7938 
7939  pgai = std::make_shared<ga_instruction_swap_indices>
7940  (pnode->tensor(), child1->tensor(), nn1, nn2, ii2, ii3);
7941  rmi.instructions.push_back(std::move(pgai));
7942  } else if (child0->node_type == GA_NODE_CONTRACT) {
7943  std::vector<size_type> ind(2), indsize(2);
7944  pga_tree_node child2(0);
7945  if (pnode->children.size() == 4)
7946  { ind[0] = 2; ind[1] = 3; }
7947  else if (pnode->children.size() == 5)
7948  { ind[0] = 2; ind[1] = 4; child2 = pnode->children[3]; }
7949  else if (pnode->children.size() == 7) {
7950  ind.resize(4); indsize.resize(4);
7951  ind[0] = 2; ind[1] = 3; ind[2] = 5; ind[3] = 6;
7952  child2 = pnode->children[4];
7953  }
7954  size_type kk = 0, ll = 1;
7955  for (size_type i = 1; i < pnode->children.size(); ++i) {
7956  if (i == ind[kk]) {
7957  ind[kk] = size_type(round(pnode->children[i]->tensor()[0])-1);
7958  indsize[kk] = pnode->children[ll]->tensor_proper_size(ind[kk]);
7959  ++kk;
7960  } else ll = i;
7961  }
7962 
7963  if (pnode->children.size() == 4) {
7964  size_type i1 = ind[0], i2 = ind[1];
7965  if (i1 > i2) std::swap(i1, i2);
7966  size_type ii2 = 1, ii3 = 1;
7967  for (size_type i = 0; i < child1->tensor_order(); ++i) {
7968  if (i > i1 && i < i2) ii2 *= child1->tensor_proper_size(i);
7969  if (i > i2) ii3 *= child1->tensor_proper_size(i);
7970  }
7971  pgai = std::make_shared<ga_instruction_contract_1_1>
7972  (pnode->tensor(), child1->tensor(), indsize[0], ii2, ii3);
7973  }
7974  else if (pnode->children.size() == 5) {
7975  // Particular cases should be detected (ii2=ii3=1 in particular).
7976  size_type i1 = ind[0], i2 = ind[1];
7977  size_type ii1 = 1, ii2 = 1, ii3 = 1, ii4 = 1;
7978  for (size_type i = 0; i < child1->tensor_order(); ++i) {
7979  if (i < i1) ii1 *= child1->tensor_proper_size(i);
7980  if (i > i1) ii2 *= child1->tensor_proper_size(i);
7981  }
7982  for (size_type i = 0; i < child2->tensor_order(); ++i) {
7983  if (i < i2) ii3 *= child2->tensor_proper_size(i);
7984  if (i > i2) ii4 *= child2->tensor_proper_size(i);
7985  }
7986  if (child1->test_function_type==1 && child2->test_function_type==2)
7987  pgai = std::make_shared<ga_instruction_contract_2_1_rev>
7988  (pnode->tensor(), child1->tensor(), child2->tensor(),
7989  indsize[0], ii1, ii2, ii3, ii4);
7990  else
7991  pgai = std::make_shared<ga_instruction_contract_2_1>
7992  (pnode->tensor(), child1->tensor(), child2->tensor(),
7993  indsize[0], ii1, ii2, ii3, ii4);
7994  }
7995  else if (pnode->children.size() == 7) {
7996  // Particular cases should be detected (ii2=ii3=1 in particular).
7997  size_type i1 = ind[0], i2 = ind[1], i3 = ind[2], i4 = ind[3];
7998  size_type nn1 = indsize[0], nn2 = indsize[1];
7999  size_type ii1 = 1, ii2 = 1, ii3 = 1, ii4 = 1, ii5 = 1, ii6 = 1;
8000  if (i1 > i2)
8001  { std::swap(i1, i2); std::swap(i3, i4); std::swap(nn1, nn2); }
8002  for (size_type i = 0; i < child1->tensor_order(); ++i) {
8003  if (i < i1) ii1 *= child1->tensor_proper_size(i);
8004  if (i > i1 && i < i2) ii2 *= child1->tensor_proper_size(i);
8005  if (i > i2) ii3 *= child1->tensor_proper_size(i);
8006  }
8007  for (size_type i = 0; i < child2->tensor_order(); ++i) {
8008  if (i < i3 && i < i4) ii4 *= child2->tensor_proper_size(i);
8009  if ((i > i3 && i < i4) || (i > i4 && i < i3))
8010  ii5 *= child2->tensor_proper_size(i);
8011  if (i > i3 && i > i4) ii6 *= child2->tensor_proper_size(i);
8012  }
8013  if (child1->test_function_type==1 && child2->test_function_type==2)
8014  pgai = std::make_shared<ga_instruction_contract_2_2_rev>
8015  (pnode->tensor(), child1->tensor(), child2->tensor(),
8016  nn1, nn2, ii1, ii2, ii3, ii4, ii5, ii6, i4 < i3);
8017  else
8018  pgai = std::make_shared<ga_instruction_contract_2_2>
8019  (pnode->tensor(), child1->tensor(), child2->tensor(),
8020  nn1, nn2, ii1, ii2, ii3, ii4, ii5, ii6, i4 < i3);
8021  }
8022  rmi.instructions.push_back(std::move(pgai));
8023  } else if (child0->node_type == GA_NODE_PREDEF_FUNC) {
8024 
8025  std::string name = child0->name;
8026  const ga_predef_function_tab &PREDEF_FUNCTIONS
8028  ga_predef_function_tab::const_iterator it = PREDEF_FUNCTIONS.find(name);
8029  const ga_predef_function &F = it->second;
8030  size_type nbargs = F.nbargs();
8031  pga_tree_node child2 = (nbargs == 2) ? pnode->children[2] : child1;
8032 
8033  if (nbargs == 1) {
8034  if (child1->tensor().size() == 1) {
8035  if (F.ftype() == 0)
8036  pgai = std::make_shared<ga_instruction_eval_func_1arg_1res>
8037  (pnode->tensor()[0], child1->tensor()[0], F.f1());
8038  else
8039  pgai = std::make_shared<ga_instruction_eval_func_1arg_1res_expr>
8040  (pnode->tensor()[0], child1->tensor()[0], F);
8041  } else {
8042  if (F.ftype() == 0)
8043  pgai = std::make_shared<ga_instruction_eval_func_1arg>
8044  (pnode->tensor(), child1->tensor(), F.f1());
8045  else
8046  pgai = std::make_shared<ga_instruction_eval_func_1arg_expr>
8047  (pnode->tensor(), child1->tensor(), F);
8048  }
8049  } else {
8050  if (child1->tensor().size() == 1 && child2->tensor().size() == 1) {
8051  if (F.ftype() == 0)
8052  pgai = std::make_shared<ga_instruction_eval_func_2arg_1res>
8053  (pnode->tensor()[0], child1->tensor()[0], child2->tensor()[0],
8054  F.f2());
8055  else
8056  pgai = std::make_shared<ga_instruction_eval_func_2arg_1res_expr>
8057  (pnode->tensor()[0], child1->tensor()[0], child2->tensor()[0],
8058  F);
8059  } else if (child1->tensor().size() == 1) {
8060  if (F.ftype() == 0)
8061  pgai =
8062  std::make_shared<ga_instruction_eval_func_2arg_first_scalar>
8063  (pnode->tensor(), child1->tensor(), child2->tensor(), F.f2());
8064  else
8065  pgai =
8066  std::make_shared<ga_instruction_eval_func_2arg_first_scalar_expr>
8067  (pnode->tensor(), child1->tensor(), child2->tensor(), F);
8068  } else if (child2->tensor().size() == 1) {
8069  if (F.ftype() == 0)
8070  pgai =
8071  std::make_shared<ga_instruction_eval_func_2arg_second_scalar>
8072  (pnode->tensor(), child1->tensor(), child2->tensor(), F.f2());
8073  else
8074  pgai =
8075  std::make_shared<ga_instruction_eval_func_2arg_second_scalar_expr>
8076  (pnode->tensor(), child1->tensor(), child2->tensor(), F);
8077  } else {
8078  if (F.ftype() == 0)
8079  pgai = std::make_shared<ga_instruction_eval_func_2arg>
8080  (pnode->tensor(), child1->tensor(), child2->tensor(), F.f2());
8081  else
8082  pgai = std::make_shared<ga_instruction_eval_func_2arg_expr>
8083  (pnode->tensor(), child1->tensor(), child2->tensor(), F);
8084  }
8085  }
8086  rmi.instructions.push_back(std::move(pgai));
8087 
8088  } else if (child0->node_type == GA_NODE_SPEC_FUNC) {
8089 
8090  GMM_ASSERT1(false, "Internal error");
8091 
8092  } else if (child0->node_type == GA_NODE_OPERATOR) {
8093 
8094  ga_predef_operator_tab &PREDEF_OPERATORS
8096  ga_predef_operator_tab::T::iterator it
8097  = PREDEF_OPERATORS.tab.find(child0->name);
8098  const ga_nonlinear_operator &OP = *(it->second);
8099  ga_nonlinear_operator::arg_list args;
8100  for (size_type i = 1; i < pnode->children.size(); ++i)
8101  args.push_back(&(pnode->children[i]->tensor()));
8102 
8103  if (child0->der1 && child0->der2 == 0) {
8104  pgai = std::make_shared<ga_instruction_eval_derivative_OP>
8105  (pnode->tensor(), OP, args, child0->der1);
8106  } else if (child0->der1 && child0->der2) {
8107  pgai = std::make_shared<ga_instruction_eval_second_derivative_OP>
8108  (pnode->tensor(), OP, args, child0->der1, child0->der2);
8109  } else {
8110  pgai = std::make_shared<ga_instruction_eval_OP>(pnode->tensor(),
8111  OP, args);
8112  }
8113  rmi.instructions.push_back(std::move(pgai));
8114 
8115  } else { // Access to a component of the tensor
8116  bgeot::multi_index mi1(size0.size()), indices;
8117  size_type nb_test = pnode->nb_test_functions();
8118  if (pnode->tensor().size() == 1) {
8119  for (size_type i = 0; i < child0->tensor_order(); ++i)
8120  mi1[i+nb_test] = size_type(round(pnode->children[i+1]->tensor()[0])-1);
8121  pgai = std::make_shared<ga_instruction_copy_scalar>
8122  (pnode->tensor()[0], child0->tensor()(mi1));
8123  } else {
8124  for (size_type i = 0; i < nb_test; ++i) indices.push_back(i);
8125  for (size_type i = 0; i < child0->tensor_order(); ++i) {
8126  if (pnode->children[i+1]->node_type != GA_NODE_ALLINDICES)
8127  mi1[i+nb_test]
8128  = size_type(round(pnode->children[i+1]->tensor()[0])- 1);
8129  else
8130  indices.push_back(i+nb_test);
8131  }
8132  pgai = std::make_shared<ga_instruction_tensor_slice>
8133  (pnode->tensor(), child0->tensor(), mi1, indices);
8134  }
8135  rmi.instructions.push_back(std::move(pgai));
8136  }
8137 
8138  break;
8139 
8140  default:GMM_ASSERT1(false, "Unexpected node type " << pnode->node_type
8141  << " in compilation. Internal error.");
8142  }
8143  if (tensor_to_clear) {
8144  gmm::clear(pnode->tensor().as_vector());
8145  if (!is_uniform) {
8146  pgai = std::make_shared<ga_instruction_clear_tensor>(pnode->tensor());
8147  rmi.elt_instructions.push_back(std::move(pgai));
8148  }
8149  }
8150  rmi.node_list[pnode->hash_value].push_back(pnode);
8151  } // ga_compile_node
8152 
8153  void ga_compile_function(ga_workspace &workspace,
8154  ga_instruction_set &gis, bool scalar) {
8155  for (size_type i = 0; i < workspace.nb_trees(); ++i) {
8156  const ga_workspace::tree_description &td = workspace.tree_info(i);
8157 
8158  gis.trees.push_back(*(td.ptree));
8159  pga_tree_node root = gis.trees.back().root;
8160  if (root) {
8161  GMM_ASSERT1(!scalar || (root->tensor().size() == 1),
8162  "The result of the given expression is not a scalar");
8163  ga_instruction_set::region_mim rm(td.mim, td.rg, 0);
8164  gis.all_instructions[rm].m = td.m;
8165  ga_if_hierarchy if_hierarchy;
8166  ga_compile_node(root, workspace, gis, gis.all_instructions[rm],
8167  *(td.m), true, if_hierarchy);
8168 
8169  gis.coeff = scalar_type(1);
8170  pga_instruction pgai;
8171  workspace.assembled_tensor() = root->tensor();
8172  pgai = std::make_shared<ga_instruction_add_to_coeff>
8173  (workspace.assembled_tensor(), root->tensor(), gis.coeff);
8174  gis.all_instructions[rm].instructions.push_back(std::move(pgai));
8175  }
8176  }
8177  }
8178 
8179  static bool ga_node_used_interpolates
8180  (const pga_tree_node pnode, const ga_workspace &workspace,
8181  std::map<std::string, std::set<std::string> > &interpolates,
8182  std::set<std::string> &interpolates_der) {
8183  bool found = false;
8184  bool intrpl(pnode->node_type == GA_NODE_INTERPOLATE_VAL ||
8185  pnode->node_type == GA_NODE_INTERPOLATE_GRAD ||
8186  pnode->node_type == GA_NODE_INTERPOLATE_HESS ||
8187  pnode->node_type == GA_NODE_INTERPOLATE_DIVERG);
8188  bool intrpl_test(pnode->node_type == GA_NODE_INTERPOLATE_VAL_TEST ||
8189  pnode->node_type == GA_NODE_INTERPOLATE_GRAD_TEST ||
8190  pnode->node_type == GA_NODE_INTERPOLATE_HESS_TEST ||
8191  pnode->node_type == GA_NODE_INTERPOLATE_DIVERG_TEST);
8192 
8193  if (intrpl || intrpl_test ||
8194  pnode->node_type == GA_NODE_INTERPOLATE_FILTER ||
8195  pnode->node_type == GA_NODE_INTERPOLATE_X ||
8196  pnode->node_type == GA_NODE_INTERPOLATE_NORMAL) {
8197  interpolates[pnode->interpolate_name].size();
8198  if (intrpl || intrpl_test) {
8199  if (workspace.variable_group_exists(pnode->name))
8200  interpolates[pnode->interpolate_name].insert(pnode->name);
8201  }
8202  found = true;
8203  }
8204  if (pnode->node_type == GA_NODE_INTERPOLATE_DERIVATIVE) {
8205  interpolates_der.insert(pnode->interpolate_name_der);
8206  interpolates[pnode->interpolate_name_der].size();
8207  if (workspace.variable_group_exists(pnode->name))
8208  interpolates[pnode->interpolate_name_der].insert(pnode->name);
8209  }
8210  for (size_type i = 0; i < pnode->children.size(); ++i)
8211  found = ga_node_used_interpolates(pnode->children[i], workspace,
8212  interpolates, interpolates_der)
8213  || found;
8214  return found;
8215  }
8216 
8217 
8218  static void ga_compile_interpolate_trans
8219  (const pga_tree_node pnode, const ga_workspace &workspace,
8220  ga_instruction_set &gis, ga_instruction_set::region_mim_instructions &rmi,
8221  const mesh &m) {
8222 
8223  std::set<std::string> interpolates_der;
8224  std::map<std::string, std::set<std::string> > transformations;
8225  ga_node_used_interpolates(pnode, workspace, transformations,
8226  interpolates_der);
8227 
8228  for (const auto &transformation : transformations) {
8229  const std::string &transname = transformation.first;
8230  bool compute_der = (interpolates_der.count(transname) != 0);
8231  if (rmi.transformations.count(transname) == 0 ||
8232  (compute_der && rmi.transformations_der.count(transname) == 0)) {
8233  rmi.transformations[transname].size();
8234  gis.transformations.insert(transname);
8235  if (compute_der) rmi.transformations_der.insert(transname);
8236  pga_instruction pgai;
8237  if (transname.compare("neighbor_element") == 0 ||
8238  transname.compare("neighbour_elt") == 0) {
8239  pgai = std::make_shared<ga_instruction_neighbor_transformation_call>
8240  (workspace, rmi.interpolate_infos[transname],
8241  workspace.interpolate_transformation(transname), gis.ctx,
8242  m, gis.ipt, gis.pai, gis.gp_pool, gis.neighbor_corresp);
8243  } else {
8244  pgai = std::make_shared<ga_instruction_transformation_call>
8245  (workspace, rmi.interpolate_infos[transname],
8246  workspace.interpolate_transformation(transname), gis.ctx,
8247  gis.Normal, m, compute_der);
8248  }
8249  if (pgai) rmi.instructions.push_back(std::move(pgai));
8250  }
8251 
8252  for (const std::string &nodename : transformation.second) {
8253  if (rmi.transformations[transname].count(nodename) == 0) {
8254  auto&& inin = rmi.interpolate_infos[transname];
8255  pga_instruction pgai =
8256  std::make_shared<ga_instruction_update_group_info>
8257  (workspace, gis, inin, nodename, inin.groups_info[nodename]);
8258  rmi.instructions.push_back(std::move(pgai));
8259  rmi.transformations[transname].insert(nodename);
8260  }
8261  }
8262  }
8263  }
8264 
8265  void ga_compile_interpolation(ga_workspace &workspace,
8266  ga_instruction_set &gis) {
8267  gis.transformations.clear();
8268  gis.all_instructions.clear();
8269  for (size_type i = 0; i < workspace.nb_trees(); ++i) {
8270  const ga_workspace::tree_description &td = workspace.tree_info(i);
8271  if (td.operation != ga_workspace::ASSEMBLY) {
8272  gis.trees.push_back(*(td.ptree));
8273 
8274  // Semantic analysis mainly to evaluate fixed size variables and data
8275  const mesh *m = td.m;
8276  GMM_ASSERT1(m, "Internal error");
8277  ga_semantic_analysis(gis.trees.back(), workspace, *m,
8278  ref_elt_dim_of_mesh(*m, *(td.rg)), true, false);
8279  pga_tree_node root = gis.trees.back().root;
8280  if (root) {
8281  // Compile tree
8282  ga_instruction_set::region_mim rm(td.mim, td.rg, 0);
8283  auto &rmi = gis.all_instructions[rm];
8284  rmi.m = td.m;
8285  rmi.im = td.mim;
8286  // rmi.interpolate_infos.clear();
8287  ga_compile_interpolate_trans(root, workspace, gis, rmi, *(td.m));
8288  ga_compile_node(root, workspace, gis, rmi, *(td.m), false,
8289  rmi.current_hierarchy);
8290 
8291  // After compile tree
8292  workspace.assembled_tensor() = root->tensor();
8293  pga_instruction pgai = std::make_shared<ga_instruction_add_to>
8294  (workspace.assembled_tensor(), root->tensor());
8295  rmi.instructions.push_back(std::move(pgai));
8296  }
8297  }
8298  }
8299  }
8300 
8301 
8302  struct var_set : std::map<std::string,size_type> {
8303  // This class indexes variable names in the order of their addition
8304  size_type operator[](const std::string &name) {
8305  if (name.empty()) return size_type(-1);
8306  size_type id = size();
8307  auto it = find(name);
8308  if (it == end()) {
8309  emplace(name, id);
8310  return id;
8311  }
8312  return it->second;
8313  }
8314  std::string operator[](const size_type &id) const {
8315  for (const auto &key_value : *this) // brute force reverse search
8316  if (key_value.second == id)
8317  return key_value.first;
8318  return std::string("");
8319  }
8320  };
8321 
8322 
8323  struct condensation_description {
8324  var_set Ivars, Jvars, Qvars; // sets of variables involved in condensation
8325  // Clusters of intercoupled condensed variables and subdiagonally coupled
8326  // primary variables for each cluster
8327  std::vector<std::set<size_type>> Qclusters, Jclusters;
8328  // Each element of Qclusters contains a group of intercoupled condensed
8329  // variables. Due to the couplings within each group, all variables of the
8330  // same group need to be condensed out simultaneously. Per definition two
8331  // clusters cannot share a common variable.
8332  // indexing of groups
8333  std::vector<size_type> cluster_of_Qvar;
8334  // Matrices of pointers to submatrices for all coupling terms
8335  gmm::dense_matrix<base_tensor *> KQQ, // diagonal
8336  KQJ, KQJpr, // subdiagonal
8337  KIQ, // superdiagonal
8338  KIJ; // outcome
8339  std::vector<base_tensor *> RI, // res. vector of coupled primary variables
8340  RQpr; // partial solution for condensed variables (initially stores residuals)
8341  };
8342 
8343  void ga_compile(ga_workspace &workspace,
8344  ga_instruction_set &gis, size_type order, bool condensation) {
8345  gis.transformations.clear();
8346  gis.all_instructions.clear();
8347  gis.unreduced_terms.clear();
8348  workspace.clear_temporary_variable_intervals();
8349 
8350  std::map<const ga_instruction_set::region_mim, condensation_description>
8351  condensations;
8352 
8353  if (condensation && order == 2) {
8354  for (size_type i = 0; i < workspace.nb_trees(); ++i) {
8355  const ga_workspace::tree_description &td = workspace.tree_info(i);
8356  if (td.order != 2 && td.order != size_type(-1))
8357  continue;
8358  ga_tree tree(*(td.ptree)); // temporary tree (not used later)
8359  ga_semantic_analysis(tree, workspace, td.mim->linked_mesh(),
8360  ref_elt_dim_of_mesh(td.mim->linked_mesh(),*(td.rg)),
8361  true, false);
8362  pga_tree_node root = tree.root;
8363  if (root) {
8364  const bool
8365  v1_is_intern = workspace.is_internal_variable(root->name_test1),
8366  v2_is_intern = workspace.is_internal_variable(root->name_test2);
8367  if (v1_is_intern || v2_is_intern) {
8368  GMM_ASSERT1(tree.secondary_domain.empty(),
8369  "Condensed variable cannot be used in secondary domain");
8370 
8371  for (const auto &key_val : condensations) {
8372  const ga_instruction_set::region_mim rm0 = key_val.first;
8373  const condensation_description &CC0 = key_val.second;
8374  if (rm0.mim() == td.mim && rm0.region() != td.rg
8375  && (CC0.Qvars.count(root->name_test1) ||
8376  CC0.Qvars.count(root->name_test2))) {
8377  mesh_region intrsct = getfem::mesh_region::intersection
8378  (*(rm0.region()), *(td.rg));
8379  GMM_ASSERT1(intrsct.is_empty(),
8380  "Cannot condense coupled variables between "
8381  "intersecting regions");
8382  }
8383  }
8384  const ga_instruction_set::region_mim rm(td.mim, td.rg, nullptr);
8385 
8386  condensation_description &CC = condensations[rm];
8387  size_type
8388  q1 = v1_is_intern ? CC.Qvars[root->name_test1] : size_type(-1),
8389  q2 = v2_is_intern ? CC.Qvars[root->name_test2] : size_type(-1);
8390  GMM_ASSERT1(q1 != size_type(-1) || q2 != size_type(-1), "Error");
8391  std::vector<size_type> selected_clusters;
8392  for (size_type j=0; j < CC.Qclusters.size(); ++j)
8393  if (CC.Qclusters[j].count(q1) || CC.Qclusters[j].count(q2))
8394  selected_clusters.push_back(j);
8395 
8396  if (selected_clusters.empty()) { // create new cluster
8397  CC.Qclusters.push_back(std::set<size_type>());
8398  if (q1 != size_type(-1)) CC.Qclusters.back().insert(q1);
8399  if (q2 != size_type(-1)) CC.Qclusters.back().insert(q2);
8400  } else { // add into existing cluster / merge clusters together
8401  auto &target = CC.Qclusters[selected_clusters[0]];
8402  if (q1 != size_type(-1)) target.insert(q1);
8403  if (q2 != size_type(-1)) target.insert(q2);
8404  for (size_type j=selected_clusters.size()-1; j > 1; --j) {
8405  auto &source = CC.Qclusters[selected_clusters[j]];
8406  target.insert(source.begin(), source.end());
8407  CC.Qclusters.erase(CC.Qclusters.begin() + selected_clusters[j]);
8408  }
8409  }
8410  } // is_internal_variable
8411  } // if (root)
8412  } // for (size_type i = 0; i < workspace.nb_trees(); ++i)
8413 
8414  for (auto &key_value : condensations) {
8415  condensation_description &CC = key_value.second;
8416  //for (const auto &cluster : CC.Qclusters) {
8417  // cout << "Clusters of coupled variables:" << endl;
8418  // for (const auto &varid : cluster) cout << "/" << CC.Qvars[varid];
8419  // cout << "/" << endl;
8420  //}
8421  size_type Qsize = CC.Qvars.size();
8422 
8423  // Jclusters will hold all J variables each cluster is coupled to
8424  CC.Jclusters.resize(CC.Qclusters.size());
8425 
8426  CC.cluster_of_Qvar.resize(Qsize);
8427  for (size_type i=0; i < CC.Qclusters.size(); ++i)
8428  for (const size_type &var : CC.Qclusters[i])
8429  CC.cluster_of_Qvar[var] = i;
8430 
8431  // Qvars: all condensed variables
8432  // Qclusters: definition of clusters of intercoupled variables of Qvars
8433  // cluster_of_Qvar: dictionary for which cluster each variable belongs to
8434  CC.KQQ.resize(Qsize, Qsize);
8435  CC.RQpr.resize(Qsize);
8436  for (size_type q=0; q < Qsize; ++q) {
8437  bgeot::multi_index mi(1);
8438  mi[0] = workspace.associated_im_data(CC.Qvars[q]) ->nb_tensor_elem();
8439  gis.condensation_tensors.push_back // memory allocation
8440  (std::make_shared<base_tensor>(mi));
8441  CC.RQpr[q] = gis.condensation_tensors.back().get();
8442  }
8443  }
8444  } // if (condensation && order == 2)
8445 
8446  std::array<ga_workspace::operation_type,3>
8447  phases{ga_workspace::PRE_ASSIGNMENT,
8448  ga_workspace::ASSEMBLY,
8449  ga_workspace::POST_ASSIGNMENT};
8450  for (const auto &phase : phases) {
8451 
8452  for (size_type i = 0; i < workspace.nb_trees(); ++i) {
8453  const ga_workspace::tree_description &td = workspace.tree_info(i);
8454  if (td.operation != phase)
8455  continue; // skip this tree in this phase
8456 
8457  if (td.order == order || td.order == size_type(-1)) {
8458  std::list<ga_tree> &trees = (phase == ga_workspace::ASSEMBLY)
8459  ? gis.trees
8460  : gis.interpolation_trees;
8461  trees.push_back(*(td.ptree));
8462  // Semantic analysis mainly to evaluate fixed size variables and data
8463  ga_semantic_analysis(trees.back(), workspace, td.mim->linked_mesh(),
8464  ref_elt_dim_of_mesh(td.mim->linked_mesh(),*(td.rg)),
8465  true, false);
8466  pga_tree_node root = trees.back().root;
8467  if (root) {
8468  // Compile tree
8469  // cout << "Will compile "; ga_print_node(root, cout); cout << endl;
8470 
8471  psecondary_domain psd(0);
8472  if (trees.back().secondary_domain.size())
8473  psd = workspace.secondary_domain(trees.back().secondary_domain);
8474  ga_instruction_set::region_mim rm(td.mim, td.rg, psd);
8475  auto &rmi = gis.all_instructions[rm];
8476  rmi.m = td.m;
8477  rmi.im = td.mim;
8478  // rmi.interpolate_infos.clear();
8479  ga_compile_interpolate_trans(root, workspace, gis, rmi, *(td.m));
8480  ga_compile_node(root, workspace, gis, rmi, *(td.m), false,
8481  rmi.current_hierarchy);
8482  // cout << "compilation finished "; ga_print_node(root, cout);
8483  // cout << endl;
8484 
8485  if (phase != ga_workspace::ASSEMBLY) { // Assignment/interpolation
8486  if (!td.varname_interpolation.empty()) {
8487  auto *imd
8488  = workspace.associated_im_data(td.varname_interpolation);
8489  auto &V = const_cast<model_real_plain_vector &>
8490  (workspace.value(td.varname_interpolation));
8491  GMM_ASSERT1(imd, "Internal error");
8492  auto pgai = std::make_shared<ga_instruction_assignment>
8493  (root->tensor(), V, gis.ctx, imd);
8494  rmi.instructions.push_back(std::move(pgai));
8495  }
8496  } else { // Addition of an assembly instruction
8497  pga_instruction pgai;
8498  switch(order) {
8499  case 0: {
8500  workspace.assembled_tensor() = root->tensor();
8501  pgai = std::make_shared<ga_instruction_add_to_coeff>
8502  (workspace.assembled_tensor(), root->tensor(), gis.coeff);
8503  break;
8504  }
8505  case 1: {
8506  GMM_ASSERT1(root->tensor_proper_size() == 1,
8507  "Invalid vector or tensor quantity. An order 1 "
8508  "weak form has to be a scalar quantity");
8509  const mesh_fem * const
8510  mf = workspace.associated_mf(root->name_test1);
8511  const im_data * const
8512  imd = workspace.associated_im_data(root->name_test1);
8513  workspace.add_temporary_interval_for_unreduced_variable
8514  (root->name_test1);
8515 
8516  base_vector &Vu = workspace.unreduced_vector(),
8517  &Vr = workspace.assembled_vector();
8518  if (mf) {
8519  const std::string &intn1 = root->interpolate_name_test1;
8520  bool secondary = !intn1.empty() &&
8521  workspace.secondary_domain_exists(intn1);
8522  fem_interpolation_context
8523  &ctx = intn1.empty() ? gis.ctx
8524  : (secondary ? rmi.secondary_domain_infos.ctx
8525  : rmi.interpolate_infos[intn1].ctx);
8526  bool interpolate =
8527  !(intn1.empty() || intn1 == "neighbor_element"
8528  || intn1 == "neighbour_elt" || secondary);
8529 
8530  if (intn1.size() && !secondary &&
8531  workspace.variable_group_exists(root->name_test1)) {
8532  ga_instruction_set::variable_group_info
8533  &vgi = rmi.interpolate_infos[intn1]
8534  .groups_info[root->name_test1];
8535  pgai = std::make_shared<ga_instruction_vector_assembly_mf>
8536  (root->tensor(), Vr, Vu, ctx,
8537  vgi.I, vgi.mf, vgi.reduced_mf,
8538  gis.coeff, gis.nbpt, gis.ipt, interpolate);
8539  for (const std::string &name
8540  : workspace.variable_group(root->name_test1))
8541  gis.unreduced_terms.emplace(name, "");
8542  } else {
8543  base_vector &V = mf->is_reduced() ? Vu : Vr;
8544  const gmm::sub_interval
8545  &I = mf->is_reduced()
8546  ? workspace.temporary_interval_of_variable
8547  (root->name_test1)
8548  : workspace.interval_of_variable(root->name_test1);
8549  pgai = std::make_shared<ga_instruction_vector_assembly_mf>
8550  (root->tensor(), V, ctx, I, *mf,
8551  gis.coeff, gis.nbpt, gis.ipt, interpolate);
8552  if (mf->is_reduced())
8553  gis.unreduced_terms.emplace(root->name_test1, "");
8554  }
8555  } else if (imd) {
8556  GMM_ASSERT1(root->interpolate_name_test1.size() == 0,
8557  "Interpolate transformation on integration "
8558  "point variable");
8559  if (!workspace.is_internal_variable(root->name_test1) ||
8560  condensation)
8561  pgai = std::make_shared<ga_instruction_vector_assembly_imd>
8562  (root->tensor(), Vr, gis.ctx,
8563  workspace.interval_of_variable(root->name_test1),
8564  *imd, gis.coeff, gis.ipt);
8565  // Variable root->name_test1 can be internal or not
8566  } else {
8567  pgai = std::make_shared<ga_instruction_vector_assembly>
8568  (root->tensor(), Vr,
8569  workspace.interval_of_variable(root->name_test1),
8570  gis.coeff);
8571  }
8572  break;
8573  }
8574  case 2: {
8575  GMM_ASSERT1(root->tensor_proper_size() == 1,
8576  "Invalid vector or tensor quantity. An order 2 "
8577  "weak form has to be a scalar quantity");
8578  const mesh_fem *mf1=workspace.associated_mf(root->name_test1),
8579  *mf2=workspace.associated_mf(root->name_test2);
8580  const im_data
8581  *imd1 = workspace.associated_im_data(root->name_test1),
8582  *imd2 = workspace.associated_im_data(root->name_test2);
8583  const std::string &intn1 = root->interpolate_name_test1,
8584  &intn2 = root->interpolate_name_test2;
8585  bool secondary1 = intn1.size() &&
8586  workspace.secondary_domain_exists(intn1);
8587  bool secondary2 = intn2.size() &&
8588  workspace.secondary_domain_exists(intn2);
8589  fem_interpolation_context
8590  &ctx1 = intn1.empty() ? gis.ctx
8591  : (secondary1 ? rmi.secondary_domain_infos.ctx
8592  : rmi.interpolate_infos[intn1].ctx),
8593  &ctx2 = intn2.empty() ? gis.ctx
8594  : (secondary2 ? rmi.secondary_domain_infos.ctx
8595  : rmi.interpolate_infos[intn2].ctx);
8596  bool interpolate = !(intn1.empty() || intn1 == "neighbor_element"
8597  || intn1 == "neighbour_elt"
8598  || secondary1) ||
8599  !(intn2.empty() || intn2 == "neighbor_element"
8600  || intn2 == "neighbour_elt"
8601  || secondary2);
8602 
8603  workspace.add_temporary_interval_for_unreduced_variable
8604  (root->name_test1);
8605  workspace.add_temporary_interval_for_unreduced_variable
8606  (root->name_test2);
8607 
8608  bool has_var_group1 = (!intn1.empty() && !secondary1 &&
8609  workspace.variable_group_exists
8610  (root->name_test1));
8611  bool has_var_group2 = (!intn2.empty() && !secondary2 &&
8612  workspace.variable_group_exists
8613  (root->name_test2));
8614  bool simple = !interpolate &&
8615  !has_var_group1 && !has_var_group2 &&
8616  mf1 && !(mf1->is_reduced()) &&
8617  mf2 && !(mf2->is_reduced());
8618 
8619  // ga instructions write into one of the following matrices
8620  auto &Krr = workspace.assembled_matrix();
8621  auto &Kru = workspace.col_unreduced_matrix();
8622  auto &Kur = workspace.row_unreduced_matrix();
8623  auto &Kuu = workspace.row_col_unreduced_matrix();
8624 
8625  if (simple) { // --> Krr
8626  const gmm::sub_interval
8627  &I1 = workspace.interval_of_variable(root->name_test1),
8628  &I2 = workspace.interval_of_variable(root->name_test2);
8629  const scalar_type
8630  &alpha1 = workspace.factor_of_variable(root->name_test1),
8631  &alpha2 = workspace.factor_of_variable(root->name_test2);
8632  if (mf1->get_qdim() == 1 && mf2->get_qdim() == 1)
8633  pgai = std::make_shared
8634  <ga_instruction_matrix_assembly_standard_scalar>
8635  (root->tensor(), Krr, ctx1, ctx2, I1, I2, mf1, mf2,
8636  alpha1, alpha2, gis.coeff, gis.nbpt, gis.ipt);
8637  else if (root->sparsity() == 10 && root->t.qdim() == 2)
8638  pgai = std::make_shared
8639  <ga_instruction_matrix_assembly_standard_vector_opt10<2>>
8640  (root->tensor(), Krr, ctx1, ctx2, I1, I2, mf1, mf2,
8641  alpha1, alpha2, gis.coeff, gis.nbpt, gis.ipt);
8642  else if (root->sparsity() == 10 && root->t.qdim() == 3)
8643  pgai = std::make_shared
8644  <ga_instruction_matrix_assembly_standard_vector_opt10<3>>
8645  (root->tensor(), Krr, ctx1, ctx2, I1, I2, mf1, mf2,
8646  alpha1, alpha2, gis.coeff, gis.nbpt, gis.ipt);
8647  else
8648  pgai = std::make_shared
8649  <ga_instruction_matrix_assembly_standard_vector>
8650  (root->tensor(), Krr, ctx1, ctx2, I1, I2, mf1, mf2,
8651  alpha1, alpha2, gis.coeff, gis.nbpt, gis.ipt);
8652  } else if (condensation &&
8653  workspace.is_internal_variable(root->name_test1) &&
8654  workspace.is_internal_variable(root->name_test2)) {
8655  // diagonal condensation matrix KQQ
8656  // Only memory allocation, gathering of relevant pointers
8657  // and data summation instructions
8658  GMM_ASSERT1(imd1 && imd2, "Internal error");
8659  GMM_ASSERT1(!interpolate, "Internal error");
8660  size_type s1 = imd1->nb_tensor_elem();
8661  size_type s2 = imd2->nb_tensor_elem();
8662 
8663  condensation_description &CC = condensations[rm];
8664  GMM_ASSERT1(CC.Qvars.count(root->name_test1) > 0 &&
8665  CC.Qvars.count(root->name_test2) > 0,
8666  "Internal error");
8667  size_type q1 = CC.Qvars[root->name_test1],
8668  q2 = CC.Qvars[root->name_test2];
8669  if (!CC.KQQ(q1,q2)) {
8670  // allocate a new matrix
8671  gis.condensation_tensors.push_back
8672  (std::make_shared<base_tensor>(s1,s2));
8673  CC.KQQ(q1,q2) = gis.condensation_tensors.back().get();
8674  pgai = std::make_shared<ga_instruction_copy_vect>
8675  (CC.KQQ(q1,q2)->as_vector(), root->tensor().as_vector());
8676  } else {
8677  // addition instruction to the previously allocated matrix
8678  pgai = std::make_shared<ga_instruction_add_to>
8679  (*CC.KQQ(q1,q2), root->tensor());
8680  }
8681  rmi.instructions.push_back(std::move(pgai));
8682  } else if (condensation &&
8683  workspace.is_internal_variable(root->name_test1)) {
8684  // subdiagonal condensation matrix KQJ
8685  // Only memory allocation, gathering of relevant pointers
8686  // and data summation instructions
8687  GMM_ASSERT1(imd1, "Internal error");
8688  GMM_ASSERT1(!interpolate, "Internal error");
8689  size_type s1 = imd1->nb_tensor_elem();
8690 
8691  condensation_description &CC = condensations[rm];
8692  GMM_ASSERT1(CC.Qvars.count(root->name_test1),
8693  "Internal error");
8694  size_type q1 = CC.Qvars[root->name_test1],
8695  j2 = CC.Jvars[root->name_test2];
8696  CC.Jclusters[CC.cluster_of_Qvar[q1]].insert(j2);
8697  if (q1 >= CC.KQJ.nrows() || j2 >= CC.KQJ.ncols())
8698  CC.KQJ.resize(std::max(CC.KQJ.nrows(), q1+1),
8699  std::max(CC.KQJ.ncols(), j2+1));
8700  if (!CC.KQJ(q1,j2)) {
8701  // allocate a new matrix. Here we do not know the size as
8702  // it may change dynamically, but for now, just use the
8703  // size of root->tensor()
8704  gis.condensation_tensors.push_back
8705  (std::make_shared<base_tensor>(root->tensor()));
8706  GMM_ASSERT1(root->tensor().size(0) == s1, "Internal error");
8707  CC.KQJ(q1,j2) = gis.condensation_tensors.back().get();
8708  if (mf2 && !mf2->is_uniform())
8709  rmi.instructions.push_back(std::make_shared
8710  <ga_instruction_second_ind_tensor>
8711  (*CC.KQJ(q1,j2), ctx2, root->qdim2, mf2, nullptr));
8712  pgai = std::make_shared<ga_instruction_copy_vect>
8713  (CC.KQJ(q1,j2)->as_vector(), root->tensor().as_vector());
8714  } else {
8715  // an extra matrix for this entry has already been
8716  // allocated, so just add the current tensor to it
8717  pgai = std::make_shared<ga_instruction_add_to>
8718  (*CC.KQJ(q1,j2), root->tensor());
8719  }
8720  rmi.instructions.push_back(std::move(pgai));
8721  } else if (condensation &&
8722  workspace.is_internal_variable(root->name_test2)) {
8723  // superdiagonal condensation matrix KIQ
8724  // Only memory allocation, gathering of relevant pointers
8725  // and data summation instructions
8726  GMM_ASSERT1(imd2, "Internal error");
8727  GMM_ASSERT1(!interpolate, "Internal error");
8728  size_type s2 = imd2->nb_tensor_elem();
8729 
8730  condensation_description &CC = condensations[rm];
8731  GMM_ASSERT1(CC.Qvars.count(root->name_test2),
8732  "Internal error");
8733  size_type i1 = CC.Ivars[root->name_test1],
8734  q2 = CC.Qvars[root->name_test2];
8735  if (i1 >= CC.KIQ.nrows() || q2 >= CC.KIQ.ncols())
8736  CC.KIQ.resize(std::max(CC.KIQ.nrows(), i1+1),
8737  std::max(CC.KIQ.ncols(), q2+1));
8738  if (!CC.KIQ(i1,q2)) {
8739  // allocate a new matrix. Here we do not know the size as
8740  // it may change dynamically, but for now, just use the
8741  // size of root->tensor()
8742  gis.condensation_tensors.push_back
8743  (std::make_shared<base_tensor>(root->tensor()));
8744  GMM_ASSERT1(root->tensor().size(1) == s2,
8745  "Internal error");
8746  CC.KIQ(i1,q2) = gis.condensation_tensors.back().get();
8747  if (mf1 && !mf1->is_uniform())
8748  rmi.instructions.push_back(std::make_shared
8749  <ga_instruction_first_ind_tensor>
8750  (*CC.KIQ(i1,q2), ctx1, root->qdim1, mf1, nullptr));
8751  pgai = std::make_shared<ga_instruction_copy_vect>
8752  (CC.KIQ(i1,q2)->as_vector(), root->tensor().as_vector());
8753  } else {
8754  // an extra matrix for this entry has already been
8755  // allocated, so just add the current tensor to it
8756  pgai = std::make_shared<ga_instruction_add_to>
8757  (*CC.KIQ(i1,q2), root->tensor());
8758  }
8759  rmi.instructions.push_back(std::move(pgai));
8760  } else if (!workspace.is_internal_variable(root->name_test1) &&
8761  !workspace.is_internal_variable(root->name_test2)) {
8762 
8763  if ((mf1 && mf1->is_reduced()) || (mf2 && mf2->is_reduced())
8764  || has_var_group1 || has_var_group2)
8765  gis.unreduced_terms.emplace(root->name_test1,
8766  root->name_test2);
8767 
8768  auto &Kxu = (mf1 && mf1->is_reduced()) ? Kuu : Kru;
8769  auto &Kxr = (mf1 && mf1->is_reduced()) ? Kur : Krr;
8770  auto &Kux = (mf2 && mf2->is_reduced()) ? Kuu : Kur;
8771  auto &Krx = (mf2 && mf2->is_reduced()) ? Kru : Krr;
8772  auto &Kxx = (mf2 && mf2->is_reduced()) ? Kxu : Kxr;
8773 
8774  const scalar_type
8775  &alpha1 = workspace.factor_of_variable(root->name_test1),
8776  &alpha2 = workspace.factor_of_variable(root->name_test2);
8777 
8778  if (has_var_group1) {
8779  ga_instruction_set::variable_group_info
8780  &vgi1 = rmi.interpolate_infos[intn1]
8781  .groups_info[root->name_test1];
8782  if (has_var_group2) {
8783  ga_instruction_set::variable_group_info
8784  &vgi2 = rmi.interpolate_infos[intn2]
8785  .groups_info[root->name_test2];
8786  pgai = std::make_shared
8787  <ga_instruction_matrix_assembly_mf_mf>
8788  (root->tensor(), Krr, Kru, Kur, Kuu, ctx1, ctx2,
8789  vgi1, vgi2,
8790  gis.coeff, gis.nbpt, gis.ipt, interpolate);
8791  } else {
8792  const gmm::sub_interval &I2 = mf2 && mf2->is_reduced()
8793  ? workspace.temporary_interval_of_variable
8794  (root->name_test2)
8795  : workspace.interval_of_variable(root->name_test2);
8796  if (mf2)
8797  pgai = std::make_shared
8798  <ga_instruction_matrix_assembly_mf_mf>
8799  (root->tensor(), Krx, Kux, ctx1, ctx2,
8800  vgi1, I2, *mf2, alpha2,
8801  gis.coeff, gis.nbpt, gis.ipt, interpolate);
8802  else // for global variable imd2 == 0
8803  pgai = std::make_shared
8804  <ga_instruction_matrix_assembly_mf_imd>
8805  (root->tensor(), Krr, Kur, ctx1, ctx2,
8806  vgi1, I2, imd2, alpha2, gis.coeff, gis.ipt);
8807  }
8808  } else { // !has_var_group1
8809  const gmm::sub_interval &I1 = mf1 && mf1->is_reduced()
8810  ? workspace.temporary_interval_of_variable
8811  (root->name_test1)
8812  : workspace.interval_of_variable(root->name_test1);
8813  if (has_var_group2) {
8814  ga_instruction_set::variable_group_info
8815  &vgi2 = rmi.interpolate_infos[intn2]
8816  .groups_info[root->name_test2];
8817  if (mf1)
8818  pgai = std::make_shared
8819  <ga_instruction_matrix_assembly_mf_mf>
8820  (root->tensor(), Kxr, Kxu, ctx1, ctx2,
8821  I1, *mf1, alpha1, vgi2,
8822  gis.coeff, gis.nbpt, gis.ipt, interpolate);
8823  else // for global variable imd1 == 0
8824  pgai = std::make_shared
8825  <ga_instruction_matrix_assembly_imd_mf>
8826  (root->tensor(), Krr, Kru, ctx1, ctx2,
8827  I1, imd1, alpha1, vgi2, gis.coeff, gis.ipt);
8828  } else { // !has_var_group2
8829  const gmm::sub_interval &I2 = mf2 && mf2->is_reduced()
8830  ? workspace.temporary_interval_of_variable
8831  (root->name_test2)
8832  : workspace.interval_of_variable(root->name_test2);
8833  if (mf1 && mf2)
8834  pgai = std::make_shared
8835  <ga_instruction_matrix_assembly_mf_mf>
8836  (root->tensor(), Kxx, ctx1, ctx2,
8837  I1, *mf1, alpha1, I2, *mf2, alpha2,
8838  gis.coeff, gis.nbpt, gis.ipt, interpolate);
8839  else if (mf1) // for global variable imd2 == 0
8840  pgai = std::make_shared
8841  <ga_instruction_matrix_assembly_mf_imd>
8842  (root->tensor(), Kxr, ctx1, ctx2,
8843  I1, *mf1, alpha1, I2, imd2, alpha2,
8844  gis.coeff, gis.ipt);
8845  else if (mf2)
8846  pgai = std::make_shared
8847  <ga_instruction_matrix_assembly_imd_mf>
8848  (root->tensor(), Krx, ctx1, ctx2,
8849  I1, imd1, alpha1, I2, *mf2, alpha2,
8850  gis.coeff, gis.ipt);
8851  else
8852  pgai = std::make_shared
8853  <ga_instruction_matrix_assembly_imd_imd>
8854  (root->tensor(), Krr, ctx1, ctx2,
8855  I1, imd1, alpha1, I2, imd2, alpha2,
8856  gis.coeff, gis.ipt);
8857  }
8858  }
8859  } // if (!simple)
8860  break;
8861  } // case 2
8862  } // switch(order)
8863  if (pgai)
8864  rmi.instructions.push_back(std::move(pgai));
8865  }
8866  } // if (root)
8867  } // if (td.order == order || td.order == size_type(-1))
8868  } // for (const ga_workspace::tree_description &td : trees_of_current_phase)
8869 
8870  if (condensation && order == 2 && phase == ga_workspace::ASSEMBLY) {
8871 
8872  auto &Krr = workspace.assembled_matrix();
8873  auto &Kru = workspace.col_unreduced_matrix();
8874  auto &Kur = workspace.row_unreduced_matrix();
8875  auto &Kuu = workspace.row_col_unreduced_matrix();
8876 
8877  for (auto &&key_val : condensations) {
8878  const ga_instruction_set::region_mim rm = key_val.first;
8879  condensation_description &CC = key_val.second;
8880  auto &rmi = gis.all_instructions[rm];
8881 
8882  CC.KQJpr.resize(CC.KQJ.nrows(), CC.KQJ.ncols());
8883  for (size_type k=0; k < CC.KQJpr.size(); ++k) {
8884  gis.condensation_tensors.push_back // memory allocation
8885  (std::make_shared<base_tensor>(2,2));
8886  CC.KQJpr[k] = gis.condensation_tensors.back().get();
8887  }
8888 
8889  pga_instruction pgai;
8890 
8891  // Add one diagonal/subdiagonal condensation instruction per cluster
8892  for (size_type k=0; k < CC.Qclusters.size(); ++k) {
8893  // extract condensed variables residuals from
8894  // workspace.assembled_vector() into RQpr
8895  for (size_type q1 : CC.Qclusters[k]) {
8896  std::string name_test1 = CC.Qvars[q1];
8897  const im_data *imd1 = workspace.associated_im_data(name_test1);
8898  const gmm::sub_interval
8899  &I1 = workspace.interval_of_variable(name_test1);
8900  pgai =
8901  std::make_shared<ga_instruction_extract_residual_on_imd_dofs>
8902  (*(CC.RQpr[q1]), workspace.cached_vector(), // cached_V --> CC.RQpr[q1]
8903  gis.ctx, I1, *imd1, gis.ipt);
8904  rmi.instructions.push_back(std::move(pgai));
8905  }
8906 
8907  // the exec() of this instruction calculates KQJpr including any
8908  // necessary size update to match the sizes of KQJ, upon size change
8909  // of primary variables J
8910  pgai = std::make_shared<ga_instruction_condensation_sub>
8911  (CC.KQJpr, CC.RQpr, CC.KQQ, CC.KQJ, CC.Qclusters[k], gis.coeff); // factor_of_variable()?
8912  rmi.instructions.push_back(std::move(pgai));
8913 
8914  // assemble/store KQJpr/RQpr matrices/vectors into the
8915  // corresponding global matrix/vector
8916  for (size_type q1 : CC.Qclusters[k]) {
8917  std::string name_test1 = CC.Qvars[q1];
8918  const im_data *imd1 = workspace.associated_im_data(name_test1);
8919 // const scalar_type
8920 // &alpha1 = workspace.factor_of_variable(name_test1); // TODO
8921  const gmm::sub_interval
8922  &I1 = workspace.interval_of_variable(name_test1);
8923  GMM_ASSERT1(imd1, "Internal error");
8924  for (size_type j2 : CC.Jclusters[k]) {
8925  std::string name_test2 = CC.Jvars[j2];
8926  const mesh_fem *mf2 = workspace.associated_mf(name_test2); // TODO: name_test2 variable group
8927  const im_data *imd2 = workspace.associated_im_data(name_test2);
8928 // const std::string &intn2 = root->interpolate_name_test2;
8929 // GMM_ASSERT1(intn2.empty(), "Coupling of internal variables "
8930 // "with interpolated variables not "
8931 // "implemented yet");
8932 // const scalar_type
8933 // &alpha2 = workspace.factor_of_variable(name_test2); // TODO
8934  const gmm::sub_interval
8935  &I2 = mf2 && mf2->is_reduced()
8936  ? workspace.temporary_interval_of_variable(name_test2)
8937  : workspace.interval_of_variable(name_test2);
8938  const base_tensor &Kq1j2pr = *(CC.KQJpr(q1,j2)); // <- input
8939  model_real_sparse_matrix
8940  &KQJpr = mf2 && mf2->is_reduced()
8941  ? workspace.col_unreduced_matrix()
8942  : workspace.internal_coupling_matrix(); // <- output
8943  if (mf2) {
8944  pgai =
8945  std::make_shared<ga_instruction_matrix_assembly_imd_mf>
8946  (Kq1j2pr, KQJpr, gis.ctx, gis.ctx,
8947  I1, imd1, gis.ONE, I2, *mf2, gis.ONE, gis.ONE, gis.ipt); // without gis.coeff
8948  // TODO: name_test2 variable group
8949  if (mf2->is_reduced())
8950  gis.unreduced_terms.emplace(name_test1, name_test2);
8951  } else // for global variable imd2 == 0
8952  pgai =
8953  std::make_shared<ga_instruction_matrix_assembly_imd_imd>
8954  (Kq1j2pr, KQJpr, gis.ctx, gis.ctx,
8955  I1, imd1, gis.ONE, I2, imd2, gis.ONE, gis.ONE, gis.ipt); // without gis.coeff
8956  rmi.instructions.push_back(std::move(pgai));
8957  } // for j2
8958  const bool initialize = true;
8959  pgai = std::make_shared<ga_instruction_vector_assembly_imd>
8960  (*(CC.RQpr[q1]), workspace.assembled_vector(), // <- overwriting internal variables residual with internal solution
8961  gis.ctx, I1, *imd1, gis.ONE, gis.ipt, initialize); // without gis.coeff
8962  rmi.instructions.push_back(std::move(pgai));
8963  } // for q1
8964  }
8965 
8966  // Add superdiagonal condensation instructions
8967  for (size_type i1=0; i1 < CC.Ivars.size(); ++i1) {
8968 
8969  std::string name_test1 = CC.Ivars[i1];
8970  const mesh_fem *mf1 = workspace.associated_mf(name_test1); // TODO: name_test1 variable group
8971  const im_data *imd1 = workspace.associated_im_data(name_test1);
8972  const scalar_type
8973  &alpha1 = workspace.factor_of_variable(name_test1);
8974  const gmm::sub_interval
8975  &I1 = mf1 && mf1->is_reduced()
8976  ? workspace.temporary_interval_of_variable(name_test1)
8977  : workspace.interval_of_variable(name_test1);
8978 
8979  // Q_of_J[j2] will hold all condensed variables q that couple
8980  // variable i1 to each variable j2
8981  std::vector<std::set<size_type>> Q_of_J(CC.Jvars.size());
8982  for (size_type q=0; q < CC.Qvars.size(); ++q)
8983  if (CC.KIQ(i1,q)) {
8984  size_type cid = CC.cluster_of_Qvar[q];
8985  for (size_type j : CC.Jclusters[cid])
8986  Q_of_J[j].insert(q);
8987  }
8988 
8989  for (size_type j2=0; j2 < CC.Jvars.size(); ++j2) {
8990  if (Q_of_J[j2].size()) { // a coupling between i1 and j2 exists
8991  std::vector<base_tensor *> Ki1Q, KQj2;
8992  for (size_type q : Q_of_J[j2]) {
8993  Ki1Q.push_back(CC.KIQ(i1,q));
8994  KQj2.push_back(CC.KQJpr(q,j2));
8995  }
8996  // allocate a tensor for storing the coupling between i1 and j2
8997  gis.condensation_tensors.push_back
8998  (std::make_shared<base_tensor>());
8999  base_tensor &Kij = *gis.condensation_tensors.back();
9000  pgai = std::make_shared<ga_instruction_condensation_super_K>
9001  (Kij, Ki1Q, KQj2);
9002  rmi.instructions.push_back(std::move(pgai));
9003  // add assembly instruction
9004  std::string name_test2 = CC.Jvars[j2];
9005  const mesh_fem *mf2 = workspace.associated_mf(name_test2); // TODO: name_test2 variable group
9006  const im_data *imd2 = workspace.associated_im_data(name_test2);
9007  // Here assuming interpolate_name_test1.empty() &&
9008  // interpolate_name_test2.empty() &&
9009  // !(secondary1 || secondary2) && !interpolate;
9010  const scalar_type
9011  &alpha2 = workspace.factor_of_variable(name_test2);
9012  const gmm::sub_interval
9013  &I2 = mf2 && mf2->is_reduced()
9014  ? workspace.temporary_interval_of_variable(name_test2)
9015  : workspace.interval_of_variable(name_test2);
9016 
9017  auto &Kxu = (mf1 && mf1->is_reduced()) ? Kuu : Kru;
9018  auto &Kxr = (mf1 && mf1->is_reduced()) ? Kur : Krr;
9019  auto &Krx = (mf2 && mf2->is_reduced()) ? Kru : Krr;
9020  auto &Kxx = (mf2 && mf2->is_reduced()) ? Kxu : Kxr;
9021 
9022  if ((mf1 && mf1->is_reduced()) || (mf2 && mf2->is_reduced()))
9023  gis.unreduced_terms.emplace(name_test1, name_test2);
9024 
9025  if (mf1 && mf2) // TODO: name_test1 or name_test2 variable group
9026  pgai = std::make_shared
9027  <ga_instruction_matrix_assembly_mf_mf>
9028  (Kij, Kxx, gis.ctx, gis.ctx,
9029  I1, *mf1, alpha1, I2, *mf2, alpha2,
9030  gis.coeff, gis.nbpt, gis.ipt, false);
9031  else if (mf1) // for global variable imd2 == 0
9032  pgai = std::make_shared
9033  <ga_instruction_matrix_assembly_mf_imd>
9034  (Kij, Kxr, gis.ctx, gis.ctx,
9035  I1, *mf1, alpha1, I2, imd2, alpha2,
9036  gis.coeff, gis.ipt);
9037  else if (mf2)
9038  pgai = std::make_shared
9039  <ga_instruction_matrix_assembly_imd_mf>
9040  (Kij, Krx, gis.ctx, gis.ctx,
9041  I1, imd1, alpha1, I2, *mf2, alpha2,
9042  gis.coeff, gis.ipt);
9043  else
9044  pgai = std::make_shared
9045  <ga_instruction_matrix_assembly_imd_imd>
9046  (Kij, Krr, gis.ctx, gis.ctx,
9047  I1, imd1, alpha1, I2, imd2, alpha2,
9048  gis.coeff, gis.ipt);
9049  rmi.instructions.push_back(std::move(pgai));
9050  } // if (Q_of_J[j2].size())
9051  } // for j2
9052 
9053  // RHS condensation instructions
9054  std::vector<base_tensor *> Ki1Q, RQpr;
9055  for (size_type q=0; q < CC.Qvars.size(); ++q)
9056  if (CC.KIQ(i1,q)) {
9057  Ki1Q.push_back(CC.KIQ(i1,q));
9058  RQpr.push_back(CC.RQpr[q]);
9059  }
9060  gis.condensation_tensors.push_back
9061  (std::make_shared<base_tensor>());
9062  base_tensor &Ri = *gis.condensation_tensors.back();
9063  pgai = std::make_shared<ga_instruction_condensation_super_R>
9064  (Ri, Ki1Q, RQpr);
9065  rmi.instructions.push_back(std::move(pgai));
9066 
9067  base_vector &R = mf1->is_reduced() ? workspace.unreduced_vector()
9068  : workspace.assembled_vector();
9069  if (mf1)
9070  pgai = std::make_shared<ga_instruction_vector_assembly_mf>
9071  (Ri, R, gis.ctx, I1, *mf1, gis.coeff, gis.nbpt, gis.ipt, false);
9072  else if (imd1)
9073  pgai = std::make_shared<ga_instruction_vector_assembly_imd>
9074  (Ri, R, gis.ctx, I1, *imd1, gis.coeff, gis.ipt);
9075  else
9076  pgai = std::make_shared<ga_instruction_vector_assembly>
9077  (Ri, R, I1, gis.coeff);
9078  rmi.instructions.push_back(std::move(pgai));
9079  } // for i1
9080  } // for (const auto &key_val : condensations)
9081  } // if (phase == ga_workspace::ASSEMBLY)
9082  } // for (const auto &phase : phases)
9083 
9084  } // ga_compile(...)
9085 
9086 
9087 
9088  //=========================================================================
9089  // Execution of a compiled set of assembly terms
9090  //=========================================================================
9091 
9092 
9093  void ga_function_exec(ga_instruction_set &gis) {
9094 
9095  for (auto &&instr : gis.all_instructions) {
9096  const auto &gil = instr.second.instructions;
9097  for (size_type j = 0; j < gil.size(); ++j) j += gil[j]->exec();
9098  }
9099  }
9100 
9101  void ga_interpolation_exec(ga_instruction_set &gis,
9102  ga_workspace &workspace,
9103  ga_interpolation_context &gic) {
9104  base_matrix G;
9105  base_small_vector un, up;
9106 
9107  for (const std::string &t : gis.transformations)
9108  workspace.interpolate_transformation(t)->init(workspace);
9109 
9110  for (auto &&instr : gis.all_instructions) {
9111 
9112  const getfem::mesh_im &mim = *(instr.first.mim());
9113  const mesh_region &region = *(instr.first.region());
9114  const getfem::mesh &m = *(instr.second.m);
9115  GMM_ASSERT1(&m == &(gic.linked_mesh()),
9116  "Incompatibility of meshes in interpolation");
9117  const auto &gilb = instr.second.begin_instructions;
9118  const auto &gile = instr.second.elt_instructions;
9119  const auto &gil = instr.second.instructions;
9120 
9121  // iteration on elements (or faces of elements)
9122  std::vector<size_type> ind;
9123  auto pai_old = papprox_integration{};
9124  for (getfem::mr_visitor v(region, m, true); !v.finished(); ++v) {
9125  if (gic.use_mim()) {
9126  if (!mim.convex_index().is_in(v.cv())) continue;
9127  gis.pai = mim.int_method_of_element(v.cv())->approx_method();
9128  } else
9129  gis.pai = 0;
9130 
9131  ind.resize(0);
9132  bgeot::pstored_point_tab pspt
9133  = gic.ppoints_for_element(v.cv(), v.f(), ind);
9134 
9135  if (pspt.get() && ind.size() && pspt->size()) {
9136  m.points_of_convex(v.cv(), G);
9137  bgeot::pgeometric_trans pgt = m.trans_of_convex(v.cv());
9138  up.resize(G.nrows());
9139  un.resize(pgt->dim());
9140 
9141  if (gis.ctx.have_pgp() && gis.ctx.pgt() == pgt && pai_old == gis.pai) {
9142  gis.ctx.change(gis.ctx.pgp(), 0, 0, G, v.cv(), v.f());
9143  } else {
9144  if (!(gic.use_pgp(v.cv()))) {
9145  gis.ctx.change(pgt, 0, (*pspt)[0], G, v.cv(), v.f());
9146  } else {
9147  gis.ctx.change(gis.gp_pool(pgt, pspt), 0, 0, G, v.cv(), v.f());
9148  }
9149  }
9150  pai_old = gis.pai;
9151 
9152  if (gis.need_elt_size)
9153  gis.elt_size = m.convex_radius_estimate(v.cv()) * scalar_type(2);
9154 
9155  // iterations on interpolation points
9156  gis.nbpt = pspt->size();
9157  for (size_type ii = 0; ii < ind.size(); ++ii) {
9158  gis.ipt = ii;
9159  if (gis.ctx.have_pgp()) gis.ctx.set_ii(ind[ii]);
9160  else gis.ctx.set_xref((*pspt)[gis.ipt]);
9161 
9162  if (ii == 0 || !(pgt->is_linear())) {
9163  // Computation of unit normal vector in case of a boundary
9164  if (v.f() != short_type(-1)) {
9165  const base_matrix& B = gis.ctx.B();
9166  gmm::copy(pgt->normals()[v.f()], un);
9167  gmm::mult(B, un, up);
9168  scalar_type nup = gmm::vect_norm2(up);
9169  gmm::scale(up,1.0/nup);
9170  gmm::clean(up, 1e-13);
9171  gis.Normal = up;
9172  } else // unit normal of a curve in 2D or a surface in 3D
9173  gis.Normal = bgeot::compute_unit_element_normal(gis.ctx);
9174  }
9175  gmm::clear(workspace.assembled_tensor().as_vector());
9176  if (ii == 0) {
9177  for (size_type j = 0; j < gilb.size(); ++j) j += gilb[j]->exec();
9178  for (size_type j = 0; j < gile.size(); ++j) j += gile[j]->exec();
9179  }
9180  for (size_type j = 0; j < gil.size(); ++j) j += gil[j]->exec();
9181  gic.store_result(v.cv(), ind[ii], workspace.assembled_tensor());
9182  }
9183  }
9184  }
9185  }
9186  for (const std::string &t : gis.transformations)
9187  workspace.interpolate_transformation(t)->finalize();
9188 
9189  gic.finalize();
9190  }
9191 
9192  void ga_exec(ga_instruction_set &gis, ga_workspace &workspace) {
9193  base_matrix G1, G2;
9194  base_small_vector un;
9195  scalar_type J1(0), J2(0);
9196 
9197  for (const std::string &t : gis.transformations)
9198  workspace.interpolate_transformation(t)->init(workspace);
9199 
9200  for (auto &instr : gis.all_instructions) {
9201  const getfem::mesh_im &mim = *(instr.first.mim());
9202  psecondary_domain psd = instr.first.psd();
9203  const getfem::mesh &m = *(instr.second.m);
9204  GMM_ASSERT1(&m == &(mim.linked_mesh()), "Incompatibility of meshes");
9205  const auto &gilb = instr.second.begin_instructions;
9206  const auto &gile = instr.second.elt_instructions;
9207  const auto &gil = instr.second.instructions;
9208 
9209  // if (gilb.size()) cout << "Begin instructions\n";
9210  // for (size_type j = 0; j < gilb.size(); ++j)
9211  // cout << typeid(*(gilb[j])).name() << endl;
9212  // if (gile.size()) cout << "\nElement instructions\n";
9213  // for (size_type j = 0; j < gile.size(); ++j)
9214  // cout << typeid(*(gile[j])).name() << endl;
9215  // cout << "\nGauss pt instructions\n";
9216  // for (size_type j = 0; j < gil.size(); ++j)
9217  // cout << typeid(*(gil[j])).name() << endl;
9218 
9219  if (!psd) { // standard integration on a single domain
9220 
9221  const mesh_region &region = *(instr.first.region());
9222 
9223  // iteration on elements (or faces of elements)
9224  size_type old_cv = size_type(-1);
9225  bgeot::pgeometric_trans pgt = 0, pgt_old = 0;
9226  pintegration_method pim = 0;
9227  papprox_integration pai = 0;
9228  bgeot::pstored_point_tab pspt = 0, old_pspt = 0;
9229  bgeot::pgeotrans_precomp pgp = 0;
9230  bool first_gp = true;
9231  for (getfem::mr_visitor v(region, m, true); !v.finished(); ++v) {
9232  if (mim.convex_index().is_in(v.cv())) {
9233  // cout << "proceed with elt " << v.cv() << " face " << v.f()<<endl;
9234  if (v.cv() != old_cv) {
9235  pgt = m.trans_of_convex(v.cv());
9236  pim = mim.int_method_of_element(v.cv());
9237  m.points_of_convex(v.cv(), G1);
9238 
9239  if (pim->type() == IM_NONE) continue;
9240  GMM_ASSERT1(pim->type() == IM_APPROX, "Sorry, exact methods "
9241  "cannot be used in high level generic assembly");
9242  pai = pim->approx_method();
9243  pspt = pai->pintegration_points();
9244  if (pspt->size()) {
9245  if (pgp && gis.pai == pai && pgt_old == pgt) {
9246  gis.ctx.change(pgp, 0, 0, G1, v.cv(), v.f());
9247  } else {
9248  if (pai->is_built_on_the_fly()) {
9249  gis.ctx.change(pgt, 0, (*pspt)[0], G1, v.cv(), v.f());
9250  pgp = 0;
9251  } else {
9252  pgp = gis.gp_pool(pgt, pspt);
9253  gis.ctx.change(pgp, 0, 0, G1, v.cv(), v.f());
9254  }
9255  pgt_old = pgt; gis.pai = pai;
9256  }
9257  if (gis.need_elt_size)
9258  gis.elt_size = convex_radius_estimate(pgt, G1)*scalar_type(2);
9259  }
9260  old_cv = v.cv();
9261  } else {
9262  if (pim->type() == IM_NONE) continue;
9263  gis.ctx.set_face_num(v.f());
9264  }
9265  if (pspt != old_pspt) { first_gp = true; old_pspt = pspt; }
9266  if (pspt->size()) {
9267  // Iterations on Gauss points
9268  size_type first_ind = 0;
9269  if (v.f() != short_type(-1)) {
9270  gis.nbpt = pai->nb_points_on_face(v.f());
9271  first_ind = pai->ind_first_point_on_face(v.f());
9272  } else {
9273  gis.nbpt = pai->nb_points_on_convex();
9274  }
9275  for (gis.ipt = 0; gis.ipt < gis.nbpt; ++(gis.ipt)) {
9276  if (pgp) gis.ctx.set_ii(first_ind+gis.ipt);
9277  else gis.ctx.set_xref((*pspt)[first_ind+gis.ipt]);
9278  if (gis.ipt == 0 || !(pgt->is_linear())) {
9279  J1 = gis.ctx.J();
9280  // Computation of unit normal vector in case of a boundary
9281  if (v.f() != short_type(-1)) {
9282  gis.Normal.resize(G1.nrows());
9283  un.resize(pgt->dim());
9284  gmm::copy(pgt->normals()[v.f()], un);
9285  gmm::mult(gis.ctx.B(), un, gis.Normal);
9286  scalar_type nup = gmm::vect_norm2(gis.Normal);
9287  J1 *= nup;
9288  gmm::scale(gis.Normal, 1.0/nup);
9289  gmm::clean(gis.Normal, 1e-13);
9290  } else // unit normal of a curve in 2D or a surface in 3D
9291  gis.Normal = bgeot::compute_unit_element_normal(gis.ctx);
9292  }
9293  auto ipt_coeff = pai->coeff(first_ind+gis.ipt);
9294  gis.coeff = J1 * ipt_coeff;
9295  bool enable_ipt = (gmm::abs(ipt_coeff) > 0.0 ||
9296  workspace.include_empty_int_points());
9297  if (!enable_ipt) gis.coeff = scalar_type(0);
9298  if (first_gp) {
9299  for (size_type j=0; j < gilb.size(); ++j) j+=gilb[j]->exec();
9300  first_gp = false;
9301  }
9302  if (gis.ipt == 0) {
9303  for (size_type j=0; j < gile.size(); ++j) j+=gile[j]->exec();
9304  }
9305  if (enable_ipt || gis.ipt == 0 || gis.ipt == gis.nbpt-1) {
9306  for (size_type j=0; j < gil.size(); ++j) j+=gil[j]->exec();
9307  }
9308  GA_DEBUG_INFO("");
9309  }
9310  }
9311  }
9312  }
9313  GA_DEBUG_INFO("-----------------------------");
9314 
9315  } else { // Integration on the product of two domains (secondary domain)
9316 
9317  auto &sdi = instr.second.secondary_domain_infos;
9318  const mesh_region &region1 = *(instr.first.region());
9319 
9320  // iteration on elements (or faces of elements)
9321  size_type old_cv1=size_type(-1), old_cv2=size_type(-1);
9322  size_type nbpt1 = 0, nbpt2 = 0;
9323  bgeot::pgeometric_trans pgt1 = 0, pgt1_old = 0, pgt2 = 0, pgt2_old = 0;
9324  pintegration_method pim1 = 0, pim2 = 0;
9325  papprox_integration pai1 = 0, pai2 = 0;
9326  bgeot::pstored_point_tab pspt1=0, old_pspt1=0, pspt2=0, old_pspt2=0;
9327  bgeot::pgeotrans_precomp pgp1 = 0, pgp2 = 0;
9328  bool first_gp = true;
9329  for (getfem::mr_visitor v1(region1, m, true); !v1.finished(); ++v1) {
9330  if (mim.convex_index().is_in(v1.cv())) {
9331  // cout << "proceed with elt " << v1.cv()<<" face " << v1.f()<<endl;
9332  if (v1.cv() != old_cv1) {
9333  pgt1 = m.trans_of_convex(v1.cv());
9334  pim1 = mim.int_method_of_element(v1.cv());
9335  m.points_of_convex(v1.cv(), G1);
9336 
9337  if (pim1->type() == IM_NONE) continue;
9338  GMM_ASSERT1(pim1->type() == IM_APPROX, "Sorry, exact methods "
9339  "cannot be used in high level generic assembly");
9340  pai1 = pim1->approx_method();
9341  pspt1 = pai1->pintegration_points();
9342  if (pspt1->size()) {
9343  if (pgp1 && gis.pai == pai1 && pgt1_old == pgt1) {
9344  gis.ctx.change(pgp1, 0, 0, G1, v1.cv(), v1.f());
9345  } else {
9346  if (pai1->is_built_on_the_fly()) {
9347  gis.ctx.change(pgt1, 0, (*pspt1)[0], G1, v1.cv(), v1.f());
9348  pgp1 = 0;
9349  } else {
9350  pgp1 = gis.gp_pool(pgt1, pspt1);
9351  gis.ctx.change(pgp1, 0, 0, G1, v1.cv(), v1.f());
9352  }
9353  pgt1_old = pgt1; gis.pai = pai1;
9354  }
9355  if (gis.need_elt_size)
9356  gis.elt_size = convex_radius_estimate(pgt1,G1)*scalar_type(2);
9357  }
9358  old_cv1 = v1.cv();
9359  } else {
9360  if (pim1->type() == IM_NONE) continue;
9361  gis.ctx.set_face_num(v1.f());
9362  }
9363  if (pspt1 != old_pspt1) { first_gp = true; old_pspt1 = pspt1; }
9364  if (pspt1->size()) {
9365  // iterations on Gauss points
9366  size_type first_ind1 = 0;
9367  if (v1.f() != short_type(-1)) {
9368  nbpt1 = pai1->nb_points_on_face(v1.f());
9369  first_ind1 = pai1->ind_first_point_on_face(v1.f());
9370  } else {
9371  nbpt1 = pai1->nb_points_on_convex();
9372  }
9373 
9374  const mesh &m2 = psd->mim().linked_mesh();
9375  const mesh_region &region2 = psd->give_region(m, v1.cv(), v1.f());
9376  for (getfem::mr_visitor v2(region2, m2, true);
9377  !v2.finished(); ++v2) {
9378  if (v2.cv() != old_cv2) {
9379  pgt2 = m2.trans_of_convex(v2.cv());
9380  pim2 = psd->mim().int_method_of_element(v2.cv());
9381  m2.points_of_convex(v2.cv(), G2);
9382 
9383  if (pim2->type() == IM_NONE) continue;
9384  GMM_ASSERT1(pim2->type() == IM_APPROX, "Sorry, exact methods "
9385  "cannot be used in high level generic assembly");
9386  pai2 = pim2->approx_method();
9387  pspt2 = pai2->pintegration_points();
9388  if (pspt2->size()) {
9389  if (pgp2 && sdi.pai == pai2 && pgt2_old == pgt2) {
9390  sdi.ctx.change(pgp2, 0, 0, G2, v2.cv(), v2.f());
9391  } else {
9392  if (pai2->is_built_on_the_fly()) {
9393  sdi.ctx.change(pgt2, 0, (*pspt2)[0], G2,v2.cv(),v2.f());
9394  pgp2 = 0;
9395  } else {
9396  pgp2 = gis.gp_pool(pgt2, pspt2);
9397  sdi.ctx.change(pgp2, 0, 0, G2, v2.cv(), v2.f());
9398  }
9399  pgt2_old = pgt2; sdi.pai = pai2;
9400  }
9401  }
9402  old_cv2 = v2.cv();
9403  } else {
9404  if (pim2->type() == IM_NONE) continue;
9405  sdi.ctx.set_face_num(v2.f());
9406  }
9407  if (pspt2 != old_pspt2) { first_gp = true; old_pspt2 = pspt2; }
9408  if (pspt2->size()) {
9409  // iterations on Gauss points
9410  size_type first_ind2 = 0;
9411  if (v2.f() != short_type(-1)) {
9412  nbpt2 = pai2->nb_points_on_face(v2.f());
9413  first_ind2 = pai2->ind_first_point_on_face(v2.f());
9414  } else {
9415  nbpt2 = gis.nbpt = pai2->nb_points_on_convex();
9416  }
9417  gis.nbpt = nbpt1 * nbpt2;
9418  gis.ipt = 0;
9419  for (size_type ipt1=0; ipt1 < nbpt1; ++ipt1) {
9420  for (size_type ipt2=0; ipt2 < nbpt2; ++ipt2, ++(gis.ipt)) {
9421 
9422  if (pgp1) gis.ctx.set_ii(first_ind1+ipt1);
9423  else gis.ctx.set_xref((*pspt1)[first_ind1+ipt1]);
9424  if (pgp2) sdi.ctx.set_ii(first_ind2+ipt2);
9425  else sdi.ctx.set_xref((*pspt2)[first_ind2+ipt2]);
9426 
9427  if (gis.ipt == 0 || !(pgt1->is_linear())) {
9428  J1 = gis.ctx.J();
9429  if (v1.f() != short_type(-1)) {
9430  gis.Normal.resize(G1.nrows());
9431  un.resize(pgt1->dim());
9432  gmm::copy(pgt1->normals()[v1.f()], un);
9433  gmm::mult(gis.ctx.B(), un, gis.Normal);
9434  scalar_type nup = gmm::vect_norm2(gis.Normal);
9435  J1 *= nup;
9436  gmm::scale(gis.Normal, 1.0/nup);
9437  gmm::clean(gis.Normal, 1e-13);
9438  } else // curve in 2D or surface in 3D
9439  gis.Normal
9441  }
9442 
9443  if (gis.ipt == 0 || !(pgt2->is_linear())) {
9444  J2 = sdi.ctx.J();
9445  if (v2.f() != short_type(-1)) {
9446  sdi.Normal.resize(G2.nrows());
9447  un.resize(pgt2->dim());
9448  gmm::copy(pgt2->normals()[v2.f()], un);
9449  gmm::mult(sdi.ctx.B(), un, sdi.Normal);
9450  scalar_type nup = gmm::vect_norm2(sdi.Normal);
9451  J2 *= nup;
9452  gmm::scale(sdi.Normal, 1.0/nup);
9453  gmm::clean(sdi.Normal, 1e-13);
9454  } else // curve in 2D or surface in 3D
9455  sdi.Normal
9457  }
9458 
9459  auto ipt_coeff = pai1->coeff(first_ind1+ipt1)
9460  * pai2->coeff(first_ind2+ipt2);
9461  gis.coeff = J1 * J2 * ipt_coeff;
9462  bool enable_ipt = (gmm::abs(ipt_coeff) > 0.0 ||
9463  workspace.include_empty_int_points());
9464  if (!enable_ipt) gis.coeff = scalar_type(0);
9465 
9466  if (first_gp) {
9467  for (size_type j=0; j < gilb.size(); ++j)
9468  j+=gilb[j]->exec();
9469  first_gp = false;
9470  }
9471  if (gis.ipt == 0) {
9472  for (size_type j=0; j < gile.size(); ++j)
9473  j+=gile[j]->exec();
9474  }
9475  if (enable_ipt || gis.ipt == 0 || gis.ipt == gis.nbpt-1) {
9476  for (size_type j=0; j < gil.size(); ++j)
9477  j+=gil[j]->exec();
9478  }
9479  GA_DEBUG_INFO("");
9480  }
9481  }
9482  }
9483  }
9484  }
9485  }
9486  }
9487  GA_DEBUG_INFO("-----------------------------");
9488  }
9489 
9490  }
9491 
9492  for (const std::string &t : gis.transformations)
9493  workspace.interpolate_transformation(t)->finalize();
9494  }
9495 
9496 
9497 } /* end of namespace */
does the inversion of the geometric transformation for a given convex
bool invert(const base_node &x, base_node &xref, scalar_type IN_EPS=1e-12, bool project_into_element=false)
given the node on the real element, returns the node on the reference element (even if it is outside ...
The object geotrans_precomp_pool Allow to allocate a certain number of geotrans_precomp and automatic...
static T & instance()
Instance from the current thread.
Describe an integration method linked to a mesh.
virtual pintegration_method int_method_of_element(size_type cv) const
return the integration method associated with an element (in no integration is associated,...
const mesh & linked_mesh() const
Give a reference to the linked mesh of type mesh.
const dal::bit_vector & convex_index(void) const
Get the set of convexes where an integration method has been assigned.
"iterator" class for regions.
static mesh_region intersection(const mesh_region &a, const mesh_region &b)
return the intersection of two mesh regions
Describe a mesh (collection of convexes (elements) and points).
Definition: getfem_mesh.h:98
virtual scalar_type convex_radius_estimate(size_type ic) const
Return an estimate of the convex largest dimension.
Definition: getfem_mesh.cc:460
sparse vector built upon std::vector.
Definition: gmm_vector.h:995
Semantic analysis of assembly trees and semantic manipulations.
Compilation and execution operations.
a subclass of mesh_im which is conformal to a number of level sets.
void copy(const L1 &l1, L2 &l2)
*‍/
Definition: gmm_blas.h:976
number_traits< typename linalg_traits< V >::value_type >::magnitude_type vect_norm2(const V &v)
Euclidean norm of a vector.
Definition: gmm_blas.h:556
number_traits< typename linalg_traits< V >::value_type >::magnitude_type vect_norminf(const V &v)
Infinity norm of a vector.
Definition: gmm_blas.h:692
void clear(L &l)
clear (fill with zeros) a vector or matrix.
Definition: gmm_blas.h:58
void resize(V &v, size_type n)
*‍/
Definition: gmm_blas.h:209
void clean(L &l, double threshold)
Clean a vector or matrix (replace near-zero entries with zeroes).
void mult(const L1 &l1, const L2 &l2, L3 &l3)
*‍/
Definition: gmm_blas.h:1663
void add(const L1 &l1, L2 &l2)
*‍/
Definition: gmm_blas.h:1275
scalar_type APIDECL convex_radius_estimate(bgeot::pgeometric_trans pgt, const base_matrix &pts)
rough estimate of the radius of the convex using the largest eigenvalue of the jacobian of the geomet...
Definition: getfem_mesh.cc:797
std::shared_ptr< const getfem::virtual_fem > pfem
type of pointer on a fem description
Definition: getfem_fem.h:243
gmm::uint16_type short_type
used as the common short type integer in the library
Definition: bgeot_config.h:72
base_small_vector compute_unit_element_normal(const geotrans_interpolation_context &c)
Unit normal vector to an element of dimension N-1 in dimension N (a curve in 2D or a surface in 3D),...
base_small_vector compute_normal(const geotrans_interpolation_context &c, size_type face)
norm of returned vector is the ratio between the face surface on the real element and the face surfac...
size_t size_type
used as the common size type in the library
Definition: bgeot_poly.h:48
std::shared_ptr< const bgeot::geometric_trans > pgeometric_trans
pointer type for a geometric transformation
size_type alpha(short_type n, short_type d)
Return the value of which is the number of monomials of a polynomial of variables and degree .
Definition: bgeot_poly.cc:46
GEneric Tool for Finite Element Methods.
void slice_vector_on_basic_dof_of_element(const mesh_fem &mf, const VEC1 &vec, size_type cv, VEC2 &coeff, size_type qmult1=size_type(-1), size_type qmult2=size_type(-1))
Given a mesh_fem.
Point tab storage.