-rwxr-xr-x 3200 libmceliece-20240812/prioritize raw
#!/usr/bin/env python3
import os
import sys
tune = {
  'sort_int16':('1597',1),
  'sort_int32':('1597',1),
  'sort_int64':('1597',1),
  'xof_shake256':('8090',1),
  'xof_bitwrite16':('-8090',1),
  'kem_348864_dec':('32',1),
  'kem_348864f_dec':('32',1),
  'kem_348864pc_dec':('32',1),
  'kem_348864pcf_dec':('32',1),
  'kem_460896_dec':('32',1),
  'kem_460896f_dec':('32',1),
  'kem_460896pc_dec':('32',1),
  'kem_460896pcf_dec':('32',1),
  'kem_6688128_dec':('32',1),
  'kem_6688128f_dec':('32',1),
  'kem_6688128pc_dec':('32',1),
  'kem_6688128pcf_dec':('32',1),
  'kem_6960119_dec':('32',1),
  'kem_6960119f_dec':('32',1),
  'kem_6960119pc_dec':('32',1),
  'kem_6960119pcf_dec':('32',1),
  'kem_8192128_dec':('32',1),
  'kem_8192128f_dec':('32',1),
  'kem_8192128pc_dec':('32',1),
  'kem_8192128pcf_dec':('32',1),
}
impls = set()
data = {}
def handle(benchmark):
  with open('benchmarks/%s' % benchmark) as f:
    for line in f:
      line = line.split()
      if line[:2] == ['mceliece','version']:
        version = line[2]
        continue
      if line[:2] == ['mceliece','arch']:
        arch = line[2]
        continue
      if line[:1] == ['cpuid']:
        cpuid = ''.join(line[1:])
      if line[:3] == ['cpucycles','selected','0']:
        cpucyclesoverhead = int(line[3])
      if line[:3] == ['randombytes','selected','26']:
        randombytesoverhead = int(line[3])
      if len(line) >= 5:
        shortfun = line[0]
        if line[1].isnumeric() and line[2] == 'implementation' and line[4] == 'compiler':
          implnum = line[1]
          implop = line[0]
          i = line[3]
          c = ' '.join(line[5:])
        if line[1].isnumeric() and shortfun in tune and line[2] == tune[shortfun][0] and line[3].isnumeric():
          o = shortfun.split('_')[0]
          p = shortfun.split('_')[1]
          assert implop == '%s_%s' % (o,p)
          assert implnum == line[1]
          cycles = int(line[3])
          cycles -= cpucyclesoverhead
          if shortfun.endswith('_keypair'): cycles -= randombytesoverhead
          if cycles < 1: cycles = 1
          key = benchmark,version,arch,cpuid,o,p,i,c
          if key not in data: data[key] = []
          data[key] += [(shortfun,cycles)]
for benchmark in sorted(os.listdir('benchmarks')):
  handle(benchmark)
impldata = {}
bestscore = {}
for key in sorted(data):
  benchmark,version,arch,cpuid,o,p,i,c = key
  assert sorted(shortfun for shortfun,cycles in data[key]) == sorted(shortfun for shortfun in tune if shortfun.split('_')[:2] == [o,p])
  score = sum(cycles*tune[shortfun][1] for shortfun,cycles in data[key])
  if (o,p,i) not in impldata: impldata[o,p,i] = []
  impldata[o,p,i] += [(benchmark,version,arch,cpuid,c,score)]
  if (benchmark,o,p) not in bestscore: bestscore[benchmark,o,p] = score
  bestscore[benchmark,o,p] = min(score,bestscore[benchmark,o,p])
os.makedirs('priority',exist_ok=True)
for o,p,i in impldata:
  with open('priority/%s-%s-%s' % (o,p,i),'w') as f:
    for benchmark,version,arch,cpuid,c,score in impldata[o,p,i]:
      if bestscore[benchmark,o,p] <= 0: continue
      f.write('%.6f %s %s %s %s %s %s\n' % (score/bestscore[benchmark,o,p],score,arch,cpuid,version,benchmark,c))