Csound Csound-dev Csound-tekno Search About

[Csnd] loops vs recursion.

Date2018-06-20 07:25
Fromthorin kerr
Subject[Csnd] loops vs recursion.
Quick question for the devs: 

When writing an UDO, is there any difference writing a loop (using while or until), vs. using opcode recursion? 

Thorin

Csound mailing list Csound@listserv.heanet.ie https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND Send bugs reports to https://github.com/csound/csound/issues Discussions of bugs and features can be posted here

Date2018-06-20 08:17
From"Jeanette C."
SubjectRe: [Csnd] loops vs recursion.
Jun 20 2018, thorin kerr has written:
...
> When writing an UDO, is there any difference writing a loop (using while or
> until), vs. using opcode recursion?
...
Hey hey Thorin,
I'm not a Csound dev, but... With recursion, you need more memory,
because every instance of the function (UDO) required during the
recursion will still be there, until the recursion is finished. So,
local variables will be there for every instance, and there is probably
something like a call-stack, where the function calls are stored in some
form.

A looop is - sort of - linear. You have a fixed number of variables and
only one instance of the UDO.

In terms of realtime I don't know. It would most likely depend on the
task you want to solve.

HTH.

Best wishes,

Jeanette

--------
  * Website: http://juliencoder.de - for summer is a state of sound
  * SoundCloud: https://soundcloud.com/jeanette_c
  * Youtube: https://www.youtube.com/channel/UCMS4rfGrTwz8W7jhC1Jnv7g
  * GitHub: https://github.com/jeanette-c
  * Twitter: https://twitter.com/jeanette_c_s

If there's nothing missing in my life
Then why do these tears come at night <3
(Britney Spears)

Csound mailing list
Csound@listserv.heanet.ie
https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND
Send bugs reports to
        https://github.com/csound/csound/issues
Discussions of bugs and features can be posted here

Date2018-06-20 09:06
FromVictor Lazzarini
SubjectRe: [Csnd] loops vs recursion.
The main difference between a loop and recursion is that loops do not instantiate
new opcode objects, just run the same object over and over again. UDOs that
are called recursively will have instantiated one object per recursive call, so they
are separate objects running simultaneously.

For example, take this code

while k1 < 10 do
 asig += oscili(ka, kf*k1)
 k1 += 1
od

it will NOT run a bank of 10 oscillators, but will run 1 oscillator sequentially
accumulating consecutive blocks of ksmps in the output asig.

Now, conversely, look a this code:

 opcode OscilBank,a,kkp
   ka,kf,ic xin
   asig oscili ka/10,kf*ic
   if ic < 10 then   
    asig += OscilBank(ka,kf,ic+1)
   endif
     xout asig
endop


Each UDO starts a separate instance of oscili and the 10-oscillator 
bank is summed up and placed in the output.

========================
Prof. Victor Lazzarini
Dean of Arts, Celtic Studies, and Philosophy,
Maynooth University,
Maynooth, Co Kildare, Ireland
Tel: 00 353 7086936
Fax: 00 353 1 7086952 

> On 20 Jun 2018, at 07:25, thorin kerr  wrote:
> 
> Quick question for the devs: 
> 
> When writing an UDO, is there any difference writing a loop (using while or until), vs. using opcode recursion? 
> 
> Thorin
> 
> Csound mailing list Csound@listserv.heanet.ie https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND Send bugs reports to https://github.com/csound/csound/issues Discussions of bugs and features can be posted here

Csound mailing list
Csound@listserv.heanet.ie
https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND
Send bugs reports to
        https://github.com/csound/csound/issues
Discussions of bugs and features can be posted here

