Skip to content

Commit

Permalink
Context parameters are now immutable. Contexts can now be thread safe.
Browse files Browse the repository at this point in the history
  • Loading branch information
Kerilk committed Jun 28, 2023
1 parent 51576d7 commit b01810a
Show file tree
Hide file tree
Showing 50 changed files with 816 additions and 1,107 deletions.
30 changes: 5 additions & 25 deletions bindings/python/cconfigspace/configuration_space.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,9 @@
from .rng import Rng
from parglare.parser import Context as PContext

ccs_create_configuration_space = _ccs_get_function("ccs_create_configuration_space", [ct.c_char_p, ct.POINTER(ccs_configuration_space)])
ccs_create_configuration_space = _ccs_get_function("ccs_create_configuration_space", [ct.c_char_p, ct.c_size_t, ct.POINTER(ccs_parameter), ct.POINTER(ccs_configuration_space)])
ccs_configuration_space_set_rng = _ccs_get_function("ccs_configuration_space_set_rng", [ccs_configuration_space, ccs_rng])
ccs_configuration_space_get_rng = _ccs_get_function("ccs_configuration_space_get_rng", [ccs_configuration_space, ct.POINTER(ccs_rng)])
ccs_configuration_space_add_parameter = _ccs_get_function("ccs_configuration_space_add_parameter", [ccs_configuration_space, ccs_parameter, ccs_distribution])
ccs_configuration_space_add_parameters = _ccs_get_function("ccs_configuration_space_add_parameters", [ccs_configuration_space, ct.c_size_t, ct.POINTER(ccs_parameter), ct.POINTER(ccs_distribution)])
ccs_configuration_space_set_distribution = _ccs_get_function("ccs_configuration_space_set_distribution", [ccs_configuration_space, ccs_distribution, ct.POINTER(ct.c_size_t)])
ccs_configuration_space_get_parameter_distribution = _ccs_get_function("ccs_configuration_space_get_parameter_distribution", [ccs_configuration_space, ct.c_size_t, ct.POINTER(ccs_distribution), ct.POINTER(ct.c_size_t)])
ccs_configuration_space_set_condition = _ccs_get_function("ccs_configuration_space_set_condition", [ccs_configuration_space, ct.c_size_t, ccs_expression])
Expand All @@ -30,10 +28,12 @@

class ConfigurationSpace(Context):
def __init__(self, handle = None, retain = False, auto_release = True,
name = ""):
name = "", parameters = None):
if handle is None:
count = len(parameters)
parameters = (ccs_parameter * count)(*[x.handle.value for x in parameters])
handle = ccs_configuration_space()
res = ccs_create_configuration_space(str.encode(name), ct.byref(handle))
res = ccs_create_configuration_space(str.encode(name), count, parameters, ct.byref(handle))
Error.check(res)
super().__init__(handle = handle, retain = False)
else:
Expand All @@ -55,26 +55,6 @@ def rng(self, r):
res = ccs_configuration_space_set_rng(self.handle, r.handle)
Error.check(res)

def add_parameter(self, parameter, distribution = None):
if distribution:
distribution = distribution.handle
res = ccs_configuration_space_add_parameter(self.handle, parameter.handle, distribution)
Error.check(res)

def add_parameters(self, parameters, distributions = None):
count = len(parameters)
if count == 0:
return None
if distributions:
if count != len(distributions):
raise Error(Result(Result.ERROR_INVALID_VALUE))
distribs = (ccs_distribution * count)(*[x.handle.value if x else x for x in distributions])
else:
distribs = None
parameters = (ccs_parameter * count)(*[x.handle.value for x in parameters])
res = ccs_configuration_space_add_parameters(self.handle, count, parameters, distribs)
Error.check(res)

def set_distribution(self, distribution, parameters):
count = distribution.dimension
if count != len(parameters):
Expand Down
10 changes: 5 additions & 5 deletions bindings/python/cconfigspace/features_space.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@
from .context import Context
from .parameter import Parameter

