Path: blob/master/thirdparty/embree/kernels/common/device.h
9905 views
// Copyright 2009-2021 Intel Corporation1// SPDX-License-Identifier: Apache-2.023#pragma once45#include "default.h"6#include "state.h"7#include "accel.h"89namespace embree10{11class BVH4Factory;12class BVH8Factory;13struct TaskArena;1415class Device : public State, public MemoryMonitorInterface16{17ALIGNED_CLASS_(16);1819public:2021/*! allocator that performs unified shared memory allocations */22template<typename T, size_t alignment>23struct allocator24{25typedef T value_type;26typedef T* pointer;27typedef const T* const_pointer;28typedef T& reference;29typedef const T& const_reference;30typedef std::size_t size_type;31typedef std::ptrdiff_t difference_type;3233allocator() {}3435allocator(Device* device)36: device(device) {}3738__forceinline pointer allocate( size_type n ) {39assert(device);40return (pointer) device->malloc(n*sizeof(T),alignment,EmbreeMemoryType::MALLOC);41}4243__forceinline void deallocate( pointer p, size_type n ) {44if (device) device->free(p);45}4647__forceinline void construct( pointer p, const_reference val ) {48new (p) T(val);49}5051__forceinline void destroy( pointer p ) {52p->~T();53}5455Device* device = nullptr;56};5758/*! vector class that performs aligned allocations from Device object */59template<typename T>60using vector = vector_t<T,allocator<T,std::alignment_of<T>::value>>;6162template<typename T, size_t alignment>63using avector = vector_t<T,allocator<T,alignment>>;6465public:6667/*! Device construction */68Device (const char* cfg);6970/*! Device destruction */71virtual ~Device ();7273/*! prints info about the device */74void print();7576/*! sets the error code */77void setDeviceErrorCode(RTCError error, std::string const& msg = "");7879/*! returns and clears the error code */80RTCError getDeviceErrorCode();8182/*! Returns the string representation for the error code. For example, for RTC_ERROR_UNKNOWN the string "RTC_ERROR_UNKNOWN" will be returned. */83static char* getDeviceErrorString();8485/*! returns the last error message */86const char* getDeviceLastErrorMessage();8788/*! sets the error code */89static void setThreadErrorCode(RTCError error, std::string const& msg = "");9091/*! returns and clears the error code */92static RTCError getThreadErrorCode();939495/*! returns the last error message */96static const char* getThreadLastErrorMessage();9798/*! processes error codes, do not call directly */99static void process_error(Device* device, RTCError error, const char* str);100101/*! invokes the memory monitor callback */102void memoryMonitor(ssize_t bytes, bool post);103104/*! sets the size of the software cache. */105void setCacheSize(size_t bytes);106107/*! sets a property */108void setProperty(const RTCDeviceProperty prop, ssize_t val);109110/*! gets a property */111ssize_t getProperty(const RTCDeviceProperty prop);112113/*! enter device by setting up some global state */114virtual void enter() {}115116/*! leave device by setting up some global state */117virtual void leave() {}118119/*! buffer allocation - using USM shared */120virtual void* malloc(size_t size, size_t align);121122/*! buffer allocation */123virtual void* malloc(size_t size, size_t align, EmbreeMemoryType type);124125/*! buffer deallocation */126virtual void free(void* ptr);127128/*! returns true if device is of type DeviceGPU */129virtual bool is_gpu() const { return false; }130131/*! returns true if device and host have shared memory system (e.g., integrated GPU) */132virtual bool has_unified_memory() const { return true; }133134virtual EmbreeMemoryType get_memory_type(void* ptr) const { return EmbreeMemoryType::MALLOC; }135136private:137138/*! initializes the tasking system */139void initTaskingSystem(size_t numThreads);140141/*! shuts down the tasking system */142void exitTaskingSystem();143144std::unique_ptr<TaskArena> arena;145146public:147148// use tasking system arena to execute func149void execute(bool join, const std::function<void()>& func);150151/*! some variables that can be set via rtcSetParameter1i for debugging purposes */152public:153static ssize_t debug_int0;154static ssize_t debug_int1;155static ssize_t debug_int2;156static ssize_t debug_int3;157158public:159std::unique_ptr<BVH4Factory> bvh4_factory;160#if defined(EMBREE_TARGET_SIMD8)161std::unique_ptr<BVH8Factory> bvh8_factory;162#endif163164private:165static const std::vector<std::string> error_strings;166167public:168static const char* getErrorString(RTCError error);169170};171172#if defined(EMBREE_SYCL_SUPPORT)173174class DeviceGPU : public Device175{176public:177178DeviceGPU(sycl::context sycl_context, const char* cfg);179~DeviceGPU();180181virtual void enter() override;182virtual void leave() override;183virtual void* malloc(size_t size, size_t align) override;184virtual void* malloc(size_t size, size_t align, EmbreeMemoryType type) override;185virtual void free(void* ptr) override;186187/* set SYCL device */188void setSYCLDevice(const sycl::device sycl_device);189190/*! returns true if device is of type DeviceGPU */191virtual bool is_gpu() const override { return true; }192193/*! returns true if device and host have shared memory system (e.g., integrated GPU) */194virtual bool has_unified_memory() const override;195196virtual EmbreeMemoryType get_memory_type(void* ptr) const override {197switch(sycl::get_pointer_type(ptr, gpu_context)) {198case sycl::usm::alloc::host: return EmbreeMemoryType::USM_HOST;199case sycl::usm::alloc::device: return EmbreeMemoryType::USM_DEVICE;200case sycl::usm::alloc::shared: return EmbreeMemoryType::USM_SHARED;201default: return EmbreeMemoryType::MALLOC;202}203}204205private:206sycl::context gpu_context;207sycl::device gpu_device;208209unsigned int gpu_maxWorkGroupSize;210unsigned int gpu_maxComputeUnits;211212public:213void* dispatchGlobalsPtr = nullptr;214215public:216inline sycl::device &getGPUDevice() { return gpu_device; }217inline sycl::context &getGPUContext() { return gpu_context; }218219inline unsigned int getGPUMaxWorkGroupSize() { return gpu_maxWorkGroupSize; }220221void init_rthw_level_zero();222void init_rthw_opencl();223};224225#endif226227struct DeviceEnterLeave228{229DeviceEnterLeave (RTCDevice hdevice);230DeviceEnterLeave (RTCScene hscene);231DeviceEnterLeave (RTCGeometry hgeometry);232DeviceEnterLeave (RTCBuffer hbuffer);233~DeviceEnterLeave();234private:235Device* device;236};237}238239240