[35] | 1 | #include "NBody.hpp" |
---|
| 2 | |
---|
| 3 | extern int numBodies; // No. of particles; |
---|
| 4 | |
---|
| 5 | /////////////////////////////////// NBody::setupCL Func \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ |
---|
| 6 | // |
---|
| 7 | // Initialize OpenCL specific information : |
---|
| 8 | // - get context, devices; |
---|
| 9 | // - create command-queues, memory buffers, program, kernels; |
---|
| 10 | // |
---|
| 11 | /////////////////////////////////// NBody::setupCL Func \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ |
---|
| 12 | |
---|
| 13 | int NBody::setupCL() |
---|
| 14 | { |
---|
| 15 | cl_int status = CL_SUCCESS; |
---|
| 16 | |
---|
| 17 | cl_device_type dType; |
---|
| 18 | |
---|
| 19 | if(deviceType.compare("cpu") == 0) |
---|
| 20 | { |
---|
| 21 | dType = CL_DEVICE_TYPE_CPU; |
---|
| 22 | } |
---|
| 23 | else //deviceType = "gpu" |
---|
| 24 | { |
---|
| 25 | dType = CL_DEVICE_TYPE_GPU; |
---|
| 26 | } |
---|
| 27 | |
---|
| 28 | /* Create context from given device type */ |
---|
| 29 | context = clCreateContextFromType( |
---|
| 30 | 0, |
---|
| 31 | dType, |
---|
| 32 | NULL, |
---|
| 33 | NULL, |
---|
| 34 | &status); |
---|
| 35 | /* |
---|
| 36 | * if opencl fails to open a context on default device GPU |
---|
| 37 | * then it falls back to CPU |
---|
| 38 | */ |
---|
| 39 | if(status != CL_SUCCESS && dType == CL_DEVICE_TYPE_GPU) |
---|
| 40 | { |
---|
| 41 | std::cout << "Unsupported GPU device; falling back to CPU ..." << std::endl; |
---|
| 42 | std::cout << std::endl; |
---|
| 43 | context = clCreateContextFromType( |
---|
| 44 | 0, |
---|
| 45 | CL_DEVICE_TYPE_CPU, |
---|
| 46 | NULL, |
---|
| 47 | NULL, |
---|
| 48 | &status); |
---|
| 49 | } |
---|
| 50 | |
---|
| 51 | if (status != CL_SUCCESS){ |
---|
| 52 | std::cout << "clCreateContextFromType failed." << std::endl; |
---|
| 53 | return 1; |
---|
| 54 | } |
---|
| 55 | |
---|
| 56 | size_t deviceListSize; |
---|
| 57 | |
---|
| 58 | /* First, get the size of device list data */ |
---|
| 59 | status = clGetContextInfo( |
---|
| 60 | context, |
---|
| 61 | CL_CONTEXT_DEVICES, |
---|
| 62 | 0, |
---|
| 63 | NULL, |
---|
| 64 | &deviceListSize); |
---|
| 65 | |
---|
| 66 | if (status != CL_SUCCESS){ |
---|
| 67 | std::cout << "clGetContextInfo failed." << std::endl; |
---|
| 68 | return 1; |
---|
| 69 | } |
---|
| 70 | |
---|
| 71 | /* Now allocate memory for device list based on the size we got earlier */ |
---|
| 72 | devices = (cl_device_id *)malloc(deviceListSize); |
---|
| 73 | |
---|
| 74 | if(devices==NULL) { |
---|
| 75 | std::cout << "Failed to allocate memory (devices)." << std::endl; |
---|
| 76 | return 1; |
---|
| 77 | } |
---|
| 78 | |
---|
| 79 | /* Now, get the device list data */ |
---|
| 80 | status = clGetContextInfo( |
---|
| 81 | context, |
---|
| 82 | CL_CONTEXT_DEVICES, |
---|
| 83 | deviceListSize, |
---|
| 84 | devices, |
---|
| 85 | NULL); |
---|
| 86 | |
---|
| 87 | if (status != CL_SUCCESS){ |
---|
| 88 | std::cout << "clGetContextInfo failed." << std::endl; |
---|
| 89 | return 1; |
---|
| 90 | } |
---|
| 91 | |
---|
| 92 | /* Create command queue */ |
---|
| 93 | |
---|
| 94 | commandQueue = clCreateCommandQueue( |
---|
| 95 | context, |
---|
| 96 | devices[0], |
---|
| 97 | 0, |
---|
| 98 | &status); |
---|
| 99 | |
---|
| 100 | if (status != CL_SUCCESS){ |
---|
| 101 | std::cout << "clCreateCommandQueue failed." << std::endl; |
---|
| 102 | return 1; |
---|
| 103 | } |
---|
| 104 | |
---|
| 105 | //---------------------------------- |
---|
| 106 | // Device infos: |
---|
| 107 | |
---|
| 108 | getDeviceInfo(); |
---|
| 109 | |
---|
| 110 | /* |
---|
| 111 | * Create and initialize memory objects |
---|
| 112 | */ |
---|
| 113 | |
---|
| 114 | //================================================ |
---|
| 115 | // Buffers used for all Kernels: |
---|
| 116 | |
---|
| 117 | /* Create memory objects for position */ |
---|
| 118 | updatedPos = clCreateBuffer( |
---|
| 119 | context, |
---|
| 120 | CL_MEM_READ_WRITE | CL_MEM_USE_HOST_PTR, |
---|
| 121 | numBodies * sizeof(cl_float4), // numbodies * (cl_float f32[4]); |
---|
| 122 | pos, |
---|
| 123 | &status); |
---|
| 124 | |
---|
| 125 | if (status != CL_SUCCESS){ |
---|
| 126 | std::cout << "clCreateBuffer failed. (updatePos)" << std::endl; |
---|
| 127 | return 1; |
---|
| 128 | } |
---|
| 129 | |
---|
| 130 | /* Create memory objects for velocity */ |
---|
| 131 | updatedVel = clCreateBuffer( |
---|
| 132 | context, |
---|
| 133 | CL_MEM_READ_ONLY | CL_MEM_USE_HOST_PTR, |
---|
| 134 | numBodies * sizeof(cl_float4), |
---|
| 135 | vel, |
---|
| 136 | &status); |
---|
| 137 | |
---|
| 138 | if (status != CL_SUCCESS){ |
---|
| 139 | std::cout << "clCreateBuffer failed. (updatedVel)" << std::endl; |
---|
| 140 | return 1; |
---|
| 141 | } |
---|
| 142 | |
---|
| 143 | //================================================ |
---|
| 144 | // Buffers used only for Leapfrog Kernels: |
---|
| 145 | |
---|
| 146 | if (integrator == LEAPFROG){ |
---|
| 147 | |
---|
| 148 | /* Create memory objects for accelerations */ |
---|
| 149 | updatedAcc = clCreateBuffer( |
---|
| 150 | context, |
---|
| 151 | CL_MEM_READ_ONLY | CL_MEM_USE_HOST_PTR, |
---|
| 152 | numBodies * sizeof(cl_float4), |
---|
| 153 | acc, |
---|
| 154 | &status); |
---|
| 155 | |
---|
| 156 | if (status != CL_SUCCESS){ |
---|
| 157 | std::cout << "clCreateBuffer failed. (updatedAcc)" << std::endl; |
---|
| 158 | return 1; |
---|
| 159 | } |
---|
| 160 | |
---|
| 161 | /* Create memory objects for collision time */ |
---|
| 162 | updatedCollTime = clCreateBuffer( |
---|
| 163 | context, |
---|
| 164 | CL_MEM_READ_ONLY | CL_MEM_USE_HOST_PTR, |
---|
| 165 | numBodies * sizeof(cl_float), |
---|
| 166 | collTime, |
---|
| 167 | &status); |
---|
| 168 | |
---|
| 169 | if (status != CL_SUCCESS){ |
---|
| 170 | std::cout << "clCreateBuffer failed. (updatedCollTime)" << std::endl; |
---|
| 171 | return 1; |
---|
| 172 | } |
---|
| 173 | |
---|
| 174 | initialAccComputation = 1; |
---|
| 175 | } |
---|
| 176 | |
---|
| 177 | return 0; |
---|
| 178 | } |
---|
| 179 | |
---|