File size: 8,545 Bytes
35aaa09 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 |
/**
* @file boinc_wrapper.cpp
* @brief Implementation of BOINC integration wrapper
*
* @authors OpenPeer AI, Riemann Computing Inc., Bleunomics, Andrew Magdy Kamal
*/
#include "boinc_wrapper.h"
#include <iostream>
#include <fstream>
#include <sstream>
#include <chrono>
#include <ctime>
#include <random>
#include <iomanip>
namespace docking_at_home {
namespace boinc {
// BOINCWrapper Implementation
BOINCWrapper::BOINCWrapper()
: is_initialized_(false), app_name_("DockingAtHOME"), worker_id_("") {
}
BOINCWrapper::~BOINCWrapper() {
if (is_initialized_) {
finalize();
}
}
bool BOINCWrapper::initialize() {
int retval = boinc_init();
if (retval) {
std::cerr << "BOINC initialization failed: " << boincerror(retval) << std::endl;
return false;
}
// Generate unique worker ID
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<> dis(100000, 999999);
worker_id_ = "WORKER_" + std::to_string(dis(gen));
is_initialized_ = true;
std::cout << "BOINC initialized successfully. Worker ID: " << worker_id_ << std::endl;
return true;
}
bool BOINCWrapper::register_application(const std::string& app_name, const std::string& version) {
if (!is_initialized_) {
std::cerr << "BOINC not initialized" << std::endl;
return false;
}
app_name_ = app_name;
std::cout << "Registered application: " << app_name << " v" << version << std::endl;
return true;
}
std::string BOINCWrapper::submit_task(const DockingTask& task) {
if (!is_initialized_) {
std::cerr << "BOINC not initialized" << std::endl;
return "";
}
if (!validate_task(task)) {
std::cerr << "Invalid task" << std::endl;
return "";
}
std::string task_id = generate_task_id();
// Upload input files
if (!upload_input_files(task)) {
std::cerr << "Failed to upload input files" << std::endl;
return "";
}
std::cout << "Task submitted successfully: " << task_id << std::endl;
return task_id;
}
bool BOINCWrapper::process_task(const DockingTask& task, DockingResult& result) {
if (!is_initialized_) {
std::cerr << "BOINC not initialized" << std::endl;
return false;
}
auto start_time = std::chrono::high_resolution_clock::now();
std::cout << "Processing task: " << task.task_id << std::endl;
std::cout << "Ligand: " << task.ligand_file << std::endl;
std::cout << "Receptor: " << task.receptor_file << std::endl;
std::cout << "GPU enabled: " << (task.use_gpu ? "Yes" : "No") << std::endl;
// Initialize result
result.task_id = task.task_id;
result.worker_id = worker_id_;
result.successful_runs = 0;
// Simulate docking process (actual AutoDock integration would go here)
for (int i = 0; i < task.num_runs; ++i) {
// Update progress
double progress = static_cast<double>(i) / task.num_runs;
update_progress(progress);
// Checkpoint every 10 runs
if (i > 0 && i % 10 == 0) {
std::string checkpoint_file = task.output_dir + "/checkpoint_" + std::to_string(i);
create_checkpoint(checkpoint_file);
}
// Simulate computation
boinc_sleep(0.1);
result.successful_runs++;
}
// Calculate computation time
auto end_time = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> elapsed = end_time - start_time;
result.computation_time = elapsed.count();
// Finalize progress
update_progress(1.0);
report_cpu_time(result.computation_time);
std::cout << "Task completed: " << task.task_id << std::endl;
std::cout << "Computation time: " << result.computation_time << " seconds" << std::endl;
return true;
}
double BOINCWrapper::get_task_progress(const std::string& task_id) {
// Query BOINC for task progress
// This would interface with BOINC database or API
return 0.0;
}
bool BOINCWrapper::get_task_results(const std::string& task_id, DockingResult& result) {
if (!is_initialized_) {
return false;
}
// Download and parse result files
return download_output_files(task_id);
}
void BOINCWrapper::update_progress(double fraction_done) {
boinc_fraction_done(fraction_done);
}
void BOINCWrapper::report_cpu_time(double cpu_time) {
boinc_report_app_status(cpu_time, 0, 0);
}
bool BOINCWrapper::create_checkpoint(const std::string& checkpoint_file) {
std::ofstream ckpt(checkpoint_file, std::ios::binary);
if (!ckpt.is_open()) {
return false;
}
// Write checkpoint data
// In a real implementation, this would save the current state
ckpt << "CHECKPOINT_V1" << std::endl;
ckpt << worker_id_ << std::endl;
ckpt.close();
boinc_checkpoint_completed();
return true;
}
bool BOINCWrapper::restore_checkpoint(const std::string& checkpoint_file) {
std::ifstream ckpt(checkpoint_file, std::ios::binary);
if (!ckpt.is_open()) {
return false;
}
// Read and restore checkpoint data
std::string version, worker;
std::getline(ckpt, version);
std::getline(ckpt, worker);
ckpt.close();
return true;
}
void BOINCWrapper::finalize() {
if (is_initialized_) {
boinc_finish(0);
is_initialized_ = false;
}
}
bool BOINCWrapper::validate_task(const DockingTask& task) {
if (task.ligand_file.empty() || task.receptor_file.empty()) {
return false;
}
if (task.num_runs <= 0 || task.num_evals <= 0) {
return false;
}
return true;
}
std::string BOINCWrapper::generate_task_id() {
auto now = std::chrono::system_clock::now();
auto now_time_t = std::chrono::system_clock::to_time_t(now);
auto now_ms = std::chrono::duration_cast<std::chrono::milliseconds>(
now.time_since_epoch()) % 1000;
std::stringstream ss;
ss << "TASK_"
<< std::put_time(std::localtime(&now_time_t), "%Y%m%d_%H%M%S")
<< "_" << now_ms.count();
return ss.str();
}
bool BOINCWrapper::upload_input_files(const DockingTask& task) {
// Use BOINC file transfer API
// In production, this would handle actual file uploads
std::cout << "Uploading input files for task: " << task.task_id << std::endl;
return true;
}
bool BOINCWrapper::download_output_files(const std::string& task_id) {
// Use BOINC file transfer API
// In production, this would handle actual file downloads
std::cout << "Downloading output files for task: " << task_id << std::endl;
return true;
}
// BOINCServer Implementation
BOINCServer::BOINCServer()
: is_initialized_(false), active_work_units_(0), completed_work_units_(0) {
}
BOINCServer::~BOINCServer() {
}
bool BOINCServer::initialize(const std::string& config_file) {
std::cout << "Initializing BOINC server with config: " << config_file << std::endl;
// Load configuration
// Parse database connection details
db_host_ = "localhost";
db_name_ = "docking_at_home";
is_initialized_ = true;
return true;
}
int BOINCServer::create_work_units(const std::vector<DockingTask>& tasks) {
if (!is_initialized_) {
return 0;
}
int created = 0;
for (const auto& task : tasks) {
// Create BOINC work unit
// This would use BOINC's work unit creation API
std::cout << "Creating work unit for task: " << task.task_id << std::endl;
created++;
active_work_units_++;
}
return created;
}
bool BOINCServer::process_result(const std::string& result_file) {
if (!is_initialized_) {
return false;
}
std::cout << "Processing result file: " << result_file << std::endl;
// Validate and store results
active_work_units_--;
completed_work_units_++;
return true;
}
std::string BOINCServer::get_statistics() {
std::stringstream ss;
ss << "{"
<< "\"active_work_units\": " << active_work_units_ << ", "
<< "\"completed_work_units\": " << completed_work_units_
<< "}";
return ss.str();
}
} // namespace boinc
} // namespace docking_at_home
|