os161-1.99
 All Data Structures
matmult-orig.c
00001 /*
00002  * Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
00003  *      The President and Fellows of Harvard College.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  * 1. Redistributions of source code must retain the above copyright
00009  *    notice, this list of conditions and the following disclaimer.
00010  * 2. Redistributions in binary form must reproduce the above copyright
00011  *    notice, this list of conditions and the following disclaimer in the
00012  *    documentation and/or other materials provided with the distribution.
00013  * 3. Neither the name of the University nor the names of its contributors
00014  *    may be used to endorse or promote products derived from this software
00015  *    without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
00018  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020  * ARE DISCLAIMED.  IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
00021  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00022  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00023  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00024  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00025  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00026  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00027  * SUCH DAMAGE.
00028  */
00029 
00030 /* matmult-orig.c 
00031  *    Test program to do matrix multiplication on large arrays.
00032  *
00033  *    Intended to stress virtual memory system.
00034  *
00035  *    This is the original CS161 matmult program. Unfortunately,
00036  *    because matrix multiplication is order N^2 in space and N^3 in
00037  *    time, when this is made large enough to be an interesting VM
00038  *    test, it becomes so large that it takes hours to run.
00039  *
00040  *    So you probably want to just run matmult, which has been
00041  *    gimmicked up to be order N^3 in space and thus have a tolerable
00042  *    running time. This version is provided for reference only.
00043  *
00044  *    Once the VM assignment is complete your system should be able to
00045  *    survive this, if you have the patience to run it.
00046  */
00047 
00048 #include <unistd.h>
00049 #include <stdio.h>
00050 
00051 #define Dim     360     /* sum total of the arrays doesn't fit in 
00052                          * physical memory 
00053                          */
00054 
00055 #define RIGHT  46397160         /* correct answer */
00056 
00057 int A[Dim][Dim];
00058 int B[Dim][Dim];
00059 int C[Dim][Dim];
00060 
00061 int
00062 main()
00063 {
00064     int i, j, k, r;
00065 
00066     for (i = 0; i < Dim; i++)           /* first initialize the matrices */
00067         for (j = 0; j < Dim; j++) {
00068              A[i][j] = i;
00069              B[i][j] = j;
00070              C[i][j] = 0;
00071         }
00072 
00073     for (i = 0; i < Dim; i++)           /* then multiply them together */
00074         for (j = 0; j < Dim; j++)
00075             for (k = 0; k < Dim; k++)
00076                  C[i][j] += A[i][k] * B[k][j];
00077 
00078     printf("matmult-orig finished.\n");
00079     r = C[Dim-1][Dim-1];
00080     printf("answer is: %d (should be %d)\n", r, RIGHT);
00081     if (r != RIGHT) {
00082             printf("FAILED\n");
00083     }
00084     else {
00085             printf("Passed.\n");
00086     }
00087     return 0;
00088 }
 All Data Structures