[85] | 1 | #!/usr/bin/env python |
---|
| 2 | |
---|
| 3 | # Timing of MPI module for Python and estimation of latency and bandwidth |
---|
| 4 | # |
---|
| 5 | # Send numerical array in a ring from processor 0 to 1 etc back to 0 |
---|
| 6 | # Perform timings and compare different sending strategies |
---|
| 7 | # |
---|
| 8 | # OMN, OCT 2001 |
---|
| 9 | |
---|
| 10 | import time, sys, pypar, Numeric |
---|
| 11 | |
---|
| 12 | # The send/recv routines |
---|
| 13 | # |
---|
| 14 | import pypar |
---|
| 15 | |
---|
| 16 | #-------------------------------------------------------------- |
---|
| 17 | |
---|
| 18 | raw = 1 # Use raw communication (1) or user friendly (0) |
---|
| 19 | vanilla = 0 # Force use of vanilla format (1) |
---|
| 20 | consistency_check = 1 # Check correctness |
---|
| 21 | |
---|
| 22 | #-------------------------------------------------------------- |
---|
| 23 | if raw: |
---|
| 24 | Send = pypar.raw_send |
---|
| 25 | Recv = pypar.raw_receive |
---|
| 26 | else: |
---|
| 27 | Send = pypar.send |
---|
| 28 | Recv = pypar.receive |
---|
| 29 | |
---|
| 30 | |
---|
| 31 | #-------------------------------------------------------------- |
---|
| 32 | # linfit |
---|
| 33 | # |
---|
| 34 | def linfit(x, y): |
---|
| 35 | """Fit a and b to the model y = ax + b. Return a,b,variance |
---|
| 36 | """ |
---|
| 37 | |
---|
| 38 | Sx = Sy = SSoN = SxoN = norm = varest = 0.0 |
---|
| 39 | N = len(x) |
---|
| 40 | assert len(y) == N, "x and y must have same length" |
---|
| 41 | |
---|
| 42 | for i in range(N): |
---|
| 43 | #print("x,y = %f, %f\n",x[i],y[i]) |
---|
| 44 | Sx = Sx + x[i] |
---|
| 45 | Sy = Sy + y[i] |
---|
| 46 | |
---|
| 47 | SxoN = Sx/N |
---|
| 48 | |
---|
| 49 | a = 0.0 |
---|
| 50 | for i in range(N): |
---|
| 51 | t = x[i] - SxoN |
---|
| 52 | SSoN = SSoN + t*t |
---|
| 53 | a = a + t*y[i] |
---|
| 54 | |
---|
| 55 | |
---|
| 56 | a = a/SSoN # a = (N Sxy - SxSy)/(NSxx - Sx^2) */ |
---|
| 57 | b = (Sy - Sx*a)/N |
---|
| 58 | |
---|
| 59 | # Quality - variance estimate \sum_i r_i^2 /(m-n) |
---|
| 60 | for i in range(N): |
---|
| 61 | norm = norm + float(x[i])*x[i] |
---|
| 62 | res = y[i] - a*x[i] - b |
---|
| 63 | varest = varest + res*res |
---|
| 64 | |
---|
| 65 | varest = varest/norm/(N-2) |
---|
| 66 | return a, b, varest |
---|
| 67 | |
---|
| 68 | |
---|
| 69 | |
---|
| 70 | |
---|
| 71 | #-------------------------------------------------------------- |
---|
| 72 | # Main program |
---|
| 73 | # |
---|
| 74 | MAXI = 10 # Number of blocks |
---|
| 75 | MAXM = 500000 # Largest block |
---|
| 76 | BLOCK = MAXM/MAXI # Block size |
---|
| 77 | |
---|
| 78 | repeats = 10 |
---|
| 79 | msgid = 0 |
---|
| 80 | vanilla = 0 #Select vanilla mode (slower but general) |
---|
| 81 | |
---|
| 82 | numprocs = pypar.size() |
---|
| 83 | myid = pypar.rank() |
---|
| 84 | processor_name = pypar.Get_processor_name() |
---|
| 85 | |
---|
| 86 | if myid == 0: |
---|
| 87 | # Main process - Create message, pass on, verify correctness and log timing |
---|
| 88 | # |
---|
| 89 | print "MAXM = %d, number of processors = %d" %(MAXM, numprocs) |
---|
| 90 | print "Measurements are repeated %d times for reliability" %repeats |
---|
| 91 | |
---|
| 92 | if numprocs < 2: |
---|
| 93 | print "Program needs at least two processors - aborting\n" |
---|
| 94 | pypar.Abort() |
---|
| 95 | |
---|
| 96 | pypar.Barrier() #Synchronize all before timing |
---|
| 97 | print "I am process %d on %s" %(myid,processor_name) |
---|
| 98 | |
---|
| 99 | |
---|
| 100 | #Initialise data and timings |
---|
| 101 | # |
---|
| 102 | from RandomArray import uniform, seed |
---|
| 103 | seed(17, 53) |
---|
| 104 | A = uniform(0.0,100.0,MAXM) |
---|
| 105 | #A = uniform(0,100,MAXM).astype('l') |
---|
| 106 | elsize = A.itemsize() |
---|
| 107 | #print elsize |
---|
| 108 | |
---|
| 109 | noelem = [0]*MAXI |
---|
| 110 | bytes = [0]*MAXI |
---|
| 111 | avgtime = [0.0]*MAXI |
---|
| 112 | mintime = [ 1000000.0]*MAXI |
---|
| 113 | maxtime = [-1000000.0]*MAXI |
---|
| 114 | |
---|
| 115 | |
---|
| 116 | |
---|
| 117 | |
---|
| 118 | if myid == 0: |
---|
| 119 | # Determine timer overhead |
---|
| 120 | cpuOH = 1.0; |
---|
| 121 | for k in range(repeats): # Repeat to get reliable timings |
---|
| 122 | t1 = pypar.Wtime() |
---|
| 123 | t2 = pypar.Wtime() |
---|
| 124 | if t2-t1 < cpuOH: cpuOH = t2-t1 |
---|
| 125 | |
---|
| 126 | print "Timing overhead is %f seconds.\n" %cpuOH |
---|
| 127 | |
---|
| 128 | |
---|
| 129 | # Pass msg circularly |
---|
| 130 | for k in range(repeats): |
---|
| 131 | if myid == 0: |
---|
| 132 | print "Run %d of %d" %(k+1,repeats) |
---|
| 133 | |
---|
| 134 | for i in range(MAXI): |
---|
| 135 | m=BLOCK*i+1 |
---|
| 136 | |
---|
| 137 | noelem[i] = m |
---|
| 138 | |
---|
| 139 | pypar.Barrier() # Synchronize |
---|
| 140 | |
---|
| 141 | if myid == 0: |
---|
| 142 | # |
---|
| 143 | # Main process |
---|
| 144 | # |
---|
| 145 | t1 = pypar.Wtime() |
---|
| 146 | Send(A[:m], 1, msgid, vanilla) |
---|
| 147 | if raw: |
---|
| 148 | C = Recv(A[:m], numprocs-1, msgid, vanilla) |
---|
| 149 | else: |
---|
| 150 | C = Recv(numprocs-1, msgid) |
---|
| 151 | t2 = pypar.Wtime() - t1 - cpuOH |
---|
| 152 | t2 = t2/numprocs |
---|
| 153 | avgtime[i] = avgtime[i] + t2 |
---|
| 154 | if t2 < mintime[i]: mintime[i] = t2 |
---|
| 155 | if t2 > maxtime[i]: maxtime[i] = t2 |
---|
| 156 | |
---|
| 157 | # Uncomment to verify integrity of data |
---|
| 158 | # However, this may affect accuracy of timings for some reason. |
---|
| 159 | # |
---|
| 160 | if consistency_check: |
---|
| 161 | assert Numeric.alltrue(C == A[:m]) |
---|
| 162 | else: |
---|
| 163 | # |
---|
| 164 | # Parallel process - get msg and pass it on |
---|
| 165 | # |
---|
| 166 | if raw: |
---|
| 167 | C = Recv(A[0:m], myid-1, msgid, vanilla) |
---|
| 168 | else: |
---|
| 169 | C = Recv(myid-1, msgid) |
---|
| 170 | Send(C, (myid+1)%numprocs, msgid, vanilla) |
---|
| 171 | |
---|
| 172 | |
---|
| 173 | # Output stats |
---|
| 174 | # |
---|
| 175 | if myid == 0: |
---|
| 176 | print "Bytes transferred time (micro seconds)" |
---|
| 177 | print " min avg max " |
---|
| 178 | print "----------------------------------------------" |
---|
| 179 | |
---|
| 180 | for i in range(MAXI): |
---|
| 181 | avgtime[i] = avgtime[i]/repeats*1.0e6 #Average micro seconds |
---|
| 182 | mintime[i] = mintime[i]*1.0e6 #Min micro seconds |
---|
| 183 | maxtime[i] = maxtime[i]*1.0e6 #Min micro seconds |
---|
| 184 | |
---|
| 185 | m = noelem[i] |
---|
| 186 | bytes[i] = elsize*noelem[i] |
---|
| 187 | |
---|
| 188 | print "%10d %10d %10d %10d" %(bytes[i], mintime[i], avgtime[i], maxtime[i]) |
---|
| 189 | |
---|
| 190 | |
---|
| 191 | Tbw, Tlat, varest = linfit(bytes, mintime) |
---|
| 192 | print "\nLinear regression on best timings (t = t_l + t_b * bytes):\n", |
---|
| 193 | print " t_b = %f\n t_l = %f" %(Tbw,Tlat) |
---|
| 194 | print " Estimated relative variance = %.9f\n" %varest |
---|
| 195 | |
---|
| 196 | print "Estimated bandwith (1/t_b): %.3f Mb/s" %(1.0/Tbw) |
---|
| 197 | print "Estimated latency: %d micro s" %int(mintime[0]-bytes[0]*Tbw) |
---|
| 198 | |
---|
| 199 | |
---|
| 200 | pypar.Finalize() |
---|
| 201 | |
---|
| 202 | |
---|
| 203 | |
---|
| 204 | |
---|
| 205 | |
---|
| 206 | |
---|