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