set random seed for compute uniform_real_distribution?

38 views
Skip to first unread message

Joshua Laferriere

unread,
Jun 28, 2016, 12:06:20 AM6/28/16
to boost-compute
Here's the code, always produces the same values

#include
<iostream>
#include <vector>
#include <algorithm>
#include <boost/compute/system.hpp>
#include <boost/compute/algorithm/copy.hpp>
#include <boost/compute/algorithm/accumulate.hpp>
#include <boost/compute/container/vector.hpp>
#include <boost/compute/types/fundamental.hpp>

#include <boost/compute/algorithm/inclusive_scan.hpp>
//#include <boost/compute/interop/opencv/core.hpp>
//#include <boost/compute/interop/opencv/highgui.hpp>
#include <boost/compute/random/default_random_engine.hpp>
#include <boost/compute/random/uniform_real_distribution.hpp>
#include <boost/compute/utility/source.hpp>
//#define CL_USE_DEPRECATED_OPENCL_1_1_APIS
//#undef CL_VERSION_1_2

namespace compute = boost::compute;

int main()
{

    // get the default compute device
    compute::device gpu = compute::system::default_device();

    // create a compute context and command queue

    compute::context ctx(gpu);
    compute::command_queue queue(ctx, gpu);

    std::vector<float> host_vector(1000000);

    size_t steps = 1000;

    // create vector on device and populate with rng's
    // calaculate random values for each step
    compute::vector<float> random_vector(steps, ctx);
    compute::default_random_engine random_engine(queue);
    compute::uniform_real_distribution<float> random_distribution(0.f, 4.f);

    random_distribution.generate(random_vector.begin(), random_vector.end(), random_engine, queue);

    float result = compute::accumulate(random_vector.begin(), random_vector.end(), 0, queue);

    std::cout << "before" << std::endl;

    // copy data back to the host
    compute::copy(
        random_vector.begin(), random_vector.end(), host_vector.begin(), queue
    );


    std::cout << "vector: [ ";
    boost::compute::copy(
        host_vector.begin(), host_vector.end(),
        std::ostream_iterator<float>(std::cout, ", "),
        queue
    );
    std::cout << "]" << std::endl;

    std::cout << result << std::endl;

    return 0;
}

Kyle Lutz

unread,
Jun 28, 2016, 12:31:27 AM6/28/16
to Joshua Laferriere, boost-compute
This seems to work for me, with a few small fixes.

First, you're constructing "host_vector" with 1000000 elements, but
only constructing "random_vector" with "steps" elements (1000).
Calling "random_distribution.generate()" will fill "random_vector"
with random numbers, but when copying back and printing on the host,
it will display the 1000 random numbers followed by 999000 zeros.

Here's a reduced version which should work:

int main()
{
compute::device gpu = compute::system::default_device();
compute::context ctx(gpu);
compute::command_queue queue(ctx, gpu);

size_t steps = 10;

compute::vector<float> random_vector(steps, ctx);
compute::default_random_engine random_engine(queue);
compute::uniform_real_distribution<float> random_distribution(0.f, 4.f);
random_distribution.generate(
random_vector.begin(), random_vector.end(), random_engine, queue
);

std::cout << "vector: [ ";
boost::compute::copy(
random_vector.begin(), random_vector.end(),
std::ostream_iterator<float>(std::cout, ", "),
queue
);
std::cout << "]" << std::endl;
}

Let me know if you still encounter issues.

-kyle
Message has been deleted

Joshua Laferriere

unread,
Jun 28, 2016, 9:58:09 AM6/28/16
to boost-compute, laferr...@gmail.com
Is this done on the device or the host?  I was aiming on generating random #'s directly on the device.  I don't see if ever being copied back to the host

Joshua Laferriere

unread,
Jun 28, 2016, 10:24:33 AM6/28/16
to boost-compute, laferr...@gmail.com
nevermind, I see you copy it to an iterator

Joshua Laferriere

unread,
Jun 28, 2016, 10:02:23 PM6/28/16
to boost-compute, laferr...@gmail.com
the same #'s are reproduced over and over again :(

I swear it was working this morning, and I just recompiled, and same issue.


On Monday, June 27, 2016 at 9:31:27 PM UTC-7, Kyle Lutz wrote:

Joshua Laferriere

unread,
Jun 28, 2016, 10:03:28 PM6/28/16
to boost-compute, laferr...@gmail.com
just checked... the same #'s were being reproduced this morning as well.

The issue I was having was not in getting the original function to work, but that all my #'s are the same.  Hence the question about setting a seed.

Joshua Laferriere

unread,
Jun 28, 2016, 10:42:04 PM6/28/16
to boost-compute, laferr...@gmail.com
let me clarify, all my sets produce the same sequence of #'s.

Kyle Lutz

unread,
Jun 28, 2016, 11:50:24 PM6/28/16
to Joshua Laferriere, boost-compute
On Tue, Jun 28, 2016 at 7:42 PM, Joshua Laferriere
<laferr...@gmail.com> wrote:
> let me clarify, all my sets produce the same sequence of #'s.

You can use the seed() [1] method to seed any of the random number
engines. For example:

int seed = 12345;
boost::compute::default_random_engine engine(queue);
engine.seed(seed, queue);

If you want to seed it with different values each time you run the
application, you can use the current time (via the std::time()
function).

-kyle

[1] http://www.boost.org/doc/libs/develop/libs/compute/doc/html/boost/compute/mersenne_twister_engine.html

Joshua Laferriere

unread,
Jun 29, 2016, 9:49:05 AM6/29/16
to boost-compute
Thank you!

Where's the documentation for such things?

Kyle Lutz

unread,
Jun 29, 2016, 11:19:51 AM6/29/16
to Joshua Laferriere, boost-compute
There is documentation for this in the API reference, for example,
see: http://www.boost.org/doc/libs/develop/libs/compute/doc/html/boost/compute/mersenne_twister_engine.html

-kyle
Reply all
Reply to author
Forward
0 new messages