WEAK int halide_device_malloc_legacy(void *user_context, struct buffer_t *old_buf, const struct halide_device_interface_t *device_interface) { halide_buffer_t new_buf = {0}; halide_dimension_t shape[4]; new_buf.dim = shape; int err = guess_type_and_dimensionality(user_context, old_buf, &new_buf); err = err || halide_upgrade_buffer_t(user_context, "", old_buf, &new_buf, /*bounds_query_only*/ 0); err = err || halide_device_malloc(user_context, &new_buf, device_interface); err = err || halide_downgrade_buffer_t_device_fields(user_context, "", &new_buf, old_buf); return err; }
WEAK int halide_default_device_and_host_malloc(void *user_context, struct buffer_t *buf, const halide_device_interface *device_interface) { size_t size = buf_size(buf); buf->host = (uint8_t *)halide_malloc(user_context, size); if (buf->host == NULL) { return -1; } int result = halide_device_malloc(user_context, buf, device_interface); if (result != 0) { halide_free(user_context, buf->host); buf->host = NULL; } return result; }
void test_device_sync() { const int W = 12, H = 32, C = 3; Image temp(W, H, C, sizeof(uint8_t), Image::Planar); int result = halide_device_malloc(nullptr, &temp.buf, halide_opengl_device_interface()); if (result != 0) { fprintf(stderr, "halide_device_malloc failed with return %d.\n", result); } else { result = halide_device_sync(nullptr, &temp.buf); if (result != 0) { fprintf(stderr, "halide_device_sync failed with return %d.\n", result); } else { fprintf(stderr, "Test device sync complete.\n"); } } }
/** Copy image data from host memory to device memory. This should not be * called directly; Halide handles copying to the device automatically. */ WEAK int halide_copy_to_device(void *user_context, struct halide_buffer_t *buf, const halide_device_interface_t *device_interface) { int result = 0; ScopedMutexLock lock(&device_copy_mutex); debug(user_context) << "halide_copy_to_device " << buf << ", host: " << buf->host << ", dev: " << buf->device << ", host_dirty: " << buf->host_dirty() << ", dev_dirty: " << buf->device_dirty() << "\n"; if (device_interface == NULL) { debug(user_context) << "halide_copy_to_device " << buf << " interface is NULL\n"; if (buf->device_interface == NULL) { debug(user_context) << "halide_copy_to_device " << buf << " no interface error\n"; return halide_error_code_no_device_interface; } device_interface = buf->device_interface; } if (buf->device && buf->device_interface != device_interface) { debug(user_context) << "halide_copy_to_device " << buf << " flipping buffer to new device\n"; if (buf->device_interface != NULL && buf->device_dirty()) { halide_assert(user_context, !buf->host_dirty()); result = copy_to_host_already_locked(user_context, buf); if (result != 0) { debug(user_context) << "halide_copy_to_device " << buf << " flipping buffer halide_copy_to_host failed\n"; return result; } } result = halide_device_free(user_context, buf); if (result != 0) { debug(user_context) << "halide_copy_to_device " << buf << " flipping buffer halide_device_free failed\n"; return result; } buf->set_host_dirty(true); // force copy back to new device below. } if (buf->device == 0) { result = halide_device_malloc(user_context, buf, device_interface); if (result != 0) { debug(user_context) << "halide_copy_to_device " << buf << " halide_copy_to_device call to halide_device_malloc failed\n"; return result; } } if (buf->host_dirty()) { debug(user_context) << "halide_copy_to_device " << buf << " host is dirty\n"; if (buf->device_dirty()) { debug(user_context) << "halide_copy_to_device " << buf << " dev_dirty is true error\n"; return halide_error_code_copy_to_device_failed; } else { result = device_interface->copy_to_device(user_context, buf); if (result == 0) { buf->set_host_dirty(false); } else { debug(user_context) << "halide_copy_to_device " << buf << "device copy_to_device returned an error\n"; return halide_error_code_copy_to_device_failed; } } } return 0; }