Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Return an object, not just a matrix, from each job #178

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/Algorithms/AlgorithmOptions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,17 @@ typedef struct AlgorithmOptions_t {

} AlgorithmOptions_t;

inline bool operator==(const AlgorithmOptions_t& a1, const AlgorithmOptions_t& a2) {
try {
for (auto const& entry : a2.options) {
if (strcmp(entry.second.c_str(), a2.options.at(entry.first).c_str())) {
return false;
}
}
} catch (const exception& e) {
return false;
}
return (a1.type == a2.type);
}

#endif // ALGORITHM_OPTIONS_HPP
2 changes: 2 additions & 0 deletions src/Algorithms/ProximalGradientDescent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,8 @@ void ProximalGradientDescent::run(LinearRegression *model) {
model->setL1_reg(model->getL1_reg()*10);
long s = y.cols();
for (long i=0; i<s; i++){
mtx.unlock();
mtx.lock();
if (shouldStop) {
break;
}
Expand Down
14 changes: 14 additions & 0 deletions src/Models/ModelOptions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,18 @@ typedef struct ModelOptions_t {

} ModelOptions_t;


inline bool operator==(const ModelOptions_t& m1, const ModelOptions_t& m2) {
try {
for (auto const& entry : m2.options) {
if (strcmp(entry.second.c_str(), m2.options.at(entry.first).c_str())) {
return false;
}
}
} catch (const exception& e) {
return false;
}
return (m1.type == m2.type);
}

#endif // MODEL_OPTIONS_HPP
4 changes: 2 additions & 2 deletions src/Models/Model_Tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,8 @@ TEST(MODEL_BASE, Prediction){
0.3525;
MatrixXf p = m.predict();
TEST_MATRIX_NEAR(p, y, 1e-3);
p = m.predict(X);
TEST_MATRIX_NEAR(p, y, 1e-3);
MatrixXf q = m.predict(X);
TEST_MATRIX_NEAR(q, y, 1e-3);
}

TEST(LINEAR_REGRESSION, CostFunction){
Expand Down
12 changes: 12 additions & 0 deletions src/Scheduler/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ cc_library(
"//Models:ModelOptions",
"//Models:TreeLasso",
"//Scheduler:Job",
"//Scheduler:JobResult",
"//Stats:FisherTest",
"//Stats:Chi2Test",
"//Stats:WaldTest"]
Expand All @@ -45,6 +46,17 @@ cc_library(
)


cc_library(
name = "JobResult",
hdrs = ["JobResult.hpp"],
copts = ["-DBAZEL"],
linkopts = [],
deps = ["//Algorithms:AlgorithmOptions",
"//Models:ModelOptions",
"//Scheduler:Job"]
)


cc_test(
name = "Scheduler_Tests",
srcs = ["Scheduler_Tests.cpp"],
Expand Down
37 changes: 27 additions & 10 deletions src/Scheduler/Job.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,6 @@ using namespace v8;

typedef unsigned int job_id_t; // job_id = 0 indicates error.

typedef struct Job_t {
Algorithm* algorithm;
exception_ptr exception;
job_id_t job_id;
Model* model;
thread::id thread_id;
Persistent<Function> callback;
uv_work_t request;
} Job_t;

typedef struct JobOptions_t {
AlgorithmOptions_t alg_opts;
ModelOptions_t model_opts;
Expand All @@ -54,6 +44,33 @@ typedef struct JobOptions_t {
: alg_opts(alg)
, model_opts(model){};

JobOptions_t(){};

} JobOptions_t;

typedef struct Job_t {
Algorithm* algorithm; // owned by Algorithm's algorithm_map
algorithm_id_t algorithm_id;
exception_ptr exception;
job_id_t job_id;
JobOptions_t options;
Model* model; // owned by Scheduler's models_map
model_id_t model_id;
mutex* mtx; // owned by Scheduler's jobs_mutex_map
thread::id thread_id;
Persistent<Function> callback;
uv_work_t* request; // Owned by this job

Job_t() {
algorithm = NULL;
model = NULL;
request = new uv_work_t();
};

~Job_t() {
delete request;
}

} Job_t;

#endif // JOB_HPP
34 changes: 34 additions & 0 deletions src/Scheduler/JobResult.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#ifndef JOBRESULT_HPP
#define JOBRESULT_HPP

#include <exception>
#include <Eigen/Dense>

#ifdef BAZEL
#include "Algorithms/AlgorithmOptions.hpp"
#include "Models/ModelOptions.hpp"
#include "Scheduler/Job.hpp"
#else
#include "../Algorithms/AlgorithmOptions.hpp"
#include "../Models/ModelOptions.hpp"
#include "Job.hpp"
#endif

using namespace Eigen;
using namespace std;

typedef struct JobResult_t {
exception_ptr exception;
JobOptions_t options;
job_id_t job_id;
MatrixXf beta;
string description;

JobResult_t() {
beta = MatrixXf();
description = "";
};

} JobResult_t;

#endif // JOBRESULT_HPP
Loading