Fully transitive const is not necessary
Bill Baxter
dnewsgroup at billbaxter.com
Thu Apr 3 06:33:20 PDT 2008
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
More information about the Digitalmars-d
mailing list