tasksys.cpp 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. // ======================================================================== //
  2. // Copyright 2009-2017 Intel Corporation //
  3. // //
  4. // Licensed under the Apache License, Version 2.0 (the "License"); //
  5. // you may not use this file except in compliance with the License. //
  6. // You may obtain a copy of the License at //
  7. // //
  8. // http://www.apache.org/licenses/LICENSE-2.0 //
  9. // //
  10. // Unless required by applicable law or agreed to in writing, software //
  11. // distributed under the License is distributed on an "AS IS" BASIS, //
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. //
  13. // See the License for the specific language governing permissions and //
  14. // limitations under the License. //
  15. // ======================================================================== //
  16. #include "../../common/algorithms/parallel_for.h"
  17. namespace embree
  18. {
  19. /* Signature of ispc-generated 'task' functions */
  20. typedef void (*ISPCTaskFunc)(void* data, int threadIndex, int threadCount, int taskIndex, int taskCount);
  21. extern "C" __dllexport void* ISPCAlloc(void** taskPtr, int64_t size, int32_t alignment)
  22. {
  23. if (*taskPtr == nullptr) *taskPtr = new std::vector<void*>;
  24. std::vector<void*>* lst = (std::vector<void*>*)(*taskPtr);
  25. void* ptr = alignedMalloc((size_t)size,alignment);
  26. lst->push_back(ptr);
  27. return ptr;
  28. }
  29. extern "C" __dllexport void ISPCSync(void* task)
  30. {
  31. std::vector<void*>* lst = (std::vector<void*>*)task;
  32. for (size_t i=0; i<lst->size(); i++) alignedFree((*lst)[i]);
  33. delete lst;
  34. }
  35. extern "C" __dllexport void ISPCLaunch(void** taskPtr, void* func, void* data, int count)
  36. {
  37. parallel_for(0, count,[&] (const range<int>& r) {
  38. const int threadIndex = (int) TaskScheduler::threadIndex();
  39. const int threadCount = (int) TaskScheduler::threadCount();
  40. for (int i=r.begin(); i<r.end(); i++)
  41. ((ISPCTaskFunc)func)(data,threadIndex,threadCount,i,count);
  42. });
  43. }
  44. }