Reputation: 385
I am getting same result while running the matrix multiplication in both GPU and CPU.
here is my code:.
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;
Here is my result:
--- 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
please help me to sort out this issue. Thanks in advance
Upvotes: 0
Views: 42
Reputation: 385
and finally i got the correct results in CPU and GPU:
code:
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;
}
output:
--- 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
Things to note: *Make sure to define VIENNACL_WITH_OPENCL in header.
*Create different buffers for different devices because in opencl the buffers are interconnected with the computing devices so that we can't use the same buffer in two different devices.
**Make sure to add viennacl::backend::finish() to wait for the kernels to finish the execution.
Upvotes: 0