Skip to content

Commit

Permalink
WIP: Figuring out how to add custom deleter to unique_ptr
Browse files Browse the repository at this point in the history
  • Loading branch information
Schlevidon committed Aug 3, 2024
1 parent 9ee1822 commit 40b6b2f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 24 deletions.
50 changes: 26 additions & 24 deletions hpc/LoadBalancer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,26 +42,20 @@ bool waitForFile(const std::string &filename)
return true;
}

std::string readLineFromFile(const std::string &filename)
std::string readLineFromFile(const std::string& filename)
{
std::ifstream file(filename);
std::string line;
std::string line = "";

if (file.is_open())
{
std::string file_contents((std::istreambuf_iterator<char>(file)),
(std::istreambuf_iterator<char>()));
line = file_contents;
file.close();
std::getline(file, line);
}
else
{
std::cerr << "Unable to open file " << filename << " ." << std::endl;
std::cerr << "Unable to open file: " << filename << std::endl;
}

// delete the line break
if (!line.empty())
line.pop_back();

return line;
}

Expand All @@ -72,7 +66,7 @@ class JobManager
// The returned object MUST release any resources that it holds once it goes out of scope in the code of the caller.
// This can be achieved by returning a unique pointer with an appropriate deleter.
// This method may return a nullptr to deny a request.
virtual std::unique_ptr<umbridge::Model, void(*)(umbridge::Model*)> requestModelAccess(const std::string& model_name) = 0;
virtual std::unique_ptr<umbridge::Model, void(*)(void*)> requestModelAccess(const std::string& model_name) = 0;

// To initialize the load balancer we first need a list of model names that are available on a server.
// Typically, this can be achieved by simply running the model code and requesting the model names from the server.
Expand All @@ -99,39 +93,45 @@ class FileBasedModelDeleter
std::string cancelation_command;
std::string file_to_delete;
};
using unique_file_based_model_ptr = std::unique_ptr<umbridge::Model, FileBasedModelDeleter>;

class FileBasedJobManager : public JobManager
{
public:
virtual std::unique_ptr<umbridge::Model, void(*)(umbridge::Model*)> requestModelAccess(const std::string& model_name) override
virtual std::unique_ptr<umbridge::Model, void(*)(void*)> requestModelAccess(const std::string& model_name) override
{

std::string submission_command = getSubmissionCommand();
std::string job_id = submitJob(submission_command);
std::string server_url = readURL(job_id);
FileBasedModelDeleter deleter(getCancelationCommand(job_id), getURLFileName(job_id));
unique_file_based_model_ptr client(new umbridge::HTTPModel(server_url, model_name), deleter);
return client;
}
protected:
virtual std::string getSubmissionCommand() = 0;
virtual std::string getCancelationCommand(const std::string& job_id) = 0;

std::unique_ptr<umbridge::HTTPModel, FileBasedModelDeleter> setDeleter(std::unique_ptr<umbridge::HTTPModel> client)
{
FileBasedModelDeleter deleter("","");
return {client, deleter};

}
std::unique_ptr<umbridge::HTTPModel> submitJobAndStartClient(const std::string& model_name) {
std::string submission_command = getSubmissionCommand();
std::string job_id = submitJob(submission_command);
std::string server_url = readURL(job_id);
auto client = connectToServer(server_url, model_name);
return client;

}

std::unique_ptr<umbridge::HTTPModel> connectToServer(const std::string& server_url, const std::string& model_name)
{
return std::make_unique<umbridge::HTTPModel>(server_url, model_name);
}

std::string getURLFileName(const std::string& job_id)
{
return url_file_prefix + job_id + url_file_suffix;
}

std::string readURL(const std::string& job_id)
{
std::filesystem::path url_file(url_file_prefix + job_id + url_file_suffix);
return readLineFromFile(url_file.string());
return readLineFromFile(getURLFileName(job_id));
}

std::string submitJob(const std::string& command)
Expand Down Expand Up @@ -181,9 +181,11 @@ class FileBasedJobManager : public JobManager

const std::filesystem::path submission_script_dir;
const std::filesystem::path submission_script_default;
// Model-specifc job-script format: <prefix><model_name><suffix>
const std::string submission_script_model_specific_prefix;
const std::string submission_script_model_specific_suffix;

// URL file format: <prefix><job-id><suffix>
const std::filesystem::path url_dir;
const std::string url_file_prefix;
const std::string url_file_suffix;
Expand Down Expand Up @@ -314,7 +316,7 @@ class LoadBalancer : public umbridge::Model
{
public:
LoadBalancer(std::string name, std::shared_ptr<JobManager> job_manager)
: umbridge::Model(name), job_manager(std::move(job_manager)) {}
: umbridge::Model(name), job_manager(job_manager) {}

std::vector<std::size_t> GetInputSizes(const json &config_json = json::parse("{}")) const override
{
Expand Down
1 change: 1 addition & 0 deletions lib/umbridge.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ namespace umbridge {
class Model {
public:
Model(std::string name) : name(name) {}
virtual ~Model() {}

virtual std::vector<std::size_t> GetInputSizes(const json& config_json = json::parse("{}")) const = 0;
virtual std::vector<std::size_t> GetOutputSizes(const json& config_json = json::parse("{}")) const = 0;
Expand Down

0 comments on commit 40b6b2f

Please sign in to comment.