ShMatrixImpl.hpp

00001 // Sh: A GPU metaprogramming language.
00002 //
00003 // Copyright 2003-2005 Serious Hack Inc.
00004 // 
00005 // This library is free software; you can redistribute it and/or
00006 // modify it under the terms of the GNU Lesser General Public
00007 // License as published by the Free Software Foundation; either
00008 // version 2.1 of the License, or (at your option) any later version.
00009 //
00010 // This library is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013 // Lesser General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU Lesser General Public
00016 // License along with this library; if not, write to the Free Software
00017 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
00018 // MA  02110-1301, USA
00020 #ifndef SHMATRIXIMPL_HPP
00021 #define SHMATRIXIMPL_HPP
00022 
00023 #include <iostream>
00024 #include <algorithm>
00025 #include <string>
00026 #include <sstream>
00027 #include "ShMatrix.hpp"
00028 #include "ShUtility.hpp"
00029 #include "ShLibMisc.hpp"
00030 
00031 namespace SH {
00032 
00033 //Constructors, destructors
00034 template<int Rows, int Cols, ShBindingType Binding, typename T>
00035 ShMatrix<Rows, Cols, Binding, T>::ShMatrix()
00036 { 
00037   if (Binding != SH_INPUT) {
00038     for (int i = 0; i < std::min(Rows, Cols); i++)
00039       m_data[i][i] = 1.0;
00040   }
00041 }
00042 
00043 template<int Rows, int Cols, ShBindingType Binding, typename T>
00044 template<ShBindingType Binding2>
00045 ShMatrix<Rows, Cols, Binding, T>::ShMatrix(const ShMatrix<Rows, Cols, Binding2, T>& other)
00046 {
00047   for (int i = 0; i < Rows; i++)
00048     m_data[i] = other[i];
00049 }
00050 
00051 template<int Rows, int Cols, ShBindingType Binding, typename T>
00052 ShMatrix<Rows, Cols, Binding, T>::~ShMatrix()
00053 {
00054 }
00055 
00056 //Operators
00057 template<int Rows, int Cols, ShBindingType Binding, typename T>
00058 ShMatrix<Rows, Cols, Binding, T>&
00059 ShMatrix<Rows, Cols, Binding, T>::operator=(const ShMatrix<Rows, Cols, Binding, T>& other)
00060 {
00061   for (int i = 0; i < Rows; i++)
00062     m_data[i] = other[i];
00063   return *this;
00064 }
00065 
00066 template<int Rows, int Cols, ShBindingType Binding, typename T>
00067 template<ShBindingType Binding2>
00068 ShMatrix<Rows, Cols, Binding, T>&
00069 ShMatrix<Rows, Cols, Binding, T>::operator=(const ShMatrix<Rows, Cols, Binding2, T>& other)
00070 {
00071   for (int i = 0; i < Rows; i++)
00072     m_data[i] = other[i];
00073   return *this;
00074 }
00075 
00076 template<int Rows, int Cols, ShBindingType Binding, typename T>
00077 ShMatrix<Rows, Cols, Binding, T>& ShMatrix<Rows, Cols, Binding, T>::operator=(const T& scalar)
00078 {
00079   for (int i = 0; i < Rows; i++) {
00080     m_data[i] = fillcast<Cols>(0.0);
00081     if (i < Cols) {
00082       m_data[i][i] = scalar;
00083     }
00084   }
00085   return *this;
00086 }
00087 
00088 template<int Rows, int Cols, ShBindingType Binding, typename T>
00089 ShMatrix<Rows, Cols, Binding, T>& ShMatrix<Rows, Cols, Binding, T>::operator=(const ShGeneric<1, T>& scalar)
00090 {
00091   for (int i = 0; i < Rows; i++) {
00092     m_data[i] = fillcast<Cols>(0.0);
00093     if (i < Cols) {
00094       m_data[i][i] = scalar;
00095     }
00096   }
00097   return *this;
00098 }
00099 
00100 template<int Rows, int Cols, ShBindingType Binding, typename T>
00101 ShAttrib<Cols, Binding, T>& ShMatrix<Rows, Cols, Binding, T>::operator[](int i)
00102 {
00103   return m_data[i];
00104 }
00105 
00106 template<int Rows, int Cols, ShBindingType Binding, typename T>
00107 const ShAttrib<Cols, Binding, T>& ShMatrix<Rows, Cols, Binding, T>::operator[](int i) const
00108 {
00109   return m_data[i];
00110 }
00111 
00112 template<int Rows, int Cols, ShBindingType Binding, typename T>
00113 template<ShBindingType Binding2>
00114 ShMatrix<Rows, Cols, Binding, T>&
00115 ShMatrix<Rows, Cols, Binding, T>::operator+=(const ShMatrix<Rows, Cols, Binding2, T>& other)
00116 {
00117   for (int i = 0; i < Rows; i++)
00118     m_data[i] += other[i];
00119   return *this;
00120 }
00121 
00122 template<int Rows, int Cols, ShBindingType Binding, typename T>
00123 template<ShBindingType Binding2>
00124 ShMatrix<Rows, Cols, Binding, T>&
00125 ShMatrix<Rows, Cols, Binding, T>::operator-=(const ShMatrix<Rows, Cols, Binding2, T>& other)
00126 {
00127   for (int i = 0; i < Rows; i++)
00128     m_data[i] -= other[i];
00129   return *this;
00130 }
00131 
00132 template<int Rows, int Cols, ShBindingType Binding, typename T>
00133 template<ShBindingType Binding2>
00134 ShMatrix<Rows, Cols, Binding, T>&
00135 ShMatrix<Rows, Cols, Binding, T>::operator/=(const ShMatrix<Rows, Cols, Binding2, T>& other)
00136 {
00137   for (int i = 0; i < Rows; i++)
00138     m_data[i] /= other[i];
00139   return *this;
00140 }
00141 
00142 template<int Rows, int Cols, ShBindingType Binding, typename T>
00143 template<ShBindingType Binding2>
00144 ShMatrix<Rows, Cols, Binding, T>&
00145 ShMatrix<Rows, Cols, Binding, T>::operator*=(const ShMatrix<Rows, Cols, Binding2, T>& other)
00146 {
00147   ShMatrix<Rows, Cols, SH_TEMP, T> r = *this * other;
00148   *this = r;
00149   return *this;
00150 }
00151 
00152 template<int Rows, int Cols, ShBindingType Binding, typename T>
00153 ShMatrix<Rows, Cols, Binding, T>&
00154 ShMatrix<Rows, Cols, Binding, T>::operator-()
00155 {
00156   for (int i = 0; i < Rows; i++)
00157     m_data[i] = -(m_data[i]);
00158   return *this;
00159 }
00160 
00161 template<int Rows, int Cols, ShBindingType Binding, typename T>
00162 ShMatrix<Rows, Cols, Binding, T>&
00163 ShMatrix<Rows, Cols, Binding, T>::operator*=(const ShGeneric<1, T>& other)
00164 {
00165   for (int i = 0; i < Rows; i++)
00166     m_data[i] *= other;
00167   return *this;
00168 }
00169   
00170 template<int Rows, int Cols, ShBindingType Binding, typename T>
00171 ShMatrix<Rows, Cols, Binding, T>&
00172 ShMatrix<Rows, Cols, Binding, T>::operator/=(const ShGeneric<1, T>& other)
00173 {
00174   for (int i = 0; i < Rows; i++)
00175     m_data[i] /= other;
00176   return *this;
00177 }
00178 
00179 template<int Rows, int Cols, ShBindingType Binding, typename T>
00180 ShMatrixRows<Rows, Cols, T>
00181 ShMatrix<Rows, Cols, Binding, T>::operator()() const
00182 {
00183   return ShMatrixRows<Rows, Cols, T>(*this);
00184 }
00185 
00186 template<int Rows, int Cols, ShBindingType Binding, typename T>
00187 ShMatrixRows<1, Cols, T>
00188 ShMatrix<Rows, Cols, Binding, T>::operator()(int i0) const
00189 {
00190   return ShMatrixRows<1, Cols, T>(*this, i0);
00191 }
00192 
00193 template<int Rows, int Cols, ShBindingType Binding, typename T>
00194 ShMatrixRows<2, Cols, T>
00195 ShMatrix<Rows, Cols, Binding, T>::operator()(int i0, int i1) const
00196 {
00197   return ShMatrixRows<2, Cols, T>(*this, i0, i1);
00198 }
00199 
00200 template<int Rows, int Cols, ShBindingType Binding, typename T>
00201 ShMatrixRows<3, Cols, T>
00202 ShMatrix<Rows, Cols, Binding, T>::operator()(int i0, int i1, int i2) const
00203 {
00204   return ShMatrixRows<3, Cols, T>(*this, i0, i1, i2);
00205 }
00206 
00207 template<int Rows, int Cols, ShBindingType Binding, typename T>
00208 ShMatrixRows<4, Cols, T>
00209 ShMatrix<Rows, Cols, Binding, T>::operator()(int i0, int i1, int i2, int i3) const
00210 {
00211   return ShMatrixRows<4, Cols, T>(*this, i0, i1, i2, i3);
00212 }
00213 
00214 template<int Rows, int Cols, ShBindingType Binding, typename T>
00215 std::ostream& operator<<(std::ostream& out,
00216                          const ShMatrix<Rows, Cols, Binding, T>& m)
00217 {
00218   for (int k = 0; k < Rows; k++) {   
00219     out << m[k];
00220     out << std::endl;
00221   }
00222     
00223   return out;
00224 }
00225   
00226   
00227 template<int Rows, int Cols, ShBindingType Binding, typename T>
00228 ShMatrix<Rows - 1, Cols -1, SH_TEMP, T>
00229 ShMatrix<Rows, Cols, Binding, T>::subMatrix(int rowToRemove,
00230                                             int columnToRemove) const
00231 {
00232   ShMatrix<Rows - 1, Cols - 1, SH_TEMP, T> myMatrix;
00233     
00234   int indices[Cols - 1];
00235   for (int i = 0; i < columnToRemove; i++) 
00236     indices[i] = i;
00237   for (int i = columnToRemove + 1; i < Cols; i++) 
00238     indices[i - 1] = i;
00239     
00240   for (int i = 0; i < rowToRemove; i++) {
00241     myMatrix[i].clone(m_data[i].template swiz<Cols - 1>(indices));
00242   }
00243     
00244   for (int i = rowToRemove + 1; i < Rows; i++) {
00245     myMatrix[i - 1].clone(m_data[i].template swiz<Cols - 1>(indices));
00246   }
00247     
00248   return myMatrix;
00249 }
00250 
00251 template<int Rows,int Cols,ShBindingType Binding, typename T>
00252 void ShMatrix<Rows, Cols, Binding, T>::setTranslation(const ShGeneric<Rows-1, T>& trans){
00253                                                         
00254   for(int i = 0;i<(Rows-1);i++)
00255     m_data[i][(Cols-1)] = trans[i];
00256 }
00257   
00258   
00259 template<int Rows,int Cols,ShBindingType Binding, typename T>
00260 void ShMatrix<Rows, Cols, Binding, T>::setScaling(const ShGeneric<Rows-1, T>& scale){
00261   for(int i = 0;i<(Rows-1);i++)
00262     m_data[i][i] = scale[i];
00263 }
00264 
00266 // ShMatrixRows stuff //
00268 
00269 
00270 template<int Rows, int Cols, typename T>
00271 template<int OR, ShBindingType Binding>
00272 ShMatrixRows<Rows, Cols, T>::ShMatrixRows(const ShMatrix<OR, Cols, Binding, T>& source,
00273                                           int i0)
00274 {
00275   SH_STATIC_CHECK(Rows == 1, Constructing_Non_1_Row_Matrix_From_1_Row);
00276   
00277   m_data[0].clone(source[i0]);
00278 }
00279 
00280 template<int Rows, int Cols, typename T>
00281 template<int OR, ShBindingType Binding>
00282 ShMatrixRows<Rows, Cols, T>::ShMatrixRows(const ShMatrix<OR, Cols, Binding, T>& source,
00283                                           int i0, int i1)
00284 {
00285   SH_STATIC_CHECK(Rows == 2, Constructing_Non_2_Row_Matrix_From_2_Rows);
00286   
00287   m_data[0].clone(source[i0]);
00288   m_data[1].clone(source[i1]);
00289 }
00290 
00291 template<int Rows, int Cols, typename T>
00292 template<int OR, ShBindingType Binding>
00293 ShMatrixRows<Rows, Cols, T>::ShMatrixRows(const ShMatrix<OR, Cols, Binding, T>& source,
00294                                           int i0, int i1, int i2)
00295 {
00296   SH_STATIC_CHECK(Rows == 3, Constructing_Non_3_Row_Matrix_From_3_Rows);
00297   
00298   m_data[0].clone(source[i0]);
00299   m_data[1].clone(source[i1]);
00300   m_data[2].clone(source[i2]);
00301 }
00302 
00303 template<int Rows, int Cols, typename T>
00304 template<int OR, ShBindingType Binding>
00305 ShMatrixRows<Rows, Cols, T>::ShMatrixRows(const ShMatrix<OR, Cols, Binding, T>& source,
00306                                           int i0, int i1, int i2, int i3)
00307 {
00308   SH_STATIC_CHECK(Rows == 4, Constructing_Non_4_Row_Matrix_From_4_Rows);
00309   
00310   m_data[0].clone(source[i0]);
00311   m_data[1].clone(source[i1]);
00312   m_data[2].clone(source[i2]);
00313   m_data[3].clone(source[i3]);
00314 }
00315 
00316 template<int Rows, int Cols, typename T>
00317 ShMatrixRows<Rows, Cols, T>::ShMatrixRows(const ShMatrixRows<Rows, Cols, T>& other)
00318 {
00319   // TODO: clone?
00320   for (int i = 0; i < Rows; i++)
00321     m_data[i] = other.m_data[i];
00322 }
00323 
00324 template<int Rows, int Cols, typename T>
00325 ShMatrixRows<Rows, Cols, T>&
00326 ShMatrixRows<Rows, Cols, T>::operator=(const ShMatrixRows<Rows, Cols, T>& other)
00327 {
00328   // TODO: clone?
00329   for (int i = 0; i < Rows; i++)
00330     m_data[i] = other.m_data[i];
00331 }
00332 
00333 template<int Rows, int Cols, typename T>
00334 ShMatrix<Rows, Cols, SH_TEMP, T>
00335 ShMatrixRows<Rows, Cols, T>::operator()() const
00336 {
00337   ShMatrix<Rows, Cols, SH_TEMP, T> r;
00338   for (int i = 0; i < Rows; i++) r[i].clone(m_data[i]);
00339   return r;
00340 }
00341 
00342 template<int Rows, int Cols, typename T>
00343 ShMatrix<Rows, 1, SH_TEMP, T>
00344 ShMatrixRows<Rows, Cols, T>::operator()(int i0) const
00345 {
00346   ShMatrix<Rows, 1, SH_TEMP, T> r;
00347   for (int i = 0; i < Rows; i++) r[i].clone(m_data[i](i0));
00348   return r;
00349 }
00350 
00351 template<int Rows, int Cols, typename T>
00352 ShMatrix<Rows, 2, SH_TEMP, T>
00353 ShMatrixRows<Rows, Cols, T>::operator()(int i0, int i1) const
00354 {
00355   ShMatrix<Rows, 2, SH_TEMP, T> r;
00356   for (int i = 0; i < Rows; i++) r[i].clone(m_data[i](i0, i1));
00357   return r;
00358 }
00359 
00360 template<int Rows, int Cols, typename T>
00361 ShMatrix<Rows, 3, SH_TEMP, T>
00362 ShMatrixRows<Rows, Cols, T>::operator()(int i0, int i1, int i2) const
00363 {
00364   ShMatrix<Rows, 3, SH_TEMP, T> r;
00365   for (int i = 0; i < Rows; i++) r[i].clone(m_data[i](i0, i1, i2));
00366   return r;
00367 }
00368 
00369 template<int Rows, int Cols, typename T>
00370 ShMatrix<Rows, 4, SH_TEMP, T>
00371 ShMatrixRows<Rows, Cols, T>::operator()(int i0, int i1, int i2, int i3) const
00372 {
00373   ShMatrix<Rows, 4, SH_TEMP, T> r;
00374   for (int i = 0; i < Rows; i++) r[i].clone(m_data[i](i0, i1, i2, i3));
00375   return r;
00376 }
00377 
00378 template<int Rows, int Cols, ShBindingType Binding, typename T>
00379 void ShMatrix<Rows, Cols, Binding, T>::range(host_type low, host_type high)
00380 {
00381   for (int i = 0; i < Rows; i++) m_data[i].range(low, high);
00382 }
00383 
00384 template<int Rows, int Cols, ShBindingType Binding, typename T>
00385 std::string ShMatrix<Rows, Cols, Binding, T>::name() const
00386   {
00387   return ShMeta::name();
00388   }
00389 
00390 template<int Rows, int Cols, ShBindingType Binding, typename T>
00391 void ShMatrix<Rows, Cols, Binding, T>::name(const std::string& n)
00392   {
00393   ShMeta::name(n);
00394   
00395   for (int i = 0; i < Rows; i++)
00396     {
00397     std::stringstream s;
00398     s << n << ".row[" << i << "]";
00399     m_data[i].name(s.str());
00400     }
00401   }
00402 
00403 template<int Rows, int Cols, ShBindingType Binding, typename T>
00404 bool ShMatrix<Rows, Cols, Binding, T>:: has_name() const
00405   {
00406   return ShMeta::has_name();
00407   }
00408   
00409 template<int Rows, int Cols, ShBindingType Binding, typename T>
00410 bool ShMatrix<Rows, Cols, Binding, T>::internal() const
00411   {
00412   return ShMeta::internal();
00413   }
00414 
00415 template<int Rows, int Cols, ShBindingType Binding, typename T>
00416 void ShMatrix<Rows, Cols, Binding, T>::internal(bool internal)
00417   {
00418   ShMeta::internal(internal);
00419   
00420   for (int i = 0; i < Rows; i++)
00421     {
00422     m_data[i].internal(internal);
00423     }
00424   }
00425 
00426 template<int Rows, int Cols, ShBindingType Binding, typename T>
00427 std::string ShMatrix<Rows, Cols, Binding, T>::title() const
00428   {
00429   return ShMeta::title();
00430   }
00431 
00432 template<int Rows, int Cols, ShBindingType Binding, typename T>
00433 void ShMatrix<Rows, Cols, Binding, T>::title(const std::string& t)
00434   {
00435   ShMeta::title(t);
00436 
00437   for (int i = 0; i < Rows; i++)
00438     {
00439     std::stringstream s;
00440     s << t << ".row[" << i << "]";
00441     m_data[i].title(s.str());
00442     }
00443   }
00444 
00445 template<int Rows, int Cols, ShBindingType Binding, typename T>
00446 std::string ShMatrix<Rows, Cols, Binding, T>::description() const
00447   {
00448   return ShMeta::description();
00449   }
00450 
00451 template<int Rows, int Cols, ShBindingType Binding, typename T>
00452 void ShMatrix<Rows, Cols, Binding, T>::description(const std::string& d)
00453   {
00454   ShMeta::description(d);
00455 
00456   for (int i = 0; i < Rows; i++)
00457     {
00458     std::stringstream s;
00459     s << d << ".row[" << i << "]";
00460     m_data[i].description(s.str());
00461     }
00462   }
00463 
00464 template<int Rows, int Cols, ShBindingType Binding, typename T>
00465 std::string ShMatrix<Rows, Cols, Binding, T>::meta(const std::string& key) const
00466   {
00467   return ShMeta::meta(key);
00468   }
00469 
00470 template<int Rows, int Cols, ShBindingType Binding, typename T>
00471 void ShMatrix<Rows, Cols, Binding, T>::meta(const std::string& key, const std::string& value)
00472   {
00473   ShMeta::meta(key, value);
00474 
00475   for (int i = 0; i < Rows; i++)
00476     {
00477     std::stringstream s;
00478     s << value << ".row[" << i << "]";
00479     m_data[i].meta(key, s.str());
00480     }
00481   }
00482 
00483 template<int Rows, int Cols, ShBindingType Binding, typename T>
00484 void ShMatrix<Rows, Cols, Binding, T>::getValues(host_type dest[]) const
00485 {
00486   int x = 0;
00487   for (int i=0; i < Rows; i++) {
00488     for (int j=0; j < Cols; j++) {
00489       typedef ShDataVariant<T, SH_HOST> VariantType;
00490       typedef ShPointer<VariantType> VariantTypePtr;
00491       VariantTypePtr c = variant_cast<T, SH_HOST>(m_data[i].getVariant());
00492       dest[x] = (*c)[j];
00493       x++;
00494     }
00495   }
00496 }
00497 
00498 }
00499 
00500 #endif

Generated on Wed Nov 9 15:29:37 2005 for Sh by  doxygen 1.4.5