Files
rocm-systems/rocclr/runtime/platform/context.hpp
T
foreman d09ca72f74 P4 to Git Change 1536925 by vsytchen@vsytchen-ocl-win10 on 2018/04/04 17:20:38
SWDEV-79445 - OCL generic changes and code clean-up

	1. This change replaces the use of std::map with std::unordered_map to improve lookup/insert time.
	2. Replace the use of std::make_pair and std::pair constructor with uniform initialization for cleaner code.
	3. Replace the use of std::Container::iterator type with the auto keyword for cleaner code.
	4. Use range based for loops where needed.

	ReviewBoardURL = http://ocltc.amd.com/reviews/r/14517/diff/

Affected files ...

... //depot/stg/opencl/drivers/opencl/api/hip/hip_platform.cpp#4 edit
... //depot/stg/opencl/drivers/opencl/api/opencl/amdocl/cl_context.cpp#58 edit
... //depot/stg/opencl/drivers/opencl/api/opencl/amdocl/cl_d3d10.cpp#16 edit
... //depot/stg/opencl/drivers/opencl/api/opencl/amdocl/cl_d3d10_amd.hpp#9 edit
... //depot/stg/opencl/drivers/opencl/api/opencl/amdocl/cl_d3d11.cpp#24 edit
... //depot/stg/opencl/drivers/opencl/api/opencl/amdocl/cl_d3d11_amd.hpp#13 edit
... //depot/stg/opencl/drivers/opencl/api/opencl/amdocl/cl_d3d9.cpp#34 edit
... //depot/stg/opencl/drivers/opencl/api/opencl/amdocl/cl_d3d9_amd.hpp#17 edit
... //depot/stg/opencl/drivers/opencl/api/opencl/amdocl/cl_gl.cpp#57 edit
... //depot/stg/opencl/drivers/opencl/api/opencl/amdocl/cl_pipe.cpp#7 edit
... //depot/stg/opencl/drivers/opencl/api/opencl/amdocl/cl_program.cpp#46 edit
... //depot/stg/opencl/drivers/opencl/api/opencl/amdocl/cl_svm.cpp#23 edit
... //depot/stg/opencl/drivers/opencl/runtime/device/appprofile.hpp#14 edit
... //depot/stg/opencl/drivers/opencl/runtime/device/cpu/cpuprogram.cpp#72 edit
... //depot/stg/opencl/drivers/opencl/runtime/device/cpu/cpuvirtual.cpp#27 edit
... //depot/stg/opencl/drivers/opencl/runtime/device/device.cpp#216 edit
... //depot/stg/opencl/drivers/opencl/runtime/device/device.hpp#297 edit
... //depot/stg/opencl/drivers/opencl/runtime/device/gpu/gpuappprofile.cpp#13 edit
... //depot/stg/opencl/drivers/opencl/runtime/device/gpu/gpubinary.cpp#59 edit
... //depot/stg/opencl/drivers/opencl/runtime/device/gpu/gpucompiler.cpp#158 edit
... //depot/stg/opencl/drivers/opencl/runtime/device/gpu/gpudevice.cpp#587 edit
... //depot/stg/opencl/drivers/opencl/runtime/device/gpu/gpukernel.cpp#322 edit
... //depot/stg/opencl/drivers/opencl/runtime/device/gpu/gpuprintf.cpp#46 edit
... //depot/stg/opencl/drivers/opencl/runtime/device/gpu/gpuprogram.cpp#237 edit
... //depot/stg/opencl/drivers/opencl/runtime/device/gpu/gpuprogram.hpp#70 edit
... //depot/stg/opencl/drivers/opencl/runtime/device/gpu/gpuresource.cpp#242 edit
... //depot/stg/opencl/drivers/opencl/runtime/device/gpu/gpuvirtual.cpp#415 edit
... //depot/stg/opencl/drivers/opencl/runtime/device/gpu/gpuvirtual.hpp#143 edit
... //depot/stg/opencl/drivers/opencl/runtime/device/pal/palappprofile.cpp#3 edit
... //depot/stg/opencl/drivers/opencl/runtime/device/pal/palcompiler.cpp#22 edit
... //depot/stg/opencl/drivers/opencl/runtime/device/pal/paldevice.cpp#79 edit
... //depot/stg/opencl/drivers/opencl/runtime/device/pal/palprintf.cpp#9 edit
... //depot/stg/opencl/drivers/opencl/runtime/device/pal/palprogram.cpp#59 edit
... //depot/stg/opencl/drivers/opencl/runtime/device/pal/palresource.cpp#60 edit
... //depot/stg/opencl/drivers/opencl/runtime/device/pal/palvirtual.cpp#84 edit
... //depot/stg/opencl/drivers/opencl/runtime/device/pal/palvirtual.hpp#46 edit
... //depot/stg/opencl/drivers/opencl/runtime/device/rocm/CMakeLists.txt#11 edit
... //depot/stg/opencl/drivers/opencl/runtime/device/rocm/pro/prodevice.cpp#4 edit
... //depot/stg/opencl/drivers/opencl/runtime/device/rocm/pro/prodevice.hpp#5 edit
... //depot/stg/opencl/drivers/opencl/runtime/device/rocm/rocbinary.hpp#6 edit
... //depot/stg/opencl/drivers/opencl/runtime/device/rocm/roccompiler.cpp#42 edit
... //depot/stg/opencl/drivers/opencl/runtime/device/rocm/roccounters.cpp#3 edit
... //depot/stg/opencl/drivers/opencl/runtime/device/rocm/rocprintf.cpp#10 edit
... //depot/stg/opencl/drivers/opencl/runtime/device/rocm/rocprogram.cpp#81 edit
... //depot/stg/opencl/drivers/opencl/runtime/platform/command.cpp#81 edit
... //depot/stg/opencl/drivers/opencl/runtime/platform/command.hpp#89 edit
... //depot/stg/opencl/drivers/opencl/runtime/platform/commandqueue.cpp#24 edit
... //depot/stg/opencl/drivers/opencl/runtime/platform/context.cpp#49 edit
... //depot/stg/opencl/drivers/opencl/runtime/platform/context.hpp#29 edit
... //depot/stg/opencl/drivers/opencl/runtime/platform/memory.cpp#129 edit
... //depot/stg/opencl/drivers/opencl/runtime/platform/memory.hpp#102 edit
... //depot/stg/opencl/drivers/opencl/runtime/platform/perfctr.hpp#7 edit
... //depot/stg/opencl/drivers/opencl/runtime/platform/program.cpp#91 edit
... //depot/stg/opencl/drivers/opencl/runtime/platform/program.hpp#43 edit
... //depot/stg/opencl/drivers/opencl/runtime/platform/sampler.hpp#9 edit
... //depot/stg/opencl/drivers/opencl/runtime/utils/flags.cpp#17 edit
2018-04-04 18:00:17 -04:00

