In this example we show how one can directly interface the ViennaCL BLAS-like shared library. For simplicity, C++ is used as a host language, but one may also use C or any other language which is able to call C functions.
The first step is to include the necessary headers:
#include <iostream>
#include <vector>
In this example we only create two vectors and swap values between them.
Before we start we need to create a backend. This allows one later to specify OpenCL command queues, CPU threads, or CUDA streams while preserving common interfaces.
Host-based Execution
We use the host to swap all odd entries of x (all ones) with all even entries in y (all twos):
viennacl::linalg::host_based::detail::extract_raw_pointer<float>(host_x), 1, 2,
viennacl::linalg::host_based::detail::extract_raw_pointer<float>(host_y), 0, 2);
std::cout << " --- Host ---" << std::endl;
std::cout << "host_x: " << host_x << std::endl;
std::cout << "host_y: " << host_y << std::endl;
CUDA-based Execution
We use CUDA to swap all even entries in x (all ones) with all odd entries in y (all twos)
#ifdef VIENNACL_WITH_CUDA
std::cout << " --- CUDA ---" << std::endl;
std::cout << "cuda_x: " << cuda_x << std::endl;
std::cout << "cuda_y: " << cuda_y << std::endl;
#endif
OpenCL-based Execution
Use OpenCL to swap all odd entries in x (all ones) with all odd entries in y (all twos)
#ifdef VIENNACL_WITH_OPENCL
long context_id = 0;
ViennaCLOpenCLSswap(my_backend, half_size,
viennacl::traits::opencl_handle(opencl_x).get(), 1, 2,
viennacl::traits::opencl_handle(opencl_y).get(), 1, 2);
std::cout << " --- OpenCL ---" << std::endl;
std::cout << "opencl_x: " << opencl_x << std::endl;
std::cout << "opencl_y: " << opencl_y << std::endl;
#endif
The last step is to clean up by destroying the backend:
std::cout << "!!!! TUTORIAL COMPLETED SUCCESSFULLY !!!!" << std::endl;
return EXIT_SUCCESS;
}
Full Example Code
#include <iostream>
#include <vector>
{
std::size_t size = 10;
viennacl::linalg::host_based::detail::extract_raw_pointer<float>(host_x), 1, 2,
viennacl::linalg::host_based::detail::extract_raw_pointer<float>(host_y), 0, 2);
std::cout << " --- Host ---" << std::endl;
std::cout << "host_x: " << host_x << std::endl;
std::cout << "host_y: " << host_y << std::endl;
#ifdef VIENNACL_WITH_CUDA
std::cout << " --- CUDA ---" << std::endl;
std::cout << "cuda_x: " << cuda_x << std::endl;
std::cout << "cuda_y: " << cuda_y << std::endl;
#endif
#ifdef VIENNACL_WITH_OPENCL
long context_id = 0;
ViennaCLOpenCLSswap(my_backend, half_size,
viennacl::traits::opencl_handle(opencl_x).get(), 1, 2,
viennacl::traits::opencl_handle(opencl_y).get(), 1, 2);
std::cout << " --- OpenCL ---" << std::endl;
std::cout << "opencl_x: " << opencl_x << std::endl;
std::cout << "opencl_y: " << opencl_y << std::endl;
#endif
std::cout << "!!!! TUTORIAL COMPLETED SUCCESSFULLY !!!!" << std::endl;
return EXIT_SUCCESS;
}