-
Notifications
You must be signed in to change notification settings - Fork 0
Home
This project emerged from a discussion on how the Intel MKL library can best be wrapped for use with C++. The code here is my suggestion for how it should be done. The main ideas are:
- Decouple the algorithms from the containers.
- Provide wrapper functions that accept any conforming matrix type as an argument. In the wrapper figure out what the appropriate values for the various matrix shape parameters to the raw MKL function; i.e., it is possible to use any matrix type with these functions.
- The wrapper functions figure out what underlying MKL function to call based on the type of data (float, double, MKL_Complex8/16) contained in the matrix.
- Provide some generic functionality like an STL allocator so that STL-container-managed-storage can be used with MKL without loss of precision or performance.
- Provide a simple matrix class that serves as a container for the raw matrix elements.
The basic ideas are implemented, but at the moment only CBLAS level 1 and cblas_?gemm functions are wrapped. All the “plumbing” is in place however, and it is a fairly mechanical task to add wrappers for functions. If this is useful to you and you are comfortable with a little C++ template coding please contribute – where there are more elegant methods for achieving the functionality provided, please let me know.
The code in the include files should be generic C++ but I’m doing all development on Linux with gcc 4.2, so there may be some issues on other platforms.
Example usage:
#include
#include “cppmkl/cppmkl_cblas.h”
#include “cppmkl/matrix.h”
#include “matrix_utils.h”int main()
cppmkl::matrix B; initm(B, “2 4; 7 8”); cppmkl::matrix C(2,2); cppmkl::cblas_gemm(A, B, C); CBLAS_TRANSPOSE transa = CblasTrans; cppmkl::cblas_gemm(A, B, C, transa); CBLAS_TRANSPOSE transb = CblasNoTrans; double alpha = 2.0; cppmkl::cblas_gemm(A, B, C, transa, transb, alpha); return 0;
{
cppmkl::matrix A;
initm(A, “2 -1; 4 5”);}
cppmkl::matrix can be instantiated to contain any numeric type including double, float, MKL_Complex8 and MKL_Complex16. Any matrix type can be used instead of cppmkl::matrix as long as it provides size1(), size2() and data() functions, so it should be easy to wrap boost, gsl or any other matrix types.
The library also provides an STL allocator so you can write code like:
std::vector<double, cppmkl::cppmkl_allocator<double> > data(10000);
and the allocator will use MKL_malloc and MKL_free to manage the memory and ensure that it is properly aligned for use with MKL functions.