| 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 |
|
| 13 |
#include <stdlib.h> |
| 14 |
#include "iochain.h" |
| 15 |
|
| 16 |
|
| 17 |
void ioc_init(ioc_chain *C, const void *in_ptr_0, void *out_ptr_0) { |
| 18 |
#ifdef _OPENMP |
| 19 |
omp_init_lock(&C->next_lock); |
| 20 |
for (size_t ii = 0; ii < IOC_SIZE; ii ++) { |
| 21 |
omp_init_lock(&(C->in_pl[ii].lock)); |
| 22 |
omp_init_lock(&(C->out_pl[ii].lock)); |
| 23 |
} |
| 24 |
#endif |
| 25 |
C->next = 0; |
| 26 |
C->in_pl[0].ptr = in_ptr_0; |
| 27 |
C->out_pl[0].ptr = out_ptr_0; |
| 28 |
} |
| 29 |
|
| 30 |
|
| 31 |
void ioc_destroy(ioc_chain *C) { |
| 32 |
#ifdef _OPENMP |
| 33 |
omp_destroy_lock(&C->next_lock); |
| 34 |
for (size_t ii = 0; ii < IOC_SIZE; ii ++) { |
| 35 |
omp_destroy_lock(&(C->in_pl[ii].lock)); |
| 36 |
omp_destroy_lock(&(C->out_pl[ii].lock)); |
| 37 |
} |
| 38 |
#endif |
| 39 |
} |
| 40 |
|
| 41 |
|
| 42 |
const void * ioc_get_in(ioc_chain *C, size_t *this_iter) { |
| 43 |
#ifdef _OPENMP |
| 44 |
omp_set_lock(&C->next_lock); |
| 45 |
#pragma omp flush |
| 46 |
#endif |
| 47 |
*this_iter = C->next; |
| 48 |
C->next ++; |
| 49 |
#ifdef _OPENMP |
| 50 |
omp_set_lock(&(C->in_pl[*this_iter % IOC_SIZE].lock)); |
| 51 |
omp_set_lock(&(C->in_pl[(*this_iter + 1) % IOC_SIZE].lock)); |
| 52 |
omp_set_lock(&(C->out_pl[(*this_iter + 1) % IOC_SIZE].lock)); |
| 53 |
omp_unset_lock(&C->next_lock); |
| 54 |
#endif |
| 55 |
return C->in_pl[*this_iter % IOC_SIZE].ptr; |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
void ioc_set_next_in(ioc_chain *C, size_t* this_iter, void* in_ptr) { |
| 60 |
C->in_pl[(*this_iter + 1) % IOC_SIZE].ptr = in_ptr; |
| 61 |
#ifdef _OPENMP |
| 62 |
omp_unset_lock(&(C->in_pl[(*this_iter + 1) % IOC_SIZE].lock)); |
| 63 |
#endif |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
void * ioc_get_out(ioc_chain *C, size_t *this_iter) { |
| 68 |
#ifdef _OPENMP |
| 69 |
omp_set_lock(&(C->out_pl[(*this_iter) % IOC_SIZE].lock)); |
| 70 |
#pragma omp flush |
| 71 |
#endif |
| 72 |
void *out_ptr = C->out_pl[*this_iter % IOC_SIZE].ptr; |
| 73 |
#ifdef _OPENMP |
| 74 |
omp_unset_lock(&(C->out_pl[(*this_iter) % IOC_SIZE].lock)); |
| 75 |
#endif |
| 76 |
return out_ptr; |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
void ioc_set_next_out(ioc_chain *C, size_t *this_iter, void* out_ptr) { |
| 81 |
C->out_pl[(*this_iter + 1) % IOC_SIZE].ptr = out_ptr; |
| 82 |
#ifdef _OPENMP |
| 83 |
omp_unset_lock(&(C->out_pl[(*this_iter + 1) % IOC_SIZE].lock)); |
| 84 |
// *in_pl[this_iter]* lock released at the end of the iteration to avoid being |
| 85 |
// overtaken by previous threads and having *out_pl[this_iter]* corrupted. |
| 86 |
// Especially worried about thread 0, iteration 0. |
| 87 |
omp_unset_lock(&(C->in_pl[(*this_iter) % IOC_SIZE].lock)); |
| 88 |
#endif |
| 89 |
} |