/* * Copyright (c) 2009, Diomidis Spinellis * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * $Id: tilingdemo.pde,v 1.2 2009/04/21 14:32:05 dds Exp $ * */ final int N = 12; // Matrix size final int TILE = 4; // Tile size final int W = 20; // Box width // After modifying the above adjust by hand the size() call // X starting coordinate of A * A = B final int AX = W; final int BX = AX + (N + 2) * W; final int Y = W; boolean tile = true; int thisFrame = 0; int tryFrame; void setup() { // Set the value of the "tile" parameter from the HTML applet if (online) { String s = param("tile"); if (s != null && s.equals("false")) tile = false; } // Explicit specification allows correct functioning of Processing's Export command // size(X + (N + 1) * W, (N + 2) * W); // println(BX + (N + 1) * W + " " + (N + 2) * W); size(560, 280); frameRate(8); background(0); stroke(255); noFill(); for (int i = 0; i < N; i++) for (int j = 0; j < N; j++) { rect(AX + i * W, Y + j * W, W, W); rect(BX + i * W, Y + j * W, W, W); } } // Draw a rectangle for the operation of the form b[i][j] += a[i][k] * a[k][j]; void drawRect(int i, int j, int k) { rect(AX + k * W, Y + i * W, W, W); rect(AX + j * W, Y + k * W, W, W); rect(BX + j * W, Y + i * W, W, W); } // Called for every operation void op(int i, int j, int k) { if (tryFrame == thisFrame) { // Draw current fill(247, 255, 19); drawRect(i, j, k); } if (tryFrame == thisFrame - 1) { // Erase previous fill(0); drawRect(i, j, k); } tryFrame++; } void draw() { tryFrame = 0; if (tile) { // Loop with tiling // assert (N % TILE) == 0; for (int i = 0; i < N; i += TILE) for (int j = 0; j < N; j += TILE) for (int k = 0; k < N; k += TILE) for (int ii = i; ii < i + TILE; ii++) for (int jj = j; jj < j + TILE; jj++) for (int kk = k; kk < k + TILE; kk++) op(ii, jj, kk); // b[i][j] += a[i][k] * a[k][j]; } else // Normal loop for (int i = 0; i < N; i++) for (int j = 0; j < N; j++) for (int k = 0; k < N; k++) op(i, j, k); // b[i][j] += a[i][k] * a[k][j]; if (thisFrame++ == N * N * N + 1) thisFrame = 0; }