1/** 2 * \file 3 * \brief Computes a matrix multiplication. Tries to do that in a cache-aware way. 4 */ 5 6/* 7 * Copyright (c) 2007, 2008, 2009, ETH Zurich. 8 * All rights reserved. 9 * 10 * This file is distributed under the terms in the attached LICENSE file. 11 * If you do not find this file, copies can be found by writing to: 12 * ETH Zurich D-INFK, Haldeneggsteig 4, CH-8092 Zurich. Attn: Systems Group. 13 */ 14 15#include <barrelfish/barrelfish.h> 16#include <stdio.h> 17#include <stdlib.h> 18#include <skb/skb.h> 19 20 21int A[5][5] = {{1,2,3,4}, 22 {5,6,7,8}, 23 {9,10,11,12}, 24 {13,14,15,16}}; 25 26int B[7][9] = {{1,2,3,4}, 27 {1,2,3,4}, 28 {1,2,3,4}, 29 {1,2,3,4}}; 30 31int C[4][4]; 32 33int A2[5][2] = {{1,2}, //matrix size 4x2 34 {3,4}, 35 {5,6}, 36 {7,8}}; 37int B2[3][4] = {{1,2,3,4}, //matrix size 2x4 38 {1,2,3,4}}; 39int C2[9][9]; //the matrix size will be 4x4 40 41 42 43int A3[16 * 512][4]; 44int B3[4][16 * 512]; 45int C3[16 * 512][16 * 512]; 46 47 48int A4[16 * 512][4]; 49int B4[4][16 * 512 + 16]; 50int C4[16 * 512][16 * 512]; 51 52 53//r = #rows in A, c = #columns in A (the real rows and columns) 54//ar, ac && br, bc && cr, cc are the sizes of the respective arrays 55static inline void matrix_mul(int r, int c, 56 int ar, int ac, 57 int A[ar][ac], 58 int br, int bc, 59 int B[br][bc], 60 int cr, int cc, 61 int C[cr][cc]) 62{ 63 for (int i = 0; i < r; i++) { 64 for (int j = 0; j < r; j++) { 65 register int tmp = 0; 66 for (int k = 0; k < c; k++) { 67 tmp = tmp + A[i][k] * B[k][j]; 68 } 69 C[i][j] = tmp; 70 } 71 } 72} 73 74static inline void matrix_print(int r, int c, int mr, int mc, int M[mr][mc]) 75{ 76 for (int i = 0; i < r; i++) { 77 printf("\n"); 78 for (int j = 0; j < c; j++) { 79 printf("%02d ", M[i][j]); 80 } 81 } 82} 83 84 85 86int main(int argc, char **argv) 87{ 88 printf("matrix mul: connecting to the SKB...\n"); 89 skb_client_connect(); 90 printf("matrix mul: connected.\n"); 91 92 skb_create_buffer(); 93 94 matrix_print(4, 4, 5, 5, A); 95 matrix_print(4, 4, 7, 9, B); 96 uint64_t start = rdtsc(); //rdtscp(); 97 matrix_mul(4, 4, 5, 5 , A, 7, 9, B, 4, 4, C); 98 uint64_t end = rdtsc(); //rdtscp(); 99 matrix_print(4, 4, 4, 4, C); 100 printf("\n%lu cycles for multiplying one matrix\n", end - start); 101 102 103 matrix_print(4, 2, 5, 2, A2); 104 matrix_print(2, 4, 3, 4, B2); 105 start = rdtsc(); //rdtscp(); 106 matrix_mul(4, 2, 5, 2 , A2, 3, 4, B2, 9, 9, C2); 107 end = rdtsc(); //rdtscp(); 108 matrix_print(4, 4, 9, 9, C2); 109 printf("\n%lu cycles for multiplying one matrix\n", end - start); 110 111 112 start = rdtsc(); //rdtscp(); 113 matrix_mul(16 * 512, 4, 16 * 512, 4 , A3, 4, 16 * 512, B3, 16 * 512, 16 * 512, C3); 114 end = rdtsc(); //rdtscp(); 115 printf("\n%lu cycles for multiplying one matrix\n", end - start); 116 117 118 start = rdtsc(); //rdtscp(); 119 matrix_mul(16 * 512, 4, 16 * 512, 4 , A4, 4, 16 * 512 + 16, B4, 120 16 * 512, 16 * 512, C4); 121 end = rdtsc(); //rdtscp(); 122 printf("\n%lu cycles for multiplying one matrix\n", end - start); 123 124 125 return 0; 126} 127