The variable does not retain the changed value.

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

The variable does not retain the changed value.

bvox123
How to assign a value to a variable?

dprovv1 = 0.0;
dprovv2 = 0.0;
for (i = [0 : 1 : 10]) {
if (dprovv2 == 0) {
echo (dprovv1);
dprovv1 = (dprovv1 + 1);
}
}

The dprovv1 variable seems not to keep the changed value.
Thank you.


I do not speak English. I apologize for my english.
Reply | Threaded
Open this post in threaded view
|

Re: The variable does not retain the changed value.

nophead
There are no variables in OpenScad, only named constants that have only one value for the whole of their scope.

In your example you would need to do something like dprovv3 = dprovv1 + i; In general something that changes during a loop will need to be a function of the loop variable.



On 27 August 2016 at 10:45, bvox123 <[hidden email]> wrote:
How to assign a value to a variable?

dprovv1 = 0.0;
dprovv2 = 0.0;
for (i = [0 : 1 : 10]) {
if (dprovv2 == 0) {
echo (dprovv1);
dprovv1 = (dprovv1 + 1);
}
}

The dprovv1 variable seems not to keep the changed value.
Thank you.


I do not speak English. I apologize for my english.




--
View this message in context: http://forum.openscad.org/The-variable-does-not-retain-the-changed-value-tp18219.html
Sent from the OpenSCAD mailing list archive at Nabble.com.

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Reply | Threaded
Open this post in threaded view
|

Re: The variable does not retain the changed value.

MichaelAtOz
Administrator
In reply to this post by bvox123
https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/General#Variables

I think we should ditch the term 'variable'...
Admin - email* me if you need anything,
or if I've done something stupid...
* click on my MichaelAtOz label, there is a link to email me.

Unless specifically shown otherwise above, my contribution is in the Public Domain; to the extent possible under law, I have waived all copyright and related or neighbouring rights to this work.
Obviously inclusion of works of previous authors is not included in the above.


The TPP is no simple “trade agreement.” Fight it! http://www.ourfairdeal.org/ time is running out!
Reply | Threaded
Open this post in threaded view
|

Re: The variable does not retain the changed value.

jon_bondy
I totally agree.  This question comes up far too often


On 8/27/2016 7:36 AM, MichaelAtOz wrote:
> https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/General#Variables
>
> I think we should ditch the term 'variable'...
>
>


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Reply | Threaded
Open this post in threaded view
|

Re: The variable does not retain the changed value.

wolf
This is a long overdue decision.
But - Who is willing and capable to do so? I remember doug.moen doing this kind of thing to the manual, and he made a big mess of it until he finally learned how things fit together . . .

OpenSCAD sticks out of the CAD programs that I have tried because it is so easy to learn - until you get creative. Then rendering gets slow - very slow, indeed, and rendering times go up to become hours, not fractions of a second. I have learned by now how to speed up rendering about 50 000 times, i.e. to cut down 2 hour rendering to about 0.1 seconds.
But to do that, you need to take into account hardware limitations, and not hide behind some lofty software design theory.

In practical terms, I can speed up rendering about 100 times by moving from OpenSCAD to Free Pascal, by ditching any attempts at functional programming. If you want that kind of speed improvement for OpenSCAD, I am willing to show you how, and why ditching functional programming is a key to substantial speed improvements.
Another key to speeding up OpenSCAD is reducing the amount of data that is cached - caching large amounts of data slows down program execution, unlike caching small amounts, which speed up execution.
wolf
tp3
Reply | Threaded
Open this post in threaded view
|

Re: The variable does not retain the changed value.

tp3
On 08/30/2016 01:14 AM, wolf wrote:
> This is a long overdue decision.
> But - Who is willing and capable to do so? I remember doug.moen doing this
> kind of thing to the manual, and he made a big mess of it until he finally
> learned how things fit together . . .
>
Talk is cheap, and in this case also feels like it's intended to be
insulting. Why do you think that's needed?

Doug may have made a mistake, we all do from time to time *when we
are actually changing things*. The only interesting part is to also
care enough to fix it. Only changes will bring things forward.
He helped OpenSCAD a lot in various places, so to counter the mood
of the quoted part, I just want to say thanks Doug.

> But to do that, you need to take into account hardware limitations, and not
> hide behind some lofty software design theory.
>
Nothing surprising here. That's also true for $10000 commercial
software that can do lots of stuff better but still has to align
with real life and constraints of the existing hardware.