ccs_create_features_space = _ccs_get_function("ccs_create_features_space", [ct.c_char_p, ct.POINTER(ccs_features_space)])
ccs_features_space_add_parameter = _ccs_get_function("ccs_features_space_add_parameter", [ccs_features_space, ccs_parameter])
ccs_features_space_add_parameters = _ccs_get_function("ccs_features_space_add_parameters", [ccs_features_space, ct.c_size_t, ct.POINTER(ccs_parameter)])
ccs_create_features_space = _ccs_get_function("ccs_create_features_space", [ct.c_char_p, ct.c_size_t, ct.POINTER(ccs_parameter), ct.POINTER(ccs_features_space)])
ccs_features_space_check_features = _ccs_get_function("ccs_features_space_check_features", [ccs_features_space, ccs_features, ct.POINTER(ccs_bool)])
ccs_features_space_check_features_values = _ccs_get_function("ccs_features_space_check_features_values", [ccs_features_space, ct.c_size_t, ct.POINTER(Datum), ct.POINTER(ccs_bool)])

class FeaturesSpace(Context):
def __init__(self, handle = None, retain = False, auto_release = True,
name = ""):
name = "", parameters = None):
if handle is None:
count = len(parameters)
parameters = (ccs_parameter * count)(*[x.handle.value for x in parameters])
handle = ccs_features_space()
res = ccs_create_features_space(str.encode(name), ct.byref(handle))
res = ccs_create_features_space(str.encode(name), count, parameters, ct.byref(handle))
Error.check(res)
super().__init__(handle = handle, retain = False)
else:
Expand Down
12 changes: 7 additions & 5 deletions bindings/python/cconfigspace/objective_space.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ class ObjectiveType(CEnumeration):
('MINIMIZE', 0),
'MAXIMIZE' ]

ccs_create_objective_space = _ccs_get_function("ccs_create_objective_space", [ct.c_char_p, ct.POINTER(ccs_objective_space)])
ccs_objective_space_add_parameter = _ccs_get_function("ccs_objective_space_add_parameter", [ccs_objective_space, ccs_parameter])
ccs_objective_space_add_parameters = _ccs_get_function("ccs_objective_space_add_parameters", [ccs_objective_space, ct.c_size_t, ct.POINTER(ccs_parameter)])
ccs_create_objective_space = _ccs_get_function("ccs_create_objective_space", [ct.c_char_p, ct.c_size_t, ct.POINTER(ccs_parameter), ct.POINTER(ccs_objective_space)])
ccs_objective_space_add_objective = _ccs_get_function("ccs_objective_space_add_objective", [ccs_objective_space, ccs_expression, ObjectiveType])
ccs_objective_space_add_objectives = _ccs_get_function("ccs_objective_space_add_objectives", [ccs_objective_space, ct.c_size_t, ct.POINTER(ccs_expression), ct.POINTER(ObjectiveType)])
ccs_objective_space_get_objective = _ccs_get_function("ccs_objective_space_get_objective", [ccs_objective_space, ct.c_size_t, ct.POINTER(ccs_expression), ct.POINTER(ObjectiveType)])
Expand All @@ -24,10 +22,14 @@ class ObjectiveType(CEnumeration):

class ObjectiveSpace(Context):
def __init__(self, handle = None, retain = False, auto_release = True,
name = ""):
name = "", parameters = None):
if handle is None:
count = len(parameters)
if count == 0:
raise Error(Result(Result.ERROR_INVALID_VALUE))
parameters = (ccs_parameter * count)(*[x.handle.value for x in parameters])
handle = ccs_objective_space()
res = ccs_create_objective_space(str.encode(name), ct.byref(handle))
res = ccs_create_objective_space(str.encode(name), count, parameters, ct.byref(handle))
Error.check(res)
super().__init__(handle = handle, retain = False)
else:
Expand Down
27 changes: 10 additions & 17 deletions bindings/python/test/test_configuration_space.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,16 @@
class TestConfigurationSpace(unittest.TestCase):

