Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: parallel



Permlink Replies: 30 - Last Post: Oct 26, 2014 3:15 AM Last Post By: Alex Belo
Paolo Valle

Posts: 127
Registered: 11/27/00
parallel
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 15, 2014 10:56 AM
Hello,

are there "real Example" how to make parallel computation. I want to make a
parallel version of matrix product computation, any suggestion ?
Borja Serrano

Posts: 172
Registered: 1/10/13
Re: parallel
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 15, 2014 11:06 AM   in response to: Paolo Valle in response to: Paolo Valle
Take a look at Stephen Ball's blog. It has several posts related:

http://delphiaball.co.uk/

--
Borja Serrano
Embarcadero Technical Support Engineer
Paolo Valle

Posts: 127
Registered: 11/27/00
Re: parallel
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 15, 2014 11:27 AM   in response to: Borja Serrano in response to: Borja Serrano
the same "easy" examples... just increment a variable or something like
that... I have tried something more complex without success, probably I am
wrong in something...
any other resource on internet ?

thank you.

"Borja Serrano" ha scritto nel messaggio
news:697326 at forums dot embarcadero dot com...

Take a look at Stephen Ball's blog. It has several posts related:

http://delphiaball.co.uk/

--
Borja Serrano
Embarcadero Technical Support Engineer
Robert Triest

Posts: 687
Registered: 3/24/05
Re: parallel
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 16, 2014 12:26 AM   in response to: Paolo Valle in response to: Paolo Valle
I don't use this yet but the blog says that there are examples shipped with Delphi EX7, like a Conway's game of life example.
Is that also a too simple example for you?

"An other example that can help get your head around how to use the Parallel Programming library
is a sample of Conways game of life for both Object Pascal and C++ in the samples directory
shipped with XE7, located in the RTL samples. e.g.
C:\Users\Public\Documents\Embarcadero\Studio\15.0\Samples\Object Pascal\RTL\Parallel Library
C:\Users\Public\Documents\Embarcadero\Studio\15.0\Samples\CPP\RTL\Parallel Library"
Paolo Valle

Posts: 127
Registered: 11/27/00
Re: parallel
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 17, 2014 10:11 AM   in response to: Robert Triest in response to: Robert Triest
thank you, I'll have a look at the samples...

"Robert Triest" ha scritto nel messaggio
news:697430 at forums dot embarcadero dot com...

I don't use this yet but the blog says that there are examples shipped with
Delphi EX7, like a Conway's game of life example.
Is that also a too simple example for you?

"An other example that can help get your head around how to use the Parallel
Programming library
is a sample of Conways game of life for both Object Pascal and C++ in the
samples directory
shipped with XE7, located in the RTL samples. e.g.
C:\Users\Public\Documents\Embarcadero\Studio\15.0\Samples\Object
Pascal\RTL\Parallel Library
C:\Users\Public\Documents\Embarcadero\Studio\15.0\Samples\CPP\RTL\Parallel
Library"
Michael Rabatsc...

Posts: 125
Registered: 1/22/07
Re: parallel
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 16, 2014 12:03 AM   in response to: Paolo Valle in response to: Paolo Valle
Am 15.10.2014 19:56, schrieb Paolo Valle:
Hello,

are there "real Example" how to make parallel computation. I want to make a
parallel version of matrix product computation, any suggestion ?
I wrote such a library myself - you can check out the code from
https://code.google.com/p/mrmath/
or
http://mrsoft.org/

Quite a few matrix routines there are built for multithreading and
of course the multiplication too.

And if you want to go nuts ;) and do massive parallel algorithms
a good thing to start is https://code.google.com/p/delphi-opencl/

kind regards
Mike
Paolo Valle

Posts: 127
Registered: 11/27/00
Re: parallel
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 16, 2014 12:17 PM   in response to: Michael Rabatsc... in response to: Michael Rabatsc...
very intersting, thank you. But examples how to use the "for" or Itask" to
accomplish such things ?

"Michael Rabatscher" ha scritto nel messaggio
news:697424 at forums dot embarcadero dot com...

Am 15.10.2014 19:56, schrieb Paolo Valle:
Hello,

are there "real Example" how to make parallel computation. I want to make
a
parallel version of matrix product computation, any suggestion ?
I wrote such a library myself - you can check out the code from
https://code.google.com/p/mrmath/
or
http://mrsoft.org/

