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