World: r4wp
[#Red] Red language group
older newer | first last |
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 [8569x3] | 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 | |
Doc, I don't know if you've done other tests, but in the benchmarks so far Red/System was only 4 times slower than optimised C in floating point code. For integer code, it was around twice slower than optimised C, so the Mersenne Twister confirms this | |
DocKimbel 22-Jun-2013 [8572x2] | I thought it was twice slower than unoptimised C? |
this will create an eternal pressure from technical people to make Red 0 based In fact, I've decided since a while to add PICKZ and POKEZ to Red so 0-base algorithms would be more natural to implement. I need to add an entry in Trello about that or I will keep forgetting about it... | |
Kaj 22-Jun-2013 [8574x2] | It's a bit confusing, but Red/System integer is as fast as unoptimised C (x 1 in function calling, 1.3 slower in hard integer as measured by XieQ), twice as slow as optimised C, and Red/System floating point is four times as slow as optimised C |
Which also means that Red/System floating point is twice as slow as unoptimised C | |
Arnold 22-Jun-2013 [8576] | Any fine examples of how to program refinements in Red(/System) I can use to copycat? |
Kaj 22-Jun-2013 [8577] | Red/System doesn't have refinements. Refinements in Red are almost exactly like in REBOL |
Arnold 22-Jun-2013 [8578] | Hmm random/seed needs an input, random does not, but has an output that random/seed has not. |
Kaj 22-Jun-2013 [8579x4] | As a function, you'd have to call that SEED or seed-random in Red/System |
If you want to offer multiple functions as a library, you can wrap their shared state in a CONTEXT | |
Then you can have random/seed, but it won't be a refinement :-) | |
And it will definitely stop looking like C :-) | |
Arnold 22-Jun-2013 [8583] | It certainly would be nice to wrap all possibilities up in one package. |
Kaj 22-Jun-2013 [8584] | I think that's something different than what I meant |
Arnold 22-Jun-2013 [8585] | I think about a flexible solution to use the one random routine you want or need. |
Kaj 22-Jun-2013 [8586x2] | You can implement several algorithms with the same interface. CONTEXT is useful there. Then you can #include the one implementation you want |
There are many such constructs in my bindings and Red itself | |
Oldes 23-Jun-2013 [8588] | Hi Doc, I noticed that you published Android support yesterday. Will you publish also some example how to use it? |
Kaj 23-Jun-2013 [8589] | Note that it's preliminary, in the development branch |
Oldes 23-Jun-2013 [8590x2] | Is it a problem? :) |
I consider all branches to be a development as all Red/S is still in alpha (or is it already beta?) version. | |
Kaj 23-Jun-2013 [8592x2] | I think Red/System counts as beta, as far as v1 is concerned |
I don't know if it's a problem. What I do know is that the Android port doesn't fully work yet. If you consider it developmental, then you can also expect Doc to add examples later. Anyway, I was just providing a preliminary answer for you because Doc is offline | |
DocKimbel 23-Jun-2013 [8594x3] | Hi Oldes, I have pushed online only the low-level layers of the Android port. I will push the rest after some cleanups most probably tomorrow. It's working fine on pre-4.1 Android systems, but I'm still struggling with low-level bugs with 4.1+. |
I will provide the full source code of the preview demo I've posted and will probably add another demo. It's still low-level manipulations of the Android Java framework, the final layers of abstractions are not yet there (dialects, schemes, ...). | |
I will need to go back to object! implementation in Red before implementing those layers. | |
Andreas 23-Jun-2013 [8597] | Quick testing of the MT Red/S and C versions on my machine give a ~4x difference in speed. |
older newer | first last |