-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage_generation.cpp
102 lines (77 loc) · 2.31 KB
/
image_generation.cpp
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
#include "image_generation.hpp"
#include "configurations.h"
#include "mlp.hpp"
#include <cuda.h>
#include <builtin_types.h>
#include "mainwindow.h"
#include "perlinnoise.h"
#include <QElapsedTimer>
// Forward declare the function in the .cu file
void mlp_run_kernel(uint8_t* res, float* w, int n);
void init_kernel(int n);
void end_kernel();
ImageGeneration::ImageGeneration()
{
draw_samples = false;
N = WIDTH * HEIGHT;
int w_count_for_model = 0;
for (int i = 1; i < layer_sizes_size; i++)
w_count_for_model += (layer_sizes[i - 1] + 1) * layer_sizes[i];
#if BW
for(int i = 0; i < mlp_count; i++)
res[i] = new uint8_t[N];
#else
for(int i = 0; i < mlp_count; i++)
res[i] = new uint8_t[N * 4];
#endif
for(int i = 0; i < mlp_count; i++)
w[i] = new float[w_count_for_model];
init_kernel(N);
}
ImageGeneration::~ImageGeneration(){
end_kernel();
}
QElapsedTimer qet;
qint64 tm0 = 0, tm1 = 0, tm2 = 0, tm3 = 0, tm4 = 0, tm5 = 0;
void ImageGeneration::genarate_image(){
mw->other_mutex.lock();
for(int i = 0; i < mlp_count; i++)
mlp_iters[i]->can_it_run = false;
for(int i = 0; i < mlp_count; i++)
while (mlp_iters[i]->is_it_running) ;
for(int i = 0; i < mlp_count; i++)
mlps[i]->fill_w_array(w[i]);
for(int i = 0; i < mlp_count; i++)
mlp_iters[i]->can_it_run = true;
for(int i = 0; i < mlp_count; i++)
while (!mlp_iters[i]->is_it_running) ;
mw->other_mutex.unlock();
tm1 = qet.nsecsElapsed();
for(int i = 0; i < mlp_count; i++)
mlp_run_kernel(res[i], w[i], N);
tm2 = qet.nsecsElapsed();
tm3 = qet.nsecsElapsed();
tm4 = qet.nsecsElapsed();
}
void ImageGeneration::image_gen_loop(){
cout << "image_gen_loop START" << endl;
qet.start();
while(true){
qet.restart();
tm0 = qet.nsecsElapsed();
genarate_image();
mut.lock();
for(int i = 0; i < mlp_count; i++)
#if BW
memcpy(si->res[i], res[i], N);
#else
memcpy(si->res[i], res[i], N * 4);
#endif
mut.unlock();
emit image_ready();
tm5 = qet.nsecsElapsed();
//ofstream off("w.txt");
//mlp->print_w(off);
/// cout << tm0 << " - " << (tm1 - tm0) << " - " << (tm2 - tm1) << " - " << (tm3 - tm2) << " - " << (tm4 - tm3) << " - " << (tm5 - tm4) << endl;
}
}