| 1 |
/* |
| 2 |
* IOchain - Distribute a chain of dependent IO events among threads. |
| 3 |
* |
| 4 |
* This file is part of Bitshuffle |
| 5 |
* Author: Kiyoshi Masui <[email protected]> |
| 6 |
* Website: https://www.github.com/kiyo-masui/bitshuffle |
| 7 |
* Created: 2014 |
| 8 |
* |
| 9 |
* See LICENSE file for details about copyright and rights to use. |
| 10 |
* |
| 11 |
* |
| 12 |
* Header File |
| 13 |
* |
| 14 |
* Similar in concept to a queue. Each task includes reading an input |
| 15 |
* and writing output, but the location of the input/output (the pointers) |
| 16 |
* depend on the previous item in the chain. |
| 17 |
* |
| 18 |
* This is designed for parallelizing blocked compression/decompression IO, |
| 19 |
* where the destination of a compressed block depends on the compressed size |
| 20 |
* of all previous blocks. |
| 21 |
* |
| 22 |
* Implemented with OpenMP locks. |
| 23 |
* |
| 24 |
* |
| 25 |
* Usage |
| 26 |
* ----- |
| 27 |
* - Call `ioc_init` in serial block. |
| 28 |
* - Each thread should create a local variable *size_t this_iter* and |
| 29 |
* pass its address to all function calls. Its value will be set |
| 30 |
* inside the functions and is used to identify the thread. |
| 31 |
* - Each thread must call each of the `ioc_get*` and `ioc_set*` methods |
| 32 |
* exactly once per iteration, starting with `ioc_get_in` and ending |
| 33 |
* with `ioc_set_next_out`. |
| 34 |
* - The order (`ioc_get_in`, `ioc_set_next_in`, *work*, `ioc_get_out`, |
| 35 |
* `ioc_set_next_out`, *work*) is most efficient. |
| 36 |
* - Have each thread call `ioc_end_pop`. |
| 37 |
* - `ioc_get_in` is blocked until the previous entry's |
| 38 |
* `ioc_set_next_in` is called. |
| 39 |
* - `ioc_get_out` is blocked until the previous entry's |
| 40 |
* `ioc_set_next_out` is called. |
| 41 |
* - There are no blocks on the very first iteration. |
| 42 |
* - Call `ioc_destroy` in serial block. |
| 43 |
* - Safe for num_threads >= IOC_SIZE (but less efficient). |
| 44 |
* |
| 45 |
*/ |
| 46 |
|
| 47 |
|
| 48 |
#ifndef IOCHAIN_H |
| 49 |
#define IOCHAIN_H |
| 50 |
|
| 51 |
|
| 52 |
#include <stdlib.h> |
| 53 |
#ifdef _OPENMP |
| 54 |
#include <omp.h> |
| 55 |
#endif |
| 56 |
|
| 57 |
|
| 58 |
#define IOC_SIZE 33 |
| 59 |
|
| 60 |
|
| 61 |
typedef struct ioc_ptr_and_lock { |
| 62 |
#ifdef _OPENMP |
| 63 |
omp_lock_t lock; |
| 64 |
#endif |
| 65 |
void *ptr; |
| 66 |
} ptr_and_lock; |
| 67 |
|
| 68 |
typedef struct ioc_const_ptr_and_lock { |
| 69 |
#ifdef _OPENMP |
| 70 |
omp_lock_t lock; |
| 71 |
#endif |
| 72 |
const void *ptr; |
| 73 |
} const_ptr_and_lock; |
| 74 |
|
| 75 |
|
| 76 |
typedef struct ioc_chain { |
| 77 |
#ifdef _OPENMP |
| 78 |
omp_lock_t next_lock; |
| 79 |
#endif |
| 80 |
size_t next; |
| 81 |
const_ptr_and_lock in_pl[IOC_SIZE]; |
| 82 |
ptr_and_lock out_pl[IOC_SIZE]; |
| 83 |
} ioc_chain; |
| 84 |
|
| 85 |
|
| 86 |
void ioc_init(ioc_chain *C, const void *in_ptr_0, void *out_ptr_0); |
| 87 |
void ioc_destroy(ioc_chain *C); |
| 88 |
const void * ioc_get_in(ioc_chain *C, size_t *this_iter); |
| 89 |
void ioc_set_next_in(ioc_chain *C, size_t* this_iter, void* in_ptr); |
| 90 |
void * ioc_get_out(ioc_chain *C, size_t *this_iter); |
| 91 |
void ioc_set_next_out(ioc_chain *C, size_t *this_iter, void* out_ptr); |
| 92 |
|
| 93 |
#endif // IOCHAIN_H |