GithubHelp home page GithubHelp logo

maratyszcza / pthreadpool Goto Github PK

View Code? Open in Web Editor NEW
345.0 18.0 132.0 212 KB

Portable (POSIX/Windows/Emscripten) thread pool for C/C++

License: BSD 2-Clause "Simplified" License

Python 0.13% C 44.97% C++ 52.25% Makefile 0.06% CMake 1.19% Starlark 1.39%
threadpool multi-threading multi-core pthreads

pthreadpool's Introduction

pthreadpool

BSD (2 clause) License Build Status

pthreadpool is a portable and efficient thread pool implementation. It provides similar functionality to #pragma omp parallel for, but with additional features.

Features:

  • C interface (C++-compatible).
  • 1D-6D loops with step parameters.
  • Run on user-specified or auto-detected number of threads.
  • Work-stealing scheduling for efficient work balancing.
  • Wait-free synchronization of work items.
  • Compatible with Linux (including Android), macOS, iOS, Windows, Emscripten environments.
  • 100% unit tests coverage.
  • Throughput and latency microbenchmarks.

Example

The following example demonstates using the thread pool for parallel addition of two arrays:

static void add_arrays(struct array_addition_context* context, size_t i) {
  context->sum[i] = context->augend[i] + context->addend[i];
}

#define ARRAY_SIZE 4

int main() {
  double augend[ARRAY_SIZE] = { 1.0, 2.0, 4.0, -5.0 };
  double addend[ARRAY_SIZE] = { 0.25, -1.75, 0.0, 0.5 };
  double sum[ARRAY_SIZE];

  pthreadpool_t threadpool = pthreadpool_create(0);
  assert(threadpool != NULL);

  const size_t threads_count = pthreadpool_get_threads_count(threadpool);
  printf("Created thread pool with %zu threads\n", threads_count);

  struct array_addition_context context = { augend, addend, sum };
  pthreadpool_parallelize_1d(threadpool,
    (pthreadpool_task_1d_t) add_arrays,
    (void*) &context,
    ARRAY_SIZE,
    PTHREADPOOL_FLAG_DISABLE_DENORMALS /* flags */);

  pthreadpool_destroy(threadpool);
  threadpool = NULL;

  printf("%8s\t%.2lf\t%.2lf\t%.2lf\t%.2lf\n", "Augend",
    augend[0], augend[1], augend[2], augend[3]);
  printf("%8s\t%.2lf\t%.2lf\t%.2lf\t%.2lf\n", "Addend",
    addend[0], addend[1], addend[2], addend[3]);
  printf("%8s\t%.2lf\t%.2lf\t%.2lf\t%.2lf\n", "Sum",
    sum[0], sum[1], sum[2], sum[3]);

  return 0;
}

pthreadpool's People

Contributors

everton1984 avatar jakirkham avatar jameshilliard avatar maratyszcza avatar mattn avatar mwtarnowski avatar ngzhian avatar orivej avatar peterjc123 avatar simonmaurer avatar xkszltl avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

pthreadpool's Issues

Build error with WinSDK 10.0.16299.0

When building pthreadpool as part of pytorch on Windows, we got strange compile errors.
Issue is originally filed in: pytorch/pytorch#48895

The error message sounds like macro related.
With some tests we found this issue is limited to WinSDK 16299 (I also tested 19041 and that works), and can be solve by defining WIN32_LEAN_AND_MEAN.
I think it should be better to ask for fix in upstream, as guarding #define around #include <windows.h>.

Recent activity broke pytorch install on raspberry pi zero

