A question about postblit constructor
I am trying to learn behavior of postblit constructor. Below code works as expected when I comment out malloc part of postblit constructor. It writes 4 if malloc part of postblit constructor is commented out. Otherwise it writes default init value of int which is 0. I wonder how new memory is allocated without an explicit malloc here. Sorry for this noob question in advance, I could not find any doc mentioning a similar case. import std.stdio; import core.stdc.stdlib; import core.stdc.string; struct S { int* vals; size_t length; this(this){ //vals = cast(int*)malloc(length * S.sizeof); memcpy(vals, vals, length * S.sizeof); writeln("copied"); } } void main() { size_t len = 2; int* vals = cast(int*)malloc(len * S.sizeof); vals[0] = 4; vals[1] = 5; S s1 = S(vals, len); S s2 = s1; writeln(s2.vals[0]); }
Re: Using shared memory and thread
On Tuesday, 5 November 2019 at 00:07:40 UTC, bioinfornatics wrote: On Tuesday, 5 November 2019 at 00:04:05 UTC, bioinfornatics wrote: On Monday, 4 November 2019 at 22:19:29 UTC, bioinfornatics wrote: On Monday, 4 November 2019 at 21:53:45 UTC, bioinfornatics wrote: On Monday, 4 November 2019 at 19:53:29 UTC, bioinfornatics wrote: [...] One image reveals more than a thousand words, see my picture :-) -> http://pix.toile-libre.org/?img=1572906675.png oups got a problem with the alpha layer: -> http://pix.toile-libre.org/?img=1572908302.png here my (not working) snippet code: -> https://run.dlang.io/gist/75429dcb8612e00635f7d53aabc4776f the example do not meet exactly the spec and he do not compile. arf the generated link raise a 404, here a gist I got some help from `feep` on IRC and he show me that the task pool can not to be const as reduce is not. So now I have this script: > https://gist.github.com/run-dlang/46c753f7233a624bb5639db615c9634a And this error: > /dlang/dmd/linux/bin64/../../src/phobos/std/parallelism.d(2730): Error: function std.parallelism.TaskPool.reduce!"a + b".reduce!(MapResult!(__lambda3, Result)).reduce cannot get frame pointer to onlineapp.countAllLines I found this thread which talk about this error, but that did not help me to fix it: https://github.com/dlang/phobos/pull/4915 Thanks for your help https://gist.github.com/run-dlang/4ffcc0641acef5eedf52a17c4995599b
Re: Using shared memory and thread
On Monday, 4 November 2019 at 19:53:29 UTC, bioinfornatics wrote: 3/ variable flagged as `shared` does at mean the variable is put into L2 cache ? First caching is controlled by CPU not programmer. Second not all architectures share L2 between cores. 4 jaguar cores share L2 cache in consoles https://images.anandtech.com/doci/6976/Screen%20Shot%202013-05-23%20at%2012.22.12%20AM_678x452.png 2 bulldozer cores share L2 cache in FX processors https://s3.amazonaws.com/hs-wordpress/wp-content/uploads/2017/12/13035240/bulldozer_021.jpg each zen core has its own L2 cache in Ryzen cpu's https://images.anandtech.com/doci/10591/HC28.AMD.Mike%20Clark.final-page-014.jpg Third your link to run.dlang is 404 While you cant control cache contents explicitly you can control it by changing memory access patterns. When you fetch a peace of data CPU will load 64 byte size bucket/chunk that contains your data into L1 cache. Since L1 cache size is limited CPU had to remove existing data from L1 based on its eviction policy. Evicted data goes into L2 cache. That is the way to control what is where. By default all variables(except immutable or static) are thread local and can only be accessed by one thread. Shared keyword makes data accessible trough multiple threads
Re: A question about postblit constructor
On Tuesday, 5 November 2019 at 08:47:05 UTC, Ferhat Kurtulmuş wrote: value of int which is 0. I wonder how new memory is allocated without an explicit malloc here. Sorry for this noob question in advance, I could not find any doc mentioning a similar case. int* vals = cast(int*)malloc(len * S.sizeof); vals[0] = 4; vals[1] = 5; S s1 = S(vals, len); S s2 = s1; writeln(s2.vals[0]); writeln(s2.vals); writeln(s1.vals); } https://run.dlang.io/is/D0nfeT
Re: A question about postblit constructor
On Tuesday, 5 November 2019 at 10:13:59 UTC, Mike Parker wrote: On Tuesday, 5 November 2019 at 08:47:05 UTC, Ferhat Kurtulmuş wrote: value of int which is 0. I wonder how new memory is allocated without an explicit malloc here. Sorry for this noob question in advance, I could not find any doc mentioning a similar case. int* vals = cast(int*)malloc(len * S.sizeof); vals[0] = 4; vals[1] = 5; S s1 = S(vals, len); S s2 = s1; writeln(s2.vals[0]); writeln(s2.vals); writeln(s1.vals); } https://run.dlang.io/is/D0nfeT Yep, it is obvious that my code is wrong. s1 and s2 point to the same memory address. I could obtain my desired behavior with copy constructor. The documentation also say "WARNING: The postblit is considered legacy and is not recommended for new code. Code should use copy constructors defined in the previous section". struct S { int* vals; size_t length; this(ref return scope S another) { vals = cast(int*)malloc(another.length * S.sizeof); memcpy(vals, another.vals, another.length * S.sizeof); writeln("copied"); } this(int* vls, size_t len){ vals = vls; length = len; } ~this(){ free(vals); writeln("freedooom!"); } }
Re: A question about postblit constructor
On Tuesday, 5 November 2019 at 10:31:05 UTC, Ferhat Kurtulmuş wrote: On Tuesday, 5 November 2019 at 10:13:59 UTC, Mike Parker wrote: [...] Yep, it is obvious that my code is wrong. s1 and s2 point to the same memory address. I could obtain my desired behavior with copy constructor. The documentation also say "WARNING: The postblit is considered legacy and is not recommended for new code. Code should use copy constructors defined in the previous section". [...] edit: they should be s1.vals and s2.vals
Re: A question about postblit constructor
On Tuesday, 5 November 2019 at 10:32:03 UTC, Ferhat Kurtulmuş wrote: On Tuesday, 5 November 2019 at 10:31:05 UTC, Ferhat Kurtulmuş wrote: On Tuesday, 5 November 2019 at 10:13:59 UTC, Mike Parker wrote: [...] Yep, it is obvious that my code is wrong. s1 and s2 point to the same memory address. I could obtain my desired behavior with copy constructor. The documentation also say "WARNING: The postblit is considered legacy and is not recommended for new code. Code should use copy constructors defined in the previous section". I meant the example as an answer to your statement, "I wonder how new memory is allocated without an explicit malloc here". The postblit was intended as a chance to "fixup" everything when you needed a deep copy. The new struct is initialized as a shallow copy, so when you enter into the postblit, the pointer is already pointing at the original location. Without assigning it a new malloc'ed address, your memcpy was essentially overwriting the original location with its own data.
Re: A question about postblit constructor
On Tuesday, 5 November 2019 at 11:20:47 UTC, Mike Parker wrote: On Tuesday, 5 November 2019 at 10:32:03 UTC, Ferhat Kurtulmuş wrote: On Tuesday, 5 November 2019 at 10:31:05 UTC, Ferhat Kurtulmuş wrote: [...] I meant the example as an answer to your statement, "I wonder how new memory is allocated without an explicit malloc here". The postblit was intended as a chance to "fixup" everything when you needed a deep copy. The new struct is initialized as a shallow copy, so when you enter into the postblit, the pointer is already pointing at the original location. Without assigning it a new malloc'ed address, your memcpy was essentially overwriting the original location with its own data. What I need was a deep copy, and I thought I could have done it using postblit constructor. Thanks for clarification.
Re: A question about postblit constructor
On Tuesday, 5 November 2019 at 12:06:44 UTC, Ferhat Kurtulmuş wrote: On Tuesday, 5 November 2019 at 11:20:47 UTC, Mike Parker wrote: On Tuesday, 5 November 2019 at 10:32:03 UTC, Ferhat Kurtulmuş wrote: [...] I meant the example as an answer to your statement, "I wonder how new memory is allocated without an explicit malloc here". The postblit was intended as a chance to "fixup" everything when you needed a deep copy. The new struct is initialized as a shallow copy, so when you enter into the postblit, the pointer is already pointing at the original location. Without assigning it a new malloc'ed address, your memcpy was essentially overwriting the original location with its own data. What I need was a deep copy, and I thought I could have done it using postblit constructor. Thanks for clarification. I think copy constructor is less confusing though.
Re: A question about postblit constructor
On Tuesday, 5 November 2019 at 12:09:15 UTC, Ferhat Kurtulmuş wrote: On Tuesday, 5 November 2019 at 12:06:44 UTC, Ferhat Kurtulmuş wrote: On Tuesday, 5 November 2019 at 11:20:47 UTC, Mike Parker wrote: On Tuesday, 5 November 2019 at 10:32:03 UTC, Ferhat Kurtulmuş wrote: [...] I meant the example as an answer to your statement, "I wonder how new memory is allocated without an explicit malloc here". The postblit was intended as a chance to "fixup" everything when you needed a deep copy. The new struct is initialized as a shallow copy, so when you enter into the postblit, the pointer is already pointing at the original location. Without assigning it a new malloc'ed address, your memcpy was essentially overwriting the original location with its own data. What I need was a deep copy, and I thought I could have done it using postblit constructor. Thanks for clarification. I think copy constructor is less confusing though. Oh I see I have to do this to make a deep copy using postblit constructor: this(this){ int* _vals = cast(int*)malloc(length * S.sizeof); foreach(i; 0..length) _vals[i] = vals[i]; vals = _vals; writeln("copied"); }
GtkDcoding Blog Interruption
My apologies. I'm experiencing a technical problem with the GtkD Coding Blog site. Today's post has been uploaded, but the site isn't being updated properly, so the new page isn't available. Everything works fine locally, so for the moment, I'm stumped. Until I can resolve this, please bear with me. I'll keep you all updated of any progress.
Re: GtkDcoding Blog Interruption
On Tuesday, 5 November 2019 at 12:31:04 UTC, Ron Tarrant wrote: Until I can resolve this, please bear with me. I'll keep you all updated of any progress. I've been in touch with GitHub support and they're in a yellow alert situation, meaning (I assume) that service for all sites hosted on GitHub Pages will be degraded until that's cleared up.
Re: No UFCS with nested functions?
On Monday, 4 November 2019 at 20:46:41 UTC, H. S. Teoh wrote: On Mon, Nov 04, 2019 at 07:51:26PM +, Tobias Pankrath via Digitalmars-d-learn wrote: Why does the following not work? It works, if I move the 'prop' out of 'foo'. UFCS is only supported for module-level functions, as far as I know. --- struct S { ubyte[12] bar; } bool foo (ref S s) { static bool prop(const(ubyte)[] f) { return f.length > 1; } return s.bar[].prop; } --- [...] T Is this a necessary limitation? It feels inconsistent and clunky.
Re: No UFCS with nested functions?
On Tuesday, November 5, 2019 9:16:27 AM MST ixid via Digitalmars-d-learn wrote: > On Monday, 4 November 2019 at 20:46:41 UTC, H. S. Teoh wrote: > > On Mon, Nov 04, 2019 at 07:51:26PM +, Tobias Pankrath via > > > > Digitalmars-d-learn wrote: > >> Why does the following not work? It works, if I move the > >> 'prop' out of 'foo'. > > > > UFCS is only supported for module-level functions, as far as I > > know. > > > >> --- > >> struct S { > >> > >>ubyte[12] bar; > >> > >> } > >> > >> bool foo (ref S s) > >> { > >> > >>static bool prop(const(ubyte)[] f) { > >> > >> return f.length > 1; > >> > >>} > >> > >>return s.bar[].prop; > >> > >> } > >> --- > > > > [...] > > > > > > T > > Is this a necessary limitation? It feels inconsistent and clunky. It's explained at the end of this section of the documentation: https://dlang.org/spec/function.html#pseudo-member - Jonathan M Davis
Re: Dub and gitsubmodules
On Monday, 4 November 2019 at 13:37:47 UTC, Sebastiaan Koppe wrote: Does anyone know a reliable way of having a dub package that contains git submodules and is to be used as a dependency? I am looking for a way to ensure the submodules are initialised before a build. You can use the "preGenerateCommands" to launch the adequate git commands. The submodule needs to be indicated using the "path" property : "preGenerateCommands" : ["git submodule update --init --recursive"], "dependencies" : { "theGitSubModule" : { "path" : "./theGitSubModulePath" }, I have done this before and while it worked I had to "dub build" twice because the project needed to be read a second time to discover new source files generated by the commands. There's a PR that is supposed to fix that: https://github.com/dlang/dub/pull/1708 But more likely at some point git submodules will be supported natively.
Re: Dub and gitsubmodules
On Tuesday, 5 November 2019 at 19:26:54 UTC, user5678 wrote: On Monday, 4 November 2019 at 13:37:47 UTC, Sebastiaan Koppe wrote: Does anyone know a reliable way of having a dub package that contains git submodules and is to be used as a dependency? I am looking for a way to ensure the submodules are initialised before a build. You can use the "preGenerateCommands" to launch the adequate git commands. The submodule needs to be indicated using the "path" property : "preGenerateCommands" : ["git submodule update --init --recursive"], "dependencies" : { "theGitSubModule" : { "path" : "./theGitSubModulePath" }, I have done this before and while it worked I had to "dub build" twice because the project needed to be read a second time to discover new source files generated by the commands. There's a PR that is supposed to fix that: https://github.com/dlang/dub/pull/1708 But more likely at some point git submodules will be supported natively. I noticed 1 thing with preGenerateCommands. They are executed "every time" even if nothing has to be rebuild. The preBuildCommands are executed only if there is a build needed. The mentioned pr only takes care about the preGenerateCommands but not preBuildCommands. Kind regards Andre
Re: Dub and gitsubmodules
On Tuesday, 5 November 2019 at 20:28:49 UTC, Andre Pany wrote: On Tuesday, 5 November 2019 at 19:26:54 UTC, user5678 wrote: On Monday, 4 November 2019 at 13:37:47 UTC, Sebastiaan Koppe wrote: Does anyone know a reliable way of having a dub package that contains git submodules and is to be used as a dependency? I am looking for a way to ensure the submodules are initialised before a build. You can use the "preGenerateCommands" to launch the adequate git commands. The submodule needs to be indicated using the "path" property : "preGenerateCommands" : ["git submodule update --init --recursive"], "dependencies" : { "theGitSubModule" : { "path" : "./theGitSubModulePath" }, I have done this before and while it worked I had to "dub build" twice because the project needed to be read a second time to discover new source files generated by the commands. There's a PR that is supposed to fix that: https://github.com/dlang/dub/pull/1708 But more likely at some point git submodules will be supported natively. I noticed 1 thing with preGenerateCommands. They are executed "every time" even if nothing has to be rebuild. The preBuildCommands are executed only if there is a build needed. I didn't know that but I think this is necessary, isn't it ? If the command creates or update a few D sources then how would DUB knows that everything is up-to-date ? (in an ideal world, were the double dub build would not be necessary). For the purpose of pulling one can obviously optimize using more complex commands in a script. In my case I used the date of the last pull so that finally the submodule got pulled only once per day. The command is still executed but you save the useless web request. The mentioned pr only takes care about the preGenerateCommands but not preBuildCommands. Kind regards Andre
How polymorphism work in D?
I have this ``` import std.stdio : writeln; abstract class Foo { } class Bar : Foo { float value; this(float t_value) { value = t_value; } } class Baz : Foo { string name; this(string t_name) { name = t_name; } } void main() { Foo foo = new Bar(10); if (/* typeof foo == Bar */) foo.value.writeln; else if (/* typeof foo == Baz */) foo.name.writeln; foo.writeln; } ``` I don't understand how i can differentiate between Bar and Baz and to get their attributes
Re: How polymorphism work in D?
On 06/11/2019 6:43 PM, OiseuKodeur wrote: I have this ``` import std.stdio : writeln; abstract class Foo { } class Bar : Foo { float value; this(float t_value) { value = t_value; } } class Baz : Foo { string name; this(string t_name) { name = t_name; } } void main() { Foo foo = new Bar(10); if (/* typeof foo == Bar */) if (Bar bar = cast(Bar)foo) bar.value.writeln; foo.value.writeln; else if (/* typeof foo == Baz */) else if (Baz baz = cast(Baz)foo) baz.name.writeln; foo.name.writeln; foo.writeln; } ``` I don't understand how i can differentiate between Bar and Baz and to get their attributes
Re: How polymorphism work in D?
On Wednesday, 6 November 2019 at 06:05:25 UTC, rikki cattermole wrote: On 06/11/2019 6:43 PM, OiseuKodeur wrote: I have this ``` import std.stdio : writeln; abstract class Foo { } class Bar : Foo { float value; this(float t_value) { value = t_value; } } class Baz : Foo { string name; this(string t_name) { name = t_name; } } void main() { Foo foo = new Bar(10); if (/* typeof foo == Bar */) if (Bar bar = cast(Bar)foo) bar.value.writeln; foo.value.writeln; else if (/* typeof foo == Baz */) else if (Baz baz = cast(Baz)foo) baz.name.writeln; foo.name.writeln; foo.writeln; } ``` I don't understand how i can differentiate between Bar and Baz and to get their attributes thanks a lot