Date2018-06-29 16:20
FromAaron Krister Johnson
SubjectRe: [Csnd] loops vs recursion.
I will always seek an iterative (loop) solution to a problem before a recursive one, when possible (and I can't think of an impossible iterative solution, only ones that are more elegantly expressed through recursion, perhaps).

The reason being, as others mentioned: memory limits will often bite you. The exception to this rule is when the platform offers proper tail-recursion, meaning that under-the-hood, your recursive solution gets transformed in such a way that it basically turns into a loop that doesn't consume the return stack on every call.

-AKJ


Aaron Krister Johnson
http://www.untwelve.org

On Wed, Jun 20, 2018 at 3:06 AM, Victor Lazzarini <Victor.Lazzarini@mu.ie> wrote:
The main difference between a loop and recursion is that loops do not instantiate
new opcode objects, just run the same object over and over again. UDOs that
are called recursively will have instantiated one object per recursive call, so they
are separate objects running simultaneously.

For example, take this code

while k1 < 10 do
 asig += oscili(ka, kf*k1)
 k1 += 1
od

it will NOT run a bank of 10 oscillators, but will run 1 oscillator sequentially
accumulating consecutive blocks of ksmps in the output asig.

Now, conversely, look a this code:

 opcode OscilBank,a,kkp
   ka,kf,ic xin
   asig oscili ka/10,kf*ic
   if ic < 10 then   
    asig += OscilBank(ka,kf,ic+1)
   endif
     xout asig
endop


Each UDO starts a separate instance of oscili and the 10-oscillator
bank is summed up and placed in the output.

========================
Prof. Victor Lazzarini
Dean of Arts, Celtic Studies, and Philosophy,
Maynooth University,
Maynooth, Co Kildare, Ireland
Tel: 00 353 7086936
Fax: 00 353 1 7086952

> On 20 Jun 2018, at 07:25, thorin kerr <thorin.kerr@gmail.com> wrote:
>
> Quick question for the devs:
>
> When writing an UDO, is there any difference writing a loop (using while or until), vs. using opcode recursion?
>
> Thorin
>
> Csound mailing list Csound@listserv.heanet.ie https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND Send bugs reports to https://github.com/csound/csound/issues Discussions of bugs and features can be posted here

Csound mailing list
Csound@listserv.heanet.ie
https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND
Send bugs reports to
        https://github.com/csound/csound/issues
Discussions of bugs and features can be posted here

Csound mailing list Csound@listserv.heanet.ie https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND Send bugs reports to https://github.com/csound/csound/issues Discussions of bugs and features can be posted here

Date2018-06-29 17:18
Fromjpff
SubjectRe: [Csnd] loops vs recursion.
The problem is the semantics of a loop are different to those f  recursion 
in csound; may not be a problem in more conventional languages though 
pesonally I prefer a recursive firm as it is usually easuer to reason 
about, prove acuracy etc.


On Fri, 29 Jun 2018, Aaron Krister Johnson wrote:

> I will always seek an iterative (loop) solution to a problem before a
> recursive one, when possible (and I can't think of an impossible iterative
> solution, only ones that are more elegantly expressed through recursion,
> perhaps).
> 
> The reason being, as others mentioned: memory limits will often bite you. The
> exception to this rule is when the platform offers proper tail-recursion,
> meaning that under-the-hood, your recursive solution gets transformed in such
> a way that it basically turns into a loop that doesn't consume the return
> stack on every call.
> -AKJ
> 
> 
> Aaron Krister Johnson
> http://www.untwelve.org
> 
> On Wed, Jun 20, 2018 at 3:06 AM, Victor Lazzarini 
> wrote:
>       The main difference between a loop and recursion is that loops do
>       not instantiate
>       new opcode objects, just run the same object over and over again.
>       UDOs that
>       are called recursively will have instantiated one object per
>       recursive call, so they
>       are separate objects running simultaneously.
>
>       For example, take this code
>
>       while k1 < 10 do
>        asig += oscili(ka, kf*k1)
>        k1 += 1
>       od
>
>       it will NOT run a bank of 10 oscillators, but will run 1
>       oscillator sequentially
>       accumulating consecutive blocks of ksmps in the output asig.
>
>       Now, conversely, look a this code:
>
>        opcode OscilBank,a,kkp
>          ka,kf,ic xin
>          asig oscili ka/10,kf*ic
>          if ic < 10 then   
>           asig += OscilBank(ka,kf,ic+1)
>          endif
>            xout asig
>       endop
> 
>
>       Each UDO starts a separate instance of oscili and the
>       10-oscillator
>       bank is summed up and placed in the output.
>
>       ========================
>       Prof. Victor Lazzarini
>       Dean of Arts, Celtic Studies, and Philosophy,
>       Maynooth University,
>       Maynooth, Co Kildare, Ireland
>       Tel: 00 353 7086936
>       Fax: 00 353 1 7086952
>
>       > On 20 Jun 2018, at 07:25, thorin kerr 
>       wrote:
>       >
>       > Quick question for the devs:
>       >
>       > When writing an UDO, is there any difference writing a loop
>       (using while or until), vs. using opcode recursion?
>       >
>       > Thorin
>       >
>       > Csound mailing list Csound@listserv.heanet.ie
>       https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND Send bugs reports
>       to https://github.com/csound/csound/issues Discussions of bugs and
>       features can be posted here
>
>       Csound mailing list
>       Csound@listserv.heanet.ie
>       https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND
>       Send bugs reports to
>               https://github.com/csound/csound/issues
>       Discussions of bugs and features can be posted here
> 
> 
> Csound mailing list Csound@listserv.heanet.ie
> https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND Send bugs reports to
> https://github.com/csound/csound/issues Discussions of bugs and features can
> be posted here
>

Csound mailing list
Csound@listserv.heanet.ie
https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND
Send bugs reports to
        https://github.com/csound/csound/issues
Discussions of bugs and features can be posted here

Date2018-06-29 17:27
FromVictor Lazzarini
SubjectRe: [Csnd] loops vs recursion.
Just to add to that, recursion in Csound is used for a particular reason, which is to instantiate
objects without having to write them down explicitly. This is impossible to do by any other
means, as objects are anonymous in the language. 

The question of memory usage is also a different one here than in other languages, as
most of its use in recursion is to do with having several objects instantiated. This would
probably not be too different if we had the means of using iteration for the same purpose
(which we don’t have). 
========================
Prof. Victor Lazzarini
Dean of Arts, Celtic Studies, and Philosophy,
Maynooth University,
Maynooth, Co Kildare, Ireland
Tel: 00 353 7086936
Fax: 00 353 1 7086952 

> On 29 Jun 2018, at 17:18, jpff  wrote:
> 
> The problem is the semantics of a loop are different to those f  recursion in csound; may not be a problem in more conventional languages though pesonally I prefer a recursive firm as it is usually easuer to reason about, prove acuracy etc.
> 
> 
> On Fri, 29 Jun 2018, Aaron Krister Johnson wrote:
> 
>> I will always seek an iterative (loop) solution to a problem before a
>> recursive one, when possible (and I can't think of an impossible iterative
>> solution, only ones that are more elegantly expressed through recursion,
>> perhaps).
>> The reason being, as others mentioned: memory limits will often bite you. The
>> exception to this rule is when the platform offers proper tail-recursion,
>> meaning that under-the-hood, your recursive solution gets transformed in such
>> a way that it basically turns into a loop that doesn't consume the return
>> stack on every call.
>> -AKJ
>> Aaron Krister Johnson
>> http://www.untwelve.org
>> On Wed, Jun 20, 2018 at 3:06 AM, Victor Lazzarini 
>> wrote:
>>      The main difference between a loop and recursion is that loops do
>>      not instantiate
>>      new opcode objects, just run the same object over and over again.
>>      UDOs that
>>      are called recursively will have instantiated one object per
>>      recursive call, so they
>>      are separate objects running simultaneously.
>> 
>>      For example, take this code
>> 
>>      while k1 < 10 do
>>       asig += oscili(ka, kf*k1)
>>       k1 += 1
>>      od
>> 
>>      it will NOT run a bank of 10 oscillators, but will run 1
>>      oscillator sequentially
>>      accumulating consecutive blocks of ksmps in the output asig.
>> 
>>      Now, conversely, look a this code:
>> 
>>       opcode OscilBank,a,kkp
>>         ka,kf,ic xin
>>         asig oscili ka/10,kf*ic
>>         if ic < 10 then   
>>          asig += OscilBank(ka,kf,ic+1)
>>         endif
>>           xout asig
>>      endop
>> 
>>      Each UDO starts a separate instance of oscili and the
>>      10-oscillator
>>      bank is summed up and placed in the output.
>> 
>>      ========================
>>      Prof. Victor Lazzarini
>>      Dean of Arts, Celtic Studies, and Philosophy,
>>      Maynooth University,
>>      Maynooth, Co Kildare, Ireland
>>      Tel: 00 353 7086936
>>      Fax: 00 353 1 7086952
>> 
>>      > On 20 Jun 2018, at 07:25, thorin kerr 
>>      wrote:
>>      >
>>      > Quick question for the devs:
>>      >
>>      > When writing an UDO, is there any difference writing a loop
>>      (using while or until), vs. using opcode recursion?
>>      >
>>      > Thorin
>>      >
>>      > Csound mailing list Csound@listserv.heanet.ie
>>      https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND Send bugs reports
>>      to https://github.com/csound/csound/issues Discussions of bugs and
>>      features can be posted here
>> 
>>      Csound mailing list
>>      Csound@listserv.heanet.ie
>>      https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND
>>      Send bugs reports to
>>              https://github.com/csound/csound/issues
>>      Discussions of bugs and features can be posted here
>> Csound mailing list Csound@listserv.heanet.ie
>> https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND Send bugs reports to
>> https://github.com/csound/csound/issues Discussions of bugs and features can
>> be posted here
>> 
> 
> Csound mailing list
> Csound@listserv.heanet.ie
> https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND
> Send bugs reports to
>       https://github.com/csound/csound/issues
> Discussions of bugs and features can be posted here


Csound mailing list
Csound@listserv.heanet.ie
https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND
Send bugs reports to
        https://github.com/csound/csound/issues
Discussions of bugs and features can be posted here

Date2018-06-29 17:53
FromAaron Krister Johnson
SubjectRe: [Csnd] loops vs recursion.
Ok, understood...I'm not sure I can connect the dots, based on not really knowing the implementation details in Csound as well as you do. That said, I would be curious about them, IOW, what you mean specifically as it relates to the implementation details of Csound.

My response was based on what I understand to be the (usual) case in the implementation of recursion vs. iteration in other languages.

Best,

AKJ
 

Aaron Krister Johnson
http://www.untwelve.org

On Fri, Jun 29, 2018 at 11:27 AM, Victor Lazzarini <Victor.Lazzarini@mu.ie> wrote:
Just to add to that, recursion in Csound is used for a particular reason, which is to instantiate
objects without having to write them down explicitly. This is impossible to do by any other
means, as objects are anonymous in the language.

The question of memory usage is also a different one here than in other languages, as
most of its use in recursion is to do with having several objects instantiated. This would
probably not be too different if we had the means of using iteration for the same purpose
(which we don’t have).
========================
Prof. Victor Lazzarini
Dean of Arts, Celtic Studies, and Philosophy,
Maynooth University,
Maynooth, Co Kildare, Ireland
Tel: 00 353 7086936
Fax: 00 353 1 7086952

> On 29 Jun 2018, at 17:18, jpff <jpff@codemist.co.uk> wrote:
>
> The problem is the semantics of a loop are different to those f  recursion in csound; may not be a problem in more conventional languages though pesonally I prefer a recursive firm as it is usually easuer to reason about, prove acuracy etc.
>
>
> On Fri, 29 Jun 2018, Aaron Krister Johnson wrote:
>
>> I will always seek an iterative (loop) solution to a problem before a
>> recursive one, when possible (and I can't think of an impossible iterative
>> solution, only ones that are more elegantly expressed through recursion,
>> perhaps).
>> The reason being, as others mentioned: memory limits will often bite you. The
>> exception to this rule is when the platform offers proper tail-recursion,
>> meaning that under-the-hood, your recursive solution gets transformed in such
>> a way that it basically turns into a loop that doesn't consume the return
>> stack on every call.
>> -AKJ
>> Aaron Krister Johnson
>> http://www.untwelve.org
>> On Wed, Jun 20, 2018 at 3:06 AM, Victor Lazzarini <Victor.Lazzarini@mu.ie>
>> wrote:
>>      The main difference between a loop and recursion is that loops do
>>      not instantiate
>>      new opcode objects, just run the same object over and over again.
>>      UDOs that
>>      are called recursively will have instantiated one object per
>>      recursive call, so they
>>      are separate objects running simultaneously.
>>
>>      For example, take this code
>>
>>      while k1 < 10 do
>>       asig += oscili(ka, kf*k1)
>>       k1 += 1
>>      od
>>
>>      it will NOT run a bank of 10 oscillators, but will run 1
>>      oscillator sequentially
>>      accumulating consecutive blocks of ksmps in the output asig.
>>
>>      Now, conversely, look a this code:
>>
>>       opcode OscilBank,a,kkp
>>         ka,kf,ic xin
>>         asig oscili ka/10,kf*ic
>>         if ic < 10 then   
>>          asig += OscilBank(ka,kf,ic+1)
>>         endif
>>           xout asig
>>      endop
>>
>>      Each UDO starts a separate instance of oscili and the
>>      10-oscillator
>>      bank is summed up and placed in the output.
>>
>>      ========================
>>      Prof. Victor Lazzarini
>>      Dean of Arts, Celtic Studies, and Philosophy,
>>      Maynooth University,
>>      Maynooth, Co Kildare, Ireland
>>      Tel: 00 353 7086936
>>      Fax: 00 353 1 7086952
>>
>>      > On 20 Jun 2018, at 07:25, thorin kerr <thorin.kerr@gmail.com>
>>      wrote:
>>      >
>>      > Quick question for the devs:
>>      >
>>      > When writing an UDO, is there any difference writing a loop
>>      (using while or until), vs. using opcode recursion?
>>      >
>>      > Thorin
>>      >
>>      > Csound mailing list Csound@listserv.heanet.ie
>>      https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND Send bugs reports
>>      to https://github.com/csound/csound/issues Discussions of bugs and
>>      features can be posted here
>>
>>      Csound mailing list
>>      Csound@listserv.heanet.ie
>>      https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND
>>      Send bugs reports to
>>              https://github.com/csound/csound/issues
>>      Discussions of bugs and features can be posted here
>> Csound mailing list Csound@listserv.heanet.ie
>> https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND Send bugs reports to
>> https://github.com/csound/csound/issues Discussions of bugs and features can
>> be posted here
>>
>
> Csound mailing list
> Csound@listserv.heanet.ie
> https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND
> Send bugs reports to
>       https://github.com/csound/csound/issues
> Discussions of bugs and features can be posted here


Csound mailing list
Csound@listserv.heanet.ie
https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND
Send bugs reports to
        https://github.com/csound/csound/issues
Discussions of bugs and features can be posted here

Csound mailing list Csound@listserv.heanet.ie https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND Send bugs reports to https://github.com/csound/csound/issues Discussions of bugs and features can be posted here

Date2018-06-29 19:53
FromMauro Giubileo
SubjectRe: [Csnd] loops vs recursion.

If I have understood well, if you create an UDO which call itself recursively to instantiate N objects (for example: applying N filters in cascade on an input signal), one problem is that the Csound engine will copy the input vector at each recursive call (and the output parameters as well, each time the recursive UDO returns), being that actually Csound UDOs don't permit passing parameters by reference. I'm worried that, in some scenarios, this could be a major drawback if you aim for good performance in real-time. In these cases, for better performance, maybe it's better to write the opcode calls N time in the code (but in this way, unfortunately, I don't think you can change the number of opcodes in cascade based on input parameters).