Quite a few matrix routines there are built for multithreading and
of course the multiplication too.

And if you want to go nuts ;) and do massive parallel algorithms
a good thing to start is https://code.google.com/p/delphi-opencl/

kind regards
Mike
Michael Rabatsc...

Posts: 125
Registered: 1/22/07
Re: parallel
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 16, 2014 11:43 PM   in response to: Paolo Valle in response to: Paolo Valle
Paolo Valle wrote:
very intersting, thank you. But examples how to use the "for" or Itask" to
accomplish such things ?

The library contains it's own threading pool classes so it is compatibel up to Delphi 2007 and
does not rely on any 3rd party components (like OmniThread - though you absolutely need
to take a look at this component suit it's terrific!)
It also implements 3 kind of thread pools: one that has been home brewn (derrived from A. Hauslandes
async calls), one based on the windows thread pool api and one which is compatible with MacOS.
If you are curious you can see how the multithreading parts work in the unit ThreadedMatrixOperations.pas in the mrMath library.
The routines are not that long and should be easily portable to a paralell for loop in XE7.

kind regards
Mike


"Michael Rabatscher" ha scritto nel messaggio
news:697424 at forums dot embarcadero dot com...

Am 15.10.2014 19:56, schrieb Paolo Valle:
Hello,

are there "real Example" how to make parallel computation. I want to make
a
parallel version of matrix product computation, any suggestion ?
I wrote such a library myself - you can check out the code from
https://code.google.com/p/mrmath/
or
http://mrsoft.org/

Quite a few matrix routines there are built for multithreading and
of course the multiplication too.

And if you want to go nuts ;) and do massive parallel algorithms
a good thing to start is https://code.google.com/p/delphi-opencl/

kind regards
Mike
Paolo Valle

Posts: 127
Registered: 11/27/00
Re: parallel
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 17, 2014 8:19 AM   in response to: Paolo Valle in response to: Paolo Valle
.. and how Can I download the source codes from https://code.google.com/p/mrmath/, I am a little bit confused (is there a button for the dowload ? on which page of web site ?).

thank you in advance

Paolo Valle wrote:
Hello,

are there "real Example" how to make parallel computation. I want to make a
parallel version of matrix product computation, any suggestion ?
Ian Barker

Posts: 98
Registered: 5/18/07
Re: parallel
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 17, 2014 8:48 AM   in response to: Paolo Valle in response to: Paolo Valle
Paolo Valle wrote:

.. and how Can I download the source codes from
https://code.google.com/p/mrmath/, I am a little bit confused (is
there a button for the dowload ? on which page of web site ?).

thank you in advance

Paolo Valle wrote:
Hello,

are there "real Example" how to make parallel computation. I want
to make a parallel version of matrix product computation, any
suggestion ?

You should use SVN to download the code:

svn checkout http://mrmath.googlecode.com/svn/trunk/ mrmath-read-only

However if you go to
https://code.google.com/p/mrmath/source/browse/#svn%2Ftrunk you can
view the code if you really want to.

--
IanB.
http://about.me/IanBarker
Paolo Valle

Posts: 127
Registered: 11/27/00
Re: parallel
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 17, 2014 10:02 AM   in response to: Ian Barker in response to: Ian Barker
.. I have to download one-by-one the pas files ?

"Ian Barker" ha scritto nel messaggio news:697835 at forums dot embarcadero dot com...

Paolo Valle wrote:

.. and how Can I download the source codes from
https://code.google.com/p/mrmath/, I am a little bit confused (is
there a button for the dowload ? on which page of web site ?).

thank you in advance

Paolo Valle wrote:
Hello,

are there "real Example" how to make parallel computation. I want
to make a parallel version of matrix product computation, any
suggestion ?

You should use SVN to download the code:

svn checkout http://mrmath.googlecode.com/svn/trunk/ mrmath-read-only

However if you go to
https://code.google.com/p/mrmath/source/browse/#svn%2Ftrunk you can
view the code if you really want to.

--
IanB.
http://about.me/IanBarker
Michael Rabatsc...

Posts: 125
Registered: 1/22/07
Re: parallel
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 18, 2014 2:09 AM   in response to: Paolo Valle in response to: Paolo Valle
Hi!

