I need to use Open MPI to distribute 2d-array in the PGM file among 10 working computers. Then I need to manipulate each value of the array to get a negative image (255-i) and then print the output back. I'm thinking of using mpi_scatter
and mpi_gather
to distribute the data. The problem now is how to read the 2-d array into sub array and send the sub array to each of the working computer to do the manipulation. I'm writing this program in C.
Can anyone can help me solve this problem or give an idea? Thank you.
Below are the example of array in the PGM file:
P2 # created by 'xv balloons_bw.tif' 640 480 255 232 227 220 216 212 209 207 206 205 205 205 207 208 209 210 211 212 211 211 213 212 211 210 209 210 210 211 212 211 210 210 210 210 211 210 210 210 210 209 210 209 208 209 208 209 210 209 208 210 209 209 208 208 208 209 208 208 208 207 207 207 206 207 207 207 207 207 207 207 207 207 207 205 204 206 205 205 204 204 204 203 202 203 202 201 201 201 200 199 199 200 199 198 198 198 197 197 198 197 196 195 195 194 193 192 192 191 191 190 190 190 190 189 189 190 188 188 188 187 187 187 186 186 186 186 187 186 186 187 188 188 187 186 186 186 185 186 186 186 187 186 186 186 185 185 187 186 185 186 185 185 186 185 184 185 186 185 186 186 186 185 186 185 185 185 184 183 184 184 183
I would normally agree with Shawn Chin about using existing libraries to do the file reading; in this case I might disagree because the file format is so simple and it's so important for MPI to know how the data is laid out in memory. A 2d nxm array allocated as a contiguous 1-d array of nxm is very different from rows scattered all over memory! As always, this is C's fault for not having real multi-d arrays. On the other hand, you could check out the libnetpbm libraries and see how it's allocated, or as Shawn suggests, copy the whole thing into contiguous memory after reading it in.
Note too that this would actually be easier with the (binary) P5 format, as one could use MPI-IO to read in the data in parallel right at the beginning, rather than having one processor doing all the reading and using scatter/gather to do the data distribution. With ascii files, you never really know how long a record is going to be, which makes coordinated I/O very difficult.
Also note that this really isn't a 2d problem - you are just doing an elementwise operation on every piece of the array. So you can greatly simplify things by just treating the data as a 1d array and ignoring the geometry. This wouldn't be the case if you were (say) applying a 2d filter to the image, as there the geometry matters and you'd have to partition data accordingly; but here we don't care.
Finally, even in this simple case you have to use scatterv and gatherv because the number of cells in the image might not evenly divide by the number of MPI tasks. You could simplify the logic here just by padding the array to make it evenly divide; then you could avoid some of the extra steps here.
So if you have a
read_pgm()
andwrite_pgm()
that you know return pointers into a single contiguous block of memory, you can do something like this: