Thread Composability Manager

1.4.1 · active · verified Sat Apr 11

tcmlib is a Python library by Intel that implements the Thread Composability Manager. It coordinates CPU resource usage between Intel® oneAPI Threading Building Blocks (oneTBB) and Intel® OpenMP to prevent excessive oversubscription when both runtimes are used concurrently within a single process. This helps optimize performance for multi-threaded Python applications, particularly in AI/ML and scientific computing workloads. The library is currently at version 1.4.1 and is actively maintained by Intel.

Warnings

Install

Imports

Quickstart

The primary way to enable the Thread Composability Manager's effects is by setting the `TCM_ENABLE` environment variable to `1` before running your Python application. This allows `tcmlib` to coordinate multi-threading runtimes like oneTBB and OpenMP used by underlying libraries (e.g., NumPy with Intel® oneAPI Math Kernel Library (oneMKL)). The Python package itself provides the necessary hooks and runtime components for this coordination.

import os
import numpy as np
import time

# The Thread Composability Manager is often enabled via an environment variable.
# For demonstration, we'll set it here, but typically it's set before the Python process starts.
# Ensure Intel oneTBB and OpenMP runtimes are installed and configured for optimal effect.

# NOTE: For actual use, set this environment variable BEFORE running your Python script,
# e.g., in bash: export TCM_ENABLE=1 python your_script.py
# Or in a system-wide environment configuration.

os.environ['TCM_ENABLE'] = os.environ.get('TCM_ENABLE', '1') # Set to '1' to enable, '0' to disable if not already set.

print(f"TCM_ENABLE is set to: {os.environ.get('TCM_ENABLE')}")

def heavy_computation(size):
    # Simulate a compute-intensive task that might use OpenMP/oneTBB internally
    start_time = time.time()
    matrix_a = np.random.rand(size, size)
    matrix_b = np.random.rand(size, size)
    result = np.dot(matrix_a, matrix_b)
    end_time = time.time()
    return end_time - start_time

if __name__ == '__main__':
    print("Starting heavy computation...")
    duration = heavy_computation(1000) # Adjust size based on your CPU/memory
    print(f"Computation finished in {duration:.4f} seconds.")
    print("To observe the effects of tcmlib, compare performance with and without TCM_ENABLE=1,")
    print("especially when other Intel-optimized libraries (like NumPy with oneMKL) are also used.")

# Clean up environment variable for subsequent runs in the same shell/notebook session if needed
# del os.environ['TCM_ENABLE']

view raw JSON →