Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

ShMemory.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 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 
00065   void removeStorage(const ShPointer<ShStorage>& storage);
00066 
00068   void add_dep(ShMemoryDep* dep);
00069 
00071   void flush();
00072 
00073 protected:
00074   ShMemory();
00075 
00076 private:
00077   int increment_timestamp();
00078 
00079   void addStorage(const ShPointer<ShStorage>& storage);
00080 
00081   typedef std::list< ShPointer<ShStorage> > StorageList;
00082   StorageList m_storages;
00083   int m_timestamp;
00084 
00086   std::list<ShMemoryDep*> dependencies;
00087 
00088   friend class ShStorage;
00089 
00091   ShMemory& operator=(const ShMemory& other);
00093   ShMemory(const ShMemory& other);
00094 };
00095 
00096 typedef ShPointer<ShMemory> ShMemoryPtr;
00097 typedef ShPointer<const ShMemory> ShMemoryCPtr;
00098 
00110 class
00111 SH_DLLEXPORT ShTransfer {
00112 public:
00113   virtual ~ShTransfer() {}
00114 
00116   virtual bool transfer(const ShStorage* from, ShStorage* to) = 0;
00117 
00121   virtual int cost(const ShStorage* from, const ShStorage* to) = 0;
00122 
00123 protected:
00124   ShTransfer(const std::string& from, const std::string& to);
00125 
00126 private:
00127   
00129   ShTransfer(const ShTransfer& other);
00131   ShTransfer& operator=(const ShTransfer& other);
00132 };
00133 
00142 class
00143 SH_DLLEXPORT ShStorage : public ShRefCountable {
00144 public:
00145   ShStorage();
00146   virtual ~ShStorage();
00147 
00149   int timestamp() const;
00150   
00152   const ShMemory* memory() const;
00154   ShMemory* memory();
00155 
00158   void sync() const;
00159 
00162   void dirty();
00163   
00166   void dirtyall();
00167   
00171   virtual std::string id() const = 0;
00172   
00175   static int cost(const ShStorage* from, const ShStorage* to);
00176 
00179   static bool transfer(const ShStorage* from, ShStorage* to);
00180 
00182   static void addTransfer(const std::string& from,
00183                           const std::string& to,
00184                           ShTransfer* transfer);
00185 
00187   void orphan();
00188   
00190   ShValueType value_type() const { return m_value_type; }
00191 
00193   void value_type(ShValueType value_type);
00194 
00196   int value_size() const { return m_value_size; }
00197 
00198 protected:
00199   ShStorage(ShMemory* memory, ShValueType value_type);
00200   
00201   ShValueType m_value_type; // type index expected of data on host
00202   int m_value_size; // size of one element
00203 
00204 private:
00205   ShMemory* m_memory;
00206   int m_timestamp;
00207 
00209   void setTimestamp(int timestamp);
00210   
00211   typedef std::map<std::pair<std::string, std::string>, ShTransfer*> TransferMap;
00212   static TransferMap* m_transfers;
00213 
00215   ShStorage(const ShStorage& other);
00217   ShStorage& operator=(const ShStorage& other);
00218 };
00219 
00220 typedef ShPointer<ShStorage> ShStoragePtr;
00221 typedef ShPointer<const ShStorage> ShStorageCPtr;
00222 
00229 class
00230 SH_DLLEXPORT ShHostStorage : public ShStorage {
00231 public:
00232   ShHostStorage(ShMemory* memory, std::size_t length, ShValueType storage_type); 
00233   ShHostStorage(ShMemory* memory, std::size_t length, void* data, ShValueType storage_type); 
00234 
00237   ~ShHostStorage();
00238 
00239   std::string id() const;
00240 
00242   std::size_t length() const;
00243 
00245   const void* data() const;
00247   void* data();
00248   
00249 private:
00250   std::size_t m_length; 
00251   void* m_data;         
00252 
00253   bool m_managed; 
00254 
00255   // NOT IMPLEMENTED
00256   ShHostStorage& operator=(const ShHostStorage& other);
00257   ShHostStorage(const ShHostStorage& other);
00258 };
00259 
00260 typedef ShPointer<ShHostStorage> ShHostStoragePtr;
00261 typedef ShPointer<const ShHostStorage> ShHostStorageCPtr;
00262 
00267 class
00268 SH_DLLEXPORT ShHostMemory : public ShMemory {
00269 public:
00270   ShHostMemory(std::size_t length, ShValueType value_type);
00271   ShHostMemory(std::size_t length, void* data, ShValueType value_type);
00272 
00273   ~ShHostMemory();
00274 
00275   ShHostStoragePtr hostStorage();
00276   ShPointer<const ShHostStorage> hostStorage() const;
00277 
00278 private:
00279   ShHostStoragePtr m_hostStorage;
00280   // NOT IMPLEMENTED
00281   ShHostMemory& operator=(const ShHostMemory& other);
00282   ShHostMemory(const ShHostMemory& other);
00283 };
00284 
00285 typedef ShPointer<ShHostMemory> ShHostMemoryPtr;
00286 typedef ShPointer<const ShHostMemory> ShHostMemoryCPtr;
00287 
00288 template<typename Functor>
00289 ShPointer<ShStorage> ShMemory::findStorage(const std::string& id, const Functor& f)
00290 {
00291   for (StorageList::iterator I = m_storages.begin(); I != m_storages.end(); ++I) {
00292     if ((*I)->id() == id && f(*I)) return *I;
00293   }
00294   return 0;
00295 }
00296 
00299 }
00300 
00301 #endif

Generated on Thu Jul 28 17:33:04 2005 for Sh by  doxygen 1.4.3-20050530