[Csnd] genarray/lenarray strange behaviour
Date | 2018-09-25 18:05 |
From | Mauro Giubileo |
Subject | [Csnd] genarray/lenarray strange behaviour |
Hi, I have some troubles with genarray / lenarray opcodes. Let's suppose we have the following code: iStart = 1 After the FIRST iteration of the while, iArray[] should have: [1 3 5 7 9], so lenarray(iArray) should be 5. Instead, strangely, the actual output from lenarray is 5 for both the above cases. Now, if In the above code, if I replace 'genarray' with a call to the following UDO that should do the same thing of the native genarray opcode: opcode genArray, i[], iip then Csound returns correctly 5 and 4, as I would expect. Is this a bug? Regards, |
Date | 2018-09-25 18:35 |
From | Mauro Giubileo |
Subject | Re: [Csnd] genarray/lenarray strange behaviour |
Sorry, a little errata corrige on my previous mail: [...] Il 2018-09-25 19:05 Mauro Giubileo ha scritto:
|
Date | 2018-09-25 19:48 |
From | jpff |
Subject | Re: [Csnd] genarray/lenarray strange behaviour |
I suspect this is by design. The second assignment to iArray checks that the array is big enough. I does not shorten it but may lengthen. Now to argue about what is right! On Tue, 25 Sep 2018, Mauro Giubileo wrote: > > Sorry, a little errata corrige on my previous mail: > > [...] > After the FIRST iteration of the while, iArray[] should have: [1 3 5 7 9], so > lenarray(iArray) should be 5. > After the SECOND iteration of the while, iArray[] should have: [2 4 6 8], so > lenarray(iArray) should be 4. > [...] > > > > Il 2018-09-25 19:05 Mauro Giubileo ha scritto: > > Hi, I have some troubles with genarray / lenarray opcodes. > > Let's suppose we have the following code: > > iStart = 1 > iEnd = 9 > > iCnt = 0 > while (iCnt < 2) do > iArray[] genarray iStart + iCnt, iEnd - iCnt, 2 > print lenarray(iArray) > iCnt += 1 > od > > After the FIRST iteration of the while, iArray[] should have: [1 3 > 5 7 9], so lenarray(iArray) should be 5. > After the SECOND iteration of the while, iArray[] should have: [1 > 3 5 7], so lenarray(iArray) should be 4. > > Instead, strangely, the actual output from lenarray is 5 for both > the above cases. > > Now, if In the above code, if I replace 'genarray' with a call to > the following UDO that should do the same thing of the native > genarray opcode: > > opcode genArray, i[], iip > iStart, \ > iEnd, \ > iStep xin ; default = 1 > > iSize = floor((iEnd - iStart) / iStep + 1) > iArr[] init iSize > > indx = 0 > while (indx < iSize) do > iArr[indx] = iStart > iStart += iStep > indx += 1 > od > > xout iArr > endop > > then Csound returns correctly 5 and 4, as I would expect. > > Is this a bug? > > > Regards, > Mauro > > > 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 |
Date | 2018-09-26 02:29 |
From | Mauro Giubileo |
Subject | Re: [Csnd] genarray/lenarray strange behaviour |
Looking at the C code, I think this behaviour is an attempt to avoid the release and reallocation of memory when you call the same genarray instruction many times in a loop, unless the new array size is bigger than the previous one. I could agree with this optimization, if it weren't for two problems it involves:
Csound engine could make useless (and sometimes dangerous, like a divide by zero) operations because it will process more elements than the last genarray should have generated. For example, let's suppose the last genarray execution should generate an array of 5 elements, but some previous execution of the same genarray opcode returned an array of 100 elements, so the above instruction will process in vain 95 "ghost" elements!! They are ghosts because the user doesn't know they exist but Csound process them, being that the real length of iArray is not 5... It is its biggest allocation until now: 100 in our example! This is happened in a real situation in a Csound program on which I'm working, and for many days I could not understand what was the problem and why sometimes it crashed... Then I looked at the C code of the array opcodes and I understood the problem... Anyway I think it should be safe and nice for all, if the genarray opcode could have an optional fourth parameter to make the user choice the desired behaviour. For example: iArray[] genarray iStart, iEnd, iStep, iFreeAndReallocMem iFreeAndReallocMem == 0 (default?) => the actual (IMHO misleading) behaviour; Il 2018-09-25 20:48 jpff ha scritto:
|
Date | 2018-09-28 21:05 |
From | jpff |
Subject | Re: [Csnd] genarray/lenarray strange behaviour |
Code now behaves as you expect. Still can optiise it but it is crrect. 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 |
Date | 2018-09-29 08:28 |
From | Mauro Giubileo |
Subject | Re: [Csnd] genarray/lenarray strange behaviour |
Ok, thank you John, genarray and slicearray now work as expected, but the same problem still remains on other math opcodes that take array too as input, like pow, sqrt, cos, cosh, sin, sinh, sininv, tan, tanh, taninv, exp, log, log10, frac, floor, ceil, abs, int. I don't know if there are others, but generally speaking, I found this behaviour in all those opcodes modded to work with array types too... Il 2018-09-28 22:05 jpff ha scritto:
|
Date | 2018-09-29 12:16 |
From | John ff |
Subject | Re: [Csnd] genarray/lenarray strange behaviour |
Maybe I missed a place to change. I found 7 and changed them. Will look for the mathematical functions, but that is in C++ Sent from TypeApp On Sep 29, 2018, 08:30, at 08:30, Mauro Giubileo |
Date | 2018-09-29 14:49 |
From | Victor Lazzarini |
Subject | Re: [Csnd] genarray/lenarray strange behaviour |
We can’t change the behaviour of functions because of backwards compatibility. Besides that, I don’t agree that there is a problem with the design. ======================== 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 Sep 2018, at 12:16, John ff |
Date | 2018-09-29 16:34 |
From | jpff |
Subject | Re: [Csnd] genarray/lenarray strange behaviour |
I disagree. The old behaviour was odd and unexpected. The revised code creates arrays of the expected size; only the plugin.h code is out of step and as it stands can lead to a crash. The function template |
Date | 2018-09-29 18:13 |
From | Victor Lazzarini |
Subject | Re: [Csnd] genarray/lenarray strange behaviour |
I am not sure what it is supposed to do now then. I never had any issues with the code as it was and these changes now broke plugin.h, so it's a problem. Victor Lazzarini Dean of Arts, Celtic Studies, and Philosophy Maynooth University Ireland > On 29 Sep 2018, at 16:35, jpff |
Date | 2018-09-29 18:25 |
From | jpff |
Subject | Re: [Csnd] genarray/lenarray strange behaviour |
I have a revised plugin.h hat builds OK. Not found how to test yet. On Sat, 29 Sep 2018, Victor Lazzarini wrote: > I am not sure what it is supposed to do now then. > > I never had any issues with the code as it was and these changes now broke plugin.h, > so it's a problem. > > Victor Lazzarini > Dean of Arts, Celtic Studies, and Philosophy > Maynooth University > Ireland > >> On 29 Sep 2018, at 16:35, jpff |
Date | 2018-09-29 18:50 |
From | Victor Lazzarini |
Subject | Re: [Csnd] genarray/lenarray strange behaviour |
you can try with the mathematical functions and arrays. Victor Lazzarini Dean of Arts, Celtic Studies, and Philosophy Maynooth University Ireland > On 29 Sep 2018, at 18:25, jpff |
Date | 2018-09-29 19:10 |
From | Mauro Giubileo |
Subject | Re: [Csnd] genarray/lenarray strange behaviour |
Hi Victor, in a previous message (https://listserv.heanet.ie/cgi-bin/wa?A2=ind1809&L=CSOUND&O=D&P=174010) I described how that design could be cause of insidious bugs in your Csound programs, when you generate/process arrays inside a loop. Best Regards, Il 2018-09-29 15:49 Victor Lazzarini ha scritto:
|
Date | 2018-09-29 19:27 |
From | Victor Lazzarini |
Subject | Re: [Csnd] genarray/lenarray strange behaviour |
I saw the message, still think that is a user error and the design is correct.
But I seem to have been overruled on this one.
Victor Lazzarini
Dean of Arts, Celtic Studies, and Philosophy
Maynooth University
Ireland
|
Date | 2018-09-30 01:35 |
From | Mauro Giubileo |
Subject | Re: [Csnd] genarray/lenarray strange behaviour |
Hi Victor, maybe I'm missing something, but I don't see how that could be an user error. I had to read the source code file "arrays.c" to figure out why my Csound program didn't work as I expected... I think that a Csound user should not worry about how an opcode is internally implemented to make its Csound scripts work correctly. If I write something like: iArray[] genarray 1, 10 * i1, 1 I think anyone would expect that soon after that statement the size of iArray should be exactly (10 * i1) wherever that statement is placed inside a Csound program. And be aware that I'm not talking about the "true" size allocated internally by the Csound engine for that array (that could very well be much bigger the size the user defined for that array, for optimization purposes), but the size the Csound user expects it should have for its algorithms to work properly from a conceptual point of view. Let's take another example. If you write this: i1 = 5 In the above example, being that the reference manual says nothing about this matter, I think anyone would assume the last iArray size should be 10*1=10 elements, but before John's changes it would have been 50. That was very misleading. Moreover, the division in the above code would be done unnecessarily 50 times at each iteration, when it should be done first 50 times, then 40 times, 30 times, 20 times and lastly 10 times... Best Regards, Il 2018-09-29 20:27 Victor Lazzarini ha scritto: I saw the message, still think that is a user error and the design is correct. |
Date | 2018-09-30 11:29 |
From | Victor Lazzarini |
Subject | Re: [Csnd] genarray/lenarray strange behaviour |
Placing genarray in a loop is the user error, in the similar way that placing oscillators etc in loops doesn't lead to expected results.
But as I said before, I am in a minority here. The only thing to make sure is that any changes do not make the design backwards
incompatible.
Victor Lazzarini
Dean of Arts, Celtic Studies, and Philosophy
Maynooth University
Ireland
|
Date | 2018-09-30 12:34 |
From | Mauro Giubileo |
Subject | Re: [Csnd] genarray/lenarray strange behaviour |
Hi Victor, I understand your point of view, but in my example I generate some tables only at i-time (for example, you want to precalculate some things inside a global array, before the score starts) and to me that's very different from oscillators behaviour, being that oscillators need to returns values only at control-rate. Maybe I'm wrong here, but I think that i-time statements, being that by definition they have to 'initialize' things, they have to reset their internal states each time I execute them, even if I run them inside a loop. It's like I did a 'reinit' before each i-time statement, but being that I'm already in a init-time execution, I don't need to do that (and, in fact, reinit works only at control-rate, otherwise it would be permitted at i-time too). Of course, all of this is just my opinion. Best regards, Il 2018-09-30 12:29 Victor Lazzarini ha scritto:
|