• Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search
 

World: r4wp

[#Red] Red language group

Arnold
20-Jun-2013
[8521x3]
my trouble seems to arise from the different roles the array and 
the pointer play in the C code vs the Red/System code. In C the pointer 
is used as indicator for initialisation and signals when a kind of 
reshuffle is required and it points to the next generated value. 
In Red/System the array points to the current/next value and a pointer 
is not needed, and you can no longer use it as before to signal the 
status. This complicates things in this case.
Oh nice effect. array1: 1 2 3 4 5 
m: 2
;;print array/m ;; 2
array1: array1 + 2
;;print array/m ;; 5

it is tricky to move the array pointer around, beware of the dog 
;)
sorry 5 has to be 4 in that example.
DocKimbel
20-Jun-2013
[8524x2]
You seem quite confused about pointers in Red/System. 


1) There's no "array" as distinct entity in Red/System, but you have 
indexed access, so you can "simulate" arrays to some extent.


2) "In C the pointer is used as indicator for initialisation and 
signals when a kind of reshuffle is required and it points to the 
next generated value." I don't understand this sentence, you should 
stick to C terminology (terms like "signals", "reshuffle" and "generated 
value" are alien to me in a C language context).


3) "In Red/System the array points to the current/next value and 
a pointer is not needed, and you can no longer use it as before to 
signal the status." I don't understand this one either... There's 
no array, only pointers. "to signal the status" has no meaning to 
me...sorry.
I think you have a complicated mental representation of what pointers 
and arrays are in C and Red/System. It's simpler and more straightforward 
than your descriptions. Basically, Red/System pointers act the same 
way as C ones (including "array" support). The only difference is 
the base for indexed accesses (1 vs 0).
Kaj
20-Jun-2013
[8526x4]
Arnold, what you call :(ran_arr_buf/1) is possible and is just
:ran_arr_buf
Sorry, even simpler:
ran_arr_buf
Arnold
20-Jun-2013
[8530]
Yes I know, I built one in already, except when you have shifted 
the ran_arr_buf, this one shifts along, but I want to return then. 
So I saved it in a sav_arr_buf to take care of this. All in all because 
I try to stay as close as possible to the original I have some extra 
bookkeeping to take care of. Later today I will clean up the code 
further and hope the smoke clears up ;)
XieQ
21-Jun-2013
[8531]
@Arnold I write the Mersenne Twister random function in Red/System 
according to an educational C standard library implementation ( http://code.google.com/p/c-standard-library/source/browse/src/internal/_rand.c
). Hope you find it useful. you can read it from here: https://gist.github.com/qtxie/5831308
DocKimbel
21-Jun-2013
[8532]
Great work XieQ, congrats! Have you checked it against a C version 
to verify that the output is the same, given the same seed number?
Kaj
21-Jun-2013
[8533]
Looks good
Arnold
21-Jun-2013
[8534x2]
I see a pattern.. I discovered I new method of software development!
My 'problem' with the Mersenne Twister is/was that if you print the 
integer it is turned into a negative number if the MSB is equal to 
1, the results could be the same, I did not check. Nevertheless it 
has inspired XieQ to work on his version? Nice to see this result 
XieQ! Keep up the good work!

 The Knuth algorythm is very interesting because of the pointers used 
 and I have little experience in pointers so I really learn a lot 
 and I hope to be able to document the differences, explaining it 
 to newbies in the Red language.
DocKimbel
21-Jun-2013
[8536]
Arnold, good idea!
Arnold
21-Jun-2013
[8537]
Question, the program needs an array and it is declared using array1: 
as int-ptr! allocate 100 * size? integer!
Then at the end I free this using free as byte-ptr! array1

Is this really necessary. If the program terminates the memory will 
be freed yes?

(Oh by the way don't free the memory when you are not on the starting 
position anymore, it results in an error.)

(Next, the source I sent to the windows environment, compiled there, 
followed by an immediate runtime error. maybe not the latest Red 
version problem again)
Kaj
21-Jun-2013
[8538]
A good operating system will free the memory when the process ends, 
but it's a bad idea to not do your own cleanup of memory and other 
resources
Arnold
21-Jun-2013
[8539x2]
Why is this *** Compilation Error: variable has to be initialized 
at root level 
m: 1
rval: 0
while [m <= 111][
    rval: random
    print ["Random nummer: " m " random waarde: " rval lf]
    m: m + 1
]
The line setting rval prevents this warning.
Well you don't know when this is do you?
Kaj
21-Jun-2013
[8541x2]
How do you mean?
The need to make variables known at the root level of the program 
is a limitation of the current Red/System compiler
Arnold
21-Jun-2013
[8543x2]
(I shared my taocp version in the red/randompublic folder)
Okay I mean you do an #include of a random library or other red(s) 
file, then declaration and init is done. Does the programmer need 
to call a random-free-memory function before ending his/her program?
DocKimbel
21-Jun-2013
[8545x2]
The line setting rval prevents this warning.
 Look at your ` waarde:` declaration in the PRINT block.
The need to make variables known at the root level of the program 
is a limitation of the current Red/System compiler

 Actually, it's not a limitation, it's a design decision based on 
 use cases like this one: https://github.com/dockimbel/Red/issues/84
Arnold
21-Jun-2013
[8547]
waarde is the dutch word for value, this is displaying the random 
generated value, it is between double quotes. In the C output I use 
something like " rval= " rval where in the Red/System I used " rval: 
" rval. This is for my convenience to tell both apart when the terminal 
windows look all the same.