You can download the project as a zip file on the download page on mrsoft.org

Kind regards
Mike


Paolo Valle wrote:
.. I have to download one-by-one the pas files ?

"Ian Barker" ha scritto nel messaggio news:697835 at forums dot embarcadero dot com...

Paolo Valle wrote:

.. and how Can I download the source codes from
https://code.google.com/p/mrmath/, I am a little bit confused (is
there a button for the dowload ? on which page of web site ?).

thank you in advance

Paolo Valle wrote:
Hello,

are there "real Example" how to make parallel computation. I want
to make a parallel version of matrix product computation, any
suggestion ?

You should use SVN to download the code:

svn checkout http://mrmath.googlecode.com/svn/trunk/ mrmath-read-only

However if you go to
https://code.google.com/p/mrmath/source/browse/#svn%2Ftrunk you can
view the code if you really want to.

--
IanB.
http://about.me/IanBarker
Paolo Valle

Posts: 127
Registered: 11/27/00
Re: parallel
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 18, 2014 8:36 AM   in response to: Michael Rabatsc... in response to: Michael Rabatsc...
downloaded and installed.

I had to remove the line " NMF in 'NMF.pas'," in the dpr file becasue the
file is not in the zip file. The The installation was OK.

The I run "MathUtilsTests", it works with the exception of
TestNMFPersistence test where a rìcrash is follwed by: "floating point
invalid operation"

I am using Delphi XE7

"Michael Rabatscher" ha scritto nel messaggio
news:697958 at forums dot embarcadero dot com...

Hi!

You can download the project as a zip file on the download page on
mrsoft.org

Kind regards
Mike


Paolo Valle wrote:
.. I have to download one-by-one the pas files ?

"Ian Barker" ha scritto nel messaggio
news:697835 at forums dot embarcadero dot com...

Paolo Valle wrote:

.. and how Can I download the source codes from
https://code.google.com/p/mrmath/, I am a little bit confused (is
there a button for the dowload ? on which page of web site ?).

thank you in advance

Paolo Valle wrote:
Hello,

are there "real Example" how to make parallel computation. I want
to make a parallel version of matrix product computation, any
suggestion ?

You should use SVN to download the code:

svn checkout http://mrmath.googlecode.com/svn/trunk/ mrmath-read-only

However if you go to
https://code.google.com/p/mrmath/source/browse/#svn%2Ftrunk you can
view the code if you really want to.

--
IanB.
http://about.me/IanBarker
Michael Rabatsc...

Posts: 125
Registered: 1/22/07
Re: parallel
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 20, 2014 12:38 AM   in response to: Paolo Valle in response to: Paolo Valle
I had to remove the line " NMF in 'NMF.pas'," in the dpr file becasue the
file is not in the zip file. The The installation was OK.

Oh right - that is an older file reference in the package.
I already fixed that in the zip file.
The test application shouldn't have that problem though.


The I run "MathUtilsTests", it works with the exception of
TestNMFPersistence test where a rìcrash is follwed by: "floating point
invalid operation"

This is due to the nature of the non negative matrix calculation
which is an iterative procedure. This procedure easily converges
to a point where the update becomes so small that
a NAN occurs.
Anyway this exception is catched and also used as an indication
to stop the procedure - if that occurs the last known good
result is returned.

kind regards
Mike
Paolo Valle

Posts: 127
Registered: 11/27/00
Re: parallel
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 18, 2014 2:08 AM   in response to: Paolo Valle in response to: Paolo Valle
what is wrong in this code ?

const
cMax = 300;

Type
TMat = array [0..TMax-1, 0..TMax-1] of real;

var
mat1, mat2, mat3, :TMat;

TParallel.For(1, cMax-1, procedure (I: Integer)
var j, k :integer;
begin
for j := 0 to cMax-1 do begin
Mat3[i,j]:=0;
for k := 0 to cMax-1 do begin
Mat3[i,j]:=Mat3[i,j]+Mat1[i,k]*Mat2[k,j];
end;
end;
end);

It seems never splitted among the cores !

"Paolo Valle" ha scritto nel messaggio
news:697322 at forums dot embarcadero dot com...

Hello,

are there "real Example" how to make parallel computation. I want to make a
parallel version of matrix product computation, any suggestion ?
Michael Rabatsc...

Posts: 125
Registered: 1/22/07
Re: parallel
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 20, 2014 1:17 AM   in response to: Paolo Valle in response to: Paolo Valle
what is wrong in this code ?

const
cMax = 300;

Type
TMat = array [0..TMax-1, 0..TMax-1] of real;

var
mat1, mat2, mat3, :TMat;

TParallel.For(1, cMax-1, procedure (I: Integer)
var j, k :integer;
begin
for j := 0 to cMax-1 do begin
Mat3[i,j]:=0;
for k := 0 to cMax-1 do begin
Mat3[i,j]:=Mat3[i,j]+Mat1[i,k]*Mat2[k,j];
end;
end;
end);

Hard to tell - from a simple look at it it seems right.
First I don't know the XE7 parallel library but here are a
few comments:

First: 300x300 is quite small though so it could be that
the thread creation time takes longer than the actual procedure
Try to change the value to at least 1000.
I don't know the library but for matrix multiplication it
would be good to have a preallocated pool of threads.

Compare the single core and multicore variants using the TStopWatch
class (or QueryPerformanceCounter)

Second: to get the best out the cores you need to take the
core's architecture into account. Matrix multiplication
is best done block wise so the cores can make the best
out of their local caches!
In the badest scenario the matrix multiplication runs
at memory speed not at the full cpu speed.

Third: don't use the real datatype any more either use single or double
instead. AFAIK real is an alias of double and I think more or less there
for legacy applications

kind regards
Mike


It seems never splitted among the cores !

"Paolo Valle" ha scritto nel messaggio
news:697322 at forums dot embarcadero dot com...

Hello,

are there "real Example" how to make parallel computation. I want to make a
parallel version of matrix product computation, any suggestion ?
Alex Belo

Posts: 626
Registered: 10/8/06
Re: parallel
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 20, 2014 2:25 AM   in response to: Michael Rabatsc... in response to: Michael Rabatsc...
Michael Rabatscher wrote:

In the badest scenario

From my dictionary:

bad
Positive degree - bad
Comparative degree - worse
Superlative degree - worst
Michael Rabatsc...

Posts: 125
Registered: 1/22/07
Re: parallel
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 20, 2014 10:55 PM   in response to: Alex Belo in response to: Alex Belo
Alex Belo wrote:
Michael Rabatscher wrote:

In the badest scenario

From my dictionary:

bad
Positive degree - bad
Comparative degree - worse
Superlative degree - worst

OH RIGHT... When clicked the send button and read it again I got
the feeling there was something wrong with it ;)
Steve Thackery

Posts: 151
Registered: 4/29/06
Re: parallel
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 21, 2014 7:33 AM   in response to: Alex Belo in response to: Alex Belo
Alex Belo wrote:

Positive degree - bad
Comparative degree - worse
Superlative degree - worst

Yeah, but what about "even worser"? Oh, and of course we mustn't
forget "baddest". I was horrified to hear "funner" a couple of years
ago.

--
SteveT
Alex Belo

Posts: 626
Registered: 10/8/06
Re: parallel
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 21, 2014 9:45 PM   in response to: Steve Thackery in response to: Steve Thackery
Steve Thackery wrote:

Alex Belo wrote:
In the badest scenario

Positive degree - bad
Comparative degree - worse
Superlative degree - worst

I'm definitely not expert in English (sometimes when I ask myself in
what area I'm expert I can not find good answer :)) but my former
English teacher would have been unhappy. :)

what about "even worser"?

What's wrong with "even worser"? Google translator seems happy...

--
ALex
Steve Thackery

Posts: 151
Registered: 4/29/06
Re: parallel
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 22, 2014 3:19 AM   in response to: Alex Belo in response to: Alex Belo
Alex Belo wrote:

What's wrong with "even worser"? Google translator seems happy...

It shouldn't be. There is no such word as "worser". You would say
"This one is worse than the other"; "The weather has got worse"; and
the next day the weather might be "even worse". But never even worser!

--
SteveT
Alex Belo

Posts: 626
Registered: 10/8/06
Re: parallel
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 22, 2014 9:46 PM   in response to: Steve Thackery in response to: Steve Thackery
Steve Thackery wrote:

What's wrong with "even worser"? Google translator seems happy...

It shouldn't be.

Ah, yes, where is no 'r'! Most likely eye subconsciously adds
non-existent 'r' at the end of a word to generate "standard"
comparative degree. :)

You would say "This one is worse than the other".

Thank you, I'll remember this. :)

--
Alex
Steve Thackery

Posts: 151
Registered: 4/29/06
Re: parallel
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 25, 2014 8:09 AM   in response to: Alex Belo in response to: Alex Belo
Alex Belo wrote:

Thank you, I'll remember this. :)

Hey, Alex, if you lived in Derbyshire you'd hear it pronounced "woss"
or "wuss". I wouldn't be surprised if some spotty kids think it's
spelt that way, too!

--
SteveT
Steve Thackery

Posts: 151
Registered: 4/29/06
Re: parallel
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 25, 2014 8:21 AM   in response to: Steve Thackery in response to: Steve Thackery
Steve Thackery wrote:

Sorry, I know this is boring, and I promise no more. But I've
remembered yet another Derbyshire pronunciation of "worse".

Think about the "e" sound in "egg". It's a distinctly strong version
of "e", which you pronounce by making your mouth "wide" inside (if you
see what I mean).

Anyway, say "e" like in "egg", but elongate it to at least three or
four times as long. It's a sound that is never used in normal English
pronunciation. Except in Derbyshire, that is. In some parts they say
"worse" but with that strange, drawn out "e"-as-in-egg sound. I'd type
it a bit like this: "we-e-e-ehs".

Still, in New York they say "woise", which is just as strange.

Here in Nottingham (next door to Derbyshire), terminal "ee" sounds, as
in coffee, become "eh". Thus "Would you like a cup of coffeh?" "I am
happeh". The same people also put a "yuh" into the middle of longer
vowel sounds. Thus the name "Jake" is pronounced "Jayuk". It is a
peculiarly ugly accent, to be honest.

Right, this is so far OT I promise to stop immediately.

--
SteveT
Alex Belo

Posts: 626
Registered: 10/8/06
Re: parallel
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 26, 2014 3:15 AM   in response to: Steve Thackery in response to: Steve Thackery
Steve Thackery wrote:

Hey, Alex, if you lived in Derbyshire you'd hear it pronounced "woss"
or "wuss".

God spared me, I live in Siberia. :)

--
Alex
Wayne Niddery

Posts: 791
Registered: 4/14/98
Re: parallel
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 25, 2014 7:50 AM   in response to: Alex Belo in response to: Alex Belo
"Alex Belo" <b dot a dot v at inbox dot ru> wrote in message
news:698585 at forums dot embarcadero dot com...

What's wrong with "even worser"? Google translator seems happy...

Because it is not "betterer" than worse.

--
Wayne Niddery
"You know what they call alternative medicine that has been proven to work?
Medicine." - Tim Minchin

Paolo Valle

Posts: 127
Registered: 11/27/00
Re: parallel
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 20, 2014 9:04 AM   in response to: Michael Rabatsc... in response to: Michael Rabatsc...
thanks for the suggestions, anyway if I size the matrix to be more than 360
x360 the application after button presses said "stack overflow" ????

"Michael Rabatscher" ha scritto nel messaggio
news:698143 at forums dot embarcadero dot com...

what is wrong in this code ?

const
cMax = 300;

Type
TMat = array [0..TMax-1, 0..TMax-1] of real;

var
mat1, mat2, mat3, :TMat;

TParallel.For(1, cMax-1, procedure (I: Integer)
var j, k :integer;
begin
for j := 0 to cMax-1 do begin
Mat3[i,j]:=0;
for k := 0 to cMax-1 do begin
Mat3[i,j]:=Mat3[i,j]+Mat1[i,k]*Mat2[k,j];
end;
end;
end);

Hard to tell - from a simple look at it it seems right.
First I don't know the XE7 parallel library but here are a
few comments:

First: 300x300 is quite small though so it could be that
the thread creation time takes longer than the actual procedure
Try to change the value to at least 1000.
I don't know the library but for matrix multiplication it
would be good to have a preallocated pool of threads.

Compare the single core and multicore variants using the TStopWatch
class (or QueryPerformanceCounter)

Second: to get the best out the cores you need to take the
core's architecture into account. Matrix multiplication
is best done block wise so the cores can make the best
out of their local caches!
In the badest scenario the matrix multiplication runs
at memory speed not at the full cpu speed.

Third: don't use the real datatype any more either use single or double
instead. AFAIK real is an alias of double and I think more or less there
for legacy applications

kind regards
Mike


It seems never splitted among the cores !

"Paolo Valle" ha scritto nel messaggio
news:697322 at forums dot embarcadero dot com...

Hello,

are there "real Example" how to make parallel computation. I want to make
a
parallel version of matrix product computation, any suggestion ?
Paolo Valle

Posts: 127
Registered: 11/27/00
Re: parallel
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 20, 2014 9:25 AM   in response to: Michael Rabatsc... in response to: Michael Rabatsc...
to be more precise:

<<<<<<<<<<<<<<<<<<<<<<<<<<<<THIS NOW WORK ! i see in the task manager all
the cores working (I always use double type, here real just for example)

const
cMax = 1000;

Type
TMat = array [0..TMax-1, 0..TMax-1] of real;

var
mat1, mat2, mat3, :TMat;

procedure TForm1.Button1Click(Sender: TObject);
begin
TParallel.For(1, cMax-1, procedure (I: Integer)
var j, k :integer;
begin
for j := 0 to cMax-1 do begin
Mat3[i,j]:=0;
for k := 0 to cMax-1 do begin
Mat3[i,j]:=Mat3[i,j]+Mat1[i,k]*Mat2[k,j];
end;
end;
end);
end;

<<<<<<<<<<<<<<<<<<<THIS DOESN'T WORK (I don't understand way !)

const
cMax = 358; //ABOVE THIS VALUE "STACK OVERFLOW", is it due to the usage
of "result" implict variable in the matrix product ?

Type
TMat = array [0..TMax-1, 0..TMax-1] of real;

var
mat1, mat2, mat3, :TMat;

procedure TForm1.Button3Click(Sender: TObject);
var
i, j : integer;
begin
for i := 0 to TMax-1 do begin
for j := 0 to TMax-1 do begin
Mat1[i,j] :=0;
if i = j then
Mat1[i,j]:=1;
end;
end;
for i := 0 to TMax-1 do begin
for j := 0 to TMax-1 do begin
Mat2[i,j] :=0;
if i = j then
Mat2[i,j]:=1;
end;
end;
Mat3:=prod(Mat1, Mat2);
end;

function prod(var Amat1, AMat2 : TMat) : TMat;
var
j, i: Integer;
k: Integer;
begin
for i := 0 to TMax-1 do begin
for j := 0 to TMax-1 do begin
Result[i,j]:=0;
for k := 0 to TMax-1 do begin
Result[i,j]:=Result[i,j]+AMat1[i,k]*AMat2[k,j];
end;
end;
end;
end;

"Michael Rabatscher" ha scritto nel messaggio
news:698143 at forums dot embarcadero dot com...

what is wrong in this code ?

const
cMax = 300;

Type
TMat = array [0..TMax-1, 0..TMax-1] of real;

var
mat1, mat2, mat3, :TMat;

TParallel.For(1, cMax-1, procedure (I: Integer)
var j, k :integer;
begin
for j := 0 to cMax-1 do begin
Mat3[i,j]:=0;
for k := 0 to cMax-1 do begin
Mat3[i,j]:=Mat3[i,j]+Mat1[i,k]*Mat2[k,j];
end;
end;
end);

Hard to tell - from a simple look at it it seems right.
First I don't know the XE7 parallel library but here are a
few comments:

First: 300x300 is quite small though so it could be that
the thread creation time takes longer than the actual procedure
Try to change the value to at least 1000.
I don't know the library but for matrix multiplication it
would be good to have a preallocated pool of threads.

Compare the single core and multicore variants using the TStopWatch
class (or QueryPerformanceCounter)

Second: to get the best out the cores you need to take the
core's architecture into account. Matrix multiplication
is best done block wise so the cores can make the best
out of their local caches!
In the badest scenario the matrix multiplication runs
at memory speed not at the full cpu speed.

Third: don't use the real datatype any more either use single or double
instead. AFAIK real is an alias of double and I think more or less there
for legacy applications

