Skip to content

Commit

Permalink
Add more comments and error checking.
Browse files Browse the repository at this point in the history
  • Loading branch information
dsharletg committed Jul 12, 2017
1 parent 726ab41 commit 90f57ed
Show file tree
Hide file tree
Showing 11 changed files with 39 additions and 19 deletions.
Binary file not shown.
Binary file not shown.
2 changes: 1 addition & 1 deletion src/runtime/hexagon_remote/bin/src/halide_hexagon_remote.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ __QAIC_HEADER_EXPORT int __QAIC_HEADER(halide_hexagon_remote_initialize_kernels_
__QAIC_HEADER_EXPORT int __QAIC_HEADER(halide_hexagon_remote_get_symbol_v4)(halide_hexagon_remote_handle_t module_ptr, const char* name, int nameLen, halide_hexagon_remote_handle_t* sym_ptr) __QAIC_HEADER_ATTRIBUTE;
__QAIC_HEADER_EXPORT int __QAIC_HEADER(halide_hexagon_remote_power_hvx_on)(void) __QAIC_HEADER_ATTRIBUTE;
__QAIC_HEADER_EXPORT int __QAIC_HEADER(halide_hexagon_remote_power_hvx_off)(void) __QAIC_HEADER_ATTRIBUTE;
__QAIC_HEADER_EXPORT int __QAIC_HEADER(halide_hexagon_remote_run_v2)(halide_hexagon_remote_handle_t module_ptr, halide_hexagon_remote_handle_t symbol, const halide_hexagon_remote_buffer* input_buffers, int input_buffersLen, halide_hexagon_remote_buffer* output_buffers, int output_buffersLen, int scalar_count, const unsigned char* small, int smallLen) __QAIC_HEADER_ATTRIBUTE;
__QAIC_HEADER_EXPORT int __QAIC_HEADER(halide_hexagon_remote_run_v2)(halide_hexagon_remote_handle_t module_ptr, halide_hexagon_remote_handle_t symbol, const halide_hexagon_remote_buffer* input_buffers, int input_buffersLen, halide_hexagon_remote_buffer* output_buffers, int output_buffersLen, int scalar_count, const unsigned char* small_input_args, int small_input_argsLen) __QAIC_HEADER_ATTRIBUTE;
__QAIC_HEADER_EXPORT int __QAIC_HEADER(halide_hexagon_remote_release_kernels_v2)(halide_hexagon_remote_handle_t module_ptr) __QAIC_HEADER_ATTRIBUTE;
__QAIC_HEADER_EXPORT int __QAIC_HEADER(halide_hexagon_remote_poll_log)(char* log, int logLen, int* read_size) __QAIC_HEADER_ATTRIBUTE;
__QAIC_HEADER_EXPORT int __QAIC_HEADER(halide_hexagon_remote_poll_profiler_state)(int* func, int* threads) __QAIC_HEADER_ATTRIBUTE;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -425,8 +425,8 @@ static const Parameter parameters[8] = {{SLIM_IFPTR32(0x8,0x10),{{(const uintptr
static const Parameter* const parameterArrays[21] = {(&(parameters[5])),(&(parameters[2])),(&(parameters[2])),(&(parameters[5])),(&(parameters[2])),(&(parameters[2])),(&(parameters[5])),(&(parameters[5])),(&(parameters[2])),(&(parameters[2])),(&(parameters[3])),(&(parameters[4])),(&(parameters[5])),(&(parameters[0])),(&(parameters[2])),(&(parameters[0])),(&(parameters[1])),(&(parameters[7])),(&(parameters[7])),(&(parameters[6])),(&(parameters[7]))};
static const Method methods[9] = {{REMOTE_SCALARS_MAKEX(0,0,0x2,0x1,0x0,0x0),0x4,0x4,3,2,(&(parameterArrays[15])),0x4,0x4},{REMOTE_SCALARS_MAKEX(0,0,0x2,0x1,0x0,0x0),0x8,0x4,4,3,(&(parameterArrays[14])),0x4,0x4},{REMOTE_SCALARS_MAKEX(0,0,0x0,0x0,0x0,0x0),0x0,0x0,0,0,0,0x0,0x0},{REMOTE_SCALARS_MAKEX(0,0,255,255,15,15),0x18,0x0,10,6,(&(parameterArrays[8])),0x4,0x1},{REMOTE_SCALARS_MAKEX(0,0,0x1,0x0,0x0,0x0),0x4,0x0,1,1,(&(parameterArrays[1])),0x4,0x0},{REMOTE_SCALARS_MAKEX(0,0,0x1,0x2,0x0,0x0),0x4,0x4,4,2,(&(parameterArrays[19])),0x4,0x4},{REMOTE_SCALARS_MAKEX(0,0,0x0,0x1,0x0,0x0),0x0,0x8,2,2,(&(parameterArrays[17])),0x1,0x4},{REMOTE_SCALARS_MAKEX(0,0,0x1,0x0,0x0,0x0),0x4,0x0,1,1,(&(parameterArrays[0])),0x4,0x0},{REMOTE_SCALARS_MAKEX(0,0,0x1,0x0,0x0,0x0),0x20,0x0,8,8,(&(parameterArrays[0])),0x4,0x0}};
static const Method* const methodArrays[10] = {&(methods[0]),&(methods[1]),&(methods[2]),&(methods[2]),&(methods[3]),&(methods[4]),&(methods[5]),&(methods[6]),&(methods[7]),&(methods[8])};
static const char strings[357] = "initialize_kernels_v3\0busbwUsagePercentage\0set_performance_mode\0poll_profiler_state\0release_kernels_v2\0bwMegabytesPerSec\0set_performance\0output_buffers\0mipsPerThread\0input_buffers\0power_hvx_off\0get_symbol_v4\0scalar_count\0power_hvx_on\0set_latency\0set_bus_bw\0module_ptr\0mipsTotal\0read_size\0set_mips\0poll_log\0threads\0sym_ptr\0symbol\0run_v2\0small\0func\0name\0code\0";
static const uint16_t methodStrings[35] = {121,288,152,268,246,103,22,234,238,329,257,322,166,137,208,336,194,257,347,314,64,342,306,297,302,278,0,352,257,43,59,84,257,180,221};
static const char strings[368] = "initialize_kernels_v3\0busbwUsagePercentage\0set_performance_mode\0poll_profiler_state\0release_kernels_v2\0bwMegabytesPerSec\0small_input_args\0set_performance\0output_buffers\0mipsPerThread\0input_buffers\0power_hvx_off\0get_symbol_v4\0scalar_count\0power_hvx_on\0set_latency\0set_bus_bw\0module_ptr\0mipsTotal\0read_size\0set_mips\0poll_log\0threads\0sym_ptr\0symbol\0run_v2\0func\0name\0code\0";
static const uint16_t methodStrings[35] = {138,305,169,285,263,103,22,251,255,346,274,339,183,154,225,121,211,274,358,331,64,353,323,314,319,295,0,363,274,43,59,84,274,197,238};
static const uint16_t methodStringsArrays[10] = {26,16,34,33,9,31,23,20,29,0};
__QAIC_SLIM_EXPORT const Interface __QAIC_SLIM(halide_hexagon_remote_slim) = {10,&(methodArrays[0]),0,0,&(methodStringsArrays [0]),methodStrings,strings};
#endif //_HALIDE_HEXAGON_REMOTE_SLIM_H
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -425,8 +425,8 @@ static const Parameter parameters[8] = {{SLIM_IFPTR32(0x8,0x10),{{(const uintptr
static const Parameter* const parameterArrays[21] = {(&(parameters[5])),(&(parameters[2])),(&(parameters[2])),(&(parameters[5])),(&(parameters[2])),(&(parameters[2])),(&(parameters[5])),(&(parameters[5])),(&(parameters[2])),(&(parameters[2])),(&(parameters[3])),(&(parameters[4])),(&(parameters[5])),(&(parameters[0])),(&(parameters[2])),(&(parameters[0])),(&(parameters[1])),(&(parameters[7])),(&(parameters[7])),(&(parameters[6])),(&(parameters[7]))};
static const Method methods[9] = {{REMOTE_SCALARS_MAKEX(0,0,0x2,0x1,0x0,0x0),0x4,0x4,3,2,(&(parameterArrays[15])),0x4,0x4},{REMOTE_SCALARS_MAKEX(0,0,0x2,0x1,0x0,0x0),0x8,0x4,4,3,(&(parameterArrays[14])),0x4,0x4},{REMOTE_SCALARS_MAKEX(0,0,0x0,0x0,0x0,0x0),0x0,0x0,0,0,0,0x0,0x0},{REMOTE_SCALARS_MAKEX(0,0,255,255,15,15),0x18,0x0,10,6,(&(parameterArrays[8])),0x4,0x1},{REMOTE_SCALARS_MAKEX(0,0,0x1,0x0,0x0,0x0),0x4,0x0,1,1,(&(parameterArrays[1])),0x4,0x0},{REMOTE_SCALARS_MAKEX(0,0,0x1,0x2,0x0,0x0),0x4,0x4,4,2,(&(parameterArrays[19])),0x4,0x4},{REMOTE_SCALARS_MAKEX(0,0,0x0,0x1,0x0,0x0),0x0,0x8,2,2,(&(parameterArrays[17])),0x1,0x4},{REMOTE_SCALARS_MAKEX(0,0,0x1,0x0,0x0,0x0),0x4,0x0,1,1,(&(parameterArrays[0])),0x4,0x0},{REMOTE_SCALARS_MAKEX(0,0,0x1,0x0,0x0,0x0),0x20,0x0,8,8,(&(parameterArrays[0])),0x4,0x0}};
static const Method* const methodArrays[10] = {&(methods[0]),&(methods[1]),&(methods[2]),&(methods[2]),&(methods[3]),&(methods[4]),&(methods[5]),&(methods[6]),&(methods[7]),&(methods[8])};
static const char strings[357] = "initialize_kernels_v3\0busbwUsagePercentage\0set_performance_mode\0poll_profiler_state\0release_kernels_v2\0bwMegabytesPerSec\0set_performance\0output_buffers\0mipsPerThread\0input_buffers\0power_hvx_off\0get_symbol_v4\0scalar_count\0power_hvx_on\0set_latency\0set_bus_bw\0module_ptr\0mipsTotal\0read_size\0set_mips\0poll_log\0threads\0sym_ptr\0symbol\0run_v2\0small\0func\0name\0code\0";
static const uint16_t methodStrings[35] = {121,288,152,268,246,103,22,234,238,329,257,322,166,137,208,336,194,257,347,314,64,342,306,297,302,278,0,352,257,43,59,84,257,180,221};
static const char strings[368] = "initialize_kernels_v3\0busbwUsagePercentage\0set_performance_mode\0poll_profiler_state\0release_kernels_v2\0bwMegabytesPerSec\0small_input_args\0set_performance\0output_buffers\0mipsPerThread\0input_buffers\0power_hvx_off\0get_symbol_v4\0scalar_count\0power_hvx_on\0set_latency\0set_bus_bw\0module_ptr\0mipsTotal\0read_size\0set_mips\0poll_log\0threads\0sym_ptr\0symbol\0run_v2\0func\0name\0code\0";
static const uint16_t methodStrings[35] = {138,305,169,285,263,103,22,251,255,346,274,339,183,154,225,121,211,274,358,331,64,353,323,314,319,295,0,363,274,43,59,84,274,197,238};
static const uint16_t methodStringsArrays[10] = {26,16,34,33,9,31,23,20,29,0};
__QAIC_SLIM_EXPORT const Interface __QAIC_SLIM(halide_hexagon_remote_slim) = {10,&(methodArrays[0]),0,0,&(methodStringsArrays [0]),methodStrings,strings};
#endif //_HALIDE_HEXAGON_REMOTE_SLIM_H
Expand Down Expand Up @@ -714,9 +714,9 @@ static __inline int _stub_method_3(remote_handle _handle, uint32_t _mid, uint32_
_allocator_deinit(_al);
return _nErr;
}
__QAIC_STUB_EXPORT int __QAIC_STUB(halide_hexagon_remote_run_v2)(halide_hexagon_remote_handle_t module_ptr, halide_hexagon_remote_handle_t symbol, const halide_hexagon_remote_buffer* input_buffers, int input_buffersLen, halide_hexagon_remote_buffer* output_buffers, int output_buffersLen, int scalar_count, const unsigned char* small, int smallLen) __QAIC_STUB_ATTRIBUTE {
__QAIC_STUB_EXPORT int __QAIC_STUB(halide_hexagon_remote_run_v2)(halide_hexagon_remote_handle_t module_ptr, halide_hexagon_remote_handle_t symbol, const halide_hexagon_remote_buffer* input_buffers, int input_buffersLen, halide_hexagon_remote_buffer* output_buffers, int output_buffersLen, int scalar_count, const unsigned char* small_input_args, int small_input_argsLen) __QAIC_STUB_ATTRIBUTE {
uint32_t _mid = 4;
return _stub_method_3(_halide_hexagon_remote_handle(), _mid, (uint32_t*)&module_ptr, (uint32_t*)&symbol, (void**)&input_buffers, (uint32_t*)&input_buffersLen, (void**)&output_buffers, (uint32_t*)&output_buffersLen, (uint32_t*)&scalar_count, (char**)&small, (uint32_t*)&smallLen);
return _stub_method_3(_halide_hexagon_remote_handle(), _mid, (uint32_t*)&module_ptr, (uint32_t*)&symbol, (void**)&input_buffers, (uint32_t*)&input_buffersLen, (void**)&output_buffers, (uint32_t*)&output_buffersLen, (uint32_t*)&scalar_count, (char**)&small_input_args, (uint32_t*)&small_input_argsLen);
}
static __inline int _stub_method_4(remote_handle _handle, uint32_t _mid, uint32_t _in0[1]) {
remote_arg _pra[1];
Expand Down
Binary file not shown.
Binary file not shown.
2 changes: 1 addition & 1 deletion src/runtime/hexagon_remote/halide_hexagon_remote.idl
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ interface halide_hexagon_remote
long run_v2(in handle_t module_ptr, in handle_t symbol,
in sequence<buffer> input_buffers,
rout sequence<buffer> output_buffers,
in long scalar_count, in buffer small);
in long scalar_count, in buffer small_input_args);

// Routine to clean up a module on the remote side.
long release_kernels_v2(in handle_t module_ptr);
Expand Down
19 changes: 16 additions & 3 deletions src/runtime/hexagon_remote/halide_remote.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,8 @@ int halide_hexagon_remote_get_symbol_v4(handle_t module_ptr, const char* name, i
int halide_hexagon_remote_run_v2(handle_t module_ptr, handle_t function,
const buffer *input_buffersPtrs, int input_buffersLen,
buffer *output_buffersPtrs, int output_buffersLen,
int scalar_count, const unsigned char *small, int smallLen) {
int scalar_count,
const unsigned char *small_input_args, int small_input_argsLen) {
// Get a pointer to the argv version of the pipeline.
pipeline_argv_t pipeline = reinterpret_cast<pipeline_argv_t>(function);

Expand All @@ -332,6 +333,8 @@ int halide_hexagon_remote_run_v2(handle_t module_ptr, handle_t function,
void **args = (void **)__builtin_alloca((input_buffersLen + scalar_count + output_buffersLen) * sizeof(void *));
buffer_t *buffers = (buffer_t *)__builtin_alloca((input_buffersLen + output_buffersLen) * sizeof(buffer_t));

const unsigned char *small_input_args_end = small_input_args + small_input_argsLen;

void **next_arg = &args[0];
buffer_t *next_buffer_t = &buffers[0];
// Input buffers come first.
Expand All @@ -340,7 +343,12 @@ int halide_hexagon_remote_run_v2(handle_t module_ptr, handle_t function,
// This buffer is passed directly.
next_buffer_t->host = input_buffersPtrs[i].data;
} else {
next_buffer_t->host = const_cast<unsigned char *>(read_buffer(small));
next_buffer_t->host = const_cast<unsigned char *>(read_buffer(small_input_args));
if (small_input_args > small_input_args_end) {
log_printf("Input buffer %d read past the end of small_input_args [%p, %p)\n",
i, small_input_args - small_input_args_end);
return halide_error_code_access_out_of_bounds;
}
}
*next_arg = next_buffer_t;
}
Expand All @@ -352,7 +360,12 @@ int halide_hexagon_remote_run_v2(handle_t module_ptr, handle_t function,
}
// Input scalars are last.
for (int i = 0; i < scalar_count; i++, next_arg++) {
*next_arg = const_cast<unsigned char *>(read_buffer(small));
*next_arg = const_cast<unsigned char *>(read_buffer(small_input_args));
if (small_input_args > small_input_args_end) {
log_printf("Input scalar %d read past the end of small_input_args by %d bytes\n",
i, small_input_args - small_input_args_end);
return halide_error_code_access_out_of_bounds;
}
}

// Prior to running the pipeline, power HVX on (if it was not already on).
Expand Down
18 changes: 10 additions & 8 deletions src/runtime/hexagon_remote/host_shim.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,29 @@ int halide_hexagon_remote_run(handle_t module_ptr, handle_t function,
const int buffer_alignment = 128;
const int scalar_alignment = 4;

int small_size = 0;
int small_input_args_size = 0;

// Add up the number and size of the small input buffers.
// Add up the number and size of the small input buffers. Buffers
// are considered small if they are less than
// min_input_buffer_size bytes.
const int min_input_buffer_size = 4096;
for (int i = 0; i < input_buffersLen; i++) {
if (input_buffersPtrs[i].dataLen < min_input_buffer_size) {
small_size += packed_buffer_size(input_buffersPtrs[i].dataLen, buffer_alignment);
small_input_args_size += packed_buffer_size(input_buffersPtrs[i].dataLen, buffer_alignment);
}
}

// Add up the number and size of the scalars.
for (int i = 0; i < input_scalarsLen; i++) {
small_size += packed_buffer_size(input_scalarsPtrs[i].dataLen, scalar_alignment);
small_input_args_size += packed_buffer_size(input_scalarsPtrs[i].dataLen, scalar_alignment);
}

// Allocate a buffer to hold the packed small buffers and scalars.
unsigned char *small = (unsigned char *)__builtin_alloca(small_size + buffer_alignment);
small = (unsigned char *)(((uintptr_t)small + buffer_alignment - 1) & ~(buffer_alignment - 1));
unsigned char *small_input_args = (unsigned char *)__builtin_alloca(small_input_args_size + buffer_alignment);
small_input_args = (unsigned char *)(((uintptr_t)small_input_args + buffer_alignment - 1) & ~(buffer_alignment - 1));

// Pack up the small buffers.
unsigned char *write = small;
unsigned char *write = small_input_args;
for (int i = 0; i < input_buffersLen; i++) {
if (input_buffersPtrs[i].dataLen < min_input_buffer_size) {
write_buffer(write, input_buffersPtrs[i].data, input_buffersPtrs[i].dataLen, buffer_alignment);
Expand All @@ -56,7 +58,7 @@ int halide_hexagon_remote_run(handle_t module_ptr, handle_t function,
return halide_hexagon_remote_run_v2(module_ptr, function,
input_buffersPtrs, input_buffersLen,
output_buffersPtrs, output_buffersLen,
input_scalarsLen, small, small_size);
input_scalarsLen, small_input_args, small_input_args_size);
}

} // extern "C"
5 changes: 5 additions & 0 deletions src/runtime/hexagon_remote/pack_buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@

#include <stdint.h>

// This header implements a simple buffer packing protocol for packing
// and unpacking scalars and buffers into a larger buffer.

template <typename T>
void write_scalar(unsigned char *&write, const T& value) {
*(T *)write = value;
Expand Down Expand Up @@ -34,6 +37,8 @@ inline uint32_t aligned_offset(const unsigned char *start, uint32_t offset, uint
return x - (uintptr_t)start;
}

// Buffers are stored with two offsets, indicating the start and end
// of the buffer from the start.
inline void write_buffer(unsigned char *&write, const unsigned char *buffer, uint32_t size, uint32_t alignment) {
unsigned char *start = write;
uint32_t header_size = sizeof(uint32_t) * 2;
Expand Down

0 comments on commit 90f57ed

Please sign in to comment.