# matrix multiplication using co and fine-grained concurrency
# usage:  a.out size

resource matrix_mult()
  # read command line argument for matrix sizes
  int n; getarg(1,n);

  # declare matrices and initialize a and b
  real a[n,n] = ([n] ([n] 1.0)),
       b[n,n] = ([n] ([n] 1.0)),
       c[n,n];

  # compute inner product of a[i,*] * b[*,j]
  procedure inner(int i, int j) {
    real sum = 0.0;
    for [k = 1 to n]
      { sum += a[i,k] * b[k,j] }
    c[i,j] = sum;
  }

  # compute n**2 inner products concurrently
  co [i = 1 to n, j = 1 to n] inner(i,j) oc

  # print result, by rows
  for [i = 1 to n] {
    for [j = 1 to n] { writes(c[i,j], " "); }  # one line per row
    write();   # append a newline
  }

end
