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 software is provided 'as-is', without any express or implied
00006 // warranty. In no event will the authors be held liable for any damages
00007 // arising from the use of this software.
00008 // 
00009 // Permission is granted to anyone to use this software for any purpose,
00010 // including commercial applications, and to alter it and redistribute it
00011 // freely, subject to the following restrictions:
00012 // 
00013 // 1. The origin of this software must not be misrepresented; you must
00014 // not claim that you wrote the original software. If you use this
00015 // software in a product, an acknowledgment in the product documentation
00016 // would be appreciated but is not required.
00017 // 
00018 // 2. Altered source versions must be plainly marked as such, and must
00019 // not be misrepresented as being the original software.
00020 // 
00021 // 3. This notice may not be removed or altered from any source
00022 // distribution.
00024 #ifndef SHMEMORY_HPP
00025 #define SHMEMORY_HPP
00026 
00027 #include <list>
00028 #include <map>
00029 #include <utility>
00030 #include <string>
00031 #include "ShDllExport.hpp"
00032 #include "ShRefCount.hpp"
00033 #include "ShMemoryDep.hpp"
00034 #include "ShStorageType.hpp"
00035 
00036 namespace SH {
00037 
00042 class ShStorage;
00043 
00052 class
00053 SH_DLLEXPORT ShMemory : public ShRefCountable {
00054 public:
00055   virtual ~ShMemory();
00056 
00058   int timestamp() const;
00059 
00061   ShPointer<ShStorage> findStorage(const std::string& id);
00062 
00065   template<typename Functor>
00066   ShPointer<ShStorage> findStorage(const std::string& id, const Functor& f);
00067 
00069   void removeStorage(const ShPointer<ShStorage>& storage);
00070 
00072   void add_dep(ShMemoryDep* dep);
00073 
00075   void flush();
00076 
00077 protected:
00078   ShMemory();
00079 
00080 private:
00081   void updateTimestamp(int timestamp);
00082 
00083   void addStorage(const ShPointer<ShStorage>& storage);
00084 
00085   typedef std::list< ShPointer<ShStorage> > StorageList;
00086   StorageList m_storages;
00087   int m_timestamp;
00088 
00090   std::list<ShMemoryDep*> dependencies;
00091 
00092   friend class ShStorage;
00093 
00095   ShMemory& operator=(const ShMemory& other);
00097   ShMemory(const ShMemory& other);
00098 };
00099 
00100 typedef ShPointer<ShMemory> ShMemoryPtr;
00101 typedef ShPointer<const ShMemory> ShMemoryCPtr;
00102 
00114 class
00115 SH_DLLEXPORT ShTransfer {
00116 public:
00117   virtual ~ShTransfer() {}
00118 
00120   virtual bool transfer(const ShStorage* from, ShStorage* to) = 0;
00121 
00125   virtual int cost() = 0;
00126 
00127 protected:
00128   ShTransfer(const std::string& from, const std::string& to);
00129 
00130 private:
00131   
00133   ShTransfer(const ShTransfer& other);
00135   ShTransfer& operator=(const ShTransfer& other);
00136 };
00137 
00146 class
00147 SH_DLLEXPORT ShStorage : public ShRefCountable {
00148 public:
00149   ShStorage();
00150   virtual ~ShStorage();
00151 
00153   int timestamp() const;
00155   void setTimestamp(int timestamp);
00157   const ShMemory* memory() const;
00159   ShMemory* memory();
00160 
00165   void sync();
00166 
00169   void dirty();
00170   
00173   void dirtyall();
00174   
00178   virtual std::string id() const = 0;
00179   
00182   static int cost(ShStorage* from, ShStorage* to);
00183 
00186   static bool transfer(ShStorage* from, ShStorage* to);
00187 
00189   static void addTransfer(const std::string& from,
00190                           const std::string& to,
00191                           ShTransfer* transfer);
00192 
00194   void orphan();
00195   
00197   ShValueType value_type() const { return m_value_type; }
00198 
00200   void value_type(ShValueType value_type) { m_value_type = value_type; }
00201 
00203   int value_size() const { return m_value_size; }
00204 
00205 protected:
00206   ShStorage(ShMemory* memory, ShValueType value_type);
00207   
00208   ShValueType m_value_type; // type index expected of data on host
00209   int m_value_size; // size of one element
00210 
00211 private:
00212   ShMemory* m_memory;
00213   int m_timestamp;
00214 
00215   typedef std::map<std::pair<std::string, std::string>, ShTransfer*> TransferMap;
00216   static TransferMap* m_transfers;
00217 
00219   ShStorage(const ShStorage& other);
00221   ShStorage& operator=(const ShStorage& other);
00222 };
00223 
00224 typedef ShPointer<ShStorage> ShStoragePtr;
00225 typedef ShPointer<const ShStorage> ShStorageCPtr;
00226 
00233 class
00234 SH_DLLEXPORT ShHostStorage : public ShStorage {
00235 public:
00236   ShHostStorage(ShMemory* memory, int length, ShValueType storage_type); 
00237   ShHostStorage(ShMemory* memory, int length, void* data, ShValueType storage_type); 
00238 
00241   ~ShHostStorage();
00242 
00243   std::string id() const;
00244 
00246   int length() const;
00247 
00249   const void* data() const;
00251   void* data();
00252   
00253 private:
00254   int m_length; 
00255   void* m_data; 
00256 
00257   bool m_managed; 
00258 
00259   // NOT IMPLEMENTED
00260   ShHostStorage& operator=(const ShHostStorage& other);
00261   ShHostStorage(const ShHostStorage& other);
00262 };
00263 
00264 typedef ShPointer<ShHostStorage> ShHostStoragePtr;
00265 typedef ShPointer<const ShHostStorage> ShHostStorageCPtr;
00266 
00271 class
00272 SH_DLLEXPORT ShHostMemory : public ShMemory {
00273 public:
00274   ShHostMemory(int length, ShValueType value_type);
00275   ShHostMemory(int length, void* data, ShValueType value_type);
00276 
00277   ~ShHostMemory();
00278 
00279   ShHostStoragePtr hostStorage();
00280   ShPointer<const ShHostStorage> hostStorage() const;
00281 
00282 private:
00283   ShHostStoragePtr m_hostStorage;
00284   // NOT IMPLEMENTED
00285   ShHostMemory& operator=(const ShHostMemory& other);
00286   ShHostMemory(const ShHostMemory& other);
00287 };
00288 
00289 typedef ShPointer<ShHostMemory> ShHostMemoryPtr;
00290 typedef ShPointer<const ShHostMemory> ShHostMemoryCPtr;
00291 
00292 template<typename Functor>
00293 ShPointer<ShStorage> ShMemory::findStorage(const std::string& id, const Functor& f)
00294 {
00295   for (StorageList::iterator I = m_storages.begin(); I != m_storages.end(); ++I) {
00296     if ((*I)->id() == id && f(*I)) return *I;
00297   }
00298   return 0;
00299 }
00300 
00303 }
00304 
00305 #endif

Generated on Wed Jun 15 18:12:40 2005 for Sh by  doxygen 1.4.3-20050530