GPUOcelot

EmulatorDevice.h

Go to the documentation of this file.
00001 
00007 #ifndef EMULATOR_DEVICE_H_INCLUDED
00008 #define EMULATOR_DEVICE_H_INCLUDED
00009 
00010 // ocelot includes
00011 #include <ocelot/executive/interface/Device.h>
00012 
00013 // hydrazine includes
00014 #include <hydrazine/implementation/Timer.h>
00015 
00016 // stdnard library includes
00017 #include <unordered_set>
00018 
00019 namespace executive
00020 {
00021   class ExecutableKernel;
00022 }
00023 
00024 namespace executive
00025 {
00027   class EmulatorDevice : public Device
00028   {
00029     public:
00031       class MemoryAllocation : public Device::MemoryAllocation
00032       {
00033         private:
00035           size_t _size;
00037           void* _pointer;
00039           unsigned int _flags;
00041           bool _external;
00042         
00043         public:
00045           MemoryAllocation();
00047           MemoryAllocation(size_t size);
00049           MemoryAllocation(size_t size, unsigned int flags);
00051           MemoryAllocation(const ir::Global& global);
00053           MemoryAllocation(void* pointer, size_t size);
00055           MemoryAllocation(void* pointer, size_t size,
00056             unsigned int flags);
00058           ~MemoryAllocation();
00059 
00060         public:
00062           MemoryAllocation(const MemoryAllocation& a);
00064           MemoryAllocation(MemoryAllocation&& a);
00065           
00067           MemoryAllocation& operator=(const MemoryAllocation& a);
00069           MemoryAllocation& operator=(MemoryAllocation&& a);
00070       
00071         public:
00073           unsigned int flags() const;
00075           void* mappedPointer() const;
00077           void* pointer() const;
00079           size_t size() const;
00081           void copy(size_t offset, const void* host, size_t size );
00083           void copy(void* host, size_t offset, size_t size) const;
00085           void memset(size_t offset, int value, size_t size);
00087           void copy(Device::MemoryAllocation* allocation, 
00088             size_t toOffset, size_t fromOffset, size_t size) const;       
00089       };
00090 
00091     protected:
00093       class Module
00094       {
00095         public:
00097           typedef std::unordered_map<std::string, void*> GlobalMap;
00099           typedef std::unordered_map<std::string, 
00100             ExecutableKernel*> KernelMap;
00102           typedef std::vector<MemoryAllocation*> AllocationVector;
00104           typedef ir::Module::TextureMap TextureMap;
00105       
00106         public:
00108           const ir::Module* ir;
00110           Device* device;
00112           GlobalMap globals;
00114           KernelMap kernels;
00116           TextureMap textures;
00117           
00118         public:
00120           Module(const ir::Module* m = 0, Device* d = 0);
00122           Module(const Module& m);
00124           virtual ~Module();
00125           
00126         public:
00128           AllocationVector loadGlobals();
00130           virtual ExecutableKernel* getKernel(
00131             const std::string& name);
00133           ir::Texture* getTexture(const std::string& name);
00134       };
00135       
00137       class OpenGLResource
00138       {
00139         public:
00141           unsigned int buffer;
00143           void* pointer;
00144       
00145         public:
00146           OpenGLResource(unsigned int buffer = 0);
00147         
00148       };
00149 
00151       typedef std::unordered_map<std::string, Module*> ModuleMap;
00152 
00154       typedef std::map<void*, MemoryAllocation*> AllocationMap;
00155       
00157       typedef std::unordered_set<unsigned int> StreamSet;
00158       
00160       typedef std::unordered_map<unsigned int, 
00161         hydrazine::Timer::Second> EventMap;
00162       
00164       typedef std::unordered_map<unsigned int, 
00165         OpenGLResource> GraphicsMap;
00166 
00167     protected:
00169       AllocationMap _allocations;
00170       
00172       ModuleMap _modules;
00173       
00175       StreamSet _streams;
00176       
00178       EventMap _events;
00179       
00181       GraphicsMap _graphics;
00182     
00184       bool _selected;
00185       
00187       unsigned int _next;
00188       
00190       hydrazine::Timer _timer;
00191             
00192     public:
00194       EmulatorDevice(unsigned int flags = 0);
00196       virtual ~EmulatorDevice();
00197       
00198     public:
00199       Device::MemoryAllocation* getMemoryAllocation(const void* address, 
00200         AllocationType type) const;
00202       Device::MemoryAllocation* getGlobalAllocation(
00203         const std::string& module, const std::string& name);
00205       Device::MemoryAllocation* allocate(size_t size);
00207       Device::MemoryAllocation* allocateHost(size_t size, 
00208         unsigned int flags);
00210       Device::MemoryAllocation* registerHost(void* pointer, size_t size, 
00211         unsigned int flags);
00213       void free(void* pointer);
00215       MemoryAllocationVector getNearbyAllocations(void* pointer) const;
00217       MemoryAllocationVector getAllAllocations() const;
00219       void clearMemory();     
00220       
00221     public:
00223       void* glRegisterBuffer(unsigned int buffer, 
00224         unsigned int flags);
00226       void* glRegisterImage(unsigned int image, 
00227         unsigned int target, unsigned int flags);
00229       void unRegisterGraphicsResource(void* resource);
00231       void mapGraphicsResource(void** resource, int count, 
00232         unsigned int stream);
00234       void* getPointerToMappedGraphicsResource(size_t& size, 
00235         void* resource);
00237       void setGraphicsResourceFlags(void* resource, 
00238         unsigned int flags);
00240       void unmapGraphicsResource(void** resource, int count,
00241         unsigned int streamID);
00242 
00243     public:
00245       virtual void load(const ir::Module* module);
00247       void unload(const std::string& name);
00249       virtual ExecutableKernel* getKernel(const std::string& module, 
00250         const std::string& kernel);
00251 
00252     public:
00254       unsigned int createEvent(int flags);
00256       void destroyEvent(unsigned int event);
00258       bool queryEvent(unsigned int event);
00260       void recordEvent(unsigned int event, unsigned int stream);
00262       void synchronizeEvent(unsigned int event);
00264       float getEventTime(unsigned int start, unsigned int end);
00265     
00266     public:
00268       unsigned int createStream();
00270       void destroyStream(unsigned int stream);
00272       bool queryStream(unsigned int stream);
00274       void synchronizeStream(unsigned int stream);
00276       void setStream(unsigned int stream);
00277     
00278     public:
00280       void bindTexture(void* pointer,
00281         const std::string& moduleName, const std::string& textureName, 
00282         const textureReference& ref, const cudaChannelFormatDesc& desc, 
00283         const ir::Dim3& size);
00285       void unbindTexture(const std::string& moduleName, 
00286         const std::string& textureName);
00288       void* getTextureReference(const std::string& moduleName, 
00289         const std::string& textureName);
00290 
00291     public:
00303       virtual void launch(const std::string& module, 
00304         const std::string& kernel, const ir::Dim3& grid, 
00305         const ir::Dim3& block, size_t sharedMemory, 
00306         const void* argumentBlock, size_t argumentBlockSize, 
00307         const trace::TraceGeneratorVector& 
00308         traceGenerators = trace::TraceGeneratorVector(),
00309         const ir::ExternalFunctionSet* externals = 0);
00311       cudaFuncAttributes getAttributes(const std::string& module, 
00312         const std::string& kernel);
00314       unsigned int getLastError();
00316       void synchronize();
00317       
00318     public:
00320       virtual void limitWorkerThreads(unsigned int threads);      
00322       virtual void setOptimizationLevel(
00323         translator::Translator::OptimizationLevel level);
00324   };
00325 }
00326 
00327 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines