Implementation:NVIDIA DALI C API V2 Init
| Knowledge Sources | |
|---|---|
| Domains | Data_Pipeline, C_API |
| Last Updated | 2026-02-08 16:00 GMT |
Overview
The C API v2 init module provides DALI library initialization and shutdown functions with reference-counted lifecycle management and auto-initialization support.
Description
This file implements the daliInit and daliShutdown functions for the DALI C API v2, along with an internal CheckInit helper used by other API functions to ensure DALI is initialized before use. It belongs to the dali::c_api namespace within the NVIDIA DALI library.
The initialization is implemented using a static lambda pattern that ensures DALIInit (configuring CPU, pinned CPU, and GPU allocators) is called exactly once regardless of how many times daliInit is invoked. An atomic counter g_init_count tracks the number of active initialization references, and an atomic boolean g_was_initialized records whether DALI was ever initialized. This allows the CheckInit helper to distinguish between "never initialized" (triggering auto-initialization) and "was initialized but shutdown was called" (returning DALI_ERROR_UNLOADING).
The daliShutdown function decrements the initialization counter and, when it reaches zero, would execute actual shutdown code (currently a placeholder). If called more times than daliInit, it restores the counter and returns DALI_ERROR_UNLOADING. Error handling in daliInit uses direct try/catch since the standard DALI_PROLOG macro cannot be used before DALI is initialized.
Usage
Call daliInit() at application startup before using any other DALI C API v2 functions. Call daliShutdown() when done to release DALI resources. Most API functions call CheckInit internally, so explicit initialization is optional but recommended for controlling the initialization timing.
Code Reference
Source Location
- Repository: NVIDIA_DALI
- File: dali/c_api_2/init.cc
- Lines: 1-68
Signature
// Public C API
daliResult_t daliInit();
daliResult_t daliShutdown();
// Internal helper (used by DALI_PROLOG macro)
namespace dali::c_api {
daliResult_t CheckInit();
}
Import
#include "dali/dali.h"
#include "dali/c_api_2/error_handling.h"
#include "dali/pipeline/init.h"
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| (none) | - | - | daliInit takes no parameters |
Outputs
| Name | Type | Description |
|---|---|---|
| daliResult_t | enum | DALI_SUCCESS on successful init/shutdown, DALI_ERROR_UNLOADING if shutdown was already called |
Usage Examples
Basic Init/Shutdown Lifecycle
#include "dali/dali.h"
int main() {
// Initialize DALI
daliResult_t r = daliInit();
if (r != DALI_SUCCESS) {
fprintf(stderr, "Failed to initialize DALI: %s\n", daliGetLastErrorMessage());
return 1;
}
// ... use DALI C API v2 functions ...
// Shutdown DALI
r = daliShutdown();
if (r != DALI_SUCCESS) {
fprintf(stderr, "Failed to shutdown DALI: %s\n", daliGetLastErrorMessage());
}
return 0;
}
Auto-Initialization via CheckInit
// Internal: CheckInit is called by DALI_PROLOG in every C API v2 function.
// If daliInit() was never called, CheckInit will call it automatically.
// If daliShutdown() was called, CheckInit returns DALI_ERROR_UNLOADING.
// Users do not need to call daliInit() explicitly if they are OK with
// auto-initialization on first API call.
daliPipeline_h pipeline = nullptr;
daliPipelineParams_t params{};
// This will auto-initialize DALI if needed:
daliResult_t r = daliPipelineCreate(&pipeline, ¶ms);