Fully transitive const is not necessary
Simen Kjaeraas
simen.kjaras at gmail.com
Thu Apr 3 07:05:44 PDT 2008
On Thu, 03 Apr 2008 15:33:20 +0200, Bill Baxter
<dnewsgroup at billbaxter.com> wrote:
> Simen Kjaeraas wrote:
>> On Thu, 03 Apr 2008 03:55:47 +0200, Bill Baxter
>> <dnewsgroup at billbaxter.com> wrote:
>>
>>> They can be run in parallel, as long as you don't stick non-pure
>>> mutating operations in between them.
>>>
>>> That's quite useful for automatically parallelizing tight loops like
>>> this:
>>>
>>> nosfx int foo(const C c) { return c.p[0]; }
>>> ...
>>>
>>> C[] array;
>>> ...// put bunch of C's in array
>>> int sum = 0;
>>> foreach(c; array) {
>>> sum += foo(c);
>>> }
>>>
>>> Those foreach bodies could be run in parallel (with appropriate reduce
>>> logic added to handling of 'sum' by compiler) since we know each call
>>> to foo() in that loop has no external side effects.
>>>
>>> This is the kind of thing OpenMP lets you do with directives like
>>> "#pragma pfor reduce". Except I don't believe OpenMP has any way to
>>> verify that foo() is really side-effect free.
>>>
>>> --bb
>> And could this not be done with real pure functions?
>> pure int foo(invariant C c) { return c.p[0];}
>> C[] array;
>> ...// put bunch of C's in array
>> int sum = 0;
>> foreach(c; array) {
>> sum += foo(cast(invariant)c);
>> }
>> We know that c will not change, so we can cast it to invariant.
>> --Simen
>
> That complicates things if you want to modify the C's in the array after
> the foreach.
>
> --bb
I can't quite see why, as long as we know the foreach has completed
running.
foo does not care whether the array changes after it's exited, and the
array does not consist of invariant Cs, they're only cast to invariant when
fed to foo.
-- Simen
More information about the Digitalmars-d
mailing list