def test_create(self):
cs = ccs.ConfigurationSpace(name = "space")
self.assertEqual( ccs.ObjectType.CONFIGURATION_SPACE, cs.object_type )
self.assertEqual( "space", cs.name )
self.assertIsInstance( cs.rng, ccs.Rng )
self.assertEqual( 0, cs.num_parameters )
self.assertEqual( [], cs.conditions )
self.assertEqual( [], cs.forbidden_clauses )
h1 = ccs.NumericalParameter.Float()
h2 = ccs.NumericalParameter.Float()
h3 = ccs.NumericalParameter.Float()
cs.add_parameter(h1)
cs.add_parameters([h2, h3])
cs = ccs.ConfigurationSpace(name = "space", parameters = [h1, h2, h3])
self.assertEqual( ccs.ObjectType.CONFIGURATION_SPACE, cs.object_type )
self.assertEqual( "space", cs.name )
self.assertIsInstance( cs.rng, ccs.Rng )
self.assertEqual( 3, cs.num_parameters )
self.assertEqual( [None, None, None], cs.conditions )
self.assertEqual( [], cs.forbidden_clauses )
self.assertEqual( h1, cs.parameter(0) )
self.assertEqual( h2, cs.parameter(1) )
self.assertEqual( h3, cs.parameter(2) )
Expand All @@ -35,11 +32,10 @@ def test_create(self):
self.assertTrue( cs.check(c) )

def test_set_distribution(self):
cs = ccs.ConfigurationSpace(name = "space")
h1 = ccs.NumericalParameter.Float()
h2 = ccs.NumericalParameter.Float()
h3 = ccs.NumericalParameter.Float()
cs.add_parameters([h1, h2, h3])
cs = ccs.ConfigurationSpace(name = "space", parameters = [h1, h2, h3])
distributions = [ ccs.UniformDistribution.Float(lower = 0.1, upper = 0.3),
ccs.UniformDistribution.Float(lower = 0.2, upper = 0.6) ]
d = ccs.MultivariateDistribution(distributions = distributions)
Expand All @@ -63,8 +59,7 @@ def test_conditions(self):
h1 = ccs.NumericalParameter.Float(lower = -1.0, upper = 1.0, default = 0.0)
h2 = ccs.NumericalParameter.Float(lower = -1.0, upper = 1.0)
h3 = ccs.NumericalParameter.Float(lower = -1.0, upper = 1.0)
cs = ccs.ConfigurationSpace(name = "space")
cs.add_parameters([h1, h2, h3])
cs = ccs.ConfigurationSpace(name = "space", parameters = [h1, h2, h3])
e1 = ccs.Expression.Less(left = h2, right = 0.0)
cs.set_condition(h3, e1)
e2 = ccs.Expression.Less(left = h3, right = 0.0)
Expand Down Expand Up @@ -142,8 +137,7 @@ def test_omp(self):
name = 'p9',
values = ['1', '8', '16'])

cs = ccs.ConfigurationSpace(name = "omp")
cs.add_parameters([p1, p2, p3, p4, p5, p6, p7, p8, p9])
cs = ccs.ConfigurationSpace(name = "omp", parameters = [p1, p2, p3, p4, p5, p6, p7, p8, p9])

cond0 = ccs.Expression.Equal(left = p1, right = '#pragma omp #P2')
cond1 = ccs.Expression.Equal(left = p1, right = '#pragma omp target teams distribute #P2')
Expand Down Expand Up @@ -239,8 +233,7 @@ def test_omp_parse(self):
name = 'p9',
values = ['1', '8', '16'])

cs = ccs.ConfigurationSpace(name = "omp")
cs.add_parameters([p1, p2, p3, p4, p5, p6, p7, p8, p9])
cs = ccs.ConfigurationSpace(name = "omp", parameters = [p1, p2, p3, p4, p5, p6, p7, p8, p9])

cs.set_condition(p2, "p1 # ['#pragma omp #P2', '#pragma omp target teams distribute #P2']")
cs.set_condition(p4, "p1 == '#pragma omp target teams distribute #P4'")
Expand Down
12 changes: 4 additions & 8 deletions bindings/python/test/test_evaluation.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,13 @@
class TestEvaluation(unittest.TestCase):

def test_create(self):
cs = ccs.ConfigurationSpace(name = "cspace")
h1 = ccs.NumericalParameter.Float()
h2 = ccs.NumericalParameter.Float()
h3 = ccs.NumericalParameter.Float()
cs.add_parameters([h1, h2, h3])
os = ccs.ObjectiveSpace(name = "ospace")
cs = ccs.ConfigurationSpace(name = "cspace", parameters = [h1, h2, h3])
v1 = ccs.NumericalParameter.Float()
v2 = ccs.NumericalParameter.Float()
os.add_parameters([v1, v2])
os = ccs.ObjectiveSpace(name = "ospace", parameters = [v1, v2])
e1 = ccs.Expression.Variable(parameter = v1)
e2 = ccs.Expression.Variable(parameter = v2)
os.add_objectives( { e1: ccs.ObjectiveType.MAXIMIZE, e2: ccs.ObjectiveType.MINIMIZE } )
Expand All @@ -40,15 +38,13 @@ def test_create(self):
self.assertEqual( ccs.Comparison.NOT_COMPARABLE, ev4.compare(ev1) )

