コード例 #1
0
int readFile(char* fileName, int dimension, int** matrixA, int** matrixB){
  // reading in data from the inputFile 
  char ch;
  int ch1;
  char* line = NULL;
  size_t len = 0;
  ssize_t read;
  FILE *fp = fopen(fileName, "r");
  int d = dimension; 

  if(fp == NULL ){
      perror("Error while opening the file.\n");
      exit(EXIT_FAILURE);
   }

    int holdingArray[(2*d*d) + 1];
    int counter = 0;

    while((read = getline(&line, &len, fp)) != -1){
      ch1 = atoi(line);
      holdingArray[counter] = ch1; 
      counter++;
    }

	// fill matrix A
    int countvar = 0;
    for (int i = 0; i < d; i++){
      for (int j = 0; j < d; j++){
        matrixA[i][j] = holdingArray[countvar];
        countvar++;
       }
    }

	// fill matrix B
    for (int i = 0; i < d; i++){
      for (int j = 0; j < d; j++){
        matrixB[i][j] = holdingArray[countvar];
        countvar++;
       }
    }
    fclose(fp);
    printf("Matrix A: \n");
    printMatrix(matrixA, dimension);
    printf("Matrix B: \n");
    printMatrix(matrixB, dimension);

    sumMatrices(matrixA, matrixB, dimension);
    subMatrices(matrixB, matrixA, dimension);
    // int** aTest = allocateQuadrant(matrixA, dimension, 0);
    // int** bTest = allocateQuadrant(matrixA, dimension, 1);
    // int** cTest = allocateQuadrant(matrixA, dimension, 2);
    // int** dTest = allocateQuadrant(matrixA, dimension, 3);
    // reglue(aTest,bTest,cTest,dTest,dimension);
    return 0; 
}
コード例 #2
0
void strassenMultMatrix(double **a,double **b,double **c,int size){
//Performs a Strassen matrix multiply operation
//This does miracles, and is recursive
//To perform a miracle, it first performs a miracle
  double **a11, **a22, **a12, **a21;
  double **b11, **b22, **b12, **b21;
  double **m1, **m2, **m3, **m4, **m5, **m6, **m7; 
  double **t1, **t2, **t3, **t4, **t5, **t6, **t7, **t8, **t9, **t10;
  int newsize = (int)size/2;
  int i;
  if (size > threshold) {
    //Allocate memory....this could get expensive pretty quickly
    a11 = (double**) malloc(sizeof(double)*newsize);
    a12 = (double**) malloc(sizeof(double)*newsize);
    a21 = (double**) malloc(sizeof(double)*newsize);
    a22 = (double**) malloc(sizeof(double)*newsize);
    b11 = (double**) malloc(sizeof(double)*newsize);
    b12 = (double**) malloc(sizeof(double)*newsize);
    b21 = (double**) malloc(sizeof(double)*newsize);
    b22 = (double**) malloc(sizeof(double)*newsize);
    m1 = (double**) malloc(sizeof(double)*newsize);
    m2 = (double**) malloc(sizeof(double)*newsize);
    m3 = (double**) malloc(sizeof(double)*newsize);
    m4 = (double**) malloc(sizeof(double)*newsize);
    m5 = (double**) malloc(sizeof(double)*newsize);
    m6 = (double**) malloc(sizeof(double)*newsize);
    m7 = (double**) malloc(sizeof(double)*newsize);
    t1 = (double**) malloc(sizeof(double)*newsize);
    t2 = (double**) malloc(sizeof(double)*newsize);
    t3 = (double**) malloc(sizeof(double)*newsize);
    t4 = (double**) malloc(sizeof(double)*newsize);
    t5 = (double**) malloc(sizeof(double)*newsize);
    t6 = (double**) malloc(sizeof(double)*newsize);
    t7 = (double**) malloc(sizeof(double)*newsize);
    t8 = (double**) malloc(sizeof(double)*newsize);
    t9 = (double**) malloc(sizeof(double)*newsize);
    t10 = (double**) malloc(sizeof(double)*newsize);
    
    for (i=0; i < newsize; i++){
      a11[i] = (double*) malloc(sizeof(double)*newsize);
      a12[i] = (double*) malloc(sizeof(double)*newsize);
      a21[i] = (double*) malloc(sizeof(double)*newsize);
      a22[i] = (double*) malloc(sizeof(double)*newsize);
      b11[i] = (double*) malloc(sizeof(double)*newsize); 
      b12[i] = (double*) malloc(sizeof(double)*newsize); 
      b21[i] = (double*) malloc(sizeof(double)*newsize);
      b22[i] = (double*) malloc(sizeof(double)*newsize);    
      m1[i] = (double*) malloc(sizeof(double)*newsize);
      m2[i] = (double*) malloc(sizeof(double)*newsize);
      m3[i] = (double*) malloc(sizeof(double)*newsize);
      m4[i] = (double*) malloc(sizeof(double)*newsize);
      m5[i] = (double*) malloc(sizeof(double)*newsize);
      m6[i] = (double*) malloc(sizeof(double)*newsize);
      m7[i] = (double*) malloc(sizeof(double)*newsize);
      t1[i] = (double*) malloc(sizeof(double)*newsize);
      t2[i] = (double*) malloc(sizeof(double)*newsize);
      t3[i] = (double*) malloc(sizeof(double)*newsize);
      t4[i] = (double*) malloc(sizeof(double)*newsize);
      t5[i] = (double*) malloc(sizeof(double)*newsize);
      t6[i] = (double*) malloc(sizeof(double)*newsize);
      t7[i] = (double*) malloc(sizeof(double)*newsize);
      t8[i] = (double*) malloc(sizeof(double)*newsize);
      t9[i] = (double*) malloc(sizeof(double)*newsize);
      t10[i] = (double*) malloc(sizeof(double)*newsize);
    }

    splitMatrix(a,a11,a12,a21,a22,size);
    splitMatrix(b,b11,b12,b21,b22,size);
    
    addMatrices(a11,a22,t1,newsize);
    addMatrices(a21,a22,t2,newsize);
    addMatrices(a11,a12,t3,newsize);
    subMatrices(a21,a11,t4,newsize);
    subMatrices(a12,a22,t5,newsize);
    addMatrices(b11,b22,t6,newsize);
    subMatrices(b12,b22,t7,newsize);
    subMatrices(b21,b11,t8,newsize);
    addMatrices(b11,b12,t9,newsize);
    addMatrices(b21,b22,t10,newsize);
    
    strassenMultMatrix(t1,t6,m1,newsize);
    strassenMultMatrix(t2,b11,m2,newsize);
    strassenMultMatrix(a11,t7,m3,newsize);
    strassenMultMatrix(a22,t8,m4,newsize);
    strassenMultMatrix(t3,b22,m5,newsize);
    strassenMultMatrix(t4,t9,m6,newsize);
    strassenMultMatrix(t5,t10,m7,newsize);
    
    addMatrices(m1,m4,a11,newsize);
    subMatrices(m5,m7,a12,newsize);
    addMatrices(m3,m1,a21,newsize);
    subMatrices(m2,m6,a22,newsize);
    subMatrices(a11,a12,b11,newsize);
    addMatrices(m3,m5,b12,newsize);
    addMatrices(m2,m4,b21,newsize);
    subMatrices(a21,a22,b22,newsize);
    
    catMatrix(c,b11,b12,b21,b22,size);
    free(a11);free(a12);free(a21);free(a22);
    free(b11);free(b12);free(b21);free(b22);
    free(t1);free(t2);free(t3);free(t4);free(t5);free(t6);free(t7);free(t8);free(t9);free(t10);
    free(m1);free(m2);free(m3);free(m4);free(m5);free(m6);free(m7);
  }
  else {
    normalMultMatrix(b,a,c,size);
  }
}