Skip to content

Commit

Permalink
Avoid allocating unused memory
Browse files Browse the repository at this point in the history
  • Loading branch information
jacquev6 committed Jun 12, 2024
1 parent 5af54ec commit 3abae5f
Show file tree
Hide file tree
Showing 12 changed files with 93 additions and 30 deletions.
7 changes: 6 additions & 1 deletion doc-sources/reference/lincs.rst
Original file line number Diff line number Diff line change
Expand Up @@ -865,10 +865,15 @@

Indexed by ``[model_index][boundary_index][criterion_index]``. The current rank of each low profile, for each model and criterion.

.. property:: high_profile_rank_indexes
:type: list[unsigned]

Indexed by ``[criterion_index]``. The index in ``high_profile_ranks``, for each single-peaked criterion.

.. property:: high_profile_ranks
:type: list[list[list[int]]]

Indexed by ``[model_index][boundary_index][criterion_index]``. The current rank of each high profile, for each model and criterion.
Indexed by ``[model_index][boundary_index][high_profile_rank_indexes[criterion_index]]``. The current rank of each high profile, for each model and single-peaked criterion.

.. property:: accuracies
:type: list[int]
Expand Down
2 changes: 2 additions & 0 deletions doc-sources/reference/lincs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,8 @@ children:
type: list[list[int]]
- name: low_profile_ranks
type: list[list[list[int]]]
- name: high_profile_rank_indexes
type: list[unsigned]
- name: high_profile_ranks
type: list[list[list[int]]]
- name: accuracies
Expand Down
1 change: 1 addition & 0 deletions integration-tests/dir-all/liblincs.txt
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ liblincs: module
criteria_count: property
get_best_accuracy: instancemethod
get_best_model: instancemethod
high_profile_rank_indexes: property
high_profile_ranks: property
iteration_index: property
low_profile_ranks: property
Expand Down
1 change: 1 addition & 0 deletions integration-tests/dir-all/lincs.txt
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ lincs: module
criteria_count: property
get_best_accuracy: instancemethod
get_best_model: instancemethod
high_profile_rank_indexes: property
high_profile_ranks: property
iteration_index: property
low_profile_ranks: property
Expand Down
26 changes: 23 additions & 3 deletions lincs/liblincs/learning/mrsort-by-weights-profiles-breed.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ LearnMrsortByWeightsProfilesBreed::LearningData::LearningData(
model_indexes(models_count),
accuracies(models_count, zeroed),
low_profile_ranks(models_count, boundaries_count, criteria_count, uninitialized),
high_profile_ranks(models_count, boundaries_count, criteria_count, uninitialized),
high_profile_rank_indexes(criteria_count, uninitialized),
high_profile_ranks(models_count, boundaries_count, count_single_peaked_criteria(), uninitialized),
weights(models_count, criteria_count, uninitialized)
{
CHRONE();
Expand All @@ -36,6 +37,25 @@ LearnMrsortByWeightsProfilesBreed::LearningData::LearningData(
for (unsigned model_index = 0; model_index != models_count; ++model_index) {
random_generators[model_index].seed(random_seed * (model_index + 1));
}

unsigned count = 0;
for (unsigned criterion_index = 0; criterion_index != criteria_count; ++criterion_index) {
if (single_peaked[criterion_index]) {
high_profile_rank_indexes[criterion_index] = count;
++count;
}
}
assert(high_profile_ranks.s0() == count);
}

unsigned LearnMrsortByWeightsProfilesBreed::LearningData::count_single_peaked_criteria() const {
unsigned count = 0;
for (unsigned criterion_index = 0; criterion_index != criteria_count; ++criterion_index) {
if (single_peaked[criterion_index]) {
++count;
}
}
return count;
}

Model LearnMrsortByWeightsProfilesBreed::LearningData::get_model(const unsigned model_index) const {
Expand All @@ -57,8 +77,8 @@ Model LearnMrsortByWeightsProfilesBreed::LearningData::get_model(const unsigned
boundary_profile.reserve(criteria_count);
for (unsigned criterion_index = 0; criterion_index != criteria_count; ++criterion_index) {
const unsigned low_profile_rank = low_profile_ranks[model_index][boundary_index][criterion_index];
const unsigned high_profile_rank = high_profile_ranks[model_index][boundary_index][criterion_index];
if (single_peaked[criterion_index]) {
const unsigned high_profile_rank = high_profile_ranks[model_index][boundary_index][high_profile_rank_indexes[criterion_index]];
boundary_profile.push_back(std::make_pair(low_profile_rank, high_profile_rank));
} else {
boundary_profile.push_back(low_profile_rank);
Expand Down Expand Up @@ -176,7 +196,7 @@ bool LearnMrsortByWeightsProfilesBreed::is_accepted(
const unsigned alternative_rank = learning_data.performance_ranks[criterion_index][alternative_index];
const unsigned low_profile_rank = learning_data.low_profile_ranks[model_index][boundary_index][criterion_index];
if (learning_data.single_peaked[criterion_index]) {
const unsigned high_profile_rank = learning_data.high_profile_ranks[model_index][boundary_index][criterion_index];
const unsigned high_profile_rank = learning_data.high_profile_ranks[model_index][boundary_index][learning_data.high_profile_rank_indexes[criterion_index]];
return low_profile_rank <= alternative_rank && alternative_rank <= high_profile_rank;
} else {
return low_profile_rank <= alternative_rank;
Expand Down
6 changes: 5 additions & 1 deletion lincs/liblincs/learning/mrsort-by-weights-profiles-breed.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ struct LearnMrsortByWeightsProfilesBreed::LearningData : public PreProcessedLear
std::vector<unsigned> model_indexes; // [model_index_index]: this is a reordering of the models' indexes
Array1D<Host, unsigned> accuracies; // [model_index]
Array3D<Host, unsigned> low_profile_ranks; // [model_index][boundary_index][criterion_index]
Array3D<Host, unsigned> high_profile_ranks; // [model_index][boundary_index][criterion_index]
Array1D<Host, unsigned> high_profile_rank_indexes; // [criterion_index], meaningful only for single-peaked criteria (i.e. when single_peaked[criterion_index] is true)
Array3D<Host, unsigned> high_profile_ranks; // [model_index][boundary_index][high_profile_rank_indexes[criterion_index]]
Array2D<Host, float> weights; // [model_index][criterion_index]
// @todo(Performance, later) Add models' ages

Expand All @@ -86,6 +87,9 @@ struct LearnMrsortByWeightsProfilesBreed::LearningData : public PreProcessedLear
bool model_is_correct(unsigned model_index) const;
bool models_are_correct() const;
#endif

private:
unsigned count_single_peaked_criteria() const;
};

struct LearnMrsortByWeightsProfilesBreed::ProfilesInitializationStrategy {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ void ImproveProfilesWithAccuracyHeuristicOnCpu::improve_low_profile_then_high_pr
const unsigned boundary_index,
const unsigned criterion_index
) {
assert(learning_data.single_peaked[criterion_index]);

improve_low_profile(
model_index,
boundary_index,
Expand All @@ -65,7 +67,7 @@ void ImproveProfilesWithAccuracyHeuristicOnCpu::improve_low_profile_then_high_pr
0 :
learning_data.low_profile_ranks[model_index][boundary_index - 1][criterion_index],
boundary_index == learning_data.boundaries_count - 1 ?
learning_data.high_profile_ranks[model_index][boundary_index][criterion_index]:
learning_data.high_profile_ranks[model_index][boundary_index][learning_data.high_profile_rank_indexes[criterion_index]]:
learning_data.low_profile_ranks[model_index][boundary_index + 1][criterion_index]
);

Expand All @@ -75,10 +77,10 @@ void ImproveProfilesWithAccuracyHeuristicOnCpu::improve_low_profile_then_high_pr
criterion_index,
boundary_index == learning_data.boundaries_count - 1 ?
learning_data.low_profile_ranks[model_index][boundary_index][criterion_index] :
learning_data.high_profile_ranks[model_index][boundary_index + 1][criterion_index],
learning_data.high_profile_ranks[model_index][boundary_index + 1][learning_data.high_profile_rank_indexes[criterion_index]],
boundary_index == 0 ?
learning_data.values_counts[criterion_index] - 1 :
learning_data.high_profile_ranks[model_index][boundary_index - 1][criterion_index]
learning_data.high_profile_ranks[model_index][boundary_index - 1][learning_data.high_profile_rank_indexes[criterion_index]]
);
}

Expand Down Expand Up @@ -307,11 +309,12 @@ void ImproveProfilesWithAccuracyHeuristicOnCpu::improve_high_profile(
const unsigned lowest_destination_rank,
const unsigned highest_destination_rank
) {
assert(learning_data.single_peaked[criterion_index]);
assert(lowest_destination_rank <= highest_destination_rank);
if (lowest_destination_rank == highest_destination_rank) {
assert(learning_data.high_profile_ranks[model_index][boundary_index][criterion_index] == lowest_destination_rank);
assert(learning_data.high_profile_ranks[model_index][boundary_index][learning_data.high_profile_rank_indexes[criterion_index]] == lowest_destination_rank);
} else {
unsigned best_destination_rank = learning_data.high_profile_ranks[model_index][boundary_index][criterion_index];
unsigned best_destination_rank = learning_data.high_profile_ranks[model_index][boundary_index][learning_data.high_profile_rank_indexes[criterion_index]];
float best_desirability = Desirability().value();

if (highest_destination_rank - lowest_destination_rank >= max_destinations_count) {
Expand Down Expand Up @@ -342,7 +345,7 @@ void ImproveProfilesWithAccuracyHeuristicOnCpu::improve_high_profile(
}

if (std::uniform_real_distribution<float>(0, 1)(learning_data.random_generators[model_index]) <= best_desirability) {
learning_data.high_profile_ranks[model_index][boundary_index][criterion_index] = best_destination_rank;
learning_data.high_profile_ranks[model_index][boundary_index][learning_data.high_profile_rank_indexes[criterion_index]] = best_destination_rank;
}
}
}
Expand Down Expand Up @@ -376,7 +379,8 @@ void ImproveProfilesWithAccuracyHeuristicOnCpu::update_move_desirability_for_hig
const unsigned alternative_index,
Desirability* desirability
) {
const unsigned current_rank = learning_data.high_profile_ranks[model_index][boundary_index][criterion_index];
assert(learning_data.single_peaked[criterion_index]);
const unsigned current_rank = learning_data.high_profile_ranks[model_index][boundary_index][learning_data.high_profile_rank_indexes[criterion_index]];
const float weight = learning_data.weights[model_index][criterion_index];

const unsigned alternative_rank = learning_data.performance_ranks[criterion_index][alternative_index];
Expand Down
Loading

0 comments on commit 3abae5f

Please sign in to comment.