implemented working multithreading

This commit is contained in:
Akos Horvath 2022-07-05 23:22:01 +02:00
parent 22892c3d70
commit c79412bdab
3 changed files with 93 additions and 16 deletions

View File

@ -110,8 +110,8 @@ void Graphics::plot(Mandelbrotc const &m)
// } // }
// } // }
for (uint32_t i = 0; i < m.screen.size(); i+=2) { for (uint32_t i = 0; i < m.s.x; i++) {
for (uint32_t j = 0; j < m.screen[0].size(); j+=2) { for (uint32_t j = 0; j < m.s.y; j++) {
uint8_t color = 255 - (m.screen[i][j]*255/m.max_iter); uint8_t color = 255 - (m.screen[i][j]*255/m.max_iter);
SDL_SetRenderDrawColor(this->renderer, color, color, color, 255); SDL_SetRenderDrawColor(this->renderer, color, color, color, 255);
SDL_RenderDrawPoint(renderer, (int)i, (int)j); SDL_RenderDrawPoint(renderer, (int)i, (int)j);
@ -128,10 +128,13 @@ void Graphics::mainLoop()
this->from = Vec2mp(fx, fy); this->from = Vec2mp(fx, fy);
this->to = Vec2mp(tx, ty); this->to = Vec2mp(tx, ty);
bool mouseDown = false;
Vec2i m_start = Vec2i(0, 0);
Vec2i m_end = Vec2i(0, 0);
Mandelbrotc m = Mandelbrotc(from, to, Vec2i(this->w, this->h), 100); Mandelbrotc m = Mandelbrotc(from, to, Vec2i(this->w, this->h), 100);
m.thread_count = 5; m.thread_count = 16;
m.start_threads(); m.start_threads(true);
while (this->running) while (this->running)
{ {
@ -160,21 +163,67 @@ void Graphics::mainLoop()
this->to.y += 0.2; this->to.y += 0.2;
} }
break; break;
case SDL_MOUSEBUTTONDOWN:
if (mouseDown)
break;
mouseDown = true;
m_start.x = event.button.x;
m_start.y = event.button.y;
break;
case SDL_MOUSEBUTTONUP:
mouseDown = false;
break;
case SDL_MOUSEMOTION: case SDL_MOUSEMOTION:
m_end.x = event.motion.x;
m_end.y = event.motion.y;
break; break;
} }
} }
std::cout << "mainloop range " << this->from.x << std::endl; if (mouseDown) {
std::cout << "mainloop range " << this->from.y << std::endl; SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
std::cout << "mainloop range " << this->to.x << std::endl; if (m_start.x < m_end.x) {
std::cout << "mainloop range " << this->to.y << std::endl; for (int i = m_start.x; i < m_end.x; i++) {
//this->plot(this->from, this->to); SDL_RenderDrawPoint(renderer, i, m_start.y);
SDL_RenderDrawPoint(renderer, i, m_end.y);
}
} else {
for (int i = m_end.x; i < m_start.x; i++) {
SDL_RenderDrawPoint(renderer, i, m_start.y);
SDL_RenderDrawPoint(renderer, i, m_end.y);
}
}
if (m_start.y < m_end.y) {
for (int i = m_start.y; i < m_end.y; i++) {
SDL_RenderDrawPoint(renderer, m_start.x, i);
SDL_RenderDrawPoint(renderer, m_end.x, i);
}
} else {
for (int i = m_end.y; i < m_start.y; i++) {
SDL_RenderDrawPoint(renderer, m_start.x, i);
SDL_RenderDrawPoint(renderer, m_end.x, i);
}
}
}
std::cout << "done" << std::endl; // std::cout << "mainloop range " << this->from.x << std::endl;
// std::cout << "mainloop range " << this->from.y << std::endl;
// std::cout << "mainloop range " << this->to.x << std::endl;
// std::cout << "mainloop range " << this->to.y << std::endl;
m.start_threads(false);
this->plot(m);
m.stop_threads();
// std::cout << "done" << std::endl;
SDL_RenderPresent(renderer); SDL_RenderPresent(renderer);
SDL_SetRenderDrawColor(this->renderer, 255, 255, 255, 255); SDL_SetRenderDrawColor(this->renderer, 255, 255, 255, 255);
SDL_RenderClear(renderer);
SDL_Delay(100); SDL_Delay(100);
if (m.done)
exit(1);
} }
m.stop_threads();
} }

