AGX Dynamics  2.37.3.2
Device.h
Go to the documentation of this file.
1 /*
2 Copyright 2007-2024. Algoryx Simulation AB.
3 
4 All AGX source code, intellectual property, documentation, sample code,
5 tutorials, scene files and technical white papers, are copyrighted, proprietary
6 and confidential material of Algoryx Simulation AB. You may not download, read,
7 store, distribute, publish, copy or otherwise disseminate, use or expose this
8 material unless having a written signed agreement with Algoryx Simulation AB, or having been
9 advised so by Algoryx Simulation AB for a time limited evaluation, or having purchased a
10 valid commercial license from Algoryx Simulation AB.
11 
12 Algoryx Simulation AB disclaims all responsibilities for loss or damage caused
13 from using this software, unless otherwise stated in written agreements with
14 Algoryx Simulation AB.
15 */
16 
17 #pragma once
18 
19 
20 #include <agx/config/AGX_USE_OPENGL.h>
21 #include <agx/config/AGX_USE_OPENCL.h>
22 #include <agx/agxCore_export.h>
23 #include <agx/Component.h>
24 #include <agx/HashTable.h>
25 #include <agx/Allocator.h>
26 #include <agx/MemoryPool.h>
27 #include <agxData/Buffer.h>
28 #include <agx/SyncTag.h>
29 #include <agx/Job.h>
30 
31 #ifdef _MSC_VER
32 # pragma warning(push)
33 # pragma warning( disable : 4275 ) // warning C4275: non dll-interface class
34 # pragma warning( disable : 4702) // warning C4702: unreachable code
35 #endif
36 
37 
38 
39 namespace agx
40 {
44  // AGX_DECLARE_POINTER_TYPES(HostDevice);
45 
57  class AGXCORE_EXPORT Device : public Component, public Object::EventListener
58  {
59  public:
60  static agx::Model *ClassModel();
61 
62  static Device *load(TiXmlElement *eDevice, Device *device);
63 
64  public:
65 
67  enum ByteOrder
68  {
71  UNDEFINED
72  };
73 
75  enum Type
76  {
77  CPU,
78  #if AGX_USE_OPENCL()
80  #endif
81  #if AGX_USE_OPENGL()
83  #endif
85 
88  GROUP
89  };
90 
91  public:
96  Device(const agx::Name& name, Type type, const agx::String& typeName, ByteOrder byteOrder = UNDEFINED);
97 
101  Type getType() const;
102 
106  const agx::String& getTypeName() const;
107 
111  ByteOrder getByteOrder() const;
112 
116  void setTypeFormat(agxData::Type *type, agxData::Format *format);
117  void setTypeFormat(const agx::String& typeName, const agx::String& formatName);
118 
122  agxData::Format *getTypeFormat(agxData::Type *type);
123 
124 
125 
126  agxData::Buffer *createBuffer(agxData::Type *type);
127  virtual agxData::Buffer *createBuffer(agxData::Format *format) = 0;
128 
129  agxData::Buffer *getBuffer(agxData::Buffer *buffer);
130 
131 
141  virtual agx::SyncTag *sendData(const void *hostBuffer, agxData::Buffer *Buffer, size_t offset, size_t size, bool blocking) = 0;
142 
152  virtual agx::SyncTag *receiveData(void *hostBuffer, agxData::Buffer *Buffer, size_t offset, size_t size, bool blocking) = 0;
153 
154  protected:
155  virtual ~Device();
156  // friend class HostDevice;
157  void setByteOrder(ByteOrder byteOrder);
158 
159  virtual void destroyCallback(agx::Object* object) override;
160 
161 
162  private:
163  Type m_type;
164  String m_typeName;
165  ByteOrder m_byteOrder;
167  TypeFormatTable m_typeFormatTable;
168 
170  BufferTable m_bufferTable;
171  };
172 
173 
174  //---------------------------------------------------------------
175 
180  {
181  public:
182  class SyncTag : public agx::SyncTag
183  {
184  public:
185  virtual Status wait() override { return SYNC_TAG_COMPLETED; }
186  virtual Status getStatus() override { return SYNC_TAG_COMPLETED; }
187  };
188 
189  public:
190  static CpuDevice *instance();
191 
192  using Device::createBuffer;
193  virtual agxData::Buffer *createBuffer(agxData::Format *format) override;
194 
195  virtual agx::SyncTag* sendData(
196  const void *hostBuffer, agxData::Buffer* buffer, size_t offset, size_t size, bool blocking) override;
197 
198  virtual agx::SyncTag* receiveData(
199  void *hostBuffer, agxData::Buffer* buffer, size_t offset, size_t size, bool blocking) override;
200 
201  public:
202  void enqueue(Task *task);
203  // void setNumThreads(int numThreads);
204  // int getNumThreads() const;
205  void wait(bool passive = false);
206 
207  protected:
208  virtual ~CpuDevice();
209 
210  private:
211  CpuDevice(const Name& name = "CpuDevice");
212 
213  private:
214  SyncTagRef m_dummyTag;
215 
216  // ThreadPtrVector m_threads;
217  // ThreadPtrVector m_deactivatedThreads;
218  MemoryPool<RangeJob> m_jobAllocator;
219  };
220 
221  //---------------------------------------------------------------
222 
227  {
228  public:
229  DeviceGroup(const Name& name, Device *primaryDevice);
230 
231  void setPrimaryDevice(Device *device);
232  Device *getPrimaryDevice();
233 
234  Device *getOrCreateDevice(Type deviceType);
235 
236  // const DeviceRefVector& getDevices() const;
237  // Device *getDevice(const String& name);
238 
239  // void addDevice(const Device *device);
240  // void removeDevice(const Device *device);
241  // void removeDevice(const String& name);
242 
243  virtual agxData::Buffer *createBuffer(agxData::Format* /*format*/) override { agxAbort1("DeviceGroup"); return nullptr; }
244 
245  virtual agx::SyncTag *sendData(const void* /*hostBuffer*/, agxData::Buffer* /*Buffer*/, size_t /*offset*/, size_t /*size*/, bool /*blocking*/) override { agxAbort1("DeviceGroup"); return nullptr; }
246  virtual agx::SyncTag *receiveData(void* /*hostBuffer*/, agxData::Buffer* /*Buffer*/, size_t /*offset*/, size_t /*size*/, bool /*blocking*/) override { agxAbort1("DeviceGroup"); return nullptr; }
247 
248 
249  protected:
250  virtual ~DeviceGroup();
251 
252  private:
253  DeviceRef m_primaryDevice;
254  // DeviceRefVector m_devices;
256  DeviceTable m_deviceTable;
257  };
258 
259 
260  //---------------------------------------------------------------
261 
262  /* Implementation */
263  AGX_FORCE_INLINE Device::Type Device::getType() const { return m_type; }
264  AGX_FORCE_INLINE const String& Device::getTypeName() const { return m_typeName; }
266 
267  // AGX_FORCE_INLINE Device *DeviceGroup::getPrimaryDevice() { return m_primaryDevice; }
268 }
269 
270 #ifdef _MSC_VER
271 # pragma warning(pop)
272 #endif
273 
AGXCORE_EXPORT Type * getType(const agx::String &name)
A component is an object containing other objects, enabling hierarchical structuring.
Definition: Component.h:38
A model is an abstract representation of the class of an agx::Object.
Definition: Model.h:40
virtual Status getStatus() override
Definition: Device.h:186
ByteOrder
Device dependent byte order, AGX prefix to avoid clash with macro definitions.
Definition: Device.h:67
const agx::String & getTypeName() const
Definition: Device.h:264
A format is an implementation of a agxData::Type.
Definition: Format.h:63
ByteOrder getByteOrder() const
Definition: Device.h:265
#define AGX_DECLARE_POINTER_TYPES(type)
Definition: Referenced.h:254
A representation of a generic task.
Definition: Task.h:57
Representation of a name string.
Definition: Name.h:32
#define AGX_FORCE_INLINE
Definition: Object.h:59
virtual Status wait() override
Definition: Device.h:185
Memory pool.
Definition: MemoryPool.h:26
A device group contain other devices, enables hierarchical device structuring.
Definition: Device.h:226
The agx namespace contains the dynamics/math part of the AGX Dynamics API.
Abstracted type.
Definition: Type.h:97
Abstract representation of a data buffer.
Definition: Buffer.h:55
Type getType() const
Definition: Device.h:263
virtual agx::SyncTag * sendData(const void *, agxData::Buffer *, size_t, size_t, bool) override
Send data to a buffer on the device.
Definition: Device.h:245
agxData::Buffer * createBuffer(agxData::Type *type)
agx::HashTable< Attribute *, BufferRef > BufferTable
Definition: EntityStorage.h:59
virtual agx::SyncTag * receiveData(void *, agxData::Buffer *, size_t, size_t, bool) override
Receive data from a buffer on the device.
Definition: Device.h:246
virtual agxData::Buffer * createBuffer(agxData::Format *) override
Definition: Device.h:243
Type
Explicit list of device types.
Definition: Device.h:75
Inheritance with partial specialization due to bug with ref_ptr containers.
An agx::Device is an abstract representation of a device on which data can be stored and processed...
Definition: Device.h:57
#define agxAbort1(msg)
Definition: debug.h:165
#define AGXCORE_EXPORT
Representation of the CPU.
Definition: Device.h:179
agx::Object is a refcounted object with a name.
Definition: Object.h:58