## digitalmars.D.announce - Mir Blog: Writing efficient numerical code in D

Relja Ljubobratovic <ljubobratovic.relja gmail.com> writes:
```Hey guys,

We have just published another post on "Writing efficient
numerical code in D", to Mir's Blog[1]. In this post we are
focusing on mir.ndslice.algorithm usage in DCV[2], computer
vision library that has recently joined libmir organization.
We've had great success in optimizing DCV's algorithms mostly by
replacing loop-style code with mir.ndslice.algorithm
multidimensional iteration utilities[3], and we thought it was

Any grammar (or any other type of) fix report is greatly
appreciated!

Also, we'd like to ask Mike Parker to forward this to reddit if
it's not too much trouble? :) Maybe only wait few days so we
could weed out some mistakes from the text.

Cheers,
Relja

[1]
http://blog.mir.dlang.io/ndslice/algorithm/optimization/2016/12/12/writing-efficient-numerical-code.html
[2] https://github.com/libmir/dcv
[3] https://github.com/libmir/dcv/pull/58
```
Dec 12 2016
Johan Engelen <j j.nl> writes:
```On Monday, 12 December 2016 at 21:58:23 UTC, Relja Ljubobratovic
wrote:
Hey guys,

We have just published another post on "Writing efficient
numerical code in D", to Mir's Blog[1].

Nice :)

- You can make the numbers in the table more readable by adding
commas?
For example, 1624687 --> 1,624,687. Probably even better is to
only show the most significant digits. 1624687 us --> 1.625 s for
example.
- Add a sentence about the benchmarking set up. " The numbers
shown are the best times of 10 trials, with LDC commandline
options `ldc2 -O3 -release`" Something like that. Or add it to
the configuration table.
- For the "convolution" example, could you elaborate on why the
Mir-based implementation is so much faster than the C-like
implementation? From the big table, I see that the speedup for
DCV's convolution algorithms is not as large as 10x.
- The "threshold" algorithm has a 19x speed up?! (I would remove
"threshold" from the average improvement calculation. The numbers
are already awesome, no need to oversell it!)
- Overall, does this mean that DCV became more than twice as
fast? How does it stack up to other vision libs after this
massive speed boost?

cheers,
Johan
```
Dec 12 2016
Relja Ljubobratovic <ljubobratovic.relja gmail.com> writes:
```On Monday, 12 December 2016 at 22:34:32 UTC, Johan Engelen wrote:
On Monday, 12 December 2016 at 21:58:23 UTC, Relja
Ljubobratovic wrote:
Hey guys,

We have just published another post on "Writing efficient
numerical code in D", to Mir's Blog[1].

Nice :)

Thanks! :)

- You can make the numbers in the table more readable by adding
commas?
For example, 1624687 --> 1,624,687. Probably even better is to
only show the most significant digits. 1624687 us --> 1.625 s
for example.

Great suggestion - I was focusing on the speedup, so I didn't pay
attention to run time, but obviously other people will.

- Add a sentence about the benchmarking set up. " The numbers
shown are the best times of 10 trials, with LDC commandline
options `ldc2 -O3 -release`" Something like that. Or add it to
the configuration table.

Thanks, will do. I agree there's an explanation missing on how
benchmark is configured.

- For the "convolution" example, could you elaborate on why the
Mir-based implementation is so much faster than the C-like
implementation? From the big table, I see that the speedup for
DCV's convolution algorithms is not as large as 10x.

DCV's convolution is parallelized, which I believe, could be the
reason. Ilya is probably the best man to elaborate the speedup vs
C implementation.

- The "threshold" algorithm has a 19x speed up?! (I would
remove "threshold" from the average improvement calculation.
The numbers are already awesome, no need to oversell it!)

I agree. I was thrilled when I saw the comparison at first, but
to be honest, I'm not sure what's the main reason. Here's the
commit diff if anyone is interested to explain:
https://github.com/libmir/dcv/pull/58/commits/0c744a7bb7308f07bd45d041c6e88e906c905fcf

- Overall, does this mean that DCV became more than twice as
fast? How does it stack up to other vision libs after this
massive speed boost?

I'm not sure. But I'm afraid we're not even close to opencv,
since its base image processing (and other) algorithms have been
intensively (manually) optimized with SIMD, but also have almost
100% coverage with OpenCL (and CUDA from 3.x). Anyhow, I'd like
not to bother with comparisons like this until we are satisfied
with DCV - there's much work yet to be done, IMHO.

cheers,
Johan

Thanks for the feedback Johan!
```
Dec 12 2016
rikki cattermole <rikki cattermole.co.nz> writes:
```On 13/12/2016 11:34 AM, Johan Engelen wrote:
On Monday, 12 December 2016 at 21:58:23 UTC, Relja Ljubobratovic wrote:
Hey guys,

We have just published another post on "Writing efficient numerical
code in D", to Mir's Blog[1].

Nice :)

- You can make the numbers in the table more readable by adding commas?
For example, 1624687 --> 1,624,687. Probably even better is to only show
the most significant digits. 1624687 us --> 1.625 s for example.

Oh underscores, then it is valid D!
```
Dec 12 2016
Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
```On 12/12/16 4:58 PM, Relja Ljubobratovic wrote:
Hey guys,

We have just published another post on "Writing efficient numerical code
in D", to Mir's Blog[1]. In this post we are focusing on
mir.ndslice.algorithm usage in DCV[2], computer vision library that has
recently joined libmir organization. We've had great success in
optimizing DCV's algorithms mostly by replacing loop-style code with
mir.ndslice.algorithm multidimensional iteration utilities[3], and we
thought it was worth blogging about.

Any grammar (or any other type of) fix report is greatly appreciated!

Also, we'd like to ask Mike Parker to forward this to reddit if it's not
too much trouble? :) Maybe only wait few days so we could weed out some
mistakes from the text.

Cheers,
Relja

[1]
http://blog.mir.dlang.io/ndslice/algorithm/optimization/2016/12/12/writing-efficient-numerical-code.html

[2] https://github.com/libmir/dcv
[3] https://github.com/libmir/dcv/pull/58

This is awesome! Mike, could you please post to reddit about 11 hours
from now if possible.

I'm about to fall asleep, but I made a quick pass through the piece.
Here are a few nits:

* "In this post I’d like give" -> "This post gives"

* "module equipped with" -> "module that provides"

* "it’s usage" -> "its usage"

* "Note: It is assumed the reader is already somewhat familiar with
understanding of the ndslice package. If not, please consult this
post/doc/webpage/etc before returning here."

* "What does it offer?" -> "What does mir.ndslice.algorithm offer?"

* "its integrated seamlessly" -> "it is integrated seamlessly"

* "by that allowing more natural flow of processing pipeline" -> "which
makes for elegantly flowing processing pipelines"

* "One of key components to make code based" -> "One key component that
makes code based"

* "LLVM based D compiler" -> "the LLVM based D compiler"

* "kernels you write" -> "computation kernels written by the end user"

* "else turned on" -> "turned on"

* ", by Johan Engelen" -> remove the comma

* "we’ve been actively refactoring" -> define the team; so far the
article referred to the author as a single person

* "with mir.ndslice.algorithm equivalent" -> "with mir.ndslice.algorithm
equivalents"

* align numbers in html tables to the right

* "As you can see, speedups" -> "Speedups"

* "as I’ll show you in this post" -> "as shown below"

* "take a look at PR" -> "take a look at the pull request"

* "Discalmer" -> "Disclaimer"

* "mind DCV project" -> "mind the DCV project"

* "showing power" -> "showing the power"

* "dive into implementation" -> "dive into the implementation"

* "without extensive optimization techniques applied" -> "without
extensive optimizations"

* "In future we’ll focus" -> "A future post will focus"

* "first we’ll take a look" -> "first let's take a look"

* "basic principle how it can efficiently replace loop-based code" ->
"basic principle of replacing loop-based code with pipelines efficiently"

* "… This code" -> "This code"

* "apply given kernel" -> "apply the given kernel"

* "To bring this example more down to earth" -> "To make the example
more concrete"

* "As you can see, we are replacing" -> "The pipeline version replaces"

* "with few magic calls" -> "with a few magic calls"

* "And as you may have noticed, at" -> "At"

* "I get ~10x speedup" -> there's been a bunch of "we" and now getting
back to "I" is surprising. You may want to make a pass making sure the
use of person is consistent.

* "that variant written" -> "that the variant written"

* "syntax, and are" -> "syntax and are"

* "this is most basic binarization" -> "this is a most basic binarization"

* "Special part to care about, related to lockstep, is that byElement
call on each input Slice" -> "The call to byElement inside lockstep is
worth special attention"

* "We can notice right away there’s a change" -> "First off, there's a
change"

* "requires this so" -> "assumes that is the case"

* "We can also notice byElement" -> "Notably, byElement"

* "examine performance differences" -> "compare the impact of these
changes on performance"

* "On my machine" -> "On a Core i7 laptop with xyz GB or RAM and xyz
other relevant parameters..."

* "result is following" -> "the results are as follows"

* "with very little effort using" -> "with very little effort by using"

* "and it’s submodules" -> "and its submodules"

* "coming for some numerical computing" -> "having an interest in
numerical computing"

* "would have to be introduced to ndslice" -> "should take a close look
at ndslice"

I suggest making two passes through the piece: (a) one pass should
reduce fillers that are abundant ("as you can see", "as I'll show you",
"as said in the docs"...) A few are fine (perhaps 2-3 in the whole
article. Then (b) one pass looking for unified use of "I" (most likely:
the author, use sparingly), "you" (the reader, use sparingly), and "we"
(most likely: the author and reader looking together at something).

The results are terrific, congrats Ilya and Relja!

Andrei
```
Dec 12 2016
Relja Ljubobratovic <ljubobratovic.relja gmail.com> writes:
```On Tuesday, 13 December 2016 at 05:27:01 UTC, Andrei Alexandrescu
wrote:
I'm about to fall asleep, but I made a quick pass through the
piece. Here are a few nits:

Thanks for an extensive feedback, Andrei! I've followed up 99% of
fixes given here[1], but unfortunately I'll be unavailable for
the rest of the day, so I'm passing the torch (or the hot potato)
to Ilya.

* "we’ve been actively refactoring" -> define the team; so far
the article referred to the author as a single person

I've done the coding, but couldn't have done it without Ilya's
consulting, so I put the 'Mir team' there. Hope thats ok.

* "On my machine" -> "On a Core i7 laptop with xyz GB or RAM
and xyz other relevant parameters..."

There are specs shown at the top of the post, so I thought it
would be best just to remove the "On my machine part". Again, I
hope that's alright.

I suggest making two passes through the piece: (a) one pass
should reduce fillers that are abundant ("as you can see", "as
I'll show you", "as said in the docs"...) A few are fine
(perhaps 2-3 in the whole article. Then (b) one pass looking
for unified use of "I" (most likely: the author, use
sparingly), "you" (the reader, use sparingly), and "we" (most
likely: the author and reader looking together at something).

As I've demonstrated, I'm quite unexperienced with writing. :)
So, thanks for the feedback, I've learned a lot! If I catch some
time during the day, I'll be sure to take another glance at it!

The results are terrific, congrats Ilya and Relja!

Andrei

Thanks a lot Andrei!
Relja

[1] https://github.com/libmir/blog/pull/32
```
Dec 12 2016
Mike Parker <aldacron gmail.com> writes:
```On Tuesday, 13 December 2016 at 05:27:01 UTC, Andrei Alexandrescu
wrote:

This is awesome! Mike, could you please post to reddit about 11
hours from now if possible.

Yes, I intend to post it to both reddit and FB, but it will have
to be a couple of hours earlier than that (Korea is UTC+9).
```
Dec 13 2016
Mike Parker <aldacron gmail.com> writes:
```On Tuesday, 13 December 2016 at 09:58:09 UTC, Mike Parker wrote:
On Tuesday, 13 December 2016 at 05:27:01 UTC, Andrei
Alexandrescu wrote:

11 hours from now if possible.

Yes, I intend to post it to both reddit and FB, but it will
have to be a couple of hours earlier than that (Korea is UTC+9).

```
Dec 13 2016
Relja Ljubobratovic <ljubobratovic.relja gmail.com> writes:
```On Tuesday, 13 December 2016 at 15:31:00 UTC, Mike Parker wrote:

Thank you, Mike!
```
Dec 13 2016
=?UTF-8?B?Tm9yZGzDtnc=?= <per.nordlow gmail.com> writes:
```On Monday, 12 December 2016 at 21:58:23 UTC, Relja Ljubobratovic
wrote:
http://blog.mir.dlang.io/ndslice/algorithm/optimization/2016/12/12/writing-efficient-numerical-code.html

Awesome!
```
Dec 13 2016
Mark <smarksc gmail.com> writes:
```On Monday, 12 December 2016 at 21:58:23 UTC, Relja Ljubobratovic
wrote:
[1]
http://blog.mir.dlang.io/ndslice/algorithm/optimization/2016/12/12/writing-efficient-numerical-code.html
[2] https://github.com/libmir/dcv
[3] https://github.com/libmir/dcv/pull/58

Very impressive work.
```
Dec 13 2016