Re: Exercise at end of Ch. 56 of "Programming in D"

2022-08-20 Thread Paul Backus via Digitalmars-d-learn

On Monday, 15 August 2022 at 02:59:59 UTC, Ali Çehreli wrote:

class Point {
  int x;
  int y;
  Color color;

  // ...

  override size_t toHash() const {
return x + y;
  }
}

The book is simply forgetting to show that function. 
(Otherwise, I never put any code without testing.)


WARNING: Hash functions can be very tricky. I have a strong 
feeling that adding two integers is not an efficient one.


Fortunately there is no need to invent our own hash function 
here, because we can use the generic hashOf [1] from druntime:


override size_t toHash() const {
return hashOf(y, hashOf(x));
}

The second argument is optional, and is used when you want to 
"accumulate" a single hash value from multiple input values.


[1] https://druntime.dpldocs.info/object.hashOf.1.html


Re: Exercise at end of Ch. 56 of "Programming in D"

2022-08-20 Thread Era Scarecrow via Digitalmars-d-learn

On Monday, 15 August 2022 at 03:19:43 UTC, johntp wrote:
Your solution worked. I guess it is a little unnatural to 
ignore the color.  I tried overriding the toHash() of Point, 
but I don't know enough D to get it to work.  I wonder if that 
could be a solution.


 Depends on what you're trying to do. Metadata unrelated to the 
value of the object i would ignore and not be part of hashing or 
comparisons. I've also done things for strings that held 
information like what original position in the array the data was 
(*for visually sorting testing*) and could yield me information 
while not interfering with the object/data in question.


 Though x+y as a hash seems terrible. I'd probably do 
((x+1000)**2)+y (assuming x and y are both going to be generally 
small ensuring the hash for location is unique. Then point(2,1) 
and point(1,2) have different hashes. But I'm not familiar with 
the exercise in question.


Re: Recommendation for parallelism with nested for loops?

2022-08-20 Thread Christian Köstlin via Digitalmars-d-learn

On 20.08.22 12:28, Christian Köstlin wrote:

On 19.08.22 03:49, Shriramana Sharma wrote:
Hello. I want to parallelize a computation which has two for loops, 
one nested within another. All inner-loop-param+outer-loop-param 
combinations can be computed independent of one another.


As I suspected, 
[https://forum.dlang.org/post/xysyidbkjdinclmrx...@forum.dlang.org](this forum post) says that only one loop can be parallelized. Will it be an error or inefficient or useless if I try to do both?


Also, what is the best way to do parallelism in such a situation?
You could also do a custom range that makes a one-dimensional range (aka 
iota out of your nested loops) and then process this with parallel.
Another way (more similar to Ali's solution) would be to write the 
nested loops as you have them, but collect the parameters for the work 
in an array or something and then process this array in parallel.


Kind regards,
Christian


Actually phobos brings already all that is required (especially the 
cartesian product):


```d
import std;

void doSomething(int i, string s)
{
writeln("%s-%s".format(i, s));
}

void main()
{
foreach (t; cartesianProduct(iota(1, 4), ["abc", "def", 
"ghi"]).parallel) {

doSomething(t.expand);
}
}
```

kind regards,
Christian



Re: Recommendation for parallelism with nested for loops?

2022-08-20 Thread Christian Köstlin via Digitalmars-d-learn

On 19.08.22 03:49, Shriramana Sharma wrote:
Hello. I want to parallelize a computation which has two for loops, one 
nested within another. All inner-loop-param+outer-loop-param 
combinations can be computed independent of one another.


As I suspected, 
[https://forum.dlang.org/post/xysyidbkjdinclmrx...@forum.dlang.org](this 
forum post) says that only one loop can be parallelized. Will it be an 
error or inefficient or useless if I try to do both?


Also, what is the best way to do parallelism in such a situation?
You could also do a custom range that makes a one-dimensional range (aka 
iota out of your nested loops) and then process this with parallel.
Another way (more similar to Ali's solution) would be to write the 
nested loops as you have them, but collect the parameters for the work 
in an array or something and then process this array in parallel.


Kind regards,
Christian