Skip to content

Commit

Permalink
Add rng as part of the configuration space constructor, making these …
Browse files Browse the repository at this point in the history
…immutable.
  • Loading branch information
Kerilk committed Mar 26, 2024
1 parent f8b3f1f commit 7750a5b
Show file tree
Hide file tree
Showing 17 changed files with 67 additions and 131 deletions.
15 changes: 6 additions & 9 deletions bindings/python/cconfigspace/configuration_space.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
from .expression_parser import parser
from .rng import Rng

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_expression), ct.c_size_t, ct.POINTER(ccs_expression), ct.POINTER(ccs_configuration_space)])
ccs_configuration_space_set_rng = _ccs_get_function("ccs_configuration_space_set_rng", [ccs_configuration_space, ccs_rng])
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_expression), ct.c_size_t, ct.POINTER(ccs_expression), ccs_rng, ct.POINTER(ccs_configuration_space)])
ccs_configuration_space_get_rng = _ccs_get_function("ccs_configuration_space_get_rng", [ccs_configuration_space, ct.POINTER(ccs_rng)])
ccs_configuration_space_get_condition = _ccs_get_function("ccs_configuration_space_get_condition", [ccs_configuration_space, ct.c_size_t, ct.POINTER(ccs_expression)])
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)])
Expand All @@ -21,7 +20,7 @@

class ConfigurationSpace(Context):
def __init__(self, handle = None, retain = False, auto_release = True,
name = "", parameters = None, conditions = None, forbidden_clauses = None):
name = "", parameters = None, conditions = None, forbidden_clauses = None, rng = None):
if handle is None:
count = len(parameters)

Expand Down Expand Up @@ -52,9 +51,12 @@ def __init__(self, handle = None, retain = False, auto_release = True,
else:
cv = None

if rng is not None:
rng = rng.handle

parameters = (ccs_parameter * count)(*[x.handle.value for x in parameters])
handle = ccs_configuration_space()
res = ccs_create_configuration_space(str.encode(name), count, parameters, cv, numfc, fcv, ct.byref(handle))
res = ccs_create_configuration_space(str.encode(name), count, parameters, cv, numfc, fcv, rng, ct.byref(handle))
Error.check(res)
super().__init__(handle = handle, retain = False)
else:
Expand All @@ -71,11 +73,6 @@ def rng(self):
Error.check(res)
return Rng.from_handle(v)

@rng.setter
def rng(self, r):
res = ccs_configuration_space_set_rng(self.handle, r.handle)
Error.check(res)

def condition(self, parameter):
if isinstance(parameter, Parameter):
parameter = self.parameter_index(parameter)
Expand Down
12 changes: 3 additions & 9 deletions bindings/ruby/lib/cconfigspace/configuration_space.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
module CCS
attach_function :ccs_create_configuration_space, [:string, :size_t, :pointer, :pointer, :size_t, :pointer, :pointer], :ccs_result_t
attach_function :ccs_configuration_space_set_rng, [:ccs_configuration_space_t, :ccs_rng_t], :ccs_result_t
attach_function :ccs_create_configuration_space, [:string, :size_t, :pointer, :pointer, :size_t, :pointer, :ccs_rng_t, :pointer], :ccs_result_t
attach_function :ccs_configuration_space_get_rng, [:ccs_configuration_space_t, :pointer], :ccs_result_t
attach_function :ccs_configuration_space_get_condition, [:ccs_configuration_space_t, :size_t, :pointer], :ccs_result_t
attach_function :ccs_configuration_space_get_conditions, [:ccs_configuration_space_t, :size_t, :pointer, :pointer], :ccs_result_t
Expand All @@ -14,7 +13,7 @@ module CCS
class ConfigurationSpace < Context

def initialize(handle = nil, retain: false, auto_release: true,
name: "", parameters: nil, conditions: nil, forbidden_clauses: nil)
name: "", parameters: nil, conditions: nil, forbidden_clauses: nil, rng: nil)
if handle
super(handle, retain: retain, auto_release: auto_release)
else
Expand Down Expand Up @@ -58,7 +57,7 @@ def initialize(handle = nil, retain: false, auto_release: true,
cptr = nil
end

CCS.error_check CCS.ccs_create_configuration_space(name, count, p_parameters, cptr, fccount, fcptr, ptr)
CCS.error_check CCS.ccs_create_configuration_space(name, count, p_parameters, cptr, fccount, fcptr, rng, ptr)
super(ptr.read_ccs_configuration_space_t, retain:false)
end
end
Expand All @@ -73,11 +72,6 @@ def rng
Rng::from_handle(ptr.read_ccs_rng_t)
end

def rng=(r)
CCS.error_check CCS.ccs_configuration_space_set_rng(@handle, r)
r
end

def condition(parameter)
case parameter
when Parameter
Expand Down
2 changes: 1 addition & 1 deletion connectors/kokkos/ccs-kokkos-connector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,7 @@ kokkosp_request_values(

CCS_CHECK(ccs_create_configuration_space(
("cs (region: " + std::to_string(regionCounter) + ")").c_str(),
numTuningVariables, cs_parameters, NULL, 0, NULL, &cs));
numTuningVariables, cs_parameters, NULL, 0, NULL, NULL, &cs));
delete[] cs_parameters;

