WEAK int halide_device_sync_legacy(void *user_context, struct buffer_t *old_buf) { 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_sync(user_context, &new_buf); err = err || halide_downgrade_buffer_t_device_fields(user_context, "", &new_buf, old_buf); return err; }
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"); } } }
int main(int argc, char** argv) { LOGI("\nvvvv vvvv vvvv"); int width = 128; int height = 128; int channels = 4; int32_t *input = (int32_t*)malloc(width * height * channels * sizeof(int32_t)); int32_t *output = (int32_t*)malloc(width * height * channels * sizeof(int32_t)); LOGI("Allocated memory for %dx%dx%d image", width, height, channels); buffer_t bt_input = make_interleaved_image(width, height, channels, input); for (int i = 0; i < std::min(bt_input.extent[0], width); i++) { for (int j = 0; j < std::min(bt_input.extent[1], height); j++) { for (int k = 0; k < bt_input.extent[2]; k++) { input[i * bt_input.stride[0] + j * bt_input.stride[1] + k * bt_input.stride[2]] = ((i + j) % 2) * 6; } } } LOGI("Input :\n"); print(bt_input); bt_input.host_dirty = true; buffer_t bt_output = make_interleaved_image(width, height, channels, output); two_kernels_filter(&bt_input, &bt_output); LOGI("Filter is done."); halide_device_sync(NULL, &bt_output); LOGI("Sync is done"); halide_copy_to_host(NULL, &bt_output); LOGI("Output :\n"); print(bt_output); int count_mismatches = 0; for (int i = 0; i < bt_output.extent[0]; i++) { for (int j = 0; j < bt_output.extent[1]; j++) { for (int k = 0; k < bt_output.extent[2]; k++) { int32_t output_value = ((int32_t*)bt_output.host)[i * bt_output.stride[0] + j * bt_output.stride[1] + k * bt_output.stride[2]]; int32_t input_value = ((int32_t*)bt_input.host)[i * bt_input.stride[0] + j * bt_input.stride[1] + k * bt_input.stride[2]]; if (output_value != input_value) { if (count_mismatches < 100) { std::ostringstream str; str << "bt_output and bt_input results differ at " << "(" << i << ", " << j << ", " << k << "):" << output_value << " != " << input_value << "\n"; LOGI("%s", str.str().c_str()); } count_mismatches++; } } } } LOGI(count_mismatches == 0? "Test passed.\n": "Test failed.\n"); halide_device_free(NULL, &bt_input); halide_device_free(NULL, &bt_output); halide_device_release(NULL, halide_openglcompute_device_interface()); LOGI("^^^^ ^^^^ ^^^^\n"); }
int Buffer::device_sync() { return halide_device_sync(NULL, raw_buffer()); }