/* $Id: matmult.c,v 1.2 2005/04/04 11:34:58 csg Exp $ */ /* matmult.c */ /* was mm.c! */ /*----------------------------------------------------------------------* * To make this program compile under our assumed embedded environment, * we had to make several changes: * - Declare all functions in ANSI style, not K&R. * this includes adding return types in all cases! * - Declare function prototypes * - Disable all output * - Disable all UNIX-style includes * * This is a program that was developed from mm.c to matmult.c by * Thomas Lundqvist at Chalmers. *----------------------------------------------------------------------*/ #ifdef TEST #include "../mini_printf.h" #include "../posix_c.h" #else #include #endif /* * MATRIX MULTIPLICATION BENCHMARK PROGRAM: * This program multiplies 2 square matrices resulting in a 3rd * matrix. It tests a compiler's speed in handling multidimensional * arrays and simple arithmetic. */ #define UPPERLIMIT 20 typedef int matrix [UPPERLIMIT][UPPERLIMIT]; int Seed; matrix ArrayA, ArrayB, ResultArray; /* Our picky compiler wants prototypes! */ void Multiply(matrix A, matrix B, matrix Res); void InitSeed(void); void Test(matrix A, matrix B, matrix Res); void Initialize(matrix Array); int RandomInteger(void); int main() { InitSeed(); Test(ArrayA, ArrayB, ResultArray); return 0; } void InitSeed(void) /* * Initializes the seed used in the random number generator. */ { Seed = 11; } void Test(matrix A, matrix B, matrix Res) /* * Runs a multiplication test on an array. Calculates and prints the * time it takes to multiply the matrices. */ { Initialize(A); Initialize(B); Multiply(A, B, Res); } void Initialize(matrix Array) /* * Intializes the given array with random integers. */ { int OuterIndex, InnerIndex; for (OuterIndex = 0; OuterIndex < UPPERLIMIT; OuterIndex++) for (InnerIndex = 0; InnerIndex < UPPERLIMIT; InnerIndex++) Array[OuterIndex][InnerIndex] = RandomInteger(); } int RandomInteger(void) /* * Generates random integers between 0 and 8095 */ { Seed = ((Seed * 133) + 81) % 8095; return (Seed); } void Multiply(matrix A, matrix B, matrix Res) /* * Multiplies arrays A and B and stores the result in ResultArray. */ { register int Outer, Inner, Index; int correct[UPPERLIMIT][UPPERLIMIT] = { {299025555, 428867510, 307439975, 386207160, 396371070, 303594065, 328409690, 341645930, 283475530, 375664920, 322836930, 258835200, 245610370, 182618575, 309017325, 335028100, 268651545, 222406195, 198855860, 364394310 }, {299416135, 407617170, 224035875, 352103630, 367514625, 298691175, 359848440, 328394850, 296822885, 379617980, 330616655, 278649795, 201811625, 250445860, 332677900, 377069120, 235601575, 249849505, 208394960, 331683255 }, {284696525, 373120150, 294929525, 354615915, 417838840, 282393455, 335120615, 383477735, 243364650, 392626135, 378642230, 293506380, 164876215, 304846015, 387796425, 372144955, 250226090, 301085760, 218606110, 361013440 }, {285308695, 436720215, 330921600, 396465720, 402310385, 340296470, 368992340, 342862740, 305888145, 422887500, 419844730, 289147785, 237306735, 244859305, 361272950, 375471285, 298553835, 246959325, 231373360, 380185620 }, {260192285, 403998870, 250372700, 324200380, 382565575, 312511650, 331926440, 366087025, 224415010, 354709455, 345530130, 312677820, 159666100, 251904635, 333926775, 332592295, 262170925, 313179830, 221042160, 321758180 }, {271510895, 365573690, 290183700, 359496170, 367919860, 299009895, 335870565, 349582990, 255379070, 386368200, 326381430, 277533510, 201252735, 253067830, 356113700, 337113310, 258240385, 258791850, 218458610, 378238545 }, {239648635, 443848495, 296942525, 362611905, 407944050, 370702050, 360703515, 339187975, 253833785, 361672305, 348498180, 287441720, 181371100, 263307485, 316808975, 370510095, 244914250, 297697330, 230124010, 323835355 }, {229941535, 432064970, 310209425, 346217530, 381053425, 310279625, 348090540, 311674075, 256387685, 326186155, 318403080, 309878195, 163966100, 242694835, 310918275, 360896445, 250254675, 258587155, 216199885, 354993555 }, {331624225, 455737550, 274157775, 396354740, 383259490, 339493205, 378766365, 374078510, 285908050, 424499310, 349918855, 363016205, 192746665, 288837040, 378106850, 391249930, 293789565, 290700810, 271224885, 360120915 }, {220700605, 315509135, 252483025, 330111085, 351966570, 262721815, 326794115, 343373980, 213120905, 357840520, 373828780, 263554100, 173631495, 228086900, 316076200, 305795225, 195101445, 259333795, 192774810, 344247360 }, {341276930, 476745285, 324404250, 452424985, 456968170, 303900240, 395632640, 398188230, 317416930, 438178320, 426189280, 360646750, 214257695, 252338175, 378630600, 423406325, 286693595, 283303420, 257251935, 422275285 }, {294551445, 447913065, 355713550, 423376820, 403822710, 382128320, 403462290, 367819090, 311518095, 439156525, 421209930, 303843185, 237178460, 259209555, 360288400, 377853010, 322569810, 286626625, 232100685, 387512795 }, {281375070, 379501990, 221204200, 333853670, 361764935, 261315320, 337370865, 315365565, 282250795, 388698475, 365637480, 285879660, 197444960, 220419905, 379882775, 345707385, 275709360, 269005075, 192760710, 362570245 }, {308886935, 415271320, 315737425, 405758330, 428853500, 277745900, 371931215, 346487300, 301302210, 387115005, 370497680, 294980195, 204993350, 280451585, 379768150, 411223170, 265583400, 260652505, 208746660, 348518305 }, {231506145, 360383640, 240769575, 285255420, 320523410, 309606270, 325590140, 310778765, 221690570, 291650075, 279016380, 284980110, 140131885, 253784655, 293661250, 319996460, 226707535, 240264700, 205859310, 324022470 }, {221734620, 316924240, 207240725, 293196395, 324739635, 244834870, 326797565, 300452165, 220367070, 256003675, 293100455, 204000410, 166630785, 222745205, 275662450, 308202560, 174283660, 230613000, 159394335, 288378545 }, {282410765, 431352230, 272711525, 393625525, 421215905, 328439485, 393888865, 347937120, 316825240, 432074390, 435987780, 319974990, 177017630, 294246070, 406029600, 414745165, 283499455, 320402665, 243968585, 394293475 }, {308276390, 443185155, 264907700, 395092575, 408634105, 354947385, 438284015, 389949420, 332286140, 385675615, 339214105, 307071040, 205647305, 304525170, 406367875, 419036140, 279270005, 293509740, 243436910, 440976450 }, {351079655, 457523660, 322449075, 428459185, 467185470, 366571065, 423664215, 445208730, 315673505, 418778345, 375985705, 314208050, 260680795, 301996225, 379555850, 413526475, 268543420, 314042420, 249572160, 410253035 }, {292180390, 395382955, 267986325, 335002775, 378450555, 289198835, 366834840, 348243945, 275004815, 359684865, 324037155, 295331215, 199708380, 263641570, 347534900, 376827490, 237554455, 253871215, 198620035, 362081675 } }; for (Outer = 0; Outer < UPPERLIMIT; Outer++) for (Inner = 0; Inner < UPPERLIMIT; Inner++) { Res [Outer][Inner] = 0; for (Index = 0; Index < UPPERLIMIT; Index++) Res[Outer][Inner] += A[Outer][Index] * B[Index][Inner]; } // Check result #ifdef TEST for(int i = 0; i < UPPERLIMIT; i++){ for(int j = 0; j < UPPERLIMIT; j++){ if(Res[i][j] != correct[i][j]){ int ok = 0; memcpy(out_mem, &ok, sizeof(int)); return; } } } int ok = 1; memcpy(out_mem, &ok, sizeof(int)); #endif }