This source file includes following definitions.
- halide_host_cpu_count
- spawn_thread_helper
- halide_spawn_thread
- halide_join_thread
- halide_mutex_lock
- halide_mutex_unlock
- halide_mutex_destroy
- halide_cond_init
- halide_cond_destroy
- halide_cond_broadcast
- halide_cond_wait
- halide_do_par_for
- halide_do_task
#include <HalideRuntime.h>
#include <qurt.h>
#include <stdlib.h>
struct halide_thread {
qurt_thread_t val;
};
int halide_host_cpu_count() {
return 4;
}
namespace {
struct spawned_thread {
void (*f)(void *);
void *closure;
void *stack;
halide_thread handle;
};
void spawn_thread_helper(void *arg) {
spawned_thread *t = (spawned_thread *)arg;
t->f(t->closure);
}
}
#define STACK_SIZE 256*1024
struct halide_thread *halide_spawn_thread(void (*f)(void *), void *closure) {
spawned_thread *t = (spawned_thread *)malloc(sizeof(spawned_thread));
t->f = f;
t->closure = closure;
t->stack = memalign(128, STACK_SIZE);
memset(&t->handle, 0, sizeof(t->handle));
qurt_thread_attr_t thread_attr;
qurt_thread_attr_init(&thread_attr);
qurt_thread_attr_set_stack_addr(&thread_attr, t->stack);
qurt_thread_attr_set_stack_size(&thread_attr, STACK_SIZE);
qurt_thread_attr_set_priority(&thread_attr, 255);
qurt_thread_create(&t->handle.val, &thread_attr, &spawn_thread_helper, t);
return (halide_thread *)t;
}
void halide_join_thread(struct halide_thread *thread_arg) {
spawned_thread *t = (spawned_thread *)thread_arg;
int ret = 0;
qurt_thread_join(t->handle.val, &ret);
free(t->stack);
free(t);
}
void halide_mutex_lock(halide_mutex *mutex) {
qurt_mutex_lock((qurt_mutex_t *)mutex);
}
void halide_mutex_unlock(halide_mutex *mutex) {
qurt_mutex_unlock((qurt_mutex_t *)mutex);
}
void halide_mutex_destroy(halide_mutex *mutex) {
qurt_mutex_destroy((qurt_mutex_t *)mutex);
memset(mutex, 0, sizeof(halide_mutex));
}
struct halide_cond {
uint64_t _private[8];
};
void halide_cond_init(struct halide_cond *cond) {
qurt_cond_init((qurt_cond_t *)cond);
}
void halide_cond_destroy(struct halide_cond *cond) {
qurt_cond_destroy((qurt_cond_t *)cond);
}
void halide_cond_broadcast(struct halide_cond *cond) {
qurt_cond_broadcast((qurt_cond_t *)cond);
}
void halide_cond_wait(struct halide_cond *cond, struct halide_mutex *mutex) {
qurt_cond_wait((qurt_cond_t *)cond, (qurt_mutex_t *)mutex);
}
#define WEAK
#include "../thread_pool_common.h"
namespace {
struct wrapped_closure {
uint8_t *closure;
int hvx_mode;
};
}
extern "C" {
int halide_do_par_for(void *user_context,
halide_task_t task,
int min, int size, uint8_t *closure) {
qurt_mutex_t *mutex = (qurt_mutex_t *)(&work_queue.mutex);
if (!work_queue.initialized) {
qurt_mutex_init(mutex);
}
wrapped_closure c = {closure, qurt_hvx_get_mode()};
int old_num_threads =
halide_set_num_threads((c.hvx_mode == QURT_HVX_MODE_128B) ? 2 : 4);
if (c.hvx_mode != -1) {
if (qurt_hvx_unlock() != QURT_EOK) {
c.hvx_mode = -1;
}
}
int ret = Halide::Runtime::Internal::default_do_par_for(user_context, task, min, size, (uint8_t *)&c);
if (c.hvx_mode != -1) {
qurt_hvx_lock((qurt_hvx_mode_t)c.hvx_mode);
}
halide_set_num_threads(old_num_threads);
return ret;
}
int halide_do_task(void *user_context, halide_task_t f,
int idx, uint8_t *closure) {
wrapped_closure *c = (wrapped_closure *)closure;
if (c->hvx_mode != -1) {
qurt_hvx_lock((qurt_hvx_mode_t)c->hvx_mode);
int ret = f(user_context, idx, c->closure);
qurt_hvx_unlock();
return ret;
} else {
return f(user_context, idx, c->closure);
}
}
}