Re: Reduce parameters [was pi program]
On Fri, 2015-09-25 at 12:54 +, John Colvin via Digitalmars-d-learn wrote: > […] > I vastly prefer the UFCS version, but unfortunately reduce has > its arguments the wrong way around for that if you use the > version that takes a seed... In which case the reduce parameter list is wrong, this is a bug and should be fixed. Is there a bug report for this I can connect with? -- Russel. = Dr Russel Winder t: +44 20 7585 2200 voip: sip:russel.win...@ekiga.net 41 Buckmaster Roadm: +44 7770 465 077 xmpp: rus...@winder.org.uk London SW11 1EN, UK w: www.russel.org.uk skype: russel_winder signature.asc Description: This is a digitally signed message part
Re: Parallel processing and further use of output
Here's a correct version: import std.parallelism, std.range, std.stdio, core.atomic; void main() { shared ulong i = 0; foreach (f; parallel(iota(1, 100+1))) { i.atomicOp!"+="(f); } i.writeln; } Thanks! Works fine. So "shared" and "atomic" is a must?
Re: Parallel processing and further use of output
On Saturday, 26 September 2015 at 12:18:16 UTC, Zoidberg wrote: I've run into an issue, which I guess could be resolved easily, if I knew how... [CODE] ulong i = 0; foreach (f; parallel(iota(1, 100+1))) { i += f; } thread_joinAll(); i.writeln; [/CODE] It's basically an example which adds all the numbers from 1 to 100 and should therefore give 5050. Running the above code gives 205579930677, leaving out "thread_joinAll()" the output is 210161213519. I suspect there's some sort of data race. Any hint how to get this straight? Here's a correct version: import std.parallelism, std.range, std.stdio, core.atomic; void main() { shared ulong i = 0; foreach (f; parallel(iota(1, 100+1))) { i.atomicOp!"+="(f); } i.writeln; }
Re: Threading Questions
Sorry I don't know the answers but these questions are interesting so BUMP ;) On Friday, 25 September 2015 at 15:19:27 UTC, bitwise wrote: 1) Are the following two snippets exactly equivalent(not just in observable behaviour)? a) Mutex mut; mut.lock(); scope(exit) mut.unlock(); b) Mutex mut; synchronized(mut) { } Will 'synchronized' call 'lock' on the Mutex, or do something else(possibly related to the interface Object.Monitor)? Don't know. Is this Object monitor a mutex or something else? 6) Does 'shared' actually have any effect on non-global variables beside the syntactic regulations? Don't think so.
Why getting private member fails using getMember trait in a template?
Suppose we have, two modules: module testOne; import std.traits; template getMember(alias T, string member) { alias getMember = Identity!(__traits(getMember, T, member)); } module app; import testOne; import std.traits; class TestOne { private { int property; } public { int func() { return 0; } } } template getMember(alias T, string member) { alias getMember = Identity!(__traits(getMember, T, member)); } void main() { pragma(msg, fullyQualifiedName!(__traits(getMember, TestOne, "property"))); pragma(msg, fullyQualifiedName!(app.getMember!(TestOne, "property"))); pragma(msg, fullyQualifiedName!(testOne.getMember!(TestOne, "property"))); } First two statements execute and I get fully qualified name, while the third one fails with next error (dmd version v2.067.1): src/testOne.d(6): Error: class app.TestOne member property is not accessible src/app.d(26): Error: template instance testOne.getMember!(TestOne, "property") error instantiating src/app.d(26):while evaluating pragma(msg, fullyQualifiedName!(testOne.getMember!(TestOne, "property")))
Re: Mac IDE with Intellisense
On Saturday, 26 September 2015 at 09:17:10 UTC, Mike McKee wrote: I was doing toHexString(myByteArray) instead of simply doing myByteArray.toHexString(). (That was on an md5 example, by the way.) Intellisense would have helped me realize this. Both these forms are the same. It's called UFCS (uniform function call syntax). Here's some material to help you understand what's going on here: http://ddili.org/ders/d.en/ufcs.html http://nomad.so/2013/08/alternative-function-syntax-in-d/ Auto-complete in D is tricky because of this feature and no-one has invested any time to figure out a nice way to provide auto-complete for this. There is DCD by Brian Schott that looks very impressive but UFCS suggestions are not implemented yet. http://forum.dlang.org/post/hlrykiboijmtn...@forum.dlang.org
Re: Mac IDE with Intellisense
On 26/09/15 9:17 PM, Mike McKee wrote: I've tried Sublime Text 3 editor on the Mac, but even it doesn't seem to have the D2 language in it yet (only D), and doesn't have intellisense for components in the imports that I do, even after saving the file after adding the import statements. What OSX editor do you recommend that would have intellisense? In all reality, I don't like intellisense -- it's annoying. However, I need it because the documentation for me is still a little hard to read and hard for me to search for a class method here or there. For instance, I was doing toHexString(myByteArray) instead of simply doing myByteArray.toHexString(). (That was on an md5 example, by the way.) Intellisense would have helped me realize this. Try Mono-D.
Re: Parallel processing and further use of output
On Saturday, 26 September 2015 at 12:33:45 UTC, anonymous wrote: foreach (f; parallel(iota(1, 100+1))) { synchronized i += f; } Is this valid syntax? I've never seen synchronized used like this before.
Mac IDE with Intellisense
I've tried Sublime Text 3 editor on the Mac, but even it doesn't seem to have the D2 language in it yet (only D), and doesn't have intellisense for components in the imports that I do, even after saving the file after adding the import statements. What OSX editor do you recommend that would have intellisense? In all reality, I don't like intellisense -- it's annoying. However, I need it because the documentation for me is still a little hard to read and hard for me to search for a class method here or there. For instance, I was doing toHexString(myByteArray) instead of simply doing myByteArray.toHexString(). (That was on an md5 example, by the way.) Intellisense would have helped me realize this.
Re: Mac IDE with Intellisense
On Saturday, 26 September 2015 at 09:17:10 UTC, Mike McKee wrote: I've tried Sublime Text 3 editor on the Mac, but even it doesn't seem to have the D2 language in it yet (only D), and doesn't have intellisense for components in the imports that I do, even after saving the file after adding the import statements. What OSX editor do you recommend that would have intellisense? In all reality, I don't like intellisense -- it's annoying. However, I need it because the documentation for me is still a little hard to read and hard for me to search for a class method here or there. For instance, I was doing toHexString(myByteArray) instead of simply doing myByteArray.toHexString(). (That was on an md5 example, by the way.) Intellisense would have helped me realize this. Have you installed dkit for sublime?
Re: Parallel processing and further use of output
On Saturday 26 September 2015 14:18, Zoidberg wrote: > I've run into an issue, which I guess could be resolved easily, > if I knew how... > > [CODE] > ulong i = 0; > foreach (f; parallel(iota(1, 100+1))) > { > i += f; > } > thread_joinAll(); > i.writeln; > [/CODE] > > It's basically an example which adds all the numbers from 1 to > 100 and should therefore give 5050. Running the above > code gives 205579930677, leaving out "thread_joinAll()" the > output is 210161213519. > > I suspect there's some sort of data race. Any hint how to get > this straight? Definitely a race, yeah. You need to prevent two += operations happening concurrently. You can use core.atomic.atomicOp!"+=" instead of plain +=: shared ulong i = 0; foreach (f; parallel(iota(1, 100+1))) { import core.atomic: atomicOp; i.atomicOp!"+="(f); } i is shared because atomicOp requires a shared variable. I'm not sure what the implications of that are, if any. Alternatively, you could use `synchronized`: ulong i = 0; foreach (f; parallel(iota(1, 100+1))) { synchronized i += f; } I'm pretty sure atomicOp is faster, though.
Parallel processing and further use of output
I've run into an issue, which I guess could be resolved easily, if I knew how... [CODE] ulong i = 0; foreach (f; parallel(iota(1, 100+1))) { i += f; } thread_joinAll(); i.writeln; [/CODE] It's basically an example which adds all the numbers from 1 to 100 and should therefore give 5050. Running the above code gives 205579930677, leaving out "thread_joinAll()" the output is 210161213519. I suspect there's some sort of data race. Any hint how to get this straight?
Re: Reduce parameters [was pi program]
On Saturday, 26 September 2015 at 06:28:22 UTC, Russel Winder wrote: On Fri, 2015-09-25 at 12:54 +, John Colvin via Digitalmars-d-learn wrote: […] I vastly prefer the UFCS version, but unfortunately reduce has its arguments the wrong way around for that if you use the version that takes a seed... In which case the reduce parameter list is wrong, this is a bug and should be fixed. Is there a bug report for this I can connect with? It's been argued about a lot. https://issues.dlang.org/show_bug.cgi?id=8755 https://github.com/D-Programming-Language/phobos/pull/861 https://github.com/D-Programming-Language/phobos/pull/1955 https://github.com/D-Programming-Language/phobos/pull/2033
Re: Parallel processing and further use of output
On Saturday, 26 September 2015 at 13:09:54 UTC, Meta wrote: On Saturday, 26 September 2015 at 12:33:45 UTC, anonymous wrote: foreach (f; parallel(iota(1, 100+1))) { synchronized i += f; } Is this valid syntax? I've never seen synchronized used like this before. I'm sure it's valid. A mutex is created for that instance of synchronized. I.e., only one thread can execute that piece of code at a time. If you're missing the braces, they're optional for single statements, as usual. http://dlang.org/statement.html#SynchronizedStatement
Re: Mac IDE with Intellisense
On Saturday, 26 September 2015 at 10:31:13 UTC, wobbles wrote: Have you installed dkit for sublime? As in? https://github.com/yazd/DKit Looks like it's alpha and doesn't run on Mac? No homebrew install?
Re: Purity of std.conv.to!string
On Saturday, 26 September 2015 at 17:08:00 UTC, Nordlöw wrote: Why is the following code not pure: float x = 3.14; import std.conv : to; auto y = x.to!string; ??? Is there a reason for it not being pure? If not, this is a serious problem as this is such a fundamental function. I don't know the exact reason but I found a couple of functions which could be marked pure which are not, including strippedOctalLiteral and isOctalLiteralString. It's possible that some function was simply overlooked and needs to be marked pure or infer purity. The larger issue at hand is that the compiler doesn't tell you where an impurity lies, merely that it exists. I mentioned this issue not too long ago while experiencing my own difficulties respecting purity.
Re: Parallel processing and further use of output
btw, on my corei5, in debug build, reduce (using double): 11msec non_parallel: 37msec parallel with atomicOp: 123msec so, that is the reason for using parallel reduce, assuming the ulong range thing will get fixed.
Re: Mac IDE with Intellisense
On Saturday, 26 September 2015 at 10:38:29 UTC, Gary Willoughby wrote: Both these forms are the same. It's called UFCS (uniform function call syntax). Here's some material to help you understand what's going on here: http://ddili.org/ders/d.en/ufcs.html http://nomad.so/2013/08/alternative-function-syntax-in-d/ Noted, and thanks.
Re: Purity of std.conv.to!string
On Saturday, 26 September 2015 at 17:08:00 UTC, Nordlöw wrote: Why is the following code not pure: float x = 3.14; import std.conv : to; auto y = x.to!string; ??? Is there a reason for it not being pure? If not, this is a serious problem as this is such a fundamental function. Maybe because of floating point numbers uncertainty that would cause the resulting string to be different for two equivalent inputs? I can't seem to put an example together though.
Purity of std.conv.to!string
Why is the following code not pure: float x = 3.14; import std.conv : to; auto y = x.to!string; ??? Is there a reason for it not being pure? If not, this is a serious problem as this is such a fundamental function.
Re: Parallel processing and further use of output
This is a work-around to get a ulong result without having the ulong as the range variable. ulong getTerm(int i) { return i; } auto sum4 = taskPool.reduce!"a + b"(std.algorithm.map!getTerm(iota(11)));
Re: Parallel processing and further use of output
On Saturday, 26 September 2015 at 17:20:34 UTC, Jay Norwood wrote: This is a work-around to get a ulong result without having the ulong as the range variable. ulong getTerm(int i) { return i; } auto sum4 = taskPool.reduce!"a + b"(std.algorithm.map!getTerm(iota(11))); or auto sum4 = taskPool.reduce!"a + b"(0UL, iota(1_000_000_001)); works for me
Re: Parallel processing and further use of output
std.parallelism.reduce documentation provides an example of a parallel sum. This works: auto sum3 = taskPool.reduce!"a + b"(iota(1.0,101.0)); This results in a compile error: auto sum3 = taskPool.reduce!"a + b"(iota(1UL,101UL)); I believe there was discussion of this problem recently ...
Re: Parallel processing and further use of output
On Saturday, 26 September 2015 at 13:09:54 UTC, Meta wrote: On Saturday, 26 September 2015 at 12:33:45 UTC, anonymous wrote: foreach (f; parallel(iota(1, 100+1))) { synchronized i += f; } Is this valid syntax? I've never seen synchronized used like this before. Atomic worked perfectly and reasonably fast. "Synchronized" may work as well, but I had to abort the execution prior to finishing because it seemed horribly slow.
Re: Purity of std.conv.to!string
On Saturday, 26 September 2015 at 17:08:00 UTC, Nordlöw wrote: Why is the following code not pure: float x = 3.14; import std.conv : to; auto y = x.to!string; ??? Is there a reason for it not being pure? If not, this is a serious problem as this is such a fundamental function. Please make an issue on https://issues.dlang.org and I'll take a look a this later. Most of the functions in std.conv are templated so it must be some internal function that's not properly annotated, or it's using manual memory management.
Re: Mac IDE with Intellisense
On Saturday, 26 September 2015 at 09:17:10 UTC, Mike McKee wrote: I've tried Sublime Text 3 editor on the Mac, but even it doesn't seem to have the D2 language in it yet (only D), and doesn't have intellisense for components in the imports that I do, even after saving the file after adding the import statements. What OSX editor do you recommend that would have intellisense? I use mono-d on win32 and OS X and am happy with it: http://wiki.dlang.org/Mono-D --Stephan