import mpi,sys from array import * from struct import * #Function to return BMP header def makeBMPHeader( width, height ): #Set up the bytes in the BMP header headerBytes = [ 66, 77, 28, 88, 0, 0, 0, 0, 0, 0, 54, 0, 0, 0] headerBytes += [40] + 3*[0] + [100] + 3*[0] + [ 75,0,0,0,1,0,24] + [0]*9 + [18,11,0,0,18,11] headerBytes += [0]*10 #Pack this data as a string data ="" for x in range(54): data += pack( 'B', headerBytes[x] ) #Create a string to overwrite the width and height in the BMP header replaceString = pack( ' distanceWhenUnbounded: break; if numIters >= maxIterationsPerPoint: break; nxt = f(nxt) numIters = numIters+1 #Convert the number of iterations to a color value colorFac = 255.0*float(numIters)/float(maxIterationsPerPoint) myRGB = ( colorFac*0.8 + 32, 24+0.1*colorFac, 0.5*colorFac ) #append this color value to a running list myArray.append( int(myRGB[2]) ) #blue first myArray.append( int(myRGB[1]) ) #The green myArray.append( int(myRGB[0]) ) #Red is last #Now I reduce the lists to process 0!! masterString = mpi.reduce( myArray.tostring(), mpi.SUM, 0 ) #Tell user that we're done message = "process " + str(mpi.rank) + " done with computation!!" print message #Process zero does the file writing if mpi.rank == 0: masterArray = array('B') masterArray.fromstring(masterString) #Write a BMP header myBMPHeader = makeBMPHeader( bmpSize[0], bmpSize[1] ) print "Header length is ", len(myBMPHeader) print "BMP size is ", bmpSize print "Data length is ", len(masterString) #Open the output file and write to the BMP outFile = open( 'output.bmp', 'w' ) outFile.write( myBMPHeader ) outFile.write( masterString ) outFile.close()