#ifndef NBODY_H_ #define NBODY_H_ #include #include #include #include #include #include #include #include #include #include #define GROUP_SIZE 64 #define ITER 5 /** * NBody * Class implements OpenCL NBody sample * */ class NBody { public: cl_double setupTime; // Time taken to setup OpenCL resources and building kernel; cl_double kernelTime; // Time taken to run kernel and read result back; size_t maxWorkGroupSize; // Max allowed work-items in a group; cl_uint maxDimensions; // Max group dimensions allowed; size_t* maxWorkItemSizes; // Max work-items sizes in each dimensions; cl_ulong totalLocalMemory; // Max local memory allowed; cl_ulong usedLocalMemory; // Used local memory; cl_float delT; // dT (timestep); cl_float espSqr; // Softening Factor; cl_float* initPos; // Initial position; cl_float* initVel; // Initial velocity; cl_float* vel; // Output velocity; cl_float* refPos; // Reference position; cl_float* refVel; // Reference velocity; cl_context context; // CL context; cl_device_id *devices; // CL device list; cl_mem updatedPos; // Position of partciles; cl_mem updatedVel; // Velocity of partciles; cl_command_queue commandQueue; // CL command queue; cl_program program; // CL program; cl_kernel kernel; // CL kernel; cl_int numParticles; // Number of particles in the system; const char * filename; // The name of the file that contains the kernel code; private: float random(float randMax, float randMin); int compareArray(const float* mat0, const float* mat1, unsigned int size); public: /** * Constructor * Initialize member variables * @param name name of sample (string) */ explicit NBody(std::string name) { setupTime = 0; kernelTime = 0; delT = 0.005f; espSqr = 50.0f; initPos = NULL; initVel = NULL; vel = NULL; refPos = NULL; refVel = NULL; devices = NULL; maxWorkItemSizes = NULL; filename = "NBody_Kernels.cl"; numParticles = 164; } /** * Constructor * Initialize member variables * @param name name of sample (const char*) */ explicit NBody(const char* name) { setupTime = 0; kernelTime = 0; delT = 0.005f; espSqr = 50.0f; initPos = NULL; initVel = NULL; vel = NULL; refPos = NULL; refVel = NULL; devices = NULL; maxWorkItemSizes = NULL; filename = "NBody_Kernels.cl"; numParticles = 1564; } ~NBody(); /** * Allocate and initialize host memory array with random values * @return 1 on success and 0 on failure */ int setupNBody(); /** * OpenCL related initialisations. * Set up Context, Device list, Command Queue, Memory buffers * Build CL kernel program executable * @return 1 on success and 0 on failure */ int setupCL(); /** * Set values for kernels' arguments * @return 1 on success and 0 on failure */ int setupCLKernels(); /** * Enqueue calls to the kernels * on to the command queue, wait till end of kernel execution. * Get kernel start and end time if timing is enabled * @return 1 on success and 0 on failure */ int runCLKernels(); /** * Load a .cl source file as a char* and it will be used * for as a parameter for creating a program * @return the char vector */ char * load_program_source(const char *filename); /** * Reference CPU implementation of Binomial Option * for performance comparison */ void nBodyCPUReference(); /** * Override from SDKSample. Print sample stats. */ void printStats(); /** * Override from SDKSample, adjust width and height * of execution domain, perform all sample setup */ int setup(); /** * Override from SDKSample * Run OpenCL NBody */ int run(); /** * Override from SDKSample * Cleanup memory allocations */ int cleanup(); }; #endif // NBODY_H_