CudaTypesBase.h

Go to the documentation of this file.
00001 /******************************************************************************
00002 *       SOFA, Simulation Open-Framework Architecture, version 1.0 beta 3      *
00003 *                (c) 2006-2008 MGH, INRIA, USTL, UJF, CNRS                    *
00004 *                                                                             *
00005 * This library is free software; you can redistribute it and/or modify it     *
00006 * under the terms of the GNU Lesser General Public License as published by    *
00007 * the Free Software Foundation; either version 2.1 of the License, or (at     *
00008 * your option) any later version.                                             *
00009 *                                                                             *
00010 * This library is distributed in the hope that it will be useful, but WITHOUT *
00011 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or       *
00012 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License *
00013 * for more details.                                                           *
00014 *                                                                             *
00015 * You should have received a copy of the GNU Lesser General Public License    *
00016 * along with this library; if not, write to the Free Software Foundation,     *
00017 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.          *
00018 *******************************************************************************
00019 *                               SOFA :: Modules                               *
00020 *                                                                             *
00021 * Authors: The SOFA Team and external contributors (see Authors.txt)          *
00022 *                                                                             *
00023 * Contact information: contact@sofa-framework.org                             *
00024 ******************************************************************************/
00025 #ifndef SOFA_GPU_CUDA_CUDATYPES_BASE_H
00026 #define SOFA_GPU_CUDA_CUDATYPES_BASE_H
00027 
00028 #include "CudaTypes.h"
00029 #include <sofa/component/linearsolver/FullMatrix.h>
00030 
00031 //#define DEBUG_BASE
00032 
00033 namespace sofa {
00034 namespace gpu {
00035 namespace cuda {
00036 
00037 using namespace sofa::defaulttype;
00038     
00039 template <class T>  
00040 class CudaBaseMatrix : public BaseMatrix {  
00041     public :
00042     
00043         CudaMatrix<T> & getCudaMatrix() {
00044             return m;           
00045         }
00046     
00047         void resize(int nbCol, int nbRow) {
00048             m.resize(nbCol,nbRow,BSIZE);        
00049         }   
00050     
00051         void resize(int nbCol, int nbRow,int ws) {
00052             m.resize(nbCol,nbRow,ws);       
00053         }   
00054     
00055         int rowSize() const {
00056             return m.getSizeY();
00057         }
00058     
00059         int colSize() const {
00060             return m.getSizeX();
00061         }
00062     
00063         SReal element(int i, int j) const {
00064             return m[i][j];
00065         }
00066             
00067         void clear() {
00068             for (unsigned j=0; j<m.getSizeX(); j++) {
00069                 for (unsigned i=0; i<m.getSizeY(); i++) {
00070                   m[j][i] = (T)(0.0);
00071                 }
00072             }
00073         }
00074             
00075         void set(int j, int i, double v) {
00076 #ifdef DEBUG_BASE
00077             if ((j>=rowSize()) || (i>=colSize())) { 
00078                 printf("forbidden acces %d %d\n",j,i);
00079                 exit(1);
00080             }           
00081 #endif          
00082             m[j][i] = (T)v;
00083         }
00084     
00085         void add(int j, int i, double v) {
00086 #ifdef DEBUG_BASE
00087             if ((j>=rowSize()) || (i>=colSize())) {     
00088                 printf("forbidden acces %d %d\n",j,i);
00089                 exit(1);
00090             }           
00091 #endif          
00092             m[j][i] += (T)v;
00093         }
00094 
00095     private :
00096         CudaMatrix<T> m;            
00097 };  
00098 
00099 template <class T>
00100 class CudaBaseVector : public BaseVector {
00101 
00102     public :
00103         CudaVector<T>& getCudaVector() {
00104             return v;
00105         }
00106     
00107         T& operator[](int i) {
00108             return v[i];
00109         }
00110     
00111         const T& operator[](int i) const {
00112             return v[i];
00113         }
00114     
00115         void resize(int nbRow) {
00116             v.resize(nbRow);
00117         }
00118 
00119         void resize(int nbRow,int warp_size) {
00120             v.resize(nbRow,warp_size);
00121         }
00122         
00123         int size() const {
00124             return v.size();
00125         }
00126 
00127         SReal element(int i) const {
00128             return v[i];
00129         }
00130 
00131         void clear() {
00132           for (int i=0; i<size(); i++) v[i]=(T)(0.0);
00133         }
00134     
00135         void set(int i, SReal val) {
00136             v[i] = (T) val;
00137         }
00138     
00139         void add(int i, SReal val) {
00140             v[i] += (T)val;
00141         }
00142 
00143     private :
00144         CudaVector<T> v;        
00145 }; 
00146 
00147 } // namespace cuda
00148 } // namespace gpu
00149 } // namespace sofa
00150 
00151 #endif

Generated on Sun Nov 23 04:04:17 2008 for Sofa by  doxygen 1.5.7.1