ipython/jupyter notebook - idea for making it a full REPL
At the moment, thanks to John Colvin's work, you can write D in an ipython/Jupyter notebook. I find it a nicer work flow for playing around with things, since you can see results inline, and iterate rapidly. In theory maybe no better than having your editor/IDE hooked up, but the difference between theory and practice is greater in practice than in theory... It's early stage, so there are no line numbers, and hard to see which bit of code a compile error refers to. But still quite useable. You can hook up your own (or code.dlang.org) libraries via dub arguments passed in the notebook. So same idea as python - your libraries do all the work and you write some light scripting in the notebook in an iterative way as you see the results evolve. And of course you can call bash, lua, redis etc from within the notebook. One thing that might be helpful is to turn it into a proper D REPL. At the moment if you write: %%pyd writefln(hello world); It won't go well. You need the imports and a function wrapper. That doesn't matter in this case, but it would be nice to be able to write immediate code that retains state without rerunning from scratch. so for example %%pyd @pdef auto getApple() { bars=priceBars(NASDAQ/AAPL); } you can in python do: bars=getApple() and then if the auto conversion works, or you have written something bars will be a python object that you can operate on - display, chart etc. but it would be nice to be able to write pure D code in REPL mode within the notebook. so: %%pyd auto bars=priceBars(NASDAQ/AAPL); and then in next cell %%pyd bars=bars[$-100..$]l; bars.chartOHLC; so you would have a REPL as we do already, but with all the features of Jupyter. I had an idea about how to do this, but I am sure it could be improved. More here: https://github.com/DlangScience/PydMagic/issues/21
Re: Template-Parameterized Variadic isInstaceOf
On Friday, 7 August 2015 at 11:45:22 UTC, Nordlöw wrote: Can somebody please explain and help out with variadic version of `isInstanceOf`? Here's a step forward: /** Returns true if $(D T) is an instance of the template $(D T) with template parameters $(D Ps). */ enum bool isInstanceOf(alias S, T, Ps...) = is(T == S!Ps); /// unittest { struct SortedRange(Range, alias pred = a b) { } alias R = int[]; alias SR = SortedRange!(R, a b); static assert(isInstanceOf!(SortedRange, SR, R, a b)); } 1. This, however, requires *all* the template parameters to be given. What I need now is a syntax to check that, in this case, *only* the second template argument `pred` matches. How do I do that? 2. Note also that this solution doesn't understand that ab and a b are semantically equivalent. Forcing usage of `binaryFun` is a temporary solution. Is there a better solution?
Re: Creating a Priority Queue: An Adventure
Okay, so, I decided to scrap the BinaryHeap version of the priority queue, going back to basics and utilizing a simple array. It works, huzzah! Code: module data_structures.priority_queue; import std.array; import std.range: assumeSorted; import std.typecons: Tuple; /* Templated Priority Queue Usage: PriorityQueue!(PRIORITY_TYPE, VALUE_TYPE, OPTIONAL_PREDICATE) */ struct PriorityQueue(P, V, alias predicate = a b) { // To make the code a bit more readable alias Tuple!(P, V) PV; PV _q[]; // Forward most function calls to the underlying array. PV[]* opDot() { return _q; } // Determine if the queue is empty @property bool empty () { return (_q.length == 0); } // Needed so foreach can work @property PV front() { return _q.front; } // Chop off the front of the array @property void popFront() { _q = _q[1 .. $]; } // Insert a record via a template tuple void insert(ref PV rec) { // Empty queue? if (_q.length == 0 ) { // just put the record into the queue _q ~= rec; return; } // Assume the queue is already sorted according to PREDICATE auto a = assumeSorted!(predicate)(_q); // Find a slice containing records with priorities less than the insertion rec auto p = a.lowerBound(rec); int location = p.length; // Insert the record _q.insertInPlace(location, rec); } void insert(PV rec) { insert(rec); } // Insert a record via decomposed priority and value void insert(P priority, V value) { PV rec = PV(priority, value); // Insert the record insert(rec); } // Merge two Priority Queues, returning the merge. // The two queues must obviously be of the same type in Priority and Value, and predicate; ref PriorityQueue!(P, V, predicate) merge(ref PriorityQueue!(P, V, predicate) qmerge) { // Make a copy of this PriorityQueue PriorityQueue!(P, V, predicate)* qreturn = new PriorityQueue!(P, V, predicate); qreturn._q = _q.dup; // Add in all the elements of the merging queue foreach(rec; qmerge) { qreturn.insert(rec); } // Return the resulting merged queue return *qreturn; } } unittest { alias P = int; alias V = string; alias PV = Tuple!(P, V); alias PQ = PriorityQueue!(P, V, a b); PQ pq, pq2, pq3; import std.typecons: tuple; // Test basic insertion pq.insert(10, HELLO10); pq.insert(11, HELLO11); pq.insert(3, HELLO3); pq.insert(31, HELLO31); pq.insert(5, HELLO5); pq.insert(10, HELLO10-2); assert(pq.length == 6); foreach (const e; pq) {}// iteration assert(!pq.empty); // shouldn't consume queue // Copy by value pq2 = pq; foreach (priority, value; pq) { pq.popFront(); } // pq and pq2 should be independent assert( !pq2.empty); assert( pq.empty ); // Test merging pq3.insert(tuple(12, HELLO12)); pq3.insert(Tuple!(int, string)(17, HELLO17)); pq3.insert(tuple(7, HELLO7)); pq = pq2.merge(pq3); assert ( !pq.empty); assert(pq.front == tuple(3, HELLO3)); pq.popFront; assert(pq.front == tuple(5, HELLO5)); pq.popFront; assert(pq.front == tuple(7, HELLO7)); pq.popFront; assert( pq.length == 6 ); } And a little driver main() to illustrate the queue a bit better: main() { PriorityQueue!(int, string) pq, pq2, pq3; pq.insert(10, HELLO10); pq.insert(11, HELLO11); pq.insert(Tuple!(int, string)(3, HELLO3)); pq.insert(5, HELLO5); pq.insert(Tuple!(int, string)(12, HELLO12)); pq.insert(Tuple!(int, string)(17, HELLO17)); pq2.insert(Tuple!(int, string)(15, HELLO15)); pq2.insert(Tuple!(int, string)(21, HELLO21)); writefln(\tPQ: %s \n\tPQ2: %s \n\tPQ3: %s, pq, pq2, pq3); pq3 = pq.merge(pq2); foreach(priority, value; pq3) { writefln(Priority: %s \tValue: %s \tLength: %s, priority, value, pq3.length);
Using std.random.uniform as a range
What is the best way to create range from uniform() function (in other words create a generator based on some function, returning, say, scalar, not range)? I did http://dpaste.dzfl.pl/53e3d9255cd7 but I'm not sure it's the best way. At least sequence using looks ugly
[dmd2.068] Bug or future?
I have some code: import std.stdio; auto dot(T, R)(T x, R y) { return x * y; } struct Vector(T) { alias selftype = Vector!T; int len = 5; pure: const @property{ static if( is( typeof( dot( selftype.init, selftype.init ) ) ) ){ auto len2() {return len * len;} } static if(is(typeof(T.init * T.init) == T)) { auto e() {return len;} } } } void main() { Vector!(float) vec; float x = vec.len2(); writeln(x); x = vec.e(); writeln(x); } I get error by compiler when i build this: main.d(30): Error: no property 'len2' for type 'Vector!float', did you mean 'len'? In dmd 2.067 is normaly. is it Bug or enhancements?
zlib performance
hi, unpacking files is kinda slow, probably i'm doing something wrong. below code is about half the speed of gnu zcat on my os x machine. why? why do i need to .dup the buffer? can i get rid of the casts? the chunk size has only a marginal influence. https://github.com/yannick/zcatd import std.zlib, std.file, std.stdio; void main(string[] args) { auto f = File(args[1], r); auto uncompressor = new UnCompress(HeaderFormat.gzip); foreach (ubyte[] buffer; f.byChunk(4096)) { auto uncompressed = cast(immutable(string)) uncompressor.uncompress(buffer.dup); write(uncompressed); } }
Re: Find on sorted range slower?
On Friday, 7 August 2015 at 05:21:32 UTC, Tofu Ninja wrote: HAHAH wow, this is hilarious, I just checked, nothing in std.algo takes advantage of sorted ranges, sort doesn't even take advantage of it! You pass a sorted range into sort and it will just resort it! Wow Who fixes this? I can look into it... is there an issue for this?
Re: Find on sorted range slower?
On Friday, 7 August 2015 at 08:18:04 UTC, Nordlöw wrote: On Friday, 7 August 2015 at 05:21:32 UTC, Tofu Ninja wrote: HAHAH wow, this is hilarious, I just checked, nothing in std.algo takes advantage of sorted ranges, sort doesn't even take advantage of it! You pass a sorted range into sort and it will just resort it! Wow Who fixes this? I can look into it... is there an issue for this? https://issues.dlang.org/show_bug.cgi?id=11667
Re: [dmd2.068] Bug or future?
On 08/06/2015 11:26 PM, VlasovRoman wrote: I have some code: Filed: https://issues.dlang.org/show_bug.cgi?id=14883 Ali
assigning a struct object to an array
Hello I just began to learn D. I have some experience with Visual Basic and (very little) C/C++. Last years I have been working with PHP. So, I try to make up a class representation of a 52 cards deck. This way : // sabot.d // version 0.0.1 import std.stdio ; void main(){ auto deck = new sabot ; } class sabot{ carte[] sabotarray ; this(){ int i ; for (i=1 ; i=52 ; i++){ carte tempcarte ; tempcarte.id = i ; sabotarray[] ~= tempcarte ; // line 17 } writeln(this.sabotarray[1]) ; } struct carte { int id ; string valeur_face ; int valeur_reelle ; int couleur ; } } But I get this error : sabot.d(17): Error: cannot append type carte to type carte I dont understand. Both sabaotarray[] and tempcarte are declared as carte type error message confirms). Why aren't they compatible ? I tryed different things, but didn't get it work. I can solve the problem by declaring a static array. But I would like to understand why this dont work. Thank you
Re: zlib performance
On Friday, 7 August 2015 at 08:50:11 UTC, Daniel Kozák wrote: ldc[2] -O -release -boundscheck=off -singleobj app.d ldc 0.15.2 beta2 2.86s user 0.55s system 77% cpu 4.392 total v2.068-devel-8f81ffc 2.86s user 0.67s system 78% cpu 4.476 total v2.067 2.88s user 0.67s system 78% cpu 4.529 total i can now reproduce the results and indeed, its faster than zcat: on a c4.xlarge aws instance running archlinux and dmd v2.067 same file as above on my macbook. best run: 2.72s user 0.39s system 99% cpu 3.134 total worst run: 3.47s user 0.46s system 99% cpu 3.970 total zcat: best: 4.45s user 0.28s system 99% cpu 4.764 total worst: 4.99s user 0.57s system 99% cpu 5.568 total so i guess on os x there is still something to be optimized
Re: [dmd2.068] Bug or future?
On 08/06/2015 11:26 PM, VlasovRoman wrote: I have some code: Reduced: import std.stdio; auto foo(T)(T) { return 42; } struct Vector(T) { pragma(msg, is(typeof(foo(Vector.init;// prints true static if(is(typeof(foo(Vector.init { static assert(false); // is not included } } void main() { Vector!float v; } 'static if' fails to include code even though the is expression produces 'true'. I think this is a regression. If others agree let's create a regression quickly before tomorrow's (today's?) planned release. Ali
Re: zlib performance
On Fri, 07 Aug 2015 08:42:45 + yawniek dl...@srtnwz.com wrote: On Friday, 7 August 2015 at 08:24:11 UTC, Daniel Kozák wrote: can you try it with ldc? ldc[2] -O -release -boundscheck=off -singleobj app.d ldc 0.15.2 beta2 2.86s user 0.55s system 77% cpu 4.392 total v2.068-devel-8f81ffc 2.86s user 0.67s system 78% cpu 4.476 total v2.067 2.88s user 0.67s system 78% cpu 4.529 total (different file, half the size of the one above:) archlinux, virtualbox vm, DMD64 D Compiler v2.067 real 0m2.079s user 0m1.193s sys 0m0.637s zcat: real 0m3.023s user 0m0.320s sys 0m2.440s is there a way to get rid of the flush in the end so everything happens in one loop? its a bit inconvenient when i have another subloop that does work I am not sure but I do not tkink so, it is currently possible. Btw. If you want to remove [i]dup. you can use this code: http://dpaste.dzfl.pl/f52c82935bb5
Re: [dmd2.068] Bug or future?
On Friday, 7 August 2015 at 06:26:21 UTC, VlasovRoman wrote: I have some code: import std.stdio; auto dot(T, R)(T x, R y) { return x * y; } struct Vector(T) { alias selftype = Vector!T; int len = 5; pure: const @property{ static if( is( typeof( dot( selftype.init, selftype.init ) ) ) ){ auto len2() {return len * len;} } static if(is(typeof(T.init * T.init) == T)) { auto e() {return len;} } } } void main() { Vector!(float) vec; float x = vec.len2(); writeln(x); x = vec.e(); writeln(x); } I get error by compiler when i build this: main.d(30): Error: no property 'len2' for type 'Vector!float', did you mean 'len'? In dmd 2.067 is normaly. is it Bug or enhancements? Does not work in 2.067 for me. Btw. you do not need to do this: alias selftype = Vector!T; You can just use Vector, or: alias selftype = Vector; if you prefer selftype as a name.
Re: zlib performance
On Friday, 7 August 2015 at 07:29:15 UTC, Daniel Kozák wrote: Which compiler and version. There has been some performance problem with IO on OSX, it should be fixed in 2.068 release i'm on master. v2.068-devel-8f81ffc also changed file read mode to rb. i don't understand why the program crashes when i do not do the .dup
Re: zlib performance
On Fri, 07 Aug 2015 08:13:01 + yawniek dl...@srtnwz.com wrote: On Friday, 7 August 2015 at 08:05:01 UTC, Daniel Kozák wrote: import std.zlib, std.file, std.stdio, std.conv; void main(string[] args) { auto f = File(args[1], rb); auto uncompressor = new UnCompress(HeaderFormat.gzip); foreach (buffer; f.byChunk(4096)) { auto uncompressed = cast(char[])(uncompressor.uncompress(buffer.idup)); write(uncompressed); } write(cast(char[])uncompressor.flush); } this is faster for me than zcat not here on os x: d version: 3.06s user 1.17s system 82% cpu 5.156 total gzcat 1.79s user 0.11s system 99% cpu 1.899 total Maybe stil some IO issues. On Linux it is OK. I remmeber a few days ago there has been some discussion about IO improvments fo osx. http://forum.dlang.org/post/mailman.184.1437841312.16005.digitalmar...@puremagic.com
Re: zlib performance
On Fri, 07 Aug 2015 08:13:01 + yawniek dl...@srtnwz.com wrote: On Friday, 7 August 2015 at 08:05:01 UTC, Daniel Kozák wrote: import std.zlib, std.file, std.stdio, std.conv; void main(string[] args) { auto f = File(args[1], rb); auto uncompressor = new UnCompress(HeaderFormat.gzip); foreach (buffer; f.byChunk(4096)) { auto uncompressed = cast(char[])(uncompressor.uncompress(buffer.idup)); write(uncompressed); } write(cast(char[])uncompressor.flush); } this is faster for me than zcat not here on os x: d version: 3.06s user 1.17s system 82% cpu 5.156 total gzcat 1.79s user 0.11s system 99% cpu 1.899 total can you try it with ldc? ldc[2] -O -release -boundscheck=off -singleobj app.d
Re: assigning a struct object to an array
On 08/07/2015 02:05 AM, Reflexive wrote: class sabot{ carte[] sabotarray ; this(){ int i ; for (i=1 ; i=52 ; i++){ carte tempcarte ; tempcarte.id = i ; sabotarray[] ~= tempcarte ; // line 17 dmd 2.068 gives a better message: Error: slice expression this.sabotarray[] is not a modifiable lvalue Just drop the []: sabotarray ~= tempcarte; After all, the symbol 'sabotarray' represents the array and you want to append to it. On the other hand, sabotarray[] is a temporary slice, which happens to be an rvalue (read: not modifiable). You don't want to append to that temporary. Ali
Re: zlib performance
On Fri, 07 Aug 2015 07:19:43 + yawniek via Digitalmars-d-learn digitalmars-d-learn@puremagic.com wrote: hi, unpacking files is kinda slow, probably i'm doing something wrong. below code is about half the speed of gnu zcat on my os x machine. why? why do i need to .dup the buffer? can i get rid of the casts? the chunk size has only a marginal influence. https://github.com/yannick/zcatd import std.zlib, std.file, std.stdio; void main(string[] args) { auto f = File(args[1], r); auto uncompressor = new UnCompress(HeaderFormat.gzip); foreach (ubyte[] buffer; f.byChunk(4096)) { auto uncompressed = cast(immutable(string)) uncompressor.uncompress(buffer.dup); write(uncompressed); } } Which compiler and version. There has been some performance problem with IO on OSX, it should be fixed in 2.068 release
Re: zlib performance
On Fri, 07 Aug 2015 07:36:39 + yawniek dl...@srtnwz.com wrote: On Friday, 7 August 2015 at 07:29:15 UTC, Daniel Kozák wrote: Which compiler and version. There has been some performance problem with IO on OSX, it should be fixed in 2.068 release i'm on master. v2.068-devel-8f81ffc also changed file read mode to rb. i don't understand why the program crashes when i do not do the .dup This is weird. I would say it should not crash
Re: zlib performance
On Fri, 07 Aug 2015 07:19:43 + yawniek dl...@srtnwz.com wrote: hi, unpacking files is kinda slow, probably i'm doing something wrong. below code is about half the speed of gnu zcat on my os x machine. why? why do i need to .dup the buffer? It depends. In your case you don't need to. byChunk() reuse buffer which means, after each call same buffer is use, so all previous data are gone. can i get rid of the casts? Yes, you can use std.conv.to import std.zlib, std.file, std.stdio, std.conv; void main(string[] args) { auto f = File(args[1], rb); auto uncompressor = new UnCompress(HeaderFormat.gzip); foreach (buffer; f.byChunk(4096)) { auto uncompressed = to!(char[])(uncompressor.uncompress(buffer)); write(uncompressed); } }
Re: zlib performance
On Friday, 7 August 2015 at 07:43:25 UTC, Daniel Kozák wrote: i don't understand why the program crashes when i do not do the .dup This is weird. I would say it should not crash exactely. but try it yourself. the fastest version i could come up so far is below. std.conv slows it down. going from a 4kb to a 4mb buffer helped. now i'm within 30% of gzcat's performance. import std.zlib, std.file, std.stdio; void main(string[] args) { auto f = File(args[1], rb); auto uncompressor = new UnCompress(HeaderFormat.gzip); foreach (ubyte[] buffer; f.byChunk(1024*1024*4)) { auto uncompressed = cast(immutable(string)) uncompressor.uncompress(buffer.dup); write(uncompressed); } }
Re: zlib performance
On Fri, 07 Aug 2015 08:01:27 + yawniek dl...@srtnwz.com wrote: On Friday, 7 August 2015 at 07:48:25 UTC, yawniek wrote: On Friday, 7 August 2015 at 07:43:25 UTC, Daniel Kozák wrote: the fastest version i could come up so far is below. std.conv slows it down. going from a 4kb to a 4mb buffer helped. now i'm within 30% of gzcat's performance. ok maybe not, there is another problem, not everything seems to get flushed, i'm missing output import std.zlib, std.file, std.stdio, std.conv; void main(string[] args) { auto f = File(args[1], rb); auto uncompressor = new UnCompress(HeaderFormat.gzip); foreach (buffer; f.byChunk(4096)) { auto uncompressed = cast(char[])(uncompressor.uncompress(buffer.idup)); write(uncompressed); } write(cast(char[])uncompressor.flush); } this is faster for me than zcat
Re: zlib performance
On Friday, 7 August 2015 at 07:48:25 UTC, yawniek wrote: On Friday, 7 August 2015 at 07:43:25 UTC, Daniel Kozák wrote: the fastest version i could come up so far is below. std.conv slows it down. going from a 4kb to a 4mb buffer helped. now i'm within 30% of gzcat's performance. ok maybe not, there is another problem, not everything seems to get flushed, i'm missing output
Re: zlib performance
On Fri, 7 Aug 2015 09:43:25 +0200 Daniel Kozák ko...@dlang.cz wrote: On Fri, 07 Aug 2015 07:36:39 + yawniek dl...@srtnwz.com wrote: On Friday, 7 August 2015 at 07:29:15 UTC, Daniel Kozák wrote: Which compiler and version. There has been some performance problem with IO on OSX, it should be fixed in 2.068 release i'm on master. v2.068-devel-8f81ffc also changed file read mode to rb. i don't understand why the program crashes when i do not do the .dup This is weird. I would say it should not crash Ok I see it is not weird because Uncompressor class probably has slice to buffer
Re: zlib performance
On Friday, 7 August 2015 at 08:05:01 UTC, Daniel Kozák wrote: import std.zlib, std.file, std.stdio, std.conv; void main(string[] args) { auto f = File(args[1], rb); auto uncompressor = new UnCompress(HeaderFormat.gzip); foreach (buffer; f.byChunk(4096)) { auto uncompressed = cast(char[])(uncompressor.uncompress(buffer.idup)); write(uncompressed); } write(cast(char[])uncompressor.flush); } this is faster for me than zcat not here on os x: d version: 3.06s user 1.17s system 82% cpu 5.156 total gzcat 1.79s user 0.11s system 99% cpu 1.899 total
Re: zlib performance
On Friday, 7 August 2015 at 08:24:11 UTC, Daniel Kozák wrote: can you try it with ldc? ldc[2] -O -release -boundscheck=off -singleobj app.d ldc 0.15.2 beta2 2.86s user 0.55s system 77% cpu 4.392 total v2.068-devel-8f81ffc 2.86s user 0.67s system 78% cpu 4.476 total v2.067 2.88s user 0.67s system 78% cpu 4.529 total (different file, half the size of the one above:) archlinux, virtualbox vm, DMD64 D Compiler v2.067 real0m2.079s user0m1.193s sys 0m0.637s zcat: real0m3.023s user0m0.320s sys 0m2.440s is there a way to get rid of the flush in the end so everything happens in one loop? its a bit inconvenient when i have another subloop that does work
Re: Find on sorted range slower?
On Friday, 7 August 2015 at 08:18:04 UTC, Nordlöw wrote: On Friday, 7 August 2015 at 05:21:32 UTC, Tofu Ninja wrote: HAHAH wow, this is hilarious, I just checked, nothing in std.algo takes advantage of sorted ranges, sort doesn't even take advantage of it! You pass a sorted range into sort and it will just resort it! Wow Who fixes this? I can look into it... is there an issue for this? I have no idea, but it is pretty silly. Sort/isSorted on a sorted range should be a nop. Find and friends, should do doing some kind of binary search. Max and min should be O(1). Some of the functions that return a sub range or a mutated range could probably be returning sorted ranges as well if its input is a sorted range, remove, strip and split at least could.
Re: Find on sorted range slower?
On Friday, 7 August 2015 at 10:01:39 UTC, Timon Gehr wrote: On 08/07/2015 11:03 AM, Tofu Ninja wrote: On Friday, 7 August 2015 at 08:18:04 UTC, Nordlöw wrote: On Friday, 7 August 2015 at 05:21:32 UTC, Tofu Ninja wrote: HAHAH wow, this is hilarious, I just checked, nothing in std.algo takes advantage of sorted ranges, sort doesn't even take advantage of it! You pass a sorted range into sort and it will just resort it! Wow Who fixes this? I can look into it... is there an issue for this? I have no idea, but it is pretty silly. Sort/isSorted on a sorted range should be a nop. Find and friends, should do doing some kind of binary search. Max and min should be O(1). Some of the functions that return a sub range or a mutated range could probably be returning sorted ranges as well if its input is a sorted range, remove, strip and split at least could. Binary search is not always faster than linear search. It will be for the majority of sorted ranges. Though if other searches are needed, find and friends could take an extra arg SearchPolicy for sorted ranges that defaults to binary search.
Re: Find on sorted range slower?
On 08/07/2015 11:03 AM, Tofu Ninja wrote: On Friday, 7 August 2015 at 08:18:04 UTC, Nordlöw wrote: On Friday, 7 August 2015 at 05:21:32 UTC, Tofu Ninja wrote: HAHAH wow, this is hilarious, I just checked, nothing in std.algo takes advantage of sorted ranges, sort doesn't even take advantage of it! You pass a sorted range into sort and it will just resort it! Wow Who fixes this? I can look into it... is there an issue for this? I have no idea, but it is pretty silly. Sort/isSorted on a sorted range should be a nop. Find and friends, should do doing some kind of binary search. Max and min should be O(1). Some of the functions that return a sub range or a mutated range could probably be returning sorted ranges as well if its input is a sorted range, remove, strip and split at least could. Binary search is not always faster than linear search.
Re: Template-Parameterized Variadic isInstaceOf
On Friday, 7 August 2015 at 11:45:22 UTC, Nordlöw wrote: Can somebody please explain and help out with variadic version of `isInstanceOf`? Here's a try at isSortedRange: enum bool isSortedRange(T, alias pred = a b) = is(T == SortedRange!(Args[0], pred), Args...); unittest { alias R = int[]; enum pred = a b; static assert(isSortedRange!(SortedRange!(R, pred), pred)); } but it fails. This simplified case enum bool isSortedRange(T, alias pred = a b) = is(T == SortedRange!Args, Args...); works. How do check that the second template argument to the instance of SortedRange matches `pred`?
Re: Concurrency Confusion
On Thursday, 6 August 2015 at 21:17:15 UTC, 岩倉 澪 wrote: On Tuesday, 4 August 2015 at 08:35:10 UTC, Dicebot wrote: // in real app use `receiveTimeout` to do useful stuff until // result message is received auto output = receiveOnly!(immutable(Bar)[]); New question: how would I receive a immutable value with receiveTimeout? I need the results from my worker thread outside of the delegate that receiveTimeout takes. Also: what is the best way to kill off the worker thread when I close the application, without having to wait for the worker thread to complete? My first thought was to use receiveTimeout in the worker thread, but the work is being done in a parallel foreach loop, and I am not sure if there is a way to safely use receiveTimeout in a parallel situation... I also found Thread.isDaemon in core.thread. I tried doing auto thread = Thread.getThis(); thread.isDaemon = true; at the start of the worker thread, but it still seems to wait for it to complete before closing. Thanks again! receiveTimeout can be used like this: void main() { spawn(workerFunc); writeln(Waiting for a message); bool received = false; while (!received) { received = receiveTimeout(600.msecs, (string message) { // === Receiving a value writeln(received: , message); }); if (!received) { writeln(... no message yet); /* ... other operations may be executed here ... */ } } } (cf. http://ddili.org/ders/d.en/concurrency.html) To stop threads immediately, I've found that the best way is to use a shared variable, typically a bool, that is changed only in one place. I hope I'll find the time on Monday to post a simple example. 1. shared bool ABORT; 2. 3.// in owner thread 4. ABORT = true; // The only place where you do this. 5. bool res; 6. while ((res = receiveOnly!bool()) == false) { debug writeln(waiting for abort ...); } // in worker thread(s) foreach () { if (ABORT) break; // working away } // ... ownerTid.send(true); If you have more than one thread to abort, you'll have to adapt lines 5 and 6 accordingly. Unfortunately, sending an abort message to a thread as in `send(thread, true)` takes too long. Setting a global flag like ABORT is instantaneous. Beware of data races though. You might want to have a look at: http://ddili.org/ders/d.en/concurrency_shared.html Especially `synchronized` and atomicOp.
Re: std.stream.MemoryStream deprecated, range is the alternative?
On Thursday, 6 August 2015 at 17:01:32 UTC, chris wrote: since memorystream is deprecated how do i do something like this with Input and Output ranges? How can i fill up an array with ranges like you can do with streams? Thanks. The InputRange primitives already exist for arrays, they are located in std.array, as well as the functions to insert elements. To achieve more advanced mutations use std.algorithm. --- import std.stdio; import std.array; import std.algorithm; byte B(T)(T t){return cast(byte) t;} struct FillerDemo { private byte cnt; byte front(){return cnt;} void popFront(){++cnt;} @property bool empty(){return cnt == 8;} } void main(string[] args) { auto rng = [0.B, 2.B, 4.B, 6.B, 8.B, 10.B, 12.B]; // reads then advances, destructively byte val = rng.front; writeln(val); rng.popFront; // fills with an array insertInPlace(rng, 0, [-4.B, -2.B, 0.B]); writeln(rng); rng = rng.init; // fills with a compatible range insertInPlace(rng, 0, *new FillerDemo); writeln(rng); // std.algorithm reverse(rng); } --- Note, if you don't know yet, that ranges are consumed. The front is lost each time popFront() is called.
Re: Template-Parameterized Variadic isInstaceOf
On Friday, 7 August 2015 at 14:13:24 UTC, Nordlöw wrote: How do check that the second template argument to the instance of SortedRange matches `pred`? Using TemplateArgsOf. I found a solution: template isSortedRange(T, alias pred = a b) { import std.traits : TemplateArgsOf; enum isSortedRange = (is(T == SortedRange!Args, Args...) pred == (TemplateArgsOf!T[1])); } /// unittest { alias R = int[]; enum pred = a b; alias SR = SortedRange!(R, pred); static assert(isSortedRange!(SR, pred)); } Any suggestions on adding support for `binaryFun!pred` aswell? My try enum isSortedRange = (is(T == SortedRange!Args, Args...) is(binaryFun!pred == binaryFun!(TemplateArgsOf!T[1]))); fails.
Re: Template-Parameterized Variadic isInstaceOf
On Friday, 7 August 2015 at 14:30:55 UTC, Nordlöw wrote: Any suggestions on adding support for `binaryFun!pred` aswell? I cracked it. template isSortedRange(T, alias pred = a b) { import std.traits : TemplateArgsOf; static if (TemplateArgsOf!T.length == 2) { import std.functional : binaryFun; alias predArg = TemplateArgsOf!T[1]; static if (isSomeString!(typeof(pred))) { alias predFun = binaryFun!pred; } else { alias predFun = pred; } static if (isSomeString!(typeof(predArg))) { alias predArgFun = binaryFun!predArg; } else { alias predArgFun = predArg; } enum isSortedRange = (is(T == SortedRange!Args, Args...) is(typeof(predFun) == typeof(predArgFun))); } else { enum isSortedRange = false; } } /// unittest { import std.functional : binaryFun; alias R = int[]; enum pred = a b; alias SR = SortedRange!(R, pred); static assert(isSortedRange!(SR, pred)); static assert(isSortedRange!(SR, binaryFun!pred)); alias SR2 = SortedRange!(R, binaryFun!pred); static assert(isSortedRange!(SR2, pred)); static assert(isSortedRange!(SR2, binaryFun!pred)); } Comments, please.
Re: Concurrency Confusion
On Friday, 7 August 2015 at 15:55:33 UTC, Chris wrote: Using a shared boolean is probably not the best way, I should have said the most efficient and reliable way.
Syntax: how to return shared?
How do I mark a function as returning shared object? This won't compile: shared Foo foo () { ... } This does, but looks somewhat awkward to me: shared (shared Foo) foo () { ... } -- Marek Janukowicz
std.parallelism example hangs compiler 2.067.1
This appears to hang up dmd compiler 2.067.1. Changing parallel(s) to s works ok. Is this a known problem? import std.stdio; import std.string; import std.format; import std.range; import std.parallelism; int main(string[] argv) { string s[10]; foreach (i, ref si ; parallel(s)){ si = format(hi:%d,i); } foreach (ref rm; s[99000..99010]){ writeln(rm); } return 0; }
Re: Syntax: how to return shared?
On 8/7/15 1:19 PM, Marek Janukowicz wrote: How do I mark a function as returning shared object? This won't compile: shared Foo foo () { ... } This does, but looks somewhat awkward to me: shared (shared Foo) foo () { ... } shared, const, immutable when applied to a member function actually are NOT affecting the return type. So for instance: const Foo foo() Does NOT return a const Foo, but: const(Foo) foo() does. The first returns a mutable Foo, and applies const to the 'this' parameter for the foo member function (a hidden parameter). So what you likely want is: shared(Foo) foo() -Steve
Re: std.parallelism example hangs compiler 2.067.1
On 8/7/15 2:37 PM, Steven Schveighoffer wrote: I'll file a bug on this. https://issues.dlang.org/show_bug.cgi?id=14886 -Steve
Re: Syntax: how to return shared?
On Friday, 7 August 2015 at 17:19:16 UTC, Marek Janukowicz wrote: shared (shared Foo) foo () { ... } That's correct, though the recommendation now is to put the other shared on teh right and change the parens a little: shared(Foo) foo() shared { } The ones without parens refer to the `this` in there and are kinda confusing to see on the left, so putting them on the right looks a bit easier (same with const btw).
Re: std.parallelism example hangs compiler 2.067.1
On 8/7/15 2:19 PM, Jay Norwood wrote: This appears to hang up dmd compiler 2.067.1. Changing parallel(s) to s works ok. Is this a known problem? import std.stdio; import std.string; import std.format; import std.range; import std.parallelism; int main(string[] argv) { string s[10]; foreach (i, ref si ; parallel(s)){ si = format(hi:%d,i); } foreach (ref rm; s[99000..99010]){ writeln(rm); } return 0; } When you said hang up, I didn't understand what you meant. Now I see, it actually hangs dmd (actually, it's not hung, it is still running as far as I can tell). If I reduce to 1, it completes the compile with an error. I think it has to do with parallel(s). In fact, this code also hangs: int main(string[] argv) { string s[10]; parallel(s); } In order to get what you really do want (no hangs, no errors), use this: parallel(s[]) I'll file a bug on this. -Steve
Re: std.getopt: checking if an option has been passed
On Friday, 7 August 2015 at 11:40:54 UTC, Laeeth Isharc wrote: What's the best way to check if an (optional) argument has been passed? One way is to use a default value, but I wonder if there is a tidier approach Thanks. (For startDate and endDate below) struct NanoClientOptions { string nanoUrl=tcp://127.0.0.1:; string[] tickers; DateTime startDate=DateTime(1,1,1); DateTime endDate=DateTime(,1,1); } auto helpInformation = getopt( args, nanoUrl, Address of kaleidic data nanoserver eg tcp://127.0.0.1:, options.nanoUrl, std.getopt.config.required, ticker|t, Tickers to download data for, options.tickers, startDate|s, Start Date, options.startDate, endDate|e, End Date, options.endDate ); Laeeth I guess answer is simple: std.getopt doesn't handle DateTimes, so I have to use a callback, in which case I can just track whether it's been passed myself.
std.getopt: checking if an option has been passed
What's the best way to check if an (optional) argument has been passed? One way is to use a default value, but I wonder if there is a tidier approach Thanks. (For startDate and endDate below) struct NanoClientOptions { string nanoUrl=tcp://127.0.0.1:; string[] tickers; DateTime startDate=DateTime(1,1,1); DateTime endDate=DateTime(,1,1); } auto helpInformation = getopt( args, nanoUrl, Address of kaleidic data nanoserver eg tcp://127.0.0.1:, options.nanoUrl, std.getopt.config.required, ticker|t, Tickers to download data for, options.tickers, startDate|s, Start Date, options.startDate, endDate|e, End Date, options.endDate ); Laeeth
Re: zlib performance
On Friday, 7 August 2015 at 09:12:32 UTC, yawniek wrote: On Friday, 7 August 2015 at 08:50:11 UTC, Daniel Kozák wrote: ldc[2] -O -release -boundscheck=off -singleobj app.d ldc 0.15.2 beta2 2.86s user 0.55s system 77% cpu 4.392 total v2.068-devel-8f81ffc 2.86s user 0.67s system 78% cpu 4.476 total v2.067 2.88s user 0.67s system 78% cpu 4.529 total i can now reproduce the results and indeed, its faster than zcat: on a c4.xlarge aws instance running archlinux and dmd v2.067 same file as above on my macbook. best run: 2.72s user 0.39s system 99% cpu 3.134 total worst run: 3.47s user 0.46s system 99% cpu 3.970 total zcat: best: 4.45s user 0.28s system 99% cpu 4.764 total worst: 4.99s user 0.57s system 99% cpu 5.568 total so i guess on os x there is still something to be optimized Can you try it without write operation (comment out all write)? And than try it without uncompression? // without compression: void main(string[] args) { auto f = File(args[1], r); foreach (buffer; f.byChunk(4096)) { write(cast(char[])buffer); } } // without write: void main(string[] args) { auto f = File(args[1], r); auto uncompressor = new UnCompress(HeaderFormat.gzip); foreach (buffer; f.byChunk(4096)) { auto uncompressed = cast(char[])(uncompressor.uncompress(buffer)); } uncompressor.flush; }
Template-Parameterized Variadic isInstaceOf
To implement a new trait isSortedRange(R, pred) needed for SortedRange specializations I need a variant of enum bool isInstanceOf(alias S, T) = is(T == S!Args, Args...); that takes the `pred` argument aswell. But I have no clue what to do with enum bool isInstanceOf(alias S, T, TParams) = is(T == S!Args, Args...); because I dont' understand the syntax is(T == S!Args, Args...); Can somebody please explain and help out with variadic version of `isInstanceOf`?
Re: zlib performance
On Friday, 7 August 2015 at 11:45:00 UTC, Daniel Kozak wrote: On Friday, 7 August 2015 at 09:12:32 UTC, yawniek wrote: [...] Can you try it without write operation (comment out all write)? And than try it without uncompression? // without compression: void main(string[] args) { auto f = File(args[1], r); foreach (buffer; f.byChunk(4096)) { write(cast(char[])buffer); } } 0.03s user 0.09s system 11% cpu 1.046 total // without write: void main(string[] args) { auto f = File(args[1], r); auto uncompressor = new UnCompress(HeaderFormat.gzip); foreach (buffer; f.byChunk(4096)) { auto uncompressed = cast(char[])(uncompressor.uncompress(buffer)); } uncompressor.flush; } 2.82s user 0.05s system 99% cpu 2.873 total
Re: assigning a struct object to an array
OK, I got it. Thank you very much. Is it you who wrote Programming in D ? It's a great e-book, very clear, I love it. Alex
Re: Template-Parameterized Variadic isInstaceOf
On Friday, 7 August 2015 at 11:45:22 UTC, Nordlöw wrote: To implement a new trait isSortedRange(R, pred) needed for SortedRange specializations I need a variant of enum bool isInstanceOf(alias S, T) = is(T == S!Args, Args...); that takes the `pred` argument aswell. But I have no clue what to do with enum bool isInstanceOf(alias S, T, TParams) = is(T == S!Args, Args...); because I dont' understand the syntax is(T == S!Args, Args...); Can somebody please explain and help out with variadic version of `isInstanceOf`? I believe that it is read as T is equal to the template instansiation of S for some args Args... Similar to the more common (T ==V[K],V,K) read as an associative array with V as the value, K as the key for some V and some K Used to get the instansiation parameters.
Re: Template-Parameterized Variadic isInstaceOf
On Friday, 7 August 2015 at 11:45:22 UTC, Nordlöw wrote: enum bool isInstanceOf(alias S, T, TParams) Correction: enum bool isInstanceOf(alias S, T, TParams...)
Re: zlib performance
On Fri, 07 Aug 2015 12:29:26 + yawniek dl...@srtnwz.com wrote: On Friday, 7 August 2015 at 11:45:00 UTC, Daniel Kozak wrote: On Friday, 7 August 2015 at 09:12:32 UTC, yawniek wrote: [...] Can you try it without write operation (comment out all write)? And than try it without uncompression? // without compression: void main(string[] args) { auto f = File(args[1], r); foreach (buffer; f.byChunk(4096)) { write(cast(char[])buffer); } } 0.03s user 0.09s system 11% cpu 1.046 total So I/O seems to be OK // without write: void main(string[] args) { auto f = File(args[1], r); auto uncompressor = new UnCompress(HeaderFormat.gzip); foreach (buffer; f.byChunk(4096)) { auto uncompressed = cast(char[])(uncompressor.uncompress(buffer)); } uncompressor.flush; } 2.82s user 0.05s system 99% cpu 2.873 total So maybe it is a zlib problem on osx?
Re: assigning a struct object to an array
On 08/07/2015 05:37 AM, Reflexive wrote: Is it you who wrote Programming in D ? Yes. (The other Ali Çehreli is a musician. :) ) It's a great e-book, very clear, I love it. Thank you very much for the kind words. Which format are you using? It is good to hear that it is acceptable as an ebook, as I've always targetted HTML and then PDF for the print version (which should be purchasable any day now). Fortunately, the tool I used (calibre) worked great. Ali
Re: Concurrency Confusion
On Friday, 7 August 2015 at 15:55:33 UTC, Chris wrote: To stop threads immediately, I've found that the best way is to use a shared variable, typically a bool, that is changed only in one place. ... Unfortunately, sending an abort message to a thread as in `send(thread, true)` takes too long. Setting a global flag like ABORT is instantaneous. Beware of data races though. You might want to have a look at: http://ddili.org/ders/d.en/concurrency_shared.html Especially `synchronized` and atomicOp. Ah, I already had a variable like ABORT in my application for signaling the main thread to close, so this was a surprisingly painless change! I made that variable shared and then did the following: instead of ABORT = true; I now do import core.atomic; atomicStore!(MemoryOrder.rel)(ABORT, true); and instead of if(ABORT) break; I now do import core.atomic; if(atomicLoad!(MemoryOrder.acq)(ABORT)) break; This works great, and with the memory ordering specified I do not see a noticeable difference in performance, whereas with the default memory ordering my ~36 second processing takes ~38 seconds. One concern I had was that `break` might be a bad idea inside of a parallel foreach. Luckily, it seems that the author(s) of std.parallelism thought of this - according to the documentation break inside of a parallel foreach throws an exception and some clever exception handling is done under the hood. I don't see an uncaught exception when I close my application, but it is now able to close without having to wait for the worker thread to complete, so everything seems fine and dandy! Thanks for the help! On Friday, 7 August 2015 at 15:55:33 UTC, Chris wrote: receiveTimeout can be used like this: ... My problem is that when you do this: received = receiveTimeout(600.msecs, (string message) { // === Receiving a value writeln(received: , message); }); message is local to the delegate that receiveTimeout takes. I want to use message outside of the delegate in the receiving thread. However, if you send an immutable value from the worker thread, afaict there would be no way to assign it to a global/outer variable without making a mutable copy (expensive!) I haven't really spent much time trying to pass my message as mutable via shared yet, but hopefully that could work...
Re: std.parallelism taskPool.map example throws exception
This appears to work ... at least, no exception: auto sm = File(fn).byLine(KeepTerminator.no) .map!a.chomp() .map!a.idup() .map!(to!double) .map!a.log10() .sum(); writeln(sum=,sm);
Re: std.parallelism taskPool.map example throws exception
This also works. auto sm = File(fn).byLineCopy() .map!a.chomp() .map!(to!double) .map!a.log10() .sum(); writeln(sum=,sm);
std.concurrency.send problems with immutable
This program works fine: import std.concurrency; struct A { string a,b; } void main () { immutable A a = immutable A( blah ); send( thisTid, a ); } But if change struct A declaration to: struct A { string a,b,c; } I get this error during compilation: /opt/dmd2/linux/bin64/../../src/phobos/std/variant.d(653): Error: cannot modify immutable expression *p /opt/dmd2/linux/bin64/../../src/phobos/std/variant.d(580): Error: template instance std.variant.VariantN!32LU.VariantN.opAssign!(immutable(A)) error instantiating /opt/dmd2/linux/bin64/../../src/phobos/std/concurrency.d(117): instantiated from here: __ctor!(immutable(A)) /opt/dmd2/linux/bin64/../../src/phobos/std/concurrency.d(628): instantiated from here: __ctor!(immutable(A)) /opt/dmd2/linux/bin64/../../src/phobos/std/concurrency.d(618): instantiated from here: _send!(immutable(A)) /opt/dmd2/linux/bin64/../../src/phobos/std/concurrency.d(594): instantiated from here: _send!(immutable(A)) Is this is a bug? On a related note - sometimes when sending a shared struct I get a compilation error similar to this: /opt/dmd2/linux/bin64/../../src/phobos/std/variant.d(638): Error: function core.stdc.string.memcpy (void* s1, const(void*) s2, ulong n) is not callable using argument types (ubyte[32]*, shared(Notification)*, ulong) /opt/dmd2/linux/bin64/../../src/phobos/std/variant.d(580): Error: template instance std.variant.VariantN!32LU.VariantN.opAssign!(shared(Notification)) error instantiating /opt/dmd2/linux/bin64/../../src/phobos/std/concurrency.d(117): instantiated from here: __ctor!(shared(Notification)) /opt/dmd2/linux/bin64/../../src/phobos/std/concurrency.d(628): instantiated from here: __ctor!(shared(Notification)) /opt/dmd2/linux/bin64/../../src/phobos/std/concurrency.d(618): instantiated from here: _send!(shared(Notification)) /opt/dmd2/linux/bin64/../../src/phobos/std/concurrency.d(594): instantiated from here: _send!(shared(Notification)) If I then add more fields to the struct (eg. dummy string a,b) it compiles fine. Is this another bug or am I missing something? -- Marek Janukowicz
Re: Using std.random.uniform as a range
On 08/07/2015 06:59 AM, drug wrote: What is the best way to create range from uniform() function (in other words create a generator based on some function, returning, say, scalar, not range)? I did http://dpaste.dzfl.pl/53e3d9255cd7 but I'm not sure it's the best way. At least sequence using looks ugly There is an undocumented (why?) Generator struct and generate() functin in std.range: import std.stdio; import std.range; import std.random; void main() { auto r = generate!(() = uniform(0, 6)) .take(10); writefln(%(%s %), r); } Ali
std.parallelism taskPool.map example throws exception
I tried to create a working example from the std.parallelism taskPool.map code, and it throws with empty strings with length 1 being passed to to!double. Anyone have a working example? I'm building on Windows with 2.067.1 dmd. import std.parallelism; import std.algorithm; import std.stdio; import std.conv; import std.math; import std.range; import std.file; void main() { auto fn = numberList.txt; auto f = File(fn,w); scope(exit) std.file.remove(fn); foreach (i ; iota(10.0,2_000.0)){ f.writefln(%g,i+0.5); } f.close(); auto lineRange = File(fn).byLine(); auto dupedLines = std.algorithm.map!a.idup(lineRange); auto nums = taskPool.map!(to!double)(dupedLines); auto logs = taskPool.map!log10(nums); double sum = 0; foreach(elem; logs) { sum += elem; } writeln(sum=,sum); }
Re: Template-Parameterized Variadic isInstaceOf
On Friday, 7 August 2015 at 14:45:44 UTC, Nordlöw wrote: On Friday, 7 August 2015 at 14:30:55 UTC, Nordlöw wrote: Any suggestions on adding support for `binaryFun!pred` aswell? I cracked it. template isSortedRange(T, alias pred = a b) { import std.traits : TemplateArgsOf; static if (TemplateArgsOf!T.length == 2) { import std.functional : binaryFun; alias predArg = TemplateArgsOf!T[1]; static if (isSomeString!(typeof(pred))) { alias predFun = binaryFun!pred; } else { alias predFun = pred; } static if (isSomeString!(typeof(predArg))) { alias predArgFun = binaryFun!predArg; } else { alias predArgFun = predArg; } enum isSortedRange = (is(T == SortedRange!Args, Args...) is(typeof(predFun) == typeof(predArgFun))); } else { enum isSortedRange = false; } } /// unittest { import std.functional : binaryFun; alias R = int[]; enum pred = a b; alias SR = SortedRange!(R, pred); static assert(isSortedRange!(SR, pred)); static assert(isSortedRange!(SR, binaryFun!pred)); alias SR2 = SortedRange!(R, binaryFun!pred); static assert(isSortedRange!(SR2, pred)); static assert(isSortedRange!(SR2, binaryFun!pred)); } Comments, please. I think you could have omitted the need for a predicate with just using isInstanceOf. enum isSortedRange(T) = isInstanceOf!(SortedRange, T); But I think yours works better for what you are trying to do, the pred of whatever function you are trying to specialize needs to match the pred of the sorted range itself, else the specialization is wrong.
Re: std.parallelism example hangs compiler 2.067.1
On Friday, 7 August 2015 at 18:51:45 UTC, Steven Schveighoffer wrote: On 8/7/15 2:37 PM, Steven Schveighoffer wrote: I'll file a bug on this. https://issues.dlang.org/show_bug.cgi?id=14886 -Steve Thanks. The workaround works ok.
Re: std.concurrency.send problems with immutable
On 08/07/2015 03:24 PM, Marek Janukowicz wrote: This program works fine: import std.concurrency; struct A { string a,b; } void main () { immutable A a = immutable A( blah ); send( thisTid, a ); } But if change struct A declaration to: struct A { string a,b,c; } I get this error during compilation: /opt/dmd2/linux/bin64/../../src/phobos/std/variant.d(653): Error: cannot modify immutable expression *p /opt/dmd2/linux/bin64/../../src/phobos/std/variant.d(580): Error: template instance std.variant.VariantN!32LU.VariantN.opAssign!(immutable(A)) error instantiating /opt/dmd2/linux/bin64/../../src/phobos/std/concurrency.d(117): instantiated from here: __ctor!(immutable(A)) /opt/dmd2/linux/bin64/../../src/phobos/std/concurrency.d(628): instantiated from here: __ctor!(immutable(A)) /opt/dmd2/linux/bin64/../../src/phobos/std/concurrency.d(618): instantiated from here: _send!(immutable(A)) /opt/dmd2/linux/bin64/../../src/phobos/std/concurrency.d(594): instantiated from here: _send!(immutable(A)) Is this is a bug? Yes, it is a bug. The reason the number of members makes a difference is that Variant's implementation does different things depending on T.sizeof: https://github.com/D-Programming-Language/phobos/blob/master/std/variant.d#L608 On a related note - sometimes when sending a shared struct I get a compilation error similar to this: Variant has a number of open bugs. Unfortunately, Variant reduces the quality of std.concurrency as Variant is used as a catch-all type by that module. Ali
Re: Concurrency Confusion
On Friday, 7 August 2015 at 22:13:35 UTC, 岩倉 澪 wrote: message is local to the delegate that receiveTimeout takes. I want to use message outside of the delegate in the receiving thread. However, if you send an immutable value from the worker thread, afaict there would be no way to assign it to a global/outer variable without making a mutable copy (expensive!) I haven't really spent much time trying to pass my message as mutable via shared yet, but hopefully that could work... Found the answer to this :) http://forum.dlang.org/post/mailman.1706.1340318206.24740.digitalmars-d-le...@puremagic.com I send the results from my worker thread with assumeUnique, and then simply cast away immutable in the receiving thread like so: (in module scope) Bar[] baz; (in application loop) import std.array if(baz.empty) { import std.concurrency, std.datetime; receiveTimeout(0.msecs, (immutable Bar[] bar){ baz = cast(Bar[])bar; }); }
Re: std.parallelism taskPool.map example throws exception
and, finally, this works using the taskPool.map, as in the std.parallelism example. So, the trick appears to be that the call to chomp is needed. auto lineRange = File(fn).byLineCopy(); auto chomped = std.algorithm.map!a.chomp(lineRange); auto nums = taskPool.map!(to!double)(chomped); auto logs = taskPool.map!log10(nums); double sum = 0; foreach(elem; logs) { sum += elem; } writeln(sum=,sum);
Re: std.parallelism taskPool.map example throws exception
Unfortunately, this is not a very good example for std.parallelism, since the measured times are better using the std.algorithm.map calls. I know from past experience that std.parallelism routines can work well when the work is spread out correctly, so this example could be improved. This is parallel D:\visd\map\map\Releasemap sum=1.17335e+07 time msecs:1242 Non-parallel D:\visd\map\map\Releasemap sum=1.17335e+07 time msecs:970 I think this example import std.parallelism; import std.algorithm; import std.stdio; import std.conv; import std.math; import std.range; import std.file; import std.datetime; void main() { auto fn = numberList.txt; auto f = File(fn,w); scope(exit) std.file.remove(fn); foreach (i ; iota(10.0,2_000_000.0)){ f.writefln(%g,i+0.5); } f.close(); std.datetime.StopWatch sw; sw.start(); auto lineRange = File(fn).byLineCopy(); auto chomped = std.algorithm.map!a.chomp(lineRange); auto nums = std.algorithm.map!(to!double)(chomped); auto logs = std.algorithm.map!log10(nums); double sum = 0; foreach(elem; logs) { sum += elem; } long tm = sw.peek().msecs; writeln(sum=,sum); writeln(time msecs:, tm); }
Re: Concurrency Confusion
On Saturday, 8 August 2015 at 00:39:57 UTC, 岩倉 澪 wrote: receiveTimeout(0.msecs, (immutable Bar[] bar){ baz = cast(Bar[])bar; }); Whoops, that should be: receiveTimeout(0.msecs, (immutable(Bar)[] bar){ baz = cast(Bar[])bar; });
Re: Concurrency Confusion
On Saturday, 8 August 2015 at 00:39:57 UTC, 岩倉 澪 wrote: Found the answer to this :) http://forum.dlang.org/post/mailman.1706.1340318206.24740.digitalmars-d-le...@puremagic.com I send the results from my worker thread with assumeUnique, and then simply cast away immutable in the receiving thread like so: (in module scope) Bar[] baz; (in application loop) import std.array if(baz.empty) { import std.concurrency, std.datetime; receiveTimeout(0.msecs, (immutable Bar[] bar){ baz = cast(Bar[])bar; }); } I'm not completely sure that it's bad in this case, but you really shouldn't be casting away immutable. It's undefined behaviour in D.