Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integrating OpenMP #212

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion backend/driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import sysconfig

from pathlib import Path
from functools import lru_cache

from triton.runtime.cache import get_cache_manager
from triton.backends.driver import DriverBase
Expand Down Expand Up @@ -55,6 +56,17 @@ def _format_of(ty):
"uint64_t": "K",
}[ty]

@lru_cache(maxsize=1)
def _openmp_available():
result = subprocess.run(["ldconfig", "-p"], stdout=subprocess.PIPE, text=True)
if result.returncode != 0:
return False
if "libomp.so" in result.stdout or "libgomp.so" in result.stdout:
return True

def _get_triton_omp_num_threads() -> str:
return os.getenv("TRITON_OMP_NUM_THREADS", "4")

def _generate_launcher(constants, signature, kernel_name):
arg_decls = ', '.join(f"{_ty_to_cpp(ty)} arg{i}" for i, ty in signature.items())
args_format = ''.join([_format_of(_extracted_type(ty)) for ty in signature.values()])
Expand All @@ -67,6 +79,8 @@ def _generate_launcher(constants, signature, kernel_name):
kernel_parameters = ', '.join(f"static_cast<{_ty_to_cpp(ty)}>(arg{i})" if ty[0] != "*" else f"0, &ptr_arg{i}" for i, ty in signature.items() if i not in constants)
kernel_parameters += ', ' if kernel_parameters else ''

omp_num_threads = _get_triton_omp_num_threads()

return f"""
#include <assert.h>
#include <stdbool.h>
Expand All @@ -84,6 +98,7 @@ def _generate_launcher(constants, signature, kernel_name):
static void _launch(int gridX, int gridY, int gridZ, {arg_decls}) {{
if (gridX*gridY*gridZ > 0) {{
// Cast "function" to the real function type.
#pragma omp parallel for collapse(3) num_threads({omp_num_threads}) schedule(guided, 64)
for(int x = 0; x < gridX; x++) {{
for(int y = 0; y < gridY; y++) {{
for(int z = 0; z < gridZ; z++) {{
Expand Down Expand Up @@ -258,7 +273,7 @@ def launch(
"g++", "-std=c++17", launcher_src_path, asm_src_path,
f"-I{py_include_dir}", f"-I{include_dir}", f"-L{py_lib_dir}",
"-shared", f"-l{py_lib}", "-fPIC", "-o", so_path
])
] + (["-fopenmp"] if _openmp_available() else []))

with open(so_path, "rb") as f:
cache_path = cache.put(f.read(), filename, binary=True)
Expand Down