FPGAのソフトコアCPUをベンチマークで測定する(ソースコード)(1/2 ページ)

連載「MAX 10 FPGAで学ぶFPGA開発入門」の第7回、「FPGAのソフトコアCPUをベンチマークで測定する」のソースコードです。

» 2016年09月09日 10時00分 公開
[PR/MONOist]
PR

・FPGAのソフトコアCPUをベンチマークで測定する

オリジナルのDhrystone(List 1)

  1. /*
  2. ****************************************************************************
  3. *
  4. * "DHRYSTONE" Benchmark Program
  5. * -----------------------------
  6. *
  7. * Version: C, Version 2.1
  8. *
  9. * File: dhry_1.c (part 2 of 3)
  10. *
  11. * Date: May 25, 1988
  12. *
  13. * Author: Reinhold P. Weicker
  14. *
  15. ****************************************************************************
  16. */
  17. #include "dhry.h"
  18. /* Global Variables: */
  19. Rec_Pointer Ptr_Glob,
  20. Next_Ptr_Glob;
  21. int Int_Glob;
  22. Boolean Bool_Glob;
  23. char Ch_1_Glob,
  24. Ch_2_Glob;
  25. int Arr_1_Glob [50];
  26. int Arr_2_Glob [50] [50];
  27. extern char *malloc ();
  28. Enumeration Func_1 ();
  29. /* forward declaration necessary since Enumeration may not simply be int */
  30. #ifndef REG
  31. Boolean Reg = false;
  32. #define REG
  33. /* REG becomes defined as empty */
  34. /* i.e. no register variables */
  35. #else
  36. Boolean Reg = true;
  37. #endif
  38. /* variables for time measurement: */
  39. #ifdef TIMES
  40. struct tms time_info;
  41. extern int times ();
  42. /* see library function "times" */
  43. #define Too_Small_Time (2*HZ)
  44. /* Measurements should last at least about 2 seconds */
  45. #endif
  46. #ifdef TIME
  47. extern long time();
  48. /* see library function "time" */
  49. #define Too_Small_Time 2
  50. /* Measurements should last at least 2 seconds */
  51. #endif
  52. #ifdef MSC_CLOCK
  53. extern clock_t clock();
  54. #define Too_Small_Time (2*HZ)
  55. #endif
  56. long Begin_Time,
  57. End_Time,
  58. User_Time;
  59. float Microseconds,
  60. Dhrystones_Per_Second;
  61. /* end of variables for time measurement */
  62. main ()
  63. /*****/
  64. /* main program, corresponds to procedures */
  65. /* Main and Proc_0 in the Ada version */
  66. {
  67. One_Fifty Int_1_Loc;
  68. REG One_Fifty Int_2_Loc;
  69. One_Fifty Int_3_Loc;
  70. REG char Ch_Index;
  71. Enumeration Enum_Loc;
  72. Str_30 Str_1_Loc;
  73. Str_30 Str_2_Loc;
  74. REG int Run_Index;
  75. REG int Number_Of_Runs;
  76. /* Initializations */
  77. Next_Ptr_Glob = (Rec_Pointer) malloc (sizeof (Rec_Type));
  78. Ptr_Glob = (Rec_Pointer) malloc (sizeof (Rec_Type));
  79. Ptr_Glob->Ptr_Comp = Next_Ptr_Glob;
  80. Ptr_Glob->Discr = Ident_1;
  81. Ptr_Glob->variant.var_1.Enum_Comp = Ident_3;
  82. Ptr_Glob->variant.var_1.Int_Comp = 40;
  83. strcpy (Ptr_Glob->variant.var_1.Str_Comp,
  84. "DHRYSTONE PROGRAM, SOME STRING");
  85. strcpy (Str_1_Loc, "DHRYSTONE PROGRAM, 1'ST STRING");
  86. Arr_2_Glob [8][7] = 10;
  87. /* Was missing in published program. Without this statement, */
  88. /* Arr_2_Glob [8][7] would have an undefined value. */
  89. /* Warning: With 16-Bit processors and Number_Of_Runs > 32000, */
  90. /* overflow may occur for this array element. */
  91. printf ("\n");
  92. printf ("Dhrystone Benchmark, Version 2.1 (Language: C)\n");
  93. printf ("\n");
  94. if (Reg)
  95. {
  96. printf ("Program compiled with 'register' attribute\n");
  97. printf ("\n");
  98. }
  99. else
  100. {
  101. printf ("Program compiled without 'register' attribute\n");
  102. printf ("\n");
  103. }
  104. printf ("Please give the number of runs through the benchmark: ");
  105. {
  106. int n;
  107. scanf ("%d", &n);
  108. Number_Of_Runs = n;
  109. }
  110. printf ("\n");
  111. printf ("Execution starts, %d runs through Dhrystone\n", Number_Of_Runs);
  112. /***************/
  113. /* Start timer */
  114. /***************/
  115. #ifdef TIMES
  116. times (&time_info);
  117. Begin_Time = (long) time_info.tms_utime;
  118. #endif
  119. #ifdef TIME
  120. Begin_Time = time ( (long *) 0);
  121. #endif
  122. #ifdef MSC_CLOCK
  123. Begin_Time = clock();
  124. #endif
  125. for (Run_Index = 1; Run_Index <= Number_Of_Runs; ++Run_Index)
  126. {
  127. Proc_5();
  128. Proc_4();
  129. /* Ch_1_Glob == 'A', Ch_2_Glob == 'B', Bool_Glob == true */
  130. Int_1_Loc = 2;
  131. Int_2_Loc = 3;
  132. strcpy (Str_2_Loc, "DHRYSTONE PROGRAM, 2'ND STRING");
  133. Enum_Loc = Ident_2;
  134. Bool_Glob = ! Func_2 (Str_1_Loc, Str_2_Loc);
  135. /* Bool_Glob == 1 */
  136. while (Int_1_Loc < Int_2_Loc) /* loop body executed once */
  137. {
  138. Int_3_Loc = 5 * Int_1_Loc - Int_2_Loc;
  139. /* Int_3_Loc == 7 */
  140. Proc_7 (Int_1_Loc, Int_2_Loc, &Int_3_Loc);
  141. /* Int_3_Loc == 7 */
  142. Int_1_Loc += 1;
  143. } /* while */
  144. /* Int_1_Loc == 3, Int_2_Loc == 3, Int_3_Loc == 7 */
  145. Proc_8 (Arr_1_Glob, Arr_2_Glob, Int_1_Loc, Int_3_Loc);
  146. /* Int_Glob == 5 */
  147. Proc_1 (Ptr_Glob);
  148. for (Ch_Index = 'A'; Ch_Index <= Ch_2_Glob; ++Ch_Index)
  149. /* loop body executed twice */
  150. {
  151. if (Enum_Loc == Func_1 (Ch_Index, 'C'))
  152. /* then, not executed */
  153. {
  154. Proc_6 (Ident_1, &Enum_Loc);
  155. strcpy (Str_2_Loc, "DHRYSTONE PROGRAM, 3'RD STRING");
  156. Int_2_Loc = Run_Index;
  157. Int_Glob = Run_Index;
  158. }
  159. }
  160. /* Int_1_Loc == 3, Int_2_Loc == 3, Int_3_Loc == 7 */
  161. Int_2_Loc = Int_2_Loc * Int_1_Loc;
  162. Int_1_Loc = Int_2_Loc / Int_3_Loc;
  163. Int_2_Loc = 7 * (Int_2_Loc - Int_3_Loc) - Int_1_Loc;
  164. /* Int_1_Loc == 1, Int_2_Loc == 13, Int_3_Loc == 7 */
  165. Proc_2 (&Int_1_Loc);
  166. /* Int_1_Loc == 5 */
  167. } /* loop "for Run_Index" */
  168. /**************/
  169. /* Stop timer */
  170. /**************/
  171. #ifdef TIMES
  172. times (&time_info);
  173. End_Time = (long) time_info.tms_utime;
  174. #endif
  175. #ifdef TIME
  176. End_Time = time ( (long *) 0);
  177. #endif
  178. #ifdef MSC_CLOCK
  179. End_Time = clock();
  180. #endif
  181. printf ("Execution ends\n");
  182. printf ("\n");
  183. printf ("Final values of the variables used in the benchmark:\n");
  184. printf ("\n");
  185. printf ("Int_Glob: %d\n", Int_Glob);
  186. printf (" should be: %d\n", 5);
  187. printf ("Bool_Glob: %d\n", Bool_Glob);
  188. printf (" should be: %d\n", 1);
  189. printf ("Ch_1_Glob: %c\n", Ch_1_Glob);
  190. printf (" should be: %c\n", 'A');
  191. printf ("Ch_2_Glob: %c\n", Ch_2_Glob);
  192. printf (" should be: %c\n", 'B');
  193. printf ("Arr_1_Glob[8]: %d\n", Arr_1_Glob[8]);
  194. printf (" should be: %d\n", 7);
  195. printf ("Arr_2_Glob[8][7]: %d\n", Arr_2_Glob[8][7]);
  196. printf (" should be: Number_Of_Runs + 10\n");
  197. printf ("Ptr_Glob->\n");
  198. printf (" Ptr_Comp: %d\n", (int) Ptr_Glob->Ptr_Comp);
  199. printf (" should be: (implementation-dependent)\n");
  200. printf (" Discr: %d\n", Ptr_Glob->Discr);
  201. printf (" should be: %d\n", 0);
  202. printf (" Enum_Comp: %d\n", Ptr_Glob->variant.var_1.Enum_Comp);
  203. printf (" should be: %d\n", 2);
  204. printf (" Int_Comp: %d\n", Ptr_Glob->variant.var_1.Int_Comp);
  205. printf (" should be: %d\n", 17);
  206. printf (" Str_Comp: %s\n", Ptr_Glob->variant.var_1.Str_Comp);
  207. printf (" should be: DHRYSTONE PROGRAM, SOME STRING\n");
  208. printf ("Next_Ptr_Glob->\n");
  209. printf (" Ptr_Comp: %d\n", (int) Next_Ptr_Glob->Ptr_Comp);
  210. printf (" should be: (implementation-dependent), same as above\n");
  211. printf (" Discr: %d\n", Next_Ptr_Glob->Discr);
  212. printf (" should be: %d\n", 0);
  213. printf (" Enum_Comp: %d\n", Next_Ptr_Glob->variant.var_1.Enum_Comp);
  214. printf (" should be: %d\n", 1);
  215. printf (" Int_Comp: %d\n", Next_Ptr_Glob->variant.var_1.Int_Comp);
  216. printf (" should be: %d\n", 18);
  217. printf (" Str_Comp: %s\n",
  218. Next_Ptr_Glob->variant.var_1.Str_Comp);
  219. printf (" should be: DHRYSTONE PROGRAM, SOME STRING\n");
  220. printf ("Int_1_Loc: %d\n", Int_1_Loc);
  221. printf (" should be: %d\n", 5);
  222. printf ("Int_2_Loc: %d\n", Int_2_Loc);
  223. printf (" should be: %d\n", 13);
  224. printf ("Int_3_Loc: %d\n", Int_3_Loc);
  225. printf (" should be: %d\n", 7);
  226. printf ("Enum_Loc: %d\n", Enum_Loc);
  227. printf (" should be: %d\n", 1);
  228. printf ("Str_1_Loc: %s\n", Str_1_Loc);
  229. printf (" should be: DHRYSTONE PROGRAM, 1'ST STRING\n");
  230. printf ("Str_2_Loc: %s\n", Str_2_Loc);
  231. printf (" should be: DHRYSTONE PROGRAM, 2'ND STRING\n");
  232. printf ("\n");
  233. User_Time = End_Time - Begin_Time;
  234. if (User_Time < Too_Small_Time)
  235. {
  236. printf ("Measured time too small to obtain meaningful results\n");
  237. printf ("Please increase number of runs\n");
  238. printf ("\n");
  239. }
  240. else
  241. {
  242. #ifdef TIME
  243. Microseconds = (float) User_Time * Mic_secs_Per_Second
  244. / (float) Number_Of_Runs;
  245. Dhrystones_Per_Second = (float) Number_Of_Runs / (float) User_Time;
  246. #else
  247. Microseconds = (float) User_Time * Mic_secs_Per_Second
  248. / ((float) HZ * ((float) Number_Of_Runs));
  249. Dhrystones_Per_Second = ((float) HZ * (float) Number_Of_Runs)
  250. / (float) User_Time;
  251. #endif
  252. printf ("Microseconds for one run through Dhrystone: ");
  253. printf ("%6.1f \n", Microseconds);
  254. printf ("Dhrystones per Second: ");
  255. printf ("%6.1f \n", Dhrystones_Per_Second);
  256. printf ("\n");
  257. }
  258. }
  259. Proc_1 (Ptr_Val_Par)
  260. /******************/
  261. REG Rec_Pointer Ptr_Val_Par;
  262. /* executed once */
  263. {
  264. REG Rec_Pointer Next_Record = Ptr_Val_Par->Ptr_Comp;
  265. /* == Ptr_Glob_Next */
  266. /* Local variable, initialized with Ptr_Val_Par->Ptr_Comp, */
  267. /* corresponds to "rename" in Ada, "with" in Pascal */
  268. structassign (*Ptr_Val_Par->Ptr_Comp, *Ptr_Glob);
  269. Ptr_Val_Par->variant.var_1.Int_Comp = 5;
  270. Next_Record->variant.var_1.Int_Comp
  271. = Ptr_Val_Par->variant.var_1.Int_Comp;
  272. Next_Record->Ptr_Comp = Ptr_Val_Par->Ptr_Comp;
  273. Proc_3 (&Next_Record->Ptr_Comp);
  274. /* Ptr_Val_Par->Ptr_Comp->Ptr_Comp
  275. == Ptr_Glob->Ptr_Comp */
  276. if (Next_Record->Discr == Ident_1)
  277. /* then, executed */
  278. {
  279. Next_Record->variant.var_1.Int_Comp = 6;
  280. Proc_6 (Ptr_Val_Par->variant.var_1.Enum_Comp,
  281. &Next_Record->variant.var_1.Enum_Comp);
  282. Next_Record->Ptr_Comp = Ptr_Glob->Ptr_Comp;
  283. Proc_7 (Next_Record->variant.var_1.Int_Comp, 10,
  284. &Next_Record->variant.var_1.Int_Comp);
  285. }
  286. else /* not executed */
  287. structassign (*Ptr_Val_Par, *Ptr_Val_Par->Ptr_Comp);
  288. } /* Proc_1 */
  289. Proc_2 (Int_Par_Ref)
  290. /******************/
  291. /* executed once */
  292. /* *Int_Par_Ref == 1, becomes 4 */
  293. One_Fifty *Int_Par_Ref;
  294. {
  295. One_Fifty Int_Loc;
  296. Enumeration Enum_Loc;
  297. Int_Loc = *Int_Par_Ref + 10;
  298. do /* executed once */
  299. if (Ch_1_Glob == 'A')
  300. /* then, executed */
  301. {
  302. Int_Loc -= 1;
  303. *Int_Par_Ref = Int_Loc - Int_Glob;
  304. Enum_Loc = Ident_1;
  305. } /* if */
  306. while (Enum_Loc != Ident_1); /* true */
  307. } /* Proc_2 */
  308. Proc_3 (Ptr_Ref_Par)
  309. /******************/
  310. /* executed once */
  311. /* Ptr_Ref_Par becomes Ptr_Glob */
  312. Rec_Pointer *Ptr_Ref_Par;
  313. {
  314. if (Ptr_Glob != Null)
  315. /* then, executed */
  316. *Ptr_Ref_Par = Ptr_Glob->Ptr_Comp;
  317. Proc_7 (10, Int_Glob, &Ptr_Glob->variant.var_1.Int_Comp);
  318. } /* Proc_3 */
  319. Proc_4 () /* without parameters */
  320. /*******/
  321. /* executed once */
  322. {
  323. Boolean Bool_Loc;
  324. Bool_Loc = Ch_1_Glob == 'A';
  325. Bool_Glob = Bool_Loc | Bool_Glob;
  326. Ch_2_Glob = 'B';
  327. } /* Proc_4 */
  328. Proc_5 () /* without parameters */
  329. /*******/
  330. /* executed once */
  331. {
  332. Ch_1_Glob = 'A';
  333. Bool_Glob = false;
  334. } /* Proc_5 */
  335. /* Procedure for the assignment of structures, */
  336. /* if the C compiler doesn't support this feature */
  337. #ifdef NOSTRUCTASSIGN
  338. memcpy (d, s, l)
  339. register char *d;
  340. register char *s;
  341. register int l;
  342. {
  343. while (l--) *d++ = *s++;
  344. }
  345. #endif
List1
       1|2 次のページへ

提供:日本アルテラ株式会社
アイティメディア営業企画/制作:MONOist 編集部/掲載内容有効期限:2016年10月10日

Copyright © ITmedia, Inc. All Rights Reserved.