#include "NBody.hpp" int numBodies; // No. of particles. This will show the same number as numParticles // class member variable of NBody, but it is used outside the class, // whitout being necessary to have a pointer to the class; extern FILE *inputDataFile; extern void* me; /////////////////////////////////// NBody::setupNBody Func \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ // // Initialize the particles in the system : // - their initial positions; // - their initial velocities. // The data is read from a file or randomly generated. // /////////////////////////////////// NBody::setupNBody Func \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ int NBody::setupNBody() { //-------------------------------------------- // Make sure numParticles is multiple of group size numParticles = (numParticles < GROUP_SIZE) ? GROUP_SIZE : numParticles; numParticles = (numParticles / GROUP_SIZE) * GROUP_SIZE; numBodies = numParticles; //-------------------------------------------- // First we will use initPos and initVel vectors to generate the // input data. They will be used in this function to // initialize the other vectors: the pos and vel vectors. initPos = (cl_float*)malloc(numBodies * sizeof(cl_float4)); if(initPos == NULL) { std::cout << "Failed to allocate host memory. (initPos)" << std::endl; return 1; } initVel = (cl_float*)malloc(numBodies * sizeof(cl_float4)); if(initVel == NULL) { std::cout << "Failed to allocate host memory. (initVel)" << std::endl; return 1; } //-------------------------------------------- // Initialization of inputs (positions and velocities). As we // can see, each particle will occupy 4 elements in the // pos and vel vectors. This is because the position and // velocity is represented by their 3 components x, y, z. // Because we work with OpenCL we use cl_float4 vectors, so // vectors with four elements. We put the mass of the particle // on the 4th position occupied by a particle in the pos vector. // If the particles data is read from a file: if (readInputData){ if (inputDataFile == NULL){ std::cout << "Error: inputData file not opened." << std::endl; ((NBody*)me)->cleanup(); exit(1); } for (int i = 0; i < numBodies ; i++){ int idx = i*4; fscanf(inputDataFile, "%f", &initPos[idx+3]); // Mass of particle i; for (int k = 0; k < 3; k++) fscanf(inputDataFile, "%f", &initPos[idx+k]); // Position of particle i; for (int k = 0; k < 3; k++) fscanf(inputDataFile, "%f", &initVel[idx+k]); // Velocity of particle i; initVel[idx+3] = 0.0f; // Unused; } }else{ // If the particles data is generated randomly: for(int i = 0; i < numBodies; ++i) { int index = 4 * i; // First 3 values are position in x,y and z direction for(int j = 0; j < 3; ++j) { initPos[index + j] = random(3, 50); } // Mass value initPos[index + 3] = random(1, 1000); //initPos[index + 3] = random(0.1, 1.0); //initPos[index + 3] = 0.04; // First 3 values are velocity in x,y and z direction for(int j = 0; j < 3; ++j) { initVel[index + j] = 0.0f; } // unused initVel[3] = 0.0f; } } //================================================ // Resources common to all integrators: //-------------------------------------------- // Allocate memory for pos and vel vectors. #if defined (_WIN32) pos = (cl_float*)_aligned_malloc(numBodies * sizeof(cl_float4), 16); #else pos = (cl_float*)memalign(16, numBodies * sizeof(cl_float4)); #endif if(pos == NULL) { std::cout << "Failed to allocate host memory. (pos)" << std::endl; return 1; } #if defined (_WIN32) vel = (cl_float*)_aligned_malloc(numBodies * sizeof(cl_float4), 16); #else vel = (cl_float*)memalign(16, numBodies * sizeof(cl_float4)); #endif if(vel == NULL) { std::cout << "Failed to allocate host memory. (vel)" << std::endl; return 1; } //-------------------------------------------- // Copy the auxiliary vectors into the pos and vel ones: memcpy(pos, initPos, 4 * numBodies * sizeof(cl_float)); memcpy(vel, initVel, 4 * numBodies * sizeof(cl_float)); //================================================ // Resources specific only for Leapfrog integrator: if (integrator == LEAPFROG){ #if defined (_WIN32) acc = (cl_float*)_aligned_malloc(numBodies * sizeof(cl_float4), 16); #else acc = (cl_float*)memalign(16, numBodies * sizeof(cl_float4)); #endif if(acc == NULL) { std::cout << "Failed to allocate host memory. (acc)" << std::endl; return 1; } #if defined (_WIN32) collTime = (cl_float*)_aligned_malloc(numBodies * sizeof(cl_float), 16); #else collTime = (cl_float*)memalign(16, numBodies * sizeof(cl_float)); #endif if(collTime == NULL) { std::cout << "Failed to allocate host memory. (collTime)" << std::endl; return 1; } } return 0; } /////////////////////////////////// NBody::random Func \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ // // Generates random values in the interval [randMax, randMin] // /////////////////////////////////// NBody::random Func \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ float NBody::random(float randMax, float randMin) { float result; result =(float)rand()/(float)RAND_MAX; return ((1.0f - result) * randMin + result *randMax); }