kind regards
Mike


It seems never splitted among the cores !

"Paolo Valle" ha scritto nel messaggio
news:697322 at forums dot embarcadero dot com...

Hello,

are there "real Example" how to make parallel computation. I want to make
a
parallel version of matrix product computation, any suggestion ?
Michael Rabatsc...

Posts: 125
Registered: 1/22/07
Re: parallel
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 20, 2014 11:20 PM   in response to: Paolo Valle in response to: Paolo Valle
Paolo Valle wrote:
to be more precise:

<<<<<<<<<<<<<<<<<<<<<<<<<<<<THIS NOW WORK ! i see in the task manager all
the cores working (I always use double type, here real just for example)

const
cMax = 1000;

Type
TMat = array [0..TMax-1, 0..TMax-1] of real;

var
mat1, mat2, mat3, :TMat;

procedure TForm1.Button1Click(Sender: TObject);
begin
TParallel.For(1, cMax-1, procedure (I: Integer)
var j, k :integer;
begin
for j := 0 to cMax-1 do begin
Mat3[i,j]:=0;
for k := 0 to cMax-1 do begin
Mat3[i,j]:=Mat3[i,j]+Mat1[i,k]*Mat2[k,j];
end;
end;
end);
end;

<<<<<<<<<<<<<<<<<<<THIS DOESN'T WORK (I don't understand way !)

const
cMax = 358; //ABOVE THIS VALUE "STACK OVERFLOW", is it due to the usage
of "result" implict variable in the matrix product ?

Type
TMat = array [0..TMax-1, 0..TMax-1] of real;

var
mat1, mat2, mat3, :TMat;

procedure TForm1.Button3Click(Sender: TObject);
var
i, j : integer;
begin
for i := 0 to TMax-1 do begin
for j := 0 to TMax-1 do begin
Mat1[i,j] :=0;
if i = j then
Mat1[i,j]:=1;
end;
end;
for i := 0 to TMax-1 do begin
for j := 0 to TMax-1 do begin
Mat2[i,j] :=0;
if i = j then
Mat2[i,j]:=1;
end;
end;
Mat3:=prod(Mat1, Mat2);
end;

I checked the assembler code a bit and I think the compiler does really something stupid here:
1.) in the begin line it reserves the complete matrix in a hidden stack variable (and that in a really stupid
way! Instead of just adding a predefined constant to the ESP variable it loops in 4KB Blocks until
the size matches the matrix size)
2.) the result is built on the stack
3.) At the end it copies back the result to the variable mat3 in a processor loop using rep movsd

The problem here is that the stack itself is per default only 1MB big so your matrix is just a little bit too
big for the stack.

Normally there shouldn't be any hidden stack variable - the result should be treated as a hidden var
parameter (as the other two) which means the function should receive the pointer to mat3 instead
of a pointer to the stack.

kind regards
Mike

Paolo Valle

Posts: 127
Registered: 11/27/00
Re: parallel
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 22, 2014 2:14 PM   in response to: Michael Rabatsc... in response to: Michael Rabatsc...
Thank you, clear explanation. In fact the other version I did "Prod(var
Mat1, Mat2, Mat3 : TMat) works fine (Mat1 & Mat2 input and Mat3 Output)

"Michael Rabatscher" ha scritto nel messaggio
news:698349 at forums dot embarcadero dot com...

Paolo Valle wrote:
to be more precise:

<<<<<<<<<<<<<<<<<<<<<<<<<<<<THIS NOW WORK ! i see in the task manager all
the cores working (I always use double type, here real just for example)

const
cMax = 1000;

Type
TMat = array [0..TMax-1, 0..TMax-1] of real;

var
mat1, mat2, mat3, :TMat;

procedure TForm1.Button1Click(Sender: TObject);
begin
TParallel.For(1, cMax-1, procedure (I: Integer)
var j, k :integer;
begin
for j := 0 to cMax-1 do begin
Mat3[i,j]:=0;
for k := 0 to cMax-1 do begin
Mat3[i,j]:=Mat3[i,j]+Mat1[i,k]*Mat2[k,j];
end;
end;
end);
end;

<<<<<<<<<<<<<<<<<<<THIS DOESN'T WORK (I don't understand way !)

