bones-compiler 1.3.1 → 1.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (307) hide show
  1. data/CHANGELOG +62 -0
  2. data/README.rdoc +14 -3
  3. data/Rakefile +13 -12
  4. data/VERSION +1 -1
  5. data/examples/applications/ffos.c +24 -8
  6. data/examples/benchmarks/PolyBench/2mm.c +0 -0
  7. data/examples/benchmarks/PolyBench/3mm.c +0 -0
  8. data/examples/benchmarks/PolyBench/adi.c +0 -0
  9. data/examples/benchmarks/PolyBench/atax.c +0 -0
  10. data/examples/benchmarks/PolyBench/bicg.c +0 -0
  11. data/examples/benchmarks/PolyBench/cholesky.c +0 -0
  12. data/examples/benchmarks/PolyBench/common.h +0 -0
  13. data/examples/benchmarks/PolyBench/correlation.c +0 -0
  14. data/examples/benchmarks/PolyBench/covariance.c +0 -0
  15. data/examples/benchmarks/PolyBench/doitgen.c +0 -0
  16. data/examples/benchmarks/PolyBench/durbin.c +0 -0
  17. data/examples/benchmarks/PolyBench/dynprog.c +0 -0
  18. data/examples/benchmarks/PolyBench/fdtd-2d-apml.c +0 -0
  19. data/examples/benchmarks/PolyBench/fdtd-2d.c +0 -0
  20. data/examples/benchmarks/PolyBench/floyd-warshall.c +0 -0
  21. data/examples/benchmarks/PolyBench/gemm.c +0 -0
  22. data/examples/benchmarks/PolyBench/gemver.c +0 -0
  23. data/examples/benchmarks/PolyBench/gesummv.c +0 -0
  24. data/examples/benchmarks/PolyBench/gramschmidt.c +0 -0
  25. data/examples/benchmarks/PolyBench/jacobi-1d-imper.c +4 -2
  26. data/examples/benchmarks/PolyBench/jacobi-2d-imper.c +1 -1
  27. data/examples/benchmarks/PolyBench/lu.c +0 -0
  28. data/examples/benchmarks/PolyBench/ludcmp.c +0 -0
  29. data/examples/benchmarks/PolyBench/mvt.c +0 -0
  30. data/examples/benchmarks/PolyBench/reg_detect.c +0 -0
  31. data/examples/benchmarks/PolyBench/seidel-2d.c +0 -0
  32. data/examples/benchmarks/PolyBench/symm.c +0 -0
  33. data/examples/benchmarks/PolyBench/syr2k.c +0 -0
  34. data/examples/benchmarks/PolyBench/syrk.c +0 -0
  35. data/examples/benchmarks/PolyBench/trisolv.c +0 -0
  36. data/examples/benchmarks/PolyBench/trmm.c +0 -0
  37. data/examples/benchmarks/Rodinia/bfs.c +143 -0
  38. data/examples/benchmarks/Rodinia/common.h +78 -0
  39. data/examples/benchmarks/Rodinia/hotspot.c +106 -126
  40. data/examples/benchmarks/Rodinia/kmeans.c +157 -164
  41. data/examples/benchmarks/Rodinia/nw.c +151 -0
  42. data/examples/benchmarks/Rodinia/pathfinder.c +88 -0
  43. data/examples/benchmarks/Rodinia/srad.c +50 -59
  44. data/examples/benchmarks/other/common.h +0 -0
  45. data/examples/benchmarks/other/dct.c +0 -0
  46. data/examples/benchmarks/other/mm.c +0 -0
  47. data/examples/benchmarks/other/saxpy.c +0 -0
  48. data/examples/chunk/example01.c +6 -4
  49. data/examples/chunk/example02.c +6 -4
  50. data/examples/chunk/example03.c +6 -4
  51. data/examples/chunk/example04.c +8 -5
  52. data/examples/chunk/example05.c +6 -4
  53. data/examples/chunk/example06.c +3 -1
  54. data/examples/chunk/example07.c +5 -2
  55. data/examples/dependences/example01.c +3 -1
  56. data/examples/dependences/example02.c +3 -1
  57. data/examples/dependences/example03.c +3 -1
  58. data/examples/dependences/example04.c +3 -1
  59. data/examples/dependences/example05.c +3 -1
  60. data/examples/element/example01.c +6 -4
  61. data/examples/element/example02.c +6 -4
  62. data/examples/element/example03.c +10 -8
  63. data/examples/element/example04.c +6 -4
  64. data/examples/element/example05.c +8 -5
  65. data/examples/element/example06.c +6 -4
  66. data/examples/element/example07.c +6 -4
  67. data/examples/element/example08.c +6 -4
  68. data/examples/element/example09.c +6 -4
  69. data/examples/element/example10.c +4 -2
  70. data/examples/element/example11.c +4 -2
  71. data/examples/element/example12.c +4 -2
  72. data/examples/element/example13.c +3 -1
  73. data/examples/fusion/example01.c +3 -12
  74. data/examples/fusion/example02.c +3 -16
  75. data/examples/fusion/example03.c +3 -1
  76. data/examples/fusion/example04.c +5 -3
  77. data/examples/fusion/example05.c +3 -1
  78. data/examples/neighbourhood/example01.c +6 -4
  79. data/examples/neighbourhood/example02.c +6 -4
  80. data/examples/neighbourhood/example03.c +6 -4
  81. data/examples/neighbourhood/example04.c +5 -3
  82. data/examples/neighbourhood/example05.c +3 -1
  83. data/examples/shared/example01.c +6 -4
  84. data/examples/shared/example02.c +6 -4
  85. data/examples/shared/example03.c +6 -4
  86. data/examples/shared/example04.c +6 -4
  87. data/examples/shared/example05.c +6 -4
  88. data/lib/adarwin/engine.rb +16 -5
  89. data/lib/adarwin/memorycopies.rb +21 -9
  90. data/lib/adarwin/nest.rb +18 -1
  91. data/lib/adarwin/preprocessor.rb +5 -2
  92. data/lib/adarwin/reference.rb +71 -6
  93. data/lib/bones/algorithm.rb +20 -5
  94. data/lib/bones/copy.rb +3 -2
  95. data/lib/bones/engine.rb +12 -9
  96. data/lib/bones/preprocessor.rb +170 -120
  97. data/lib/bones/variablelist.rb +1 -1
  98. data/lib/cast.rb +11 -0
  99. data/lib/castaddon.rb +23 -6
  100. data/lib/castaddon/node_adarwin.rb +17 -0
  101. data/lib/castaddon/node_common.rb +6 -0
  102. data/lib/castaddon/transformations.rb +13 -9
  103. data/skeletons/CPU-C/common/epilogue.c +0 -0
  104. data/skeletons/CPU-C/common/globals.c +0 -0
  105. data/skeletons/CPU-C/common/globals_kernel.c +0 -0
  106. data/skeletons/CPU-C/common/header.c +0 -0
  107. data/skeletons/CPU-C/common/mem_copy_D2H.c +0 -0
  108. data/skeletons/CPU-C/common/mem_copy_H2D.c +0 -0
  109. data/skeletons/CPU-C/common/mem_epilogue.c +0 -0
  110. data/skeletons/CPU-C/common/mem_global.c +0 -0
  111. data/skeletons/CPU-C/common/mem_prologue.c +0 -0
  112. data/skeletons/CPU-C/common/prologue.c +0 -0
  113. data/skeletons/CPU-C/common/timer_1_start.c +0 -0
  114. data/skeletons/CPU-C/common/timer_1_stop.c +0 -0
  115. data/skeletons/CPU-C/common/timer_2_start.c +0 -0
  116. data/skeletons/CPU-C/common/timer_2_stop.c +0 -0
  117. data/skeletons/CPU-C/common/timer_globals.c +5 -0
  118. data/skeletons/CPU-C/kernel/default.host.c +0 -0
  119. data/skeletons/CPU-C/kernel/default.kernel.c +0 -0
  120. data/skeletons/CPU-C/skeletons.txt +0 -0
  121. data/skeletons/CPU-OPENCL-AMD/common/epilogue.c +0 -0
  122. data/skeletons/CPU-OPENCL-AMD/common/globals.c +0 -0
  123. data/skeletons/CPU-OPENCL-AMD/common/globals_kernel.c +0 -0
  124. data/skeletons/CPU-OPENCL-AMD/common/header.c +0 -0
  125. data/skeletons/CPU-OPENCL-AMD/common/mem_copy_D2H.c +0 -0
  126. data/skeletons/CPU-OPENCL-AMD/common/mem_copy_H2D.c +0 -0
  127. data/skeletons/CPU-OPENCL-AMD/common/mem_epilogue.c +0 -0
  128. data/skeletons/CPU-OPENCL-AMD/common/mem_prologue.c +0 -0
  129. data/skeletons/CPU-OPENCL-AMD/common/prologue.c +0 -0
  130. data/skeletons/CPU-OPENCL-AMD/common/timer_1_start.c +0 -0
  131. data/skeletons/CPU-OPENCL-AMD/common/timer_1_stop.c +0 -0
  132. data/skeletons/CPU-OPENCL-AMD/common/timer_2_start.c +0 -0
  133. data/skeletons/CPU-OPENCL-AMD/common/timer_2_stop.c +0 -0
  134. data/skeletons/CPU-OPENCL-AMD/kernel/D-element-to-1-shared.host.c +0 -0
  135. data/skeletons/CPU-OPENCL-AMD/kernel/D-element-to-1-shared.kernel.cl +0 -0
  136. data/skeletons/CPU-OPENCL-AMD/kernel/default.host.c +0 -0
  137. data/skeletons/CPU-OPENCL-AMD/kernel/default.kernel.cl +0 -0
  138. data/skeletons/CPU-OPENCL-AMD/skeletons.txt +0 -0
  139. data/skeletons/CPU-OPENCL-INTEL/common/epilogue.c +0 -0
  140. data/skeletons/CPU-OPENCL-INTEL/common/globals.c +0 -0
  141. data/skeletons/CPU-OPENCL-INTEL/common/globals_kernel.c +0 -0
  142. data/skeletons/CPU-OPENCL-INTEL/common/header.c +0 -0
  143. data/skeletons/CPU-OPENCL-INTEL/common/mem_copy_D2H.c +0 -0
  144. data/skeletons/CPU-OPENCL-INTEL/common/mem_copy_H2D.c +0 -0
  145. data/skeletons/CPU-OPENCL-INTEL/common/mem_epilogue.c +0 -0
  146. data/skeletons/CPU-OPENCL-INTEL/common/mem_global.c +0 -0
  147. data/skeletons/CPU-OPENCL-INTEL/common/mem_prologue.c +0 -0
  148. data/skeletons/CPU-OPENCL-INTEL/common/prologue.c +0 -0
  149. data/skeletons/CPU-OPENCL-INTEL/common/timer_1_start.c +0 -0
  150. data/skeletons/CPU-OPENCL-INTEL/common/timer_1_stop.c +0 -0
  151. data/skeletons/CPU-OPENCL-INTEL/common/timer_2_start.c +0 -0
  152. data/skeletons/CPU-OPENCL-INTEL/common/timer_2_stop.c +0 -0
  153. data/skeletons/CPU-OPENCL-INTEL/common/timer_globals.c +5 -0
  154. data/skeletons/CPU-OPENCL-INTEL/kernel/D-element-to-1-shared.host.c +0 -0
  155. data/skeletons/CPU-OPENCL-INTEL/kernel/D-element-to-1-shared.kernel.cl +0 -0
  156. data/skeletons/CPU-OPENCL-INTEL/kernel/default.host.c +0 -0
  157. data/skeletons/CPU-OPENCL-INTEL/kernel/default.kernel.cl +0 -0
  158. data/skeletons/CPU-OPENCL-INTEL/skeletons.txt +0 -0
  159. data/skeletons/CPU-OPENMP/common/epilogue.c +0 -0
  160. data/skeletons/CPU-OPENMP/common/globals.c +0 -0
  161. data/skeletons/CPU-OPENMP/common/globals_kernel.c +0 -0
  162. data/skeletons/CPU-OPENMP/common/header.c +0 -0
  163. data/skeletons/CPU-OPENMP/common/mem_copy_D2H.c +0 -0
  164. data/skeletons/CPU-OPENMP/common/mem_copy_H2D.c +0 -0
  165. data/skeletons/CPU-OPENMP/common/mem_epilogue.c +0 -0
  166. data/skeletons/CPU-OPENMP/common/mem_global.c +0 -0
  167. data/skeletons/CPU-OPENMP/common/mem_prologue.c +0 -0
  168. data/skeletons/CPU-OPENMP/common/prologue.c +0 -0
  169. data/skeletons/CPU-OPENMP/common/timer_1_start.c +0 -0
  170. data/skeletons/CPU-OPENMP/common/timer_1_stop.c +0 -0
  171. data/skeletons/CPU-OPENMP/common/timer_2_start.c +0 -0
  172. data/skeletons/CPU-OPENMP/common/timer_2_stop.c +0 -0
  173. data/skeletons/CPU-OPENMP/common/timer_globals.c +2 -0
  174. data/skeletons/CPU-OPENMP/kernel/D-element-to-1-shared.host.c +0 -0
  175. data/skeletons/CPU-OPENMP/kernel/D-element-to-1-shared.kernel.c +3 -3
  176. data/skeletons/CPU-OPENMP/kernel/default.host.c +0 -0
  177. data/skeletons/CPU-OPENMP/kernel/default.kernel.c +0 -0
  178. data/skeletons/CPU-OPENMP/skeletons.txt +0 -0
  179. data/skeletons/GPU-CUDA/common/epilogue.c +0 -0
  180. data/skeletons/GPU-CUDA/common/globals.c +0 -0
  181. data/skeletons/GPU-CUDA/common/globals_kernel.c +0 -0
  182. data/skeletons/GPU-CUDA/common/header.c +0 -0
  183. data/skeletons/GPU-CUDA/common/mem_async_alloc.c +0 -0
  184. data/skeletons/GPU-CUDA/common/mem_async_copyin.c +0 -0
  185. data/skeletons/GPU-CUDA/common/mem_async_copyout.c +0 -0
  186. data/skeletons/GPU-CUDA/common/mem_async_free.c +0 -0
  187. data/skeletons/GPU-CUDA/common/mem_copy_D2H.c +0 -0
  188. data/skeletons/GPU-CUDA/common/mem_copy_H2D.c +0 -0
  189. data/skeletons/GPU-CUDA/common/mem_epilogue.c +0 -0
  190. data/skeletons/GPU-CUDA/common/mem_global.c +0 -0
  191. data/skeletons/GPU-CUDA/common/mem_prologue.c +0 -0
  192. data/skeletons/GPU-CUDA/common/prologue.c +0 -0
  193. data/skeletons/GPU-CUDA/common/scheduler.c +2 -2
  194. data/skeletons/GPU-CUDA/common/timer_1_start.c +0 -0
  195. data/skeletons/GPU-CUDA/common/timer_1_stop.c +0 -0
  196. data/skeletons/GPU-CUDA/common/timer_2_start.c +0 -0
  197. data/skeletons/GPU-CUDA/common/timer_2_stop.c +0 -0
  198. data/skeletons/GPU-CUDA/common/timer_globals.c +0 -0
  199. data/skeletons/GPU-CUDA/kernel/2xN-N-chunk-1-N-to-D-element.host.c +0 -0
  200. data/skeletons/GPU-CUDA/kernel/2xN-N-chunk-1-N-to-D-element.kernel.cu +0 -0
  201. data/skeletons/GPU-CUDA/kernel/D-element-to-1-shared.host.c +0 -0
  202. data/skeletons/GPU-CUDA/kernel/D-element-to-1-shared.kernel.cu +0 -0
  203. data/skeletons/GPU-CUDA/kernel/D-element-to-N-shared.host.c +0 -0
  204. data/skeletons/GPU-CUDA/kernel/D-element-to-N-shared.kernel.cu +0 -0
  205. data/skeletons/GPU-CUDA/kernel/N-N-chunk-1-N-to-D-element.host.c +0 -0
  206. data/skeletons/GPU-CUDA/kernel/N-N-chunk-1-N-to-D-element.kernel.cu +0 -0
  207. data/skeletons/GPU-CUDA/kernel/N-neighbourhood-N-to-N-element.host.c +0 -0
  208. data/skeletons/GPU-CUDA/kernel/N-neighbourhood-N-to-N-element.kernel.cu +0 -0
  209. data/skeletons/GPU-CUDA/kernel/default.host.c +0 -0
  210. data/skeletons/GPU-CUDA/kernel/default.kernel.cu +0 -0
  211. data/skeletons/GPU-CUDA/skeletons.txt +4 -2
  212. data/skeletons/GPU-OPENCL-AMD/common/epilogue.c +0 -0
  213. data/skeletons/GPU-OPENCL-AMD/common/globals.c +0 -0
  214. data/skeletons/GPU-OPENCL-AMD/common/globals_kernel.c +0 -0
  215. data/skeletons/GPU-OPENCL-AMD/common/header.c +0 -0
  216. data/skeletons/GPU-OPENCL-AMD/common/mem_copy_D2H.c +0 -0
  217. data/skeletons/GPU-OPENCL-AMD/common/mem_copy_H2D.c +0 -0
  218. data/skeletons/GPU-OPENCL-AMD/common/mem_epilogue.c +0 -0
  219. data/skeletons/GPU-OPENCL-AMD/common/mem_prologue.c +0 -0
  220. data/skeletons/GPU-OPENCL-AMD/common/prologue.c +0 -0
  221. data/skeletons/GPU-OPENCL-AMD/common/timer_1_start.c +0 -0
  222. data/skeletons/GPU-OPENCL-AMD/common/timer_1_stop.c +0 -0
  223. data/skeletons/GPU-OPENCL-AMD/common/timer_2_start.c +0 -0
  224. data/skeletons/GPU-OPENCL-AMD/common/timer_2_stop.c +0 -0
  225. data/skeletons/GPU-OPENCL-AMD/kernel/D-element-to-1-shared.host.c +0 -0
  226. data/skeletons/GPU-OPENCL-AMD/kernel/D-element-to-1-shared.kernel.cl +0 -0
  227. data/skeletons/GPU-OPENCL-AMD/kernel/default.host.c +0 -0
  228. data/skeletons/GPU-OPENCL-AMD/kernel/default.kernel.cl +0 -0
  229. data/skeletons/GPU-OPENCL-AMD/skeletons.txt +0 -0
  230. data/skeletons/verification/header.c +0 -0
  231. data/skeletons/verification/timer_start.c +0 -0
  232. data/skeletons/verification/timer_stop.c +0 -0
  233. data/skeletons/verification/verify_results.c +0 -0
  234. data/test/bones/test_algorithm.rb +0 -0
  235. data/test/bones/test_common.rb +0 -0
  236. data/test/bones/test_preprocessor.rb +0 -0
  237. data/test/bones/test_species.rb +0 -0
  238. data/test/bones/test_variable.rb +0 -0
  239. data/test/examples/benchmarks/PolyBench/2mm_species.c +1 -1
  240. data/test/examples/benchmarks/PolyBench/3mm_species.c +0 -0
  241. data/test/examples/benchmarks/PolyBench/adi_species.c +0 -0
  242. data/test/examples/benchmarks/PolyBench/atax_species.c +0 -0
  243. data/test/examples/benchmarks/PolyBench/bicg_species.c +0 -0
  244. data/test/examples/benchmarks/PolyBench/cholesky_species.c +0 -0
  245. data/test/examples/benchmarks/PolyBench/correlation_species.c +0 -0
  246. data/test/examples/benchmarks/PolyBench/covariance_species.c +0 -0
  247. data/test/examples/benchmarks/PolyBench/doitgen_species.c +0 -0
  248. data/test/examples/benchmarks/PolyBench/durbin_species.c +0 -0
  249. data/test/examples/benchmarks/PolyBench/dynprog_species.c +0 -0
  250. data/test/examples/benchmarks/PolyBench/fdtd-2d-apml_species.c +0 -0
  251. data/test/examples/benchmarks/PolyBench/fdtd-2d_species.c +0 -0
  252. data/test/examples/benchmarks/PolyBench/floyd-warshall_species.c +0 -0
  253. data/test/examples/benchmarks/PolyBench/gemm_species.c +0 -0
  254. data/test/examples/benchmarks/PolyBench/gemver_species.c +0 -0
  255. data/test/examples/benchmarks/PolyBench/gesummv_species.c +0 -0
  256. data/test/examples/benchmarks/PolyBench/gramschmidt_species.c +0 -0
  257. data/test/examples/benchmarks/PolyBench/jacobi-1d-imper_species.c +0 -0
  258. data/test/examples/benchmarks/PolyBench/jacobi-2d-imper_species.c +0 -0
  259. data/test/examples/benchmarks/PolyBench/lu_species.c +0 -0
  260. data/test/examples/benchmarks/PolyBench/ludcmp_species.c +0 -0
  261. data/test/examples/benchmarks/PolyBench/mvt_species.c +0 -0
  262. data/test/examples/benchmarks/PolyBench/reg_detect_species.c +0 -0
  263. data/test/examples/benchmarks/PolyBench/seidel-2d_species.c +0 -0
  264. data/test/examples/benchmarks/PolyBench/symm_species.c +0 -0
  265. data/test/examples/benchmarks/PolyBench/syr2k_species.c +0 -0
  266. data/test/examples/benchmarks/PolyBench/syrk_species.c +0 -0
  267. data/test/examples/benchmarks/PolyBench/trisolv_species.c +0 -0
  268. data/test/examples/benchmarks/PolyBench/trmm_species.c +0 -0
  269. data/test/examples/chunk/example01_species.c +3 -3
  270. data/test/examples/chunk/example02_species.c +3 -3
  271. data/test/examples/chunk/example03_species.c +3 -3
  272. data/test/examples/chunk/example04_species.c +3 -3
  273. data/test/examples/chunk/example05_species.c +3 -3
  274. data/test/examples/chunk/example06_species.c +1 -1
  275. data/test/examples/chunk/example07_species.c +3 -2
  276. data/test/examples/dependences/example01_species.c +1 -1
  277. data/test/examples/dependences/example02_species.c +1 -1
  278. data/test/examples/dependences/example03_species.c +1 -1
  279. data/test/examples/dependences/example04_species.c +1 -1
  280. data/test/examples/dependences/example05_species.c +1 -1
  281. data/test/examples/element/example01_species.c +3 -3
  282. data/test/examples/element/example02_species.c +3 -3
  283. data/test/examples/element/example03_species.c +7 -7
  284. data/test/examples/element/example04_species.c +3 -3
  285. data/test/examples/element/example05_species.c +3 -3
  286. data/test/examples/element/example06_species.c +3 -3
  287. data/test/examples/element/example07_species.c +3 -3
  288. data/test/examples/element/example08_species.c +3 -3
  289. data/test/examples/element/example09_species.c +3 -3
  290. data/test/examples/element/example10_species.c +1 -1
  291. data/test/examples/element/example11_species.c +1 -1
  292. data/test/examples/element/example12_species.c +1 -1
  293. data/test/examples/element/example13_species.c +1 -1
  294. data/test/examples/neighbourhood/example01_species.c +3 -3
  295. data/test/examples/neighbourhood/example02_species.c +3 -3
  296. data/test/examples/neighbourhood/example03_species.c +3 -3
  297. data/test/examples/neighbourhood/example04_species.c +3 -3
  298. data/test/examples/neighbourhood/example05_species.c +1 -1
  299. data/test/examples/shared/example01_species.c +3 -3
  300. data/test/examples/shared/example02_species.c +3 -3
  301. data/test/examples/shared/example03_species.c +3 -3
  302. data/test/examples/shared/example04_species.c +3 -3
  303. data/test/examples/shared/example05_species.c +3 -3
  304. data/test/test_helper.rb +2 -2
  305. metadata +266 -252
  306. checksums.yaml +0 -15
  307. data/examples/benchmarks/Rodinia/cfd.c +0 -180
@@ -0,0 +1,78 @@
1
+
2
+ //########################################################################
3
+ //### Includes
4
+ //########################################################################
5
+
6
+ #include <stdio.h>
7
+ #include <stdlib.h>
8
+ #include <math.h>
9
+ #include <float.h>
10
+
11
+ //########################################################################
12
+ //### Hotspot
13
+ //########################################################################
14
+
15
+ // Parameters
16
+ #define GRID_ROWS 512 // Number of rows in the grid (positive integer)
17
+ #define GRID_COLS 512 // Number of columns in the grid (positive integer)
18
+ #define SIM_TIME 10 // Number of iterations
19
+ #define TEMPERATURE_FILE "data/hotspot_temperature_512.txt" // Name of the file containing the initial temperature values of each cell
20
+ #define POWER_FILE "data/hotspot_power_512.txt" // Name of the file containing the dissipated power values of each cell
21
+
22
+ // Defines
23
+ #define STRING_SIZE 256 // Length of the strings in the temperature and power files
24
+ #define MAX_PD (3.0e6) // Maximum power density possible (say 300W for a 10mm x 10mm chip)
25
+ #define PRECISION 0.001 // Required precision in degrees
26
+ #define SPEC_HEAT_SI 1.75e6 //
27
+ #define K_SI 100 //
28
+ #define FACTOR_CHIP 0.5 // Capacitance fitting factor
29
+ #define T_CHIP 0.0005 // Chip temperature
30
+ #define CHIP_HEIGHT 0.016 // Chip height
31
+ #define CHIP_WIDTH 0.016 // Chip width
32
+ #define AMB_TEMP 80.0 // Ambient temperature, assuming no package at all
33
+
34
+ //########################################################################
35
+ //### Srad
36
+ //########################################################################
37
+
38
+ // Defines
39
+ #define ROWS 256 // Number of ROWS in the domain
40
+ #define COLS 256 // Number of COLS in the domain
41
+ #define R1 0 // y1 position of the speckle
42
+ #define R2 31 // y2 position of the speckle
43
+ #define C1 0 // x1 position of the speckle
44
+ #define C2 31 // x2 position of the speckle
45
+ #define LAMBDA 0.5 // Lambda value
46
+ #define NITER 4 // Number of iterations
47
+
48
+ //########################################################################
49
+ //### Pathfinder
50
+ //########################################################################
51
+
52
+ // Configuration
53
+ #define PATHCOLS 100000
54
+ #define PATHROWS 100
55
+
56
+ // Seed
57
+ #define M_SEED 9
58
+
59
+ //########################################################################
60
+ //### Kmeans
61
+ //########################################################################
62
+
63
+ #define SIZE (494020)
64
+ #define NUM_CLUSTERS 20
65
+ #define DIMENSIONS 2
66
+ #define THRESHOLD 0.001
67
+
68
+ //########################################################################
69
+ //### BFS
70
+ //########################################################################
71
+
72
+ // Settings
73
+ #define MAX_NODES 1000000
74
+
75
+ // Config
76
+ #define FILENAME "/home/cnugteren/software/rodinia_2.4/data/bfs/graph1MW_6.txt"
77
+
78
+ //########################################################################
@@ -15,47 +15,12 @@
15
15
  // == File information
16
16
  // Filename...........applications/hotspot.c
17
17
  // Authors............Cedric Nugteren
18
- // Last modified on...10-Aug-2012
18
+ // Last modified on...06-Jun-2014
19
19
  //
20
-
21
- //########################################################################
22
- //### Includes
23
- //########################################################################
24
-
25
- #include <stdio.h>
26
- #include <stdlib.h>
27
- #include <math.h>
28
-
29
- //########################################################################
30
- //### Input parameters
31
- //########################################################################
32
-
33
- #define GRID_ROWS 64 // Number of rows in the grid (positive integer)
34
- #define GRID_COLS 64 // Number of columns in the grid (positive integer)
35
- #define SIM_TIME 2 // Number of iterations
36
- #define TEMPERATURE_FILE "data/hotspot_temperature_64.txt" // Name of the file containing the initial temperature values of each cell
37
- #define POWER_FILE "data/hotspot_power_64.txt" // Name of the file containing the dissipated power values of each cell
38
-
39
- //########################################################################
40
- //### Defines
41
20
  //########################################################################
42
21
 
43
- #define STRING_SIZE 256 // Length of the strings in the temperature and power files
44
- #define MAX_PD (3.0e6) // Maximum power density possible (say 300W for a 10mm x 10mm chip)
45
- #define PRECISION 0.001 // Required precision in degrees
46
- #define SPEC_HEAT_SI 1.75e6 //
47
- #define K_SI 100 //
48
- #define FACTOR_CHIP 0.5 // Capacitance fitting factor
49
- #define T_CHIP 0.0005 // Chip temperature
50
- #define CHIP_HEIGHT 0.016 // Chip height
51
- #define CHIP_WIDTH 0.016 // Chip width
52
- #define AMB_TEMP 80.0 // Ambient temperature, assuming no package at all
53
-
54
- //########################################################################
55
- //### Forward declarations
56
- //########################################################################
57
-
58
- void read_input(double* array, const char* filename);
22
+ // Includes
23
+ #include "common.h"
59
24
 
60
25
  //########################################################################
61
26
  //### Start of the main function
@@ -64,15 +29,13 @@ void read_input(double* array, const char* filename);
64
29
  int main(void) {
65
30
 
66
31
  // Declare the loop iterators
67
- int r,c,iter;
32
+ int r,c;
33
+ int iter = 0;
68
34
 
69
35
  // Declare other/helper variables
70
- int index;
71
36
  double delta;
72
- int row = GRID_ROWS;
73
- int col = GRID_COLS;
74
- double grid_height = CHIP_HEIGHT/row;
75
- double grid_width = CHIP_WIDTH/col;
37
+ double grid_height = CHIP_HEIGHT/GRID_ROWS;
38
+ double grid_width = CHIP_WIDTH/GRID_COLS;
76
39
 
77
40
  // Set domain variables
78
41
  double cap = FACTOR_CHIP*SPEC_HEAT_SI*T_CHIP*grid_width*grid_height;
@@ -84,14 +47,38 @@ int main(void) {
84
47
 
85
48
  // Initialising memory
86
49
  printf("\n[hotspot] Initialising memory"); fflush(stdout);
87
- double* temperature = (double*) calloc(row*col, sizeof(double));
88
- double* power = (double*) calloc(row*col, sizeof(double));
89
- double* result = (double*) calloc(row*col, sizeof(double));
50
+ double temperature[GRID_ROWS][GRID_COLS];
51
+ double power[GRID_ROWS][GRID_COLS];
52
+ double result[GRID_ROWS][GRID_COLS];
90
53
 
91
- // Read initial temperature and power arrays
54
+ // Read initial temperature array
92
55
  printf("\n[hotspot] Populating memory"); fflush(stdout);
93
- read_input(temperature, TEMPERATURE_FILE);
94
- read_input(power, POWER_FILE);
56
+ char string[STRING_SIZE];
57
+ double value;
58
+ FILE* file_pointer1 = fopen(TEMPERATURE_FILE, "r");
59
+ if (!file_pointer1) { printf("\n[hotspot] Error: file '%s' could not be opened for reading\n\n", TEMPERATURE_FILE); fflush(stdout); exit(1); }
60
+ for (r=0; r<GRID_ROWS; r++) {
61
+ for (c=0; c<GRID_COLS; c++) {
62
+ fgets(string, STRING_SIZE, file_pointer1);
63
+ if (feof(file_pointer1)) { printf("\n[hotspot] Error: not enough lines in file '%s'\n\n", TEMPERATURE_FILE); fflush(stdout); exit(1); }
64
+ if ((sscanf(string, "%lf", &value) != 1) ) { printf("\n[hotspot] Error: invalid file format for '%s'\n\n", TEMPERATURE_FILE); fflush(stdout); exit(1); }
65
+ temperature[r][c] = value;
66
+ }
67
+ }
68
+ fclose(file_pointer1);
69
+
70
+ // Read initial power array
71
+ FILE* file_pointer2 = fopen(POWER_FILE, "r");
72
+ if (!file_pointer2) { printf("\n[hotspot] Error: file '%s' could not be opened for reading\n\n", POWER_FILE); fflush(stdout); exit(1); }
73
+ for (r=0; r<GRID_ROWS; r++) {
74
+ for (c=0; c<GRID_COLS; c++) {
75
+ fgets(string, STRING_SIZE, file_pointer2);
76
+ if (feof(file_pointer2)) { printf("\n[hotspot] Error: not enough lines in file '%s'\n\n", POWER_FILE); fflush(stdout); exit(1); }
77
+ if ((sscanf(string, "%lf", &value) != 1) ) { printf("\n[hotspot] Error: invalid file format for '%s'\n\n", POWER_FILE); fflush(stdout); exit(1); }
78
+ power[r][c] = value;
79
+ }
80
+ }
81
+ fclose(file_pointer2);
95
82
 
96
83
  // Perform the computation a given number of times
97
84
  printf("\n[hotspot] Performing the computation %d times",SIM_TIME); fflush(stdout);
@@ -100,100 +87,112 @@ int main(void) {
100
87
 
101
88
  // Transient solver driver routine: convert the heat transfer differential equations to difference equations
102
89
  // and solve the difference equations by iterating
103
- for (r=0; r<row; r++) {
104
- for (c=0; c<col; c++) {
90
+ for (r=0; r<GRID_ROWS; r++) {
91
+ for (c=0; c<GRID_COLS; c++) {
92
+
93
+ // Load the temperatures from memory
94
+ double temp_r_c = temperature[r][c];
95
+ double temp_rp1_c = 0;
96
+ if (r < GRID_ROWS-1) { temp_rp1_c = temperature[r+1][c]; }
97
+ double temp_rm1_c = 0;
98
+ if (r > 0) { temp_rm1_c = temperature[r-1][c]; }
99
+ double temp_r_cp1 = 0;
100
+ if (c < GRID_COLS-1) { temp_r_cp1 = temperature[r][c+1]; }
101
+ double temp_r_cm1 = 0;
102
+ if (c > 0) { temp_r_cm1 = temperature[r][c-1]; }
103
+
104
+ // Load the power
105
+ double power_r_c = power[r][c];
105
106
 
106
107
  // Corner 1
107
108
  if ( (r == 0) && (c == 0) ) {
108
- delta = (step / cap) * (power[0] +
109
- (temperature[1] - temperature[0]) / Rx +
110
- (temperature[col] - temperature[0]) / Ry +
111
- (AMB_TEMP - temperature[0]) / Rz);
109
+ delta = (step / cap) * (power_r_c +
110
+ (temp_r_cp1 - temp_r_c) / Rx +
111
+ (temp_rp1_c - temp_r_c) / Ry +
112
+ (AMB_TEMP - temp_r_c) / Rz);
112
113
  }
113
114
  // Corner 2
114
- else if ((r == 0) && (c == col-1)) {
115
- delta = (step / cap) * (power[c] +
116
- (temperature[c-1] - temperature[c]) / Rx +
117
- (temperature[c+col] - temperature[c]) / Ry +
118
- (AMB_TEMP - temperature[c]) / Rz);
115
+ else if ((r == 0) && (c == GRID_COLS-1)) {
116
+ delta = (step / cap) * (power_r_c +
117
+ (temp_r_cm1 - temp_r_c) / Rx +
118
+ (temp_rp1_c - temp_r_c) / Ry +
119
+ (AMB_TEMP - temp_r_c) / Rz);
119
120
  }
120
121
  // Corner 3
121
- else if ((r == row-1) && (c == col-1)) {
122
- delta = (step / cap) * (power[r*col+c] +
123
- (temperature[r*col+c-1] - temperature[r*col+c]) / Rx +
124
- (temperature[(r-1)*col+c] - temperature[r*col+c]) / Ry +
125
- (AMB_TEMP - temperature[r*col+c]) / Rz);
122
+ else if ((r == GRID_ROWS-1) && (c == GRID_COLS-1)) {
123
+ delta = (step / cap) * (power_r_c +
124
+ (temp_r_cm1 - temp_r_c) / Rx +
125
+ (temp_rm1_c - temp_r_c) / Ry +
126
+ (AMB_TEMP - temp_r_c) / Rz);
126
127
  }
127
128
  // Corner 4
128
- else if ((r == row-1) && (c == 0)) {
129
- delta = (step / cap) * (power[r*col] +
130
- (temperature[r*col+1] - temperature[r*col]) / Rx +
131
- (temperature[(r-1)*col] - temperature[r*col]) / Ry +
132
- (AMB_TEMP - temperature[r*col]) / Rz);
129
+ else if ((r == GRID_ROWS-1) && (c == 0)) {
130
+ delta = (step / cap) * (power_r_c +
131
+ (temp_r_cp1 - temp_r_c) / Rx +
132
+ (temp_rm1_c - temp_r_c) / Ry +
133
+ (AMB_TEMP - temp_r_c) / Rz);
133
134
  }
134
135
  // Edge 1
135
136
  else if (r == 0) {
136
- delta = (step / cap) * (power[c] +
137
- (temperature[c+1] + temperature[c-1] - 2.0*temperature[c]) / Rx +
138
- (temperature[col+c] - temperature[c]) / Ry +
139
- (AMB_TEMP - temperature[c]) / Rz);
137
+ delta = (step / cap) * (power_r_c +
138
+ (temp_r_cp1 + temp_r_cm1 - 2.0*temp_r_c) / Rx +
139
+ (temp_rp1_c - temp_r_c) / Ry +
140
+ (AMB_TEMP - temp_r_c) / Rz);
140
141
  }
141
142
  // Edge 2
142
- else if (c == col-1) {
143
- delta = (step / cap) * (power[r*col+c] +
144
- (temperature[(r+1)*col+c] + temperature[(r-1)*col+c] - 2.0*temperature[r*col+c]) / Ry +
145
- (temperature[r*col+c-1] - temperature[r*col+c]) / Rx +
146
- (AMB_TEMP - temperature[r*col+c]) / Rz);
143
+ else if (c == GRID_COLS-1) {
144
+ delta = (step / cap) * (power_r_c +
145
+ (temp_rp1_c + temp_rm1_c - 2.0*temp_r_c) / Ry +
146
+ (temp_r_cm1 - temp_r_c) / Rx +
147
+ (AMB_TEMP - temp_r_c) / Rz);
147
148
  }
148
149
  // Edge 3
149
- else if (r == row-1) {
150
- delta = (step / cap) * (power[r*col+c] +
151
- (temperature[r*col+c+1] + temperature[r*col+c-1] - 2.0*temperature[r*col+c]) / Rx +
152
- (temperature[(r-1)*col+c] - temperature[r*col+c]) / Ry +
153
- (AMB_TEMP - temperature[r*col+c]) / Rz);
150
+ else if (r == GRID_ROWS-1) {
151
+ delta = (step / cap) * (power_r_c +
152
+ (temp_r_cp1 + temp_r_cm1 - 2.0*temp_r_c) / Rx +
153
+ (temp_rm1_c - temp_r_c) / Ry +
154
+ (AMB_TEMP - temp_r_c) / Rz);
154
155
  }
155
156
  // Edge 4
156
157
  else if (c == 0) {
157
- delta = (step / cap) * (power[r*col] +
158
- (temperature[(r+1)*col] + temperature[(r-1)*col] - 2.0*temperature[r*col]) / Ry +
159
- (temperature[r*col+1] - temperature[r*col]) / Rx +
160
- (AMB_TEMP - temperature[r*col]) / Rz);
158
+ delta = (step / cap) * (power_r_c +
159
+ (temp_rp1_c + temp_rm1_c - 2.0*temp_r_c) / Ry +
160
+ (temp_r_cp1 - temp_r_c) / Rx +
161
+ (AMB_TEMP - temp_r_c) / Rz);
161
162
  }
162
163
  // Inside the chip
163
164
  else {
164
- delta = (step / cap) * (power[r*col+c] +
165
- (temperature[(r+1)*col+c] + temperature[(r-1)*col+c] - 2.0*temperature[r*col+c]) / Ry +
166
- (temperature[r*col+c+1] + temperature[r*col+c-1] - 2.0*temperature[r*col+c]) / Rx +
167
- (AMB_TEMP - temperature[r*col+c]) / Rz);
165
+ delta = (step / cap) * (power_r_c +
166
+ (temp_rp1_c + temp_rm1_c - 2.0*temp_r_c) / Ry +
167
+ (temp_r_cp1 + temp_r_cm1 - 2.0*temp_r_c) / Rx +
168
+ (AMB_TEMP - temp_r_c) / Rz);
168
169
  }
169
170
 
170
171
  // Update the temperatures
171
- result[r*col+c] = temperature[r*col+c] + delta;
172
+ result[r][c] = temperature[r][c] + delta;
172
173
  }
173
174
  }
174
175
 
175
176
  // Copy the result as the new temperatures
176
- for (r=0; r<row; r++) {
177
- for (c=0; c<col; c++) {
178
- temperature[r*col+c] = result[r*col+c];
177
+ for (r=0; r<GRID_ROWS; r++) {
178
+ for (c=0; c<GRID_COLS; c++) {
179
+ temperature[r][c] = result[r][c];
179
180
  }
180
181
  }
181
182
  }
182
183
  #pragma endscop
183
184
 
184
185
  // Print the values matrix
185
- printf("\n[hotspot] Printing the final temperatures:\n\n"); fflush(stdout);
186
- for (r=0; r<row; r++) {
187
- for (c=0; c<col; c++) {
188
- index = r*col+c;
189
- printf("%6d: %.3lf ", index, temperature[index]);
190
- }
191
- printf("\n");
192
- }
186
+ //printf("\n[hotspot] Printing the final temperatures:\n\n"); fflush(stdout);
187
+ //for (r=0; r<GRID_ROWS; r++) {
188
+ // for (c=0; c<GRID_COLS; c++) {
189
+ // printf("%6d: %.3lf ", r*GRID_COLS+c, temperature[r][c]);
190
+ // }
191
+ // printf("\n");
192
+ //}
193
193
 
194
194
  // Clean-up and exit
195
195
  printf("\n[hotspot] Completed\n\n"); fflush(stdout);
196
- free(temperature); free(power); free(result);
197
196
  fflush(stdout);
198
197
  return 0;
199
198
  }
@@ -202,27 +201,8 @@ int main(void) {
202
201
  //### Function to read an input file (power or temperature values)
203
202
  //########################################################################
204
203
 
205
- void read_input(double* array, const char* filename) {
206
- int r, c;
207
- char string[STRING_SIZE];
208
- double value;
204
+ void read_input(double array[GRID_ROWS][GRID_COLS], const char* filename) {
209
205
 
210
- // Open the file
211
- FILE* file_pointer = fopen(filename, "r");
212
- if (!file_pointer) { printf("\n[hotspot] Error: file '%s' could not be opened for reading\n\n", filename); fflush(stdout); exit(1); }
213
-
214
- // Process the file
215
- for (r=0; r<GRID_ROWS; r++) {
216
- for (c=0; c<GRID_COLS; c++) {
217
- fgets(string, STRING_SIZE, file_pointer);
218
- if (feof(file_pointer)) { printf("\n[hotspot] Error: not enough lines in file '%s'\n\n", filename); fflush(stdout); exit(1); }
219
- if ((sscanf(string, "%lf", &value) != 1) ) { printf("\n[hotspot] Error: invalid file format for '%s'\n\n", filename); fflush(stdout); exit(1); }
220
- array[r*GRID_COLS+c] = value;
221
- }
222
- }
223
-
224
- // Clean-up and return
225
- fclose(file_pointer);
226
206
  }
227
207
 
228
208
  //########################################################################
@@ -1,164 +1,157 @@
1
- //
2
- // This file is part of the Bones source-to-source compiler examples. This C-code
3
- // demonstrates the use of Bones for an example application: 'K-means clustering',
4
- // as also available in the Rodinia benchmark suite. For more information on the
5
- // application or on Bones please use the contact information below.
6
- //
7
- // == This implementation of K-means clustering is inspired by:
8
- // Author.............Roger Zhang
9
- // Web address........http://cs.smu.ca/~r_zhang/code/kmeans.c
10
- //
11
- // == More information on Bones
12
- // Contact............Cedric Nugteren <c.nugteren@tue.nl>
13
- // Web address........http://parse.ele.tue.nl/bones/
14
- //
15
- // == File information
16
- // Filename...........applications/kmeans.c
17
- // Authors............Cedric Nugteren
18
- // Last modified on...10-Aug-2012
19
- //
20
-
21
- //########################################################################
22
- //### Includes
23
- //########################################################################
24
-
25
- #include <stdio.h>
26
- #include <math.h>
27
- #include <float.h>
28
-
29
- //########################################################################
30
- //### Defines
31
- //########################################################################
32
-
33
- #define SIZE 512
34
- #define NUM_CLUSTERS 20
35
- #define DIMENSIONS 2
36
- #define THRESHOLD 0.0001
37
-
38
- //########################################################################
39
- //### Start of the main function
40
- //########################################################################
41
- int main(void) {
42
-
43
- // Declare the loop iterators
44
- int i,j,k;
45
-
46
- // Declare the error variables
47
- double error = DBL_MAX;
48
- double old_error;
49
- int iterations = 0;
50
-
51
- // Declare the distance variables and arrays
52
- double distance[1];
53
- double min_distance[1];
54
- double distances[SIZE];
55
-
56
- // Initialising memory
57
- printf("\n[k-means] Initialising memory"); fflush(stdout);
58
- double input[SIZE][DIMENSIONS];
59
- double centroids[NUM_CLUSTERS][DIMENSIONS];
60
- double centroids_temp[NUM_CLUSTERS][DIMENSIONS];
61
- int output[SIZE];
62
- int counts[NUM_CLUSTERS];
63
-
64
- // Set the input data
65
- printf("\n[k-means] Populating memory"); fflush(stdout);
66
- for (i=0; i<SIZE; i++) {
67
- input[i][0] = (i/16);
68
- input[i][1] = i%4;
69
- }
70
-
71
- // Pick k initial centroids
72
- printf("\n[k-means] Setting 'k' initial centroids"); fflush(stdout);
73
- for (k=0; k<NUM_CLUSTERS; k++) {
74
- for (j=0; j<DIMENSIONS; j++) {
75
- centroids[k][j] = input[(SIZE/NUM_CLUSTERS)*k][j];
76
- }
77
- }
78
-
79
- // Perform the k-means clustering algorithm, end when the error is not becoming smaller
80
- printf("\n[k-means] Perform the clustering algorithm"); fflush(stdout);
81
- do {
82
- #pragma scop
83
-
84
- // Save the error from the last step
85
- old_error = error;
86
- error = 0;
87
-
88
- // Clear old counts and temporary centroids
89
- for (k=0; k<NUM_CLUSTERS; k++) {
90
- counts[k] = 0;
91
- for (j=0; j<DIMENSIONS; j++) {
92
- centroids_temp[k][j] = 0;
93
- }
94
- }
95
-
96
- // Iterate over all data points
97
- for (i=0; i<SIZE; i++) {
98
-
99
- // Find the closest cluster
100
- min_distance[0] = DBL_MAX;
101
- for (k=0; k<NUM_CLUSTERS; k++) {
102
- distance[0] = 0;
103
- for (j=0; j<DIMENSIONS; j++) {
104
- distance[0] += pow(input[i][j]-centroids[k][j],2);
105
- }
106
- if (distance[0] < min_distance[0]) {
107
- output[i] = k;
108
- min_distance[0] = distance[0];
109
- }
110
- }
111
-
112
- // Update the size and temporary centroid of the destination cluster
113
- for (j=0; j<DIMENSIONS; j++) {
114
- centroids_temp[output[i]][j] += input[i][j];
115
- }
116
- counts[output[i]] += 1;
117
-
118
- // Store the resulting distance
119
- distances[i] = min_distance[0];
120
- }
121
-
122
- // Update the standard error
123
- for (i=0; i<SIZE; i++) {
124
- error += distances[i];
125
- }
126
-
127
- // Update all centroids
128
- for (k=0; k<NUM_CLUSTERS; k++) {
129
- for (j=0; j<DIMENSIONS; j++) {
130
- if (counts[k]) {
131
- centroids[k][j] = centroids_temp[k][j] / counts[k];
132
- }
133
- else {
134
- centroids[k][j] = centroids_temp[k][j];
135
- }
136
- }
137
- }
138
-
139
- // Go to the next iteration
140
- iterations += 1;
141
-
142
- #pragma endscop
143
- } while (fabs(error-old_error) > THRESHOLD);
144
-
145
- // Print the results
146
- printf("\n[k-means] Algorithm finished in %d iterations with an error of %.3lf", iterations, error); fflush(stdout);
147
- printf("\n[k-means] Printing the results: \n\n"); fflush(stdout);
148
- for (k=0; k<NUM_CLUSTERS; k++) {
149
- printf("Cluster %2i: ", k);
150
- for (i=0; i<SIZE; i++) {
151
- if (output[i] == k) {
152
- printf("%3i ", i);
153
- }
154
- }
155
- printf("\n");
156
- }
157
-
158
- // Clean-up and exit the function
159
- printf("\n[k-means] Completed\n\n"); fflush(stdout);
160
- fflush(stdout);
161
- return 0;
162
- }
163
-
164
- //########################################################################
1
+ //
2
+ // This file is part of the Bones source-to-source compiler examples. This C-code
3
+ // demonstrates the use of Bones for an example application: 'K-means clustering',
4
+ // as also available in the Rodinia benchmark suite. For more information on the
5
+ // application or on Bones please use the contact information below.
6
+ //
7
+ // == This implementation of K-means clustering is inspired by:
8
+ // Author.............Roger Zhang
9
+ // Web address........http://cs.smu.ca/~r_zhang/code/kmeans.c
10
+ //
11
+ // == More information on Bones
12
+ // Contact............Cedric Nugteren <c.nugteren@tue.nl>
13
+ // Web address........http://parse.ele.tue.nl/bones/
14
+ //
15
+ // == File information
16
+ // Filename...........applications/kmeans.c
17
+ // Authors............Cedric Nugteren
18
+ // Last modified on...06-Jun-2014
19
+ //
20
+ //########################################################################
21
+
22
+ // Includes
23
+ #include "common.h"
24
+
25
+ //########################################################################
26
+ //### Start of the main function
27
+ //########################################################################
28
+ int main(void) {
29
+
30
+ // Declare the loop iterators
31
+ int i,j,k;
32
+
33
+ // Declare the error variables
34
+ float error[1] = { 100000.0 };
35
+ float old_error;
36
+ int iterations = 0;
37
+
38
+ // Declare the distance variables and arrays
39
+ float distances[SIZE];
40
+
41
+ // Initialising memory
42
+ printf("\n[k-means] Initialising memory"); fflush(stdout);
43
+ float input[SIZE][DIMENSIONS];
44
+ float centroids[NUM_CLUSTERS][DIMENSIONS];
45
+ float centroids_temp[NUM_CLUSTERS][DIMENSIONS];
46
+ int output[SIZE];
47
+ int counts[NUM_CLUSTERS];
48
+
49
+ // Set the input data
50
+ printf("\n[k-means] Populating memory"); fflush(stdout);
51
+ for (i=0; i<SIZE; i++) {
52
+ input[i][0] = (i/16);
53
+ input[i][1] = i%4;
54
+ }
55
+
56
+ // Pick k initial centroids
57
+ printf("\n[k-means] Setting 'k' initial centroids"); fflush(stdout);
58
+ for (k=0; k<NUM_CLUSTERS; k++) {
59
+ for (j=0; j<DIMENSIONS; j++) {
60
+ centroids[k][j] = input[(SIZE/NUM_CLUSTERS)*k][j];
61
+ }
62
+ }
63
+
64
+ // Perform the k-means clustering algorithm, end when the error is not becoming smaller
65
+ printf("\n[k-means] Perform the clustering algorithm"); fflush(stdout);
66
+ do {
67
+ //for (int iters=0; iters<10; iters++) {
68
+
69
+ // Start of the scop
70
+ #pragma scop
71
+
72
+ // Save the error from the last step
73
+ old_error = error[0];
74
+ error[0] = 0;
75
+
76
+ // Clear old counts and temporary centroids
77
+ for (k=0; k<NUM_CLUSTERS; k++) {
78
+ counts[k] = 0;
79
+ }
80
+ for (k=0; k<NUM_CLUSTERS; k++) {
81
+ for (j=0; j<DIMENSIONS; j++) {
82
+ centroids_temp[k][j] = 0;
83
+ }
84
+ }
85
+ // Iterate over all data points
86
+ for (i=0; i<SIZE; i++) {
87
+
88
+ // Find the closest cluster
89
+ float min_distance = 100000.0;
90
+ for (k=0; k<NUM_CLUSTERS; k++) {
91
+ float distance = 0;
92
+ for (j=0; j<DIMENSIONS; j++) {
93
+ float val = (input[i][j]-centroids[k][j]);
94
+ distance += val * val;
95
+ }
96
+ if (distance < min_distance) {
97
+ output[i] = k;
98
+ min_distance = distance;
99
+ }
100
+ }
101
+
102
+ // Update the size and temporary centroid of the destination cluster
103
+ int cluster_index = output[i];
104
+ for (j=0; j<DIMENSIONS; j++) {
105
+ centroids_temp[cluster_index][j] += input[i][j];
106
+ }
107
+ counts[cluster_index] += 1;
108
+
109
+ // Store the resulting distance
110
+ distances[i] = min_distance;
111
+ }
112
+
113
+ // Update the standard error
114
+ for (i=0; i<SIZE; i++) {
115
+ error[0] += distances[i];
116
+ }
117
+
118
+ // Update all centroids
119
+ for (k=0; k<NUM_CLUSTERS; k++) {
120
+ for (j=0; j<DIMENSIONS; j++) {
121
+ int count = counts[k];
122
+ float val;
123
+ if (count > 0) {
124
+ val = centroids_temp[k][j] / count;
125
+ }
126
+ else {
127
+ val = centroids_temp[k][j];
128
+ }
129
+ centroids[k][j] = val;
130
+ }
131
+ }
132
+
133
+ // Go to the next iteration
134
+ iterations += 1;
135
+ #pragma endscop
136
+ //}
137
+ } while (fabs(error[0]-old_error) > THRESHOLD);
138
+
139
+ // Print the results
140
+ printf("\n[k-means] Algorithm finished in %d iterations with an error of %.3lf", iterations, error[0]); fflush(stdout);
141
+ //printf("\n[k-means] Printing the results: \n\n"); fflush(stdout);
142
+ //for (k=0; k<NUM_CLUSTERS; k++) {
143
+ // printf("Cluster %2i: ", k);
144
+ // for (i=0; i<SIZE; i++) {
145
+ // if (output[i] == k) {
146
+ // printf("%3i ", i);
147
+ // }
148
+ // }
149
+ // printf("\n");
150
+ //}
151
+
152
+ // Clean-up and exit the function
153
+ printf("\n[k-means] Completed\n\n"); fflush(stdout);
154
+ fflush(stdout);
155
+ return 0;
156
+ }
157
+ //########################################################################