| 172 | } |
| 173 | |
| 174 | void gpu_data:: |
| 175 | set_size( |
| 176 | size_t new_size |
| 177 | ) |
| 178 | { |
| 179 | if (new_size == 0) |
| 180 | { |
| 181 | if (device_in_use) |
| 182 | { |
| 183 | // Wait for any possible CUDA kernels that might be using our memory block to |
| 184 | // complete before we free the memory. |
| 185 | synchronize_stream(0); |
| 186 | device_in_use = false; |
| 187 | } |
| 188 | wait_for_transfer_to_finish(); |
| 189 | data_size = 0; |
| 190 | host_current = true; |
| 191 | device_current = true; |
| 192 | device_in_use = false; |
| 193 | the_device_id = 0; |
| 194 | data_host.reset(); |
| 195 | data_device.reset(); |
| 196 | } |
| 197 | else if (new_size != data_size) |
| 198 | { |
| 199 | if (device_in_use) |
| 200 | { |
| 201 | // Wait for any possible CUDA kernels that might be using our memory block to |
| 202 | // complete before we free the memory. |
| 203 | synchronize_stream(0); |
| 204 | device_in_use = false; |
| 205 | } |
| 206 | wait_for_transfer_to_finish(); |
| 207 | data_size = new_size; |
| 208 | host_current = true; |
| 209 | device_current = true; |
| 210 | device_in_use = false; |
| 211 | |
| 212 | if (!cuda::use_cuda()) |
| 213 | { |
| 214 | data_host.reset(new float[new_size], std::default_delete<float[]>()); |
| 215 | the_device_id = 0; |
| 216 | return; |
| 217 | } |
| 218 | |
| 219 | try |
| 220 | { |
| 221 | CHECK_CUDA(cudaGetDevice(&the_device_id)); |
| 222 | |
| 223 | // free memory blocks before we allocate new ones. |
| 224 | data_host.reset(); |
| 225 | data_device.reset(); |
| 226 | |
| 227 | void* data; |
| 228 | CHECK_CUDA(cudaMallocHost(&data, new_size*sizeof(float))); |
| 229 | // Note that we don't throw exceptions since the free calls are invariably |
| 230 | // called in destructors. They also shouldn't fail anyway unless someone |
| 231 | // is resetting the GPU card in the middle of their program. |
nothing calls this directly
no test coverage detected