diracBenchmark.py 4.1 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#!/usr/bin/python

########################################################################
# File :    diracBenchmark.py
# Author :  Andrew McNab 
########################################################################

""" DIRAC Benchmark 2012 by Ricardo Graciani, and wrapper functions to
    run multiple instances in parallel
"""

import os
import sys
import random
import urllib
import multiprocessing

version = '0.1 DB12'

def singleDiracBenchmark( iterations = 1, resultObject = None ):
  """ Get Normalized Power of one CPU in DIRAC Benchmark 2012 units (DB12)
  """

  # This number of iterations corresponds to 1kHS2k.seconds, i.e. 250 HS06 seconds

  n = int( 1000 * 1000 * 12.5 )
  calib = 250.0

  m = long( 0 )
  m2 = long( 0 )
  p = 0
  p2 = 0
  # Do one iteration extra to allow CPUs with variable speed (we ignore zeroth iteration)
  for i in range( iterations + 1 ):
    if i == 1:
      start = os.times()
    # Now the iterations
    for _j in xrange( n ):
      t = random.normalvariate( 10, 1 )
      m += t
      m2 += t * t
      p += t
      p2 += t * t

  end = os.times()
  cput = sum( end[:4] ) - sum( start[:4] )
  wall = end[4] - start[4]

  if not cput:
    return None
  
  else:  
    if resultObject is not None:
      # This makes it easy to use with multiprocessing.Process
      resultObject.value = calib * iterations / cput

    # Return DIRAC-compatible values too
    return { 'CPU' : cput, 'WALL' : wall, 'NORM' : calib * iterations / cput, 'UNIT' : 'DB12' }

def multipleDiracBenchmark( instances = 1, iterations = 1 ):

  """ Run multiple instances of the DIRAC Benchmark in parallel  
  """

  processes = []
  results = []

  # Set up all the subprocesses
  for i in range( instances ):
    results.append( multiprocessing.Value('d', 0.0) )
    processes.append( multiprocessing.Process( target = singleDiracBenchmark, args = ( iterations, results[i] ) ) )
 
  # Start them all off at the same time 
  for p in processes:  
    p.start()
    
  # Wait for them all to finish
  for p in processes:
    p.join()

  raw = [ result.value for result in results ]

  # Return the list of raw results, and the sum and mean of the list
  return { 'raw' : raw, 'sum' : sum(raw), 'mean' : sum(raw)/len(raw) }
  
def wholenodeDiracBenchmark( instances = None, iterations = 1 ): 

  """ Run as many instances as needed to occupy the whole machine
  """
  
  # Try $MACHINEFEATURES first if not given by caller
  if not instances and 'MACHINEFEATURES' in os.environ:
    try:
      instances = int( urllib2.urlopen( os.environ['MACHINEFEATURES'] + '/total_cpu' ).read() )
    except:
      pass

  # If not given by caller or $MACHINEFEATURES/total_cpu then just count CPUs
  if not instances:
    try:
      instances = multiprocessing.cpu_count()
    except:
      instances = 1
  
  return multipleDiracBenchmark( instances = instances, iterations = iterations )
  
def jobslotDiracBenchmark( instances = None, iterations = 1 ):

  """ Run as many instances as needed to occupy the job slot
  """

  # Try $JOBFEATURES first if not given by caller
  if not instances and 'JOBFEATURES' in os.environ:
    try:
      instances = int( urllib2.urlopen( os.environ['JOBFEATURES'] + '/allocated_cpu' ).read() )
    except:
      pass

  # If not given by caller or $JOBFEATURES/allocated_cpu then just run one instance
  if not instances:
    instances = 1
  
  return multipleDiracBenchmark( instances = instances, iterations = iterations )

#
# If we run as a command
#   
if __name__ == "__main__":

  if len(sys.argv) == 1 or sys.argv[1] == 'single':
    print singleDiracBenchmark()['sum']
    sys.exit(0)

  if sys.argv[1] == 'version':
    print version
    sys.exit(0)

  if sys.argv[1] == 'wholenode':
    result = wholenodeDiracBenchmark()
    print result['mean'],result['sum'],result['raw']
    sys.exit(0)

  if sys.argv[1] == 'jobslot':
    result = jobslotDiracBenchmark()
    print result['mean'],result['sum'],result['raw']
    sys.exit(0)

  try:
    instances = int( sys.argv[1] )
  except:
    sys.exit(1)
  else:
    result = multipleDiracBenchmark(instances = instances)
    print result['mean'],result['sum'],result['raw']
    sys.exit(0)