Skip to content

compute::copy returns incorrect values when template parameter mismatchs #600

@MossanTokyo

Description

@MossanTokyo

When templates for compute::vector<T> gpu_vec and std::vector<U> host_vec mismatch, compute::copy(gpu_vec.begin(),gpu_vec.end(),host_vec.begin(),queue) returns incorrect values.

I think it would be nice to throw an error or write a warning in documentation.

The following code demonstrate the issue and was compiled under Visual Studio 2015 with the latest develop branch.

#include <boost/compute.hpp>
#include<iostream>

namespace compute = boost::compute;

// select device
compute::device select_device()
{
    auto devices = compute::system::devices();

    int i = 0;
    for (auto & elem : devices)
    {
        std::cout << "device[" << i++ << "]: " << elem.name() << std::endl;
    }

    int device_id;
    std::cout << "select device: ";
    std::cin >> device_id;

    return devices[device_id];
}

int main()
{
    // select device
    auto device = select_device();
    compute::context context(device);
    compute::command_queue queue(context, device);

    //sample data
    compute::vector<int> gpu_vec(5, context);


    int start = 0;

    // generate 0 to 4
    compute::iota(gpu_vec.begin(), gpu_vec.end(), start, queue);


    // gpu->host
    // instead of std::vector<int>
    std::vector<double> host_vec(5);

    compute::copy(gpu_vec.begin(),gpu_vec.end(),host_vec.begin(),queue);

    // check result
    for (auto & elem : host_vec)
    {
        std::cout << elem << std::endl;
    }

    return 0;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions