Recent

Author Topic: PasMP - a parallel-processing/multi-processing library for Object Pascal  (Read 13625 times)

BeRo

  • New Member
  • *
  • Posts: 45
    • My site
PasMP

PasMP is a cross-platform parallel-processing/multi-processing library for Object Pascal for Delphi and FreePascal

GitHub project page:

https://github.com/BeRo1985/pasmp

License:

zlib

Why:

Because System.Threading and OmniThreadLibrary are Delphi-only libraries and MTProcs has also his own problems (FreePascal-only, inefficient lock-based work-stealing, etc.) and at least System.Threading from Delphi XE7 has also a bit more inefficient work-stealing implementation.

Features: 

  • Low-level-based design with optional high-level-based constructs
  • Designed for fully-strict fork-join model in mind (because it's less error-prone to work with it than with terminally-strict fork-join for my taste), but it can be also abused for more flexible models, the only important thing is, that you're releasing the jobs again as soon as they are completed (the simplest weay would be by calling TPasMP.Reset per workload-frame), otherwise you'll have memory leaks, or just use the PasMPJobFlagReleaseOnFinish flag.
  • It can be used a job scheduler for multithreaded game engines and so on
  • Work-first lock-free work-stealing dynamic-sized Chase-Lev queue/deque (where only the resizing code part of it isn't lock-free, so that on the other hand it is garbage-collector-free)
  • Lock-free job memory allocator (al least lock-free on x86-32 and x86-64 targets)
  • Parallel-for pattern
  • Parallel intro sort (direct and indirect)
  • Parellel merge sort (direct and indirect)
  • Optional strict singleton usage option per global PasMPUseAsStrictSingletonInstance define (besides the option of usage of multiple PasMP instances)
  • Compatible with FreePascal >= 2.6.x and Delphi >= 7
  • Cross platform (Windows, Linux, etc.)

Leledumbo

  • Hero Member
  • *****
  • Posts: 8757
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: PasMP - a parallel-processing/multi-processing library for Object Pascal
« Reply #1 on: February 08, 2016, 10:04:23 am »
First clone and compile result on x86_64-linux using either FPC 3.0.0 and fresh (just svn up today) 3.1.1:
  • The examples are Windows only :(
  • PasMP unit cannot be compiled, pthread_* identifiers not found. There is a pthread.inc in rtl directory, including that to the source makes the source ALMOST compile. Final result is: PasMP.pas(1898,15) Fatal: Internal error 200706094. Congratulations, you've found a bug in the compiler (please report)
End of first try

BeRo

  • New Member
  • *
  • Posts: 45
    • My site
Re: PasMP - a parallel-processing/multi-processing library for Object Pascal
« Reply #2 on: February 08, 2016, 12:51:27 pm »
First clone and compile result on x86_64-linux using either FPC 3.0.0 and fresh (just svn up today) 3.1.1:
  • The examples are Windows only :(
  • PasMP unit cannot be compiled, pthread_* identifiers not found. There is a pthread.inc in rtl directory, including that to the source makes the source ALMOST compile. Final result is: PasMP.pas(1898,15) Fatal: Internal error 200706094. Congratulations, you've found a bug in the compiler (please report)
End of first try

I've updated the examples, so that they are no longer windows-only.

And I've workaround the UInt64*UInt64ConstantValue FPC internal error issue now, by moving the uint64 constant value into a constant variable.

At least it compiles for me under Linux on my x86 DevBoard now:

Code: [Select]
[root@kabini ~]# ppcx64 -b -dUseCThreads PasMP.pas
Free Pascal Compiler version 3.0.0 [2015/11/26] for x86_64
Copyright (c) 1993-2015 by Florian Klaempfl and others
Target OS: Linux for x86-64
Compiling PasMP.pas
3940 lines compiled, 0.4 sec
« Last Edit: February 08, 2016, 01:49:28 pm by BeRo »

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11452
  • FPC developer.
Re: PasMP - a parallel-processing/multi-processing library for Object Pascal
« Reply #3 on: February 08, 2016, 02:02:58 pm »
There is a proper unit pthreads?

BeRo

  • New Member
  • *
  • Posts: 45
    • My site
Re: PasMP - a parallel-processing/multi-processing library for Object Pascal
« Reply #4 on: February 08, 2016, 02:40:59 pm »
There is a proper unit pthreads?

The problem is, that PasMP should be also compatible with the Android/iOS(/MacOSX) NextGen Delphi Compiler in future, so that the ifdef-hell at the uses-clause in PasMP.pas will be get bigger, when I'll use the pthreads unit from FPC and the Posix.Pthread unit from the NextGen Delphi compiler. But yes, that is definitely the more clean way then, and I will implement it soon so also.

BeniBela

  • Hero Member
  • *****
  • Posts: 906
    • homepage
Re: PasMP - a parallel-processing/multi-processing library for Object Pascal
« Reply #5 on: February 08, 2016, 03:15:48 pm »
Is it smart and scalable? Such libraries all have to be smart and scalable

BeRo

  • New Member
  • *
  • Posts: 45
    • My site
Re: PasMP - a parallel-processing/multi-processing library for Object Pascal
« Reply #6 on: February 08, 2016, 03:27:14 pm »
Is it smart and scalable? Such libraries all have to be smart and scalable

Yep, see https://www.youtube.com/watch?v=7G3i4B4rJY0 where every worker thread is pinned to a own CPU core (virtual CPU core because it's on a Intel Xeon E3 CPU with active hyperthreading).

But using the (fully strict) fork/join-parallel-programming-model-pragma should&must first be learned for in order to use it properly in a efficient and correct way, in the case when you don't want to restrict you only on TPasMP.ParallelFor, TPasMP.Parallel*Sort and so on.






« Last Edit: February 08, 2016, 03:29:05 pm by BeRo »

Leledumbo

  • Hero Member
  • *****
  • Posts: 8757
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: PasMP - a parallel-processing/multi-processing library for Object Pascal
« Reply #7 on: February 08, 2016, 03:44:44 pm »
Alright, it compiles directly on x86_64-linux now. It seems that -dUseCThreads is not needed despite the ifdef, with or without it the speed and size is the same. The speed matches my number of cores (2), so OK. Now I must find some code to parallelize ;)

BeRo

  • New Member
  • *
  • Posts: 45
    • My site
Re: PasMP - a parallel-processing/multi-processing library for Object Pascal
« Reply #8 on: February 21, 2016, 08:50:17 pm »
PasMP updated: https://github.com/BeRo1985/pasmp

New features:

  • Thread-safe single producer single consumer queue (untyped and typed, bounded-only, lock-free)
  • Thread-safe multiple producer multiple consumer stack (untyped and typed, bounded and unbounded, lock-free on x86-32/x86-64/ARM32, lock-based on another CPU targets)
  • Thread-safe multiple producer multiple consumer queue (untyped and typed, bounded and unbounded, lock-free on x86-32/x86-64/ARM32, lock-based on another CPU targets)
  • Thread-safe hash table (untyped and typed, hybrid-implementation of lock-free and fine-graded lock-based single-operation code parts)
  • Thread-safe dynamic-sized array (untyped and typed, fine-graded lock-based)
  • TPasMPMath class with class static useful (primary bit-twiddling) math function methods
  • TPasMPInterlocked class with class static atomic function methods
  • TPasMPMemoryBarrier class with class static memory barrier function methods
  • TPasMPMemory class with class static aligned memory allocation function methods
  • Synchronisation primitives:
    • TPasMPEvent
    • TPasMPSimpleEvent
    • TPasMPCriticalSection
    • TPasMPMutex
    • TPasMPConditionVariableLock
    • TPasMPConditionVariable
    • TPasMPSemaphore
    • TPasMPInvertedSemaphore
    • TPasMPMultipleReaderSingleWriterLock
    • TPasMPMultipleReaderSingleWriterSpinLock
    • TPasMPSlimReaderWriterLock
    • TPasMPSpinLock
    • TPasMPBarrier
  • and more...

BeniBela

  • Hero Member
  • *****
  • Posts: 906
    • homepage
Re: PasMP - a parallel-processing/multi-processing library for Object Pascal
« Reply #9 on: February 22, 2016, 01:10:28 pm »
Have you tested it on Android?

SymbolicFrank

  • Hero Member
  • *****
  • Posts: 1313
Re: PasMP - a parallel-processing/multi-processing library for Object Pascal
« Reply #10 on: February 22, 2016, 03:30:48 pm »
Nice! Just what I was looking for!

Going to test it right away.

SymbolicFrank

  • Hero Member
  • *****
  • Posts: 1313
Re: PasMP - a parallel-processing/multi-processing library for Object Pascal
« Reply #11 on: February 24, 2016, 11:52:25 am »
I get the following errors with Lazarus 1.6 on Windows 7:

Code: [Select]
Compile Project, OS: win64, CPU: x86_64, Target: zipcodeserver.exe: Exit code 1, Errors: 6
PasMP.pas(1119,56) Error: Duplicate identifier "TPasMPSingleProducerSingleConsumerBoundedQueue"
PasMP.pas(1168,28) Error: Duplicate identifier "TPasMPBoundedStack"
PasMP.pas(1216,30) Error: Duplicate identifier "TPasMPUnboundedStack"
PasMP.pas(1265,28) Error: Duplicate identifier "TPasMPBoundedQueue"
PasMP.pas(1305,30) Error: Duplicate identifier "TPasMPUnboundedQueue"
PasMP.pas(1448,28) Error: Duplicate identifier "TPasMPDynamicArray"

Also, because of all the {$ifdef} (which is great, for the platform independence) it isn't all that readable. So, I was wondering: do you have a list of the classes and when to use them? Like, which queue is best, if I want a multiple writer / single reader queue of records that include a long string? Or, how do I make a list of threads and find out if they still exist, or have the termination go through that list?

Otherwise I could just use the primitives and make them myself, but that would be a waste.

BeRo

  • New Member
  • *
  • Posts: 45
    • My site
Hm, which FPC version you do using ?

I'm testing (and have tested) each PasMP code change with Delphi 7, Delphi XE7, FPC 3.0.0 stable and the most current correct compilable FPC SVN trunk.

My output with FPC 3.0.0:

Code: [Select]
Free Pascal Compiler version 3.0.0 [2015/11/16] for i386
Copyright (c) 1993-2015 by Florian Klaempfl and others
Target OS: Win32 for i386
Compiling PasMP.pas
10818 lines compiled, 0.4 sec


SymbolicFrank

  • Hero Member
  • *****
  • Posts: 1313
Yes, I'm sorry. I had a corrupt Lazarus install. I just tested it with 1.6 / FPC 3.0.0, and now it works.

Do you have a list of the components and when to use which one?

BeRo

  • New Member
  • *
  • Posts: 45
    • My site
Yes, I'm sorry. I had a corrupt Lazarus install. I just tested it with 1.6 / FPC 3.0.0, and now it works.

Good ;)

Do you have a list of the components and when to use which one?

TPasMPSingleProducerSingleConsumerBoundedQueue => Single producer single consumer fixed-sized queue

TPasMPBoundedStack => Multiple producer multiple consumer fixed-sized stack

TPasMPUnboundedStack => Multiple producer multiple consumer dynamic-sized stack (can raise memory-full-exceptions on wrong usage, for example when the consumers pops at too rare intervals items while the producers pushs new items with very fast/short intervals)

TPasMPBoundedQueue => Multiple producer multiple consumer fixed-sized queue

TPasMPUnboundedQueue => Multiple producer multiple consumer dynamic-sized queue (can raise memory-full-exceptions on wrong usage, for example when the consumers pops at too rare intervals items while the producers pushs new items with very fast/short intervals)

TPasMPDynamicArray => Just a thread-safe multiple producer multiple consumer dynamic-sized array

So for your "multiple writer / single reader queue" usage I would choice either TPasMPBoundedQueue or TPasMPUnboundedQueue.

 

TinyPortal © 2005-2018