[ 12%] Building C object confu-deps/pthreadpool/CMakeFiles/pthreadpool.dir/src/threadpool-pthreads.c.o
In file included from /home/pi/pytorch_install/pytorch/build/confu-srcs/pthreadpool/src/threadpool-pthreads.c:35:
/home/pi/pytorch_install/pytorch/build/confu-srcs/pthreadpool/include/pthreadpool.h:183:2: warning: ‘pthreadpool_function_1d_t’ is deprecated [-Wdeprecated-declarations]
pthreadpool_function_1d_t function,
^~~~~~~~~~~~~~~~~~~~~~~~~
/home/pi/pytorch_install/pytorch/build/confu-srcs/pthreadpool/include/pthreadpool.h:189:2: warning: ‘pthreadpool_function_1d_tiled_t’ is deprecated [-Wdeprecated-declarations]
pthreadpool_function_1d_tiled_t function,
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/home/pi/pytorch_install/pytorch/build/confu-srcs/pthreadpool/include/pthreadpool.h:196:2: warning: ‘pthreadpool_function_2d_t’ is deprecated [-Wdeprecated-declarations]
pthreadpool_function_2d_t function,
^~~~~~~~~~~~~~~~~~~~~~~~~
/home/pi/pytorch_install/pytorch/build/confu-srcs/pthreadpool/include/pthreadpool.h:203:2: warning: ‘pthreadpool_function_2d_tiled_t’ is deprecated [-Wdeprecated-declarations]
pthreadpool_function_2d_tiled_t function,
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/home/pi/pytorch_install/pytorch/build/confu-srcs/pthreadpool/include/pthreadpool.h:212:2: warning: ‘pthreadpool_function_3d_tiled_t’ is deprecated [-Wdeprecated-declarations]
pthreadpool_function_3d_tiled_t function,
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/home/pi/pytorch_install/pytorch/build/confu-srcs/pthreadpool/include/pthreadpool.h:223:2: warning: ‘pthreadpool_function_4d_tiled_t’ is deprecated [-Wdeprecated-declarations]
pthreadpool_function_4d_tiled_t function,
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/tmp/cc3nCuOW.s: Assembler messages:
/tmp/cc3nCuOW.s:605: Error: immediate expression requires a # prefix -- orr r3,0x1000000' /tmp/cc3nCuOW.s:855: Error: immediate expression requires a # prefix -- orr r3,0x1000000'
/tmp/cc3nCuOW.s:948: Error: immediate expression requires a # prefix -- orr r3,0x1000000' /tmp/cc3nCuOW.s:1068: Error: immediate expression requires a # prefix -- orr r3,0x1000000'
/tmp/cc3nCuOW.s:1147: Error: immediate expression requires a # prefix -- orr r3,0x1000000' /tmp/cc3nCuOW.s:1321: Error: immediate expression requires a # prefix -- orr r3,0x1000000'
/tmp/cc3nCuOW.s:1486: Error: immediate expression requires a # prefix -- orr r3,0x1000000' /tmp/cc3nCuOW.s:1684: Error: immediate expression requires a # prefix -- orr r3,0x1000000'
/tmp/cc3nCuOW.s:1924: Error: immediate expression requires a # prefix -- orr r3,0x1000000' /tmp/cc3nCuOW.s:2206: Error: immediate expression requires a # prefix -- orr r3,0x1000000'
/tmp/cc3nCuOW.s:2530: Error: immediate expression requires a # prefix -- `orr r3,0x1000000'
make[2]: *** [confu-deps/pthreadpool/CMakeFiles/pthreadpool.dir/build.make:76: confu-deps/pthreadpool/CMakeFiles/pthreadpool.dir/src/threadpool-pthreads.c.o] Error 1
make[1]: *** [CMakeFiles/Makefile2:517: confu-deps/pthreadpool/CMakeFiles/pthreadpool.dir/all] Error 2
make: *** [Makefile:141: all] Error 2
Traceback (most recent call last):
File "setup.py", line 759, in
build_deps()
File "setup.py", line 311, in build_deps
cmake=cmake)
File "/home/pi/pytorch_install/pytorch/tools/build_pytorch_libs.py", line 59, in build_caffe2
cmake.build(my_env)
File "/home/pi/pytorch_install/pytorch/tools/setup_helpers/cmake.py", line 334, in build
self.run(build_args, my_env)
File "/home/pi/pytorch_install/pytorch/tools/setup_helpers/cmake.py", line 142, in run
check_call(command, cwd=self.build_dir, env=env)
File "/usr/lib/python3.7/subprocess.py", line 347, in check_call
raise CalledProcessError(retcode, cmd)
subprocess.CalledProcessError: Command '['cmake', '--build', '.', '--target', 'install', '--config', 'Release', '--', '-j', '1']' returned non-zero exit status 2.
pi@raspberrypi:~/

pytorch/pytorch#27539

Help pytorch migrate to the "new" API

pthreadpool_function_1d_tiled_t and friends are deprecated. These are producing many warnings when building pytorch: they are used in quantized/cpu/cnnpack. It would be nice if someone well versed in pthreadpool could provide some guidance or even a PR to update them.

Termux compilation tips

I needed it for pytorch compilation. Spent half a day trying to compile this, as dependency. Tips for Termux users only:

  1. If you're compiling this code on Termux and encounter an error about the missing 'unistd.h' file, you might need to install the 'ndk-sysroot'. It has it natively, one of very few apt packages that does not get installed to /opt or .local/lib
  2. Remove this code security cruft first, as it gets stuck eith smth Google. Yes. Thus :
