[phobos] Parallelism in Phobos

David Simcha dsimcha at gmail.com
Fri Aug 27 08:55:42 PDT 2010


This should do the job:

import std.stdio, std.perf, core.atomic;

// Cut and pasted from ParallelFuture.
void atomicIncUint(ref uint num) {
    asm {
        mov EAX, num;
        lock;
        inc int ptr [EAX];
        mov EAX, [EAX];
    }
}

void main() {
    auto pc = new PerformanceCounter;
    pc.start;

    shared uint num = 0;
    while(num < 100_000_000) {
        atomicIncUint(num);
    }

    pc.stop;
    writeln(pc.milliseconds);  // 1772 ms

    pc.start;

    num = 0;
    while(num < 100_000_000) {
        atomicOp!"+="(num, 1U);  // 2539 ms
    }

    pc.stop;
    writeln(pc.milliseconds);
}

So the difference is there, but it's not huge.  In addition, the lack of
anything like atomicOp!"++"(num); seems kind of weird.

Also, atomicOp shouldn't be so picky about types.  For example, atomically
adding an int to a uint should work.

On Fri, Aug 27, 2010 at 11:43 AM, Andrei Alexandrescu <andrei at erdani.com>wrote:

> This should be validated by benchmarking.
>
> Andrei
>
>
> On 8/27/10 7:45 PDT, David Simcha wrote:
>
>> IIRC (maybe this has changed recently) atomic increments in core.atomic
>> are based on CAS instructions in a while loop, which is how more generic
>> lock free primitives are made.  Atomic increment should be special cased
>> to directly use lock; inc [someRegister];.
>>
>> On Fri, Aug 27, 2010 at 10:40 AM, Sean Kelly <sean at invisibleduck.org
>> <mailto:sean at invisibleduck.org>> wrote:
>>
>>    On Aug 27, 2010, at 7:11 AM, David Simcha wrote:
>>     >
>>     >>
>>     >> I see you have some CAS instructions. Sean, I think it's a good
>>    time to collaborate with David to put them into druntime or
>>    std.concurrency.
>>     >
>>     > Yeah, D needs a real atomics library.  core.atomic is a good
>>    start, but I won't use it until it can efficiently do things like
>>    atomic increment.
>>
>>    How could it be made more efficient?
>>    _______________________________________________
>>    phobos mailing list
>>    phobos at puremagic.com <mailto:phobos at puremagic.com>
>>
>>    http://lists.puremagic.com/mailman/listinfo/phobos
>>
>>
>>
>>
>> _______________________________________________
>> phobos mailing list
>> phobos at puremagic.com
>> http://lists.puremagic.com/mailman/listinfo/phobos
>>
> _______________________________________________
> phobos mailing list
> phobos at puremagic.com
> http://lists.puremagic.com/mailman/listinfo/phobos
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/phobos/attachments/20100827/57649fcf/attachment.html>


More information about the phobos mailing list