Skip to content

Commit

Permalink
Configurations are immutable and validated at construction.
Browse files Browse the repository at this point in the history
  • Loading branch information
Kerilk committed Oct 11, 2024
1 parent ccbd8e2 commit c88992a
Show file tree
Hide file tree
Showing 15 changed files with 88 additions and 267 deletions.
7 changes: 0 additions & 7 deletions bindings/python/cconfigspace/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
ccs_create_configuration = _ccs_get_function("ccs_create_configuration", [ccs_configuration_space, ccs_features, ct.c_size_t, ct.POINTER(Datum), ct.POINTER(ccs_configuration)])
ccs_configuration_get_configuration_space = _ccs_get_function("ccs_configuration_get_configuration_space", [ccs_configuration, ct.POINTER(ccs_configuration_space)])
ccs_configuration_get_features = _ccs_get_function("ccs_configuration_get_features", [ccs_configuration, ct.POINTER(ccs_features)])
ccs_configuration_check = _ccs_get_function("ccs_configuration_check", [ccs_configuration, ct.POINTER(ccs_bool)])

class Configuration(Binding):
def __init__(self, handle = None, retain = False, auto_release = True,
Expand Down Expand Up @@ -57,9 +56,3 @@ def features(self):
else:
self._features = None
return self._features

def check(self):
valid = ccs_bool()
res = ccs_configuration_check(self.handle, ct.byref(valid))
Error.check(res)
return False if valid.value == 0 else True
7 changes: 0 additions & 7 deletions bindings/python/cconfigspace/configuration_space.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
ccs_configuration_space_get_conditions = _ccs_get_function("ccs_configuration_space_get_conditions", [ccs_configuration_space, ct.c_size_t, ct.POINTER(ccs_expression), ct.POINTER(ct.c_size_t)])
ccs_configuration_space_get_forbidden_clause = _ccs_get_function("ccs_configuration_space_get_forbidden_clause", [ccs_configuration_space, ct.c_size_t, ct.POINTER(ccs_expression)])
ccs_configuration_space_get_forbidden_clauses = _ccs_get_function("ccs_configuration_space_get_forbidden_clauses", [ccs_configuration_space, ct.c_size_t, ct.POINTER(ccs_expression), ct.POINTER(ct.c_size_t)])
ccs_configuration_space_check_configuration = _ccs_get_function("ccs_configuration_space_check_configuration", [ccs_configuration_space, ccs_configuration, ct.POINTER(ccs_bool)])
ccs_configuration_space_get_default_configuration = _ccs_get_function("ccs_configuration_space_get_default_configuration", [ccs_configuration_space, ccs_feature_space, ct.POINTER(ccs_configuration)])
ccs_configuration_space_sample = _ccs_get_function("ccs_configuration_space_sample", [ccs_configuration_space, ccs_distribution_space, ccs_feature_space, ccs_rng, ct.POINTER(ccs_configuration)])
ccs_configuration_space_samples = _ccs_get_function("ccs_configuration_space_samples", [ccs_configuration_space, ccs_distribution_space, ccs_feature_space, ccs_rng, ct.c_size_t, ct.POINTER(ccs_configuration)])
Expand Down Expand Up @@ -171,12 +170,6 @@ def forbidden_clauses(self):
self._forbidden_clauses = tuple(Expression.from_handle(ccs_expression(x)) for x in v)
return self._forbidden_clauses

def check(self, configuration):
valid = ccs_bool()
res = ccs_configuration_space_check_configuration(self.handle, configuration.handle, ct.byref(valid))
Error.check(res)
return False if valid.value == 0 else True

def default_configuration(self, features = None):
v = ccs_configuration()
if features is not None:
Expand Down
10 changes: 2 additions & 8 deletions bindings/python/test/test_configuration_space.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,10 @@ def test_create(self):
self.assertEqual( h3, cs.parameter(2) )
self.assertEqual( (h1, h2, h3), cs.parameters )
self.assertEqual( h2, cs.parameter_by_name(h2.name) )
self.assertTrue( cs.check(cs.default_configuration()) )
self.assertIsNone( cs.feature_space )
c = cs.sample()
self.assertTrue( cs.check(c) )
self.assertEqual( cs.handle.value, c.configuration_space.handle.value )
for c in cs.samples(100):
self.assertTrue( cs.check(c) )
cs.samples(100)

def test_set_distribution(self):
h1 = ccs.NumericalParameter.Float()
Expand Down Expand Up @@ -113,7 +110,7 @@ def extract_active_parameters(self, values):
res = ['p1']
for v in values:
if v != ccs.inactive:
res += ["p{}".format(m[1]) for m in re.finditer('#P(\d)', v)]
res += ["p{}".format(m[1]) for m in re.finditer('#P(\\d)', v)]
return res

def test_omp(self):
Expand Down Expand Up @@ -202,7 +199,6 @@ def test_omp(self):
all_params = [ "p{}".format(i) for i in range(1,10) ]
for i in range(1000):
s = cs.sample()
self.assertTrue( s.check() )
active_params = self.extract_active_parameters(s.values)
for par in active_params:
self.assertNotEqual( ccs.inactive, s.value(par) )
Expand Down Expand Up @@ -274,7 +270,6 @@ def test_omp_parse(self):
all_params = [ "p{}".format(i) for i in range(1,10) ]
for i in range(1000):
s = cs.sample()
self.assertTrue( s.check() )
active_params = self.extract_active_parameters(s.values)
for par in active_params:
self.assertNotEqual( ccs.inactive, s.value(par) )
Expand All @@ -287,7 +282,6 @@ def test_omp_parse(self):
cs_copy = ccs.Object.deserialize(buffer = buff)
for i in range(1000):
s = cs_copy.sample()
self.assertTrue( s.check() )
active_params = self.extract_active_parameters(s.values)
for par in active_params:
self.assertNotEqual( ccs.inactive, s.value(par) )
Expand Down
9 changes: 0 additions & 9 deletions bindings/ruby/lib/cconfigspace/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ module CCS

attach_function :ccs_create_configuration, [:ccs_configuration_space_t, :ccs_features_t, :size_t, :pointer, :pointer], :ccs_result_t
attach_function :ccs_configuration_get_features, [:ccs_configuration_t, :pointer], :ccs_result_t
attach_function :ccs_configuration_check, [:ccs_configuration_t, :pointer], :ccs_result_t

class Configuration < Binding
alias configuration_space context
Expand Down Expand Up @@ -32,13 +31,5 @@ def initialize(handle = nil, retain: false, auto_release: true,
def self.from_handle(handle, retain: true, auto_release: true)
self::new(handle, retain: retain, auto_release: auto_release)
end

def check
ptr = MemoryPointer::new(:ccs_bool_t)
CCS.error_check CCS.ccs_configuration_check(@handle, ptr)
return ptr.read_ccs_bool_t == CCS::FALSE ? false : true
end

end

end
7 changes: 0 additions & 7 deletions bindings/ruby/lib/cconfigspace/configuration_space.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ module CCS
attach_function :ccs_configuration_space_get_conditions, [:ccs_configuration_space_t, :size_t, :pointer, :pointer], :ccs_result_t
attach_function :ccs_configuration_space_get_forbidden_clause, [:ccs_configuration_space_t, :size_t, :pointer], :ccs_result_t
attach_function :ccs_configuration_space_get_forbidden_clauses, [:ccs_configuration_space_t, :size_t, :pointer, :pointer], :ccs_result_t
attach_function :ccs_configuration_space_check_configuration, [:ccs_configuration_space_t, :ccs_configuration_t, :pointer], :ccs_result_t
attach_function :ccs_configuration_space_get_default_configuration, [:ccs_configuration_space_t, :ccs_features_t, :pointer], :ccs_result_t
attach_function :ccs_configuration_space_sample, [:ccs_configuration_space_t, :ccs_distribution_space_t, :ccs_features_t, :ccs_rng_t, :pointer], :ccs_result_t
attach_function :ccs_configuration_space_samples, [:ccs_configuration_space_t, :ccs_distribution_space_t, :ccs_features_t, :ccs_rng_t, :size_t, :pointer], :ccs_result_t
Expand Down Expand Up @@ -106,12 +105,6 @@ def forbidden_clause(index)
Expression.from_handle(ptr.read_ccs_expression_t)
end

def check(configuration)
ptr = MemoryPointer::new(:ccs_bool_t)
CCS.error_check CCS.ccs_configuration_space_check_configuration(@handle, configuration.handle, ptr)
return ptr.read_ccs_bool_t == CCS::FALSE ? false : true
end

def default_configuration(features: nil)
ptr = MemoryPointer::new(:ccs_configuration_t)
CCS.error_check CCS.ccs_configuration_space_get_default_configuration(@handle, features, ptr)
Expand Down
9 changes: 1 addition & 8 deletions bindings/ruby/test/test_configuration_space.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,10 @@ def test_create
assert_equal( h3, cs.parameter(2) )
assert_equal( [h1, h2, h3], cs.parameters )
assert_equal( h2, cs.parameter_by_name(h2.name) )
assert( cs.check(cs.default_configuration) )
assert_nil( cs.feature_space )
c = cs.sample
assert( cs.check(c) )
assert_equal( cs.handle, c.configuration_space.handle )
cs.samples(100).each { |c|
assert( cs.check(c) )
}
cs.samples(100)
end

def test_set_distribution
Expand Down Expand Up @@ -211,7 +207,6 @@ def test_omp

1000.times {
s = cs.sample
assert( s.check )
active_params = extract_active_parameters(s.values)
active_params.each { |par|
refute_equal( CCS::Inactive, s.value(par) )
Expand Down Expand Up @@ -289,7 +284,6 @@ def test_omp_parse

1000.times {
s = cs.sample
assert( s.check )
active_params = extract_active_parameters(s.values)
active_params.each { |par|
refute_equal( CCS::Inactive, s.value(par) )
Expand All @@ -305,7 +299,6 @@ def test_omp_parse
cs_copy = CCS::deserialize(buffer: buff)
1000.times {
s = cs_copy.sample
assert( s.check )
active_params = extract_active_parameters(s.values)
active_params.each { |par|
refute_equal( CCS::Inactive, s.value(par) )
Expand Down
24 changes: 0 additions & 24 deletions include/cconfigspace/configuration.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,30 +82,6 @@ ccs_configuration_get_features(
ccs_configuration_t configuration,
ccs_features_t *features_ret);

/**
* Check that the configuration is a valid configuration for the configuration
* space.
* @param[in] configuration
* @param[out] is_valid_ret a pointer to a variable that will hold the result
* of the check. Result will be #CCS_TRUE if the
* configuration is valid. Result will be #CCS_FALSE
* if an active parameter value is not a valid value
* for this parameter; or if an inactive parameter
* value is not inactive; or if a forbidden clause
* would be evaluating to #ccs_true
* @return #CCS_RESULT_SUCCESS on success
* @return #CCS_RESULT_ERROR_INVALID_OBJECT if \p configuration is not a valid
* CCS configuration
* @return #CCS_RESULT_ERROR_INVALID_CONFIGURATION if \p configuration has
* become invalid for the configuration space
* @remarks
* This function is thread-safe
*/
extern ccs_result_t
ccs_configuration_check(
ccs_configuration_t configuration,
ccs_bool_t *is_valid_ret);

#ifdef __cplusplus
}
#endif
Expand Down
27 changes: 0 additions & 27 deletions include/cconfigspace/configuration_space.h
Original file line number Diff line number Diff line change
Expand Up @@ -204,33 +204,6 @@ ccs_configuration_space_get_forbidden_clauses(
ccs_expression_t *expressions,
size_t *num_expressions_ret);

/**
* Check that a configuration is a valid in a configuration space.
* @param[in] configuration_space
* @param[in] configuration
* @param[out] is_valid_ret a pointer to a variable that will hold the result
* of the check. Result will be #CCS_TRUE if the
* configuration is valid. Result will be #CCS_FALSE
* if an active parameter value is not a valid value
* for this parameter; or if an inactive parameter
* value is not inactive; or if a forbidden clause
* would be evaluating to #ccs_true
* @return #CCS_RESULT_SUCCESS on success
* @return #CCS_RESULT_ERROR_INVALID_OBJECT if \p configuration_space is not a
* valid CCS configuration space; or if \p configuration is not a valid CCS
* configuration
* @return #CCS_RESULT_ERROR_INVALID_VALUE if \p is_valid_ret is NULL
* @return #CCS_RESULT_ERROR_INVALID_CONFIGURATION if \p configuration is not
* associated to the configuration space
* @remarks
* This function is thread-safe
*/
extern ccs_result_t
ccs_configuration_space_check_configuration(
ccs_configuration_space_t configuration_space,
ccs_configuration_t configuration,
ccs_bool_t *is_valid_ret);

/**
* Get the default configuration of a configuration space
* @param[in] configuration_space
Expand Down
38 changes: 15 additions & 23 deletions src/configuration.c
Original file line number Diff line number Diff line change
Expand Up @@ -199,18 +199,22 @@ _ccs_create_configuration(
config->data->num_bindings++;
}
if (values) {
memcpy(config->data->values, values,
num_parameters * sizeof(ccs_datum_t));
ccs_bool_t is_valid;
for (size_t i = 0; i < num_values; i++)
if (values[i].flags & CCS_DATUM_FLAG_TRANSIENT)
CCS_VALIDATE_ERR_GOTO(
err,
ccs_context_validate_value(
(ccs_context_t)
configuration_space,
i, values[i],
config->data->values + i),
errinit);
CCS_VALIDATE_ERR_GOTO(
err,
ccs_context_validate_value(
(ccs_context_t)configuration_space, i,
values[i], config->data->values + i),
errinit);
CCS_VALIDATE_ERR_GOTO(
err,
_check_configuration(
configuration_space, config, &is_valid),
errinit);
CCS_REFUTE_ERR_GOTO(
err, !is_valid, CCS_RESULT_ERROR_INVALID_VALUE,
errinit);
}
*configuration_ret = config;
return CCS_RESULT_SUCCESS;
Expand Down Expand Up @@ -269,15 +273,3 @@ ccs_configuration_get_features(
*features_ret = configuration->data->features;
return CCS_RESULT_SUCCESS;
}

ccs_result_t
ccs_configuration_check(
ccs_configuration_t configuration,
ccs_bool_t *is_valid_ret)
{
CCS_CHECK_OBJ(configuration, CCS_OBJECT_TYPE_CONFIGURATION);
CCS_VALIDATE(ccs_configuration_space_check_configuration(
configuration->data->configuration_space, configuration,
is_valid_ret));
return CCS_RESULT_SUCCESS;
}
84 changes: 0 additions & 84 deletions src/configuration_space.c
Original file line number Diff line number Diff line change
Expand Up @@ -725,90 +725,6 @@ ccs_configuration_space_get_default_configuration(
return err;
}

static ccs_result_t
_test_forbidden(
ccs_configuration_space_t configuration_space,
ccs_configuration_t configuration,
ccs_bool_t *is_valid)
{
ccs_expression_t *forbidden_clauses =
configuration_space->data->forbidden_clauses;
*is_valid = CCS_FALSE;
for (size_t i = 0; i < configuration_space->data->num_forbidden_clauses;
i++) {
ccs_datum_t result;
CCS_VALIDATE(ccs_expression_eval(
forbidden_clauses[i], configuration->data->num_bindings,
configuration->data->bindings, &result));
if (result.type == CCS_DATA_TYPE_INACTIVE)
continue;
if (result.type == CCS_DATA_TYPE_BOOL &&
result.value.i == CCS_TRUE)
return CCS_RESULT_SUCCESS;
}
*is_valid = CCS_TRUE;
return CCS_RESULT_SUCCESS;
}

static inline ccs_result_t
_check_configuration(
ccs_configuration_space_t configuration_space,
ccs_configuration_t configuration,
ccs_bool_t *is_valid_ret)
{
size_t *indexes = configuration_space->data->sorted_indexes;
ccs_parameter_t *parameters = configuration_space->data->parameters;
ccs_expression_t *conditions = configuration_space->data->conditions;
ccs_datum_t *values = configuration->data->values;

for (size_t i = 0; i < configuration_space->data->num_parameters; i++) {
ccs_bool_t active = CCS_TRUE;
size_t index = indexes[i];
if (conditions[index]) {
ccs_datum_t result;
CCS_VALIDATE(ccs_expression_eval(
conditions[index],
configuration->data->num_bindings,
configuration->data->bindings, &result));
if (!(result.type == CCS_DATA_TYPE_BOOL &&
result.value.i == CCS_TRUE))
active = CCS_FALSE;
}
if (active != (values[index].type == CCS_DATA_TYPE_INACTIVE ?
CCS_FALSE :
CCS_TRUE)) {
*is_valid_ret = CCS_FALSE;
return CCS_RESULT_SUCCESS;
}
if (active) {
CCS_VALIDATE(ccs_parameter_check_value(
parameters[index], values[index],
is_valid_ret));
if (*is_valid_ret == CCS_FALSE)
return CCS_RESULT_SUCCESS;
}
}
CCS_VALIDATE(_test_forbidden(
configuration_space, configuration, is_valid_ret));
return CCS_RESULT_SUCCESS;
}

ccs_result_t
ccs_configuration_space_check_configuration(
ccs_configuration_space_t configuration_space,
ccs_configuration_t configuration,
ccs_bool_t *is_valid_ret)
{
CCS_CHECK_OBJ(configuration_space, CCS_OBJECT_TYPE_CONFIGURATION_SPACE);
CCS_CHECK_OBJ(configuration, CCS_OBJECT_TYPE_CONFIGURATION);
CCS_REFUTE(
configuration->data->configuration_space != configuration_space,
CCS_RESULT_ERROR_INVALID_CONFIGURATION);
CCS_VALIDATE(_check_configuration(
configuration_space, configuration, is_valid_ret));
return CCS_RESULT_SUCCESS;
}

static ccs_result_t
_sample(ccs_configuration_space_t configuration_space,
ccs_distribution_space_t distribution_space,
Expand Down
Loading

0 comments on commit c88992a

Please sign in to comment.