| 202 | } |
| 203 | |
| 204 | bool Memory::allocHostMemory(void* initFrom, bool allocHostMem, bool forceCopy) { |
| 205 | // Sanity checks (the parameters should have been prevalidated by the API) |
| 206 | assert(!(flags_ & (CL_MEM_USE_HOST_PTR | CL_MEM_COPY_HOST_PTR) && (initFrom == NULL) && |
| 207 | !allocHostMem && !isSvmPtrCommited())); |
| 208 | assert( |
| 209 | !((initFrom != NULL) && !forceCopy && |
| 210 | !(flags_ & (CL_MEM_USE_HOST_PTR | CL_MEM_COPY_HOST_PTR | CL_MEM_EXTERNAL_PHYSICAL_AMD)))); |
| 211 | assert(!(flags_ & CL_MEM_COPY_HOST_PTR && flags_ & CL_MEM_USE_HOST_PTR)); |
| 212 | |
| 213 | const std::vector<Device*>& devices = context_().devices(); |
| 214 | |
| 215 | // This allocation is necessary to use coherency mechanism |
| 216 | // for the initialization |
| 217 | if (getMemFlags() & (CL_MEM_COPY_HOST_PTR | CL_MEM_ALLOC_HOST_PTR)) { |
| 218 | // Extra system memory allocation and copy can be very expensive. |
| 219 | // Thus, avoid it if runtime doesn't perform deferred allocations |
| 220 | if ((devices.size() == 1) || DISABLE_DEFERRED_ALLOC) { |
| 221 | allocHostMem = false; |
| 222 | setHostMem(initFrom); |
| 223 | } else { |
| 224 | allocHostMem = true; |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | // Did application request to use host memory? |
| 229 | if (getMemFlags() & CL_MEM_USE_HOST_PTR) { |
| 230 | setHostMem(initFrom); |
| 231 | |
| 232 | // Recalculate image size according to pitch |
| 233 | Image* image = asImage(); |
| 234 | if (image != NULL) { |
| 235 | if (image->getDims() < 3) { |
| 236 | size_ = image->getRowPitch() * image->getHeight(); |
| 237 | } else { |
| 238 | size_ = image->getSlicePitch() * image->getDepth(); |
| 239 | } |
| 240 | } |
| 241 | } |
| 242 | // Allocate host memory buffer if needed. |
| 243 | // @note: SVM host memory allocation should be done in the device backend |
| 244 | else if (allocHostMem && !isInterop() && !(getMemFlags() & CL_MEM_SVM_FINE_GRAIN_BUFFER)) { |
| 245 | if (!hostMemRef_.allocateMemory(size_, context_())) { |
| 246 | DevLogError("Cannot allocate Host Memory Buffer \n"); |
| 247 | return false; |
| 248 | } |
| 249 | |
| 250 | // Copy data to the backing store if the app has requested |
| 251 | if (((flags_ & CL_MEM_COPY_HOST_PTR) || forceCopy) && (initFrom != NULL)) { |
| 252 | copyToBackingStore(initFrom); |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | if (allocHostMem && type_ == CL_MEM_OBJECT_PIPE) { |
| 257 | // Initialize the pipe for a CPU device |
| 258 | clk_pipe_t* pipe = reinterpret_cast<clk_pipe_t*>(getHostMem()); |
| 259 | pipe->read_idx = 0; |
| 260 | pipe->write_idx = 0; |
| 261 | pipe->end_idx = asPipe()->getMaxNumPackets(); |
no test coverage detected