File: MultiDevice.cpp

package info (click to toggle)
ospray 3.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 10,040 kB
  • sloc: cpp: 80,569; ansic: 951; sh: 805; makefile: 171; python: 69
file content (563 lines) | stat: -rw-r--r-- 17,476 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
// Copyright 2009 Intel Corporation
// SPDX-License-Identifier: Apache-2.0

// ospray
#include "MultiDevice.h"
#include "MultiDeviceFrameBuffer.h"
#include "MultiDeviceRenderTask.h"
#include "camera/registration.h"
#include "common/DeviceRTImpl.h"
#include "fb/LocalFB.h"
#include "fb/SparseFB.h"
#include "fb/registration.h"
#include "geometry/registration.h"
#include "lights/registration.h"
#include "render/LoadBalancer.h"
#include "render/registration.h"
#include "texture/registration.h"
#include "volume/transferFunction/registration.h"

#include "rkcommon/tasking/parallel_for.h"
#include "rkcommon/tasking/tasking_system_init.h"
#include "rkcommon/utility/CodeTimer.h"
#include "rkcommon/utility/getEnvVar.h"
#include "rkcommon/utility/multidim_index_sequence.h"

namespace ospray {
namespace api {

/////////////////////////////////////////////////////////////////////////
// ManagedObject Implementation /////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////
void MultiDevice::commit()
{
  Device::commit();

  hostDevice.commit();

  if (subdevices.empty()) {
    auto OSPRAY_NUM_SUBDEVICES =
        utility::getEnvVar<int>("OSPRAY_NUM_SUBDEVICES");
    int numSubdevices =
        OSPRAY_NUM_SUBDEVICES.value_or(getParam("numSubdevices", 1));
    postStatusMsg(OSP_LOG_DEBUG) << "# of subdevices =" << numSubdevices;

    std::vector<std::shared_ptr<TiledLoadBalancer>> subdeviceLoadBalancers;
    for (int i = 0; i < numSubdevices; ++i) {
      std::unique_ptr<devicert::Device> drtDevice =
          devicert::make_device_unique(i, debugMode);
      auto d = rkcommon::make_unique<ISPCDevice>(std::move(drtDevice));
      d->commit();
      subdevices.emplace_back(std::move(d));
      subdeviceLoadBalancers.push_back(subdevices.back()->loadBalancer);
    }
    loadBalancer =
        rkcommon::make_unique<MultiDeviceLoadBalancer>(subdeviceLoadBalancers);
  }
}

/////////////////////////////////////////////////////////////////////////
// Device Implementation ////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////

int MultiDevice::loadModule(const char *name)
{
  OSPError err = OSP_NO_ERROR;
  for (auto &d : subdevices) {
    auto e = d->loadModule(name);
    if (e != OSP_NO_ERROR) {
      err = (OSPError)e;
    }
  }
  return err;
}

// OSPRay Data Arrays ///////////////////////////////////////////////////

static void multiDeviceObjectDeleter(const void *object, const void *)
{
  memory::RefCount *o = (memory::RefCount *)object;
  o->refDec();
}

OSPData MultiDevice::newSharedData(const void *sharedData,
    OSPDataType type,
    const vec3ul &numItems,
    const vec3l &byteStride,
    OSPDeleterCallback freeFunction,
    const void *userData)
{
  MultiDeviceObject *o = new MultiDeviceObject;
  o->freeFunction = freeFunction;
  o->userData = userData;
  o->sharedData = sharedData;
  // Data arrays of OSPRay objects need to populate the corresponding subdevice
  // data arrays with the objects for that subdevice
  if (type & OSP_OBJECT) {
    for (size_t i = 0; i < subdevices.size(); ++i) {
      o->objects.push_back(
          (OSPObject) new Data(*subdevices[i], type, numItems));
    }

    // A little lazy here, but using the Data object to just give me a view
    // + the index sequence iterator to use to step over the stride
    Data *multiData =
        new Data(hostDevice, sharedData, type, numItems, byteStride);
    o->sharedDataDirtyReference = multiData;

    index_sequence_3D seq(numItems);
    for (auto idx : seq) {
      MultiDeviceObject *mobj = *(MultiDeviceObject **)multiData->data(idx);
      retain((OSPObject)mobj);

      // Copy the subdevice object handles to the data arrays for each subdevice
      for (size_t i = 0; i < subdevices.size(); ++i) {
        Data *subdeviceData = (Data *)o->objects[i];
        std::memcpy(
            subdeviceData->data(idx), &mobj->objects[i], sizeof(OSPObject));
      }
    }
  } else {
    for (auto &d : subdevices) {
      o->refInc();
      o->objects.push_back(d->newSharedData(
          sharedData, type, numItems, byteStride, multiDeviceObjectDeleter, o));
    }
  }
  return (OSPData)o;
}

OSPData MultiDevice::newData(OSPDataType type, const vec3ul &numItems)
{
  MultiDeviceObject *o = new MultiDeviceObject;
  for (auto &d : subdevices) {
    o->objects.push_back(d->newData(type, numItems));
  }
  return (OSPData)o;
}

void MultiDevice::copyData(
    const OSPData source, OSPData destination, const vec3ul &destinationIndex)
{
  const MultiDeviceObject *srcs = (const MultiDeviceObject *)source;
  MultiDeviceObject *dsts = (MultiDeviceObject *)destination;
  for (size_t i = 0; i < subdevices.size(); ++i) {
    subdevices[i]->copyData(
        (OSPData)srcs->objects[i], (OSPData)dsts->objects[i], destinationIndex);
  }
}

// Renderable Objects ///////////////////////////////////////////////////

OSPLight MultiDevice::newLight(const char *type)
{
  MultiDeviceObject *o = new MultiDeviceObject;
  for (auto &d : subdevices) {
    o->objects.push_back(d->newLight(type));
  }
  return (OSPLight)o;
}

OSPCamera MultiDevice::newCamera(const char *type)
{
  MultiDeviceObject *o = new MultiDeviceObject;
  for (auto &d : subdevices) {
    o->objects.push_back(d->newCamera(type));
  }
  return (OSPCamera)o;
}

OSPGeometry MultiDevice::newGeometry(const char *type)
{
  MultiDeviceObject *o = new MultiDeviceObject;
  for (auto &d : subdevices) {
    o->objects.push_back(d->newGeometry(type));
  }
  return (OSPGeometry)o;
}

OSPVolume MultiDevice::newVolume(const char *type)
{
  MultiDeviceObject *o = new MultiDeviceObject;
  for (auto &d : subdevices) {
    o->objects.push_back(d->newVolume(type));
  }
  return (OSPVolume)o;
}

OSPGeometricModel MultiDevice::newGeometricModel(OSPGeometry geom)
{
  MultiDeviceObject *g = (MultiDeviceObject *)geom;
  MultiDeviceObject *o = new MultiDeviceObject;
  for (size_t i = 0; i < subdevices.size(); ++i) {
    OSPGeometry subdeviceObj =
        g ? (OSPGeometry)g->objects[i] : (OSPGeometry) nullptr;
    o->objects.push_back(subdevices[i]->newGeometricModel(subdeviceObj));
  }
  return (OSPGeometricModel)o;
}
OSPVolumetricModel MultiDevice::newVolumetricModel(OSPVolume volume)
{
  MultiDeviceObject *v = (MultiDeviceObject *)volume;
  MultiDeviceObject *o = new MultiDeviceObject;
  for (size_t i = 0; i < subdevices.size(); ++i) {
    OSPVolume subdeviceObj = v ? (OSPVolume)v->objects[i] : (OSPVolume) nullptr;
    o->objects.push_back(subdevices[i]->newVolumetricModel(subdeviceObj));
  }
  return (OSPVolumetricModel)o;
}

// Model Meta-Data //////////////////////////////////////////////////////

OSPMaterial MultiDevice::newMaterial(const char *material_type)
{
  MultiDeviceObject *o = new MultiDeviceObject;
  for (auto &d : subdevices) {
    o->objects.push_back(d->newMaterial(material_type));
  }
  return (OSPMaterial)o;
}

OSPTransferFunction MultiDevice::newTransferFunction(const char *type)
{
  MultiDeviceObject *o = new MultiDeviceObject;
  for (auto &d : subdevices) {
    o->objects.push_back(d->newTransferFunction(type));
  }
  return (OSPTransferFunction)o;
}

OSPTexture MultiDevice::newTexture(const char *type)
{
  MultiDeviceObject *o = new MultiDeviceObject;
  for (auto &d : subdevices) {
    o->objects.push_back(d->newTexture(type));
  }
  return (OSPTexture)o;
}

// Instancing ///////////////////////////////////////////////////////////

OSPGroup MultiDevice::newGroup()
{
  MultiDeviceObject *o = new MultiDeviceObject;
  for (auto &d : subdevices) {
    o->objects.push_back(d->newGroup());
  }
  return (OSPGroup)o;
}

OSPInstance MultiDevice::newInstance(OSPGroup group)
{
  MultiDeviceObject *g = (MultiDeviceObject *)group;
  MultiDeviceObject *o = new MultiDeviceObject;
  for (size_t i = 0; i < subdevices.size(); ++i) {
    OSPGroup subdeviceObj = g ? (OSPGroup)g->objects[i] : (OSPGroup) nullptr;
    o->objects.push_back(subdevices[i]->newInstance(subdeviceObj));
  }
  return (OSPInstance)o;
}

// Top-level Worlds /////////////////////////////////////////////////////

OSPWorld MultiDevice::newWorld()
{
  MultiDeviceObject *o = new MultiDeviceObject;
  for (auto &d : subdevices) {
    o->objects.push_back(d->newWorld());
  }
  return (OSPWorld)o;
}

box3f MultiDevice::getBounds(OSPObject obj)
{
  MultiDeviceObject *o = (MultiDeviceObject *)obj;
  // Everything is replicated across the subdevices, so we
  // can just query the bounds from one of them
  return subdevices[0]->getBounds(o->objects[0]);
}

// Object + Parameter Lifetime Management ///////////////////////////////

void MultiDevice::setObjectParam(
    OSPObject object, const char *name, OSPDataType type, const void *mem)
{
  // If the type is an OSPObject, we need to find the per-subdevice objects
  // and set them appropriately
  MultiDeviceObject *o = (MultiDeviceObject *)object;
  if (type & OSP_OBJECT) {
    MultiDeviceObject *p = *(MultiDeviceObject **)mem;
    for (size_t i = 0; i < subdevices.size(); ++i) {
      subdevices[i]->setObjectParam(o->objects[i], name, type, &p->objects[i]);
    }
  } else {
    for (size_t i = 0; i < subdevices.size(); ++i) {
      subdevices[i]->setObjectParam(o->objects[i], name, type, mem);
    }
  }

  // Handle parameters for rowmajor framebuffer
  MultiDeviceFrameBuffer *fb = dynamic_cast<MultiDeviceFrameBuffer *>(o);
  if (fb) {
    if (type & OSP_OBJECT) {
      MultiDeviceObject *p = *(MultiDeviceObject **)mem;
      hostDevice.setObjectParam(
          (OSPObject)fb->rowmajorFb.ptr, name, type, &p->objects[0]);
    } else {
      hostDevice.setObjectParam((OSPObject)fb->rowmajorFb.ptr, name, type, mem);
    }
  }
}

void MultiDevice::removeObjectParam(OSPObject object, const char *name)
{
  MultiDeviceObject *o = (MultiDeviceObject *)object;
  for (size_t i = 0; i < subdevices.size(); ++i) {
    subdevices[i]->removeObjectParam(o->objects[i], name);
  }

  // Handle parameters for rowmajor framebuffer
  MultiDeviceFrameBuffer *fb = dynamic_cast<MultiDeviceFrameBuffer *>(o);
  if (fb)
    hostDevice.removeObjectParam((OSPObject)fb->rowmajorFb.ptr, name);
}

void MultiDevice::commit(OSPObject object)
{
  MultiDeviceObject *o = (MultiDeviceObject *)object;

  // Applications can change their shared buffer contents directly,
  // so shared arrays of objects have to do more to ensure that the
  // contents are up to date. Specifically the handles have to be
  // translated down to the subdevice specific handles correctly.
  if (o->sharedDataDirtyReference) {
    Data *multiData = o->sharedDataDirtyReference;
    const vec3ul &numItems = multiData->numItems;
    index_sequence_3D seq(numItems);
    for (auto idx : seq) {
      MultiDeviceObject *mobj = *(MultiDeviceObject **)multiData->data(idx);
      retain((OSPObject)mobj);

      // Copy the subdevice object handles to the data arrays for each subdevice
      for (size_t i = 0; i < subdevices.size(); ++i) {
        Data *subdeviceData = (Data *)o->objects[i];
        std::memcpy(
            subdeviceData->data(idx), &mobj->objects[i], sizeof(OSPObject));
      }
    }
  }

  for (size_t i = 0; i < subdevices.size(); ++i) {
    subdevices[i]->commit(o->objects[i]);
  }

  // Handle commits for rowmajor framebuffer
  MultiDeviceFrameBuffer *fb = dynamic_cast<MultiDeviceFrameBuffer *>(o);
  if (fb)
    hostDevice.commit((OSPObject)fb->rowmajorFb.ptr);
}

void MultiDevice::release(OSPObject object)
{
  memory::RefCount *o = (memory::RefCount *)object;
  MultiDeviceObject *mo = dynamic_cast<MultiDeviceObject *>(o);
  if (mo) {
    if (mo->sharedDataDirtyReference) {
      Data *multiData = mo->sharedDataDirtyReference;
      const vec3ul &numItems = multiData->numItems;
      index_sequence_3D seq(numItems);
      for (auto idx : seq) {
        MultiDeviceObject *mobj = *(MultiDeviceObject **)multiData->data(idx);
        mobj->refDec();
      }
    }
    for (size_t i = 0; i < mo->objects.size(); ++i) {
      subdevices[i]->release(mo->objects[i]);
    }
  }
  o->refDec();
}

void MultiDevice::retain(OSPObject object)
{
  memory::RefCount *o = (memory::RefCount *)object;
  MultiDeviceObject *mo = dynamic_cast<MultiDeviceObject *>(o);
  if (mo) {
    for (size_t i = 0; i < mo->objects.size(); ++i) {
      subdevices[i]->retain(mo->objects[i]);
    }
  }
  o->refInc();
}

// FrameBuffer Manipulation /////////////////////////////////////////////

OSPFrameBuffer MultiDevice::frameBufferCreate(
    const vec2i &size, const OSPFrameBufferFormat mode, const uint32 channels)
{
  MultiDeviceFrameBuffer *o = new MultiDeviceFrameBuffer();
  o->rowmajorFb = new LocalFrameBuffer(hostDevice, size, mode, channels);
  // Need one refDec here for the local scope ref (see issue about Ref<>)
  o->rowmajorFb->refDec();

  const vec2i totalTiles = divRoundUp(size, vec2i(TILE_SIZE));
  for (size_t i = 0; i < subdevices.size(); ++i) {
    // Assign tiles round-robin among the subdevices
    std::vector<uint32_t> tileIDs;
    for (uint32_t tid = 0; tid < totalTiles.long_product(); ++tid) {
      if (tid % subdevices.size() == i) {
        tileIDs.push_back(tid);
      }
    }

    FrameBuffer *fbi =
        new SparseFrameBuffer(*subdevices[i], size, mode, channels, tileIDs);
    o->objects.push_back((OSPFrameBuffer)fbi);
  }
  return (OSPFrameBuffer)o;
}

OSPImageOperation MultiDevice::newImageOp(const char *type)
{
  // In principle we don't need multiple ImageOp objects, but as long as we do
  // not use objects reference counting in MultiDeviceObject we need them for
  // proper memory handling
  MultiDeviceObject *o = new MultiDeviceObject();
  for (auto &d : subdevices) {
    o->objects.push_back(hostDevice.newImageOp(type));
  }
  return (OSPImageOperation)o;
}

const void *MultiDevice::frameBufferMap(
    OSPFrameBuffer _fb, const OSPFrameBufferChannel channel)
{
  MultiDeviceFrameBuffer *o = (MultiDeviceFrameBuffer *)_fb;
  return o->rowmajorFb->mapBuffer(channel);
}

void MultiDevice::frameBufferUnmap(const void *mapped, OSPFrameBuffer _fb)
{
  MultiDeviceFrameBuffer *o = (MultiDeviceFrameBuffer *)_fb;
  o->rowmajorFb->unmap(mapped);
}

float MultiDevice::getVariance(OSPFrameBuffer _fb)
{
  MultiDeviceFrameBuffer *o = (MultiDeviceFrameBuffer *)_fb;
  return o->rowmajorFb->getVariance();
}

void MultiDevice::resetAccumulation(OSPFrameBuffer _fb)
{
  MultiDeviceFrameBuffer *o = (MultiDeviceFrameBuffer *)_fb;
  o->rowmajorFb->clear();
  for (size_t i = 0; i < subdevices.size(); ++i) {
    SparseFrameBuffer *fbi = (SparseFrameBuffer *)o->objects[i];
    fbi->clear();
  }
}

// Frame Rendering //////////////////////////////////////////////////////

OSPRenderer MultiDevice::newRenderer(const char *type)
{
  MultiDeviceObject *o = new MultiDeviceObject;
  for (auto &d : subdevices) {
    o->objects.push_back(d->newRenderer(type));
  }
  return (OSPRenderer)o;
}

OSPFuture MultiDevice::renderFrame(OSPFrameBuffer _framebuffer,
    OSPRenderer _renderer,
    OSPCamera _camera,
    OSPWorld _world)
{
  MultiDeviceFrameBuffer *multiFb = (MultiDeviceFrameBuffer *)_framebuffer;
  multiFb->rowmajorFb->setCompletedEvent(OSP_NONE_FINISHED);
  for (size_t i = 0; i < multiFb->objects.size(); ++i) {
    SparseFrameBuffer *fbi = (SparseFrameBuffer *)multiFb->objects[i];
    fbi->setCompletedEvent(OSP_NONE_FINISHED);
  }

  MultiDeviceObject *multiRenderer = (MultiDeviceObject *)_renderer;
  MultiDeviceObject *multiCamera = (MultiDeviceObject *)_camera;
  MultiDeviceObject *multiWorld = (MultiDeviceObject *)_world;

  retain((OSPObject)multiFb);
  retain((OSPObject)multiRenderer);
  retain((OSPObject)multiCamera);
  retain((OSPObject)multiWorld);

  auto *f = new MultiDeviceRenderTask(multiFb, [=]() {
    utility::CodeTimer timer;
    timer.start();
    loadBalancer->renderFrame(multiFb, multiRenderer, multiCamera, multiWorld);
    timer.stop();

    release((OSPObject)multiFb);
    release((OSPObject)multiRenderer);
    release((OSPObject)multiCamera);
    release((OSPObject)multiWorld);

    return timer.seconds();
  });

  return (OSPFuture)f;
}

int MultiDevice::isReady(OSPFuture _task, OSPSyncEvent event)
{
  auto *task = (Future *)_task;
  return task->isFinished(event);
}

void MultiDevice::wait(OSPFuture _task, OSPSyncEvent event)
{
  auto *task = (Future *)_task;
  task->wait(event);
}

void MultiDevice::cancel(OSPFuture _task)
{
  auto *task = (Future *)_task;
  return task->cancel();
}

float MultiDevice::getProgress(OSPFuture _task)
{
  auto *task = (Future *)_task;
  return task->getProgress();
}

float MultiDevice::getTaskDuration(OSPFuture _task)
{
  auto *task = (Future *)_task;
  return task->getTaskDuration();
}

OSPPickResult MultiDevice::pick(OSPFrameBuffer _fb,
    OSPRenderer _renderer,
    OSPCamera _camera,
    OSPWorld _world,
    const vec2f &screenPos)
{
  MultiDeviceObject *multiFb = (MultiDeviceObject *)_fb;
  FrameBuffer *fb = (FrameBuffer *)multiFb->objects[0];

  MultiDeviceObject *multiRenderer = (MultiDeviceObject *)_renderer;
  MultiDeviceObject *multiCamera = (MultiDeviceObject *)_camera;
  MultiDeviceObject *multiWorld = (MultiDeviceObject *)_world;

  // Data in the multidevice is all replicated, so we just run the pick on the
  // first subdevice
  return subdevices[0]->pick((OSPFrameBuffer)fb,
      (OSPRenderer)multiRenderer->objects[0],
      (OSPCamera)multiCamera->objects[0],
      (OSPWorld)multiWorld->objects[0],
      screenPos);
}

} // namespace api
} // namespace ospray