From 05e4175ef6fd5f4c33bd0dea8837a8bf511d89c9 Mon Sep 17 00:00:00 2001 From: Stepan Fomenko Date: Mon, 13 Jul 2020 15:24:45 +0300 Subject: [PATCH] Small refactoring --- experiments/block_choice.cpp | 52 +++++++++++++++++++----------------- 1 file changed, 27 insertions(+), 25 deletions(-) diff --git a/experiments/block_choice.cpp b/experiments/block_choice.cpp index 4f56999..2d2bea7 100644 --- a/experiments/block_choice.cpp +++ b/experiments/block_choice.cpp @@ -52,7 +52,7 @@ void link(sm::block* out, sm::block* in) { in->input_blocks.push_back(out); } -PIVector scheme_generate(int blocks_count = 100, int begin_block_count = 3, int expansion = 3, float connectivity = 0.3) { +PIVector scheme_generate(int blocks_count = 100, int begin_block_count = 3, int expansion_shift = 0, int expansion_d = 3, float connectivity = 0.3) { PIVector start_blocks; if (blocks_count <= 0) return start_blocks; is_calc_statuses.resize(blocks_count, false); @@ -66,7 +66,7 @@ PIVector scheme_generate(int blocks_count = 100, int begin_block_cou PIVector current_blocks = start_blocks; do { - int new_blocks_count = rand() % (2 * expansion) - expansion + current_blocks.size(); + int new_blocks_count = rand() % (2 * expansion_d) - expansion_d + expansion_shift + current_blocks.size(); if (new_blocks_count < 1) new_blocks_count = 1; new_blocks_count = new_blocks_count + all_block_count > blocks_count ? blocks_count - all_block_count : new_blocks_count; @@ -134,21 +134,21 @@ void unlock(sm::block* block, int locks_count = -1) { block->barrier.clear(std::memory_order_release); } -int try_lock(PIVector& block_pool) { - for (int i = 0; i < block_pool.size(); ++i) { - auto block = block_pool[i]; - if (block->barrier.test_and_set(std::memory_order_acquire)) continue; +bool try_lock(sm::block* block) { + if (block->barrier.test_and_set(std::memory_order_acquire)) return false; - int locks_count = 0; - for (auto & input_block : block->input_blocks) { - if (input_block->barrier.test_and_set(std::memory_order_acquire)) break; - locks_count++; - } - if (locks_count == block->input_blocks.size()) return i; - - unlock(block, locks_count); + int locks_count = 0; + for (auto & input_block : block->input_blocks) { + if (input_block->barrier.test_and_set(std::memory_order_acquire)) break; + locks_count++; + } + + if (locks_count == block->input_blocks.size()) { + return true; + } else { + unlock(block, locks_count); + return false; } - return -1; } sm::block* try_lock_next_and_post(std::atomic_flag& block_pool_flag, PIVector& block_pool, bool& is_block_pool_empty, PIVector& new_available_blocks) { @@ -163,11 +163,13 @@ sm::block* try_lock_next_and_post(std::atomic_flag& block_pool_flag, PIVectoris_calc_idx << ": locked for calc" << std::endl; + for (int i = 0; i < block_pool.size(); ++i) { + if (try_lock(block_pool[i])) { + block = block_pool[i]; + block_pool.remove(i); +// std::cout << block->is_calc_idx << ": locked for calc" << std::endl; + break; + } } } @@ -203,9 +205,9 @@ void post_available_blocks(sm::block* calc_block, PIVector& new_avai } sm::time_report algorithm_runnable(std::atomic_flag& block_pool_flag, - PIVector& block_pool, - std::atomic_int& waiting_threads_flags, - unsigned i, unsigned thread_count) { + PIVector& block_pool, + std::atomic_int& waiting_threads_flags, + unsigned i, unsigned thread_count) { bool is_block_pool_empty; bool is_block_pool_empty_old; PIVector new_available_blocks; @@ -290,8 +292,8 @@ void print_performance(std::vector>& duration_futur int main() { srand(time(nullptr)); - PIVector start_blocks = scheme_generate(1000, 3, 4, 0.2); -// scheme_print(start_blocks); + PIVector start_blocks = scheme_generate(100, 3, 1, 3, 0.1); + scheme_print(start_blocks); auto duration_futures = check_performance(start_blocks, 1); print_performance(duration_futures);