即使在上下文切换之后也得到相同的结果
Posted
技术标签:
【中文标题】即使在上下文切换之后也得到相同的结果【英文标题】: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
请帮我解决这个问题。 提前致谢
【问题讨论】:
viennacl.sourceforge.net/doc/manual-multi-device.html 我猜你对上下文、平台和设备感到困惑。你错过了这个:viennacl::ocl::current_context().switch_device(1);并且 gtx1080 在 gemm 的东西中不可能有与 CPU 相同的性能 【参考方案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() 以等待内核完成执行。
【讨论】:
以上是关于即使在上下文切换之后也得到相同的结果的主要内容,如果未能解决你的问题,请参考以下文章
结合中断上下文切换和进程上下文切换分析Linux内核的一般执行过程
结合中断上下文切换和进程上下文切换分析Linux内核的一般执行过程
Lab3:结合中断上下文切换和进程上下文切换分析Linux内核一般执行过程
结合中断上下文切换和进程上下文切换分析Linux内核的一般执行过程
即使在使用 DelegatingSecurityContextAsyncTaskExecutor 之后,Spring 上下文也没有得到更新