Regards,
Mauro


Il 2018-06-29 18:27 Victor Lazzarini ha scritto:

Just to add to that, recursion in Csound is used for a particular reason, which is to instantiate
objects without having to write them down explicitly. This is impossible to do by any other
means, as objects are anonymous in the language.

The question of memory usage is also a different one here than in other languages, as
most of its use in recursion is to do with having several objects instantiated. This would
probably not be too different if we had the means of using iteration for the same purpose
(which we don't have).
========================
Prof. Victor Lazzarini
Dean of Arts, Celtic Studies, and Philosophy,
Maynooth University,
Maynooth, Co Kildare, Ireland
Tel: 00 353 7086936
Fax: 00 353 1 7086952

On 29 Jun 2018, at 17:18, jpff <jpff@codemist.co.uk> wrote:

The problem is the semantics of a loop are different to those f  recursion in csound; may not be a problem in more conventional languages though pesonally I prefer a recursive firm as it is usually easuer to reason about, prove acuracy etc.


On Fri, 29 Jun 2018, Aaron Krister Johnson wrote:

I will always seek an iterative (loop) solution to a problem before a
recursive one, when possible (and I can't think of an impossible iterative
solution, only ones that are more elegantly expressed through recursion,
perhaps).
The reason being, as others mentioned: memory limits will often bite you. The
exception to this rule is when the platform offers proper tail-recursion,
meaning that under-the-hood, your recursive solution gets transformed in such
a way that it basically turns into a loop that doesn't consume the return
stack on every call.
-AKJ
Aaron Krister Johnson
http://www.untwelve.org
On Wed, Jun 20, 2018 at 3:06 AM, Victor Lazzarini <Victor.Lazzarini@mu.ie>
wrote:
     The main difference between a loop and recursion is that loops do
     not instantiate
     new opcode objects, just run the same object over and over again.
     UDOs that
     are called recursively will have instantiated one object per
     recursive call, so they
     are separate objects running simultaneously.

     For example, take this code

     while k1 < 10 do
      asig += oscili(ka, kf*k1)
      k1 += 1
     od

     it will NOT run a bank of 10 oscillators, but will run 1
     oscillator sequentially
     accumulating consecutive blocks of ksmps in the output asig.

     Now, conversely, look a this code:

      opcode OscilBank,a,kkp
        ka,kf,ic xin
        asig oscili ka/10,kf*ic
        if ic < 10 then   
         asig += OscilBank(ka,kf,ic+1)
        endif
          xout asig
     endop

     Each UDO starts a separate instance of oscili and the
     10-oscillator
     bank is summed up and placed in the output.

     ========================
     Prof. Victor Lazzarini
     Dean of Arts, Celtic Studies, and Philosophy,
     Maynooth University,
     Maynooth, Co Kildare, Ireland
     Tel: 00 353 7086936
     Fax: 00 353 1 7086952

     > On 20 Jun 2018, at 07:25, thorin kerr <thorin.kerr@gmail.com>
     wrote:
     >
     > Quick question for the devs:
     >
     > When writing an UDO, is there any difference writing a loop
     (using while or until), vs. using opcode recursion?
     >
     > Thorin
     >
     > Csound mailing list Csound@listserv.heanet.ie
     https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND Send bugs reports
     to https://github.com/csound/csound/issues Discussions of bugs and
     features can be posted here

     Csound mailing list
     Csound@listserv.heanet.ie
     https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND
     Send bugs reports to
             https://github.com/csound/csound/issues
     Discussions of bugs and features can be posted here
Csound mailing list Csound@listserv.heanet.ie
https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND Send bugs reports to
https://github.com/csound/csound/issues Discussions of bugs and features can
be posted here

Csound mailing list
Csound@listserv.heanet.ie
https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND
Send bugs reports to
      https://github.com/csound/csound/issues
Discussions of bugs and features can be posted here


Csound mailing list
Csound@listserv.heanet.ie
https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND
Send bugs reports to
        https://github.com/csound/csound/issues
Discussions of bugs and features can be posted here

Date2018-06-29 19:59
FromVictor Lazzarini
SubjectRe: [Csnd] loops vs recursion.
It works well and there is not a major drawback. There is some memory use, but performance has been shown to be very good.

Victor Lazzarini
Dean of Arts, Celtic Studies, and Philosophy
Maynooth University
Ireland

On 29 Jun 2018, at 19:53, Mauro Giubileo <mgiubileo@COMPUTERALTAFED.IT> wrote:

If I have understood well, if you create an UDO which call itself recursively to instantiate N objects (for example: applying N filters in cascade on an input signal), one problem is that the Csound engine will copy the input vector at each recursive call (and the output parameters as well, each time the recursive UDO returns), being that actually Csound UDOs don't permit passing parameters by reference. I'm worried that, in some scenarios, this could be a major drawback if you aim for good performance in real-time. In these cases, for better performance, maybe it's better to write the opcode calls N time in the code (but in this way, unfortunately, I don't think you can change the number of opcodes in cascade based on input parameters).

