常用CULA实例
CULA 安装包里包含了几个例子,用来显示如何使用CULA.这些例子放在‘examples’ 目录里.
这里列出几个常用的,使用CULA函数的例子.
Initialization and Shutdown
#include <cula.h> culaStatus s; s = culaInitialize(); if(s != culaNoError) { printf("%s\n", culaGetErrorString(s)); /* ... Error Handling ... */ } /* ... Your code ... */ culaShutdown();
Argument Errors
#include <cula.h> culaStatus s; s = culaSgeqrf(-1, -1, NULL, -1, NULL); /* obviously wrong */ if(s != culaNoError) { if(s == culaArgumentError) printf("Argument %d has an illegal value\n", culaGetErrorInfo()); else printf("%s\n", culaGetErrorString(s)); }
Data Errors
#include <cula.h> float* A = malloc(20*20*sizeof(float)); memset(A, 0, 20*20*sizeof(float)); /* singular matrix, illegal for LU (getrf) */ int ipiv[20]; s = culaSgetrf(20, 20, A, 20, ipiv); if( s != culaNoError ) { if( s == culaDataError ) printf("Data error with code %d, please see LAPACK documentation\n", culaGetErrorInfo()); else printf("%s\n", culaGetErrorString(s)); }
Printing Errors to the Console
#include <cula.h> culaStatus s; int info; char buf[256]; s = <cula function>; if( s != culaNoError ) { info = culaGetErrorInfo(); culaGetErrorInfoString(s, info, buf, sizeof(buf)); printf("%s", buf); }
使用 C++ 接口
#include <cula.hpp> template<class T> void GenericLU(T* A, int N) // type of T will be determined by the compiler { std::vector<int> piv(N); // no need for type specifier - determined automatically on overloads culaStatus s = culaGetrf(N, N, A, N, &ipiv[0]); // check errors }
检查有没有正确连接到库
#include <cula.h> int MeetsMinimumCulaRequirements() { int cudaMinimumVersion = culaGetCudaMinimumVersion(); int cudaRuntimeVersion = culaGetCudaRuntimeVersion(); int cudaDriverVersion = culaGetCudaDriverVersion(); int cublasMinimumVersion = culaGetCublasMinimumVersion(); int cublasRuntimeVersion = culaGetCublasRuntimeVersion(); if(cudaRuntimeVersion < cudaMinimumVersion) { printf("CUDA runtime version is insufficient; " "version %d or greater is required\n", cudaMinimumVersion); return 0; } if(cudaDriverVersion < cudaMinimumVersion) { printf("CUDA driver version is insufficient; " "version %d or greater is required\n", cudaMinimumVersion); return 0; } if(cublasRuntimeVersion < cublasMinimumVersion) { printf("CUBLAS runtime version is insufficient; " "version %d or greater is required\n", cublasMinimumVersion); return 0; } return 1; }