zamotivator

Untitled

May 8th, 2012
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.90 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. import os
  4. import sys
  5. import itertools
  6.  
  7. # routines
  8. PREFIX_NAME='tile_repart'
  9.  
  10. TEST= os.path.join('testcases', 't', 'checkin', 'other')
  11. RESULT= os.path.join('testcases', 'r', 'checkin', 'other')
  12. DATA= os.path.join('testcases', 'data')
  13. DATA_LOAD= os.path.join('..', 'tests', 'harness', DATA)
  14.  
  15. def getter(prefix, suffix):
  16. def result(*setting):
  17. return os.path.join(prefix, '%s_%s.%s' % (PREFIX_NAME, "_".join(setting), suffix))
  18. return result
  19. PATH_LIST= [ TEST, RESULT, DATA, DATA_LOAD ]
  20. SUFFIX_LIST= [ 'test', 'result', 'txt', 'txt' ]
  21. GET_PATH_LIST= [ getter(prefix, suffix) for (prefix, suffix) in zip(PATH_LIST, SUFFIX_LIST) ]
  22.  
  23. # constant
  24. SOURCE='source'
  25. RESULT='result'
  26.  
  27. NAME='name'
  28. SIZE='size'
  29. COUNT='count'
  30. TYPE='type'
  31. MULTIPLIER='multiplier'
  32.  
  33. # chunks sizes
  34. SAME='same'
  35. ALIGNED_INCREASE='aligned_increase'
  36. ALIGNED_DECREASE='aligned_decrease'
  37. NOT_ALIGNED='not_aligned'
  38. PARTIALLY_ALIGNED='partially_aligned'
  39. CHUNK_SIZES_LIST=[SAME, ALIGNED_INCREASE, ALIGNED_DECREASE, NOT_ALIGNED, PARTIALLY_ALIGNED]
  40.  
  41. # overlap
  42. OVERLAP_SAME='overlap_same'
  43. OVERLAP_NONE='overlap_none'
  44. OVERLAP_ADD='overlap_add'
  45. OVERLAP_REMOVE='overlap_remove'
  46. OVERLAP_INCREASE='overlap_increase'
  47. OVERLAP_DECREASE='overlap_decrease'
  48. OVERLAP_LIST=[OVERLAP_ADD, OVERLAP_REMOVE, OVERLAP_INCREASE, OVERLAP_DECREASE]
  49.  
  50. # algorithm
  51. DENSE='dense'
  52. SPARSE='sparse'
  53. ALGORITHM_LIST=[DENSE, SPARSE]
  54.  
  55. # all available settings
  56. SETTING_LIST= [CHUNK_SIZES_LIST, OVERLAP_LIST, ALGORITHM_LIST]
  57.  
  58. # combination which should included
  59. def include_setting(chunk_sizes, overlap, algorithm):
  60. return not ((chunk_sizes == SAME) and overlap in set([OVERLAP_SAME, OVERLAP_NONE]))
  61.  
  62. # generate permutations list
  63. PERMUTATIONS_LIST= itertools.ifilter(lambda setting: include_setting(*setting), itertools.product(*SETTING_LIST))
  64.  
  65. def iterate_permutations(function):
  66. def result():
  67. for setting in PERMUTATIONS_LIST:
  68. function(*setting)
  69. return result
  70.  
  71. # print single setting
  72. def print_setting(function):
  73. def result(*setting):
  74. print "TEST [Chunk sizes: '%s' Overlap: '%s' Algorithm: '%s']" % tuple(setting)
  75. function(*setting)
  76. return result
  77.  
  78. # setting
  79.  
  80. DIMENSION_LIST=[ {NAME: 'x', MULTIPLIER: 1} ] # {NAME: 'y', MULTIPLIER: 3}
  81.  
  82. ATTRIBUTE_LIST=[ {NAME: 'a', TYPE: 'int32'} ] # {NAME: 'b', TYPE: 'int64'}
  83.  
  84. O_ZERO= 0
  85. O_VALUE= 1
  86. O_LESSER= 2
  87. O_GREATER= 3
  88. OVERLAP_DICT= {
  89. OVERLAP_NONE: { SOURCE: O_ZERO, RESULT: O_ZERO },
  90. OVERLAP_SAME: { SOURCE: O_VALUE, RESULT: O_VALUE },
  91. OVERLAP_ADD: { SOURCE: O_ZERO, RESULT: O_VALUE },
  92. OVERLAP_REMOVE: { SOURCE: O_VALUE, RESULT: O_ZERO },
  93. OVERLAP_INCREASE: { SOURCE: O_LESSER, RESULT: O_GREATER },
  94. OVERLAP_DECREASE: { SOURCE: O_GREATER, RESULT: O_LESSER }
  95. }
  96.  
  97. CS_ZERO= 0
  98. CS_VALUE= 2
  99. CS_LESSER= 2
  100. CS_GREATER= 3
  101. CS_COPRIME_FIRST= 2
  102. CS_COPRIME_SECOND= 3
  103. CHUNK_SIZES_DICT= {
  104. SAME: { SOURCE: { SIZE: CS_VALUE, COUNT: CS_VALUE },
  105. RESULT: { SIZE: CS_VALUE, COUNT: CS_VALUE } },
  106. ALIGNED_INCREASE: { SOURCE: { SIZE: CS_LESSER, COUNT: CS_GREATER },
  107. RESULT: { SIZE: CS_GREATER, COUNT: CS_LESSER } },
  108. ALIGNED_DECREASE: { SOURCE: { SIZE: CS_GREATER, COUNT: CS_LESSER },
  109. RESULT: { SIZE: CS_LESSER, COUNT: CS_GREATER } },
  110. NOT_ALIGNED: { SOURCE: { SIZE: CS_COPRIME_FIRST, COUNT: CS_COPRIME_SECOND },
  111. RESULT: { SIZE: CS_COPRIME_SECOND, COUNT: CS_COPRIME_FIRST } },
  112. PARTIALLY_ALIGNED: { SOURCE: { SIZE: CS_COPRIME_FIRST*2, COUNT: CS_COPRIME_SECOND*2 },
  113. RESULT: { SIZE: CS_COPRIME_SECOND*2, COUNT: CS_COPRIME_FIRST*2 } }
  114. }
  115.  
  116. # dimensions
  117. def dimension(chunk, overlap):
  118. def generate(setting):
  119. size= chunk[SIZE]
  120. maximum= chunk[SIZE] * chunk[COUNT]
  121. return '%s=0:%s,%s,%s' % tuple([ setting[NAME] ] + [ str(item * setting[MULTIPLIER]) for item in [maximum, size, overlap] ])
  122. return ','.join([ generate(setting) for setting in DIMENSION_LIST ])
  123.  
  124. # attributes
  125. def attribute():
  126. def generate(attribute):
  127. return '%s: %s' % (attribute[NAME], attribute[TYPE])
  128. return ','.join(map(generate, ATTRIBUTE_LIST))
  129.  
  130. # array
  131. def create_array(name, chunk, overlap):
  132. return 'create array %s [%s] <%s>' % (name, attribute(), dimension(chunk, overlap))
  133.  
  134. def remove_array(name):
  135. return 'remove(%s)' % name
  136.  
  137. # generate tests, expected results, data for tests
  138. @iterate_permutations
  139. @print_setting
  140. def generate(*setting):
  141. (chunk_sizes, overlap, algorithm)= tuple(setting)
  142.  
  143. def data():
  144. return 'data'
  145.  
  146. def test():
  147. def setup():
  148. chunk_d= CHUNK_SIZES_DICT[chunk_sizes]
  149. overlap_d= OVERLAP_DICT[overlap]
  150. source= create_array(SOURCE, chunk_d[SOURCE], overlap_d[SOURCE])
  151. result= create_array(RESULT, chunk_d[RESULT], overlap_d[RESULT])
  152.  
  153. data_load_index= PATH_LIST.index(DATA_LOAD)
  154. get_data_load_path= GET_PATH_LIST[data_load_index]
  155. data_load_path= get_data_load_path(chunk_sizes, overlap, algorithm)
  156. data_load= "load(%s, '%s')" % (SOURCE, data_load_path)
  157. return [source, result, data_load]
  158. def test():
  159. return ['scan(source)', 'repart(source, result)', 'store(repart(source, result), result)', 'scan(result)']
  160. def cleanup():
  161. return [ remove_array(name) for name in [SOURCE, RESULT] ]
  162. header= [ ['--' + header] for header in ['setup', 'test', 'cleanup'] ]
  163. queries= [ [query + ';' for query in generator()] for generator in [setup, test, cleanup] ]
  164. lines= reduce(lambda result, line: result + line, [ header + queries for (header, queries) in zip(header, queries) ])
  165. return os.linesep.join(lines)
  166.  
  167. def result():
  168. return 'result'
  169.  
  170. def write_file(name, data):
  171. f= open(name, 'w')
  172. f.write(data)
  173. f.close()
  174. print "Wrote: '%s'" % name
  175.  
  176. ACTION_LIST= [test, result, data]
  177. [ write_file(get_path(*setting), action()) for (get_path, action) in zip(GET_PATH_LIST, ACTION_LIST) ]
  178.  
  179. @iterate_permutations
  180. @print_setting
  181. def clean(*setting):
  182. def remove_file(name):
  183. os.remove(name)
  184. print "Removed: '%s'" % name
  185. [ remove_file(get_path(*setting)) for get_path in GET_PATH_LIST[:-1] ]
  186.  
  187. def unknown():
  188. sys.stderr.write('Error: unknown acion "%s"%s' % (sys.argv[1], os.linesep))
  189. exit(-1)
  190.  
  191. if __name__ == "__main__":
  192. if len(sys.argv) != 2:
  193. sys.stderr.write('Error: specify action (generate or clean)%s' % os.linesep)
  194. exit(-1)
  195. { 'generate': generate, 'clean': clean }.get(sys.argv[1], unknown)()
Advertisement
Add Comment
Please, Sign In to add comment