Caching in the macro kernal

Hi,

I have implemented a piece of code adding a body forcing with a number of Fourier mode with random phase. The random phases will be updated while run time so it has been passed by the external variable hphs0:

<%pyfr:macro name='bdforc' params='t, u, ploc, src' externs='hphs0'>

  // Spatial variable
  fpdtype_t ploc_t = atan2(ploc[1], ploc[2]);

  // Declare hz
  fpdtype_t hz;

  // Generate function hz
  % for j in range(nphase):
    hz += sin(${j}*ploc_t + hphs0[0][${j}] * 2 * ${np.pi});
  % endfor

  % for i in range(3):
    src[${i + 1}] += ${Amp} * hz;
  % endfor
</%pyfr:macro>

This code is working however the performance is not ideal. The reason is that it has to add up all Fourier modes considered by variable nphase which could be a few hundred. But phase is not updating every single step. In fact it often do several thousand steps. So question is that is it possible to cache hz in the backend? Or is there any better way to pass this variable?

Regards,
Zhenyang

Yes, it should not be difficult for a plugin to pre-compute these factors at each solution point and them pass them in as an extern to the kernel. If the performance is better depends on how efficiently you code things on the Python side.

You can look at the turbulent inflow plugin for a reference on how arrays can be created and updated.

Regards, Freddie.

But in this case, the variable I want to pass is pointwise. In the set_external function, spec is required as the input, which is broadcasting in the turbulent plugin. What should I choose for this input for my case?

Regards,
Zhenyang

If you just want to pass in a single number per solution point then a spec of in fpdtype_t will do the job.

Regards, Freddie.

1 Like