00001 #ifndef cublas_Matrix_H
00002 #define cublas_Matrix_H
00003
00004 #include <lac/cublas++.h>
00005 #include <cuda_runtime.h>
00006
00007
00008
00009
00010
00011
00012 struct textureReference;
00013 struct cudaArray;
00014
00015 namespace cublas {
00016
00017 template<typename T>
00018 class TextureMatrix {
00019 const textureReference * __texref;
00020 unsigned int __n_rows, __n_cols;
00021 cudaArray * d_data;
00022
00023 void copy_from(const ::FullMatrix<T> & other);
00024 void reinit(unsigned int n_rows, unsigned int n_cols);
00025 void deinit();
00026
00027 void obtain_tex_ref(const char * _texref_name);
00028 void create_tex_ref();
00029 public:
00030 TextureMatrix();
00031 TextureMatrix(unsigned int n_rows, unsigned int n_cols);
00032 TextureMatrix(::FullMatrix<T> & other);
00033
00034 TextureMatrix(const char * _texref_name);
00035 TextureMatrix(const char * _texref_name, unsigned int n_rows, unsigned int n_cols);
00036 TextureMatrix(const char * _texref_name, ::FullMatrix<T> & other);
00037
00038 ~TextureMatrix();
00039
00040 TextureMatrix<T> & operator = (const ::FullMatrix<T> & other);
00041
00042 unsigned int n_rows() { return this->__n_rows; };
00043 unsigned int n_cols() { return this->__n_cols; };
00044 };
00045
00046 }
00047
00048
00049
00050
00051
00052
00053
00054 template<typename T>
00055 cublas::TextureMatrix<T>::TextureMatrix() : d_data(NULL) {
00056 create_tex_ref();
00057 __n_rows = 0;
00058 __n_cols = 0;
00059 }
00060
00061 template<typename T>
00062 cublas::TextureMatrix<T>::TextureMatrix(unsigned int n_rows, unsigned int n_cols) : d_data(NULL) {
00063 create_tex_ref();
00064 reinit(n_rows, n_cols);
00065 }
00066
00067 template<typename T>
00068 cublas::TextureMatrix<T>::TextureMatrix(::FullMatrix<T> & other) : d_data(NULL) {
00069 create_tex_ref();
00070 copy_from(other);
00071 }
00072
00073 template<typename T>
00074 cublas::TextureMatrix<T>::TextureMatrix(const char * _texref_name) : d_data(NULL) {
00075 obtain_tex_ref(_texref_name);
00076 __n_rows = 0;
00077 __n_cols = 0;
00078 }
00079
00080 template<typename T>
00081 cublas::TextureMatrix<T>::TextureMatrix(const char * _texref_name, unsigned int n_rows, unsigned int n_cols) : d_data(NULL) {
00082 obtain_tex_ref(_texref_name);
00083 reinit(n_rows, n_cols);
00084 }
00085
00086 template<typename T>
00087 cublas::TextureMatrix<T>::TextureMatrix(const char * _texref_name, ::FullMatrix<T> & other) : d_data(NULL) {
00088 obtain_tex_ref(_texref_name);
00089 copy_from(other);
00090 }
00091
00092 template<typename T>
00093 cublas::TextureMatrix<T>::~TextureMatrix() {
00094 if (d_data) {
00095 deinit();
00096 }
00097 }
00098
00099 template<typename T>
00100 void cublas::TextureMatrix<T>::reinit(unsigned int _n_rows, unsigned int _n_cols) {
00101 if (d_data != NULL && (_n_rows != __n_rows || _n_cols != __n_cols)) {
00102 deinit();
00103 }
00104
00105 if ((d_data == NULL) && (_n_rows * _n_cols > 0)) {
00106 __n_rows = _n_rows;
00107 __n_cols = _n_cols;
00108
00109
00110 cudaChannelFormatDesc channelDesc = cudaCreateChannelDesc<T>();
00111
00112
00113 cutilSafeCall(cudaMallocArray(&d_data, &channelDesc, __n_cols, __n_rows));
00114
00115
00116 cutilSafeCall(cudaBindTextureToArray(__texref, d_data, &channelDesc));
00117 }
00118 }
00119
00120 template<typename T>
00121 void cublas::TextureMatrix<T>::deinit() {
00122 if (d_data) {
00123 cutilSafeCall(cudaFreeArray(d_data));
00124 d_data = NULL;
00125 }
00126 __n_rows = 0;
00127 __n_cols = 0;
00128 }
00129
00130 template<typename T>
00131 void cublas::TextureMatrix<T>::create_tex_ref() {
00132
00133 }
00134
00135 template<typename T>
00136 void cublas::TextureMatrix<T>::obtain_tex_ref(const char * _texref_name) {
00137 cutilSafeCall(cudaGetTextureReference(&__texref, _texref_name));
00138 }
00139
00140 template<typename T>
00141 cublas::TextureMatrix<T> & cublas::TextureMatrix<T>::operator=(const ::FullMatrix<T> & other) {
00142 copy_from(other);
00143 }
00144
00145 template<typename T>
00146 void cublas::TextureMatrix<T>::copy_from(const ::FullMatrix<T> & other) {
00147 reinit(other.n_rows(), other.n_cols());
00148
00149
00150 FullMatrixAccessor<T> accessor(other);
00151 cutilSafeCall(cudaMemcpy2DToArray(this->d_data,
00152 0, 0,
00153 accessor.val(),
00154 this->n_cols() * sizeof(T),
00155 this->n_cols() * sizeof(T),
00156 this->n_rows(),
00157 cudaMemcpyHostToDevice));
00158 }
00159
00160
00161 #endif
00162