def test_serialize(self):
cs = ccs.ConfigurationSpace(name = "cspace")
h1 = ccs.NumericalParameter.Float()
h2 = ccs.NumericalParameter.Float()
h3 = ccs.NumericalParameter.Float()
cs.add_parameters([h1, h2, h3])
os = ccs.ObjectiveSpace(name = "ospace")
cs = ccs.ConfigurationSpace(name = "cspace", parameters = [h1, h2, h3])
v1 = ccs.NumericalParameter.Float()
v2 = ccs.NumericalParameter.Float()
os.add_parameters([v1, v2])
os = ccs.ObjectiveSpace(name = "ospace", parameters = [v1, v2])
e1 = ccs.Expression.Variable(parameter = v1)
e2 = ccs.Expression.Variable(parameter = v2)
os.add_objectives( { e1: ccs.ObjectiveType.MAXIMIZE, e2: ccs.ObjectiveType.MINIMIZE } )
Expand Down
9 changes: 3 additions & 6 deletions bindings/python/test/test_features_space.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,12 @@
class TestFeaturesSpace(unittest.TestCase):

def test_create(self):
cs = ccs.FeaturesSpace(name = "space")
self.assertEqual( ccs.ObjectType.FEATURES_SPACE, cs.object_type )
self.assertEqual( "space", cs.name )
self.assertEqual( 0, cs.num_parameters )
h1 = ccs.NumericalParameter.Float(lower = 0.0, upper = 1.0)
h2 = ccs.NumericalParameter.Float(lower = 0.0, upper = 1.0)
h3 = ccs.NumericalParameter.Float(lower = 0.0, upper = 1.0)
cs.add_parameter(h1)
cs.add_parameters([h2, h3])
cs = ccs.FeaturesSpace(name = "space", parameters = [h1, h2, h3])
self.assertEqual( ccs.ObjectType.FEATURES_SPACE, cs.object_type )
self.assertEqual( "space", cs.name )
self.assertEqual( 3, cs.num_parameters )
self.assertEqual( h1, cs.parameter(0) )
self.assertEqual( h2, cs.parameter(1) )
Expand Down
9 changes: 3 additions & 6 deletions bindings/python/test/test_features_tuner.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,18 @@

class TestFeaturesTuner(unittest.TestCase):
def create_tuning_problem(self):
cs = ccs.ConfigurationSpace(name = "cspace")
h1 = ccs.NumericalParameter.Float(lower = -5.0, upper = 5.0)
h2 = ccs.NumericalParameter.Float(lower = -5.0, upper = 5.0)
h3 = ccs.NumericalParameter.Float(lower = -5.0, upper = 5.0)
cs.add_parameters([h1, h2, h3])
os = ccs.ObjectiveSpace(name = "ospace")
cs = ccs.ConfigurationSpace(name = "cspace", parameters = [h1, h2, h3])
v1 = ccs.NumericalParameter.Float(lower = float('-inf'), upper = float('inf'))
v2 = ccs.NumericalParameter.Float(lower = float('-inf'), upper = float('inf'))
os.add_parameters([v1, v2])
os = ccs.ObjectiveSpace(name = "ospace", parameters = [v1, v2])
e1 = ccs.Expression.Variable(parameter = v1)
e2 = ccs.Expression.Variable(parameter = v2)
os.add_objectives( [e1, e2] )
fs = ccs.FeaturesSpace(name = "fspace")
f1 = ccs.CategoricalParameter(values = [True, False])
fs.add_parameter(f1)
fs = ccs.FeaturesSpace(name = "fspace", parameters = [f1])
return (cs, fs, os)

def test_create_random(self):
Expand Down
9 changes: 3 additions & 6 deletions bindings/python/test/test_objective_space.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,13 @@
class TestObjectiveSpace(unittest.TestCase):

