use rand_r instead of rand in BrushMaskScalarApplicator and TotalRandomColorSource
ClosedPublic

Authored by tpaulssen on Nov 28 2015, 10:31 PM.

Diff Detail

Repository
R37 Krita
Lint
Automatic diff as part of commit; lint not applicable.
Unit
Automatic diff as part of commit; unit tests not applicable.
tpaulssen updated this revision to Diff 1421.Nov 28 2015, 10:31 PM
tpaulssen retitled this revision from to use rand_r instead of rand in BrushMaskScalarApplicator and TotalRandomColorSource.
tpaulssen updated this object.
tpaulssen edited the test plan for this revision. (Show Details)
tpaulssen set the repository for this revision to R37 Krita.
tpaulssen added a project: Krita.
tpaulssen updated this revision to Diff 1422.Nov 28 2015, 11:28 PM

this version of the diff uses the random number generator that's safe to use in concurrent situations to initialize a per-stack random state that then gets used in the inner loops.

it's still fast, but it doesn't concurrently access the same random state across threads.

boud asked to be subscribed via IRC.

Hi tpaulssen: please add Boud and myself as reviewers to your new diffs. Nobody gets alerted that about the unreviewed patches if no reviewer has been specified.

woltherav added a subscriber: woltherav.

I think some network hiccup must've happened brcause I can recall you and boud being set as reviewers

i set boud as a subscriber, but not as a reviewer. my mistake!

abrahams requested changes to this revision.Dec 8 2015, 11:12 PM
abrahams edited edge metadata.

I think this is a good idea, but rand_r does not work cross-platform. Could you see if you can get the same speedup with C++ stdlib?

http://www.stroustrup.com/C++11FAQ.html#std-random

This revision now requires changes to proceed.Dec 8 2015, 11:12 PM

There is also a class KisRandomSource which does some random number generation, you could put a more permanent solution there. It also makes Boost available.

rempt edited edge metadata.Dec 10 2015, 7:49 AM

I'm not sure KisRandomGenerator is the right place: that is more concerned with predictable random for a given x,y location so filters that need random can be implemented as filter layers/masks.

tpaulssen updated this revision to Diff 1508.Dec 13 2015, 6:35 PM
tpaulssen edited edge metadata.

Use C++11 random instead of rand in fuzzy brushes and TotalRandomColorSource

Dear @abrahams, with these new changes that use the c++11 random engines i still get the noticable speedup.

except the TotalRandomColorSource is very intensely bottlenecked by the kc.fromQColor call - especially the color space calculations. No clue what to do about that, though.

You can do...

int channels = dev->colorspace()->channelCount()
quint data[channels];

for (i=0;i<channels;i++) {
    data[i] = YOURRANDOMCODE;
}
data[channels-1]=255;//make sure the opacity is 100%

KoColor color(data, dev->colorspace());

Mind you, this code will currently crash in CMYK because there's a bug in CMYK colorspace traits that is fixed in my branch, but I didn't expect it to need to be fixed in master yet... But it should get your the full value of the image CS.

We've decided on IRC that KoColorspace should probably supply randomization functionality, so this patch is now, in my opinion, ready to land.

abrahams accepted this revision.Dec 13 2015, 11:31 PM
abrahams edited edge metadata.

OK, great! I do have one question though, do we want to create the rand_dev, rand_engine and rand_distr as class members instead of instantiating them at the time of the function call?

This revision is now accepted and ready to land.Dec 13 2015, 11:31 PM

@abrahams I've checked and the methods in question get called concurrently, so having the engines be shared would cause concurrent access to the seed again, which is what I had hoped to prevent with these patches in the first place. I don't know of a better approach than this, but if someone ever finds one, they are surely free to put it in :)

abrahams added a comment.EditedDec 14 2015, 3:00 AM

Cool, I was just curious. Ship it! ✔️

This revision was automatically updated to reflect the committed changes.