core_main.c 16 KB

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