posted 1 year ago

I'm very new to multithreading and don't have much experience using inner classes.

The task is to add two matrices containing double values in a parallelized way.

My idea was to do this recursively, splitting the big matrixes into smaller ones and performing the addition when the matrices reached a certain size limit, then fusing them.

The parallelized code runs 40-80x slower than the serialized code.

I suspect that I'm doing something wrong here. Perhaps it's because I create so many new matrices, or because I traverse them so many times.

Here is the code:

Thankful for any help.

The task is to add two matrices containing double values in a parallelized way.

My idea was to do this recursively, splitting the big matrixes into smaller ones and performing the addition when the matrices reached a certain size limit, then fusing them.

The parallelized code runs 40-80x slower than the serialized code.

I suspect that I'm doing something wrong here. Perhaps it's because I create so many new matrices, or because I traverse them so many times.

Here is the code:

Thankful for any help.

posted 1 year ago

Sounds reasonable; although you seem to have chosen a very complex way of "splitting" - after all, there's nothing that says a matrix

I suspect so too, but nothing

Many structural things do though:

1. You don't have a

2. All your methods are

3. You appear to assume that matrices are square.

4. (probably because of 3) You've chosen the

I think if I was trying to do this,

Forget all that "parallel" stuff for the moment and simply design a

Add itself to another Matrix and return the results in a third one - ie, Return a portion of itself as a "sub-Matrix" - ie, Replace a portion of itself with data from another and you may well find you need other stuff as well.

Once you have that, I suspect you'll find the business of "parallelling" much simpler. Right now, you're trying to solve everything at once with code; and it's holding you back.

HIH

Winston

Paul Folder wrote:My idea was to do this recursively, splitting the big matrixes into smaller ones and performing the addition when the matrices reached a certain size limit, then fusing them.

Sounds reasonable; although you seem to have chosen a very complex way of "splitting" - after all, there's nothing that says a matrix

*has*to be square.

I suspect that I'm doing something wrong here...

I suspect so too, but nothing

*logically*leaps out to me at the moment - although I don't like the fact that you

`join()`

*before*you do the calculation.

Many structural things do though:

1. You don't have a

`Matrix`class.

2. All your methods are

`static`.

3. You appear to assume that matrices are square.

4. (probably because of 3) You've chosen the

*most*compilcated way to "split" a matrix - ie, into smaller "squares".

I think if I was trying to do this,

`Matrix`is where I'd start.

Forget all that "parallel" stuff for the moment and simply design a

`Matrix`class that can at the very least

`public Matrix add(Matrix other)`.

`public Matrix subMatrix(int fromRow, int toRow)`.

`Matrix`- ie,

`public Matrix copyFrom(Matrix other, int toRow)`.

Once you have that, I suspect you'll find the business of "parallelling" much simpler. Right now, you're trying to solve everything at once with code; and it's holding you back.

HIH

Winston

"Leadership is nature's way of removing morons from the productive flow" - Dogbert

Articles by Winston can be found here

Piet Souris

Rancher

Posts: 1979

67

posted 1 year ago

- 1

hi Paul,

apart from Winstons remarks, I would say: just look

at the enormous amount of labour that goes into splitting

the two matrices into 12 other matrices, starting a number

of threads, joining back the results...

I bet the serial version is finished before your twelve

matrices are created. To be sure you would need

to split your code up into some methods and use

a profiler.

As an aside: what if the matrices are n x n, with n odd?

apart from Winstons remarks, I would say: just look

at the enormous amount of labour that goes into splitting

the two matrices into 12 other matrices, starting a number

of threads, joining back the results...

I bet the serial version is finished before your twelve

matrices are created. To be sure you would need

to split your code up into some methods and use

a profiler.

As an aside: what if the matrices are n x n, with n odd?