Skip to content

Commit

Permalink
Create abstraction for pthread_once()/InitOnceExecuteOnce() calls to …
Browse files Browse the repository at this point in the history
…avoid boilerplate code in many places and also to make other implementations possible.

Some (e.g.) DSP targets don't have a working version of pthreads, so we can't rely on `pthreads_once()`. This provides a single bottleneck we can use to customize for those targets (rather than dozens spread across dozens of files), and makes the code pleasantly terser.

Note that this change should be functionally a no-op, just code refactoring to make upcoming changes more seamless.

PiperOrigin-RevId: 659672037
  • Loading branch information
xnnpack-bot committed Aug 6, 2024
1 parent c4a28da commit f1f7e46
Show file tree
Hide file tree
Showing 32 changed files with 374 additions and 2,101 deletions.
9 changes: 9 additions & 0 deletions BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,13 @@ xnnpack_cc_library(
],
)

xnnpack_cc_library(
name = "init_once",
hdrs = ["src/xnnpack/init-once.h"],
gcc_copts = xnnpack_gcc_std_copts(),
msvc_copts = xnnpack_msvc_std_copts(),
)

xnnpack_cc_library(
name = "microparams",
hdrs = [
Expand Down Expand Up @@ -420,6 +427,7 @@ xnnpack_cc_library(
msvc_copts = xnnpack_msvc_std_copts(),
deps = [
":common",
":init_once",
":logging",
] + select({
":cpuinfo_enabled": ["@cpuinfo"],
Expand Down Expand Up @@ -498,6 +506,7 @@ xnnpack_cc_library(
":enable_jit",
":experiments_config",
":hardware_config",
":init_once",
":logging",
":microkernels_h",
":microparams_init",
Expand Down
25 changes: 3 additions & 22 deletions src/configs/argmaxpool-config.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,16 @@
#include <assert.h>
#include <stddef.h>

#ifdef _WIN32
#include <windows.h>
#else
#include <pthread.h>
#endif

#include "xnnpack/argmaxpool.h"
#include "xnnpack/common.h"
#include "xnnpack/config.h"
#include "xnnpack/init-once.h"
#include "xnnpack/microfnptr.h"

static struct xnn_argmaxpool_config f32_argmaxpool_config[XNN_MAX_F32_ARGMAXPOOL_UKERNELS ] = {0};

#if XNN_PLATFORM_WINDOWS
static INIT_ONCE init_guard_f32_argmaxpool = INIT_ONCE_STATIC_INIT;
#else
static pthread_once_t init_guard_f32_argmaxpool = PTHREAD_ONCE_INIT;
#endif
XNN_INIT_ONCE_GUARD(f32_argmaxpool);

static void init_f32_argmaxpool_config(void) {
#if XNN_ARCH_ARM
Expand Down Expand Up @@ -117,22 +109,11 @@ static void init_f32_argmaxpool_config(void) {
#endif
}

#if XNN_PLATFORM_WINDOWS
static BOOL CALLBACK init_f32_argmaxpool_config_windows(PINIT_ONCE init_once, PVOID parameter, PVOID* context) {
init_f32_argmaxpool_config();
return TRUE;
}
#endif

const struct xnn_argmaxpool_config* xnn_init_f32_argmaxpool_config() {
const struct xnn_hardware_config* hardware_config = xnn_init_hardware_config();
if (hardware_config == NULL) {
return NULL;
}
#if XNN_PLATFORM_WINDOWS
InitOnceExecuteOnce(&init_guard_f32_argmaxpool, &init_f32_argmaxpool_config_windows, NULL, NULL);
#else
pthread_once(&init_guard_f32_argmaxpool, &init_f32_argmaxpool_config);
#endif
XNN_INIT_ONCE(f32_argmaxpool);
return f32_argmaxpool_config;
}
54 changes: 7 additions & 47 deletions src/configs/avgpool-config.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,20 @@
#include <assert.h>
#include <stddef.h>

#ifdef _WIN32
#include <windows.h>
#else
#include <pthread.h>
#endif

#include "xnnpack/avgpool.h"
#include "xnnpack/common.h"
#include "xnnpack/config.h"
#include "xnnpack/init-once.h"
#include "xnnpack/microfnptr.h"
#include "xnnpack/microparams-init.h"

static struct xnn_avgpool_config f16_avgpool_config = {0};
static struct xnn_avgpool_config f32_avgpool_config = {0};
static struct xnn_avgpool_config qu8_avgpool_config = {0};

#if XNN_PLATFORM_WINDOWS
static INIT_ONCE init_guard_f16_avgpool = INIT_ONCE_STATIC_INIT;
static INIT_ONCE init_guard_f32_avgpool = INIT_ONCE_STATIC_INIT;
static INIT_ONCE init_guard_qu8_avgpool = INIT_ONCE_STATIC_INIT;
#else
static pthread_once_t init_guard_f16_avgpool = PTHREAD_ONCE_INIT;
static pthread_once_t init_guard_f32_avgpool = PTHREAD_ONCE_INIT;
static pthread_once_t init_guard_qu8_avgpool = PTHREAD_ONCE_INIT;
#endif
XNN_INIT_ONCE_GUARD(f16_avgpool);
XNN_INIT_ONCE_GUARD(f32_avgpool);
XNN_INIT_ONCE_GUARD(qu8_avgpool);

static void init_f16_avgpool_config(void) {
#if XNN_ARCH_ARM && XNN_ENABLE_ARM_FP16_VECTOR && XNN_ENABLE_ARM_FP16_SCALAR
Expand Down Expand Up @@ -187,33 +176,12 @@ static void init_qu8_avgpool_config(void) {
#endif
}

#if XNN_PLATFORM_WINDOWS
static BOOL CALLBACK init_f16_avgpool_config_windows(PINIT_ONCE init_once, PVOID parameter, PVOID* context) {
init_f16_avgpool_config();
return TRUE;
}

static BOOL CALLBACK init_f32_avgpool_config_windows(PINIT_ONCE init_once, PVOID parameter, PVOID* context) {
init_f32_avgpool_config();
return TRUE;
}

static BOOL CALLBACK init_qu8_avgpool_config_windows(PINIT_ONCE init_once, PVOID parameter, PVOID* context) {
init_qu8_avgpool_config();
return TRUE;
}
#endif

const struct xnn_avgpool_config* xnn_init_f16_avgpool_config() {
const struct xnn_hardware_config* hardware_config = xnn_init_hardware_config();
if (hardware_config == NULL || !xnn_is_f16_compatible_config(hardware_config)) {
return NULL;
}
#if XNN_PLATFORM_WINDOWS
InitOnceExecuteOnce(&init_guard_f16_avgpool, &init_f16_avgpool_config_windows, NULL, NULL);
#else
pthread_once(&init_guard_f16_avgpool, &init_f16_avgpool_config);
#endif
XNN_INIT_ONCE(f16_avgpool);
return &f16_avgpool_config;
}

Expand All @@ -222,11 +190,7 @@ const struct xnn_avgpool_config* xnn_init_f32_avgpool_config() {
if (hardware_config == NULL) {
return NULL;
}
#if XNN_PLATFORM_WINDOWS
InitOnceExecuteOnce(&init_guard_f32_avgpool, &init_f32_avgpool_config_windows, NULL, NULL);
#else
pthread_once(&init_guard_f32_avgpool, &init_f32_avgpool_config);
#endif
XNN_INIT_ONCE(f32_avgpool);
return &f32_avgpool_config;
}

Expand All @@ -235,10 +199,6 @@ const struct xnn_avgpool_config* xnn_init_qu8_avgpool_config() {
if (hardware_config == NULL) {
return NULL;
}
#if XNN_PLATFORM_WINDOWS
InitOnceExecuteOnce(&init_guard_qu8_avgpool, &init_qu8_avgpool_config_windows, NULL, NULL);
#else
pthread_once(&init_guard_qu8_avgpool, &init_qu8_avgpool_config);
#endif
XNN_INIT_ONCE(qu8_avgpool);
return &qu8_avgpool_config;
}
Loading

0 comments on commit f1f7e46

Please sign in to comment.