I'm having a hard time believing this isn't a dupe, but I can't find any posts answering this question, so I'll try to make it a good one.
Essentially the problem is this: you have a matrix of size Row X Column
and your number of processors is P
. The max size of your partition (the number of elements each processor is allocated) is ((Rows*Columns)/processors)+1
, and you'll have to do some logic to make sure you don't go out of bounds. Each processor is assigned a starting point and must jump forward P
indexes.
So on a 4x4 matrix which is sequentially numbered like the following:
[1] [2] [3] [4]
[5] [6] [7] [8]
[9] [10][11][12]
[13][14][15][16]
a processor with id 0
would get 1, 4, 7, 10, 13, and 16. (the max partition size).
The algorithm my instructor gave us is like this:
i / columns = row# and i % columns = column#
This worked for the examples he gave us like 6 / 4 = 1
and 6 % 4 = 2
so 6 is at index [1][2]
The logic is intuitive at least, but fails on several occasions.
So what is an algorithm which can reliably produce the indexes of the desired values on an NxM
matrix?
The "algorithm" given for calculating indices is based on all indices being zero-based, including the "matrix numbering":
0 1 2 3
0 0 1 2 3
1 4 5 6 7
2 8 9 10 11
3 12 13 14 15
Now you can see that 6 really is at row 1 column 2.