View File

@ -9,26 +9,52 @@ Mandelbrotc::Mandelbrotc(Vec2mp const &f, Vec2mp const &t, Vec2i const &s, uint3
: f(f), t(t), s(s), max_iter(mi) : f(f), t(t), s(s), max_iter(mi)
{ {
this->threads = std::vector<std::thread>(); this->threads = std::vector<std::thread>();
this->screen = std::vector<std::vector<uint8_t>>(); //this->screen = std::vector<std::vector<uint8_t>>();
this->screen = new uint8_t*[s.x];
for(int i = 0; i < s.x; ++i)
this->screen[i] = new uint8_t[s.y];
this->done = false;
} }
void Mandelbrotc::start_threads() void Mandelbrotc::start_threads(bool first)
{ {
if (!this->done && !first)
return;
Mandelbrotc const &m = *this;
for (int i = 0; i < this->thread_count; i++) {
std::thread t = std::thread(&Mandelbrotc::calc, this, i);
this->threads.push_back(std::move(t));
std::cout << "thread created" << std::endl;
}
this->done = false;
} }
void Mandelbrotc::stop_threads()
{
for (int i = 0; i < this->thread_count; i++) {
while (!this->threads[i].joinable())
this->threads[i].join();
}
}
void Mandelbrotc::calc(const uint8_t tid) void Mandelbrotc::calc(const uint8_t tid)
{ {
mpreal x, y; mpreal x, y;
std::vector<uint8_t> v = std::vector<uint8_t>();
for(double i = tid; i < this->s.x; i+=this->thread_count) { for(double i = tid; i < this->s.x; i+=this->thread_count) {
for(double j = 0; j < this->s.y; j++) { for(double j = 0; j < this->s.y; j++) {
x = (mpfr::abs(this->f.x - this->t.x) * (i / this->s.x)) + this->f.x; x = (mpfr::abs(this->f.x - this->t.x) * (i / this->s.x)) + this->f.x;
y = (mpfr::abs(this->f.y - this->t.y) * (j / this->s.y)) + this->f.y; y = (mpfr::abs(this->f.y - this->t.y) * (j / this->s.y)) + this->f.y;
this->screen[i][j] = mandelbrot(Vec2mp(x, y)); this->screen[(int)i][(int)j] = this->mandelbrot(Vec2mp(x, y));
} }
} }
std::cout << "thread " << tid << " done" << std::endl;
if (tid == thread_count-1)
this->done = true;
} }
uint32_t Mandelbrotc::mandelbrot(const Vec2mp &n) uint32_t Mandelbrotc::mandelbrot(const Vec2mp &n)

View File

@ -15,11 +15,13 @@ public:
uint8_t thread_count; uint8_t thread_count;
std::vector<std::thread> threads; std::vector<std::thread> threads;
Vec2i s; Vec2i s;
std::vector<std::vector<uint8_t>> screen; //std::vector<std::vector<uint8_t>> screen;
uint8_t **screen;
bool done;
uint32_t max_iter; uint32_t max_iter;
Mandelbrotc(Vec2mp const &f, Vec2mp const &t, Vec2i const &s, uint32_t mi); Mandelbrotc(Vec2mp const &f, Vec2mp const &t, Vec2i const &s, uint32_t mi);
void start_threads(); void start_threads(bool first);
void stop_threads(); void stop_threads();
uint32_t mandelbrot(const Vec2mp &n); uint32_t mandelbrot(const Vec2mp &n);
void calc(const uint8_t tid); void calc(const uint8_t tid);