File: ispc_tasksys.cpp

package info (click to toggle)
ospray 3.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 10,048 kB
  • sloc: cpp: 80,569; ansic: 951; sh: 805; makefile: 170; python: 69
file content (71 lines) | stat: -rw-r--r-- 1,803 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
// Copyright 2009 Intel Corporation
// SPDX-License-Identifier: Apache-2.0

#include <assert.h>
#include <vector>
#include "common/OSPCommon.h"
#include "rkcommon/memory/malloc.h"
#include "rkcommon/tasking/parallel_for.h"

namespace ospray {

/* Signature of ispc-generated 'task' functions */
using ISPCTaskFunc = void (*)(void *data,
    int threadIndex,
    int threadCount,
    int taskIndex,
    int taskCount,
    int taskIndex0,
    int taskIndex1,
    int taskIndex2,
    int taskCount0,
    int taskCount1,
    int taskCount2);

extern "C" OSPRAY_SDK_INTERFACE void *ISPCAlloc(
    void **taskPtr, int64_t size, int32_t alignment)
{
  if (*taskPtr == nullptr)
    *taskPtr = new std::vector<void *>;
  std::vector<void *> *lst = (std::vector<void *> *)(*taskPtr);
  void *ptr = memory::alignedMalloc((size_t)size, alignment);
  lst->push_back(ptr);
  return ptr;
}

extern "C" OSPRAY_SDK_INTERFACE void ISPCSync(void *task)
{
  std::vector<void *> *lst = (std::vector<void *> *)task;
  for (size_t i = 0; i < lst->size(); i++)
    memory::alignedFree((*lst)[i]);
  delete lst;
}

extern "C" OSPRAY_SDK_INTERFACE void ISPCLaunch(void ** /*taskPtr*/,
    void *func,
    void *data,
    int taskCount0,
    int taskCount1,
    int taskCount2)
{
  const size_t nTasks =
      size_t(taskCount0) * size_t(taskCount1) * size_t(taskCount2);
  tasking::parallel_for(nTasks, [&](const size_t i) {
    const int taskIndex0 = i % taskCount0;
    const int taskIndex1 = (i / taskCount0) % taskCount1;
    const int taskIndex2 = i / (size_t(taskCount0) * taskCount1);
    ((ISPCTaskFunc)func)(data,
        i,
        nTasks,
        i,
        nTasks,
        taskIndex0,
        taskIndex1,
        taskIndex2,
        taskCount0,
        taskCount1,
        taskCount2);
  });
}

} // namespace ospray