diff --git a/sf-writer/include/ImageAssembler.hpp b/sf-writer/include/ImageAssembler.hpp index 525b4c4..87f1070 100644 --- a/sf-writer/include/ImageAssembler.hpp +++ b/sf-writer/include/ImageAssembler.hpp @@ -15,20 +15,18 @@ class ImageAssembler { ImageMetadataBlock* metadata_buffer_; std::atomic_int* buffer_status_; - int read_slot_id_; - std::atomic_int write_slot_id_; - public: ImageAssembler(const size_t n_modules); virtual ~ImageAssembler(); - int get_free_slot(); - void process(const int slot_id, + bool is_slot_free(const int slot_id); + bool is_slot_full(const int slot_id); + + void process(int slot_id, const int i_module, const BufferBinaryBlock* block_buffer); - int get_full_slot(); void free_slot(const int slot_id); ImageMetadataBlock* get_metadata_buffer(const int slot_id); diff --git a/sf-writer/src/ImageAssembler.cpp b/sf-writer/src/ImageAssembler.cpp index b090489..fe271c4 100644 --- a/sf-writer/src/ImageAssembler.cpp +++ b/sf-writer/src/ImageAssembler.cpp @@ -15,9 +15,6 @@ ImageAssembler::ImageAssembler(const size_t n_modules) : for (size_t i=0; i 0) { - return write_slot_id_; - } + return buffer_status_[slot_id % IA_N_SLOTS] > 0; +} - return -1; +bool ImageAssembler::is_slot_full(const int slot_id) +{ + return buffer_status_[slot_id % IA_N_SLOTS] == 0; } void ImageAssembler::process( - const int slot_id, + int slot_id, const int i_module, const BufferBinaryBlock* block_buffer) { + slot_id %= IA_N_SLOTS; + // TODO: Temp workaround. Make proper initialization. if (i_module == 0) { metadata_buffer_[slot_id].block_first_pulse_id = @@ -72,37 +72,21 @@ void ImageAssembler::process( } } - auto previous_status = buffer_status_[slot_id].fetch_sub(1); - - // 1 because fetch is done before subtraction. - if (previous_status == 1) { - auto next_write_slot_id = (int)((write_slot_id_+1) % IA_N_SLOTS); - write_slot_id_ = next_write_slot_id; - } -} - -int ImageAssembler::get_full_slot() -{ - if (buffer_status_[read_slot_id_] == 0) { - return read_slot_id_; - } - - return -1; + buffer_status_[slot_id]--; } void ImageAssembler::free_slot(const int slot_id) { - buffer_status_[slot_id] = n_modules_; - - read_slot_id_ = (int)((read_slot_id_+1) % IA_N_SLOTS); + buffer_status_[slot_id % IA_N_SLOTS] = n_modules_; } ImageMetadataBlock* ImageAssembler::get_metadata_buffer(const int slot_id) { - return &(metadata_buffer_[slot_id]); + return &(metadata_buffer_[slot_id % IA_N_SLOTS]); } char* ImageAssembler::get_data_buffer(const int slot_id) { - return image_buffer_ + (slot_id * image_buffer_slot_n_bytes_); + return image_buffer_ + + ((slot_id % IA_N_SLOTS) * image_buffer_slot_n_bytes_); } diff --git a/sf-writer/src/main.cpp b/sf-writer/src/main.cpp index 7978eeb..56bb458 100644 --- a/sf-writer/src/main.cpp +++ b/sf-writer/src/main.cpp @@ -26,10 +26,10 @@ void read_buffer( BufferBinaryReader block_reader(device, channel_name); auto block_buffer = new BufferBinaryBlock(); + int slot_id = 0; for (uint64_t block_id:buffer_blocks) { - int slot_id; - while((slot_id = image_assembler.get_free_slot()) == -1) { + while(!image_assembler.is_slot_free(slot_id)) { this_thread::sleep_for(chrono::milliseconds( WRITER_IMAGE_ASSEMBLER_RETRY_MS)); } @@ -45,6 +45,7 @@ void read_buffer( start_time = steady_clock::now(); image_assembler.process(slot_id, i_module, block_buffer); + slot_id++; end_time = steady_clock::now(); uint64_t compose_us_duration = duration_cast( @@ -116,10 +117,10 @@ int main (int argc, char *argv[]) JFH5Writer writer(output_file, start_pulse_id, stop_pulse_id, n_modules); + int slot_id = 0; for (uint64_t block_id:buffer_blocks) { - int slot_id; - while((slot_id = image_assembler.get_full_slot()) == -1) { + while(!image_assembler.is_slot_full(slot_id)) { this_thread::sleep_for(chrono::milliseconds( WRITER_IMAGE_ASSEMBLER_RETRY_MS)); } @@ -136,6 +137,7 @@ int main (int argc, char *argv[]) end_time-start_time).count(); image_assembler.free_slot(slot_id); + slot_id++; cout << "sf_writer:avg_write_us "; cout << write_us_duration / BUFFER_BLOCK_SIZE << endl;