summaryrefslogtreecommitdiff
path: root/mthread.cpp
blob: 796055e850fed52a606779da04838efd9bf39903 (plain)
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
#include "mthread.hpp"
#include <iostream>
#include <complex>
#include <unistd.h>
#include <thread>
#include <chrono>
#include <cmath>
#include <algorithm>
#include <atomic>
using namespace std;

mthread::mthread(
    unsigned int x_mn, unsigned int x_mx, complex<double> c_min, complex<double> c_max, 
    unsigned int inf_cutoff, unsigned int max_iter, png& image, double *g_vmap, unsigned int *g_histogram, 
    mthread **worker_list, unsigned int id, unsigned int jobs, atomic<uint32_t>& progress) 
  : x_min_orig(x_mn), x_max_orig(x_mx), 
  c_min(c_min), c_max(c_max), 
  inf_cutoff(inf_cutoff), max_iter(max_iter), image(image), id(id), worker_cnt(jobs), progress(progress){

  workers = worker_list;
  x_min = x_mn;
  x_max = x_mx;
  y_min = 0;
  y_max = image.height();
  vmap = g_vmap;
  histogram = g_histogram;
  step = (c_max - c_min) / complex<double>(image.height(), image.width());
  my_thread = NULL;
}

void mthread::dispatch() {
  if((my_thread) && (my_thread->joinable())) delete my_thread;
  my_thread = new thread([this] {render();}); 
}


mthread::~mthread() {
  if((my_thread) && (my_thread->joinable())) {
    my_thread->join();
    delete my_thread;
  }
}


void mthread::join() {
  if((my_thread) && (my_thread->joinable())) my_thread->join();
}


void mthread::render() {
  uint32_t image_width = image.width();
  unsigned int iter;
  unsigned int worker, workers_finished;
  uint32_t loads[worker_cnt];
  double pixel_value;
  complex<double> c, a;
  struct mthread_status *peer_status;
  struct mthread_divinfo divinfo;

  unique_lock<mutex> ack;
  unique_lock<mutex> syn_ack;


  status.status_lock.lock();
  status.searching = false;
  status.share_finished = false;
  status.div_syn = false;
  status.div_error = false;
  status.status_lock.unlock();


  y_min = 0;
  y_max = image.height();



  for(;;) {
    //thread is actively rendering
    for(on_y = y_min; on_y < y_max; on_y++) {
      progress++;
      status.status_lock.lock();
      status.row_load = y_max - on_y;
      if(status.div_syn) {

        status.div_error = status.row_load <= min_lines;

        if(status.div_error) {
          status.ack_lk.unlock();
          status.msg_notify.notify_all();
        }
        else {
          syn_ack = unique_lock<mutex>(status.syn_ack_lk);
          status.ack_lk.unlock();
          status.msg_notify.notify_all();
          status.msg_notify.wait(syn_ack);
          status.row_load = y_max - on_y;
          syn_ack.unlock();
          //new x/y min/max is ajusted by other thread, we can continue as normal.
        }
      }
      status.status_lock.unlock(); 

      for(on_x = x_min; on_x < x_max; on_x++) {
      c = (step * complex<double>(on_x,on_y)) + c_min;
      a = 0;
        for(iter = 0; iter < max_iter; iter++) {
          if(abs(a) >= inf_cutoff) break;
          a = a*a + c;
        }
        if(iter >= max_iter) {
          iter = 0;
          vmap[(on_y * image_width) + on_x] = 0;
        }
        else {
          pixel_value = (iter + 1) - (log((log(pow(abs(a), 2.0)) / 2.0) / log(2.0))); 
          vmap[(on_y * image_width) + on_x] = pixel_value;
          histogram[(int)pixel_value]++;
        }
      }
    }


    //thread is now searching for work
    
    /** 2022 comment:
     * this state should have been moved to a seperate function to allow rendering methods to differ
     * from inherited mthreads without needing to reimpliment searching **/
    status.status_lock.lock(); 
    status.searching = true;
    status.share_finished = true;
    status.status_lock.unlock();

    while(status.searching) {
      workers_finished = 0;
      for(worker = 0; worker < worker_cnt; worker++) {
        peer_status = &workers[worker]->status; 
        peer_status->status_lock.lock();

        if((peer_status->share_finished) && (worker != id)) {
          workers_finished++;
        }
        if((worker == id) || 
            (peer_status->searching) || (peer_status->div_syn) || (peer_status->row_load < min_lines)) {
          loads[worker] = 0;
          peer_status->status_lock.unlock();
          continue;
        }
        loads[worker] = peer_status->row_load;
        peer_status->status_lock.unlock();
      }
      if(workers_finished >= worker_cnt - 1) {
        return;
      }
      for(;;) {
        worker = distance(loads, max_element(loads, &loads[worker_cnt]));
        if(!loads[worker]) break;
        peer_status = &workers[worker]->status;
        peer_status->status_lock.lock();
        if((peer_status->searching) || (peer_status->div_syn) || (peer_status->row_load < min_lines)) {
          loads[worker] = 0;
          peer_status->status_lock.unlock();
          continue;
        }
        ack = unique_lock<mutex>(peer_status->ack_lk);
        peer_status->div_syn = true;
        peer_status->status_lock.unlock();
        peer_status->msg_notify.wait(ack);
        ack.unlock();
        if(peer_status->div_error) {
          loads[worker] = 0;
          peer_status->status_lock.lock();
          peer_status->div_error = false;
          peer_status->div_syn = false;
          peer_status->status_lock.unlock();
          continue;
        }

        divinfo = workers[worker]->divide();
        peer_status->syn_ack_lk.unlock();
        peer_status->msg_notify.notify_all();
        y_min = divinfo.y_min;
        y_max = divinfo.y_max;
        x_min = divinfo.x_min;
        x_max = divinfo.x_max;
        status.status_lock.lock();
        status.searching = false;
        status.status_lock.unlock();
        break;
      }
    }
  }
}

struct mthread_divinfo mthread::divide() {
  struct mthread_divinfo ret;
  ret.x_min = x_min;
  ret.x_max = x_max;
  ret.y_min = ((y_max - on_y) / 2) + on_y;
  ret.y_max = y_max;
  y_min = on_y;
  y_max = ret.y_min;
  status.div_syn = false;
  return ret;
}