D easily overlooked?

Joakim via Digitalmars-d digitalmars-d at puremagic.com
Sun Sep 3 12:28:41 PDT 2017


On Sunday, 3 September 2017 at 17:56:26 UTC, thinwybk wrote:
> On Saturday, 2 September 2017 at 17:00:46 UTC, Joakim wrote:
>> On Saturday, 2 September 2017 at 15:41:54 UTC, Joakim wrote:
>>> D:
>>> https://bitbucket.org/qznc/d-shootout/raw/898f7f3b3c5d55680229113e973ef95ece6f711a/progs/nbody/nbody.d
>>>
>>> ldc 1.4 beta1, llvm 4.0.1
>>>
>>> ldc2 -O3 nbody.d
>>>
>>> The D version averages 2.5 seconds, the C++ version 6 
>>> seconds, which means D would likely still be at the top of 
>>> that n-body ranking today.
>>
>> Sorry, I assumed the D version worked fine and didn't bother 
>> to check the output, turns out it needs two foreach loops 
>> changed in advance(dt).  Specifically, "Body i" should be 
>> changed to "ref Body i" in both foreach statements, so the 
>> data is actually updated. ;)
>>
>> After that change, the C++ version wins by a little, 6 seconds 
>> vs. 6.5 seconds for the D translation.  I see that the C++ 
>> version directly invokes SIMD intrinsics, so perhaps that's to 
>> be expected.
>
> What needs to be adjusted for optimization? If you let me know 
> it I adjust it here 
> https://github.com/fkromer/exploringBB/blob/nbody/chp05/performance/nbody.d and/or https://github.com/fkromer/exploringBB/blob/nbody/chp05/performance/build and/or https://github.com/fkromer/exploringBB/blob/nbody/chp05/performance/run#L25

It's not optimization, right now it's subtly incorrect, because a 
struct passed into a foreach loop is copied.  Add a "ref" to 
"Body i" when initializing each foreach loop in advance() to 
remedy that, so that it produces the same output as the C++ 
version for the same input.


More information about the Digitalmars-d mailing list