Exemple #1
0
int hpx_main()
{
    using hpx::make_continuation;

    increment_action inc;
    increment_with_future_action inc_f;
    mult2_action mult;

    // test locally, fully equivalent to plain hpx::async
    {
        hpx::future<int> f1 = hpx::async_continue(
            inc, make_continuation(), hpx::find_here(), 42);
        HPX_TEST_EQ(f1.get(), 43);

        hpx::promise<std::int32_t> p;
        hpx::shared_future<std::int32_t> f = p.get_future();

        hpx::future<int> f2 = hpx::async_continue(
            inc_f, make_continuation(), hpx::find_here(), f);

        p.set_value(42);
        HPX_TEST_EQ(f2.get(), 43);
    }

    // test remotely, if possible, fully equivalent to plain hpx::async
    std::vector<hpx::id_type> localities = hpx::find_remote_localities();
    if (!localities.empty())
    {
        hpx::future<int> f1 = hpx::async_continue(
            inc, make_continuation(), localities[0], 42);
        HPX_TEST_EQ(f1.get(), 43);

        hpx::promise<std::int32_t> p;
        hpx::shared_future<std::int32_t> f = p.get_future();

        hpx::future<int> f2 = hpx::async_continue(
            inc_f, make_continuation(), localities[0], f);

        p.set_value(42);
        HPX_TEST_EQ(f2.get(), 43);
    }

    // test chaining locally
    {
        hpx::future<int> f = hpx::async_continue(
            inc, make_continuation(mult), hpx::find_here(), 42);
        HPX_TEST_EQ(f.get(), 86);

        f = hpx::async_continue(inc,
            make_continuation(mult, make_continuation()), hpx::find_here(), 42);
        HPX_TEST_EQ(f.get(), 86);

        f = hpx::async_continue(inc,
            make_continuation(mult, make_continuation(inc)), hpx::find_here() ,42);
        HPX_TEST_EQ(f.get(), 87);

        f = hpx::async_continue(inc,
            make_continuation(mult, make_continuation(inc, make_continuation())),
            hpx::find_here(), 42);
        HPX_TEST_EQ(f.get(), 87);
    }

    // test chaining remotely, if possible
    if (!localities.empty())
    {
        hpx::future<int> f = hpx::async_continue(inc,
            make_continuation(mult, localities[0]), localities[0], 42);
        HPX_TEST_EQ(f.get(), 86);

        f = hpx::async_continue(inc,
            make_continuation(mult, localities[0], make_continuation()),
            localities[0], 42);
        HPX_TEST_EQ(f.get(), 86);

        f = hpx::async_continue(inc,
            make_continuation(mult, localities[0],
                make_continuation(inc)), localities[0], 42);
        HPX_TEST_EQ(f.get(), 87);

        f = hpx::async_continue(inc,
            make_continuation(mult, localities[0],
                make_continuation(inc, make_continuation())), localities[0], 42);
        HPX_TEST_EQ(f.get(), 87);

        f = hpx::async_continue(inc,
            make_continuation(mult, localities[0],
                make_continuation(inc, localities[0])), localities[0], 42);
        HPX_TEST_EQ(f.get(), 87);

        f = hpx::async_continue(inc,
            make_continuation(mult, localities[0],
                make_continuation(inc, localities[0], make_continuation())),
            localities[0], 42);
        HPX_TEST_EQ(f.get(), 87);

        f = hpx::async_continue(inc,
            make_continuation(mult), localities[0], 42);
        HPX_TEST_EQ(f.get(), 86);

        f = hpx::async_continue(inc,
            make_continuation(mult, make_continuation()), localities[0], 42);
        HPX_TEST_EQ(f.get(), 86);
    }

    return hpx::finalize();
}
Exemple #2
0
int hpx_main()
{
    using hpx::make_continuation;

    increment_action inc;
    increment_with_future_action inc_f;
    mult2_action mult;

    // test locally, fully equivalent to plain hpx::async
    {
        callback_called.store(0);
        hpx::future<int> f1 = hpx::async_continue_cb(
            inc, make_continuation(), hpx::find_here(), &cb, 42);
        HPX_TEST_EQ(f1.get(), 43);

        hpx::promise<std::int32_t> p;
        hpx::shared_future<std::int32_t> f = p.get_future();

        hpx::future<int> f2 = hpx::async_continue_cb(
            inc_f, make_continuation(), hpx::find_here(), &cb, f);

        p.set_value(42);
        HPX_TEST_EQ(f2.get(), 43);

        // The callback should have been called 2 times. wait for a short period
        // of time, to allow it for it to be fully executed
        hpx::this_thread::sleep_for(std::chrono::milliseconds(100));
        HPX_TEST_EQ(callback_called.load(), 2);
    }

    // test remotely, if possible, fully equivalent to plain hpx::async
    std::vector<hpx::id_type> localities = hpx::find_remote_localities();
    if (!localities.empty())
    {
        callback_called.store(0);
        hpx::future<int> f1 = hpx::async_continue_cb(
            inc, make_continuation(), localities[0], &cb, 42);
        HPX_TEST_EQ(f1.get(), 43);

        hpx::promise<std::int32_t> p;
        hpx::shared_future<std::int32_t> f = p.get_future();

        hpx::future<int> f2 = hpx::async_continue_cb(
            inc_f, make_continuation(), localities[0], &cb, f);

        p.set_value(42);
        HPX_TEST_EQ(f2.get(), 43);

        // The callback should have been called 2 times. wait for a short period
        // of time, to allow it for it to be fully executed
        hpx::this_thread::sleep_for(std::chrono::milliseconds(100));
        HPX_TEST_EQ(callback_called.load(), 2);
    }

    // test chaining locally
    {
        callback_called.store(0);
        hpx::future<int> f = hpx::async_continue_cb(
            inc, make_continuation(mult), hpx::find_here(), &cb, 42);
        HPX_TEST_EQ(f.get(), 86);

        f = hpx::async_continue_cb(inc,
            make_continuation(mult, make_continuation()), hpx::find_here(),
            &cb, 42);
        HPX_TEST_EQ(f.get(), 86);

        f = hpx::async_continue_cb(inc,
            make_continuation(mult, make_continuation(inc)), hpx::find_here(),
            &cb, 42);
        HPX_TEST_EQ(f.get(), 87);

        f = hpx::async_continue_cb(inc,
            make_continuation(mult, make_continuation(inc, make_continuation())),
            hpx::find_here(), &cb, 42);
        HPX_TEST_EQ(f.get(), 87);

        // The callback should have been called 4 times. wait for a short period
        // of time, to allow it for it to be fully executed
        hpx::this_thread::sleep_for(std::chrono::milliseconds(100));
        HPX_TEST_EQ(callback_called.load(), 4);
    }

    // test chaining remotely, if possible
    if (!localities.empty())
    {
        callback_called.store(0);
        hpx::future<int> f = hpx::async_continue_cb(inc,
            make_continuation(mult, localities[0]), localities[0], &cb, 42);
        HPX_TEST_EQ(f.get(), 86);

        f = hpx::async_continue_cb(inc,
            make_continuation(mult, localities[0], make_continuation()),
            localities[0], &cb, 42);
        HPX_TEST_EQ(f.get(), 86);

        f = hpx::async_continue_cb(inc,
            make_continuation(mult, localities[0],
                make_continuation(inc)), localities[0], &cb, 42);
        HPX_TEST_EQ(f.get(), 87);

        f = hpx::async_continue_cb(inc,
            make_continuation(mult, localities[0],
                make_continuation(inc, make_continuation())), localities[0],
            &cb, 42);
        HPX_TEST_EQ(f.get(), 87);

        f = hpx::async_continue_cb(inc,
            make_continuation(mult, localities[0],
                make_continuation(inc, localities[0])), localities[0], &cb, 42);
        HPX_TEST_EQ(f.get(), 87);

        f = hpx::async_continue_cb(inc,
            make_continuation(mult, localities[0],
                make_continuation(inc, localities[0], make_continuation())),
                localities[0], &cb, 42);
        HPX_TEST_EQ(f.get(), 87);

        f = hpx::async_continue_cb(inc,
            make_continuation(mult), localities[0], &cb, 42);
        HPX_TEST_EQ(f.get(), 86);

        f = hpx::async_continue_cb(inc,
            make_continuation(mult, make_continuation()), localities[0],
            &cb, 42);
        HPX_TEST_EQ(f.get(), 86);

        // The callback should have been called 8 times. wait for a short period
        // of time, to allow it for it to be fully executed
        hpx::this_thread::sleep_for(std::chrono::milliseconds(100));
        HPX_TEST_EQ(callback_called.load(), 8);
    }

    return hpx::finalize();
}