parallel_runner.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. */
  9. /**
  10. * @file parallel_runner.h
  11. */
  12. /** API for running data operations in parallel in a multi-threaded environment.
  13. * This module allows the JPEG XL caller to define their own way of creating and
  14. * assigning threads.
  15. *
  16. * The JxlParallelRunner function type defines a parallel data processing
  17. * runner that may be implemented by the caller to allow the library to process
  18. * in multiple threads. The multi-threaded processing in this library only
  19. * requires to run the same function over each number of a range, possibly
  20. * running each call in a different thread. The JPEG XL caller is responsible
  21. * for implementing this logic using the thread APIs available in their system.
  22. * For convenience, a C++ implementation based on std::thread is provided in
  23. * jpegxl/parallel_runner_thread.h (part of the jpegxl_threads library).
  24. *
  25. * Thread pools usually store small numbers of heterogeneous tasks in a queue.
  26. * When tasks are identical or differ only by an integer input parameter, it is
  27. * much faster to store just one function of an integer parameter and call it
  28. * for each value. Conventional vector-of-tasks can be run in parallel using a
  29. * lambda function adapter that simply calls task_funcs[task].
  30. *
  31. * If no multi-threading is desired, a @c NULL value of JxlParallelRunner
  32. * will use an internal implementation without multi-threading.
  33. */
  34. #ifndef JXL_PARALLEL_RUNNER_H_
  35. #define JXL_PARALLEL_RUNNER_H_
  36. #include <stddef.h>
  37. #include <stdint.h>
  38. #ifdef __cplusplus
  39. extern "C" {
  40. #endif
  41. /** Return code used in the JxlParallel* functions as return value. A value
  42. * of ::JXL_PARALLEL_RET_SUCCESS means success and any other value means error.
  43. * The special value ::JXL_PARALLEL_RET_RUNNER_ERROR can be used by the runner
  44. * to indicate any other error.
  45. */
  46. typedef int JxlParallelRetCode;
  47. /**
  48. * Code returned by the @ref JxlParallelRunInit function to indicate success.
  49. */
  50. #define JXL_PARALLEL_RET_SUCCESS (0)
  51. /**
  52. * Code returned by the @ref JxlParallelRunInit function to indicate a general
  53. * error.
  54. */
  55. #define JXL_PARALLEL_RET_RUNNER_ERROR (-1)
  56. /**
  57. * Parallel run initialization callback. See @ref JxlParallelRunner for details.
  58. *
  59. * This function MUST be called by the JxlParallelRunner only once, on the
  60. * same thread that called @ref JxlParallelRunner, before any parallel
  61. * execution. The purpose of this call is to provide the maximum number of
  62. * threads that the
  63. * @ref JxlParallelRunner will use, which can be used by JPEG XL to allocate
  64. * per-thread storage if needed.
  65. *
  66. * @param jpegxl_opaque the @p jpegxl_opaque handle provided to
  67. * @ref JxlParallelRunner() must be passed here.
  68. * @param num_threads the maximum number of threads. This value must be
  69. * positive.
  70. * @return 0 if the initialization process was successful.
  71. * @return an error code if there was an error, which should be returned by
  72. * @ref JxlParallelRunner().
  73. */
  74. typedef JxlParallelRetCode (*JxlParallelRunInit)(void* jpegxl_opaque,
  75. size_t num_threads);
  76. /**
  77. * Parallel run data processing callback. See @ref JxlParallelRunner for
  78. * details.
  79. *
  80. * This function MUST be called once for every number in the range [start_range,
  81. * end_range) (including start_range but not including end_range) passing this
  82. * number as the @p value. Calls for different value may be executed from
  83. * different threads in parallel.
  84. *
  85. * @param jpegxl_opaque the @p jpegxl_opaque handle provided to
  86. * @ref JxlParallelRunner() must be passed here.
  87. * @param value the number in the range [start_range, end_range) of the call.
  88. * @param thread_id the thread number where this function is being called from.
  89. * This must be lower than the @p num_threads value passed to
  90. * @ref JxlParallelRunInit.
  91. */
  92. typedef void (*JxlParallelRunFunction)(void* jpegxl_opaque, uint32_t value,
  93. size_t thread_id);
  94. /**
  95. * JxlParallelRunner function type. A parallel runner implementation can be
  96. * provided by a JPEG XL caller to allow running computations in multiple
  97. * threads. This function must call the initialization function @p init in the
  98. * same thread that called it and then call the passed @p func once for every
  99. * number in the range [start_range, end_range) (including start_range but not
  100. * including end_range) possibly from different multiple threads in parallel.
  101. *
  102. * The @ref JxlParallelRunner function does not need to be re-entrant. This
  103. * means that the same @ref JxlParallelRunner function with the same
  104. * runner_opaque provided parameter will not be called from the library from
  105. * either @p init or
  106. * @p func in the same decoder or encoder instance. However, a single decoding
  107. * or encoding instance may call the provided @ref JxlParallelRunner multiple
  108. * times for different parts of the decoding or encoding process.
  109. *
  110. * @return 0 if the @p init call succeeded (returned 0) and no other error
  111. * occurred in the runner code.
  112. * @return JXL_PARALLEL_RET_RUNNER_ERROR if an error occurred in the runner
  113. * code, for example, setting up the threads.
  114. * @return the return value of @p init() if non-zero.
  115. */
  116. typedef JxlParallelRetCode (*JxlParallelRunner)(
  117. void* runner_opaque, void* jpegxl_opaque, JxlParallelRunInit init,
  118. JxlParallelRunFunction func, uint32_t start_range, uint32_t end_range);
  119. /* The following is an example of a @ref JxlParallelRunner that doesn't use any
  120. * multi-threading. Note that this implementation doesn't store any state
  121. * between multiple calls of the ExampleSequentialRunner function, so the
  122. * runner_opaque value is not used.
  123. JxlParallelRetCode ExampleSequentialRunner(void* runner_opaque,
  124. void* jpegxl_opaque,
  125. JxlParallelRunInit init,
  126. JxlParallelRunFunction func,
  127. uint32_t start_range,
  128. uint32_t end_range) {
  129. // We only use one thread (the currently running thread).
  130. JxlParallelRetCode init_ret = (*init)(jpegxl_opaque, 1);
  131. if (init_ret != 0) return init_ret;
  132. // In case of other initialization error (for example when initializing the
  133. // threads) one can return JXL_PARALLEL_RET_RUNNER_ERROR.
  134. for (uint32_t i = start_range; i < end_range; i++) {
  135. // Every call is in the thread number 0. These don't need to be in any
  136. // order.
  137. (*func)(jpegxl_opaque, i, 0);
  138. }
  139. return JXL_PARALLEL_RET_SUCCESS;
  140. }
  141. */
  142. #ifdef __cplusplus
  143. }
  144. #endif
  145. #endif /* JXL_PARALLEL_RUNNER_H_ */
  146. /** @}*/