def test_create(self):
os = ccs.ObjectiveSpace(name = "space")
self.assertEqual( "space", os.name )
self.assertEqual( 0, os.num_parameters )
self.assertEqual( [], os.objectives )
h1 = ccs.NumericalParameter.Float()
h2 = ccs.NumericalParameter.Float()
h3 = ccs.NumericalParameter.Float()
os.add_parameter(h1)
os.add_parameters([h2, h3])
os = ccs.ObjectiveSpace(name = "space", parameters = [h1, h2, h3])
self.assertEqual( "space", os.name )
self.assertEqual( 3, os.num_parameters )
self.assertEqual( [], os.objectives )
self.assertEqual( h1, os.parameter(0) )
self.assertEqual( h2, os.parameter(1) )
self.assertEqual( h3, os.parameter(2) )
Expand Down
6 changes: 2 additions & 4 deletions bindings/python/test/test_tree_evaluation.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,9 @@ class TestTreeEvaluation(unittest.TestCase):
def test_create(self):
tree = generate_tree(4, 0)
ts = ccs.StaticTreeSpace(name = 'space', tree = tree)
os = ccs.ObjectiveSpace(name = "ospace")
v1 = ccs.NumericalParameter.Float()
v2 = ccs.NumericalParameter.Float()
os.add_parameters([v1, v2])
os = ccs.ObjectiveSpace(name = "ospace", parameters = [v1, v2])
e1 = ccs.Expression.Variable(parameter = v1)
e2 = ccs.Expression.Variable(parameter = v2)
os.add_objectives( { e1: ccs.ObjectiveType.MAXIMIZE, e2: ccs.ObjectiveType.MINIMIZE } )
Expand All @@ -48,10 +47,9 @@ def test_create(self):
def test_serialize(self):
tree = generate_tree(4, 0)
ts = ccs.StaticTreeSpace(name = 'space', tree = tree)
os = ccs.ObjectiveSpace(name = "ospace")
v1 = ccs.NumericalParameter.Float()
v2 = ccs.NumericalParameter.Float()
os.add_parameters([v1, v2])
os = ccs.ObjectiveSpace(name = "ospace", parameters = [v1, v2])
e1 = ccs.Expression.Variable(parameter = v1)
e2 = ccs.Expression.Variable(parameter = v2)
os.add_objectives( { e1: ccs.ObjectiveType.MAXIMIZE, e2: ccs.ObjectiveType.MINIMIZE } )
Expand Down
3 changes: 1 addition & 2 deletions bindings/python/test/test_tree_tuner.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,8 @@ class TestTreeTuner(unittest.TestCase):
def create_tuning_problem(self):
tree = generate_tree(5, 0)
ts = ccs.StaticTreeSpace(name = 'space', tree = tree)
os = ccs.ObjectiveSpace(name = "ospace")
v1 = ccs.NumericalParameter.Float(lower = float('-inf'), upper = float('inf'))
os.add_parameter(v1)
os = ccs.ObjectiveSpace(name = "ospace", parameters = [v1])
e1 = ccs.Expression.Variable(parameter = v1)
os.add_objectives( {e1: ccs.ObjectiveType.MAXIMIZE} )
return (ts, os)
Expand Down
6 changes: 2 additions & 4 deletions bindings/python/test/test_tuner.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,13 @@

class TestTuner(unittest.TestCase):
def create_tuning_problem(self):
cs = ccs.ConfigurationSpace(name = "cspace")
h1 = ccs.NumericalParameter.Float(lower = -5.0, upper = 5.0)
h2 = ccs.NumericalParameter.Float(lower = -5.0, upper = 5.0)
h3 = ccs.NumericalParameter.Float(lower = -5.0, upper = 5.0)
cs.add_parameters([h1, h2, h3])
os = ccs.ObjectiveSpace(name = "ospace")
cs = ccs.ConfigurationSpace(name = "cspace", parameters = [h1, h2, h3])
v1 = ccs.NumericalParameter.Float(lower = float('-inf'), upper = float('inf'))
v2 = ccs.NumericalParameter.Float(lower = float('-inf'), upper = float('inf'))
os.add_parameters([v1, v2])
os = ccs.ObjectiveSpace(name = "ospace", parameters = [v1, v2])
e1 = ccs.Expression.Variable(parameter = v1)
e2 = ccs.Expression.Variable(parameter = v2)
os.add_objectives( [e1, e2] )
Expand Down
Loading

0 comments on commit b01810a

Please sign in to comment.