211 строки
6.9 KiB
C++

//
// Copyright (c) 2008 Advanced Micro Devices, Inc. All rights reserved.
//
#ifndef CONTEXT_HPP_
#define CONTEXT_HPP_
#include "top.hpp"
#include "device/device.hpp"
#include "platform/object.hpp"
#include "platform/agent.hpp"
#include <vector>
#include <unordered_map>
namespace amd {
/*! \addtogroup Runtime
* @{
*
* \addtogroup Contexts
* @{
*/
class GLFunctions;
class DeviceQueue;
class Context : public RuntimeObject {
std::vector<Device*> devices_;
public:
enum DeviceFlagIdx {
GLDeviceKhrIdx = 0, //!< GL
D3D10DeviceKhrIdx, //!< D3D10
OfflineDevicesIdx, //!< Offline devices
CommandInterceptIdx, //!< Command intercept
D3D11DeviceKhrIdx, //!< D3D11
InteropUserSyncIdx, //!< Interop user sync enabled
D3D9DeviceKhrIdx, //!< d3d9 device
D3D9DeviceEXKhrIdx, //!< d3d9EX device
D3D9DeviceVAKhrIdx, //!< d3d9VA device
EGLDeviceKhrIdx, //!< EGL device
LastDeviceFlagIdx
};
enum Flags {
GLDeviceKhr = 1 << GLDeviceKhrIdx, //!< GL
D3D10DeviceKhr = 1 << D3D10DeviceKhrIdx, //!< D3D10
OfflineDevices = 1 << OfflineDevicesIdx, //!< Offline devices
CommandIntercept = 1 << CommandInterceptIdx, //!< Command intercept
D3D11DeviceKhr = 1 << D3D11DeviceKhrIdx, //!< D3D11
InteropUserSync = 1 << InteropUserSyncIdx, //!< Interop user sync enabled
D3D9DeviceKhr = 1 << D3D9DeviceKhrIdx, //!< d3d9 device
D3D9DeviceEXKhr = 1 << D3D9DeviceEXKhrIdx, //!< d3d9EX device
D3D9DeviceVAKhr = 1 << D3D9DeviceVAKhrIdx, //!< d3d9VA device
EGLDeviceKhr = 1 << EGLDeviceKhrIdx, //!< EGL device
};
//! Context info structure
struct Info {
uint flags_; //!< Context info flags
void* hDev_[LastDeviceFlagIdx]; //!< Device object reference
void* hCtx_; //!< Context object reference
size_t propertiesSize_; //!< Size of the original properties in bytes
cl_int(CL_CALLBACK* commandIntercept_)(cl_event, cl_int*);
};
struct DeviceQueueInfo {
DeviceQueue* defDeviceQueue_; //!< Default device queue
uint deviceQueueCnt_; //!< The number of device queues
DeviceQueueInfo() : defDeviceQueue_(NULL), deviceQueueCnt_(0) {}
};
private:
// Copying a Context is not allowed
Context(const Context&);
Context& operator=(const Context&);
protected:
bool terminate() {
if (Agent::shouldPostContextEvents()) {
Agent::postContextFree(as_cl(this));
}
return true;
}
//! Context destructor
~Context();
public:
/*! \brief Helper function to check the context properties and initialize
* context info structure
*
* \return An errcode if invalid, CL_SUCCESS if valid
*/
static int checkProperties(const cl_context_properties* properties, //!< Properties
Info* info //!< Info structure
);
//! Default constructor
Context(const std::vector<Device*>& devices, //!< List of all devices
const Info& info //!< Context info structure
);
//! Compare two Context instances.
bool operator==(const Context& rhs) const { return this == &rhs; }
bool operator!=(const Context& rhs) const { return !(*this == rhs); }
/*! Creates the context
*
* \return An errcode if runtime fails the context creation,
* CL_SUCCESS otherwise
*/
int create(const intptr_t* properties //!< Original context properties
);
/**
* Allocate host memory using either a custom device allocator or a generic
* OS allocator
*
* @param size Allocation size, in bytes
* @param alignment Desired alignment, in bytes
* @param atomics The buffer should support platform (SVM) atomics
*/
void* hostAlloc(size_t size, size_t alignment, bool atomics = false) const;
/**
* Release host memory
* @param ptr Pointer allocated using ::hostAlloc. If the pointer has been
* allocated elsewhere, the behavior is undefined
*/
void hostFree(void* ptr) const;
/**
* Allocate SVM buffer
*
* @param size Allocation size, in bytes
* @param alignment Desired alignment, in bytes
* @param flags The flags to create a svm space
*/
void* svmAlloc(size_t size, size_t alignment, cl_svm_mem_flags flags = CL_MEM_READ_WRITE);
/**
* Release SVM buffer
* @param ptr Pointer allocated using ::svmAlloc. If the pointer has been
* allocated elsewhere, the behavior is undefined
*/
void svmFree(void* ptr) const;
//! Return the devices associated with this context.
const std::vector<Device*>& devices() const { return devices_; }
//! Return the SVM capable devices associated with this context.
const std::vector<Device*>& svmDevices() const { return svmAllocDevice_; }
//! Returns true if the given device is associated with this context.
bool containsDevice(const Device* device) const;
//! Returns the context info structure
const Info& info() const { return info_; }
//! Returns a pointer to the original properties
const cl_context_properties* properties() const { return properties_; }
//! Returns a pointer to the OpenGL context
GLFunctions* glenv() const { return glenv_; }
//! RTTI internal implementation
virtual ObjectType objectType() const { return ObjectTypeContext; }
//! Returns context lock for the serialized access to the context
Monitor& lock() { return ctxLock_; }
//! Returns TRUE if runtime succesfully added a device queue
DeviceQueue* defDeviceQueue(const Device& dev) const;
//! Returns TRUE if runtime succesfully added a device queue
bool isDevQueuePossible(const Device& dev);
//! Returns TRUE if runtime succesfully added a device queue
void addDeviceQueue(const Device& dev, //!< Device object
DeviceQueue* queue, //!< Device queue
bool defDevQueue //!< Added device queue will be the default queue
);
//! Removes a device queue from the list of queues
void removeDeviceQueue(const Device& dev, //!< Device object
DeviceQueue* queue //!< Device queue
);
//! Set the default device queue
void setDefDeviceQueue(const Device& dev, DeviceQueue* queue)
{ deviceQueues_[&dev].defDeviceQueue_ = queue; };
private:
const Info info_; //!< Context info structure
cl_context_properties* properties_; //!< Original properties
GLFunctions* glenv_; //!< OpenGL context
Device* customHostAllocDevice_; //!< Device responsible for host allocations
std::vector<Device*> svmAllocDevice_; //!< Devices can support SVM allocations
std::unordered_map<const Device*, DeviceQueueInfo> deviceQueues_; //!< Device queues mapping
mutable Monitor ctxLock_; //!< Lock for the context access
};
/*! @}
* @}
*/
} // namespace amd
#endif /*CONTEXT_HPP_*/