> Another key to speeding up OpenSCAD is reducing the amount of data that is
> cached - caching large amounts of data slows down program execution, unlike
> caching small amounts, which speed up execution.
>
If you have some numbers and specific suggestions for improvements,
please share. I guess everybody will welcome any speed-up in
render performance.

ciao,
  Torsten.


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
-- Torsten
Reply | Threaded
Open this post in threaded view
|

Re: The variable does not retain the changed value.

jon_bondy
In reply to this post by wolf
I'm curious about this:

"In practical terms, I can speed up rendering about 100 times by moving from
OpenSCAD to Free Pascal, by ditching any attempts at functional programming."

Can you show me an example of how to do this?  What libraries would be used?
Etc?

Jon

On 8/29/2016 7:14 PM, wolf wrote:

> This is a long overdue decision.
> But - Who is willing and capable to do so? I remember doug.moen doing this
> kind of thing to the manual, and he made a big mess of it until he finally
> learned how things fit together . . .
>
> OpenSCAD sticks out of the CAD programs that I have tried because it is so
> easy to learn - until you get creative. Then rendering gets slow - very
> slow, indeed, and rendering times go up to become hours, not fractions of a
> second. I have learned by now how to speed up rendering about 50 000 times,
> i.e. to cut down 2 hour rendering to about 0.1 seconds.
> But to do that, you need to take into account hardware limitations, and not
> hide behind some lofty software design theory.
>
> In practical terms, I can speed up rendering about 100 times by moving from
> OpenSCAD to Free Pascal, by ditching any attempts at functional programming.
> If you want that kind of speed improvement for OpenSCAD, I am willing to
> show you how, and why ditching functional programming is a key to
> substantial speed improvements.
> Another key to speeding up OpenSCAD is reducing the amount of data that is
> cached - caching large amounts of data slows down program execution, unlike
> caching small amounts, which speed up execution.
> wolf
>
>


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Reply | Threaded
Open this post in threaded view
|

Re: The variable does not retain the changed value.

Alan Cox
In reply to this post by wolf
> In practical terms, I can speed up rendering about 100 times by moving from
> OpenSCAD to Free Pascal, by ditching any attempts at functional programming.

That I doubt for the same precision. If you profile OpenScad the
functional language parsing part of it is lost in the noise. It's all the
computation done on the geometries that are the problem.

> If you want that kind of speed improvement for OpenSCAD, I am willing to
> show you how, and why ditching functional programming is a key to
> substantial speed improvements.

No. Read the profile data for the execution.

> Another key to speeding up OpenSCAD is reducing the amount of data that is
> cached - caching large amounts of data slows down program execution, unlike
> caching small amounts, which speed up execution.

Also wrong. The performance of a cache is a function of the available
space, the cost of data regeneration and search. In the OpenSCAD case
the only number that is even relevant is the cost of regenerating the
data because it dominates the rest.

We know how to speed up OpenSCAD. It's just hard work.

1. Move away from CGAL or write CGAL classes for stable fixed point 64bit
maths
2. Add parallelism where it's currently heavily single threaded
3. Switch to a different set of algorithms with different scaling
properties

