Re: Why C is much slower than Fortran

Daniel Barker <sokal@holyrood.ed.ac.uk>
2 Jun 1999 01:41:14 -0400

          From comp.compilers

Related articles
[6 earlier articles]
Re: Why C is much slower than Fortran gneuner@dyn.com (1999-05-16)
Re: Why C is much slower than Fortran reid@micro.ti.com (Reid Tatge) (1999-05-20)
Re: Why C is much slower than Fortran jhallen@world.std.com (1999-05-29)
Re: Why C is much slower than Fortran hwstock@wizard.com (H.W. Stockman) (1999-06-02)
Re: Why C is much slower than Fortran erik@arbat.com (Erik Corry) (1999-06-02)
Re: Why C is much slower than Fortran lindahl@pbm.com (1999-06-02)
Re: Why C is much slower than Fortran sokal@holyrood.ed.ac.uk (Daniel Barker) (1999-06-02)
Re: Why C is much slower than Fortran djb@koobera.math.uic.edu (1999-06-02)
Re: Why C is much slower than Fortran Peter.Mayne@compaq.com (Peter Mayne) (1999-06-03)
Re: Why C is much slower than Fortran lindahl@pbm.com (1999-06-06)
Re: Why C is much slower than Fortran john@iastate.edu (1999-06-12)
Re: Why C is much slower than Fortran erik@arbat.com (Erik Corry) (1999-06-14)
Re: Why C is much slower than Fortran jeff@jeff-jackson.com (Jeffrey Glen Jackson) (1999-06-19)
| List of all articles for this month |

From: Daniel Barker <sokal@holyrood.ed.ac.uk>
Newsgroups: comp.lang.c++,comp.compilers
Date: 2 Jun 1999 01:41:14 -0400
Organization: Edinburgh University
References: <3710584B.1C0F05F5@hotmail.com> <7esvkr$v8g$1@camel29.mindspring.com> <37122569.BF79CD19@hotmail.com> <3712311D.BA9027D4@hotmail.com> <7etenl$nk5$1@alexander.INS.CWRU.Edu> 99-04-048 99-04-105 99-04-107
Keywords: performance, comment

On 30 Apr 1999, Daniel Barker wrote:


> void f(unsigned int *ip, int lim, size_t *used, unsigned char byte_val)
> /* for each element i of the lim-element array whose first element is
> * pointed to by ip, set the first used[i] bytes to byte_val and the
> * remaining bytes to zero; used[i] must lie in the interval
> * [0, sizeof(unsigned int)]; the resulting value of ip[i] is
> * implementation-defined */
> {


[snip]


> }


(As someone has kindly pointed out, the action of the function did not
match this comment. Apologies.)


[snip]


> [C lets you alias anything to anything, and that does indeed cause
> optimization problems. The C9X draft has a "restrict" keyword that lets
> you assure the compiler that a pointer doesn't have aliasing problems.
> I presume the MIPS hack says that only data of the same type can be
> aliased, sort of the same thing here. -John]


Might a mechanism to specify independence of loop iterations (for whatever
reason) have been more useful than a mechanism to restrict pointers?


For example, consider this code, using permutation vectors:


    /* ... */


    int a[LIM]; /* array of scalars */
    int b[LIM]; /* array of scalars */


    int ac[LIM]; /* array of cursors to a */
    int bc[LIM]; /* array of cursors to b */


    int i; /* loop counter */


    /* ... set all elements of "a" and "b"; set each element of "ac" to a
      * different value in the interval [0.."LIM"-1]; set each element of
      * "bc" to a different value in the interval [0.."LIM"-1]; and then ...
      */


    for (i = 0; i < LIM; i++)
        a[ac[i]] = a[ac[i]] * b[bc[i]];


Array "a" is only accessed through a single (implied) pointer and array
"b" is only accessed through a single (implied) pointer. But this says
nothing about whether the iterations of this loop are independent. I do
not see how C9x's "restrict" could help much here. It could inform the
compiler that the arrays in question, "a", "ac", "b" and "bc", do not
overlap. However, if the array definitions are visible (e.g.,
function-scope within the function including the loop), the compiler could
already deduce this without "restrict".


Permutations also a present a problem for optimized FORTRAN 77
compilation, though there exist implementation-dependent ways around the
problem in either C or FORTRAN. (I do not know enough Fortran 90 to say
where that lies.)


Another area a more general "independence" directive would be useful is if
there is a function call in a loop:


    int f(int i)
    /* a "pure" function, relying on nothing external, static or
      * I/O-related */
    {
        return i * i;
    }


    /* ... */


    for (i = 0; i < LIM; i++)
      a[i] = f(a[i]);


If f() is compiled separately from the function including the loop, it
might be useful for the programmer to tell the compiler that all calls of
f() are independent, and may be performed in parallel. It would be
possible to specify this in the interface to f(). However, specifying it
at the loop would allow the same mechanism to be used as with the above
code using permutations.


If neither C9x nor ANSI C++ has a standard "independence" directive, this
is a shame, and I hope one is added in the next standard along the line.


Daniel Barker.
[I believe that you could declare restricted pointers within the loop to
give the compiler the required hint. -John]


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.