Simpler MEX Multi-Threading w/ a Persistent Thread Pool

버전 1.1.2 (12.9 KB) 작성자: oreoman
Further speed up your MEX files by making it easier to add in support for multi-threading and eliminate multi-threading overhead.
다운로드 수: 69
업데이트 날짜: 2021/7/5
I can't believe this actually worked.
For smaller problems that can't be vectorized, properly setting up multi-threading within a MEX file can create enough overhead that the single-threaded solution is often faster. Using a thread pool eliminates that overhead. The pool is created once, and then can be re-used in multiple calls to your MEX function. The thread pool is then closed out and cleared from memory by typing 'clear all' or 'clear mex' in the command line (or closing MATLAB).
Depending on the problem, you can potentially see a speedup of up to 'maxNumCompThreads', though that's probably not likely. In my very basic testing, I'm seeing ~2x speedup (with four threads) over many iterations of a small problem. For the real-world problem I created this for, I'm seeing a ~60% speedup compared to standard MEX multi-threading, saving me a couple of days in total simulation time.
In addition, adding multi-threading to a MEX file is now much easier, as you only need to learn how to use one function, AddThreadPoolJob. You then just have to remember to close your MEX file with SynchronizeThreads so that you don't get awesome race conditions.
See the included madd_threadpool.c example and compare to madd.c that uses regular multi-threading to figure out what's going on. There's also more documentation in threadpool.h. To see how single- thread vs. multi-thread vs. thread pooled vs. vectorized code compares, compile things and check out threadpool_timings.m.
NOTE: properly vectorized MATLAB code will always be faster than this. Only use this stuff if you can't vectorize your code but it still lends itself to straightforward parallelization, e.g. integrating recurrence relations in multiple dimensions.

인용 양식

oreoman (2024). Simpler MEX Multi-Threading w/ a Persistent Thread Pool (https://github.com/michael-nix/MATLAB-MEX-Threadpool), GitHub. 검색됨 .

MATLAB 릴리스 호환 정보
개발 환경: R2019a
R2018a 이상 릴리스와 호환
플랫폼 호환성
Windows macOS Linux
카테고리
Help CenterMATLAB Answers에서 Write C Functions Callable from MATLAB (MEX Files)에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

GitHub 디폴트 브랜치를 사용하는 버전은 다운로드할 수 없음

버전 게시됨 릴리스 정보
1.1.2

Simple bug fix in madd_threadpool.c

1.1.1

Connected to GitHub...

1.1.0

Added a few helper functions to make my life easier, and make it possible to use the same thread pool among multiple MEX files. Not properly documented 'cause I'm lazy.

1.0.0

이 GitHub 애드온의 문제를 보거나 보고하려면 GitHub 리포지토리로 가십시오.
이 GitHub 애드온의 문제를 보거나 보고하려면 GitHub 리포지토리로 가십시오.