Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Tencent Ncnn SimpleOMP

From Leeroopedia
Revision as of 16:50, 16 February 2026 by Admin (talk | contribs) (Auto-imported from implementations/Tencent_Ncnn_SimpleOMP.md)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)


Knowledge Sources
Domains Concurrency, Embedded Systems
Last Updated 2026-02-09 19:00 GMT

Overview

Implements a minimal OpenMP runtime replacement that supports the #pragma omp parallel for pattern used throughout ncnn, compatible with both LLVM (libomp) and GCC (libgomp) compiler ABIs.

Description

The simpleomp module consists of a header (simpleomp.h, 42 lines) declaring the public API and an implementation (simpleomp.cpp, 792 lines) providing the thread pool and OpenMP ABI functions. The module is conditional on the NCNN_SIMPLEOMP compile flag.

The implementation builds a thread pool system using ncnn's own Thread, Mutex, and ConditionVariable primitives:

  • KMPGlobal: Manages a persistent pool of worker threads and a KMPTaskQueue (a bounded circular buffer for dispatching work).
  • KMPTask: Describes a unit of parallel work, including the microtask function pointer, argument count, arguments, thread number, and completion signaling.
  • KMPTaskQueue: A thread-safe bounded circular buffer that supports batch dispatch and blocking get operations.

When the compiler-generated OpenMP fork call is invoked (__kmpc_fork_call for Clang or GOMP_parallel for GCC), the runtime creates KMPTask objects and dispatches them to the thread pool. Worker threads pick tasks from the queue, execute the microtask function, and signal completion.

For Clang, the implementation handles up to 31 captured variables via typed function pointer casts (kmpc_micro_0 through kmpc_micro_31). Thread-local storage tracks the current thread's ID within a parallel region. The runtime also implements __kmpc_for_static_init / __kmpc_for_static_fini for static loop scheduling and __kmpc_barrier for barrier synchronization.

The header declares standard OpenMP API functions: omp_get_max_threads(), omp_set_num_threads(), omp_get_dynamic(), omp_set_dynamic(), omp_get_num_threads(), omp_get_thread_num(), and Intel KMP extensions kmp_get_blocktime() and kmp_set_blocktime().

Usage

This module is used automatically when building ncnn with NCNN_SIMPLEOMP=ON on platforms lacking a system OpenMP runtime (e.g., some Android NDK configurations or custom toolchains). Standard #pragma omp parallel for annotations in ncnn layer code work transparently with this backend.

Code Reference

Source Location

Signature

// Public OpenMP-compatible API
int omp_get_max_threads();
void omp_set_num_threads(int num_threads);
int omp_get_dynamic();
void omp_set_dynamic(int dynamic);
int omp_get_num_threads();
int omp_get_thread_num();

// Intel KMP extensions
int kmp_get_blocktime();
void kmp_set_blocktime(int blocktime);

Import

#include "simpleomp.h"

I/O Contract

Inputs

Name Type Required Description
num_threads int For omp_set_num_threads Number of threads to use for parallel regions
dynamic int For omp_set_dynamic Enable (1) or disable (0) dynamic thread adjustment
blocktime int For kmp_set_blocktime Time in ms threads wait before sleeping after parallel region

Outputs

Name Type Description
return (omp_get_max_threads) int Maximum number of threads available
return (omp_get_num_threads) int Number of threads in the current parallel region
return (omp_get_thread_num) int Thread ID of the calling thread (0-based)
return (omp_get_dynamic) int Whether dynamic thread adjustment is enabled
return (kmp_get_blocktime) int Current blocktime value in milliseconds

Usage Examples

Controlling Thread Count

#include "simpleomp.h"

// Set the number of threads for parallel regions
omp_set_num_threads(4);

// Query current settings
int max_threads = omp_get_max_threads();

Transparent Usage in Layer Code

// Layer implementations use standard OpenMP pragmas.
// simpleomp intercepts the compiler-generated ABI calls.
#pragma omp parallel for num_threads(opt.num_threads)
for (int i = 0; i < channels; i++)
{
    // per-channel computation
}

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment