Skip to content
This repository was archived by the owner on Jul 23, 2024. It is now read-only.

Commit 740f61f

Browse files
icemelonyangchen-MS
authored andcommitted
[Runtime] Enable option to use OpenMP thread pool (apache#4089)
1 parent 9b3a424 commit 740f61f

File tree

4 files changed

+80
-0
lines changed

4 files changed

+80
-0
lines changed

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ tvm_option(USE_LLVM "Build with LLVM, can be set to specific llvm-config path" O
3232
tvm_option(USE_STACKVM_RUNTIME "Include stackvm into the runtime" OFF)
3333
tvm_option(USE_GRAPH_RUNTIME "Build with tiny graph runtime" ON)
3434
tvm_option(USE_GRAPH_RUNTIME_DEBUG "Build with tiny graph runtime debug mode" OFF)
35+
tvm_option(USE_OPENMP "Build with OpenMP thread pool implementation" OFF)
3536
tvm_option(USE_SGX "Build with SGX" OFF)
3637
tvm_option(USE_RTTI "Build with RTTI" ON)
3738
tvm_option(USE_MSVC_MT "Build with MT" OFF)
@@ -187,6 +188,7 @@ include(cmake/modules/VTA.cmake)
187188
include(cmake/modules/CUDA.cmake)
188189
include(cmake/modules/OpenCL.cmake)
189190
include(cmake/modules/OpenGL.cmake)
191+
include(cmake/modules/OpenMP.cmake)
190192
include(cmake/modules/Vulkan.cmake)
191193
include(cmake/modules/Metal.cmake)
192194
include(cmake/modules/ROCM.cmake)

cmake/config.cmake

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,10 @@ set(USE_BLAS none)
105105
# set(USE_MKL_PATH ../IntelSWTools/compilers_and_libraries_2018/windows/mkl) for WIN32
106106
set(USE_MKL_PATH none)
107107

108+
# Whether use OpenMP thread pool, choices: gnu, intel
109+
# Note: "gnu" uses gomp library, "intel" uses iomp5 library
110+
set(USE_OPENMP none)
111+
108112
# Whether use contrib.random in runtime
109113
set(USE_RANDOM OFF)
110114

cmake/modules/OpenMP.cmake

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
18+
# OpenMP Module
19+
if(USE_OPENMP STREQUAL "gnu")
20+
find_package(OpenMP)
21+
if(OPENMP_FOUND)
22+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
23+
list(APPEND TVM_RUNTIME_LINKER_LIBS ${OpenMP_CXX_LIBRARIES})
24+
add_definitions(-DTVM_THREADPOOL_USE_OPENMP=1)
25+
message(STATUS "Build with OpenMP ${OpenMP_CXX_LIBRARIES}")
26+
else()
27+
add_definitions(-DTVM_THREADPOOL_USE_OPENMP=0)
28+
message(WARNING "OpenMP cannot be found, use TVM threadpool instead.")
29+
endif()
30+
elseif(USE_OPENMP STREQUAL "intel")
31+
find_package(OpenMP)
32+
if(OPENMP_FOUND)
33+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
34+
if (MSVC)
35+
find_library(OMP_LIBRARY NAMES libiomp5md)
36+
else()
37+
find_library(OMP_LIBRARY NAMES iomp5)
38+
endif()
39+
list(APPEND TVM_RUNTIME_LINKER_LIBS ${OMP_LIBRARY})
40+
add_definitions(-DTVM_THREADPOOL_USE_OPENMP=1)
41+
message(STATUS "Build with OpenMP " ${OMP_LIBRARY})
42+
else()
43+
add_definitions(-DTVM_THREADPOOL_USE_OPENMP=0)
44+
message(WARNING "OpenMP cannot be found, use TVM threadpool instead.")
45+
endif()
46+
else()
47+
add_definitions(-DTVM_THREADPOOL_USE_OPENMP=0)
48+
endif()

src/runtime/thread_pool.cc

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
#include <tvm/runtime/threading_backend.h>
1111
#include <dmlc/thread_local.h>
1212
#include <dmlc/logging.h>
13+
#if TVM_THREADPOOL_USE_OPENMP
14+
#include <omp.h>
15+
#endif
1316
#include <thread>
1417
#include <condition_variable>
1518
#include <mutex>
@@ -358,12 +361,34 @@ int TVMBackendParallelLaunch(
358361
FTVMParallelLambda flambda,
359362
void* cdata,
360363
int num_task) {
364+
#if !TVM_THREADPOOL_USE_OPENMP
361365
int res = tvm::runtime::ThreadPool::ThreadLocal()->Launch(
362366
flambda, cdata, num_task, 1);
363367
return res;
368+
#else
369+
int num_workers = tvm::runtime::threading::MaxConcurrency();
370+
if (num_task == 0) num_task = num_workers;
371+
omp_set_num_threads(num_workers);
372+
#pragma omp parallel num_threads(num_workers)
373+
{
374+
TVMParallelGroupEnv env;
375+
env.num_task = num_task;
376+
std::atomic<int32_t>* sync_counter = new std::atomic<int>[num_task * tvm::runtime::kSyncStride];
377+
for (int i = 0; i < num_task; ++i) {
378+
sync_counter[i * tvm::runtime::kSyncStride].store(
379+
0, std::memory_order_relaxed);
380+
}
381+
env.sync_handle = sync_counter;
382+
(*flambda)(omp_get_thread_num(), &env, cdata);
383+
}
384+
return 0;
385+
#endif
364386
}
365387

366388
int TVMBackendParallelBarrier(int task_id, TVMParallelGroupEnv* penv) {
389+
#if TVM_THREADPOOL_USE_OPENMP
390+
#pragma omp barrier
391+
#else
367392
using tvm::runtime::kSyncStride;
368393
int num_task = penv->num_task;
369394
std::atomic<int>* sync_counter =
@@ -379,5 +404,6 @@ int TVMBackendParallelBarrier(int task_id, TVMParallelGroupEnv* penv) {
379404
}
380405
}
381406
std::atomic_thread_fence(std::memory_order_acquire);
407+
#endif
382408
return 0;
383409
}

0 commit comments

Comments
 (0)