1 | #ifndef NBODY_H_ |
---|
2 | #define NBODY_H_ |
---|
3 | |
---|
4 | #include <CL/cl.h> |
---|
5 | |
---|
6 | #include <iostream> |
---|
7 | #include <stdio.h> |
---|
8 | #include <stdlib.h> |
---|
9 | #include <assert.h> |
---|
10 | #include <string.h> |
---|
11 | #include <cmath> |
---|
12 | #include <malloc.h> |
---|
13 | #include <time.h> |
---|
14 | |
---|
15 | #include <GL/glut.h> |
---|
16 | |
---|
17 | #define GROUP_SIZE 64 |
---|
18 | #define ITER 5 |
---|
19 | |
---|
20 | /** |
---|
21 | * NBody |
---|
22 | * Class implements OpenCL NBody sample |
---|
23 | * |
---|
24 | */ |
---|
25 | |
---|
26 | class NBody |
---|
27 | { |
---|
28 | public: |
---|
29 | cl_double setupTime; // Time taken to setup OpenCL resources and building kernel; |
---|
30 | cl_double kernelTime; // Time taken to run kernel and read result back; |
---|
31 | |
---|
32 | size_t maxWorkGroupSize; // Max allowed work-items in a group; |
---|
33 | cl_uint maxDimensions; // Max group dimensions allowed; |
---|
34 | size_t* maxWorkItemSizes; // Max work-items sizes in each dimensions; |
---|
35 | cl_ulong totalLocalMemory; // Max local memory allowed; |
---|
36 | cl_ulong usedLocalMemory; // Used local memory; |
---|
37 | |
---|
38 | cl_float delT; // dT (timestep); |
---|
39 | cl_float espSqr; // Softening Factor; |
---|
40 | cl_float* initPos; // Initial position; |
---|
41 | cl_float* initVel; // Initial velocity; |
---|
42 | cl_float* vel; // Output velocity; |
---|
43 | cl_float* refPos; // Reference position; |
---|
44 | cl_float* refVel; // Reference velocity; |
---|
45 | cl_context context; // CL context; |
---|
46 | cl_device_id *devices; // CL device list; |
---|
47 | cl_mem updatedPos; // Position of partciles; |
---|
48 | cl_mem updatedVel; // Velocity of partciles; |
---|
49 | cl_command_queue commandQueue; // CL command queue; |
---|
50 | cl_program program; // CL program; |
---|
51 | cl_kernel kernel; // CL kernel; |
---|
52 | |
---|
53 | cl_int numParticles; // Number of particles in the system; |
---|
54 | const char * filename; // The name of the file that contains the kernel code; |
---|
55 | |
---|
56 | |
---|
57 | private: |
---|
58 | |
---|
59 | float random(float randMax, float randMin); |
---|
60 | int compareArray(const float* mat0, const float* mat1, unsigned int size); |
---|
61 | |
---|
62 | public: |
---|
63 | /** |
---|
64 | * Constructor |
---|
65 | * Initialize member variables |
---|
66 | * @param name name of sample (string) |
---|
67 | */ |
---|
68 | explicit NBody(std::string name) |
---|
69 | { |
---|
70 | setupTime = 0; |
---|
71 | kernelTime = 0; |
---|
72 | delT = 0.005f; |
---|
73 | espSqr = 50.0f; |
---|
74 | initPos = NULL; |
---|
75 | initVel = NULL; |
---|
76 | vel = NULL; |
---|
77 | refPos = NULL; |
---|
78 | refVel = NULL; |
---|
79 | devices = NULL; |
---|
80 | maxWorkItemSizes = NULL; |
---|
81 | filename = "NBody_Kernels.cl"; |
---|
82 | numParticles = 164; |
---|
83 | } |
---|
84 | |
---|
85 | /** |
---|
86 | * Constructor |
---|
87 | * Initialize member variables |
---|
88 | * @param name name of sample (const char*) |
---|
89 | */ |
---|
90 | explicit NBody(const char* name) |
---|
91 | { |
---|
92 | setupTime = 0; |
---|
93 | kernelTime = 0; |
---|
94 | delT = 0.005f; |
---|
95 | espSqr = 50.0f; |
---|
96 | initPos = NULL; |
---|
97 | initVel = NULL; |
---|
98 | vel = NULL; |
---|
99 | refPos = NULL; |
---|
100 | refVel = NULL; |
---|
101 | devices = NULL; |
---|
102 | maxWorkItemSizes = NULL; |
---|
103 | filename = "NBody_Kernels.cl"; |
---|
104 | numParticles = 1564; |
---|
105 | } |
---|
106 | |
---|
107 | ~NBody(); |
---|
108 | |
---|
109 | /** |
---|
110 | * Allocate and initialize host memory array with random values |
---|
111 | * @return 1 on success and 0 on failure |
---|
112 | */ |
---|
113 | int setupNBody(); |
---|
114 | |
---|
115 | /** |
---|
116 | * OpenCL related initialisations. |
---|
117 | * Set up Context, Device list, Command Queue, Memory buffers |
---|
118 | * Build CL kernel program executable |
---|
119 | * @return 1 on success and 0 on failure |
---|
120 | */ |
---|
121 | int setupCL(); |
---|
122 | |
---|
123 | /** |
---|
124 | * Set values for kernels' arguments |
---|
125 | * @return 1 on success and 0 on failure |
---|
126 | */ |
---|
127 | int setupCLKernels(); |
---|
128 | |
---|
129 | /** |
---|
130 | * Enqueue calls to the kernels |
---|
131 | * on to the command queue, wait till end of kernel execution. |
---|
132 | * Get kernel start and end time if timing is enabled |
---|
133 | * @return 1 on success and 0 on failure |
---|
134 | */ |
---|
135 | int runCLKernels(); |
---|
136 | |
---|
137 | /** |
---|
138 | * Load a .cl source file as a char* and it will be used |
---|
139 | * for as a parameter for creating a program |
---|
140 | * @return the char vector |
---|
141 | */ |
---|
142 | char * load_program_source(const char *filename); |
---|
143 | |
---|
144 | /** |
---|
145 | * Reference CPU implementation of Binomial Option |
---|
146 | * for performance comparison |
---|
147 | */ |
---|
148 | void nBodyCPUReference(); |
---|
149 | |
---|
150 | /** |
---|
151 | * Override from SDKSample. Print sample stats. |
---|
152 | */ |
---|
153 | void printStats(); |
---|
154 | |
---|
155 | /** |
---|
156 | * Override from SDKSample, adjust width and height |
---|
157 | * of execution domain, perform all sample setup |
---|
158 | */ |
---|
159 | int setup(); |
---|
160 | |
---|
161 | /** |
---|
162 | * Override from SDKSample |
---|
163 | * Run OpenCL NBody |
---|
164 | */ |
---|
165 | int run(); |
---|
166 | |
---|
167 | /** |
---|
168 | * Override from SDKSample |
---|
169 | * Cleanup memory allocations |
---|
170 | */ |
---|
171 | int cleanup(); |
---|
172 | }; |
---|
173 | |
---|
174 | #endif // NBODY_H_ |
---|