{ "metadata": { "name": "" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Matrix-matrix multiplication by rows" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We continue to look at how the FLAMEPy API can be used to implement different matrix-matrix multiplication algorithms. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "First, we create some matrices." ] }, { "cell_type": "code", "collapsed": false, "input": [ "import numpy as np\n", "\n", "m = 4\n", "n = 3\n", "k = 5\n", "\n", "C = np.matrix( np.random.random( (m, n) ) )\n", "print( 'C = ' )\n", "print( C )\n", "\n", "Cold = np.matrix( np.zeros( (m,n ) ) )\n", "Cold = np.matrix( np.copy( C ) ) # an alternative way of doing a \"hard\" copy, in this case of a matrix\n", " \n", "A = np.matrix( np.random.random( (m, k) ) )\n", "print( 'A = ' )\n", "print( A )\n", "\n", "B = np.matrix( np.random.random( (k, n) ) )\n", "print( 'B = ' )\n", "print( B )" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "

The algorithm

\n", "\n", "\"Matrix-matrix\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

The routine Gemm_nn_unb_var2( A, B, C )

\n", "\n", "This routine computes $ C := A B + C $ by rows. The \"\\_nn\\_\" means that this is the \"No transpose, No transpose\" case of matrix multiplication. \n", "The reason for this is that the operations $ C := A^T B + C $ (\"\\_tn\\_\" or \"Transpose, No transpose\"), $ C := A B^T + C $ (\"\\_nt\\_\" or \"No transpose, Transpose\"), and $ C := A^T B^T + C $ (\"\\_tt\\_\" or \"Transpose, Transpose\") are also encountered. \n", " \n", "The specific laff function we will use is\n", "\n", "\n", "Use the Spark webpage to generate a code skeleton. (Make sure you adjust the name of the routine.)" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Your code here..." ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "C = np.matrix( np.copy( Cold ) ) # restore C \n", "\n", "Gemm_nn_unb_var2( A, B, C )\n", "\n", "print( 'C - ( Cold + A * B )' )\n", "print( C - ( Cold + A * B ) )" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Bingo! It works!" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Watch the algorithm at work!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Copy and paste the code into PictureFLAME , a webpage where you can watch your routine in action. Just cut and paste into the box. \n", "\n", "Disclaimer: we implemented a VERY simple interpreter. If you do something wrong, we cannot guarantee the results. But if you do it right, you are in for a treat.\n", "\n", "If you want to reset the problem, just click in the box into which you pasted the code and hit \"next\" again." ] } ], "metadata": {} } ] }