alias cmakeinstall='rm CMakeCache.txt && echo "Removing silly Werror 🪲 directives en masse..." & time find . -type f -exec sed -i "s/-Werror//g" {} \; && export CFLAGS="-fuse-ld=lld  -pthread -g -march=armv8-a -mtune=cortex-a53 -Wall -Wextra"  &&  echo "Starting compilation 👩‍💻 proper... "&& cmake -DCMAKE_INSTALL_PREFIX=$PREFIX . && time make -j4 && make install' 

is my newest one liner.

Ref:

~/downloads/pthreadpool $ uname -a            Linux localhost 4.14.186+ #1 SMP PREEMPT Thu Mar 17 16:28:22 CST 2022 aarch64 Android       ~/downloads/pthreadpool $

PS. You may test it with:

#include <stdio.h>                            #include <pthreadpool.h>                                                                    void task(void* arg, size_t i) {                  printf("Hello from task %zu\n", i);       }                                                                                           int main() {                                      pthreadpool_t pool = pthreadpool_create(4);                                                                                               pthreadpool_parallelize_1d(pool, (pthreadpool_task_1d_t)task, NULL, 10, 0);                                                               pthreadpool_destroy(pool);                    return 0;                                 }

with : gcc -o test4 test4.c -pthread -lpthreadpool :
~/.../pytorch/build $ ./test4 Hello from task 3 Hello from task 4 Hello from task 5 Hello from task 2 Hello from task 1 Hello from task 9 Hello from task 7 Hello from task 0 Hello from task 6 Hello from task 8 ~/.../pytorch/build $

...

Can't install properly

I am new to make/cmake and can't install this library properly. I used these commands in order:

cmake -S * -B build
cd build
make
sudo make install

But, when I try to compile addition.c file, get this error:

