00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #if !defined(WIN32) && !defined(__APPLE__)
00023 #include <GL/glx.h>
00024 #endif
00025
00026 #include "luxrays/core/intersectiondevice.h"
00027 #include "luxrays/core/pixeldevice.h"
00028 #include "luxrays/core/context.h"
00029 #include "luxrays/kernels/kernels.h"
00030
00031 namespace luxrays {
00032
00033
00034
00035
00036
00037 void DeviceDescription::FilterOne(std::vector<DeviceDescription *> &deviceDescriptions) {
00038 int gpuIndex = -1;
00039 int cpuIndex = -1;
00040 for (size_t i = 0; i < deviceDescriptions.size(); ++i) {
00041 if ((cpuIndex == -1) && (deviceDescriptions[i]->GetType() == DEVICE_TYPE_NATIVE_THREAD))
00042 cpuIndex = i;
00043 #if !defined(LUXRAYS_DISABLE_OPENCL)
00044 else if ((gpuIndex == -1) && (deviceDescriptions[i]->GetType() == DEVICE_TYPE_OPENCL) &&
00045 (((OpenCLDeviceDescription *)deviceDescriptions[i])->GetOpenCLType() == OCL_DEVICE_TYPE_GPU)) {
00046 gpuIndex = i;
00047 break;
00048 }
00049 #endif
00050 }
00051
00052 if (gpuIndex != -1) {
00053 std::vector<DeviceDescription *> selectedDev;
00054 selectedDev.push_back(deviceDescriptions[gpuIndex]);
00055 deviceDescriptions = selectedDev;
00056 } else if (gpuIndex != -1) {
00057 std::vector<DeviceDescription *> selectedDev;
00058 selectedDev.push_back(deviceDescriptions[cpuIndex]);
00059 deviceDescriptions = selectedDev;
00060 } else
00061 deviceDescriptions.clear();
00062 }
00063
00064 void DeviceDescription::Filter(const DeviceType type,
00065 std::vector<DeviceDescription *> &deviceDescriptions) {
00066 size_t i = 0;
00067 while (i < deviceDescriptions.size()) {
00068 if ((type != DEVICE_TYPE_ALL) && (deviceDescriptions[i]->GetType() != type)) {
00069
00070 deviceDescriptions.erase(deviceDescriptions.begin() + i);
00071 } else
00072 ++i;
00073 }
00074 }
00075
00076 std::string DeviceDescription::GetDeviceType(const DeviceType type) {
00077 switch (type) {
00078 case DEVICE_TYPE_ALL:
00079 return "ALL";
00080 case DEVICE_TYPE_NATIVE_THREAD:
00081 return "NATIVE_THREAD";
00082 case DEVICE_TYPE_OPENCL:
00083 return "OPENCL";
00084 case DEVICE_TYPE_VIRTUAL:
00085 return "VIRTUAL";
00086 default:
00087 return "UNKNOWN";
00088 }
00089 }
00090
00091
00092
00093
00094
00095 Device::Device(const Context *context, const DeviceType type, const unsigned int index) :
00096 deviceContext(context), deviceType(type) {
00097 deviceIndex = index;
00098 started = false;
00099 }
00100
00101 Device::~Device() {
00102 }
00103
00104 void Device::Start() {
00105 assert (!started);
00106 started = true;
00107 }
00108
00109 void Device::Stop() {
00110 assert (started);
00111 started = false;
00112 }
00113
00114
00115
00116
00117
00118 void NativeThreadDeviceDescription::AddDeviceDescs(std::vector<DeviceDescription *> &descriptions) {
00119 unsigned int count = boost::thread::hardware_concurrency();
00120
00121
00122 char buf[64];
00123 for (size_t i = 0; i < count; ++i) {
00124 sprintf(buf, "NativeThread-%03d", (int)i);
00125 std::string deviceName = std::string(buf);
00126
00127 descriptions.push_back(new NativeThreadDeviceDescription(deviceName, i));
00128 }
00129 }
00130
00131
00132
00133
00134
00135 #if !defined(LUXRAYS_DISABLE_OPENCL)
00136
00137 std::string OpenCLDeviceDescription::GetDeviceType(const cl_int type) {
00138 switch (type) {
00139 case CL_DEVICE_TYPE_ALL:
00140 return "TYPE_ALL";
00141 case CL_DEVICE_TYPE_DEFAULT:
00142 return "TYPE_DEFAULT";
00143 case CL_DEVICE_TYPE_CPU:
00144 return "TYPE_CPU";
00145 case CL_DEVICE_TYPE_GPU:
00146 return "TYPE_GPU";
00147 default:
00148 return "TYPE_UNKNOWN";
00149 }
00150 }
00151
00152 std::string OpenCLDeviceDescription::GetDeviceType(const OpenCLDeviceType type) {
00153 switch (type) {
00154 case OCL_DEVICE_TYPE_ALL:
00155 return "ALL";
00156 case OCL_DEVICE_TYPE_DEFAULT:
00157 return "DEFAULT";
00158 case OCL_DEVICE_TYPE_CPU:
00159 return "CPU";
00160 case OCL_DEVICE_TYPE_GPU:
00161 return "GPU";
00162 default:
00163 return "UNKNOWN";
00164 }
00165 }
00166
00167 OpenCLDeviceType OpenCLDeviceDescription::GetOCLDeviceType(const cl_int type) {
00168 switch (type) {
00169 case CL_DEVICE_TYPE_ALL:
00170 return OCL_DEVICE_TYPE_ALL;
00171 case CL_DEVICE_TYPE_DEFAULT:
00172 return OCL_DEVICE_TYPE_DEFAULT;
00173 case CL_DEVICE_TYPE_CPU:
00174 return OCL_DEVICE_TYPE_CPU;
00175 case CL_DEVICE_TYPE_GPU:
00176 return OCL_DEVICE_TYPE_GPU;
00177 default:
00178 return OCL_DEVICE_TYPE_UNKNOWN;
00179 }
00180 }
00181
00182 void OpenCLDeviceDescription::AddDeviceDescs(
00183 const cl::Platform &oclPlatform, const OpenCLDeviceType filter,
00184 std::vector<DeviceDescription *> &descriptions) {
00185
00186 VECTOR_CLASS<cl::Device> oclDevices;
00187 oclPlatform.getDevices(CL_DEVICE_TYPE_ALL, &oclDevices);
00188
00189
00190 for (size_t i = 0; i < oclDevices.size(); ++i) {
00191 OpenCLDeviceType type = GetOCLDeviceType(oclDevices[i].getInfo<CL_DEVICE_TYPE>());
00192
00193 if ((filter == OCL_DEVICE_TYPE_ALL) || (filter == type)) {
00194 OpenCLDeviceDescription *desc = new OpenCLDeviceDescription(oclDevices[i], i);
00195 descriptions.push_back(desc);
00196 }
00197 }
00198 }
00199
00200 cl::Context &OpenCLDeviceDescription::GetOCLContext() const {
00201 if (!oclContext) {
00202
00203 VECTOR_CLASS<cl::Device> devices;
00204 devices.push_back(oclDevice);
00205 cl::Platform platform = oclDevice.getInfo<CL_DEVICE_PLATFORM>();
00206
00207 if (enableOpenGLInterop) {
00208 #if defined (__APPLE__)
00209 CGLContextObj kCGLContext = CGLGetCurrentContext();
00210 CGLShareGroupObj kCGLShareGroup = CGLGetShareGroup(kCGLContext);
00211 cl_context_properties cps[] = {
00212 CL_CONTEXT_PROPERTY_USE_CGL_SHAREGROUP_APPLE, (cl_context_properties)kCGLShareGroup,
00213 0
00214 };
00215 #else
00216 #ifdef WIN32
00217 cl_context_properties cps[] = {
00218 CL_GL_CONTEXT_KHR, (cl_context_properties)wglGetCurrentContext(),
00219 CL_WGL_HDC_KHR, (cl_context_properties)wglGetCurrentDC(),
00220 CL_CONTEXT_PLATFORM, (cl_context_properties)platform(),
00221 0
00222 };
00223 #else
00224 cl_context_properties cps[] = {
00225 CL_GL_CONTEXT_KHR, (cl_context_properties)glXGetCurrentContext(),
00226 CL_GLX_DISPLAY_KHR, (cl_context_properties)glXGetCurrentDisplay(),
00227 CL_CONTEXT_PLATFORM, (cl_context_properties)platform(),
00228 0
00229 };
00230 #endif
00231 #endif
00232
00233 oclContext = new cl::Context(devices, cps);
00234 } else {
00235 cl_context_properties cps[] = {
00236 CL_CONTEXT_PLATFORM, (cl_context_properties)platform(), 0
00237 };
00238
00239 oclContext = new cl::Context(devices, cps);
00240 }
00241 }
00242
00243 return *oclContext;
00244 }
00245
00246 void OpenCLDeviceDescription::Filter(const OpenCLDeviceType type,
00247 std::vector<DeviceDescription *> &deviceDescriptions) {
00248 if (type == OCL_DEVICE_TYPE_ALL)
00249 return;
00250
00251 size_t i = 0;
00252 while (i < deviceDescriptions.size()) {
00253 if ((deviceDescriptions[i]->GetType() == DEVICE_TYPE_OPENCL) &&
00254 (((OpenCLDeviceDescription *)deviceDescriptions[i])->GetOpenCLType() != type)) {
00255
00256 deviceDescriptions.erase(deviceDescriptions.begin() + i);
00257 } else
00258 ++i;
00259 }
00260 }
00261
00262 #endif
00263
00264
00265
00266
00267
00268 IntersectionDevice::IntersectionDevice(const Context *context, const DeviceType type, const unsigned int index) :
00269 Device(context, type, index) {
00270 dataSet = NULL;
00271 }
00272
00273 IntersectionDevice::~IntersectionDevice() {
00274 }
00275
00276 void IntersectionDevice::SetDataSet(const DataSet *newDataSet) {
00277 assert (!started);
00278 assert (newDataSet != NULL);
00279 assert (newDataSet->IsPreprocessed());
00280
00281 dataSet = newDataSet;
00282 }
00283
00284 void IntersectionDevice::Start() {
00285 assert (dataSet != NULL);
00286
00287 Device::Start();
00288
00289 statsStartTime = WallClockTime();
00290 statsTotalRayCount = 0.0;
00291 statsDeviceIdleTime = 0.0;
00292 statsDeviceTotalTime = 0.0;
00293 }
00294
00295
00296
00297
00298
00299 PixelDevice::PixelDevice(const Context *context, const DeviceType type, const unsigned int index) :
00300 Device(context, type, index) {
00301 }
00302
00303 PixelDevice::~PixelDevice() {
00304 }
00305
00306 void PixelDevice::Init(const unsigned int w, const unsigned int h) {
00307 assert (!started);
00308
00309 width = w;
00310 height = h;
00311 }
00312
00313 void PixelDevice::Start() {
00314 Device::Start();
00315
00316 statsTotalSampleTime = 0.0;
00317 statsTotalSamplesCount = 0.0;
00318 }
00319
00320 }