Regards,
Mauro


Il 2018-06-29 18:27 Victor Lazzarini ha scritto:

Just to add to that, recursion in Csound is used for a particular reason, which is to instantiate
objects without having to write them down explicitly. This is impossible to do by any other
means, as objects are anonymous in the language.

The question of memory usage is also a different one here than in other languages, as
most of its use in recursion is to do with having several objects instantiated. This would
probably not be too different if we had the means of using iteration for the same purpose
(which we don't have).
========================
Prof. Victor Lazzarini
Dean of Arts, Celtic Studies, and Philosophy,
Maynooth University,
Maynooth, Co Kildare, Ireland
Tel: 00 353 7086936
Fax: 00 353 1 7086952

On 29 Jun 2018, at 17:18, jpff <jpff@codemist.co.uk> wrote:

The problem is the semantics of a loop are different to those f  recursion in csound; may not be a problem in more conventional languages though pesonally I prefer a recursive firm as it is usually easuer to reason about, prove acuracy etc.


On Fri, 29 Jun 2018, Aaron Krister Johnson wrote:

I will always seek an iterative (loop) solution to a problem before a
recursive one, when possible (and I can't think of an impossible iterative
solution, only ones that are more elegantly expressed through recursion,
perhaps).
The reason being, as others mentioned: memory limits will often bite you. The
exception to this rule is when the platform offers proper tail-recursion,
meaning that under-the-hood, your recursive solution gets transformed in such
a way that it basically turns into a loop that doesn't consume the return
stack on every call.
-AKJ
Aaron Krister Johnson
http://www.untwelve.org
On Wed, Jun 20, 2018 at 3:06 AM, Victor Lazzarini <Victor.Lazzarini@mu.ie>
wrote:
     The main difference between a loop and recursion is that loops do
     not instantiate
     new opcode objects, just run the same object over and over again.
     UDOs that
     are called recursively will have instantiated one object per
     recursive call, so they
     are separate objects running simultaneously.

     For example, take this code

     while k1 < 10 do
      asig += oscili(ka, kf*k1)
      k1 += 1
     od

     it will NOT run a bank of 10 oscillators, but will run 1
     oscillator sequentially
     accumulating consecutive blocks of ksmps in the output asig.

     Now, conversely, look a this code:

      opcode OscilBank,a,kkp
        ka,kf,ic xin
        asig oscili ka/10,kf*ic
        if ic < 10 then   
         asig += OscilBank(ka,kf,ic+1)
        endif
          xout asig
     endop

     Each UDO starts a separate instance of oscili and the
     10-oscillator
     bank is summed up and placed in the output.

     ========================
     Prof. Victor Lazzarini
     Dean of Arts, Celtic Studies, and Philosophy,
     Maynooth University,
     Maynooth, Co Kildare, Ireland
     Tel: 00 353 7086936
     Fax: 00 353 1 7086952

     > On 20 Jun 2018, at 07:25, thorin kerr <thorin.kerr@gmail.com>
     wrote:
     >
     > Quick question for the devs:
     >
     > When writing an UDO, is there any difference writing a loop
     (using while or until), vs. using opcode recursion?
     >
     > Thorin
     >
     > Csound mailing list Csound@listserv.heanet.ie
     https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND Send bugs reports
     to https://github.com/csound/csound/issues Discussions of bugs and
     features can be posted here

     Csound mailing list
     Csound@listserv.heanet.ie
     https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND
     Send bugs reports to
             https://github.com/csound/csound/issues
     Discussions of bugs and features can be posted here
Csound mailing list Csound@listserv.heanet.ie
https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND Send bugs reports to
https://github.com/csound/csound/issues Discussions of bugs and features can
be posted here

Csound mailing list
Csound@listserv.heanet.ie
https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND
Send bugs reports to
      https://github.com/csound/csound/issues
Discussions of bugs and features can be posted here


Csound mailing list
Csound@listserv.heanet.ie
https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND
Send bugs reports to
        https://github.com/csound/csound/issues
Discussions of bugs and features can be posted here
Csound mailing list Csound@listserv.heanet.ie https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND Send bugs reports to https://github.com/csound/csound/issues Discussions of bugs and features can be posted here

Date2018-06-29 20:03
Fromjpff
SubjectRe: [Csnd] loops vs recursion.
#if you really must use an unwrapped loop, then give more istances than 
you need (ie a aximum) and leave early.  Seems coplex when the 
recursion works so well

On Fri, 29 Jun 2018, Victor Lazzarini wrote:

> It works well and there is not a major drawback. There is some memory use, but
> performance has been shown to be very good.
> 
> Victor Lazzarini Dean of Arts, Celtic Studies, and Philosophy
> Maynooth University
> Ireland
> 
> On 29 Jun 2018, at 19:53, Mauro Giubileo  wrote:
>
>       If I have understood well, if you create an UDO which call itself
>       recursively to instantiate N objects (for example: applying N
>       filters in cascade on an input signal), one problem is that the
>       Csound engine will copy the input vector at each recursive call
>       (and the output parameters as well, each time the recursive UDO
>       returns), being that actually Csound UDOs don't permit passing
>       parameters by reference. I'm worried that, in some scenarios, this
>       could be a major drawback if you aim for good performance in
>       real-time. In these cases, for better performance, maybe it's
>       better to write the opcode calls N time in the code (but in this
>       way, unfortunately, I don't think you can change the number of
>       opcodes in cascade based on input parameters).
>
>       Regards,
>       Mauro
> 
>
>       Il 2018-06-29 18:27 Victor Lazzarini ha scritto:
>
>             Just to add to that, recursion in Csound is used for a
>             particular reason, which is to instantiate
>             objects without having to write them down explicitly.
>             This is impossible to do by any other
>             means, as objects are anonymous in the language.
>
>             The question of memory usage is also a different one
>             here than in other languages, as
>             most of its use in recursion is to do with having
>             several objects instantiated. This would
>             probably not be too different if we had the means of
>             using iteration for the same purpose
>             (which we don't have).
>             ========================
>             Prof. Victor Lazzarini
>             Dean of Arts, Celtic Studies, and Philosophy,
>             Maynooth University,
>             Maynooth, Co Kildare, Ireland
>             Tel: 00 353 7086936
>             Fax: 00 353 1 7086952
>
>                   On 29 Jun 2018, at 17:18, jpff
>                    wrote:
>
>                   The problem is the semantics of a loop are
>                   different to those f  recursion in csound;
>                   may not be a problem in more conventional
>                   languages though pesonally I prefer a
>                   recursive firm as it is usually easuer to
>                   reason about, prove acuracy etc.
> 
>
>                   On Fri, 29 Jun 2018, Aaron Krister Johnson
>                   wrote:
>
>                         I will always seek an
>                         iterative (loop) solution to a
>                         problem before a
>                         recursive one, when possible
>                         (and I can't think of an
>                         impossible iterative
>                         solution, only ones that are
>                         more elegantly expressed
>                         through recursion,
>                         perhaps).
>                         The reason being, as others
>                         mentioned: memory limits will
>                         often bite you. The
>                         exception to this rule is when
>                         the platform offers proper
>                         tail-recursion,
>                         meaning that under-the-hood,
>                         your recursive solution gets
>                         transformed in such
>                         a way that it basically turns
>                         into a loop that doesn't
>                         consume the return
>                         stack on every call.
>                         -AKJ
>                         Aaron Krister Johnson
>                         http://www.untwelve.org
>                         On Wed, Jun 20, 2018 at 3:06
>                         AM, Victor Lazzarini
>                         
>                         wrote:
>                              The main difference
>                         between a loop and recursion
>                         is that loops do
>                              not instantiate
>                              new opcode objects, just
>                         run the same object over and
>                         over again.
>                              UDOs that
>                              are called recursively
>                         will have instantiated one
>                         object per
>                              recursive call, so they
>                              are separate objects
>                         running simultaneously.
>
>                              For example, take this
>                         code
>
>                              while k1 < 10 do
>                               asig += oscili(ka,
>                         kf*k1)
>                               k1 += 1
>                              od
>
>                              it will NOT run a bank of
>                         10 oscillators, but will run 1
>                              oscillator sequentially
>                              accumulating consecutive
>                         blocks of ksmps in the output
>                         asig.
>
>                              Now, conversely, look a
>                         this code:
>
>                               opcode OscilBank,a,kkp
>                                 ka,kf,ic xin
>                                 asig oscili
>                         ka/10,kf*ic
>                                 if ic < 10 then   
>                                  asig +=
>                         OscilBank(ka,kf,ic+1)
>                                 endif
>                                   xout asig
>                              endop
>
>                              Each UDO starts a
>                         separate instance of oscili
>                         and the
>                              10-oscillator
>                              bank is summed up and
>                         placed in the output.
>
>                              ========================
>                              Prof. Victor Lazzarini
>                              Dean of Arts, Celtic
>                         Studies, and Philosophy,
>                              Maynooth University,
>                              Maynooth, Co Kildare,
>                         Ireland
>                              Tel: 00 353 7086936
>                              Fax: 00 353 1 7086952
>
>                              > On 20 Jun 2018, at
>                         07:25, thorin kerr
>                         
>                              wrote:
>                              >
>                              > Quick question for the
>                         devs:
>                              >
>                              > When writing an UDO, is
>                         there any difference writing a
>                         loop
>                              (using while or until),
>                         vs. using opcode recursion?
>                              >
>                              > Thorin
>                              >
>                              > Csound mailing list
>                         Csound@listserv.heanet.ie
>                              https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND
>                         Send bugs reports
>                              to
>                         https://github.com/csound/csound/issues
>                         Discussions of bugs and
>                              features can be posted
>                         here
>
>                              Csound mailing list
>                              Csound@listserv.heanet.ie
>                              https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND
>                              Send bugs reports to
>                                      https://github.com/csound/csound/issues
>                              Discussions of bugs and
>                         features can be posted here
>                         Csound mailing list
>                         Csound@listserv.heanet.ie
>                         https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND
>                         Send bugs reports to
>                         https://github.com/csound/csound/issues
>                         Discussions of bugs and
>                         features can
>                         be posted here
> 
>
>                   Csound mailing list
>                   Csound@listserv.heanet.ie
>                   https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND
>                   Send bugs reports to
>                         https://github.com/csound/csound/issues
>                   Discussions of bugs and features can be
>                   posted here
> 
> 
>
>             Csound mailing list
>             Csound@listserv.heanet.ie
>             https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND
>             Send bugs reports to
>                     https://github.com/csound/csound/issues
>             Discussions of bugs and features can be posted here
> 
> Csound mailing list Csound@listserv.heanet.ie
> https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND Send bugs reports to
> https://github.com/csound/csound/issues Discussions of bugs and features
> can be posted here
> 
> Csound mailing list Csound@listserv.heanet.ie
> https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND Send bugs reports to
> https://github.com/csound/csound/issues Discussions of bugs and features can
> be posted here
>

Csound mailing list
Csound@listserv.heanet.ie
https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND
Send bugs reports to
        https://github.com/csound/csound/issues
Discussions of bugs and features can be posted here

Date2018-06-30 17:32
FromMauro Giubileo
SubjectRe: [Csnd] loops vs recursion.

You are right, if I have a limit on the max number of operations in cascade, I can do something like that:

(iN is a parameter for the number of opcodes you have to cascade):

if (iN > 0) then
 aX some_native_opcode aX, parameters...
 if (iN > 1) then
  aX some_native_opcode aX, parameters...
  if (iN > 2) then
   aX some_native_opcode aX, parameters...
   if (iN > 3) then
    aX some_native_opcode aX, parameters...
     ...
      ...
       if (iN > (MAX_CASCADE - 1)) then
        aX some_native_opcode aX, parameters...
       endif
      ...
     ...

   endif
  endif
 endif
endif

That is horrible, from a software engineering point of view, but I think it should be faster than a recursion with parameters passed by copy (instead of passing by reference), expecially for big ksmps (because big ksmps increases the number of array elements to copy at each recursive call, for the input a-rate signal). Of course, if the actual recursion performances were sufficient for my application, I would definitely choose the recursive version, because it's smaller, more flexible and easier to maintain.

Best Regards,
Mauro


Il 2018-06-29 21:03 jpff ha scritto:

#if you really must use an unwrapped loop, then give more istances than you need (ie a aximum) and leave early.  Seems coplex when the recursion works so well

On Fri, 29 Jun 2018, Victor Lazzarini wrote:

It works well and there is not a major drawback. There is some memory use, but
performance has been shown to be very good.

Victor Lazzarini Dean of Arts, Celtic Studies, and Philosophy
Maynooth University
Ireland

On 29 Jun 2018, at 19:53, Mauro Giubileo <mgiubileo@COMPUTERALTAFED.IT> wrote:

      If I have understood well, if you create an UDO which call itself
      recursively to instantiate N objects (for example: applying N
      filters in cascade on an input signal), one problem is that the
      Csound engine will copy the input vector at each recursive call
      (and the output parameters as well, each time the recursive UDO
      returns), being that actually Csound UDOs don't permit passing
      parameters by reference. I'm worried that, in some scenarios, this
      could be a major drawback if you aim for good performance in
      real-time. In these cases, for better performance, maybe it's
      better to write the opcode calls N time in the code (but in this
      way, unfortunately, I don't think you can change the number of
      opcodes in cascade based on input parameters).

      Regards,
      Mauro


      Il 2018-06-29 18:27 Victor Lazzarini ha scritto:

            Just to add to that, recursion in Csound is used for a
            particular reason, which is to instantiate
            objects without having to write them down explicitly.
            This is impossible to do by any other
            means, as objects are anonymous in the language.

            The question of memory usage is also a different one
            here than in other languages, as
            most of its use in recursion is to do with having
            several objects instantiated. This would
            probably not be too different if we had the means of
            using iteration for the same purpose
            (which we don't have).
            ========================
            Prof. Victor Lazzarini
            Dean of Arts, Celtic Studies, and Philosophy,
            Maynooth University,
            Maynooth, Co Kildare, Ireland
            Tel: 00 353 7086936
            Fax: 00 353 1 7086952

                  On 29 Jun 2018, at 17:18, jpff
                  <jpff@codemist.co.uk> wrote:

                  The problem is the semantics of a loop are
                  different to those f  recursion in csound;
                  may not be a problem in more conventional
                  languages though pesonally I prefer a
                  recursive firm as it is usually easuer to
                  reason about, prove acuracy etc.


                  On Fri, 29 Jun 2018, Aaron Krister Johnson
                  wrote:

                        I will always seek an
                        iterative (loop) solution to a
                        problem before a
                        recursive one, when possible
                        (and I can't think of an
                        impossible iterative
                        solution, only ones that are
                        more elegantly expressed
                        through recursion,
                        perhaps).
                        The reason being, as others
                        mentioned: memory limits will
                        often bite you. The
                        exception to this rule is when
                        the platform offers proper
                        tail-recursion,
                        meaning that under-the-hood,
                        your recursive solution gets
                        transformed in such
                        a way that it basically turns
                        into a loop that doesn't
                        consume the return
                        stack on every call.
                        -AKJ
                        Aaron Krister Johnson
                        http://www.untwelve.org
                        On Wed, Jun 20, 2018 at 3:06
                        AM, Victor Lazzarini
                        <Victor.Lazzarini@mu.ie>
                        wrote:
                             The main difference
                        between a loop and recursion
                        is that loops do
                             not instantiate
                             new opcode objects, just
                        run the same object over and
                        over again.
                             UDOs that
                             are called recursively
                        will have instantiated one
                        object per
                             recursive call, so they
                             are separate objects
                        running simultaneously.

                             For example, take this
                        code

                             while k1 < 10 do
                              asig += oscili(ka,
                        kf*k1)
                              k1 += 1
                             od

                             it will NOT run a bank of
                        10 oscillators, but will run 1
                             oscillator sequentially
                             accumulating consecutive
                        blocks of ksmps in the output
                        asig.

                             Now, conversely, look a
                        this code:

                              opcode OscilBank,a,kkp
                                ka,kf,ic xin
                                asig oscili
                        ka/10,kf*ic
                                if ic < 10 then   
                                 asig +=
                        OscilBank(ka,kf,ic+1)
                                endif
                                  xout asig
                             endop

                             Each UDO starts a
                        separate instance of oscili
                        and the
                             10-oscillator
                             bank is summed up and
                        placed in the output.

                             ========================
                             Prof. Victor Lazzarini
                             Dean of Arts, Celtic
                        Studies, and Philosophy,
                             Maynooth University,
                             Maynooth, Co Kildare,
                        Ireland
                             Tel: 00 353 7086936
                             Fax: 00 353 1 7086952

                             > On 20 Jun 2018, at
                        07:25, thorin kerr
                        <thorin.kerr@gmail.com>
                             wrote:
                             >
                             > Quick question for the
                        devs:
                             >
                             > When writing an UDO, is
                        there any difference writing a
                        loop
                             (using while or until),
                        vs. using opcode recursion?
                             >
                             > Thorin
                             >
                             > Csound mailing list
                        Csound@listserv.heanet.ie
                             https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND
                        Send bugs reports
                             to
                        https://github.com/csound/csound/issues
                        Discussions of bugs and
                             features can be posted
                        here

                             Csound mailing list
                             Csound@listserv.heanet.ie
                             https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND
                             Send bugs reports to
                                     https://github.com/csound/csound/issues
                             Discussions of bugs and
                        features can be posted here
                        Csound mailing list
                        Csound@listserv.heanet.ie
                        https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND
                        Send bugs reports to
                        https://github.com/csound/csound/issues
                        Discussions of bugs and
                        features can
                        be posted here


                  Csound mailing list
                  Csound@listserv.heanet.ie
                  https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND
                  Send bugs reports to
                        https://github.com/csound/csound/issues
                  Discussions of bugs and features can be
                  posted here



            Csound mailing list
            Csound@listserv.heanet.ie
            https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND
            Send bugs reports to
                    https://github.com/csound/csound/issues
            Discussions of bugs and features can be posted here

Csound mailing list Csound@listserv.heanet.ie
https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND Send bugs reports to
https://github.com/csound/csound/issues Discussions of bugs and features
can be posted here

Csound mailing list Csound@listserv.heanet.ie
https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND Send bugs reports to
https://github.com/csound/csound/issues Discussions of bugs and features can
be posted here

Csound mailing list
Csound@listserv.heanet.ie
https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND
Send bugs reports to
       https://github.com/csound/csound/issues
Discussions of bugs and features can be posted here