/usr/bin/ld: /tmp/cc3SOpOd.o: in function main': addition.c:(.text+0xe3): undefined reference to pthreadpool_create'
/usr/bin/ld: addition.c:(.text+0x122): undefined reference to pthreadpool_get_threads_count' /usr/bin/ld: addition.c:(.text+0x18a): undefined reference to pthreadpool_parallelize_1d'
/usr/bin/ld: addition.c:(.text+0x199): undefined reference to `pthreadpool_destroy'
collect2: error: ld returned 1 exit status

threadpool get single pthread api

I use pthreadpool_create api create a pthreadpool. And I want to set cpu affinity with every thread in the pool.
Does Pthreadpool support this ? Or does Pthreadpool support api that get single pthread in the pool with index?

Build on IOS platform using XCode generator

Hi, on iOS platform CMake with XCode generator breaks on find_package(THREADS)

This is general issue with cmake and this generator, you can take a look at:
https://gitlab.kitware.com/cmake/cmake/issues/16695

Could you include following patch in master code?

diff --git a/CMakeLists.txt b/CMakeLists.txt
index b2affe4..2d66c07 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -94,7 +94,9 @@ PTHREADPOOL_TARGET_ENABLE_C99(pthreadpool)
 TARGET_LINK_LIBRARIES(pthreadpool PUBLIC pthreadpool_interface)
 IF(NOT CMAKE_SYSTEM_NAME STREQUAL "Emscripten")
   SET(CMAKE_THREAD_PREFER_PTHREAD TRUE)
-  FIND_PACKAGE(Threads REQUIRED)
+  IF(NOT CMAKE_GENERATOR STREQUAL "Xcode")
+    FIND_PACKAGE(Threads REQUIRED)
+  ENDIF()
   IF(CMAKE_USE_PTHREADS_INIT)
     IF(CMAKE_SYSTEM_NAME STREQUAL "Linux" OR CMAKE_SYSTEM_NAME STREQUAL "Android")
       TARGET_COMPILE_OPTIONS(pthreadpool PUBLIC -pthread)

If needed I can create pull request?
Regards,
Milan

heap-buffer-overflow in pthreadpool_create()

see https://issuetracker.google.com/211696118 for the original bug report.

the code's moved about and is at

const size_t threadpool_size = sizeof(struct pthreadpool) + threads_count * sizeof(struct thread_info);
now, but the bug's still there.

probably wants to be something like this instead:

	const size_t threadpool_size;
	if (__builtin_mul_overflow(threads_count, sizeof(struct thread_info), &threadpool_size) ||
	    __builtin_add_overflow(threadpool_size, sizeof(struct pthreadpool), &threadpool_size)) {
	    return NULL;
	}

or the original reporter's

+	if (threads_count > (UINT_MAX - sizeof(struct pthreadpool)) / sizeof(struct thread_info)) {
+		return NULL;
+	}

(which is more conservative because UINT_MAX isn't the right constant on LP64.)

Add .pc file for better discoverablity

pthreadpool lacks a pthreadpool.pc pkg-config file for the build system like CMake and Meson to find it as an external dependencies.

If pthreadpool only use cmake, I can help add it. But I don't know how to work with bazel and confu

Implementation in EMSCRIPTEN

when compiling in EMSCRIPTEN environment, there is only shim.c got compiled.

This is the concerning CMakeLists.txt part,

IF(EMSCRIPTEN)
  LIST(APPEND PTHREADPOOL_SRCS src/shim.c)
ELSE()
  LIST(APPEND PTHREADPOOL_SRCS src/portable-api.c src/memory.c)
  IF(APPLE AND (PTHREADPOOL_SYNC_PRIMITIVE STREQUAL "default" OR PTHREADPOOL_SYNC_PRIMITIVE STREQUAL "gcd"))
    LIST(APPEND PTHREADPOOL_SRCS src/gcd.c)
  ELSEIF(CMAKE_SYSTEM_NAME MATCHES "^(Windows|CYGWIN|MSYS)$" AND (PTHREADPOOL_SYNC_PRIMITIVE STREQUAL "default" OR PTHREADPOOL_SYNC_PRIMITIVE STREQUAL "event"))
    LIST(APPEND PTHREADPOOL_SRCS src/windows.c)
  ELSE()
    LIST(APPEND PTHREADPOOL_SRCS src/pthreads.c)
  ENDIF()
  IF(PTHREADPOOL_ENABLE_FASTPATH)
    LIST(APPEND PTHREADPOOL_SRCS src/fastpath.c)
  ENDIF()
ENDIF()

And after checking the implementation in that file, I found that all tasks are handled serially.

Here is a code snippet from shim.c,

void pthreadpool_parallelize_1d(
	struct pthreadpool* threadpool,
	pthreadpool_task_1d_t task,
	void* argument,
	size_t range,
	uint32_t flags)
{
	for (size_t i = 0; i < range; i++) {
		task(argument, i);
	}
}

As all tasks run in main thread, can I say that this thread pool does not support EMSCRIPTEN?

pthreadpool_deallocate and pthreadpool_allocate are not defined.

I am having trouble building pthreadpool (the end goal is to build NNPACK and NNPACK-darknet). First I run confu setup and python configure.py before failing to build using ninja. I see these functions are defined in the threadpool-object.h file, but cannot locate its implementation. Could I be missing a dependency somewhere? Thank you in advance.

https://github.com/Maratyszcza/pthreadpool.git

Detected root directory: /home/pi/NNPACK-darknet
/usr/local/lib/python2.7/dist-packages/confu/manifest.py:36: YAMLLoadWarning: calling yaml.load() without Loader=... is deprecated, as the default Loader is unsafe. Please read https://msg.pyyaml.org/load for full details.
manifest_yaml = yaml.load(manifest_text)
Configuring dependency pthreadpool
Configuring dependency fxdiv
Configuring dependency googletest
Configuring dependency googlebenchmark
Traceback (most recent call last):
File "./configure.py", line 396, in
main(sys.argv[1:]).generate()
File "./configure.py", line 36, in main
(build.deps.pthreadpool, build.deps.fxdiv, build.deps.fp16): any,
File "/usr/local/lib/python2.7/dist-packages/confu/builds/deps.py", line 48, in getattr
config = configure.main(["--target", self._target])
File "/home/pi/NNPACK-darknet/deps/pthreadpool/configure.py", line 20, in main
build.static_library("pthreadpool", [build.cc(src) for src in sources])
File "/usr/local/lib/python2.7/dist-packages/confu/builds/unix.py", line 72, in cc
return self._compile("cc", source_path)
File "/usr/local/lib/python2.7/dist-packages/confu/builds/unix.py", line 56, in _compile
source_path = validate_source_path(source_path, self.source_dir)
File "/usr/local/lib/python2.7/dist-packages/confu/validators.py", line 67, in validate_source_path
.format(source_path=source_path))
ValueError: Specified source file /home/pi/NNPACK-darknet/deps/pthreadpool/src/threadpool-legacy.c does not exist

How to use this library?

I am very sorry to bother you. I would like to ask what files I need to add to my include and src folders if I want to use your thread pool, and what changes I need to make to my cmakelists.txt. I am not very familiar with cmake, just that I need to use the thread pool for my my parallel computing homework and I will indicate the source of this thread pool in my homework. Thank you very much!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.