const
cMax = 358; //ABOVE THIS VALUE "STACK OVERFLOW", is it due to the
usage
of "result" implict variable in the matrix product ?

Type
TMat = array [0..TMax-1, 0..TMax-1] of real;

var
mat1, mat2, mat3, :TMat;

procedure TForm1.Button3Click(Sender: TObject);
var
i, j : integer;
begin
for i := 0 to TMax-1 do begin
for j := 0 to TMax-1 do begin
Mat1[i,j] :=0;
if i = j then
Mat1[i,j]:=1;
end;
end;
for i := 0 to TMax-1 do begin
for j := 0 to TMax-1 do begin
Mat2[i,j] :=0;
if i = j then
Mat2[i,j]:=1;
end;
end;
Mat3:=prod(Mat1, Mat2);
end;

I checked the assembler code a bit and I think the compiler does really
something stupid here:
1.) in the begin line it reserves the complete matrix in a hidden stack
variable (and that in a really stupid
way! Instead of just adding a predefined constant to the ESP variable it
loops in 4KB Blocks until
the size matches the matrix size)
2.) the result is built on the stack
3.) At the end it copies back the result to the variable mat3 in a processor
loop using rep movsd

The problem here is that the stack itself is per default only 1MB big so
your matrix is just a little bit too
big for the stack.

Normally there shouldn't be any hidden stack variable - the result should be
treated as a hidden var
parameter (as the other two) which means the function should receive the
pointer to mat3 instead
of a pointer to the stack.

kind regards
Mike

Paolo Valle

Posts: 127
Registered: 11/27/00
Re: parallel
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 25, 2014 1:51 AM   in response to: Michael Rabatsc... in response to: Michael Rabatsc...
The way it works should be updated as you suggest

"Michael Rabatscher" ha scritto nel messaggio
news:698349 at forums dot embarcadero dot com...

Paolo Valle wrote:
to be more precise:

<<<<<<<<<<<<<<<<<<<<<<<<<<<<THIS NOW WORK ! i see in the task manager all
the cores working (I always use double type, here real just for example)

const
cMax = 1000;

Type
TMat = array [0..TMax-1, 0..TMax-1] of real;

var
mat1, mat2, mat3, :TMat;

procedure TForm1.Button1Click(Sender: TObject);
begin
TParallel.For(1, cMax-1, procedure (I: Integer)
var j, k :integer;
begin
for j := 0 to cMax-1 do begin
Mat3[i,j]:=0;
for k := 0 to cMax-1 do begin
Mat3[i,j]:=Mat3[i,j]+Mat1[i,k]*Mat2[k,j];
end;
end;
end);
end;

<<<<<<<<<<<<<<<<<<<THIS DOESN'T WORK (I don't understand way !)

const
cMax = 358; //ABOVE THIS VALUE "STACK OVERFLOW", is it due to the
usage
of "result" implict variable in the matrix product ?

Type
TMat = array [0..TMax-1, 0..TMax-1] of real;

var
mat1, mat2, mat3, :TMat;

procedure TForm1.Button3Click(Sender: TObject);
var
i, j : integer;
begin
for i := 0 to TMax-1 do begin
for j := 0 to TMax-1 do begin
Mat1[i,j] :=0;
if i = j then
Mat1[i,j]:=1;
end;
end;
for i := 0 to TMax-1 do begin
for j := 0 to TMax-1 do begin
Mat2[i,j] :=0;
if i = j then
Mat2[i,j]:=1;
end;
end;
Mat3:=prod(Mat1, Mat2);
end;

I checked the assembler code a bit and I think the compiler does really
something stupid here:
1.) in the begin line it reserves the complete matrix in a hidden stack
variable (and that in a really stupid
way! Instead of just adding a predefined constant to the ESP variable it
loops in 4KB Blocks until
the size matches the matrix size)
2.) the result is built on the stack
3.) At the end it copies back the result to the variable mat3 in a processor
loop using rep movsd

The problem here is that the stack itself is per default only 1MB big so
your matrix is just a little bit too
big for the stack.

Normally there shouldn't be any hidden stack variable - the result should be
treated as a hidden var
parameter (as the other two) which means the function should receive the
pointer to mat3 instead
of a pointer to the stack.

kind regards
Mike

Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02