thread_parallel_runner.h 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /* Copyright (c) the JPEG XL Project Authors. All rights reserved.
  2. *
  3. * Use of this source code is governed by a BSD-style
  4. * license that can be found in the LICENSE file.
  5. */
  6. /** @addtogroup libjxl_threads
  7. * @{
  8. * @file thread_parallel_runner.h
  9. * @brief implementation using std::thread of a ::JxlParallelRunner.
  10. */
  11. /** Implementation of JxlParallelRunner than can be used to enable
  12. * multithreading when using the JPEG XL library. This uses std::thread
  13. * internally and related synchronization functions. The number of threads
  14. * created is fixed at construction time and the threads are re-used for every
  15. * ThreadParallelRunner::Runner call. Only one concurrent
  16. * JxlThreadParallelRunner call per instance is allowed at a time.
  17. *
  18. * This is a scalable, lower-overhead thread pool runner, especially suitable
  19. * for data-parallel computations in the fork-join model, where clients need to
  20. * know when all tasks have completed.
  21. *
  22. * This thread pool can efficiently load-balance millions of tasks using an
  23. * atomic counter, thus avoiding per-task virtual or system calls. With 48
  24. * hyperthreads and 1M tasks that add to an atomic counter, overall runtime is
  25. * 10-20x higher when using std::async, and ~200x for a queue-based thread
  26. */
  27. #ifndef JXL_THREAD_PARALLEL_RUNNER_H_
  28. #define JXL_THREAD_PARALLEL_RUNNER_H_
  29. #include <jxl/jxl_threads_export.h>
  30. #include <jxl/memory_manager.h>
  31. #include <jxl/parallel_runner.h>
  32. #include <stddef.h>
  33. #include <stdint.h>
  34. #include <stdlib.h>
  35. #ifdef __cplusplus
  36. extern "C" {
  37. #endif
  38. /** Parallel runner internally using std::thread. Use as @ref JxlParallelRunner.
  39. */
  40. JXL_THREADS_EXPORT JxlParallelRetCode JxlThreadParallelRunner(
  41. void* runner_opaque, void* jpegxl_opaque, JxlParallelRunInit init,
  42. JxlParallelRunFunction func, uint32_t start_range, uint32_t end_range);
  43. /** Creates the runner for @ref JxlThreadParallelRunner. Use as the opaque
  44. * runner.
  45. */
  46. JXL_THREADS_EXPORT void* JxlThreadParallelRunnerCreate(
  47. const JxlMemoryManager* memory_manager, size_t num_worker_threads);
  48. /** Destroys the runner created by @ref JxlThreadParallelRunnerCreate.
  49. */
  50. JXL_THREADS_EXPORT void JxlThreadParallelRunnerDestroy(void* runner_opaque);
  51. /** Returns a default num_worker_threads value for
  52. * @ref JxlThreadParallelRunnerCreate.
  53. */
  54. JXL_THREADS_EXPORT size_t JxlThreadParallelRunnerDefaultNumWorkerThreads(void);
  55. #ifdef __cplusplus
  56. }
  57. #endif
  58. #endif /* JXL_THREAD_PARALLEL_RUNNER_H_ */
  59. /** @}*/