Boost logo

Boost :

From: Frank Mori Hess (frank.hess_at_[hidden])
Date: 2008-05-20 10:10:15


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Monday 19 May 2008 22:22 pm, Dean Michael Berris wrote:
>
> For the special container types I've been pondering, aside from
> automatically waiting on contained futures there's the possibility of
> making standard algorithms more specialized for these container types
> (or via tag dispatch).
>
> Perhaps having a special std::transform() implementation (the five
> argument version) that does a 'wait on any' future in the container
> and in a non-deterministic order apply a function on the contained
> future value that becomes available, and the result mapped to the
> appropriate location in the resulting container.
>

That's interesting. I can do that with ordinary containers/std::transform by
using a poet::active_function from libpoet as the binary function passed to
the transform algorithm. The active function's scheduler performs each add
operation as the futures become ready. Example code attached.

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)

iD8DBQFIMtvK5vihyNWuA4URApfIAJ0Uc9x8MJZ/mEubT12aKzmTVE5Y6wCg4Eyu
iuYOLdFnfltTe3nnoYWd4m0=
=r7As
-----END PGP SIGNATURE-----

--Boundary-00=_KvtMIT/zS/g8HD4
Content-Type: text/x-c++src;
  charset="iso-8859-1";
  name="transform.cpp"
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
        filename="transform.cpp"

// Copyright (C) Frank Mori Hess 2007-2008
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)

#include <cmath>
#include <functional>
#include <iostream>
#include <poet/active_function.hpp>
#include <vector>

int main()
{
        unsigned i;
        const unsigned vector_length = 10;
        // input data, assigned actual values later
        std::vector<poet::promise<double> > x_promises;
        std::vector<poet::promise<double> > y_promises;
        // we push the promises on onto the std::vector one at a time to make
        // sure the std::vector doesn't copy-construct them from each other.
        for(i = 0; i < vector_length; ++i)
        {
                x_promises.push_back(poet::promise<double>());
                y_promises.push_back(poet::promise<double>());
        }

        // active_functions
        std::plus<double> passive_adder;
        poet::active_function<double (double, double)> active_adder(passive_adder);

        std::vector<poet::future<double> > x;
        std::copy(x_promises.begin(), x_promises.end(), std::back_inserter(x));
        std::vector<poet::future<double> > y;
        std::copy(y_promises.begin(), y_promises.end(), std::back_inserter(y));
        std::vector<poet::future<double> > sum;

        std::transform(x.begin(), x.end(), y.begin(), std::back_inserter(sum), active_adder);

        // fulfill input promises
        for(i = 0; i < x_promises.size(); ++i)
        {
                x_promises.at(i).fulfill(i % 4);
                y_promises.at(i).fulfill(i % 3);
        }

        // wait for futures to become ready and convert them to values
        for(i = 0; i < sum.size(); ++i)
        {
                std::cout << "sum " << i << " = " << sum.at(i).get() << "\n";
        }

        return 0;
}

--Boundary-00=_KvtMIT/zS/g8HD4--


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