【问题标题】:Getting same result even after context switching即使在上下文切换之后也得到相同的结果
【发布时间】:2019-02-25 13:29:31
【问题描述】:

我在 GPU 和 CPU 中运行矩阵乘法时得到相同的结果。

这是我的代码:

    viennacl::ocl::set_context_platform_index(1, 1);
    viennacl::ocl::set_context_platform_index(0, 0);

    viennacl::ocl::switch_context(0);
    std::cout << "--- Computing matrix-matrix product using viennacl in GPU ---" << std::endl;
    timer.start();
    vcl_C = viennacl::linalg::prod(vcl_A, vcl_B);
    exec_time = timer.get();
    std::cout << " - Execution time: " << exec_time << std::endl;
    std::cout << "result on GPU: "<<viennacl::ocl::current_device().name() << std::endl;

//same operation on CPU

    std::cout << "coming here" << std::endl;
    viennacl::ocl::switch_context(1);
    std::cout << "--- Computing matrix-matrix product using viennacl in CPU ---" << std::endl;
    timer.start();
    vcl_C = viennacl::linalg::prod(vcl_A, vcl_B);
    exec_time = timer.get();
    std::cout << " - Execution time: " << exec_time << std::endl;

    std::cout << "result on CPU: " << viennacl::ocl::current_device().name() << std::endl << std::endl;

这是我的结果:

--- Computing matrix-matrix product using viennacl in GPU ---
 - Execution time: 24.4675
result on GPU: GeForce GTX 1080
coming here
--- Computing matrix-matrix product using viennacl in CPU ---
 - Execution time: 24.4654
result on CPU: Intel(R) Xeon(R) CPU E3-1225 v5 @ 3.30GHz

请帮我解决这个问题。 提前致谢

【问题讨论】:

标签: opencl viennacl


【解决方案1】:

最后我在 CPU 和 GPU 上得到了正确的结果:

代码:

int main()
{
    typedef float     ScalarType;

    viennacl::tools::timer timer;
    double exec_timecpu;
    double exec_timegpu;

    viennacl::tools::uniform_random_numbers<ScalarType> randomNumber;

    viennacl::ocl::set_context_platform_index(1, 1);
    viennacl::ocl::set_context_platform_index(0, 0);

    viennacl::ocl::switch_context(1);

    viennacl::matrix<ScalarType> vcl_A(BLAS3_MATRIX_SIZE, BLAS3_MATRIX_SIZE);
    viennacl::matrix<ScalarType, viennacl::column_major> vcl_B(BLAS3_MATRIX_SIZE, BLAS3_MATRIX_SIZE);
    viennacl::matrix<ScalarType> vcl_C(BLAS3_MATRIX_SIZE, BLAS3_MATRIX_SIZE);

    for (unsigned int i = 0; i < vcl_A.size1(); ++i)
        for (unsigned int j = 0; j < vcl_A.size2(); ++j)
            vcl_A(i,j) = randomNumber();

    for (unsigned int i = 0; i < vcl_B.size1(); ++i)
        for (unsigned int j = 0; j < vcl_B.size2(); ++j)
            vcl_B(i,j) = randomNumber();

    std::cout << std::endl;
    std::cout << "--- Computing matrix-matrix product using viennacl in CPU ---" << std::endl;
    timer.start();
    vcl_C = viennacl::linalg::prod(vcl_A, vcl_B);
    viennacl::backend::finish();
    exec_timecpu = timer.get();

    std::cout << " - Execution time: " << exec_timecpu << std::endl;

    std::cout << "result on CPU: " << viennacl::ocl::current_device().name() << std::endl << std::endl;

    //same operation on GPU

    viennacl::ocl::switch_context(0);

    viennacl::matrix<ScalarType > vcl_GA(BLAS3_MATRIX_SIZE, BLAS3_MATRIX_SIZE);
    viennacl::matrix<ScalarType > vcl_GB(BLAS3_MATRIX_SIZE, BLAS3_MATRIX_SIZE);
    viennacl::matrix<ScalarType > vcl_GC(BLAS3_MATRIX_SIZE, BLAS3_MATRIX_SIZE);

    for (unsigned int i = 0; i < vcl_GA.size1(); ++i)
        for (unsigned int j = 0; j < vcl_GA.size2(); ++j)
            vcl_GA(i,j) = randomNumber();

    for (unsigned int i = 0; i < vcl_GB.size1(); ++i)
        for (unsigned int j = 0; j < vcl_GB.size2(); ++j)
            vcl_GB(i,j) = randomNumber();

    std::cout << "--- Computing matrix-matrix product using viennacl in GPU ---" << std::endl;
    vcl_GC = viennacl::linalg::prod(vcl_GA, vcl_GB);
    timer.start();
    vcl_GC = viennacl::linalg::prod(vcl_GA, vcl_GB);
    viennacl::backend::finish();
    exec_timegpu = timer.get();
    std::cout << " - Execution time: " << exec_timegpu << std::endl;
    std::cout << "result on GPU: "<<viennacl::ocl::current_device().name() << std::endl;

    return 0;
}

输出:

--- Computing matrix-matrix product using viennacl in CPU ---
 - Execution time: 0.559754
result on CPU: Intel(R) Xeon(R) CPU E3-1225 v5 @ 3.30GHz

--- Computing matrix-matrix product using viennacl in GPU ---
 - Execution time: 0.004177
result on GPU: GeForce GTX 1080

注意事项: *确保在 header 中定义 VIENNACL_WITH_OPENCL。

*为不同的设备创建不同的缓冲区,因为在opencl中缓冲区是与计算设备互连的,所以我们不能在两个不同的设备上使用同一个缓冲区。

**确保添加 viennacl::backend::finish() 以等待内核完成执行。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-07-25
    • 2016-08-13
    • 2017-11-26
    • 2020-05-06
    • 1970-01-01
    • 2016-06-03
    • 1970-01-01
    • 2023-04-02
    相关资源
    最近更新 更多