-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomputation_module.c
More file actions
148 lines (129 loc) · 4.01 KB
/
computation_module.c
File metadata and controls
148 lines (129 loc) · 4.01 KB
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
#include <unistd.h>
#include <stdio.h>
#include <stdbool.h>
#include <complex.h>
#include "computation_module.h"
#include "event_queue.h"
#include "messages.h"
#include "prg_io_nonblock.h"
#include "utils.h"
#include <sys/types.h>
comp_module_t comp_module = {
.c_re = 0,
.c_im = 0,
.n = 0,
.d_re = 0,
.d_im = 0,
.cid = 0,
.chunk_re = 0,
.chunk_im = 0,
.chunk_n_re = 0,
.chunk_n_im = 0,
.abort = false,
.pipe_out = -1
};
bool set_module_compute(message *msg, void *d) {
comp_module.c_re = msg->data.set_compute.c_re;
comp_module.c_im = msg->data.set_compute.c_im;
comp_module.d_re = msg->data.set_compute.d_re;
comp_module.d_im = msg->data.set_compute.d_im;
comp_module.n = msg->data.set_compute.n;
comp_module.pipe_out = *(int *)d;
return true;
}
void module_compute(message *msg_incoming) {
if (comp_module.abort){
message msg_to_send;
msg_to_send.type = MSG_ABORT;
send_message_to_pipe(&msg_to_send);
}
else {
comp_module.cid = msg_incoming->data.compute.cid;
comp_module.chunk_re = msg_incoming->data.compute.re;
comp_module.chunk_im = msg_incoming->data.compute.im;
comp_module.chunk_n_re = msg_incoming->data.compute.n_re;
comp_module.chunk_n_im = msg_incoming->data.compute.n_im;
for (u_int8_t i = 0; i < comp_module.chunk_n_re; i++)
{
for (u_int8_t j = 0; j < comp_module.chunk_n_im; j++)
{
double complex z = (i * comp_module.d_re + comp_module.chunk_re) + (j * comp_module.d_im + comp_module.chunk_im) * I;
double complex c = comp_module.c_re + comp_module.c_im * I;
//init calc
z = complex_module_pow(z) + c;
u_int8_t iteration = 1;
while (complex_module_abs(z) < 2 && iteration <= comp_module.n) {
z = complex_module_pow(z) + c;
if (iteration != comp_module.n)
{
iteration++;
} else {
break;
}
}
prepare_pixel_to_msg(i, j, iteration);
}
}
event ev = { .type = EV_COMPUTE_DATA_DONE};
queue_push(ev);
}
}
void abort_module_compute(void) {
comp_module.abort = true;
}
void enable_module_compute(void) {
comp_module.abort = false;
}
bool is_module_aborted(void) {
return comp_module.abort;
}
void prepare_pixel_to_msg(u_int8_t i, u_int8_t j, u_int8_t iteration){
message msg_to_send;
msg_to_send.type = MSG_COMPUTE_DATA;
msg_to_send.data.compute_data.cid = comp_module.cid;
msg_to_send.data.compute_data.i_re = i;
msg_to_send.data.compute_data.i_im = j;
msg_to_send.data.compute_data.iter = iteration;
send_message_to_pipe(&msg_to_send);
}
void send_message_to_pipe(message *msg) {
uint8_t msg_buf[sizeof(message)];
int msg_len;
my_assert(fill_message_buf(msg, msg_buf, sizeof(msg_buf), &msg_len), __func__, __LINE__, __FILE__);
if (!(write(comp_module.pipe_out, msg_buf, msg_len) == msg_len)) {
error("Sending message to incoming pipe failed\n");
}
}
double complex complex_module_pow(double complex z) {
double real = creal(z);
double imag = cimag(z);
double realPart = real * real - imag * imag;
double imagPart = real * imag * 2;
return realPart + imagPart * I;
}
double complex_module_abs(double complex z) {
double real = creal(z);
double imag = cimag(z);
return (my_module_sqrt(real * real + imag * imag));
}
double my_module_sqrt(double x) {
//special case for 0 and 1
if (x < 2){
return x;
}
double y = x;
double z = (y + (x / y)) / 2;
//continue iterating until the difference between y and z is less than 0.00001
while (my_module_abs(y - z) >= 0.00001) {
y = z;
z = (y + (x / y)) / 2;
}
return z;
}
double my_module_abs(double x) {
if (x < 0)
{
return x * -1;
}
return x;
}