[35] | 1 | #include "NBody.hpp" |
---|
| 2 | |
---|
| 3 | int numBodies; // No. of particles. This will show the same number as numParticles |
---|
| 4 | // class member variable of NBody, but it is used outside the class, |
---|
| 5 | // whitout being necessary to have a pointer to the class; |
---|
| 6 | extern FILE *inputDataFile; |
---|
| 7 | extern void* me; |
---|
| 8 | |
---|
| 9 | /////////////////////////////////// NBody::setupNBody Func \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ |
---|
| 10 | // |
---|
| 11 | // Initialize the particles in the system : |
---|
| 12 | // - their initial positions; |
---|
| 13 | // - their initial velocities. |
---|
| 14 | // The data is read from a file or randomly generated. |
---|
| 15 | // |
---|
| 16 | /////////////////////////////////// NBody::setupNBody Func \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ |
---|
| 17 | |
---|
| 18 | int NBody::setupNBody() |
---|
| 19 | { |
---|
| 20 | //-------------------------------------------- |
---|
| 21 | // Make sure numParticles is multiple of group size |
---|
| 22 | numParticles = (numParticles < GROUP_SIZE) ? GROUP_SIZE : numParticles; |
---|
| 23 | numParticles = (numParticles / GROUP_SIZE) * GROUP_SIZE; |
---|
| 24 | |
---|
| 25 | numBodies = numParticles; |
---|
| 26 | |
---|
| 27 | //-------------------------------------------- |
---|
| 28 | // First we will use initPos and initVel vectors to generate the |
---|
| 29 | // input data. They will be used in this function to |
---|
| 30 | // initialize the other vectors: the pos and vel vectors. |
---|
| 31 | |
---|
| 32 | initPos = (cl_float*)malloc(numBodies * sizeof(cl_float4)); |
---|
| 33 | if(initPos == NULL) |
---|
| 34 | { |
---|
| 35 | std::cout << "Failed to allocate host memory. (initPos)" << std::endl; |
---|
| 36 | return 1; |
---|
| 37 | } |
---|
| 38 | |
---|
| 39 | initVel = (cl_float*)malloc(numBodies * sizeof(cl_float4)); |
---|
| 40 | if(initVel == NULL) |
---|
| 41 | { |
---|
| 42 | std::cout << "Failed to allocate host memory. (initVel)" << std::endl; |
---|
| 43 | return 1; |
---|
| 44 | } |
---|
| 45 | |
---|
| 46 | //-------------------------------------------- |
---|
| 47 | // Initialization of inputs (positions and velocities). As we |
---|
| 48 | // can see, each particle will occupy 4 elements in the |
---|
| 49 | // pos and vel vectors. This is because the position and |
---|
| 50 | // velocity is represented by their 3 components x, y, z. |
---|
| 51 | // Because we work with OpenCL we use cl_float4 vectors, so |
---|
| 52 | // vectors with four elements. We put the mass of the particle |
---|
| 53 | // on the 4th position occupied by a particle in the pos vector. |
---|
| 54 | |
---|
| 55 | // If the particles data is read from a file: |
---|
| 56 | if (readInputData){ |
---|
| 57 | |
---|
| 58 | if (inputDataFile == NULL){ |
---|
| 59 | std::cout << "Error: inputData file not opened." << std::endl; |
---|
| 60 | ((NBody*)me)->cleanup(); |
---|
| 61 | exit(1); |
---|
| 62 | } |
---|
| 63 | |
---|
| 64 | for (int i = 0; i < numBodies ; i++){ |
---|
| 65 | int idx = i*4; |
---|
| 66 | |
---|
| 67 | fscanf(inputDataFile, "%f", &initPos[idx+3]); // Mass of particle i; |
---|
| 68 | |
---|
| 69 | for (int k = 0; k < 3; k++) |
---|
| 70 | fscanf(inputDataFile, "%f", &initPos[idx+k]); // Position of particle i; |
---|
| 71 | |
---|
| 72 | for (int k = 0; k < 3; k++) |
---|
| 73 | fscanf(inputDataFile, "%f", &initVel[idx+k]); // Velocity of particle i; |
---|
| 74 | |
---|
| 75 | initVel[idx+3] = 0.0f; // Unused; |
---|
| 76 | |
---|
| 77 | } |
---|
| 78 | |
---|
| 79 | }else{ |
---|
| 80 | // If the particles data is generated randomly: |
---|
| 81 | for(int i = 0; i < numBodies; ++i) |
---|
| 82 | { |
---|
| 83 | int index = 4 * i; |
---|
| 84 | |
---|
| 85 | // First 3 values are position in x,y and z direction |
---|
| 86 | for(int j = 0; j < 3; ++j) |
---|
| 87 | { |
---|
| 88 | initPos[index + j] = random(3, 50); |
---|
| 89 | } |
---|
| 90 | |
---|
| 91 | // Mass value |
---|
| 92 | initPos[index + 3] = random(1, 1000); |
---|
| 93 | //initPos[index + 3] = random(0.1, 1.0); |
---|
| 94 | //initPos[index + 3] = 0.04; |
---|
| 95 | |
---|
| 96 | // First 3 values are velocity in x,y and z direction |
---|
| 97 | for(int j = 0; j < 3; ++j) |
---|
| 98 | { |
---|
| 99 | initVel[index + j] = 0.0f; |
---|
| 100 | } |
---|
| 101 | |
---|
| 102 | // unused |
---|
| 103 | initVel[3] = 0.0f; |
---|
| 104 | } |
---|
| 105 | } |
---|
| 106 | |
---|
| 107 | //================================================ |
---|
| 108 | // Resources common to all integrators: |
---|
| 109 | |
---|
| 110 | //-------------------------------------------- |
---|
| 111 | // Allocate memory for pos and vel vectors. |
---|
| 112 | |
---|
| 113 | #if defined (_WIN32) |
---|
| 114 | pos = (cl_float*)_aligned_malloc(numBodies * sizeof(cl_float4), 16); |
---|
| 115 | #else |
---|
| 116 | pos = (cl_float*)memalign(16, numBodies * sizeof(cl_float4)); |
---|
| 117 | #endif |
---|
| 118 | |
---|
| 119 | if(pos == NULL) |
---|
| 120 | { |
---|
| 121 | std::cout << "Failed to allocate host memory. (pos)" << std::endl; |
---|
| 122 | return 1; |
---|
| 123 | } |
---|
| 124 | |
---|
| 125 | #if defined (_WIN32) |
---|
| 126 | vel = (cl_float*)_aligned_malloc(numBodies * sizeof(cl_float4), 16); |
---|
| 127 | #else |
---|
| 128 | vel = (cl_float*)memalign(16, numBodies * sizeof(cl_float4)); |
---|
| 129 | #endif |
---|
| 130 | |
---|
| 131 | if(vel == NULL) |
---|
| 132 | { |
---|
| 133 | std::cout << "Failed to allocate host memory. (vel)" << std::endl; |
---|
| 134 | return 1; |
---|
| 135 | } |
---|
| 136 | |
---|
| 137 | //-------------------------------------------- |
---|
| 138 | // Copy the auxiliary vectors into the pos and vel ones: |
---|
| 139 | memcpy(pos, initPos, 4 * numBodies * sizeof(cl_float)); |
---|
| 140 | memcpy(vel, initVel, 4 * numBodies * sizeof(cl_float)); |
---|
| 141 | |
---|
| 142 | //================================================ |
---|
| 143 | // Resources specific only for Leapfrog integrator: |
---|
| 144 | |
---|
| 145 | if (integrator == LEAPFROG){ |
---|
| 146 | #if defined (_WIN32) |
---|
| 147 | acc = (cl_float*)_aligned_malloc(numBodies * sizeof(cl_float4), 16); |
---|
| 148 | #else |
---|
| 149 | acc = (cl_float*)memalign(16, numBodies * sizeof(cl_float4)); |
---|
| 150 | #endif |
---|
| 151 | |
---|
| 152 | if(acc == NULL) |
---|
| 153 | { |
---|
| 154 | std::cout << "Failed to allocate host memory. (acc)" << std::endl; |
---|
| 155 | return 1; |
---|
| 156 | } |
---|
| 157 | |
---|
| 158 | #if defined (_WIN32) |
---|
| 159 | collTime = (cl_float*)_aligned_malloc(numBodies * sizeof(cl_float), 16); |
---|
| 160 | #else |
---|
| 161 | collTime = (cl_float*)memalign(16, numBodies * sizeof(cl_float)); |
---|
| 162 | #endif |
---|
| 163 | |
---|
| 164 | if(collTime == NULL) |
---|
| 165 | { |
---|
| 166 | std::cout << "Failed to allocate host memory. (collTime)" << std::endl; |
---|
| 167 | return 1; |
---|
| 168 | } |
---|
| 169 | } |
---|
| 170 | |
---|
| 171 | return 0; |
---|
| 172 | } |
---|
| 173 | |
---|
| 174 | /////////////////////////////////// NBody::random Func \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ |
---|
| 175 | // |
---|
| 176 | // Generates random values in the interval [randMax, randMin] |
---|
| 177 | // |
---|
| 178 | /////////////////////////////////// NBody::random Func \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ |
---|
| 179 | |
---|
| 180 | float NBody::random(float randMax, float randMin) |
---|
| 181 | { |
---|
| 182 | float result; |
---|
| 183 | result =(float)rand()/(float)RAND_MAX; |
---|
| 184 | |
---|
| 185 | return ((1.0f - result) * randMin + result *randMax); |
---|
| 186 | } |
---|