-
Notifications
You must be signed in to change notification settings - Fork 0
/
cosmos_simulate.cpp
388 lines (234 loc) · 8.56 KB
/
cosmos_simulate.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
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
/*
### cosmos_simulate
To compile, use the command
```bash
clang++ cosmos_simulate.cpp -o cosmos_simulate -std=c++11 -lOpenCL
```
To run, use the command
```bash
./cosmos_simulate <frames> [bodies=24576]
```
This will cause cosmos_simulate to output a bunch of frames to the directory
that it was started in. These frames will be binary files, copied directly
from OpenCL output.
## Even more speed
To speed up simulations and renders even more, compile with
```bash
clang++ cosmos_tool.cpp -o cosmos_tool -std=c++11 -lOpenCL -Ofast -march=native
```
*/
#include <iostream>
#include <sstream>
#include <fstream>
#include <ctime>
#include <cmath>
#include <memory>
// Random value between 0.0f and 1.0f.
inline float rand_01()
{
return float(rand()) / float(RAND_MAX);
}
// Convert degrees to radians.
inline float degrad(float x)
{
return 2.0f * M_PI * (x / 360.0f);
}
// Include stb_image_write.
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb_image_write.h"
// Include OpenCL.
#ifdef __APPLE__
#define CL_SILENCE_DEPRECATION
#include <OpenCL/OpenCL.h>
#else
#include <CL/cl.h>
#endif
// Include the kernel source.
#define __stringify(source) #source
const char* kernel_source =
#include "cosmos_simulate.cl"
#undef __stringify
size_t kernel_source_size = strlen(kernel_source);
// Include the thermal colormap.
#include "thermal_colormap.h"
// Write a message to std::cout.
void say(std::string message)
{
std::cout << message << std::endl;
}
// Entry point.
int main(int argc, char** argv)
{
// Parse command line arguments.
if (argc != 2 && argc != 3)
{
std::cout << "Usage: " << argv[0] << " <frames> [bodies=24576]" << std::endl;
return EXIT_FAILURE;
}
int frames = atoi(argv[1]);
// Create variables to hold return codes.
cl_int r_code;
cl_int r_code1;
cl_int r_code2;
// Create identifier objects to hold information about the available
// platforms and available devices.
cl_platform_id platform_id = NULL;
cl_device_id device_id = NULL;
// Create unsigned integer objects to hold the amount of available
// platforms and available devices.
cl_uint num_platforms;
cl_uint num_devices;
// Get the first available platform and store the amount of available
// platforms.
clGetPlatformIDs(1, &platform_id, &num_platforms);
// Get the first available device on the first available platform. Store
// the amount of available devices. This device will be referred to as the
// 'default device'.
clGetDeviceIDs(platform_id, CL_DEVICE_TYPE_DEFAULT, 1, &device_id, &num_devices);
// Create an OpenCL context on the default device.
cl_context context = clCreateContext(0, 1, &device_id, NULL, NULL, &r_code);
// Make sure the OpenCL context was created successfully.
if (r_code != CL_SUCCESS)
{
say("Could not create an OpenCL context.");
return EXIT_FAILURE;
}
// Create an OpenCL command queue.
cl_command_queue command_queue = clCreateCommandQueue(context, device_id, 0, &r_code);
// Make sure the OpenCL command queue was created successfully.
if (r_code != CL_SUCCESS)
{
say("Could not create an OpenCL command queue.");
return EXIT_FAILURE;
}
// Allocate CPU memory for the n-body simulation.
// PARAM: The n-body count.
cl_int n = 1024 * 24;
if (argc == 3)
{
n = atoi(argv[2]);
}
cl_float4* state1 = (cl_float4*)malloc(n * sizeof(cl_float4));
cl_float4* state2 = (cl_float4*)malloc(n * sizeof(cl_float4));
// Make sure both arrays were allocated successfully.
if (!state1 || !state2)
{
say("Could not allocate local CPU memory.");
return EXIT_FAILURE;
}
// Initilize the n-body simulation.
// PARAM: The initial seed.
srand(time(NULL));
// PARAM: The initial spawn size.
const float xr = 16000.0f;
const float yr = 16000.0f;
for (int i = 0; i < n; i++)
{
// Generate a random body.
// PARAM: The body creation routine.
cl_float ang = rand_01() * 2.0f * M_PI;
cl_float rad = rand_01();
cl_float x = (xr * rad) * cos(ang);
cl_float y = (yr * rad) * sin(ang);
cl_float vx = cos(ang + degrad(90.0f)) * rad * 64.0f;
cl_float vy = sin(ang + degrad(90.0f)) * rad * 64.0f;
// Write the body to the first state.
state1[i] = {x, y, vx, vy};
}
// Clear the second state.
memset(state2, 0, n * sizeof(cl_float4));
// Allocate GPU memory for the n-body simulation.
cl_mem gpu_state1 = clCreateBuffer(context, CL_MEM_READ_WRITE, n * sizeof(cl_float4), NULL, &r_code1);
cl_mem gpu_state2 = clCreateBuffer(context, CL_MEM_READ_WRITE, n * sizeof(cl_float4), NULL, &r_code2);
// Make sure both arrays were allocated successfully.
if (r_code1 != CL_SUCCESS || r_code2 != CL_SUCCESS)
{
say("Could not allocate GPU memory.");
return EXIT_FAILURE;
}
// Copy the contents of the CPU n-body simulation memory to the GPU n-body
// simulation memory.
r_code1 = clEnqueueWriteBuffer(command_queue, gpu_state1, CL_TRUE, 0, n * sizeof(cl_float4), state1, 0, NULL, NULL);
r_code2 = clEnqueueWriteBuffer(command_queue, gpu_state2, CL_TRUE, 0, n * sizeof(cl_float4), state2, 0, NULL, NULL);
// Make sure both arrays were copied successfully.
if (r_code1 != CL_SUCCESS || r_code2 != CL_SUCCESS)
{
say("Could not copy CPU memory to GPU memory.");
return EXIT_FAILURE;
}
// Create an OpenCL program from the kernel source.
cl_program program = clCreateProgramWithSource(context, 1, (const char**)&kernel_source, (const size_t*)&kernel_source_size, &r_code);
// Make sure the OpenCL program was created successfully.
if (r_code != CL_SUCCESS)
{
say("Could not create an OpenCL program.");
return EXIT_FAILURE;
}
// Build the OpenCL program.
r_code = clBuildProgram(program, 1, &device_id, NULL, NULL, NULL);
// Make sure the OpenCL program was built successfully.
if (r_code != CL_SUCCESS)
{
say("Could not build an OpenCL program.");
return EXIT_FAILURE;
}
// Create the OpenCL kernel from the function "n_body_cl" within the
// OpenCL program.
cl_kernel kernel = clCreateKernel(program, "n_body_cl", &r_code);
// Make sure the OpenCL kernel was created successfully.
if (r_code != CL_SUCCESS)
{
say("Could not create an OpenCL kernel.");
return EXIT_FAILURE;
}
// Set the n-body count parameter of the kernel.
clSetKernelArg(kernel, 2, sizeof(cl_int), &n);
// Set the state parameters of the kernel.
clSetKernelArg(kernel, 3, sizeof(cl_mem), (void*)&gpu_state1);
clSetKernelArg(kernel, 4, sizeof(cl_mem), (void*)&gpu_state2);
// Get the simulation starting time.
time_t sim_start = time(NULL);
// Start the simulation!
for (int i = 0; i < frames; i++)
{
// Get the starting time.
clock_t begin = clock();
// Set the timestep and softening parameters.
cl_float gpu_float_args[2] = {float(i + 1), float(i + 1)};
// PARAM: gpu_float_args[0] is the timestep.
// PARAM: gpu_float_args[1] is the softening.
clSetKernelArg(kernel, 0, sizeof(cl_float), &(gpu_float_args[0]));
clSetKernelArg(kernel, 1, sizeof(cl_float), &(gpu_float_args[1]));
// Do one iteration of the n-body simulation.
size_t global_work_size = n;
// PARAM: local_work_size should be modified depending on your GPU.
// This should be tested on a realtime renderer first, such as
// CobaltXII/boiler/experimental/n_body_cl/.
size_t local_work_size = 256;
clEnqueueNDRangeKernel(command_queue, kernel, 1, NULL, &global_work_size, &local_work_size, 0, NULL, NULL);
// Read state2 back into local CPU memory.
clEnqueueReadBuffer(command_queue, gpu_state2, CL_TRUE, 0, n * sizeof(cl_float4), state2, 0, NULL, NULL);
// Get the iteration end time.
clock_t end_iteration = clock();
// Export state2.
std::stringstream name_builder;
name_builder << "frame_" << i << ".dat";
std::ofstream frame(name_builder.str());
frame.write((const char*)state2, n * sizeof(cl_float4));
frame.close();
// Get the export end time.
clock_t end_export = clock();
// Print frame data.
std::cout << "Frame " << i << " done in " << float(end_export - begin) / float(CLOCKS_PER_SEC) << " s (" << float(end_iteration - begin) / float(CLOCKS_PER_SEC) << " s on calculations, " << float(end_export - end_iteration) / float(CLOCKS_PER_SEC) << " s on export)" << std::endl;
// Swap buffers.
std::swap(gpu_state1, gpu_state2);
clSetKernelArg(kernel, 3, sizeof(cl_mem), (void*)&gpu_state1);
clSetKernelArg(kernel, 4, sizeof(cl_mem), (void*)&gpu_state2);
}
// Get the simulation ending time.
time_t sim_end = time(NULL);
// Print the overall runtime details.
std::cout << "Simulated and output " << frames << " frames in " << sim_end - sim_start << " s (" << float(sim_end - sim_start) / float(frames) << " s/frame)" << std::endl;
// Exit successfully.
return EXIT_SUCCESS;
}