Subject: 4x4 Matrix Multiply

From: Jonas Schlein <[email-masked]>
Date: Mon, 4 Dec 1995 12:08:55 -0500 (EST)

#include <assert.h>
#include <stdio.h>
#include <string.h>
#include <task.h>
#include <ulocks.h>

void matmul (int x[4][4], int y[4][4], int r[4][4]);
void matprn (int r[4][4]);

int main (void)
{
  int x[4][4] = {{13,15,16, 3},
                 { 9, 1, 7, 4},
                 { 2,14,11, 8},
                 {10, 6,12, 5}};
  int y[4][4] = {{13, 8, 7,11},
                 { 6, 5, 1, 4},
                 {12, 2, 9,10},
                 {15,16,14, 3}};
  int r[4][4];

  /* Set us to 4*4 processes */
  assert (m_set_procs (16) == 0);

  /* Do the multiply */
  assert (m_fork (matmul, x, y, r) == 0);
  assert (m_kill_procs ()          == 0);

  /* Print out the result */
  matprn (r);

  return (0);
}

void matmul (int x[4][4], int y[4][4], int r[4][4])
{
  int i = m_get_myid() % 4,
      j = m_get_myid() / 4,
      k;

  r[i][j] = 0;
  for (k = 0; k < 4; k++)
     r[i][j] += x[i][k] * y[k][j];
}

void matprn (int r[4][4])
{
  int i, j;

  for (i = 0; i < 4; i++)
     {
     for (j = 0; j < 4; j++)
        printf ("%3d ", r[i][j]);
     printf ("\n");
     }
}