-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cc
209 lines (170 loc) · 5.04 KB
/
main.cc
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
205
206
207
208
209
/* Copyright 2014
* Afshin Haidari
* Steve Novakov
* Jeff Taylor
*/
#include <unistd.h>
#include <cassert>
#include <thread>
#include <chrono>
#include <cmath>
#include "fifo.h"
#include "oclenv.h"
#include "oclptxhandler.h"
#include "particlegen.h"
#include "samplemanager.h"
#include "threading.h"
cl_ulong8 rng_zero = {{0,}};
std::chrono::high_resolution_clock::time_point t_end;
std::chrono::high_resolution_clock::time_point t_start;
void start_timer()
{
t_start = std::chrono::high_resolution_clock::now();
}
void end_timer(const char *verb)
{
t_end = std::chrono::high_resolution_clock::now();
std::chrono::duration<float> delta_t = t_end - t_start;
printf("Time to %s: %fs\n", verb, delta_t.count());
}
int main(int argc, char **argv)
{
const int kStepsPerKernel = 1000;
const int kNumReducers = 1;
FILE *global_fd;
Fifo<struct OclPtxHandler::particle_data> *particles_fifo;
// Startup the samplemanager
puts("Loading samples...");
start_timer();
SampleManager sample_manager;
sample_manager.ParseCommandLine(argc, argv);
end_timer("load samples");
puts("Setting up OpenCL...");
start_timer();
// Create our oclenv
OclEnv env;
env.OclInit();
env.NewCLCommandQueues(
sample_manager.GetOclptxOptions().gpuselect.value());
const unsigned short int * rubbish_mask =
sample_manager.GetExclusionMaskToArray();
const unsigned short int * stop_mask =
sample_manager.GetTerminationMaskToArray();
std::vector<unsigned short int*>* waypoints =
sample_manager.GetWayMasksToVector();
env.AvailableGPUMem(
sample_manager.GetFDataPtr(),
sample_manager.GetOclptxOptions(),
waypoints->size(),
rubbish_mask,
stop_mask
);
env.CreateKernels("standard");
env.AllocateSamples(
sample_manager.GetFDataPtr(),
sample_manager.GetPhiDataPtr(),
sample_manager.GetThetaDataPtr(),
sample_manager.GetBrainMaskToArray(),
rubbish_mask,
stop_mask,
waypoints
);
global_fd = fopen("./path_output", "w");
if (NULL == global_fd)
{
perror("Couldn't open file");
exit(1);
}
cl_float4 dims = sample_manager.brain_mask_dim();
int min_steps = ceil(
sample_manager.GetOclptxOptions().distthresh.value()
* sample_manager.GetOclptxOptions().steplength.value()
/ dims.s[0]);
int fibst = sample_manager.GetOclptxOptions().fibst.value() - 1;
if (fibst > 1);
fibst = 1;
struct OclPtxHandler::particle_attrs attrs = {
sample_manager.brain_mask_dim(),
kStepsPerKernel,
sample_manager.GetOclptxOptions().nsteps.value(), // max_steps
min_steps,
0, // Particles per side not determined here.
env.GetEnvData()->nx,
env.GetEnvData()->ny,
env.GetEnvData()->nz,
env.GetEnvData()->ns, // num_samples
sample_manager.GetOclptxOptions().c_thr.value(), // curv threshold
env.GetEnvData()->n_waypts,
sample_manager.GetOclptxOptions().steplength.value(),
env.GetEnvData()->lx,
env.GetEnvData()->ly,
env.GetEnvData()->lz,
fibst,
sample_manager.GetOclptxOptions().randfib.value(),
sample_manager.GetOclptxOptions().fibthresh.value()
}; // num waymasks.
int num_dev = env.HowManyCQ();
// Create a new oclptxhandler.
OclPtxHandler *handler = new OclPtxHandler[num_dev];
std::thread *gpu_managers[num_dev];
int total_particles = 0;
for (int i = 0; i < num_dev; ++i)
{
handler[i].Init(env.GetContext(),
env.GetCq(i),
env.GetKernel(i),
env.GetSumKernel(i),
&attrs,
global_fd,
env.GetKernelWorkGroupInfo(i),
env.GetEnvData(),
env.GetDevicePdf(i));
total_particles += handler[i].particles_per_side();
}
ParticleGenerator particle_gen;
particles_fifo = particle_gen.Init(total_particles);
for (int i = 0; i < num_dev; ++i)
{
gpu_managers[i] = new std::thread(
threading::RunThreads,
&handler[i],
particles_fifo,
kNumReducers);
}
end_timer("set up OpenCL");
puts("Tracking...");
start_timer();
int64_t count = 0;
float percent;
float rate;
int64_t total = particle_gen.total_particles();
while (count < total)
{
count = particles_fifo->count();
percent = (100. * count) / total;
t_end = std::chrono::high_resolution_clock::now();
std::chrono::duration<float> track_time = (t_end - t_start);
rate = count / track_time.count();
// Internally, we count each particle twice (once per direction). The
// user doesn't expect that, so we correct it here by dividing by two.
printf("Processed %li/%li. [%2.2f%%] [%.f particles/sec]\r",
count / 2, total / 2, percent, rate / 2);
fflush(stdout);
usleep(100000); // .1s
}
printf("\n");
for (int i = 0; i < num_dev; ++i)
{
gpu_managers[i]->join();
}
end_timer("track");
puts("Writing to file...");
start_timer();
for (int i = 0; i < num_dev; ++i)
handler[i].RunSumKernel();
env.PdfsToFile("pdf_out");
end_timer("write to file");
delete[] handler;
fclose(global_fd);
return 0;
}