目前,有:
然而,这些都是C接口。
显然,您可以在C++代码中使用它们,但是如果这些接口是为C++设计的,则不能获得所有的好处。所以我的问题是:是否存在于本机C++?中?
发布于 2017-12-14 13:51:45
最近,田纳西大学发布了这两个接口:
使用这些本机C++实现有一些直接的好处。
让我们考虑一个基本的泛型编程示例:
假设您想要缩放一个向量:v = alpha*v
,这要归功于?scal函数。
和cblas,如果您想要“泛型”的东西,即它对于所有受支持的标量类型(float
、double
、complex<float>
、complex<double>
、.)都具有相同的接口,那么所有C函数都必须包装:
void scal(cblas_int n,float alpha, float *x, cblas_int inc) {
...
cblas_sscal(n,alpha,x,inc); <- s for float
}
void scal(cblas_int n,double alpha, double *x, cblas_int inc) {
...
cblas_dscal(n,alpha,x,inc); <- d for double
}
// do the same for complex<float>, complex<double> ...
这是因为在C中,如果两个函数有不同的名称:cblas_sscal
、cblas_dscal
.在C++中,您可以使用相同的名称scal,这是根据其参数分配给正确函数的编译器任务,在这里float
,double
.
现在的情况。与提到的https://bitbucket.org/icl/blaspp C++本机接口,这个样板代码已经写了一次了。例如,如果您查看scal.hh头文件,您有:
void scal(int64_t n, float alpha, float *x, int64_t incx ) { ... }
void scal(int64_t n, double alpha, double *x, int64_t incx ) { ... }
void scal(int64_t n, std:complex<float> alpha, std:complex<float> *x, int64_t incx ) { ... }
void scal(int64_t n, std:complex<double> alpha, std:complex<double> *x, int64_t incx ) { ... }
甚至还有一个通用的实现:
template<typename T>
void scal(int64_t n, T alpha, T *x, int64_t incx ) { ... }
因此,使用这个C++接口,很容易为您的收藏矢量/矩阵类型定义一个通用接口。例如,使用std::vector<T>
,您只需编写(独立于实际T类型):
template<typename T>
void scal(T alpha, std::vector<T>& v) {
scal(v.size(),alpha,v.data(),1);
}
不再有样板代码!
然而,要注意的是:
。
template< typename TA, typename TX >
void trmm(
blas::Layout layout,
blas::Side side,
blas::Uplo uplo,
blas::Op trans,
blas::Diag diag,
int64_t m,
int64_t n,
typename blas::traits2<TA, TX>::scalar_t alpha,
TA const *A, int64_t lda,
TX *B, int64_t ldb )
{
throw std::exception(); // not yet implemented
}
发布于 2017-12-14 14:23:56
这并不是你想要的,但是建议Blaze C++文库
Blaze是一个开源的、高性能的C++数学库,用于密集和稀疏算法.凭借其最先进的智能表达式模板实现,Blaze将领域特定语言的优雅和易用性与HPC级别的性能结合在一起,使其成为可用的最直观和最快的C++数学库之一。 烈火图书馆提供..。
我没有亲自使用它,但是基准显示,这个库运行得很好。开发社区非常友好,开放供讨论。
https://stackoverflow.com/questions/47814995
复制相似问题