valarray iterators?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Sanyi

    #1

    valarray iterators?

    Hello,

    I've did some some numerical programs in C/C++ before, but decided to use
    STL to write neater code. Then again I don't want to give up speed so I have
    a couple of questions. Apparently the recommended structure for a fast
    numeric code is valarray. Alas, from the sparse documentation in the books I
    looked into and a google search it appears that there are no iterators
    associated to them. I tried to "simulate" them with pointers but I am not
    sure if I am allowed to do so.

    Here is a basic example based on vector for calculating the first few
    factorials.
    //Example 1
    #include <vector>
    #include <numeric>
    #include <iostream>
    #include <algorithm>
    using namespace std;

    const int MAX=10;
    vector<double> v1(MAX), v2(v1);

    double gen_n()
    {
    static int i = 1;
    return i++;
    }

    int main()
    {
    generate(v1.beg in(), v1.end(), gen_n);
    partial_sum(v1. begin(), v1.end(), v2.begin(),
    multiplies<doub le>() );

    ostream_iterato r <double> output(cout, " ");
    copy(v1.begin() , v1.end(), output);
    cout << endl;
    copy(v2.begin() , v2.end(), output);
    cout << endl;
    }

    // Example 2: same program with valarray instead of vector
    #include <valarray>
    #include <numeric>
    #include <iostream>
    #include <algorithm>
    using namespace std;

    const int MAX = 10;
    valarray<double > v1(MAX), v2(v1);

    double gen_n()
    {
    static int i = 1;
    return i++;
    }

    int main()
    {
    generate(&v1[0], &v1[MAX], gen_n);
    partial_sum(&v1[0], &v1[MAX], &v2[0], multiplies<doub le>() );

    ostream_iterato r <double> output(cout, " ");
    copy(&v1[0], &v1[MAX], output);
    cout << endl;
    copy(&v2[0], &v2[MAX], output);
    cout << endl;

    return 0;
    }

    When I compile it, I get the same correct result, but I wonder:

    1. Can I use &v1[0] and the like instead of iterators? Will the second
    version run correctly on every platform/compiler, or just it so happens on
    my computer?

    2. Running time: the above overly simplified code is very similar to
    something I run in a function zillion times. (MAX can be as high as 1000000,
    the generating function is much more complicated). Will the use of generate
    and partial_sum slow it down considerably and thus negating the reason for
    using valarray? I can always substitute them with loops, but then again I'll
    be back at the amorphous mess I had before.

    3. Any further tips and caveats from professional number-crunchers and
    others are more than welcome.

    Thanks,
    Sanyi


  • Ioannis Vranos

    #2
    Re: valarray iterators?

    Sanyi wrote:[color=blue]
    > Hello,
    >
    > I've did some some numerical programs in C/C++ before, but decided to use
    > STL to write neater code. Then again I don't want to give up speed so I have
    > a couple of questions. Apparently the recommended structure for a fast
    > numeric code is valarray.[/color]


    For code under severe time constraints. That's why it does not provide
    an iterator concept.

    [color=blue]
    > Alas, from the sparse documentation in the books I
    > looked into and a google search it appears that there are no iterators
    > associated to them. I tried to "simulate" them with pointers but I am not
    > sure if I am allowed to do so.[/color]


    Yes you only pointers can be used instead of iterators in the case of
    valarray.

    [color=blue]
    > When I compile it, I get the same correct result, but I wonder:
    >
    > 1. Can I use &v1[0] and the like instead of iterators? Will the second
    > version run correctly on every platform/compiler, or just it so happens on
    > my computer?[/color]


    Yes to all.


    [color=blue]
    > 2. Running time: the above overly simplified code is very similar to
    > something I run in a function zillion times. (MAX can be as high as 1000000,
    > the generating function is much more complicated). Will the use of generate
    > and partial_sum slow it down considerably and thus negating the reason for
    > using valarray? I can always substitute them with loops, but then again I'll
    > be back at the amorphous mess I had before.[/color]


    No there should be no slow-down.

    [color=blue]
    > 3. Any further tips and caveats from professional number-crunchers and
    > others are more than welcome.[/color]


    I am not in numerics, however I can say that you should use valarray
    only when you have to (that is you feel that your application is
    crawling for example).


    Here is an old test of mine that compares the times of sorting among
    valarray, vector and built in arrays in the free store. Although the
    subject is something else, you may find it useful:







    --
    Ioannis Vranos


    Comment

    • Jerry Coffin

      #3
      Re: valarray iterators?

      > I've did some some numerical programs in C/C++ before, but decided to
      use[color=blue]
      > STL to write neater code. Then again I don't want to give up speed so[/color]
      I have[color=blue]
      > a couple of questions. Apparently the recommended structure for a[/color]
      fast[color=blue]
      > numeric code is valarray. Alas, from the sparse documentation in the[/color]
      books I[color=blue]
      > looked into and a google search it appears that there are no[/color]
      iterators[color=blue]
      > associated to them. I tried to "simulate" them with pointers but I am[/color]
      not[color=blue]
      > sure if I am allowed to do so.[/color]

      The valarray class was in the wrong place at the wrong time, so to
      speak. It was oriented toward improving performance on vector
      processors, by making it easy to apply an operation to a complete
      array. Unfortunately, while this makes the code easy to vectorize, it
      also generally makes poor use of caches unless you're working with
      arrays small enough to fit enitrely in the cache.

      If you're using a typical processor where cache usage will be a
      determining factor in speed, you'll probably gain little (if anything)
      from using valarray. Yes, it's intended to prevent aliasing, and in so
      doing in theory it helps compilers produce better code. For better or
      worse, the number of compilers that produce noticeably better code
      because of this seems to be exceptionally small. A pointer to an item
      in a valarray would be an alias, which it's at least trying to ensure
      you can't do. As I recall, its idea was that you should treat the
      valarray as a whole. When that wasn't suitable, you should create a
      slice and work on the slice as a whole.

      I should add that while played with valarrays a little bit early in
      their history, I quickly concluded that they provided little real
      benefit for my purposes, so I have only a little experience with them,
      and that was long enough ago that my memory is probably a long ways
      from perfect.

      --
      Later,
      Jerry.

      The universe is a figment of its own imagination.

      Comment

      Working...