/* mkMatrix.c  written march 15th  by ian a. mason @ une        */
/* modifed for torus multiplication may 25                      */
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include "mmlib.h"

#define PERMS 0666

int main(int argc, char *argv[]){
  int fd[3]; 
  float *a, *b, *c;
  int matrix_size, i;
  int length;
  if((argc != 5) || 
     ((fd[0] = creat(argv[1], PERMS)) == -1) ||
     ((fd[1] = creat(argv[2], PERMS)) == -1) ||
     ((fd[2] = creat(argv[3], PERMS)) == -1) ||
     ((matrix_size = atoi(argv[4])) <= 0)){
    fprintf (stderr, "Usage: %s matrixA matrixB matrixC dimension\n", argv[0]);
    exit(1); }
  length =  matrix_size * matrix_size;
  a = (float*)calloc(length, sizeof(float));
  b = (float*)calloc(length, sizeof(float));
  c = (float*)calloc(length, sizeof(float));
  if ((a == NULL) ||
      (b == NULL) ||
      (c == NULL)) {
    fprintf(stderr, 
            "%s: out of memory!\n", 
            argv[0]);
    free(a); 
    free(b); 
    free(c); }
  init_block(a, b, c, matrix_size, 0, 0);
  if(set_block_row(fd[0], matrix_size, 0, 0, 0, length, a) == -1)
    perror("write of matrix A");
  if(set_block_row(fd[1], matrix_size, 0, 0, 0, length, b) == -1)
    perror("write of matrix B");
  if(set_block_row(fd[2], matrix_size, 0, 0, 0, length, c) == -1)
    perror("write of matrix C");
  for(i = 0; i < 3; i++) close(fd[i]);
  printf("Done writing files\n");
  return 0;
}