#if CCS_DEBUG
Expand Down
20 changes: 4 additions & 16 deletions include/cconfigspace/configuration_space.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ extern "C" {
* @param[in] forbidden_clauses an array o \p num_forbidden_clauses expressions
* to add as forbidden clauses to the
* configuration space
* @param[in] rng an optional CCS rng object
* @param[out] configuration_space_ret a pointer to the variable that will hold
* the newly created configuration space
* @return #CCS_RESULT_SUCCESS on success
Expand All @@ -35,7 +36,8 @@ extern "C" {
* num_parameters is NULL; or if \p forbidden_clauses is NULL and \p
* num_forbidden_clauses is greater than 0
* @return #CCS_RESULT_ERROR_INVALID_OBJECT if a parameter is not a valid CCS
* parameter; or if an expression is not a valid CCS expression
* parameter; or if an expression is not a valid CCS expression; or if \p
* rng is not NULL and is not a valid CCS rng
* @return #CCS_RESULT_ERROR_INVALID_PARAMETER if a parameter's type is
* CCS_PARAMETER_TYPE_STRING; or if a parameter appears more than once in \p
* parameters; or if two or more parameters share the same name; or if an
Expand All @@ -57,23 +59,9 @@ ccs_create_configuration_space(
ccs_expression_t *conditions,
size_t num_forbidden_clauses,
ccs_expression_t *forbidden_clauses,
ccs_rng_t rng,
ccs_configuration_space_t *configuration_space_ret);

/**
* Set (replace) the internal rng of the configuration space.
* @param[in,out] configuration_space
* @param[in] rng the rng to use in the configuration space
* @return #CCS_RESULT_SUCCESS on success
* @return #CCS_RESULT_ERROR_INVALID_OBJECT if \p configuration_space is not a
* valid CCS configuration space; or \p rng is not a valid CCS rng
* @remarks
* This function is thread-safe
*/
extern ccs_result_t
ccs_configuration_space_set_rng(
ccs_configuration_space_t configuration_space,
ccs_rng_t rng);

/**
* Get the internal rng of the configuration space.
* @param[in] configuration_space
Expand Down
2 changes: 1 addition & 1 deletion samples/test_python.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ create_problem(ccs_configuration_space_t *cs, ccs_objective_space_t *os)
parameters[1] = parameter2 = create_numerical("y", -5.0, 5.0);

err = ccs_create_configuration_space(
"2dplane", 2, parameters, NULL, 0, NULL, &cspace);
"2dplane", 2, parameters, NULL, 0, NULL, NULL, &cspace);
assert(err == CCS_RESULT_SUCCESS);

parameter3 = create_numerical("z", -CCS_INFINITY, CCS_INFINITY);
Expand Down
2 changes: 1 addition & 1 deletion samples/test_ruby.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ create_problem(ccs_configuration_space_t *cs, ccs_objective_space_t *os)
parameters[1] = parameter2 = create_numerical("y", -5.0, 5.0);

err = ccs_create_configuration_space(
"2dplane", 2, parameters, NULL, 0, NULL, &cspace);
"2dplane", 2, parameters, NULL, 0, NULL, NULL, &cspace);
assert(err == CCS_RESULT_SUCCESS);

parameter3 = create_numerical("z", -CCS_INFINITY, CCS_INFINITY);
Expand Down
88 changes: 24 additions & 64 deletions src/configuration_space.c
Original file line number Diff line number Diff line change
Expand Up @@ -188,30 +188,19 @@ _ccs_configuration_space_serialize_size(
size_t *cum_size,
_ccs_object_serialize_options_t *opts)
{
ccs_result_t err = CCS_RESULT_SUCCESS;
CCS_OBJ_RDLOCK(object);
switch (format) {
case CCS_SERIALIZE_FORMAT_BINARY:
CCS_VALIDATE_ERR_GOTO(
err,
_ccs_serialize_bin_size_ccs_configuration_space(
(ccs_configuration_space_t)object, cum_size,
opts),
end);
CCS_VALIDATE(_ccs_serialize_bin_size_ccs_configuration_space(
(ccs_configuration_space_t)object, cum_size, opts));
break;
default:
CCS_RAISE_ERR_GOTO(
err, CCS_RESULT_ERROR_INVALID_VALUE, end,
CCS_RAISE(
CCS_RESULT_ERROR_INVALID_VALUE,
"Unsupported serialization format: %d", format);
}
CCS_VALIDATE_ERR_GOTO(
err,
_ccs_object_serialize_user_data_size(
object, format, cum_size, opts),
end);
end:
CCS_OBJ_UNLOCK(object);
return err;
CCS_VALIDATE(_ccs_object_serialize_user_data_size(
object, format, cum_size, opts));
return CCS_RESULT_SUCCESS;
}

static ccs_result_t
Expand All @@ -222,30 +211,20 @@ _ccs_configuration_space_serialize(
char **buffer,
_ccs_object_serialize_options_t *opts)
{
ccs_result_t err = CCS_RESULT_SUCCESS;
CCS_OBJ_RDLOCK(object);
switch (format) {
case CCS_SERIALIZE_FORMAT_BINARY:
CCS_VALIDATE_ERR_GOTO(
err,
_ccs_serialize_bin_ccs_configuration_space(
(ccs_configuration_space_t)object, buffer_size,
buffer, opts),
end);
CCS_VALIDATE(_ccs_serialize_bin_ccs_configuration_space(
(ccs_configuration_space_t)object, buffer_size, buffer,
opts));
break;
default:
CCS_RAISE_ERR_GOTO(
err, CCS_RESULT_ERROR_INVALID_VALUE, end,
CCS_RAISE(
CCS_RESULT_ERROR_INVALID_VALUE,
"Unsupported serialization format: %d", format);
}
CCS_VALIDATE_ERR_GOTO(
err,
_ccs_object_serialize_user_data(
object, format, buffer_size, buffer, opts),
end);
end:
CCS_OBJ_UNLOCK(object);
return err;
CCS_VALIDATE(_ccs_object_serialize_user_data(
object, format, buffer_size, buffer, opts));
return CCS_RESULT_SUCCESS;
}

static _ccs_configuration_space_ops_t _configuration_space_ops = {
Expand Down Expand Up @@ -522,6 +501,7 @@ ccs_create_configuration_space(
ccs_expression_t *conditions,
size_t num_forbidden_clauses,
ccs_expression_t *forbidden_clauses,
ccs_rng_t rng,
ccs_configuration_space_t *configuration_space_ret)
{
CCS_CHECK_PTR(name);
Expand All @@ -538,6 +518,8 @@ ccs_create_configuration_space(
CCS_CHECK_ARY(num_forbidden_clauses, forbidden_clauses);
for (size_t i = 0; i < num_forbidden_clauses; i++)
CCS_CHECK_OBJ(forbidden_clauses[i], CCS_OBJECT_TYPE_EXPRESSION);
if (rng)
CCS_CHECK_OBJ(rng, CCS_OBJECT_TYPE_RNG);

ccs_result_t err;
uintptr_t mem = (uintptr_t)calloc(
Expand All @@ -553,8 +535,10 @@ ccs_create_configuration_space(
strlen(name) + 1);
CCS_REFUTE(!mem, CCS_RESULT_ERROR_OUT_OF_MEMORY);
uintptr_t mem_orig = mem;
ccs_rng_t rng;
CCS_VALIDATE_ERR_GOTO(err, ccs_create_rng(&rng), errmem);
if (rng)
CCS_VALIDATE_ERR_GOTO(err, ccs_retain_object(rng), errmem);
else
CCS_VALIDATE_ERR_GOTO(err, ccs_create_rng(&rng), errmem);

ccs_configuration_space_t config_space;
config_space = (ccs_configuration_space_t)mem;
Expand Down Expand Up @@ -621,32 +605,14 @@ ccs_create_configuration_space(
#undef utarray_oom
#define utarray_oom() exit(-1)

ccs_result_t
ccs_configuration_space_set_rng(
ccs_configuration_space_t configuration_space,
ccs_rng_t rng)
{
CCS_CHECK_OBJ(configuration_space, CCS_OBJECT_TYPE_CONFIGURATION_SPACE);
CCS_CHECK_OBJ(rng, CCS_OBJECT_TYPE_RNG);
CCS_VALIDATE(ccs_retain_object(rng));
CCS_OBJ_WRLOCK(configuration_space);
ccs_rng_t tmp = configuration_space->data->rng;
configuration_space->data->rng = rng;
CCS_OBJ_UNLOCK(configuration_space);
CCS_VALIDATE(ccs_release_object(tmp));
return CCS_RESULT_SUCCESS;
}

ccs_result_t
ccs_configuration_space_get_rng(
ccs_configuration_space_t configuration_space,
ccs_rng_t *rng_ret)
{
CCS_CHECK_OBJ(configuration_space, CCS_OBJECT_TYPE_CONFIGURATION_SPACE);
CCS_CHECK_PTR(rng_ret);
CCS_OBJ_RDLOCK(configuration_space);
*rng_ret = configuration_space->data->rng;
CCS_OBJ_UNLOCK(configuration_space);
return CCS_RESULT_SUCCESS;
}

Expand Down Expand Up @@ -838,7 +804,7 @@ static inline ccs_result_t
_ccs_configuration_space_samples(
ccs_configuration_space_t configuration_space,
ccs_distribution_space_t distrib_space,
ccs_rng_t r,
ccs_rng_t rng,
size_t num_configurations,
ccs_configuration_t *configurations)
{
Expand All @@ -848,7 +814,6 @@ _ccs_configuration_space_samples(
ccs_bool_t found;
ccs_configuration_t config = NULL;
ccs_distribution_space_t distribution_space;
ccs_rng_t rng;

if (distrib_space) {
distribution_space = distrib_space;
Expand All @@ -857,11 +822,8 @@ _ccs_configuration_space_samples(
distribution_space =
configuration_space->data->default_distribution_space;

if (!r) {
CCS_OBJ_RDLOCK(configuration_space);
if (!rng)
rng = configuration_space->data->rng;
} else
rng = r;

for (size_t i = 0; i < num_configurations; i++)
configurations[i] = NULL;
Expand Down Expand Up @@ -895,8 +857,6 @@ _ccs_configuration_space_samples(
end:
if (distrib_space)
CCS_OBJ_UNLOCK(distrib_space);
if (!r)
CCS_OBJ_UNLOCK(configuration_space);
return err;
}

Expand Down
8 changes: 2 additions & 6 deletions src/configuration_space_deserialize.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,8 @@ _ccs_deserialize_bin_configuration_space(
ccs_create_configuration_space(
data.name, data.num_parameters, data.parameters,
data.conditions, data.num_forbidden_clauses,
data.forbidden_clauses, configuration_space_ret),
end);
CCS_VALIDATE_ERR_GOTO(
res,
ccs_configuration_space_set_rng(
*configuration_space_ret, data.rng),
data.forbidden_clauses, data.rng,
configuration_space_ret),
end);
if (opts && opts->map_values && opts->handle_map)
CCS_VALIDATE_ERR_GOTO(
Expand Down
4 changes: 2 additions & 2 deletions tests/test_condition.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ test_simple(void)
assert(err == CCS_RESULT_SUCCESS);

err = ccs_create_configuration_space(
"space", 2, parameters, conditions, 0, NULL, &space);
"space", 2, parameters, conditions, 0, NULL, NULL, &space);
assert(err == CCS_RESULT_SUCCESS);

for (int i = 0; i < 100; i++) {
Expand Down Expand Up @@ -123,7 +123,7 @@ test_transitive(void)
assert(err == CCS_RESULT_SUCCESS);

err = ccs_create_configuration_space(
"space", 3, parameters, conditions, 0, NULL, &space);
"space", 3, parameters, conditions, 0, NULL, NULL, &space);
assert(err == CCS_RESULT_SUCCESS);

err = ccs_release_object(conditions[0]);
Expand Down
Loading

0 comments on commit 7750a5b

Please sign in to comment.