Tutor 42 Multiprocessing Programming
Tutor 42 Multiprocessing Programming
Synchronous or blocking
Asynchronous or non blocking
Dont confuse it with parallel programming (simultaneous) in which we can have two
synchronous functions in dependence but each in a separate thread to split and
speed up the results.
Sure, two asynchronous functions can be started at the same time if they can handle
parallel core processing and that's what we do now:
if ExecuteProcess(exepath+'maxbox3.exe '+
FILETO_RUN +' para1', SW_SHOW, 1, false) = 0 then
writeln('Multiprocessing Runs on CPU 1');
if ExecuteProcess(exepath+'maxbox3.exe '+
FILETO_RUN +' para2', SW_SHOW, 2, false) = 0 then
writeln('Multiprocessing Runs on CPU 2');
if ExecuteProcess(exepath+'maxbox3.exe '+
FILETO_RUN +' para3', SW_SHOW, 4, false) = 0 then
writeln('Multiprocessing Runs on CPU 3');
if ExecuteMultiProcessor(exepath+'maxbox3.exe '+
FILETO_RUN +' para4', SW_SHOW, 8, false) = 0 then begin
writeln('Multiprocessing Runs on CPU 4');
ShowMessage(SysErrorMessage(GetLastError))
end;
Its just a name convention that the last of the 4 calls is ExecuteMultiProcessor
to tell me its the last one in async mode but concerning operation its no difference
between ExecuteMultiProcessor or ExecuteProcess:
function ExecuteProcess(FileName: string; Visibility: Integer;
BitMask: Integer; Synch: Boolean): Longword;
The function runs a program on a specified set of CPUs on a multiprocessor system!
With the filename we specify the name of the program we want to launch, also with
parameters:
Const FILETO_RUN ='examples/044_queens_performer3.txt';
www.softwareschule.ch
Ideally, a dual or more core processor is nearly twice as powerful as a single core
processor. In practice, performance gains are said to be about fifty percent: a dual
www.softwareschule.ch
A function which returns no data (has no result value) can always be called
asynchronously, cause we dont have to wait for a result or theres no need to
synchronise the functions.
Lets begin with the application structure process in general of an API call:
The PlaySound function plays a sound specified by the given file name, resource, or
system event. (A system event may be associated with a sound in the registry or in
the WIN.INI file.)
1. Header: Declared in msystem.h; include Windows.h.
2. Library: Use Winmm.dll or the lib.
3. Declaration of the external function
4. Load the DLL and call the API function
Static at start time
Dynamic at run time
5. Unload the DLL (loaded DLL: C:\WINDOWS\system32\winmm.dll)
So we call the API function dynamic at runtime and of course asynchronously. The
sound is played asynchronously and PlaySound returns immediately after beginning
the sound. To terminate an asynchronously played waveform sound, call PlaySound
with pszSound set to NULL.
The API call to the function just works fine, doesn't have any glitch.
On the other side a sound is played synchronously, and PlaySound returns after
the sound event completes. This is the default behaviour in case you have a list of
songs.
If you click on the menu <Debug/Modules Count> you can see all the libraries
(modules) loaded by your app and of course also the winmm.dll with our function
PlaySound() in it.
25 function BOOL PlaySound(
LPCTSTR pszSound,
HMODULE hmod,
DWORD fdwSound
);
Before this starter code will work you will need to download maXbox from the
website. It can be get from http://sourceforge.net/projects/maxbox site. Once the
download has finished, unzip the file, making sure that you preserve the folder
structure as it is. If you double-click maxbox3.exe the box opens a default program.
Make sure the version is at least 3.9 because Modules Count use that. Test it with
F2 / F9 or press Compile and you should hear a sound a browser will open. So far
so good now well open the example:
630_multikernel3.TXT
www.softwareschule.ch
As we now know, PlaySound can't play two sounds at the same time, even if you do
use async flag. You might be able to get this to work by having two separate threads
both calling PlaySound synchronously.
The object will not let you play two sounds at once, even if you create two instances.
You will need to bring in the native windows API " mciSendString".
You can test it with F4 or menu /Output/New Instance which opens a second
instance of maXbox (see the following screenshot).
An example of the low-level mciSendString():
mciSendString(@"open C:\Users\Desktop\applause.wav type waveaudio
alias applause", null, 0, IntPtr.Zero);
mciSendString(@"play applause", null, 0, IntPtr.Zero);
www.softwareschule.ch
So your code will now show all three possibilities in a sequence. First with start with a
dialog which stops the control flow because its a modal dialog we have to wait and
pass it. Second on line 28 our function is called in sync-mode sync and we have to
wait or in other words we are blocked. Third in line 30 we call the same function with
the async flag set to 1 and now you can follow at the same time the music plays and
a loop of numbers on the output can be seen.
In the end of the multi-core processing you see also CPU time decay:
In line 33 we start almost the same time a sound twice, yes its the same sound
therefore you can hear the delay or an echo to prove its parallel! OK. Its a trick to
open another function with the same song to show the simultaneous mode.
If you increase the delay of sound (for example with sleep or with a massive CPU
payload), the echo effect space grows to more than milliseconds time of its start
offset.
//Result:= Writeln(FloatToStr(IntPower(62,8))) = 218340105584896
27
28
PlaySound(pchar(ExePath+'examples\maxbox.wav'), 0, 0);
29
Sleep(20)
30
PlaySound(pchar(ExePath+'examples\maxbox.wav'), 0, 1);
31
//Parallel
32
//sleep(20)
33
closeMP3;
34
playMP3(mp3path);
www.softwareschule.ch
//Sync
//Async
//Parallel Trick
35
36
37
38
inFrm.color:= clblue;
39
//inFrm.close;
40 End.
This sleep is here just so you can distinguish the two sounds playing
simultaneously.
By the way: You can also set a hash function around a sound file to distinguish it.
A hash function is a (mathematical) function which receives an input of arbitrary
length and returns a function value (hash val) of a fixed length (usually 128/160 bits).
Till now we are discussing the topics of sync and async calls and the way it
processes the calls by the receiver parallel or not. Asynchronous calls or connections
allow your app to continue processing without waiting for the process (function) to be
completely closed but not always in a simultaneous mode.
Avoiding bottlenecks. With only one thread, a program must stop all
execution when waiting for slow processes such as accessing files on
disk, communicating with other machines, or displaying multimedia
www.softwareschule.ch
content. The CPU sits idle until the process completes. With multiple
threads, your application can continue execution in separate threads
while one thread waits for the results of a slow process.
Organizing program behaviour. Often, a program's run can be
organized into several parallel processes that function independently.
Use threads to launch a single section of code simultaneously for each
of these parallel cases.
Multiprocessing. If the system running your program has multiple
processors, you can improve performance by dividing the work into
several threads and letting them run simultaneously on separate
processors.
In case youre new to the concept, a thread is basically a very beneficial alternative
(in most cases) to spawning a new process. Programs you use every day make great
use of threads whether you know it or not but you have to program it.
The most basic example is whenever youre using a program that has a lengthy task
to achieve (say, downloading a file or backing up a database), the program (on the
server) will most likely spawn a thread to do that job.
This is due to the fact that if a thread was not created, the main thread (where your
main function is running) would be stuck waiting for the event to complete, and the
screen would freeze.
For example first is a listener thread that listens and waits for a connection. So we
don't have to worry about threads, the built in thread will be served by for example
Indy though parameter:
IdTCPServer1Execute(AThread: TIdPeerThread)
When our DWS-client is connected, these threads transfer all the communication
operations to another thread. This technique is very efficient because your client
application will be able to connect any time, even if there are many different
connections to the server. The second command " CTR_FILE" transfers the app to the
client:
Or another example is AES cryptography which is used to exchange encrypted data
with other users in a parallel way but not a parallel function. It does not contain
functions for key management. The keys have to be exchanged between the users
on a secure parallel channel. In our case we just use a secure password!
88 procedure EncryptMediaAES(sender: TObject);
106
107
Start;
108
AESSymetricExecute(selectFile, selectFile+'_encrypt',AESpassw);
109
mpan.font.color:= clblue;
110
mpan.font.size:= 30;
111
www.softwareschule.ch
112
Screen.Cursor:= crDefault;
113
Stop;
114
115
116
117
Because Synchronize uses a form message loop, it does not work in console
applications. For console applications use other mechanisms, such as a mutex (see
graph below) or critical sections, to protect access to RTL or VCL objects.
The point is you can combine asynchronous calls with threads! For example
asynchronous data fetches or command execution does not block a current thread of
execution.
But then your function or object has to be thread safe. So whats thread-safe.
Because multiple clients can access for example your remote data module simultaneously, you must guard your instance data (properties, contained objects, and so
on) as well as global variables.
Tasks for advanced studies:
www.softwareschule.ch
10
How can you crack a password with a massive parallel concept? Study all
about a salt. A 512-bit salt is used by password derived keys, which means
there are 2^512 keys for each password. So a same password doesnt
generate always a same key. This significantly decreases vulnerability to
'off-line' dictionary' attacks (pre-computing all keys for a dictionary is very
difficult when a salt is used). The derived key is returned as a hex or
base64 encoded string. The salt must be a string that uses the same
encoding.
We also use a lot of more multi scripts to teach and the wish to enhance it with a
thread, simply take a look in the meantime at 141_thread.txt,
210_public_private_cryptosystem.txt and 138_sorting_swap_search2.txt. At
least lets say a few words about massive threads and parallel programming and
what functions they perform.
Do not create too many threads in your apps. The overhead in managing multiple
threads can impact performance. The recommended limit is 16 threads per process
on single processor systems. This limit assumes that most of those threads are
waiting for external events. If all threads are active, you will want to use fewer.
You can create multiple instances of the same thread type to execute parallel code.
For example, you can launch a new instance of a thread in response to some user
action, allowing each thread to perform the expected response.
One note about async execution with fork on Linux with libc-commands; there
will be better solutions (execute and wait and so on) and we still work on it, so I'm
curious about comments, therefore my aim is to publish improvements in a basic
framework on sourceforge.net depends on your feedback ;)
Try to change the sound file in order to compare two sounds at the same time:
04 pchar(ExePath+'examples\maxbox.wav'));
Try to find out more about the synchronisation object schema above and the
question if it works in a synchronous or asynchronous mode.
www.softwareschule.ch
11
h,ym
This is the last starter of the sequel 1-42. The work has been
finished!
[email protected]
https://github.com/maxkleiner/maXbox3/releases
http://sourceforge.net/projects/delphiwebstart
http://www.softwareschule.ch/maxbox.htm
http://sourceforge.net/projects/maxbox
http://sourceforge.net/apps/mediawiki/maxbox/
My Own Experiences:
http://www.softwareschule.ch/download/armasuisse_components.pdf
SHA1:
CRC32:
maXbox3.exe F0AB7D054111F5CE46BA122D6280397A841C6FAB
maXbox3.exe 602A885C
www.softwareschule.ch
12