Skip to content

Commit

Permalink
Prefer expected == atomic_compare_exchange(ptr, expected, desired) (#…
Browse files Browse the repository at this point in the history
…2387)

* Prefer expected == atomic_compare_exchange(ptr, expected, desired)

Signed-off-by: Damien L-G <dalg24@gmail.com>

* Reintroduce break statement that was accidentally removed

Signed-off-by: Damien L-G <dalg24@gmail.com>

---------

Signed-off-by: Damien L-G <dalg24@gmail.com>
  • Loading branch information
dalg24 authored Oct 18, 2024
1 parent ff46aee commit 8b318e6
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 38 deletions.
2 changes: 1 addition & 1 deletion common/src/KokkosKernels_HashmapAccumulator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,7 @@ struct HashmapAccumulator {
Kokkos::atomic_add(values + hash, value);
return __insert_success;
} else if (keys[hash] == -1) {
if (Kokkos::atomic_compare_exchange_strong(keys + hash, -1, key)) {
if (-1 == Kokkos::atomic_compare_exchange(keys + hash, -1, key)) {
// should only be here if we used a new hash
used_hashes[Kokkos::atomic_fetch_add(used_hash_size, size_type(1))] = hash;
Kokkos::atomic_add(values + hash, value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ class UniformMemoryPool {
data_type *get_arbitrary_free_chunk(const size_t &thread_index, const size_t max_tries) const {
size_t chunk_index = thread_index & modular_num_chunks;
size_t num_try = 0;
while (!Kokkos::atomic_compare_exchange_strong(pchunk_locks + chunk_index, 0, 1)) {
while (0 != Kokkos::atomic_compare_exchange(pchunk_locks + chunk_index, 0, 1)) {
chunk_index = (chunk_index + 1) & modular_num_chunks;
++num_try;
if (num_try > max_tries) {
Expand Down
2 changes: 1 addition & 1 deletion graph/impl/KokkosGraph_ExplicitCoarsening_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ struct ExplicitGraphCoarsening {
KOKKOS_INLINE_FUNCTION bool insert(lno_t cluster, lno_t nei, int* table) const {
unsigned h = xorshiftHash(nei);
for (unsigned i = h; i < h + 2; i++) {
if (Kokkos::atomic_compare_exchange_strong(&table[i % tableSize()], cluster, nei)) return true;
if (cluster == Kokkos::atomic_compare_exchange(&table[i % tableSize()], cluster, nei)) return true;
}
return false;
}
Expand Down
18 changes: 9 additions & 9 deletions graph/src/KokkosGraph_CoarsenHeuristics.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class coarsen_heuristics {
if (bucket >= t_buckets) bucket -= t_buckets;
if (buckets(bucket) == ORD_MAX) {
// attempt to insert into bucket
if (Kokkos::atomic_compare_exchange_strong(&buckets(bucket), ORD_MAX, i)) {
if (ORD_MAX == Kokkos::atomic_compare_exchange(&buckets(bucket), ORD_MAX, i)) {
break;
}
}
Expand Down Expand Up @@ -140,8 +140,8 @@ class coarsen_heuristics {
// need to enforce an ordering condition to allow hard-stall
// conditions to be broken
if (condition ^ swap) {
if (Kokkos::atomic_compare_exchange_strong(&match(u), ORD_MAX, v)) {
if (u == v || Kokkos::atomic_compare_exchange_strong(&match(v), ORD_MAX, u)) {
if (ORD_MAX == Kokkos::atomic_compare_exchange(&match(u), ORD_MAX, v)) {
if (u == v || ORD_MAX == Kokkos::atomic_compare_exchange(&match(v), ORD_MAX, u)) {
ordinal_t cv = Kokkos::atomic_fetch_add(&nvertices_coarse(), 1);
vcmap(u) = cv;
vcmap(v) = cv;
Expand Down Expand Up @@ -201,8 +201,8 @@ class coarsen_heuristics {
// need to enforce an ordering condition to allow hard-stall
// conditions to be broken
if (condition ^ swap) {
if (Kokkos::atomic_compare_exchange_strong(&match(u), ORD_MAX, v)) {
if (u == v || Kokkos::atomic_compare_exchange_strong(&match(v), ORD_MAX, u)) {
if (ORD_MAX == Kokkos::atomic_compare_exchange(&match(u), ORD_MAX, v)) {
if (u == v || ORD_MAX == Kokkos::atomic_compare_exchange(&match(v), ORD_MAX, u)) {
ordinal_t cv = u;
if (v < u) {
cv = v;
Expand Down Expand Up @@ -859,8 +859,8 @@ class coarsen_heuristics {
// need to enforce an ordering condition to allow hard-stall
// conditions to be broken
if (condition ^ swap) {
if (Kokkos::atomic_compare_exchange_strong(&match(u), ORD_MAX, v)) {
if (u == v || Kokkos::atomic_compare_exchange_strong(&match(v), ORD_MAX, u)) {
if (ORD_MAX == Kokkos::atomic_compare_exchange(&match(u), ORD_MAX, v)) {
if (u == v || ORD_MAX == Kokkos::atomic_compare_exchange(&match(v), ORD_MAX, u)) {
// u == v avoids problems if there is a self-loop edge
ordinal_t cv = Kokkos::atomic_fetch_add(&nvertices_coarse(), 1);
vcmap(u) = cv;
Expand Down Expand Up @@ -1084,8 +1084,8 @@ class coarsen_heuristics {
// need to enforce an ordering condition to allow hard-stall
// conditions to be broken
if (condition ^ swap) {
if (Kokkos::atomic_compare_exchange_strong(&match(u), ORD_MAX, v)) {
if (Kokkos::atomic_compare_exchange_strong(&match(v), ORD_MAX, u)) {
if (ORD_MAX == Kokkos::atomic_compare_exchange(&match(u), ORD_MAX, v)) {
if (ORD_MAX == Kokkos::atomic_compare_exchange(&match(v), ORD_MAX, u)) {
ordinal_t cv = Kokkos::atomic_fetch_add(&nvertices_coarse(), 1);
vcmap(u) = cv;
vcmap(v) = cv;
Expand Down
14 changes: 8 additions & 6 deletions sparse/impl/KokkosSparse_bspgemm_impl_kkmem.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -607,7 +607,7 @@ struct KokkosBSPGEMM<HandleType, a_row_view_t_, a_lno_nnz_view_t_, a_scalar_nnz_
if (!insert_is_on) {
try_to_insert = false;
break;
} else if (Kokkos::atomic_compare_exchange_strong(keys + trial, init_value, my_b_col)) {
} else if (init_value == Kokkos::atomic_compare_exchange(keys + trial, init_value, my_b_col)) {
kk_vector_block_add_mul(block_dim, vals + trial * block_size, a_val, b_val);
Kokkos::atomic_inc(used_hash_sizes);
if (used_hash_sizes[0] > max_first_level_hash_size) insert_is_on = false;
Expand All @@ -629,7 +629,7 @@ struct KokkosBSPGEMM<HandleType, a_row_view_t_, a_lno_nnz_view_t_, a_scalar_nnz_
} else if (keys[trial] == init_value) {
if (!insert_is_on) {
break;
} else if (Kokkos::atomic_compare_exchange_strong(keys + trial, init_value, my_b_col)) {
} else if (init_value == Kokkos::atomic_compare_exchange(keys + trial, init_value, my_b_col)) {
kk_vector_block_add_mul(block_dim, vals + trial * block_size, a_val, b_val);
Kokkos::atomic_inc(used_hash_sizes);
if (used_hash_sizes[0] > max_first_level_hash_size) insert_is_on = false;
Expand All @@ -651,7 +651,8 @@ struct KokkosBSPGEMM<HandleType, a_row_view_t_, a_lno_nnz_view_t_, a_scalar_nnz_
fail = 0;
break;
} else if (global_acc_row_keys[trial] == init_value) {
if (Kokkos::atomic_compare_exchange_strong(global_acc_row_keys + trial, init_value, my_b_col)) {
if (init_value ==
Kokkos::atomic_compare_exchange(global_acc_row_keys + trial, init_value, my_b_col)) {
kk_vector_block_add_mul(block_dim, global_acc_row_vals + trial * block_size, a_val, b_val);
// Kokkos::atomic_inc(used_hash_sizes + 1);
// c_row_vals[trial] = my_b_val;
Expand All @@ -669,7 +670,8 @@ struct KokkosBSPGEMM<HandleType, a_row_view_t_, a_lno_nnz_view_t_, a_scalar_nnz_
kk_vector_block_add_mul(block_dim, global_acc_row_vals + trial * block_size, a_val, b_val);
break;
} else if (global_acc_row_keys[trial] == init_value) {
if (Kokkos::atomic_compare_exchange_strong(global_acc_row_keys + trial, init_value, my_b_col)) {
if (init_value ==
Kokkos::atomic_compare_exchange(global_acc_row_keys + trial, init_value, my_b_col)) {
// Kokkos::atomic_inc(used_hash_sizes + 1);
kk_vector_block_add_mul(block_dim, global_acc_row_vals + trial * block_size, a_val, b_val);
// c_row_vals[trial] = my_b_val;
Expand Down Expand Up @@ -877,7 +879,7 @@ struct KokkosBSPGEMM<HandleType, a_row_view_t_, a_lno_nnz_view_t_, a_scalar_nnz_
fail = 0;
break;
} else if (keys[trial] == init_value) {
if (Kokkos::atomic_compare_exchange_strong(keys + trial, init_value, my_b_col)) {
if (init_value == Kokkos::atomic_compare_exchange(keys + trial, init_value, my_b_col)) {
kk_vector_block_add_mul(block_dim, vals + trial * block_size, a_val, b_val);
fail = 0;
break;
Expand All @@ -893,7 +895,7 @@ struct KokkosBSPGEMM<HandleType, a_row_view_t_, a_lno_nnz_view_t_, a_scalar_nnz_
fail = 0;
break;
} else if (keys[trial] == init_value) {
if (Kokkos::atomic_compare_exchange_strong(keys + trial, init_value, my_b_col)) {
if (init_value == Kokkos::atomic_compare_exchange(keys + trial, init_value, my_b_col)) {
kk_vector_block_add_mul(block_dim, vals + trial * block_size, a_val, b_val);
fail = 0;
break;
Expand Down
2 changes: 1 addition & 1 deletion sparse/impl/KokkosSparse_partitioning_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ struct BalloonClustering {
auto state = randPool.get_state();
do {
root = state.rand(numRows);
} while (!Kokkos::atomic_compare_exchange_strong(&vertClusters(root), numClusters, i));
} while (numClusters != Kokkos::atomic_compare_exchange(&vertClusters(root), numClusters, i));
randPool.free_state(state);
distances(root) = 0;
pressure(root) = 1;
Expand Down
2 changes: 1 addition & 1 deletion sparse/impl/KokkosSparse_spgemm_impl_compression.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,7 @@ struct KokkosSPGEMM<HandleType, a_row_view_t_, a_lno_nnz_view_t_, a_scalar_nnz_v
Kokkos::atomic_fetch_or(result_vals + new_hash, n_set);
break;
} else if (result_keys[new_hash] == r) {
if (Kokkos::atomic_compare_exchange_strong(result_keys + new_hash, r, n_set_index)) {
if (r == Kokkos::atomic_compare_exchange(result_keys + new_hash, r, n_set_index)) {
// MD 4/4/18: on these architectures there can be divergence in
// the warp. once the keys are set, some other vector lane might
// be doing a fetch_or before we set with n_set. Therefore it is
Expand Down
14 changes: 8 additions & 6 deletions sparse/impl/KokkosSparse_spgemm_impl_kkmem.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -692,7 +692,7 @@ struct KokkosSPGEMM<HandleType, a_row_view_t_, a_lno_nnz_view_t_, a_scalar_nnz_v
if (!insert_is_on) {
try_to_insert = false;
break;
} else if (Kokkos::atomic_compare_exchange_strong(keys + trial, init_value, my_b_col)) {
} else if (init_value == Kokkos::atomic_compare_exchange(keys + trial, init_value, my_b_col)) {
Kokkos::atomic_add(vals + trial, my_b_val);
Kokkos::atomic_inc(used_hash_sizes);
if (used_hash_sizes[0] > max_first_level_hash_size) insert_is_on = false;
Expand All @@ -714,7 +714,7 @@ struct KokkosSPGEMM<HandleType, a_row_view_t_, a_lno_nnz_view_t_, a_scalar_nnz_v
} else if (keys[trial] == init_value) {
if (!insert_is_on) {
break;
} else if (Kokkos::atomic_compare_exchange_strong(keys + trial, init_value, my_b_col)) {
} else if (init_value == Kokkos::atomic_compare_exchange(keys + trial, init_value, my_b_col)) {
Kokkos::atomic_add(vals + trial, my_b_val);
Kokkos::atomic_inc(used_hash_sizes);
if (used_hash_sizes[0] > max_first_level_hash_size) insert_is_on = false;
Expand All @@ -737,7 +737,8 @@ struct KokkosSPGEMM<HandleType, a_row_view_t_, a_lno_nnz_view_t_, a_scalar_nnz_v
fail = 0;
break;
} else if (global_acc_row_keys[trial] == init_value) {
if (Kokkos::atomic_compare_exchange_strong(global_acc_row_keys + trial, init_value, my_b_col)) {
if (init_value ==
Kokkos::atomic_compare_exchange(global_acc_row_keys + trial, init_value, my_b_col)) {
Kokkos::atomic_add(global_acc_row_vals + trial, my_b_val);
// Kokkos::atomic_inc(used_hash_sizes + 1);
// c_row_vals[trial] = my_b_val;
Expand All @@ -756,7 +757,8 @@ struct KokkosSPGEMM<HandleType, a_row_view_t_, a_lno_nnz_view_t_, a_scalar_nnz_v

break;
} else if (global_acc_row_keys[trial] == init_value) {
if (Kokkos::atomic_compare_exchange_strong(global_acc_row_keys + trial, init_value, my_b_col)) {
if (init_value ==
Kokkos::atomic_compare_exchange(global_acc_row_keys + trial, init_value, my_b_col)) {
// Kokkos::atomic_inc(used_hash_sizes + 1);
Kokkos::atomic_add(global_acc_row_vals + trial, my_b_val);
// c_row_vals[trial] = my_b_val;
Expand Down Expand Up @@ -976,7 +978,7 @@ struct KokkosSPGEMM<HandleType, a_row_view_t_, a_lno_nnz_view_t_, a_scalar_nnz_v
fail = 0;
break;
} else if (keys[trial] == init_value) {
if (Kokkos::atomic_compare_exchange_strong(keys + trial, init_value, my_b_col)) {
if (init_value == Kokkos::atomic_compare_exchange(keys + trial, init_value, my_b_col)) {
Kokkos::atomic_add(vals + trial, my_b_val);
fail = 0;
break;
Expand All @@ -992,7 +994,7 @@ struct KokkosSPGEMM<HandleType, a_row_view_t_, a_lno_nnz_view_t_, a_scalar_nnz_v
fail = 0;
break;
} else if (keys[trial] == init_value) {
if (Kokkos::atomic_compare_exchange_strong(keys + trial, init_value, my_b_col)) {
if (init_value == Kokkos::atomic_compare_exchange(keys + trial, init_value, my_b_col)) {
Kokkos::atomic_add(vals + trial, my_b_val);
fail = 0;
break;
Expand Down
Loading

0 comments on commit 8b318e6

Please sign in to comment.