clusterizer show server information in imgui. server can dispatch waiting jobs and some fixes

This commit is contained in:
2025-02-22 23:48:21 +01:00
parent 05ba0447d2
commit 31358ff2c9
8 changed files with 171 additions and 39 deletions

View File

@ -6,7 +6,7 @@
/* By: ycontre <ycontre@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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();

View File

@ -6,7 +6,7 @@
/* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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<uint8_t> &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<uint8_t> sendBuf;
if(_receiveBuffer[0] == JOB)
clientGetJob(sendBuf);
clientGetJob();
if(sendBuf.size())
(void)write(1, sendBuf.data(), sendBuf.size());

View File

@ -0,0 +1,58 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* imgui.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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();
}
}

View File

@ -6,7 +6,7 @@
/* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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<uint8_t> &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;
}
}

View File

@ -6,7 +6,7 @@
/* By: ycontre <ycontre@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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();