[Csnd] loops vs recursion.

classic Classic list List threaded Threaded
11 messages Options
Reply | Threaded
Open this post in threaded view
|

[Csnd] loops vs recursion.

thorin kerr
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 [hidden email] 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
Reply | Threaded
Open this post in threaded view
|

Re: [Csnd] loops vs recursion.

Jeanette C.
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
[hidden email]
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
Reply | Threaded
Open this post in threaded view
|

Re: [Csnd] loops vs recursion.

Victor Lazzarini-2
In reply to this post by thorin kerr
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 <[hidden email]> 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 [hidden email] 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
[hidden email]
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
Reply | Threaded
Open this post in threaded view
|

Re: [Csnd] loops vs recursion.

akjmicro
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 <[hidden email]> 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 <[hidden email]> 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 [hidden email] 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
[hidden email]
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 [hidden email] 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
Reply | Threaded
Open this post in threaded view
|

Re: [Csnd] loops vs recursion.

jpff
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 <[hidden email]>
> 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 <[hidden email]>
>       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 [hidden email]
>       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
>       [hidden email]
>       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 [hidden email]
> 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
[hidden email]
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
Reply | Threaded
Open this post in threaded view
|

Re: [Csnd] loops vs recursion.

Victor Lazzarini-2
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 <[hidden email]> 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 <[hidden email]>
>> 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 <[hidden email]>
>>      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 [hidden email]
>>      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
>>      [hidden email]
>>      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 [hidden email]
>> 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
> [hidden email]
> 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
[hidden email]
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
Reply | Threaded
Open this post in threaded view
|

Re: [Csnd] loops vs recursion.

akjmicro
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 <[hidden email]> 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 <[hidden email]> 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 <[hidden email]>
>> 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 <[hidden email]>
>>      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 [hidden email]
>>      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
>>      [hidden email]
>>      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 [hidden email]
>> 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
> [hidden email]
> 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
[hidden email]
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 [hidden email] 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
Reply | Threaded
Open this post in threaded view
|

Re: [Csnd] loops vs recursion.

Mauro Giubileo
In reply to this post by Victor Lazzarini-2

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 <[hidden email]> 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 <[hidden email]>
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 <[hidden email]>
     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 [hidden email]
     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
     [hidden email]
     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 [hidden email]
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
[hidden email]
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
[hidden email]
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 [hidden email] 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
Reply | Threaded
Open this post in threaded view
|

Re: [Csnd] loops vs recursion.

Victor Lazzarini-2
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 <[hidden email]> 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 <[hidden email]> 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 <[hidden email]>
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 <[hidden email]>
     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 [hidden email]
     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
     [hidden email]
     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 [hidden email]
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
[hidden email]
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
[hidden email]
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 [hidden email] 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 [hidden email] 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
Reply | Threaded
Open this post in threaded view
|

Re: [Csnd] loops vs recursion.

jpff
#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 <[hidden email]> 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
>                   <[hidden email]> 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
>                         <[hidden email]>
>                         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
>                         <[hidden email]>
>                              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
>                         [hidden email]
>                              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
>                              [hidden email]
>                              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
>                         [hidden email]
>                         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
>                   [hidden email]
>                   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
>             [hidden email]
>             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 [hidden email]
> 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 [hidden email]
> 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
[hidden email]
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
Reply | Threaded
Open this post in threaded view
|

Re: [Csnd] loops vs recursion.

Mauro Giubileo

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 <[hidden email]> 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
                  <[hidden email]> 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
                        <[hidden email]>
                        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
                        <[hidden email]>
                             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
                        [hidden email]
                             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
                             [hidden email]
                             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
                        [hidden email]
                        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
                  [hidden email]
                  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
            [hidden email]
            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 [hidden email]
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 [hidden email]
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
[hidden email]
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 [hidden email] 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