GC and MMM
Hi All! Does GC scan manually allocated memory? I want to use huge manually allocated hash tables and I don't want to GC scan them because performance reasons. Best regards, Ilya
Re: automatically verifying code samples in phobos docs
On 2015-08-20 01:41, Laeeth Isharc wrote: Should this be done? How? Just use a documented unit tests block: /// unittest { // code goes here } It will be run as part of the unit tests and it will be included when generating the documentation. Although I don't have a good solution for the code in dlang.org that's not part of the Phobos/druntime documentation. I sent a pull request for the std.net.curl docs. They all talk about assigning the results of web requests to strings, but at least on my setup this does not work (cannot assign char[] to string). I was trying to walk someone else through using this and it was confusing. Error: cannot implicitly convert expression (get(http.dlang.org, AutoProtocol())) of type char[] to string BTW I don't know why you can't convert a char[] to string - seems harmless enough conversion that way around. char[] is mutable while string is not. Someone else can have a reference to the same data and change it will it's typed as string, breaking the type system. -- /Jacob Carlborg
Re: automatically verifying code samples in phobos docs
On Thursday, 20 August 2015 at 06:28:44 UTC, Jacob Carlborg wrote: On 2015-08-20 01:41, Laeeth Isharc wrote: [...] Just use a documented unit tests block: /// unittest { // code goes here } It will be run as part of the unit tests and it will be included when generating the documentation. Although I don't have a good solution for the code in dlang.org that's not part of the Phobos/druntime documentation. [...] char[] is mutable while string is not. Someone else can have a reference to the same data and change it will it's typed as string, breaking the type system. Will AutoProtocol().idup not make this work? Make an immutable copy of whatever AutoProtocol() returns, which should be then immutable char[] (i.e. string)
Re: unusual bare metal target: Amazon Dash
On Tuesday, 18 August 2015 at 01:32:13 UTC, Laeeth Isharc wrote: I don't know whether D can run on one, but from a quick look perhaps feasible. Running D on something like this (perhaps it's underpowered, but looked to have similar spec to what people had been doing with related ARM cortex processors) would certainly make the point very vivid that it can be a bare metal programming language. Only 1Mb of flash RAM for the program - is that enough? Yes, with the right techniques. Everything one needs to get started is documented at http://wiki.dlang.org/Minimal_semihosted_ARM_Cortex-M_%22Hello_World%22 A more complete proof of concept on similar hardware can be found at https://github.com/JinShil/stm32f42_discovery_demo Mike
Re: automatically verifying code samples in phobos docs
On 2015-08-20 10:49, wobbles wrote: Will AutoProtocol().idup not make this work? Make an immutable copy of whatever AutoProtocol() returns, which should be then immutable char[] (i.e. string) Yes, that should work. -- /Jacob Carlborg
Re: New to D - playing with Thread and false Sharing
On 8/21/2015 3:37 AM, Dejan Lekic wrote: Keep in mind that in D everything is thread-local by default! :) For shared resources use __gshared or shared (although I do not know for sure whether shared works or not). Note: shared is __gshared but with mutex's added.
Re: New to D - playing with Thread and false Sharing
On Thursday, 20 August 2015 at 20:01:58 UTC, tony288 wrote: On Thursday, 20 August 2015 at 15:37:35 UTC, Dejan Lekic wrote: [...] Thanks, I changed the code and the previous one was already using shared. import std.stdio; import core.time; import core.thread; [...] Keep in mind java may be using green threads as opposed to kernel threads. The equivalent in D is a Fiber.
Re: GC and MMM
On Thursday, 20 August 2015 at 17:13:33 UTC, Ilya Yaroshenko wrote: Hi All! Does GC scan manually allocated memory? I want to use huge manually allocated hash tables and I don't want to GC scan them because performance reasons. Best regards, Ilya Yes, just don't store any GC managed pointers in said manually allocated memory. Or at the very least consider any GC managed pointers to be weak pointers. core.memory add/remove range have to be used put c malloc memory into the GC, simply don't call these functions.
Re: GC and MMM
On Thursday, 20 August 2015 at 17:13:33 UTC, Ilya Yaroshenko wrote: Hi All! Does GC scan manually allocated memory? I want to use huge manually allocated hash tables and I don't want to GC scan them because performance reasons. Best regards, Ilya GC does not scan memory allocated with malloc from core.stdc.
Re: unusual bare metal target: Amazon Dash
On Thursday, 20 August 2015 at 09:08:02 UTC, Mike wrote: On Tuesday, 18 August 2015 at 01:32:13 UTC, Laeeth Isharc wrote: I don't know whether D can run on one, but from a quick look perhaps feasible. Running D on something like this (perhaps it's underpowered, but looked to have similar spec to what people had been doing with related ARM cortex processors) would certainly make the point very vivid that it can be a bare metal programming language. Only 1Mb of flash RAM for the program - is that enough? Yes, with the right techniques. Everything one needs to get started is documented at http://wiki.dlang.org/Minimal_semihosted_ARM_Cortex-M_%22Hello_World%22 A more complete proof of concept on similar hardware can be found at https://github.com/JinShil/stm32f42_discovery_demo Mike Thanks, Mike. I have a mind to do this when time just to make a point next time someone who has never used the language starts on about the GC... ;) Unfort without libraries to control wifi unit or ultrasound, that's of minimal value today, but I am sure someone will figure that out in time.
Re: Reading dmd's source code
On Thursday, 20 August 2015 at 04:25:01 UTC, Freddy wrote: dmd's source code is very big, are there tips for reading it(important files)? Might make for a good http://wiki.dlang.org/ page, particularly after the transition to ddmd.
Re: automatically verifying code samples in phobos docs
On Thursday, 20 August 2015 at 06:28:44 UTC, Jacob Carlborg wrote: On 2015-08-20 01:41, Laeeth Isharc wrote: Should this be done? How? Just use a documented unit tests block: /// unittest { // code goes here } It will be run as part of the unit tests and it will be included when generating the documentation. Although I don't have a good solution for the code in dlang.org that's not part of the Phobos/druntime documentation. It think it would be a positive for dlang.org's code to be unit tested, but it might also be tricky if that were the only option for including code in dlang.org. For instance, suppose the dlang.org page looks something like text code block text code block that depends on above code block I actually see this rather often. If you change the code blocks to unit tests, then the second code block would not compile by itself. I see two options: 1) make one big unit test and have the second piece of text be a comment in it, 2) include the original unit test in the second code block (messy and has its own set of problems). Maybe there is a solution to this, but I think it would require some sort of change. Maybe one solution is to improve the way text comments can be used with unit tests. Alternately, it might be interesting to have a way to refer/import other unit tests.
Re: Why does not my program is not running?
On Thursday 20 August 2015 22:31, Unknow wrote: I'm writed a program for calculating the e number. I can compile the source code but when i try run the program, system gives 'program stopped working' error. Source code; // main.d module main; import std.file; import std.conv; long factorial(long i){ if (i == 0){ return 1; }else{ return(i * factorial(i-1)); } } void main(string[] args){ real *e; e = new real; *e = 0; long *integer; integer = new long; *integer = 1; for(; *integer = 100; *integer++){ *e = (*e) + (*integer / factorial(*integer)); } if(exists(e) != 0) { std.file.write(e, to!string(*e)); }else{ //... } delete(e); delete(integer); } This has a couple issues. 1) The factorial gets huge quickly. For example, factorial(100) definitely won't fit in a long. It'll wrap around and give wrong results. 2) *integer++ doesn't do what you think it does. The increment is done before the dereference. You could fix this, but: 3) Don't use pointers like that. You really don't need to new reals and longs like that. 4) `*integer / factorial(*integer)` does integer division, which you don't want there. You can multiply with `1.0` (a floating point number) to force floating point division: `*integer * 1.0 / factorial(*integer)`. 5) I'm not sure what you want the `exists` condition to do. The way it is, the file e is only (over)written when it exists already. This may not be what you want. Better write it like this: if(exists(e)) { /* Do what you want when the file exists. */ } else { /* Do what you want when the file doesn't exist. */ } 6) Don't use delete unless you know exactly what you're doing (which you don't yet). Here's a cleaned up version of your code: module main; import std.stdio: writeln; long factorial(long i){ if (i == 0){ return 1; }else{ return(i * factorial(i-1)); } } void main(string[] args){ real e = 0; long integer = 1; for(; integer = 10; integer++){ e = e + integer * 1.0 / factorial(integer); } writeln(e); /* 2.71828 */ } I severely limited the range of integer. I don't know off the top of my head how large you can make it without hitting overflow. I removed the file writing, because I'm not sure if you want to write it only when it doesn't exist, or only overwrite when it does exist.
Re: Why does not my program is not running?
On 8/20/15 4:31 PM, Unknow wrote: module main; import std.file; import std.conv; long factorial(long i){ if (i == 0){ return 1; }else{ return(i * factorial(i-1)); } } Note, this is not going to work. At 21!, long will run out of bits. From https://en.wikipedia.org/wiki/Factorial, 100! is 9.332621544×10^157, requiring 157 *decimal* digits, not to mention the base-2 digits that would be required. void main(string[] args){ real *e; e = new real; *e = 0; long *integer; integer = new long; *integer = 1; There is no need for this, you are making pointers from everything, just declare the variables: real e = 0; long integer = 1; for(; *integer = 100; *integer++){ And here is why you crash. *integer++ means return the value currently pointed at by integer, then increment the POINTER by one. This points at some garbage memory (on my system, it happens to be zeroed, so it didn't crash). Not using pointers will help a lot in this code. *e = (*e) + (*integer / factorial(*integer)); The second portion of this calculation is INTEGER math, which means except for 1 / 1, it's going to be 0. What you want is (with new style that doesn't use pointers): e = e + (real(integer) / factorial(integer)); The real(...) casts the result to a real before continuing. } if(exists(e) != 0) { std.file.write(e, to!string(*e)); Just want to confirm here, you are writing e only if it *already exists*? }else{ //... } delete(e); delete(integer); Without pointers, you would remove these. In addition, you do not need to delete pointers even if you did use them -- D is garbage collected. } -Steve
Re: Why does not my program is not running?
On Thursday, 20 August 2015 at 21:11:07 UTC, anonymous wrote: I severely limited the range of integer. I don't know off the top of my head how large you can make it without hitting overflow. I removed the file writing, because I'm not sure if you want to write it only when it doesn't exist, or only overwrite when it does exist. with integer == 66 the factorial overflows and becomes 0 (on my machine) = integer division by 0... The overflow happens at lower values, too.
Re: Why does not my program is not running?
On Thursday 20 August 2015 23:11, anonymous wrote: 2) *integer++ doesn't do what you think it does. The increment is done before the dereference. You could fix this, but: I got that one wrong. Steven Schveighoffer has it right. The pointer is incremented after the currently pointed-to value is returned. Still not what was expected, of course.
Re: Why does not my program is not running?
On Thursday, 20 August 2015 at 21:10:27 UTC, Steven Schveighoffer wrote: On 8/20/15 4:31 PM, Unknow wrote: module main; import std.file; import std.conv; long factorial(long i){ if (i == 0){ return 1; }else{ return(i * factorial(i-1)); } } Note, this is not going to work. At 21!, long will run out of bits. From https://en.wikipedia.org/wiki/Factorial, 100! is 9.332621544×10^157, requiring 157 *decimal* digits, not to mention the base-2 digits that would be required. void main(string[] args){ real *e; e = new real; *e = 0; long *integer; integer = new long; *integer = 1; There is no need for this, you are making pointers from everything, just declare the variables: real e = 0; long integer = 1; for(; *integer = 100; *integer++){ And here is why you crash. *integer++ means return the value currently pointed at by integer, then increment the POINTER by one. This points at some garbage memory (on my system, it happens to be zeroed, so it didn't crash). Not using pointers will help a lot in this code. *e = (*e) + (*integer / factorial(*integer)); The second portion of this calculation is INTEGER math, which means except for 1 / 1, it's going to be 0. What you want is (with new style that doesn't use pointers): e = e + (real(integer) / factorial(integer)); The real(...) casts the result to a real before continuing. } if(exists(e) != 0) { std.file.write(e, to!string(*e)); Just want to confirm here, you are writing e only if it *already exists*? }else{ //... } delete(e); delete(integer); Without pointers, you would remove these. In addition, you do not need to delete pointers even if you did use them -- D is garbage collected. } -Steve Thanks a lot of! Im new to D lang.
Why does not my program is not running?
I'm writed a program for calculating the e number. I can compile the source code but when i try run the program, system gives 'program stopped working' error. Source code; // main.d module main; import std.file; import std.conv; long factorial(long i){ if (i == 0){ return 1; }else{ return(i * factorial(i-1)); } } void main(string[] args){ real *e; e = new real; *e = 0; long *integer; integer = new long; *integer = 1; for(; *integer = 100; *integer++){ *e = (*e) + (*integer / factorial(*integer)); } if(exists(e) != 0) { std.file.write(e, to!string(*e)); }else{ //... } delete(e); delete(integer); }
Re: App Build Error
On Monday, 17 August 2015 at 01:02:51 UTC, Nicholas Wilson wrote: On Sunday, 16 August 2015 at 17:33:52 UTC, Benjamin wrote: I'm having an issue with building my app - even a simple trivial app (shown below). [...] OS X version? Have you configured your dmd.conf? iirc it requires linker path changes or something. Have you looked in /usr/local/lib for libiconv.* ? Have you any idea why it's targeting i386 and not, say i686 and not passing -m64 by default? (You're getting the arch mismatch because you passed -m64 and dmd for some reason didn't) I'm running OSX 10.10.5. I've not changed dmd.conf. I did a fresh install (2.068) after removing the previous version (2.067). I noticed the arch mismatch, but this is how it was installed. Nothing special. Thanks! Ben.
flush MessageBox
Is there a way to flush a thread's message box other than aborting the thread? MailBox is private: https://github.com/D-Programming-Language/phobos/blob/master/std/concurrency.d#L1778
New to D - playing with Thread and false Sharing
Hi, I have been playing for a short time with D. I wanted to play with Thread, which I believe is lower level than concurrency and parallelism library (might be wrong here ?) . I was also learning and reading about false sharing - multiple thread = same cacheline. And I decided to play with both to 1 understand more D and see the results of false sharing. So I wrong some code. But it seems the time to process a shared struct shared long is always the same. Regardless of adding paddings. My thoughts is that as I'm new to D. That it can only be my fault and not doing things correctly. I doubt the compiler etc.. adds etc padding? If you have any code to show it, it would be really great. But this is what I have done : I found this article http://mechanical-sympathy.blogspot.co.uk/2011/08/false-sharing-java-7.html and tried to emulate this. In java I do get the same result as them and can understand the code. But curious in D i get completely different results. import std.stdio; import core.time; import core.thread; void main() { writeln(PaddingStruct.sizeof); MonoTime before = MonoTime.currTime; runTest(); MonoTime after = MonoTime.currTime; Duration timeElapsed = after - before; writeln(Duration is , timeElapsed) ; } private static void runTest() { Thread[] threads = new Thread[4]; for (int i = 0; i threads.length; i++) threads[i] = new FalseSharing(i);//putting a new thread in the array foreach(Thread t ; threads){//starts all threads t.start(); } foreach(Thread t; threads){ t.join(); } } class FalseSharing : Thread { private static PaddingStruct[] longs = new PaddingStruct[4]; private const int threadIndex; this(const int index) { threadIndex = index; PaddingStruct a; longs[threadIndex] = a; super(run); } void run() { long i = 500L * 1000L * 1000L; writeln( threadindex is - , threadIndex); writeln( i is - , i); while (0 != --i){longs[threadIndex].value = i;} writeln(threaded ,threadIndex, ended so a=1 - is it? , longs[threadIndex].value); } } struct PaddingStruct { static shared long value = 0L; //shared across multiple thread //long p1,p2,p3,p4,p1a;//padding. }
Re: New to D - playing with Thread and false Sharing
Keep in mind that in D everything is thread-local by default! :) For shared resources use __gshared or shared (although I do not know for sure whether shared works or not).
Re: New to D - playing with Thread and false Sharing
On Thursday, 20 August 2015 at 15:37:35 UTC, Dejan Lekic wrote: Keep in mind that in D everything is thread-local by default! :) For shared resources use __gshared or shared (although I do not know for sure whether shared works or not). Thanks, I changed the code and the previous one was already using shared. import std.stdio; import core.time; import core.thread; void main() { MonoTime before = MonoTime.currTime; runTest(); MonoTime after = MonoTime.currTime; Duration timeElapsed = after - before; writeln(Duration is , timeElapsed) ; } private static void runTest() { Thread[] threads = new Thread[4]; FalseSharing[] fs = new FalseSharing[4]; for (int i = 0; i threads.length; i++){ FalseSharing fsx = new FalseSharing(i); fs[i] = fsx; threads[i] = fsx; } foreach(Thread t ; threads){//starts all threads t.start(); } foreach(Thread t; threads){ t.join(); } } static PaddingClass[] longs = new PaddingClass[4]; class FalseSharing : Thread { private const int threadIndex; this(const int index) { threadIndex = index; longs[threadIndex] = new PaddingClass(); super(run); } void run() { long max = 1000L * 100L * 100L * 10L; for(long i=1; i=max ; i++){longs[threadIndex].value = i;} } } public static shared class PaddingClass { public double p1,p2,p3,p4,p5,p6;//padding. public shared long value = 0L; //shared across multiple thread } So what I see, D ( of course and expected) using more or less the same syntax as java. Behaves very differently. Which I mean padding or not, D behaves slower than Java with padding by a long way. Now what I would like to know, how would I make this code more efficient? Which is basically the aim I'm trying to achieve. Any pointers would be really help full. Should I use concurrency/parallelism etc..? Thanks
Re: Why does not my program is not running?
On 8/20/15 5:15 PM, anonymous2 wrote: On Thursday, 20 August 2015 at 21:11:07 UTC, anonymous wrote: I severely limited the range of integer. I don't know off the top of my head how large you can make it without hitting overflow. with integer == 66 the factorial overflows and becomes 0 (on my machine) = integer division by 0... The overflow happens at lower values, too. I did this in my test of his code: long factorial(long i){ if (i == 0){ return 1; }else{ auto r = factorial(i-1); assert(r * i / r == i, i.to!string); return r * i; } } And it printed 21 for the exception. 20! fits in a long, 21! doesn't. -Steve
Re: flush MessageBox
On Thursday, 20 August 2015 at 15:25:57 UTC, Chris wrote: Is there a way to flush a thread's message box other than aborting the thread? MailBox is private: https://github.com/D-Programming-Language/phobos/blob/master/std/concurrency.d#L1778 flush from inside the thread? You could call receiveTimeout with a 0 timer in a loop until it returns false.