1
0
mirror of https://github.com/exaloop/codon.git synced 2025-06-03 15:03:52 +08:00
codon/bench/set_partition/set_partition.py
A. R. Shajii e1016f9e9a
Upgrade to LLVM 15 (#56)
* Upgrade to LLVM 15 (WIP)

* Call `setPresplitCoroutine()` on coro LLVM funcs

* Use new pass manager

* Update deps

* Update docs

* Fix exceptions on M1

* Add libunwind

* Use Orc and JITLink for "codon run"

* JITLink integration

* Fix callback

* Fix strbuf, fix GC root registration

* Fix test init

* Fix llvm function

* Fix pickle, float atomics

* Add TargetLibraryAnalysis

* Use new LLVM pass manager in GPU codegen

* Fix libunwind linking

* Fix libunwind

* Fix GPU passes

* Don't link libunwind explicitly

* Bump version

* Update plugins for new pass manager

* Fix bash error

* Fix GPU GV extraction

* Move simd module to experimental folder

* Update file read

* Add benchmark programs

* Add dynamic tuple

* Fix parser tuple slicing bug

* Make DynamicTuple interoperable with Tuple

* Fix DynamicTuple GPU interop

* Dockerize builds

* Simplify CMake

* Revert "Simplify CMake"

This reverts commit 08d2920349b5033750b54b4fb7aaa9bac264b750.

Co-authored-by: Ibrahim Numanagić <ibrahimpasa@gmail.com>
2022-10-14 09:31:10 -04:00

52 lines
1.5 KiB
Python

# https://stackoverflow.com/questions/73473074/speed-up-set-partition-generation-by-skipping-ones-with-subsets-smaller-or-large
import time
def conforms(candidate, minsize, forgive):
"""
Check if partition `candidate` is at most `forgive` additions from making
all its elements conform to having minimum size `minsize`
"""
deficit = 0
for p in candidate:
need = minsize - len(p)
if need > 0:
deficit += need
# Is the deficit small enough?
return (deficit <= forgive)
def partition_filtered(collection, minsize=1, forgive=0):
"""
Generate partitions that contain at least `minsize` elements per set;
allow `forgive` missing elements, which can get added in subsequent steps
"""
if len(collection) == 1:
yield [ collection ]
return
first = collection[0]
for smaller in partition_filtered(collection[1:], minsize, forgive=forgive+1):
# insert `first` in each of the subpartition's subsets
for n, subset in enumerate(smaller):
candidate = smaller[:n] + [[ first ] + subset] + smaller[n+1:]
if conforms(candidate, minsize, forgive):
yield candidate
# put `first` in its own subset
candidate = [ [ first ] ] + smaller
if conforms(candidate, minsize, forgive):
yield candidate
import time
t = time.time()
something = list(range(1, 14))
v = partition_filtered(something, minsize=2)
x = 0
for p in v:
p.sort()
x += p[len(p) // 3][0]
print(x)
print(time.time() - t)