All of those are very involved. For complex designs you may want to look
at ImplicitCAD (https://github.com/colah/ImplicitCAD). It's a relative
language wise of OpenSCAD but uses Haskell as a programming language and
has parallelism and uses an algorithmic approach which for complex
objects tends to scale better. It's underlying models can also do things
that OpenSCAD struggles with such as nice bevels and fillets.

ImplicitCAD however is also rather lacking in the nice UI features that
OpenSCAD has so if you want to knock together an object, spin it around
in the UI and work on it the experience simply doesn't compare.

Alan



_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Reply | Threaded
Open this post in threaded view
|

Re: The variable does not retain the changed value.

wolf
You asked for it, here it is.
The attached file HelixSource.tar.gz contains the source code to create a helix composed of 200000 vertices and dump it on your hard disk as a 112 MiB .stl file. To compile it, Lazarus 1.6 is required. According to the Lazarus website, "write it once, compile anywhere" i.e. no special requirements regarding your operating system. Whether that is true I have not tested. Execution time on my system ( Intel Core i5-3470@3.4GHz, 4 CPU cores without hyperthreading, Level 1 cache size :4 x 32 kB, Level 2 cache: 4x256kB, Level 3 cache:6 MB shared cache, Physical memory: 32 GB, Operating system: 64 bit Kubuntu) is about 0.2 seconds, of which about half is used to render the 200000 vertices into 32 bit .stl format. Each vertex is calculated using 80 bit floating point numbers, with a 64 bit mantissa. I have tried to use 64bit fixed precision format until I realized that rounding errors cannot be avoided this way, since part of the work is calculating the length (norm) of vectors, which would require 128 bits if the result were not to be rounded. The .stl data are 32 bit floats, in line with the .stl spec.
Execution times are quite variable, in line with what to expect if caching is the main contributor determining execution speed.

Compare the 0.1 second true rendering time under Free Pascal / Lazarus with the 11 seconds rendering consumed when following the same programming strategy in OpenSCAD (shared in re: improve rendering speed, 9 June 2016, this forum, or the nearly 2 hours OpenSCAD requires for rendering if the helix is constructed from spheres, or the one hour if constructed, following parkinbot, from thin slices joined by hull(), or the 20 minutes it takes to render parkinbot's helix implementing a suggestion by Ronaldo how to produce slices of zero thickness.
Compare these speed increases also with Alan Cox' claim that parallel execution does lead to major speed increases. My system has just 4 cores, and if each core is fully utilized, I may expect a speed increase of somewhere between 2 and 3 times, not more. SSE instructions, allowing 128bit data, are useful for vectorizing 32 or 64 bit floats, with another 4 resp. 2 times speed increase. I use 80 bits, and cannot use them at all.

I presume I have demonstrated that I know what I am talking about?

By the way, if you decide to go to the trouble and compile my program, load the resultant .stl file into both OpenSCAD and MeshLab and try to rotate it. Which program is still usable with files of this size?

This concludes part one of what I have to say, about speed increases that can be expected if functional programming is officially ditched. Part 2 will show that functional programming has, in version 2015.03-1,  already been ditched - inofficially. It is just that the key people have not realized it.
wolf
Reply | Threaded
Open this post in threaded view
|

Re: The variable does not retain the changed value.

Parkinbot
wolf wrote
I presume I have demonstrated that I know what I am talking about?
I'd rather doubt that. You have written and tweaked a single benign thing and use it to shoot against a paradigm blindly. Show us, how you e.g. intersect two or more of your helixes and allow for any boolean operation in just 0.1s. Or show us, how you implement something like this:



Reply | Threaded
Open this post in threaded view
|

Re: The variable does not retain the changed value.

wolf
In reply to this post by wolf
When OpenSCAD got equipped with List Comprehension, the compiler-enforced prohibition against true variables fell by the wayside. A list may be used as the foundation of a user-controlled memory system. Consider this code:

// create list
FirstList=[for (i=[0:1:6]) i];
LastOne=FirstList[len(FirstList)-1];
echo(len(FirstList),LastOne);

This piece of code first changes the content of a variable and then echoes the last entry into the list. More sophisticated versions of this design could be used to construct a library that allows transparently to execute imperative code in OpenSCAD, with the compiler none the wiser.

// sum up all elements in list
AddUp=[for (i=[1:1:len(FirstList)-1]) FirstList[i-1]+FirstList[i]];
Sum=AddUp[len(AddUp)-1];
echo(AddUp,Sum);

And if you read FirstList from an external file, you have input/output facilities for OpenSCAD.

Not convenient and rather roundabout? I agree wholeheartedly. But it is possible . . .

Now, translate above into (pseudo)Assembler instructions. This would look something like:
1:  move mem_A Register_A     // load a value into a register
2:  add   mem_B Register_A     // do some work, here adding another number to what is in Register_A
3:  move Register_A mem_C    // save the result
4:  jump 1                                 // repeat the work
Control routines, i.e. when to skip line 4, I have omitted, as they are not essential to my argument.

The distinction here between imperative and functional programming is that for imperative code, line 3 is optional and the compiler does not enforce its presence. But for true functional programming, each memory location can accept a value only once and thus, each intermediate result needs to be saved. This in turn says that functional programming burdens the CPU with more work, and that means the CPU is slower. You can, of course, permit the compiler to be more lenient and not store every intermediate result. But that is fudging the issue . . . As it does speed up program execution!

If all three memory locations are in my level 1 cache, each Assembler instruction will take one clock cycle and the whole lot just over one nanosecond on my machine. But if they are not in level 1 cache, the cache controller will require time to fetch it, from level 2 cache, if it is there, from level 3 cache, if it is there, from general memory, if it is there, from the hard disk cache, if it is there, and if the data are nowhere to be found, from the swap file on my hard disk. Since that turns at 7200rpm, data read rate is 7200/60=120bit/second/read head. This means there is speed differential between data from the fastest memory location (=cache) to the slowest of about 1 in 10 million! And again, details like latency I have not considered, as they only lower speed even further. What I have observed is that speeding up by a factor of 70000 (2 hours /0.1 seconds) is possible, not all of it originating from imperative vs functional coding style. So far, I can assign only a factor of 100 to it, from 11 seconds to 0.1 seconds.

Since you have asked for it, libraries I do not use at all, if I can possibly avoid it. Coding speed is not an issue for me, understanding what I am coding, and why, is. And therefore, I take issue with people who provide misleading answers, people who waste my time, and that of others. Code that I write is unlikely to ever leave the level of experimental code. That is good enough for me. But what I learn from writing this code I share with those who care to listen, and only them.
wolf
Reply | Threaded
Open this post in threaded view
|

Re: The variable does not retain the changed value.

nophead
Where is the reassignment to a variable in your example? You just made a second list that is the sum of pairs of the first list. No list contents were modified.

Just because a functional language doesn't allow reassignment it doesn't mean that the compiler can't reuse memory. In fact compilers for imperative languages often create an intermediate code that produces a new symbol for each assignment and then allocates and re-uses registers and memory the most efficient way it can. The result bears little relationship to the source code.

On 31 August 2016 at 11:37, wolf <[hidden email]> wrote:
When OpenSCAD got equipped with List Comprehension, the compiler-enforced
prohibition against true variables fell by the wayside. A list may be used
as the foundation of a user-controlled memory system. Consider this code:

// create list
FirstList=[for (i=[0:1:6]) i];
LastOne=FirstList[len(FirstList)-1];
echo(len(FirstList),LastOne);

This piece of code first changes the content of a variable and then echoes
the last entry into the list. More sophisticated versions of this design
could be used to construct a library that allows transparently to execute
imperative code in OpenSCAD, with the compiler none the wiser.

// sum up all elements in list
AddUp=[for (i=[1:1:len(FirstList)-1]) FirstList[i-1]+FirstList[i]];
Sum=AddUp[len(AddUp)-1];
echo(AddUp,Sum);

And if you read FirstList from an external file, you have input/output
facilities for OpenSCAD.

Not convenient and rather roundabout? I agree wholeheartedly. But it is
possible . . .

Now, translate above into (pseudo)Assembler instructions. This would look
something like:
1:  move mem_A Register_A     // load a value into a register
2:  add   mem_B Register_A     // do some work, here adding another number
to what is in Register_A
3:  move Register_A mem_C    // save the result
4:  jump 1                                 // repeat the work
Control routines, i.e. when to skip line 4, I have omitted, as they are not
essential to my argument.

The distinction here between imperative and functional programming is that
for imperative code, line 3 is optional and the compiler does not enforce
its presence. But for true functional programming, each memory location can
accept a value only once and thus, each intermediate result needs to be
saved. This in turn says that functional programming burdens the CPU with
more work, and that means the CPU is slower. You can, of course, permit the
compiler to be more lenient and not store every intermediate result. But
that is fudging the issue . . . As it does speed up program execution!

If all three memory locations are in my level 1 cache, each Assembler
instruction will take one clock cycle and the whole lot just over one
nanosecond on my machine. But if they are not in level 1 cache, the cache
controller will require time to fetch it, from level 2 cache, if it is
there, from level 3 cache, if it is there, from general memory, if it is
there, from the hard disk cache, if it is there, and if the data are nowhere
to be found, from the swap file on my hard disk. Since that turns at
7200rpm, data read rate is 7200/60=120bit/second/read head. This means there
is speed differential between data from the fastest memory location (=cache)
to the slowest of about 1 in 10 million! And again, details like latency I
have not considered, as they only lower speed even further. What I have
observed is that speeding up by a factor of 70000 (2 hours /0.1 seconds) is
possible, not all of it originating from imperative vs functional coding
style. So far, I can assign only a factor of 100 to it, from 11 seconds to
0.1 seconds.

Since you have asked for it, libraries I do not use at all, if I can
possibly avoid it. Coding speed is not an issue for me, understanding what I
am coding, and why, is. And therefore, I take issue with people who provide
misleading answers, people who waste my time, and that of others. Code that
I write is unlikely to ever leave the level of experimental code. That is
good enough for me. But what I learn from writing this code I share with
those who care to listen, and only them.
wolf



--
View this message in context: http://forum.openscad.org/The-variable-does-not-retain-the-changed-value-tp18219p18278.html
Sent from the OpenSCAD mailing list archive at Nabble.com.

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org