It makes sense to circumvent issues like 84 by this method. Good 
to know this, thank you for explaining.
DocKimbel
21-Jun-2013
[8548]
Oops, sorry, haven't see the quotes.
Kaj
21-Jun-2013
[8549x2]
Arnold, setup and cleanup depend entirely on the library you #include. 
Some don't need any, some need setup but no cleanup, some expect 
both
However, this doesn't apply to your code at hand. You allocate memory 
in your program, so you should also free it
XieQ
21-Jun-2013
[8551x3]
After fix some bugs in my Random function, I got the same result 
against the C version with same seed finally.
One bug need to mention:

After doing mod operation, I use the result as index to access the 
array,it's OK in C, but will cause strange behavior in Red/System. 
Because mod will produce 0 and Red/System use 1-base array.
n: c + state-half-size % state-size
Then I modified the code as below to solve this issue:
n: c - 1 + state-half-size % state-size + 1
I also do a benchmark. It's seems too early to do this, but I can't 
contain my curiously ;-)
Produce 99999999 random numbers

Compare to no optimizition C version, the Red/System version is about 
1.3 times slower.
Compare to an optimizition C version, it's about 2 times slower.
Not bad! :-)
More detail: https://gist.github.com/qtxie/5835643
PeterWood
21-Jun-2013
[8554]
That's impressive XieQ
Kaj
21-Jun-2013
[8555]
That speed result is consistent with earlier benchmarks
Arnold
22-Jun-2013
[8556x2]
@Kaj, if I free the memory at the library funtion level then I lose 
the generator state all the time, I expect that not to be the best 
thing for the randomness of the produced numbers. Say I am a programmer 
and I #include the random functions file/lib. Do I need to call a 
random/free function at the end of my program yes or no?
@XieQ you subtract 1 first and then add it back? All indices get 
shifted 1, including the last one. In loops in C it is i.e. j < N 
and in R/S it will be j <= N. 
You meant curiosity not curiously.
DocKimbel
22-Jun-2013
[8558]
XieQ, indeed impressive results, I usually expects current Red/System 
to perform about 4-5 times slower than optimized C.
Arnold
22-Jun-2013
[8559x4]
Now we can generate the random numbers using the random algorythms. 
It is still a long way to make the random function like REBOL's random: 
http://www.rebol.com/docs/words/wrandom.html
Including a /secure that is, there are even other RNG's out there 
suitable for this purpose waiting for their transcoding to Red/System.
Yes the speed indicates there could be a possible bug, but the MS 
code is pretty straightforward and more one-to-one transcodable, 
so it might as well be right.
MS=MT
DocKimbel
22-Jun-2013
[8563x3]
@Xieq, I have in plan since the beginning to add fast low-level `odd?` 
and `even?` function. When they'll be implemented, that should give 
to your code a little additional boost. ;-)
Anyway, your implementation is a good hint that Red/System 2.0 should 
be able to give us general performances between unoptimized and optimized 
C code.
BTW, Arnold's and XieQ's work, and my own recent struggling with 
1-based Red/System low-level issues are hints that I need to reconsider 
1-base vs 0-base indexing in Red/System. Low-level algorithms are 
not 1-base friendly.
Arnold
22-Jun-2013
[8566x2]
Still prefer to make the choice as a programmer. How it is done behind 
the curtains is up to the compiler. Compare this with the address 
used in memory. Changing your array from 0-based to 1-based, it ends 
up in the same memory address (say that is the only change you made 
in the source). 

Besides using the 0-based structure was perhaps a struggle for the 
original programmer!!
We, no I, limited myself to keep as close to the original as possible. 
Also because of the obfusction factor of the source. I believe much 
of the construction could be made more like in the REBOL way of doing 
things. A C program just is not a Red/System program. My goal was 
to get a working program reproducing the same result in the shortest 
time. Because I had little idea what the original program did because 
it is complicating things on purpose this was the way to do it. Now 
based on this useable program it can be made more Red/REBOL-like 
step by step.

I do feel for you, for indeed a lot of stuff out there is C or 0-based 
and it brings its problems along, but the next step in bringing Red/System 
to 0-based is having Red being 0-based as well?
DocKimbel
22-Jun-2013
[8568]
[...] the next step in bringing Red/System to 0-based is having Red 
being 0-based as well?


No, it's not directly related. Implementing low-level algorithms 
that require working with index 0 is not the common usage of Red. 
Also, the +/-1 offset required in such case has no noticeable performance 
impact in a Red program while in Red/System, in a tight nested loop, 
the impact could be significant.


The most important part in considering a 0-based indexing system 
for Red/System is mainly helping the user and avoiding common programming 
errors (even I get caught by it from time to time, but it's probably 
due to my strong C background).
Kaj
22-Jun-2013
[8569x2]
The problem is that there is no final solution. In practice, I have 
mixed Red and Red/System code in many places, and I'm already often 
mixed up between the two. So if you change Red/System to be 0 based, 
there will be a new class of errors where programmers think in Red 
in their Red/System code, or in Red/System in their Red code. And 
as Arnold hints at, this will create an eternal pressure from technical 
people to make Red 0 based, and then we have given up on the human 
centered goals of REBOL and are back at square one - which will then 
have to be renamed square 0
Arnold, if you want to provide your random function as a library 
to other programmers, then yes, you should include a function to 
clean up its internal state