Skip to content

Commit

Permalink
chore: study for CuPy version of notebook solution
Browse files Browse the repository at this point in the history
  • Loading branch information
ManasviGoyal committed Jun 28, 2024
1 parent 8e24156 commit d607f75
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions studies/cuda-kernels/combinations/lexicographic_formula.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import cupy as cp

def distincts(content, counts):
counts_comb = counts * (counts - 1) // 2
offsets_comb = cp.cumsum(cp.concatenate((cp.array([0]), counts_comb)))
parents_comb = cp.zeros(8, dtype=int)
for i in range(1, len(offsets_comb)):
parents_comb[offsets_comb[i-1]:offsets_comb[i]] = i - 1

local_indices = cp.arange(offsets_comb[-1]) - offsets_comb[parents_comb]

n = counts[parents_comb]
b = 2 * n - 1
i = cp.floor((b - cp.sqrt(b * b - 8 * local_indices)) / 2).astype(counts_comb.dtype)
j = local_indices + i * (i - b + 2) // 2 + 1

starts_parents = cp.cumsum(cp.concatenate((cp.array([0]), counts)))[:-1][parents_comb]
i += starts_parents
j += starts_parents

if max(i.max(), j.max()) >= len(content):
raise IndexError("index exceeds the bounds of the content array.")

out_content = cp.vstack((content[i], content[j]))

return out_content

content = cp.array([0, 1, 2, 3, 4, 5, 6, 7])
counts = cp.array([2, 0, 4, 2])
result = distincts(content, counts)

print("combinations:\n", result)

0 comments on commit d607f75

Please sign in to comment.