这是一个本人学习 csapp 的 learning 库

138 lines
4.8 KiB

  1. #!/usr//bin/python
  2. #
  3. # driver.py - The driver tests the correctness of the student's cache
  4. # simulator and the correctness and performance of their transpose
  5. # function. It uses ./test-csim to check the correctness of the
  6. # simulator and it runs ./test-trans on three different sized
  7. # matrices (32x32, 64x64, and 61x67) to test the correctness and
  8. # performance of the transpose function.
  9. #
  10. import subprocess;
  11. import re;
  12. import os;
  13. import sys;
  14. import optparse;
  15. #
  16. # computeMissScore - compute the score depending on the number of
  17. # cache misses
  18. #
  19. def computeMissScore(miss, lower, upper, full_score):
  20. if miss <= lower:
  21. return full_score
  22. if miss >= upper:
  23. return 0
  24. score = (miss - lower) * 1.0
  25. range = (upper- lower) * 1.0
  26. return round((1 - score / range) * full_score, 1)
  27. #
  28. # main - Main function
  29. #
  30. def main():
  31. # Configure maxscores here
  32. maxscore= {};
  33. maxscore['csim'] = 27
  34. maxscore['transc'] = 1
  35. maxscore['trans32'] = 8
  36. maxscore['trans64'] = 8
  37. maxscore['trans61'] = 10
  38. # Parse the command line arguments
  39. p = optparse.OptionParser()
  40. p.add_option("-A", action="store_true", dest="autograde",
  41. help="emit autoresult string for Autolab");
  42. opts, args = p.parse_args()
  43. autograde = opts.autograde
  44. # Check the correctness of the cache simulator
  45. print "Part A: Testing cache simulator"
  46. print "Running ./test-csim"
  47. p = subprocess.Popen("./test-csim",
  48. shell=True, stdout=subprocess.PIPE)
  49. stdout_data = p.communicate()[0]
  50. # Emit the output from test-csim
  51. stdout_data = re.split('\n', stdout_data)
  52. for line in stdout_data:
  53. if re.match("TEST_CSIM_RESULTS", line):
  54. resultsim = re.findall(r'(\d+)', line)
  55. else:
  56. print "%s" % (line)
  57. # Check the correctness and performance of the transpose function
  58. # 32x32 transpose
  59. print "Part B: Testing transpose function"
  60. print "Running ./test-trans -M 32 -N 32"
  61. p = subprocess.Popen("./test-trans -M 32 -N 32 | grep TEST_TRANS_RESULTS",
  62. shell=True, stdout=subprocess.PIPE)
  63. stdout_data = p.communicate()[0]
  64. result32 = re.findall(r'(\d+)', stdout_data)
  65. # 64x64 transpose
  66. print "Running ./test-trans -M 64 -N 64"
  67. p = subprocess.Popen("./test-trans -M 64 -N 64 | grep TEST_TRANS_RESULTS",
  68. shell=True, stdout=subprocess.PIPE)
  69. stdout_data = p.communicate()[0]
  70. result64 = re.findall(r'(\d+)', stdout_data)
  71. # 61x67 transpose
  72. print "Running ./test-trans -M 61 -N 67"
  73. p = subprocess.Popen("./test-trans -M 61 -N 67 | grep TEST_TRANS_RESULTS",
  74. shell=True, stdout=subprocess.PIPE)
  75. stdout_data = p.communicate()[0]
  76. result61 = re.findall(r'(\d+)', stdout_data)
  77. # Compute the scores for each step
  78. csim_cscore = map(int, resultsim[0:1])
  79. trans_cscore = int(result32[0]) * int(result64[0]) * int(result61[0]);
  80. miss32 = int(result32[1])
  81. miss64 = int(result64[1])
  82. miss61 = int(result61[1])
  83. trans32_score = computeMissScore(miss32, 300, 600, maxscore['trans32']) * int(result32[0])
  84. trans64_score = computeMissScore(miss64, 1300, 2000, maxscore['trans64']) * int(result64[0])
  85. trans61_score = computeMissScore(miss61, 2000, 3000, maxscore['trans61']) * int(result61[0])
  86. total_score = csim_cscore[0] + trans32_score + trans64_score + trans61_score
  87. # Summarize the results
  88. print "\nCache Lab summary:"
  89. print "%-22s%8s%10s%12s" % ("", "Points", "Max pts", "Misses")
  90. print "%-22s%8.1f%10d" % ("Csim correctness", csim_cscore[0],
  91. maxscore['csim'])
  92. misses = str(miss32)
  93. if miss32 == 2**31-1 :
  94. misses = "invalid"
  95. print "%-22s%8.1f%10d%12s" % ("Trans perf 32x32", trans32_score,
  96. maxscore['trans32'], misses)
  97. misses = str(miss64)
  98. if miss64 == 2**31-1 :
  99. misses = "invalid"
  100. print "%-22s%8.1f%10d%12s" % ("Trans perf 64x64", trans64_score,
  101. maxscore['trans64'], misses)
  102. misses = str(miss61)
  103. if miss61 == 2**31-1 :
  104. misses = "invalid"
  105. print "%-22s%8.1f%10d%12s" % ("Trans perf 61x67", trans61_score,
  106. maxscore['trans61'], misses)
  107. print "%22s%8.1f%10d" % ("Total points", total_score,
  108. maxscore['csim'] +
  109. maxscore['trans32'] +
  110. maxscore['trans64'] +
  111. maxscore['trans61'])
  112. # Emit autoresult string for Autolab if called with -A option
  113. if autograde:
  114. autoresult="%.1f:%d:%d:%d" % (total_score, miss32, miss64, miss61)
  115. print "\nAUTORESULT_STRING=%s" % autoresult
  116. # execute main only if called as a script
  117. if __name__ == "__main__":
  118. main()