ShMemory.hpp

00001 // Sh: A GPU metaprogramming language.
00002 //
00003 // Copyright 2003-2006 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 SHMEMORY_HPP
00021 #define SHMEMORY_HPP
00022 
00023 #include <list>
00024 #include <map>
00025 #include <utility>
00026 #include <string>
00027 #include "ShDllExport.hpp"
00028 #include "ShRefCount.hpp"
00029 #include "ShMemoryDep.hpp"
00030 #include "ShStorageType.hpp"
00031 
00032 namespace SH {
00033 
00038 class ShStorage;
00039 
00048 class
00049 SH_DLLEXPORT ShMemory : public ShRefCountable {
00050 public:
00051   virtual ~ShMemory();
00052 
00054   int timestamp() const;
00055 
00057   ShPointer<ShStorage> findStorage(const std::string& id);
00058 
00061   template<typename Functor>
00062   ShPointer<ShStorage> findStorage(const std::string& id, const Functor& f);
00063   template<typename Functor>
00064   ShPointer<ShStorage> findStorage(const std::string& id, Functor& f);
00065 
00067   void removeStorage(const ShPointer<ShStorage>& storage);
00068 
00070   void add_dep(ShMemoryDep* dep);
00071 
00073   void flush();
00074   
00076   void freeze(bool state);
00077 
00078 protected:
00079   ShMemory();
00080 
00081 private:
00082   int increment_timestamp();
00083 
00084   void addStorage(const ShPointer<ShStorage>& storage);
00085 
00086   typedef std::list< ShPointer<ShStorage> > StorageList;
00087   StorageList m_storages;
00088   int m_timestamp;
00089 
00090   bool m_frozen;
00091   int m_frozenTimestamp;
00092 
00094   std::list<ShMemoryDep*> dependencies;
00095 
00096   friend class ShStorage;
00097 
00099   ShMemory& operator=(const ShMemory& other);
00101   ShMemory(const ShMemory& other);
00102 };
00103 
00104 typedef ShPointer<ShMemory> ShMemoryPtr;
00105 typedef ShPointer<const ShMemory> ShMemoryCPtr;
00106 
00118 class
00119 SH_DLLEXPORT ShTransfer {
00120 public:
00121   virtual ~ShTransfer() {}
00122 
00124   virtual bool transfer(const ShStorage* from, ShStorage* to) = 0;
00125 
00129   virtual int cost(const ShStorage* from, const ShStorage* to) = 0;
00130 
00131 protected:
00132   ShTransfer(const std::string& from, const std::string& to);
00133 
00134 private:
00135   
00137   ShTransfer(const ShTransfer& other);
00139   ShTransfer& operator=(const ShTransfer& other);
00140 };
00141 
00150 class
00151 SH_DLLEXPORT ShStorage : public ShRefCountable {
00152 public:
00153   ShStorage();
00154   virtual ~ShStorage();
00155 
00157   int timestamp() const;
00158   
00160   const ShMemory* memory() const;
00162   ShMemory* memory();
00163 
00166   void sync() const;
00167 
00170   void dirty();
00171   
00174   void dirtyall();
00175   
00179   virtual std::string id() const = 0;
00180   
00183   static int cost(const ShStorage* from, const ShStorage* to);
00184 
00187   static bool transfer(const ShStorage* from, ShStorage* to);
00188 
00190   static void addTransfer(const std::string& from,
00191                           const std::string& to,
00192                           ShTransfer* transfer);
00193 
00195   void orphan();
00196   
00198   ShValueType value_type() const { return m_value_type; }
00199 
00201   void value_type(ShValueType value_type);
00202 
00204   int value_size() const { return m_value_size; }
00205 
00206 protected:
00207   ShStorage(ShMemory* memory, ShValueType value_type);
00208   
00209   ShValueType m_value_type; // type index expected of data on host
00210   int m_value_size; // size of one element
00211 
00212 private:
00213   ShMemory* m_memory;
00214   int m_timestamp;
00215 
00217   void setTimestamp(int timestamp);
00218   
00219   typedef std::map<std::pair<std::string, std::string>, ShTransfer*> TransferMap;
00220   static TransferMap* m_transfers;
00221 
00223   ShStorage(const ShStorage& other);
00225   ShStorage& operator=(const ShStorage& other);
00226 };
00227 
00228 typedef ShPointer<ShStorage> ShStoragePtr;
00229 typedef ShPointer<const ShStorage> ShStorageCPtr;
00230 
00237 class
00238 SH_DLLEXPORT ShHostStorage : public ShStorage {
00239 public:
00240   ShHostStorage(ShMemory* memory, std::size_t length, ShValueType storage_type); 
00241   ShHostStorage(ShMemory* memory, std::size_t length, void* data, ShValueType storage_type); 
00242 
00245   ~ShHostStorage();
00246 
00247   std::string id() const;
00248 
00250   std::size_t length() const;
00251 
00253   const void* data() const;
00255   void* data();
00256   
00257 private:
00258   std::size_t m_length; 
00259   void* m_data;         
00260 
00261   bool m_managed; 
00262 
00263   // NOT IMPLEMENTED
00264   ShHostStorage& operator=(const ShHostStorage& other);
00265   ShHostStorage(const ShHostStorage& other);
00266 };
00267 
00268 typedef ShPointer<ShHostStorage> ShHostStoragePtr;
00269 typedef ShPointer<const ShHostStorage> ShHostStorageCPtr;
00270 
00275 class
00276 SH_DLLEXPORT ShHostMemory : public ShMemory {
00277 public:
00278   ShHostMemory(std::size_t length, ShValueType value_type);
00279   ShHostMemory(std::size_t length, void* data, ShValueType value_type);
00280 
00281   ~ShHostMemory();
00282 
00283   ShHostStoragePtr hostStorage();
00284   ShPointer<const ShHostStorage> hostStorage() const;
00285 
00286 private:
00287   ShHostStoragePtr m_hostStorage;
00288   // NOT IMPLEMENTED
00289   ShHostMemory& operator=(const ShHostMemory& other);
00290   ShHostMemory(const ShHostMemory& other);
00291 };
00292 
00293 typedef ShPointer<ShHostMemory> ShHostMemoryPtr;
00294 typedef ShPointer<const ShHostMemory> ShHostMemoryCPtr;
00295 
00296 template<typename Functor>
00297 ShPointer<ShStorage> ShMemory::findStorage(const std::string& id, Functor& f)
00298 {
00299   for (StorageList::iterator I = m_storages.begin(); I != m_storages.end(); ++I) {
00300     if ((*I)->id() == id && f(*I)) return *I;
00301   }
00302   return 0;
00303 }
00304 
00305 template<typename Functor>
00306 ShPointer<ShStorage> ShMemory::findStorage(const std::string& id, const Functor& f)
00307 {
00308   for (StorageList::iterator I = m_storages.begin(); I != m_storages.end(); ++I) {
00309     if ((*I)->id() == id && f(*I)) return *I;
00310   }
00311   return 0;
00312 }
00313 
00316 }
00317 
00318 #endif

Generated on Thu Feb 16 14:51:36 2006 for Sh by  doxygen 1.4.6