Skip to content
Snippets Groups Projects
Commit 320ca2d7 authored by Thomas Purcell's avatar Thomas Purcell
Browse files

OMP works for classification as well

parent f2cf266a
Branches
No related tags found
No related merge requests found
......@@ -123,10 +123,10 @@ std::array<double, 2> SISSOClassifier::svm_error(std::vector<SVMWrapper>& svm, s
int SISSOClassifier::get_max_error_ind(int n_models, int* n_convex_overlap, double* svm_score, double* svm_margin, double* scores)
{
std::transform(n_convex_overlap, n_convex_overlap, svm_score, scores, [this](int n_overlap, double score){return static_cast<double>(n_overlap * _n_samp) + score;});
double max_dist = *std::max_element(svm_margin, svm_margin) + 0.01;
std::transform(svm_margin, svm_margin, scores, scores, [&max_dist](double margin, double score){return score + (1.0 - margin / max_dist);});
return std::max_element(scores, scores) - scores;
std::transform(n_convex_overlap, n_convex_overlap + n_models, svm_score, scores, [this](int n_overlap, double score){return static_cast<double>(n_overlap * _n_samp) + score;});
double max_dist = *std::max_element(svm_margin, svm_margin + n_models) + 0.01;
std::transform(svm_margin, svm_margin + n_models, scores, scores, [&max_dist](double margin, double score){return score + (1.0 - margin / max_dist);});
return std::max_element(scores, scores + n_models) - scores;
}
std::vector<std::vector<int>> SISSOClassifier::find_min_svm(std::vector<std::vector<int>>& test_inds, int n_keep)
......@@ -216,7 +216,7 @@ void SISSOClassifier::l0_norm(std::vector<double>& prop, int n_dim)
if(inds.back() >= 0)
{
// #pragma omp parallel firstprivate(_svm, _lp, max_error_ind)
#pragma omp parallel firstprivate(_svm, _lp, max_error_ind)
{
std::vector<int> min_inds_private(min_inds);
std::vector<int> min_n_convex_overlap_private(min_n_convex_overlap);
......@@ -227,14 +227,19 @@ void SISSOClassifier::l0_norm(std::vector<double>& prop, int n_dim)
std::vector<double> scores(n_get_models);
int ii_prev = 0;
// #pragma omp for schedule(dynamic)
#pragma omp for schedule(dynamic) firstprivate(inds)
for(int ii = 0; ii < n_interactions; ii += _mpi_comm->size())
{
util_funcs::iterate(inds, inds.size(), _mpi_comm->size() * (ii - ii_prev));
int n_convex_overlap = 0;
for(auto& lp : _lp)
{
n_convex_overlap += lp.get_n_overlap(inds);
}
if(n_convex_overlap <= min_n_convex_overlap_private[max_error_ind])
{
......@@ -253,8 +258,7 @@ void SISSOClassifier::l0_norm(std::vector<double>& prop, int n_dim)
ii_prev = ii;
}
// #pragma omp barrier
// #pragma omp critical
#pragma omp critical
{
max_error_ind = get_max_error_ind(n_get_models, min_n_convex_overlap.data(), min_svm_score.data(), min_svm_margin.data(), scores.data());
for(int ee = 0; ee < min_n_convex_overlap.size(); ++ee)
......@@ -272,6 +276,8 @@ void SISSOClassifier::l0_norm(std::vector<double>& prop, int n_dim)
}
}
}
std::vector<int> all_min_n_convex_overlap(_mpi_comm->size() * n_get_models);
std::vector<double> all_min_svm_score(_mpi_comm->size() * n_get_models);
std::vector<double> all_min_svm_margin(_mpi_comm->size() * n_get_models);
......@@ -291,6 +297,7 @@ void SISSOClassifier::l0_norm(std::vector<double>& prop, int n_dim)
{
while((ii < all_min_n_convex_overlap.size()) && (all_min_n_convex_overlap[inds[ii]] == all_min_n_convex_overlap[inds[nn]]))
++ii;
if(ii - nn == 1)
{
nn = ii;
......@@ -304,7 +311,9 @@ void SISSOClassifier::l0_norm(std::vector<double>& prop, int n_dim)
test_inds = find_min_svm(test_inds, std::min(ii - nn, n_get_models - nn));
for(int cc = 0; cc < test_inds.size(); ++cc)
{
std::copy_n(test_inds[cc].begin(), n_dim, &all_min_inds[(cc + nn) * n_dim]);
}
nn = ii;
++ii;
}
......@@ -312,7 +321,6 @@ void SISSOClassifier::l0_norm(std::vector<double>& prop, int n_dim)
std::vector<model_node_ptr> min_nodes(n_dim);
std::vector<ModelClassifier> models;
// #pragma omp parallel for
for(int rr = 0; rr < n_get_models; ++rr)
{
node_value_arrs::clear_temp_test_reg();
......@@ -322,11 +330,9 @@ void SISSOClassifier::l0_norm(std::vector<double>& prop, int n_dim)
min_nodes[ii] = std::make_shared<ModelNode>(_feat_space->phi_selected()[index]->arr_ind(), _feat_space->phi_selected()[index]->rung(), _feat_space->phi_selected()[index]->expr(), _feat_space->phi_selected()[index]->postfix_expr(), _feat_space->phi_selected()[index]->value(), _feat_space->phi_selected()[index]->test_value(), _feat_space->phi_selected()[index]->unit());
}
#pragma omp critical
models.push_back(ModelClassifier(_prop_unit, _prop, _prop_test, min_nodes, _task_sizes_train, _task_sizes_test, _fix_intercept));
}
_models.push_back(models);
}
......
......
......@@ -599,6 +599,7 @@ void FeatureSpace::sis(std::vector<double>& prop)
++ii;
std::vector<double> scores_prev_sel;
if(node_value_arrs::N_SELECTED > _n_sis_select)
{
scores_prev_sel.resize(_phi_selected.size());
......@@ -727,6 +728,7 @@ void FeatureSpace::sis(std::vector<double>& prop)
if(cur_feat != node_value_arrs::N_SELECTED)
throw std::logic_error("SIS went through all features and did not select enough.");
if(_mpi_comm->rank() == 0)
{
out_file_stream << "#";
......
......
......@@ -50,23 +50,17 @@ void project_funcs::project_classify(double* prop, double* scores, std::vector<n
convex_hull::initialize_projection(sizes, prop);
#pragma omp parallel firstprivate(prop)
{
int start = omp_get_thread_num() * (phi.size() / omp_get_num_threads()) + std::min(omp_get_thread_num(), static_cast<int>(phi.size()) % omp_get_num_threads());
int end = (omp_get_thread_num() + 1) * (phi.size() / omp_get_num_threads()) + std::min(omp_get_thread_num() + 1, static_cast<int>(phi.size()) % omp_get_num_threads());
std::transform(phi.begin() + start, phi.begin() + end, scores + start, [](node_ptr feat){return convex_hull::overlap_1d(feat->value_ptr());});
std::transform(phi.begin(), phi.end(), scores, [](node_ptr feat){return convex_hull::overlap_1d(feat->value_ptr());});
for(int pp = 1; pp < n_prop; ++pp)
{
prop += n_samp;
convex_hull::initialize_projection(sizes, prop);
std::transform(phi.begin() + start, phi.begin() + end, scores + start, scores + start, [](node_ptr feat, double score){return std::min(convex_hull::overlap_1d(feat->value_ptr()), score);});
}
std::transform(scores + start, scores + end, scores + start, [](double score){return std::isnan(score) ? std::numeric_limits<double>::infinity() : score;});
#pragma omp barrier
std::transform(phi.begin(), phi.end(), scores, scores, [](node_ptr feat, double score){return std::min(convex_hull::overlap_1d(feat->value_ptr()), score);});
}
std::transform(scores, scores + phi.size(), scores, [](double score){return std::isnan(score) ? std::numeric_limits<double>::infinity() : score;});
}
void project_funcs::project_r_no_omp(double* prop, double* scores, std::vector<node_ptr>& phi, std::vector<int>& sizes, int n_prop)
{
int n_samp = std::accumulate(sizes.begin(), sizes.end(), 0);
......
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment