core_main.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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. /* 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,(ee_u16)0x3340,(ee_u16)0x6a79,(ee_u16)0xe714,(ee_u16)0xe3c1};
  29. static ee_u16 matrix_known_crc[] = {(ee_u16)0xbe52,(ee_u16)0x1199,(ee_u16)0x5608,(ee_u16)0x1fd7,(ee_u16)0x0747};
  30. static ee_u16 state_known_crc[] = {(ee_u16)0x5e47,(ee_u16)0x39bf,(ee_u16)0xe5a4,(ee_u16)0x8e3a,(ee_u16)0x8d84};
  31. void *iterate(void *pres) {
  32. ee_u32 i;
  33. ee_u16 crc;
  34. core_results *res=(core_results *)pres;
  35. ee_u32 iterations=res->iterations;
  36. res->crc=0;
  37. res->crclist=0;
  38. res->crcmatrix=0;
  39. res->crcstate=0;
  40. for (i=0; i<iterations; i++) {
  41. crc=core_bench_list(res,1);
  42. res->crc=crcu16(crc,res->crc);
  43. crc=core_bench_list(res,-1);
  44. res->crc=crcu16(crc,res->crc);
  45. if (i==0) res->crclist=res->crc;
  46. }
  47. return NULL;
  48. }
  49. #if (SEED_METHOD==SEED_ARG)
  50. ee_s32 get_seed_args(int i, int argc, char *argv[]);
  51. #define get_seed(x) (ee_s16)get_seed_args(x,argc,argv)
  52. #define get_seed_32(x) get_seed_args(x,argc,argv)
  53. #else /* via function or volatile */
  54. ee_s32 get_seed_32(int i);
  55. #define get_seed(x) (ee_s16)get_seed_32(x)
  56. #endif
  57. #if (MEM_METHOD==MEM_STATIC)
  58. ee_u8 static_memblk[TOTAL_DATA_SIZE];
  59. #endif
  60. char *mem_name[3] = {"Static","Heap","Stack"};
  61. /* Function: main
  62. Main entry routine for the benchmark.
  63. This function is responsible for the following steps:
  64. 1 - Initialize input seeds from a source that cannot be determined at compile time.
  65. 2 - Initialize memory block for use.
  66. 3 - Run and time the benchmark.
  67. 4 - Report results, testing the validity of the output if the seeds are known.
  68. Arguments:
  69. 1 - first seed : Any value
  70. 2 - second seed : Must be identical to first for iterations to be identical
  71. 3 - third seed : Any value, should be at least an order of magnitude less then the input size, but bigger then 32.
  72. 4 - Iterations : Special, if set to 0, iterations will be automatically determined such that the benchmark will run between 10 to 100 secs
  73. */
  74. #if MAIN_HAS_NOARGC
  75. MAIN_RETURN_TYPE main(void) {
  76. int argc=0;
  77. char *argv[1];
  78. #else
  79. MAIN_RETURN_TYPE main(int argc, char *argv[]) {
  80. #endif
  81. ee_u16 i,j=0,num_algorithms=0;
  82. ee_s16 known_id=-1,total_errors=0;
  83. ee_u16 seedcrc=0;
  84. CORE_TICKS total_time;
  85. core_results results[MULTITHREAD];
  86. #if (MEM_METHOD==MEM_STACK)
  87. ee_u8 stack_memblock[TOTAL_DATA_SIZE*MULTITHREAD];
  88. #endif
  89. /* first call any initializations needed */
  90. portable_init(&(results[0].port), &argc, argv);
  91. /* First some checks to make sure benchmark will run ok */
  92. if (sizeof(struct list_head_s)>128) {
  93. ee_printf("list_head structure too big for comparable data!\n");
  94. return MAIN_RETURN_VAL;
  95. }
  96. results[0].seed1=get_seed(1);
  97. results[0].seed2=get_seed(2);
  98. results[0].seed3=get_seed(3);
  99. results[0].iterations=get_seed_32(4);
  100. #if CORE_DEBUG
  101. results[0].iterations=1;
  102. #endif
  103. results[0].execs=get_seed_32(5);
  104. if (results[0].execs==0) { /* if not supplied, execute all algorithms */
  105. results[0].execs=ALL_ALGORITHMS_MASK;
  106. }
  107. /* put in some default values based on one seed only for easy testing */
  108. if ((results[0].seed1==0) && (results[0].seed2==0) && (results[0].seed3==0)) { /* validation run */
  109. results[0].seed1=0;
  110. results[0].seed2=0;
  111. results[0].seed3=0x66;
  112. }
  113. if ((results[0].seed1==1) && (results[0].seed2==0) && (results[0].seed3==0)) { /* perfromance run */
  114. results[0].seed1=0x3415;
  115. results[0].seed2=0x3415;
  116. results[0].seed3=0x66;
  117. }
  118. #if (MEM_METHOD==MEM_STATIC)
  119. results[0].memblock[0]=(void *)static_memblk;
  120. results[0].size=TOTAL_DATA_SIZE;
  121. results[0].err=0;
  122. #if (MULTITHREAD>1)
  123. #error "Cannot use a static data area with multiple contexts!"
  124. #endif
  125. #elif (MEM_METHOD==MEM_MALLOC)
  126. for (i=0 ; i<MULTITHREAD; i++) {
  127. ee_s32 malloc_override=get_seed(7);
  128. if (malloc_override != 0)
  129. results[i].size=malloc_override;
  130. else
  131. results[i].size=TOTAL_DATA_SIZE;
  132. results[i].memblock[0]=portable_malloc(results[i].size);
  133. results[i].seed1=results[0].seed1;
  134. results[i].seed2=results[0].seed2;
  135. results[i].seed3=results[0].seed3;
  136. results[i].err=0;
  137. results[i].execs=results[0].execs;
  138. }
  139. #elif (MEM_METHOD==MEM_STACK)
  140. for (i=0 ; i<MULTITHREAD; i++) {
  141. results[i].memblock[0]=stack_memblock+i*TOTAL_DATA_SIZE;
  142. results[i].size=TOTAL_DATA_SIZE;
  143. results[i].seed1=results[0].seed1;
  144. results[i].seed2=results[0].seed2;
  145. results[i].seed3=results[0].seed3;
  146. results[i].err=0;
  147. results[i].execs=results[0].execs;
  148. }
  149. #else
  150. #error "Please define a way to initialize a memory block."
  151. #endif
  152. /* Data init */
  153. /* Find out how space much we have based on number of algorithms */
  154. for (i=0; i<NUM_ALGORITHMS; i++) {
  155. if ((1<<(ee_u32)i) & results[0].execs)
  156. num_algorithms++;
  157. }
  158. for (i=0 ; i<MULTITHREAD; i++)
  159. results[i].size=results[i].size/num_algorithms;
  160. /* Assign pointers */
  161. for (i=0; i<NUM_ALGORITHMS; i++) {
  162. ee_u32 ctx;
  163. if ((1<<(ee_u32)i) & results[0].execs) {
  164. for (ctx=0 ; ctx<MULTITHREAD; ctx++)
  165. results[ctx].memblock[i+1]=(char *)(results[ctx].memblock[0])+results[0].size*j;
  166. j++;
  167. }
  168. }
  169. /* call inits */
  170. for (i=0 ; i<MULTITHREAD; i++) {
  171. if (results[i].execs & ID_LIST) {
  172. results[i].list=core_list_init(results[0].size,results[i].memblock[1],results[i].seed1);
  173. }
  174. if (results[i].execs & ID_MATRIX) {
  175. core_init_matrix(results[0].size, results[i].memblock[2], (ee_s32)results[i].seed1 | (((ee_s32)results[i].seed2) << 16), &(results[i].mat) );
  176. }
  177. if (results[i].execs & ID_STATE) {
  178. core_init_state(results[0].size,results[i].seed1,results[i].memblock[3]);
  179. }
  180. }
  181. /* automatically determine number of iterations if not set */
  182. if (results[0].iterations==0) {
  183. secs_ret secs_passed=0;
  184. ee_u32 divisor;
  185. results[0].iterations=1;
  186. while (secs_passed < (secs_ret)1) {
  187. results[0].iterations*=10;
  188. start_time();
  189. iterate(&results[0]);
  190. stop_time();
  191. secs_passed=time_in_secs(get_time());
  192. }
  193. /* now we know it executes for at least 1 sec, set actual run time at about 10 secs */
  194. divisor=(ee_u32)secs_passed;
  195. 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 */
  196. divisor=1;
  197. results[0].iterations*=1+10/divisor;
  198. }
  199. /* perform actual benchmark */
  200. start_time();
  201. #if (MULTITHREAD>1)
  202. if (default_num_contexts>MULTITHREAD) {
  203. default_num_contexts=MULTITHREAD;
  204. }
  205. for (i=0 ; i<default_num_contexts; i++) {
  206. results[i].iterations=results[0].iterations;
  207. results[i].execs=results[0].execs;
  208. core_start_parallel(&results[i]);
  209. }
  210. for (i=0 ; i<default_num_contexts; i++) {
  211. core_stop_parallel(&results[i]);
  212. }
  213. #else
  214. iterate(&results[0]);
  215. #endif
  216. stop_time();
  217. total_time=get_time();
  218. /* get a function of the input to report */
  219. seedcrc=crc16(results[0].seed1,seedcrc);
  220. seedcrc=crc16(results[0].seed2,seedcrc);
  221. seedcrc=crc16(results[0].seed3,seedcrc);
  222. seedcrc=crc16(results[0].size,seedcrc);
  223. switch (seedcrc) { /* test known output for common seeds */
  224. case 0x8a02: /* seed1=0, seed2=0, seed3=0x66, size 2000 per algorithm */
  225. known_id=0;
  226. ee_printf("6k performance run parameters for coremark.\n");
  227. break;
  228. case 0x7b05: /* seed1=0x3415, seed2=0x3415, seed3=0x66, size 2000 per algorithm */
  229. known_id=1;
  230. ee_printf("6k validation run parameters for coremark.\n");
  231. break;
  232. case 0x4eaf: /* seed1=0x8, seed2=0x8, seed3=0x8, size 400 per algorithm */
  233. known_id=2;
  234. ee_printf("Profile generation run parameters for coremark.\n");
  235. break;
  236. case 0xe9f5: /* seed1=0, seed2=0, seed3=0x66, size 666 per algorithm */
  237. known_id=3;
  238. ee_printf("2K performance run parameters for coremark.\n");
  239. break;
  240. case 0x18f2: /* seed1=0x3415, seed2=0x3415, seed3=0x66, size 666 per algorithm */
  241. known_id=4;
  242. ee_printf("2K validation run parameters for coremark.\n");
  243. break;
  244. default:
  245. total_errors=-1;
  246. break;
  247. }
  248. if (known_id>=0) {
  249. for (i=0 ; i<default_num_contexts; i++) {
  250. results[i].err=0;
  251. if ((results[i].execs & ID_LIST) &&
  252. (results[i].crclist!=list_known_crc[known_id])) {
  253. ee_printf("[%u]ERROR! list crc 0x%04x - should be 0x%04x\n",i,results[i].crclist,list_known_crc[known_id]);
  254. results[i].err++;
  255. }
  256. if ((results[i].execs & ID_MATRIX) &&
  257. (results[i].crcmatrix!=matrix_known_crc[known_id])) {
  258. ee_printf("[%u]ERROR! matrix crc 0x%04x - should be 0x%04x\n",i,results[i].crcmatrix,matrix_known_crc[known_id]);
  259. results[i].err++;
  260. }
  261. if ((results[i].execs & ID_STATE) &&
  262. (results[i].crcstate!=state_known_crc[known_id])) {
  263. ee_printf("[%u]ERROR! state crc 0x%04x - should be 0x%04x\n",i,results[i].crcstate,state_known_crc[known_id]);
  264. results[i].err++;
  265. }
  266. total_errors+=results[i].err;
  267. }
  268. }
  269. total_errors+=check_data_types();
  270. /* and report results */
  271. ee_printf("CoreMark Size : %lu\n",(ee_u32)results[0].size);
  272. ee_printf("Total ticks : %lu\n",(ee_u32)total_time);
  273. #if HAS_FLOAT
  274. ee_printf("Total time (secs): %f\n",time_in_secs(total_time));
  275. if (time_in_secs(total_time) > 0)
  276. ee_printf("Iterations/Sec : %f\n",default_num_contexts*results[0].iterations/time_in_secs(total_time));
  277. #else
  278. ee_printf("Total time (secs): %d\n",time_in_secs(total_time));
  279. if (time_in_secs(total_time) > 0)
  280. ee_printf("Iterations/Sec : %d\n",default_num_contexts*results[0].iterations/time_in_secs(total_time));
  281. #endif
  282. if (time_in_secs(total_time) < 10) {
  283. ee_printf("ERROR! Must execute for at least 10 secs for a valid result!\n");
  284. total_errors++;
  285. }
  286. ee_printf("Iterations : %lu\n",(ee_u32)default_num_contexts*results[0].iterations);
  287. ee_printf("Compiler version : %s\n",COMPILER_VERSION);
  288. ee_printf("Compiler flags : %s\n",COMPILER_FLAGS);
  289. #if (MULTITHREAD>1)
  290. ee_printf("Parallel %s : %d\n",PARALLEL_METHOD,default_num_contexts);
  291. #endif
  292. ee_printf("Memory location : %s\n",MEM_LOCATION);
  293. /* output for verification */
  294. ee_printf("seedcrc : 0x%04x\n",seedcrc);
  295. if (results[0].execs & ID_LIST)
  296. for (i=0 ; i<default_num_contexts; i++)
  297. ee_printf("[%d]crclist : 0x%04x\n",i,results[i].crclist);
  298. if (results[0].execs & ID_MATRIX)
  299. for (i=0 ; i<default_num_contexts; i++)
  300. ee_printf("[%d]crcmatrix : 0x%04x\n",i,results[i].crcmatrix);
  301. if (results[0].execs & ID_STATE)
  302. for (i=0 ; i<default_num_contexts; i++)
  303. ee_printf("[%d]crcstate : 0x%04x\n",i,results[i].crcstate);
  304. for (i=0 ; i<default_num_contexts; i++)
  305. ee_printf("[%d]crcfinal : 0x%04x\n",i,results[i].crc);
  306. if (total_errors==0) {
  307. ee_printf("Correct operation validated. See readme.txt for run and reporting rules.\n");
  308. #if HAS_FLOAT
  309. if (known_id==3) {
  310. ee_printf("CoreMark 1.0 : %f / %s %s",default_num_contexts*results[0].iterations/time_in_secs(total_time),COMPILER_VERSION,COMPILER_FLAGS);
  311. #if defined(MEM_LOCATION) && !defined(MEM_LOCATION_UNSPEC)
  312. ee_printf(" / %s",MEM_LOCATION);
  313. #else
  314. ee_printf(" / %s",mem_name[MEM_METHOD]);
  315. #endif
  316. #if (MULTITHREAD>1)
  317. ee_printf(" / %d:%s",default_num_contexts,PARALLEL_METHOD);
  318. #endif
  319. ee_printf("\n");
  320. }
  321. #endif
  322. }
  323. if (total_errors>0)
  324. ee_printf("Errors detected\n");
  325. if (total_errors<0)
  326. ee_printf("Cannot validate operation for these seed values, please compare with results on a known platform.\n");
  327. #if (MEM_METHOD==MEM_MALLOC)
  328. for (i=0 ; i<MULTITHREAD; i++)
  329. portable_free(results[i].memblock[0]);
  330. #endif
  331. /* And last call any target specific code for finalizing */
  332. portable_fini(&(results[0].port));
  333. return MAIN_RETURN_VAL;
  334. }