From 31358ff2c9c8e20fdaea1de01834b561acc054cf Mon Sep 17 00:00:00 2001 From: tomoron Date: Sat, 22 Feb 2025 23:48:21 +0100 Subject: [PATCH] clusterizer show server information in imgui. server can dispatch waiting jobs and some fixes --- Makefile | 1 + includes/RT/Clusterizer.hpp | 37 +++++++++--- includes/RT/Window.hpp | 5 +- srcs/RT.cpp | 4 +- srcs/class/Clusterizer/client.cpp | 7 +-- srcs/class/Clusterizer/imgui.cpp | 58 +++++++++++++++++++ srcs/class/Clusterizer/server.cpp | 93 ++++++++++++++++++++++++------- srcs/class/Window.cpp | 5 +- 8 files changed, 171 insertions(+), 39 deletions(-) create mode 100644 srcs/class/Clusterizer/imgui.cpp diff --git a/Makefile b/Makefile index 61ccb7f..c0dbd68 100644 --- a/Makefile +++ b/Makefile @@ -65,6 +65,7 @@ ALL_SRCS := $(IMGUI_SRCS) gl.cpp \ class/Clusterizer/Clusterizer.cpp\ class/Clusterizer/client.cpp \ class/Clusterizer/server.cpp \ + class/Clusterizer/imgui.cpp \ SRCS := $(ALL_SRCS:%=$(SRCS_DIR)/%) OBJS := $(addprefix $(OBJS_DIR)/, $(SRCS:%.cpp=%.o)) diff --git a/includes/RT/Clusterizer.hpp b/includes/RT/Clusterizer.hpp index 2c9e9ac..9279be3 100644 --- a/includes/RT/Clusterizer.hpp +++ b/includes/RT/Clusterizer.hpp @@ -6,7 +6,7 @@ /* By: tomoron +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/02/20 18:25:18 by tomoron #+# #+# */ -/* Updated: 2025/02/22 01:43:51 by tomoron ### ########.fr */ +/* Updated: 2025/02/22 23:36:23 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,19 +15,28 @@ # include "RT.hpp" +typedef enum e_job_status +{ + WAITING, + IN_PROGRESS, + DONE +} t_job_status; + typedef struct s_job { std::string scene; glm::vec3 pos; glm::vec2 dir; size_t samples; - size_t id; + + size_t id; } t_job; typedef struct s_client { std::vector buffer; t_job *curJob; + int progress; bool ready; } t_client; @@ -49,21 +58,25 @@ class Clusterizer Clusterizer(Arguments &args); ~Clusterizer(); - void update(void); - bool getError(void); + void update(void); + bool getError(void); + void imguiRender(void); private: bool _isActive; bool _isServer; bool _error; - std::vector _jobs; + std::vector _jobs[3]; + + void imguiJobStat(void); + void imguiClients(void); private: //client void initClient(std::string &dest); void openClientConnection(const char *ip, int port); void clientHandleBuffer(void); void updateClient(void); - void clientGetJob(std::vector &sendBuf); + void clientGetJob(void); void clientReceive(void); int _serverFd; @@ -73,20 +86,26 @@ class Clusterizer t_job _currentJob; private: //server - void initServer(std::string port); - void updateServer(void); + void initServer(std::string port); + void updateServer(void); void initServerSocket(int port); - void acceptClients(void); + int acceptClients(void); void updatePollfds(void); int updateBuffer(int fd); void handleBuffer(int fd, std::vector &buf); + void addJob(std::string scene, glm::vec3 pos, glm::vec2 dir, size_t samples); void deleteClient(int fd); + + int dispatchJobs(void); + + int _serverSocket; struct pollfd *_pollfds; std::map _clients; + size_t _curId; }; #endif diff --git a/includes/RT/Window.hpp b/includes/RT/Window.hpp index 47a47b3..a00e17b 100644 --- a/includes/RT/Window.hpp +++ b/includes/RT/Window.hpp @@ -6,7 +6,7 @@ /* By: ycontre +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/13 16:15:41 by TheRed #+# #+# */ -/* Updated: 2025/02/15 22:54:27 by tomoron ### ########.fr */ +/* Updated: 2025/02/22 22:11:34 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,6 +17,7 @@ class Scene; class ShaderProgram; +class Clusterizer; class Window { @@ -36,7 +37,7 @@ class Window static void mouseButtonCallback(GLFWwindow *window, int button, int action, int mods); void imGuiNewFrame(); - void imGuiRender(ShaderProgram &raytracing_program); + void imGuiRender(ShaderProgram &raytracing_program, Clusterizer &clusterizer); GLFWwindow *getWindow(void) const; float getFps(void) const; diff --git a/srcs/RT.cpp b/srcs/RT.cpp index f2f7d15..9cd9bb8 100644 --- a/srcs/RT.cpp +++ b/srcs/RT.cpp @@ -6,7 +6,7 @@ /* By: ycontre +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/09/27 14:51:49 by TheRed #+# #+# */ -/* Updated: 2025/02/21 18:17:33 by tomoron ### ########.fr */ +/* Updated: 2025/02/22 22:09:46 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ @@ -103,7 +103,7 @@ int main(int argc, char **argv) render_program.use(); drawScreenTriangle(VAO, textures[0], render_program.getProgram()); - window.imGuiRender(raytracing_program); + window.imGuiRender(raytracing_program, clusterizer); window.display(); window.pollEvents(); diff --git a/srcs/class/Clusterizer/client.cpp b/srcs/class/Clusterizer/client.cpp index f0d97c9..609e84f 100644 --- a/srcs/class/Clusterizer/client.cpp +++ b/srcs/class/Clusterizer/client.cpp @@ -6,7 +6,7 @@ /* By: tomoron +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/02/20 21:08:38 by tomoron #+# #+# */ -/* Updated: 2025/02/22 02:02:32 by tomoron ### ########.fr */ +/* Updated: 2025/02/22 22:50:10 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ @@ -60,14 +60,13 @@ void Clusterizer::openClientConnection(const char *ip, int port) (void)write(_serverFd, &sendBuffer, 1); } -void Clusterizer::clientGetJob(std::vector &sendBuf) +void Clusterizer::clientGetJob(void) { if(_receiveBuffer.size() < sizeof(t_job) + 1) return ; _currentJob = *(t_job *)(_receiveBuffer.data() + 1); _receiveBuffer.erase(_receiveBuffer.begin(), _receiveBuffer.begin() + sizeof(t_job) + 1); - sendBuf.push_back(ACK); } void Clusterizer::clientHandleBuffer(void) @@ -75,7 +74,7 @@ void Clusterizer::clientHandleBuffer(void) std::vector sendBuf; if(_receiveBuffer[0] == JOB) - clientGetJob(sendBuf); + clientGetJob(); if(sendBuf.size()) (void)write(1, sendBuf.data(), sendBuf.size()); diff --git a/srcs/class/Clusterizer/imgui.cpp b/srcs/class/Clusterizer/imgui.cpp new file mode 100644 index 0000000..79f5210 --- /dev/null +++ b/srcs/class/Clusterizer/imgui.cpp @@ -0,0 +1,58 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* imgui.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tomoron +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/02/22 19:52:51 by tomoron #+# #+# */ +/* Updated: 2025/02/22 23:39:46 by tomoron ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "RT.hpp" + +void Clusterizer::imguiJobStat(void) +{ + ImGui::Text("jobs : "); + ImGui::Text(" waiting : %lu", _jobs[WAITING].size()); + ImGui::Text(" in progress : %lu", _jobs[IN_PROGRESS].size()); + ImGui::Text(" done : %lu", _jobs[DONE].size()); +} + +void Clusterizer::imguiClients(void) +{ + std::string status; + + ImGui::Text("clients : "); + ImGui::BeginChild("clientList", ImVec2(0, 0), true, 0); + for(auto it = _clients.begin();it != _clients.end(); it++) + { + if(!it->second.ready) + status = "not ready"; + else if(it->second.ready && it->second.curJob) + status = "working"; + else if(it->second.ready) + status = "idle"; + + ImGui::Text("status : %s", status.c_str()); + if(it->second.curJob) + ImGui::ProgressBar((float)it->second.progress / 100); + if(std::next(it) != _clients.end()) + ImGui::Separator(); + } + ImGui::EndChild(); +} + +void Clusterizer::imguiRender(void) +{ + if(!_isServer) + return ; + + if (ImGui::CollapsingHeader("Clusterizer")) + { + imguiJobStat(); + ImGui::Separator(); + imguiClients(); + } +} diff --git a/srcs/class/Clusterizer/server.cpp b/srcs/class/Clusterizer/server.cpp index 0ce5e45..3ee4270 100644 --- a/srcs/class/Clusterizer/server.cpp +++ b/srcs/class/Clusterizer/server.cpp @@ -6,7 +6,7 @@ /* By: tomoron +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/02/20 21:08:38 by tomoron #+# #+# */ -/* Updated: 2025/02/21 22:19:20 by tomoron ### ########.fr */ +/* Updated: 2025/02/22 23:36:42 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,6 +15,7 @@ void Clusterizer::initServer(std::string port) { _pollfds = 0; + _curId = 0; try { @@ -56,23 +57,24 @@ void Clusterizer::updatePollfds(void) _pollfds = new struct pollfd[_clients.size()]; for(auto it = _clients.begin(); it != _clients.end(); it++) { - _pollfds[std::distance(it, _clients.begin())].fd = it->first; - _pollfds[std::distance(it, _clients.begin())].events = POLLIN; - _pollfds[std::distance(it, _clients.begin())].revents = 0; + _pollfds[std::distance(_clients.begin(), it)].fd = it->first; + _pollfds[std::distance(_clients.begin(), it)].events = POLLIN; + _pollfds[std::distance(_clients.begin(), it)].revents = 0; } } -void Clusterizer::acceptClients(void) +int Clusterizer::acceptClients(void) { int fd; fd = accept(_serverSocket, 0, 0); - if (fd != -1) { - std::cout << "new client :" << fd << std::endl; - _clients[fd].ready = 0; - _clients[fd].curJob = 0; - updatePollfds(); - } + if (fd == -1) + return(0); + std::cout << "new client :" << fd << std::endl; + _clients[fd].ready = 0; + _clients[fd].curJob = 0; + updatePollfds(); + return(1); } void Clusterizer::deleteClient(int fd) @@ -95,6 +97,7 @@ void Clusterizer::handleBuffer(int fd, std::vector &buf) if(buf[0] == RDY) { _clients[fd].ready = 1; + std::cout << "client " << fd << " is ready !" << std::endl; sendBuffer.push_back(ACK); } @@ -108,27 +111,77 @@ int Clusterizer::updateBuffer(int fd) size_t ret; ret = recv(fd, buffer, 512, 0); - if(!ret) + if(!ret || ret == (size_t)-1) return(1); _clients[fd].buffer.insert(_clients[fd].buffer.end(), buffer, buffer + ret); handleBuffer(fd, _clients[fd].buffer); return(0); } +int Clusterizer::dispatchJobs(void) +{ + t_job *tmp; + int dispatched; + + dispatched = 0; + if(!_jobs[WAITING].size()) + return (0); + + for(auto it = _clients.begin(); it != _clients.end(); it++) + { + if(it->second.ready) + { + tmp = _jobs[WAITING].front(); + _jobs[WAITING].erase(_jobs[WAITING].begin()); + _jobs[IN_PROGRESS].push_back(tmp); + (void)write(it->first, &tmp, sizeof(t_job)); + it->second.ready = 0; + it->second.progress = 0; + it->second.curJob = tmp; + dispatched = 1; + } + } + return (dispatched); +} + +void Clusterizer::addJob(std::string scene, glm::vec3 pos, glm::vec2 dir, size_t samples) +{ + t_job *tmp; + + tmp = new t_job; + tmp->scene = scene; + tmp->pos = pos; + tmp->dir = dir; + tmp->samples = samples; + tmp->id = _curId++; + _jobs[WAITING].push_back(tmp); +} + void Clusterizer::updateServer(void) { int recv; + int didSomething; - acceptClients(); - recv = poll(_pollfds, _clients.size(), 1); - if(!recv) - return ; - for(auto it = _clients.begin(); it != _clients.end(); it++) + didSomething = 1; + while(didSomething) { - if(_pollfds[std::distance(it, _clients.begin())].revents & POLLIN) + didSomething = acceptClients(); + recv = poll(_pollfds, _clients.size(), 1); + if(!recv) + return ; + for(auto it = _clients.begin(); it != _clients.end(); it++) { - if (updateBuffer(it->first)) - deleteClient(it->first); + if(_pollfds[std::distance(_clients.begin(), it)].revents & POLLIN) + { + didSomething = 1; + if (updateBuffer(it->first)) + { + deleteClient(it->first); + break; + } + } } + if(dispatchJobs()) + didSomething = 1; } } diff --git a/srcs/class/Window.cpp b/srcs/class/Window.cpp index 0ef2952..4b0e0d8 100644 --- a/srcs/class/Window.cpp +++ b/srcs/class/Window.cpp @@ -6,7 +6,7 @@ /* By: ycontre +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/13 16:16:24 by TheRed #+# #+# */ -/* Updated: 2025/02/17 21:40:43 by tomoron ### ########.fr */ +/* Updated: 2025/02/22 22:15:32 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ @@ -191,7 +191,7 @@ void Window::imGuiNewFrame() ImGui::NewFrame(); } -void Window::imGuiRender(ShaderProgram &raytracing_program) +void Window::imGuiRender(ShaderProgram &raytracing_program, Clusterizer &clusterizer) { bool has_changed = false; @@ -318,6 +318,7 @@ void Window::imGuiRender(ShaderProgram &raytracing_program) _renderer->renderImgui(); + clusterizer.imguiRender(); ImGui::End();