Boost logo

Boost :

Subject: Re: [boost] for_each abstraction
From: Jeffrey Lee Hellrung, Jr. (jhellrung_at_[hidden])
Date: 2010-11-17 15:58:03


On 11/17/2010 11:55 AM, Karsten Ahnert wrote:
>> What do you mean by "handles"? Do you want to just package up the
>> for_each function together with the sequence type you want to operate
>> on, effectively reducing everything to a unary function accepting the op
>> parameter? I don't know of such an abstraction in boost.
>
> Yes, exactly. This looks like a quite general concept for me and I was
> wondering if something has already been implemented.

Not that I know of, at least not formally and with wide dissemination
and adoption.

> Maybe a small example is:
>
> struct std_algorithm
> {
> template< Op>
> static void for_each( std::vector< double> &vec , Op op )
> {
> std::for_each( vec.begin() , vec.end() , op );
> }
> };
>
> struct thrust_algorithm
> {
> template< Op>
> static void for_each( thrust::device_vector< double> &vec , Op op )
> {
> thrust::for_each( vec.begin() , vec.end() , op );
> }
> };
>
>
> // the complex algorithm we want to implement
> template< class Algorithm , class Sequence>
> void do_something( Sequence&seq )
> {
> ...
> Algebra::for_each( seq , op() );

      ^^^^^^^
      Algorithm

> ...
> }
>
>
> // use youe code via
>
> // the standard version
> std::vector< double> vec1;
> do_something< std_algorithm>( vec1 );
>
> // the thrust (CUDA) version
> thrust::device_vector< double> vec2;
> do_something< thrust_algorithm>( vec2 );

I was imagining something slightly different, where the for_each
functions are more tightly coupled to the sequence types. In other
words, std::vector and thrust::device_vector both have a for_each member
function (or maybe a less intrusive for_each free function found, for
example, via ADL):

std::vector< double > vec1;
vec1.for_each(op());
thrust::device_vector< double > vec2;
vec2.for_each(op());

>> There was a topic that was brought up by...I'm going to say Joel Falcou,
>> but I may have that wrong...discussing segmented iterators and iteration
>> in general (I would search "segmented iterators"). I believe his use
>> case was enabling the use of simd instruction in the iteration, which
>> required special treatment of the first few and last few elements of the
>> range. One abstraction that makes this efficient is to push the
>> iteration to the responsibility of the range or sequence you're
>> operating on. This seems to be roughly what you might want to consider
>> doing here (and is basically equivalent to packaging the for_each
>> function up with the sequence, as I suggested above).
>
> I think you mean this one
>
> http://thread.gmane.org/gmane.comp.lib.boost.devel/209431

Yes, that's it. Apologies to Mathias.

> altough I am not sure I fully understand.

It's a different use case but with (I believe) a similar solution:
ranges and sequences provide their own iteration mechanism.

- Jeff


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk