core_main.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. /*
  2. Copyright 2018 Embedded Microprocessor Benchmark Consortium (EEMBC)
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. Original Author: Shay Gal-on
  13. */
  14. /* File: core_main.c
  15. This file contains the framework to acquire a block of memory, seed
  16. initial parameters, tun t he benchmark and report the results.
  17. */
  18. #include "coremark.h"
  19. /* Function: iterate
  20. Run the benchmark for a specified number of iterations.
  21. Operation:
  22. For each type of benchmarked algorithm:
  23. a - Initialize the data block for the algorithm.
  24. b - Execute the algorithm N times.
  25. Returns:
  26. NULL.
  27. */
  28. static ee_u16 list_known_crc[] = { (ee_u16)0xd4b0,
  29. (ee_u16)0x3340,
  30. (ee_u16)0x6a79,
  31. (ee_u16)0xe714,
  32. (ee_u16)0xe3c1 };
  33. static ee_u16 matrix_known_crc[] = { (ee_u16)0xbe52,
  34. (ee_u16)0x1199,
  35. (ee_u16)0x5608,
  36. (ee_u16)0x1fd7,
  37. (ee_u16)0x0747 };
  38. static ee_u16 state_known_crc[] = { (ee_u16)0x5e47,
  39. (ee_u16)0x39bf,
  40. (ee_u16)0xe5a4,
  41. (ee_u16)0x8e3a,
  42. (ee_u16)0x8d84 };
  43. void *
  44. iterate(void *pres)
  45. {
  46. ee_u32 i;
  47. ee_u16 crc;
  48. core_results *res = (core_results *)pres;
  49. ee_u32 iterations = res->iterations;
  50. res->crc = 0;
  51. res->crclist = 0;
  52. res->crcmatrix = 0;
  53. res->crcstate = 0;
  54. for (i = 0; i < iterations; i++)
  55. {
  56. crc = core_bench_list(res, 1);
  57. res->crc = crcu16(crc, res->crc);
  58. crc = core_bench_list(res, -1);
  59. res->crc = crcu16(crc, res->crc);
  60. if (i == 0)
  61. res->crclist = res->crc;
  62. }
  63. return NULL;
  64. }
  65. #if (SEED_METHOD == SEED_ARG)
  66. ee_s32 get_seed_args(int i, int argc, char *argv[]);
  67. #define get_seed(x) (ee_s16) get_seed_args(x, argc, argv)
  68. #define get_seed_32(x) get_seed_args(x, argc, argv)
  69. #else /* via function or volatile */
  70. ee_s32 get_seed_32(int i);
  71. #define get_seed(x) (ee_s16) get_seed_32(x)
  72. #endif
  73. #if (MEM_METHOD == MEM_STATIC)
  74. ee_u8 static_memblk[TOTAL_DATA_SIZE];
  75. #endif
  76. char *mem_name[3] = { "Static", "Heap", "Stack" };
  77. /* Function: main
  78. Main entry routine for the benchmark.
  79. This function is responsible for the following steps:
  80. 1 - Initialize input seeds from a source that cannot be determined at
  81. compile time. 2 - Initialize memory block for use. 3 - Run and time the
  82. benchmark. 4 - Report results, testing the validity of the output if the
  83. seeds are known.
  84. Arguments:
  85. 1 - first seed : Any value
  86. 2 - second seed : Must be identical to first for iterations to be
  87. identical 3 - third seed : Any value, should be at least an order of
  88. magnitude less then the input size, but bigger then 32. 4 - Iterations :
  89. Special, if set to 0, iterations will be automatically determined such that
  90. the benchmark will run between 10 to 100 secs
  91. */
  92. #if MAIN_HAS_NOARGC
  93. MAIN_RETURN_TYPE
  94. ee_main(void)
  95. {
  96. int argc = 0;
  97. char *argv[1];
  98. #else
  99. MAIN_RETURN_TYPE
  100. ee_main(int argc, char *argv[])
  101. {
  102. #endif
  103. ee_u16 i, j = 0, num_algorithms = 0;
  104. ee_s16 known_id = -1, total_errors = 0;
  105. ee_u16 seedcrc = 0;
  106. CORE_TICKS total_time;
  107. core_results results[MULTITHREAD];
  108. #if (MEM_METHOD == MEM_STACK)
  109. ee_u8 stack_memblock[TOTAL_DATA_SIZE * MULTITHREAD];
  110. #endif
  111. /* first call any initializations needed */
  112. portable_init(&(results[0].port), &argc, argv);
  113. /* First some checks to make sure benchmark will run ok */
  114. if (sizeof(struct list_head_s) > 128)
  115. {
  116. ee_printf("list_head structure too big for comparable data!");
  117. return MAIN_RETURN_VAL;
  118. }
  119. results[0].seed1 = get_seed(1);
  120. results[0].seed2 = get_seed(2);
  121. results[0].seed3 = get_seed(3);
  122. results[0].iterations = get_seed_32(4);
  123. #if CORE_DEBUG
  124. results[0].iterations = 1;
  125. #endif
  126. results[0].execs = get_seed_32(5);
  127. if (results[0].execs == 0)
  128. { /* if not supplied, execute all algorithms */
  129. results[0].execs = ALL_ALGORITHMS_MASK;
  130. }
  131. /* put in some default values based on one seed only for easy testing */
  132. if ((results[0].seed1 == 0) && (results[0].seed2 == 0)
  133. && (results[0].seed3 == 0))
  134. { /* performance run */
  135. results[0].seed1 = 0;
  136. results[0].seed2 = 0;
  137. results[0].seed3 = 0x66;
  138. }
  139. if ((results[0].seed1 == 1) && (results[0].seed2 == 0)
  140. && (results[0].seed3 == 0))
  141. { /* validation run */
  142. results[0].seed1 = 0x3415;
  143. results[0].seed2 = 0x3415;
  144. results[0].seed3 = 0x66;
  145. }
  146. #if (MEM_METHOD == MEM_STATIC)
  147. results[0].memblock[0] = (void *)static_memblk;
  148. results[0].size = TOTAL_DATA_SIZE;
  149. results[0].err = 0;
  150. #if (MULTITHREAD > 1)
  151. #error "Cannot use a static data area with multiple contexts!"
  152. #endif
  153. #elif (MEM_METHOD == MEM_MALLOC)
  154. for (i = 0; i < MULTITHREAD; i++)
  155. {
  156. ee_s32 malloc_override = get_seed(7);
  157. if (malloc_override != 0)
  158. results[i].size = malloc_override;
  159. else
  160. results[i].size = TOTAL_DATA_SIZE;
  161. results[i].memblock[0] = portable_malloc(results[i].size);
  162. results[i].seed1 = results[0].seed1;
  163. results[i].seed2 = results[0].seed2;
  164. results[i].seed3 = results[0].seed3;
  165. results[i].err = 0;
  166. results[i].execs = results[0].execs;
  167. }
  168. #elif (MEM_METHOD == MEM_STACK)
  169. for (i = 0; i < MULTITHREAD; i++)
  170. {
  171. results[i].memblock[0] = stack_memblock + i * TOTAL_DATA_SIZE;
  172. results[i].size = TOTAL_DATA_SIZE;
  173. results[i].seed1 = results[0].seed1;
  174. results[i].seed2 = results[0].seed2;
  175. results[i].seed3 = results[0].seed3;
  176. results[i].err = 0;
  177. results[i].execs = results[0].execs;
  178. }
  179. #else
  180. #error "Please define a way to initialize a memory block."
  181. #endif
  182. /* Data init */
  183. /* Find out how space much we have based on number of algorithms */
  184. for (i = 0; i < NUM_ALGORITHMS; i++)
  185. {
  186. if ((1 << (ee_u32)i) & results[0].execs)
  187. num_algorithms++;
  188. }
  189. for (i = 0; i < MULTITHREAD; i++)
  190. results[i].size = results[i].size / num_algorithms;
  191. /* Assign pointers */
  192. for (i = 0; i < NUM_ALGORITHMS; i++)
  193. {
  194. ee_u32 ctx;
  195. if ((1 << (ee_u32)i) & results[0].execs)
  196. {
  197. for (ctx = 0; ctx < MULTITHREAD; ctx++)
  198. results[ctx].memblock[i + 1]
  199. = (char *)(results[ctx].memblock[0]) + results[0].size * j;
  200. j++;
  201. }
  202. }
  203. /* call inits */
  204. for (i = 0; i < MULTITHREAD; i++)
  205. {
  206. if (results[i].execs & ID_LIST)
  207. {
  208. results[i].list = core_list_init(
  209. results[0].size, results[i].memblock[1], results[i].seed1);
  210. }
  211. if (results[i].execs & ID_MATRIX)
  212. {
  213. core_init_matrix(results[0].size,
  214. results[i].memblock[2],
  215. (ee_s32)results[i].seed1
  216. | (((ee_s32)results[i].seed2) << 16),
  217. &(results[i].mat));
  218. }
  219. if (results[i].execs & ID_STATE)
  220. {
  221. core_init_state(
  222. results[0].size, results[i].seed1, results[i].memblock[3]);
  223. }
  224. }
  225. /* automatically determine number of iterations if not set */
  226. if (results[0].iterations == 0)
  227. {
  228. secs_ret secs_passed = 0;
  229. ee_u32 divisor;
  230. results[0].iterations = 1;
  231. while (secs_passed < (secs_ret)1)
  232. {
  233. results[0].iterations *= 10;
  234. start_time();
  235. iterate(&results[0]);
  236. stop_time();
  237. secs_passed = time_in_secs(get_time());
  238. }
  239. /* now we know it executes for at least 1 sec, set actual run time at
  240. * about 10 secs */
  241. divisor = (ee_u32)secs_passed;
  242. if (divisor == 0) /* some machines cast float to int as 0 since this
  243. conversion is not defined by ANSI, but we know at
  244. least one second passed */
  245. divisor = 1;
  246. results[0].iterations *= 1 + 10 / divisor;
  247. }
  248. /* perform actual benchmark */
  249. start_time();
  250. #if (MULTITHREAD > 1)
  251. if (default_num_contexts > MULTITHREAD)
  252. {
  253. default_num_contexts = MULTITHREAD;
  254. }
  255. for (i = 0; i < default_num_contexts; i++)
  256. {
  257. results[i].iterations = results[0].iterations;
  258. results[i].execs = results[0].execs;
  259. core_start_parallel(&results[i]);
  260. }
  261. for (i = 0; i < default_num_contexts; i++)
  262. {
  263. core_stop_parallel(&results[i]);
  264. }
  265. #else
  266. iterate(&results[0]);
  267. #endif
  268. stop_time();
  269. total_time = get_time();
  270. /* get a function of the input to report */
  271. seedcrc = crc16(results[0].seed1, seedcrc);
  272. seedcrc = crc16(results[0].seed2, seedcrc);
  273. seedcrc = crc16(results[0].seed3, seedcrc);
  274. seedcrc = crc16(results[0].size, seedcrc);
  275. switch (seedcrc)
  276. { /* test known output for common seeds */
  277. case 0x8a02: /* seed1=0, seed2=0, seed3=0x66, size 2000 per algorithm */
  278. known_id = 0;
  279. ee_printf("6k performance run parameters for coremark.");
  280. break;
  281. case 0x7b05: /* seed1=0x3415, seed2=0x3415, seed3=0x66, size 2000 per
  282. algorithm */
  283. known_id = 1;
  284. ee_printf("6k validation run parameters for coremark.");
  285. break;
  286. case 0x4eaf: /* seed1=0x8, seed2=0x8, seed3=0x8, size 400 per algorithm
  287. */
  288. known_id = 2;
  289. ee_printf("Profile generation run parameters for coremark.");
  290. break;
  291. case 0xe9f5: /* seed1=0, seed2=0, seed3=0x66, size 666 per algorithm */
  292. known_id = 3;
  293. ee_printf("2K performance run parameters for coremark.");
  294. break;
  295. case 0x18f2: /* seed1=0x3415, seed2=0x3415, seed3=0x66, size 666 per
  296. algorithm */
  297. known_id = 4;
  298. ee_printf("2K validation run parameters for coremark.");
  299. break;
  300. default:
  301. total_errors = -1;
  302. break;
  303. }
  304. if (known_id >= 0)
  305. {
  306. for (i = 0; i < default_num_contexts; i++)
  307. {
  308. results[i].err = 0;
  309. if ((results[i].execs & ID_LIST)
  310. && (results[i].crclist != list_known_crc[known_id]))
  311. {
  312. ee_printf("[%u]ERROR! list crc 0x%04x - should be 0x%04x",
  313. i,
  314. results[i].crclist,
  315. list_known_crc[known_id]);
  316. results[i].err++;
  317. }
  318. if ((results[i].execs & ID_MATRIX)
  319. && (results[i].crcmatrix != matrix_known_crc[known_id]))
  320. {
  321. ee_printf("[%u]ERROR! matrix crc 0x%04x - should be 0x%04x",
  322. i,
  323. results[i].crcmatrix,
  324. matrix_known_crc[known_id]);
  325. results[i].err++;
  326. }
  327. if ((results[i].execs & ID_STATE)
  328. && (results[i].crcstate != state_known_crc[known_id]))
  329. {
  330. ee_printf("[%u]ERROR! state crc 0x%04x - should be 0x%04x",
  331. i,
  332. results[i].crcstate,
  333. state_known_crc[known_id]);
  334. results[i].err++;
  335. }
  336. total_errors += results[i].err;
  337. }
  338. }
  339. total_errors += check_data_types();
  340. /* and report results */
  341. ee_printf("CoreMark Size : %lu", (long unsigned)results[0].size);
  342. ee_printf("Total ticks : %lu", (long unsigned)total_time);
  343. #if HAS_FLOAT
  344. ee_printf("Total time (secs): %f", time_in_secs(total_time));
  345. if (time_in_secs(total_time) > 0)
  346. ee_printf("Iterations/Sec : %f",
  347. default_num_contexts * results[0].iterations
  348. / time_in_secs(total_time));
  349. #else
  350. ee_printf("Total time (secs): %d", time_in_secs(total_time));
  351. if (time_in_secs(total_time) > 0)
  352. ee_printf("Iterations/Sec : %d",
  353. default_num_contexts * results[0].iterations
  354. / time_in_secs(total_time));
  355. #endif
  356. if (time_in_secs(total_time) < 10)
  357. {
  358. ee_printf(
  359. "ERROR! Must execute for at least 10 secs for a valid result!");
  360. total_errors++;
  361. }
  362. ee_printf("Iterations : %lu",
  363. (long unsigned)default_num_contexts * results[0].iterations);
  364. ee_printf("Compiler version : %s", COMPILER_VERSION);
  365. ee_printf("Compiler flags : %s", COMPILER_FLAGS);
  366. #if (MULTITHREAD > 1)
  367. ee_printf("Parallel %s : %d", PARALLEL_METHOD, default_num_contexts);
  368. #endif
  369. ee_printf("Memory location : %s", MEM_LOCATION);
  370. /* output for verification */
  371. ee_printf("seedcrc : 0x%04x", seedcrc);
  372. if (results[0].execs & ID_LIST)
  373. for (i = 0; i < default_num_contexts; i++)
  374. ee_printf("[%d]crclist : 0x%04x", i, results[i].crclist);
  375. if (results[0].execs & ID_MATRIX)
  376. for (i = 0; i < default_num_contexts; i++)
  377. ee_printf("[%d]crcmatrix : 0x%04x", i, results[i].crcmatrix);
  378. if (results[0].execs & ID_STATE)
  379. for (i = 0; i < default_num_contexts; i++)
  380. ee_printf("[%d]crcstate : 0x%04x", i, results[i].crcstate);
  381. for (i = 0; i < default_num_contexts; i++)
  382. ee_printf("[%d]crcfinal : 0x%04x", i, results[i].crc);
  383. if (total_errors == 0)
  384. {
  385. ee_printf(
  386. "Correct operation validated. See README.md for run and reporting "
  387. "rules.");
  388. #if HAS_FLOAT
  389. if (known_id == 3)
  390. {
  391. ee_printf("CoreMark 1.0 : %f / %s %s",
  392. default_num_contexts * results[0].iterations
  393. / time_in_secs(total_time),
  394. COMPILER_VERSION,
  395. COMPILER_FLAGS);
  396. #if defined(MEM_LOCATION) && !defined(MEM_LOCATION_UNSPEC)
  397. ee_printf(" / %s", MEM_LOCATION);
  398. #else
  399. ee_printf(" / %s", mem_name[MEM_METHOD]);
  400. #endif
  401. #if (MULTITHREAD > 1)
  402. ee_printf(" / %d:%s", default_num_contexts, PARALLEL_METHOD);
  403. #endif
  404. ee_printf("");
  405. }
  406. #endif
  407. }
  408. if (total_errors > 0)
  409. ee_printf("Errors detected");
  410. if (total_errors < 0)
  411. ee_printf(
  412. "Cannot validate operation for these seed values, please compare "
  413. "with results on a known platform.");
  414. #if (MEM_METHOD == MEM_MALLOC)
  415. for (i = 0; i < MULTITHREAD; i++)
  416. portable_free(results[i].memblock[0]);
  417. #endif
  418. /* And last call any target specific code for finalizing */
  419. portable_fini(&(results[0].port));
  420. return MAIN_RETURN_VAL;
  421. }