Small refactoring
This commit is contained in:
@@ -52,7 +52,7 @@ void link(sm::block* out, sm::block* in) {
|
|||||||
in->input_blocks.push_back(out);
|
in->input_blocks.push_back(out);
|
||||||
}
|
}
|
||||||
|
|
||||||
PIVector<sm::block*> scheme_generate(int blocks_count = 100, int begin_block_count = 3, int expansion = 3, float connectivity = 0.3) {
|
PIVector<sm::block*> scheme_generate(int blocks_count = 100, int begin_block_count = 3, int expansion_shift = 0, int expansion_d = 3, float connectivity = 0.3) {
|
||||||
PIVector<sm::block*> start_blocks;
|
PIVector<sm::block*> start_blocks;
|
||||||
if (blocks_count <= 0) return start_blocks;
|
if (blocks_count <= 0) return start_blocks;
|
||||||
is_calc_statuses.resize(blocks_count, false);
|
is_calc_statuses.resize(blocks_count, false);
|
||||||
@@ -66,7 +66,7 @@ PIVector<sm::block*> scheme_generate(int blocks_count = 100, int begin_block_cou
|
|||||||
|
|
||||||
PIVector<sm::block*> current_blocks = start_blocks;
|
PIVector<sm::block*> current_blocks = start_blocks;
|
||||||
do {
|
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;
|
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;
|
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);
|
block->barrier.clear(std::memory_order_release);
|
||||||
}
|
}
|
||||||
|
|
||||||
int try_lock(PIVector<sm::block*>& block_pool) {
|
bool try_lock(sm::block* block) {
|
||||||
for (int i = 0; i < block_pool.size(); ++i) {
|
if (block->barrier.test_and_set(std::memory_order_acquire)) return false;
|
||||||
auto block = block_pool[i];
|
|
||||||
if (block->barrier.test_and_set(std::memory_order_acquire)) continue;
|
|
||||||
|
|
||||||
int locks_count = 0;
|
int locks_count = 0;
|
||||||
for (auto & input_block : block->input_blocks) {
|
for (auto & input_block : block->input_blocks) {
|
||||||
if (input_block->barrier.test_and_set(std::memory_order_acquire)) break;
|
if (input_block->barrier.test_and_set(std::memory_order_acquire)) break;
|
||||||
locks_count++;
|
locks_count++;
|
||||||
}
|
}
|
||||||
if (locks_count == block->input_blocks.size()) return i;
|
|
||||||
|
if (locks_count == block->input_blocks.size()) {
|
||||||
unlock(block, locks_count);
|
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<sm::block*>& block_pool, bool& is_block_pool_empty, PIVector<sm::block*>& new_available_blocks) {
|
sm::block* try_lock_next_and_post(std::atomic_flag& block_pool_flag, PIVector<sm::block*>& block_pool, bool& is_block_pool_empty, PIVector<sm::block*>& new_available_blocks) {
|
||||||
@@ -163,11 +163,13 @@ sm::block* try_lock_next_and_post(std::atomic_flag& block_pool_flag, PIVector<sm
|
|||||||
is_block_pool_empty = true;
|
is_block_pool_empty = true;
|
||||||
} else {
|
} else {
|
||||||
is_block_pool_empty = false;
|
is_block_pool_empty = false;
|
||||||
int block_idx = try_lock(block_pool);
|
for (int i = 0; i < block_pool.size(); ++i) {
|
||||||
if (block_idx != -1) {
|
if (try_lock(block_pool[i])) {
|
||||||
block = block_pool[block_idx];
|
block = block_pool[i];
|
||||||
block_pool.remove(block_idx);
|
block_pool.remove(i);
|
||||||
// std::cout << block->is_calc_idx << ": locked for calc" << std::endl;
|
// 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<sm::block*>& new_avai
|
|||||||
}
|
}
|
||||||
|
|
||||||
sm::time_report algorithm_runnable(std::atomic_flag& block_pool_flag,
|
sm::time_report algorithm_runnable(std::atomic_flag& block_pool_flag,
|
||||||
PIVector<sm::block*>& block_pool,
|
PIVector<sm::block*>& block_pool,
|
||||||
std::atomic_int& waiting_threads_flags,
|
std::atomic_int& waiting_threads_flags,
|
||||||
unsigned i, unsigned thread_count) {
|
unsigned i, unsigned thread_count) {
|
||||||
bool is_block_pool_empty;
|
bool is_block_pool_empty;
|
||||||
bool is_block_pool_empty_old;
|
bool is_block_pool_empty_old;
|
||||||
PIVector<sm::block*> new_available_blocks;
|
PIVector<sm::block*> new_available_blocks;
|
||||||
@@ -290,8 +292,8 @@ void print_performance(std::vector<std::future<sm::time_report>>& duration_futur
|
|||||||
int main() {
|
int main() {
|
||||||
srand(time(nullptr));
|
srand(time(nullptr));
|
||||||
|
|
||||||
PIVector<sm::block*> start_blocks = scheme_generate(1000, 3, 4, 0.2);
|
PIVector<sm::block*> start_blocks = scheme_generate(100, 3, 1, 3, 0.1);
|
||||||
// scheme_print(start_blocks);
|
scheme_print(start_blocks);
|
||||||
|
|
||||||
auto duration_futures = check_performance(start_blocks, 1);
|
auto duration_futures = check_performance(start_blocks, 1);
|
||||||
print_performance(duration_futures);
|
print_performance(duration_futures);
|
||||||
|
|||||||
Reference in New Issue
Block a user