forked from tlgimenes/CLGLSim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
123 lines (102 loc) · 4.02 KB
/
main.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
//--------------------------------//
// //
// Author: Tiago Lobato Gimenes //
// email: [email protected] //
// //
//--------------------------------//
#ifdef WIN32
#include "CLGLWindows.hpp"
#else
#include "CLGLLinux.hpp"
#endif
#include "CLGLWindow.hpp"
#include "CLGLError.hpp"
#include "CLGLParser.hpp"
#include "CLGLDataLoader.hpp"
#include <string>
#include <fstream>
#include <CL/cl.hpp>
int main(int argc, char * argv[])
{
float rungeStep;
int NUM_PART;
std::string windowTitle = "CLGLSim 1.0";
CLGLParser console = CLGLParser(argc, argv);
CLGLSim::curKernel = console.curKernel;
NUM_PART = console.particlesNum;
rungeStep = console.rungeStep;
std::string kernelFileName = console.kernelFile;
std::cout << "-----------------------------------" << std::endl;
std::cout << "Using " << console.kernel << " Kernel !" << std::endl;
std::cout << "-----------------------------------" << std::endl;
body * hostData;
// Define Window Height and Width
CLGLWindow::window_height = 768;
CLGLWindow::window_width = 1366;
try{
// Start CLGL
#ifndef WIN32
CLGLLinux clgl = CLGLLinux();
#else
CLGLWindows clgl = CLGLWindows();
#endif
clgl.CLGLStart(argc, argv, windowTitle, CLGLWindow::window_height, CLGLWindow::window_width);
// Get Window ID
CLGLWindow::glutWindowHandle = clgl.CLGLGetWindowID();
CLGLWindow window = CLGLWindow();
std::cout << "-----------------------------------" << std::endl;
std::cout << "Loading Data" << std::endl;
std::cout << "-----------------------------------" << std::endl;
if(console.isDataFileSet())
hostData = loadDataFromFile(console.dataFile, &NUM_PART);
else
hostData = genData(NUM_PART);
// Set the Number of Particles beeing simulated
CLGLWindow::NumParticles = NUM_PART;
// Print info About Current Platform and devices
clgl.CLGLPrintAllInfo();
// Build the Source of the kernel
clgl.CLGLBuildProgramSource(kernelFileName);
// Build the function console.kernel in the kernel
clgl.CLGLBuildKernel(console.kernel);
console.kernel = "Gravity_rk1";
clgl.CLGLBuildKernel(console.kernel);
console.kernel = "Gravity_rk2";
clgl.CLGLBuildKernel(console.kernel);
console.kernel = "Gravity_rk4";
clgl.CLGLBuildKernel(console.kernel);
std::cout << "-----------------------------------" << std::endl;
std::cout << "Pushing Data to Device" << std::endl;
std::cout << "-----------------------------------" << std::endl;
clgl.CLGLLoadVBODataToDevice(hostData->pos.size() * sizeof(vector), &(hostData->pos[0]), CL_MEM_READ_WRITE);
clgl.CLGLLoadVBODataToDevice(hostData->color.size() * sizeof(vector), &(hostData->color[0]), CL_MEM_READ_WRITE);
clgl.CLGLLoadDataToDevice(CL_TRUE, NUM_PART * sizeof(GLfloat), hostData->mass, CL_MEM_READ_ONLY);
clgl.CLGLLoadDataToDevice(CL_TRUE, hostData->vel.size() * sizeof(vector), &(hostData->vel[0]), CL_MEM_READ_WRITE);
std::vector<cl::Buffer> buff = *clgl.CLGLGetBuffer();
std::vector<cl::Memory> mem = *clgl.CLGLGetBufferGL();
std::vector<cl::Kernel> ker = *clgl.CLGLGetKernel();
// Set the Kernel ARGS
for(unsigned int i=0; i < ker.size(); i++){
clgl.CLGLSetArg(0, buff[0], ker[i]);
clgl.CLGLSetArg(1, buff[1], ker[i]);
clgl.CLGLSetArg(2, mem[0], ker[i]);
clgl.CLGLSetArg(3, sizeof(float), &rungeStep, ker[i]);
clgl.CLGLSetArg(4, sizeof(int), &NUM_PART, ker[i]);
}
// Set CLGLSim static members
CLGLSim::vbo = clgl.CLGLGetVBO();
CLGLSim::ParticlesNum = NUM_PART;
CLGLSim::rungeStep = rungeStep;
CLGLSim::clgl = &(clgl);
CLGLSim::rkx = &ker;
std::cout << "-----------------------------------" << std::endl;
std::cout << "Runing the Window" << std::endl;
std::cout << "-----------------------------------" << std::endl;
window.CLGLRunWindow();
}
catch(cl::Error error)
{
std::cout << error.what() << CLGLError::errToStr(error.err())->c_str() << std::endl;
}
return 0;
}