MatDirBlk *mat_rdirblk(MatrixFile *file, int blknum) { MatDirBlk *matdirblk; int i, j, err, ndirs; int dirbufr[MatBLKSIZE / 4]; FILE *fptr = file->fptr; matrix_errno = MAT_OK; matrix_errtxt[0] = '\0'; matdirblk = (MatDirBlk *) malloc(MatBLKSIZE); if (matdirblk == NULL) return (NULL); err = read_matrix_data(fptr, blknum, 1, (char *) dirbufr, SunLong); if (err == ERROR) { free(matdirblk); return (NULL); } matdirblk->nfree = dirbufr[0]; matdirblk->nextblk = dirbufr[1]; matdirblk->prvblk = dirbufr[2]; matdirblk->nused = dirbufr[3]; if (matdirblk->nused > 31) { matrix_errno = MAT_INVALID_DIRBLK; free(matdirblk); return (NULL); } ndirs = (MatBLKSIZE / 4 - 4) / 4; for (i = 0; i < ndirs; i++) { matdirblk->matdir[i].matnum = 0; matdirblk->matdir[i].strtblk = 0; matdirblk->matdir[i].endblk = 0; matdirblk->matdir[i].matstat = 0; } for (i = 0; i < matdirblk->nused; i++) { j = i + 1; matdirblk->matdir[i].matnum = dirbufr[j * 4 + 0]; matdirblk->matdir[i].strtblk = dirbufr[j * 4 + 1]; matdirblk->matdir[i].endblk = dirbufr[j * 4 + 2]; matdirblk->matdir[i].matstat = dirbufr[j * 4 + 3]; } return (matdirblk); }
/** * Reads two matrices from the file and: * - performs qr decomposition on the first matrix U * - prints q and r to the console * - performs qr iterations on the second matrix A * - prints its eigen values to the console * * Matrix input format for both matrices is: * The first line has m and n indicating number of rows and columns. * Each of the next m lines contains n floats indicating the rows of the matrix. * * Sample input: * 3 3 * 1.0 1.0 0.0 * 1.0 2.0 1.0 * -2.0 -3.0 1.0 * 2 2 * 7.0 2.0 * 2.0 4.0 */ int main(int argc, char **argv) { FILE* fp = NULL; matrix* u = NULL; matrix* q = NULL; matrix* r = NULL; matrix* a = NULL; matrix* a_k = NULL; size_t m, n; /* Read the input. */ fp = fopen("input.txt", "r"); if (fp == NULL) { perror("Failed to open input.txt"); exit(EXIT_FAILURE); } /* Read the dimensions of the matrix U. */ fscanf(fp, "%zu %zu", &m, &n); /* Alloate the matrices U Q and R. */ u = new_matrix(m, n); q = new_matrix(m, n); r = new_matrix(m, n); /* Read matrix data. */ read_matrix_data(fp, u); /* Perform qr decomposition. */ qr_decomposition(u, q, r); /* Print the matrices. */ printf("QR decomposition:\n"); printf("U:\n"); print_matrix_data(stdout, u); printf("Q:\n"); print_matrix_data(stdout, q); printf("R:\n"); print_matrix_data(stdout, r); /* Free memory occupied by q and r. */ destroy_matrix(u); destroy_matrix(q); destroy_matrix(r); /* Read the dimensions of the matrix A. */ fscanf(fp, "%zu %zu", &m, &n); /* Allocate a vector for storing eigen values and matrices A and A_k */ a = new_matrix(m, n); a_k = new_matrix(m, n); float eigen_values[m]; /* Read matix data. */ read_matrix_data(fp, a); /* Perform qr iterations. */ qr_iterations(a, a_k); /* Extract the eigen values. */ extract_diagonal(a_k, eigen_values, m); /* Print the matrix A and its eigen values. */ printf("\nQR iterations:\n"); printf("A:\n"); print_matrix_data(stdout, a); printf("Eigen values:\n"); print_vector(stdout, eigen_values, m); /* free memory occupied by A and A_k. */ destroy_matrix(a); destroy_matrix(a_k); /* Close the input file. */ fclose(fp); return 0; }
int mat_enter(FILE *fptr, Main_header *mhptr, int matnum, int nblks) { int dirblk, dirbufr[128], i, nxtblk, busy, oldsize; matrix_errno = MAT_OK; matrix_errtxt[0] = '\0'; dirblk = MatFirstDirBlk; if( fseek(fptr, 0, 0) ) return( ERROR ); /* * nfs locks are very time consuming lockf( fileno(fptr), F_LOCK, 0); */ if (read_matrix_data(fptr, dirblk, 1, (char *) dirbufr, SunLong) == ERROR) return (ERROR); busy = 1; while (busy) { nxtblk = dirblk + 1; for (i = 4; i < 128; i += 4) { if (dirbufr[i] == 0) { busy = 0; break; } else if (dirbufr[i] == matnum) { oldsize = dirbufr[i + 2] - dirbufr[i + 1] + 1; if (oldsize < nblks) { dirbufr[i] = 0xFFFFFFFF; write_matrix_data(fptr, dirblk, 1, (char*)dirbufr, SunLong); nxtblk = dirbufr[i + 2] + 1; } else { nxtblk = dirbufr[i + 1]; dirbufr[0]++; dirbufr[3]--; busy = 0; break; } } else { nxtblk = dirbufr[i + 2] + 1; } } if (!busy) break; if (dirbufr[1] != MatFirstDirBlk) { dirblk = dirbufr[1]; if (read_matrix_data(fptr, dirblk, 1, (char *) dirbufr, SunLong) == ERROR) return (ERROR); } else { dirbufr[1] = nxtblk; if (write_matrix_data(fptr, dirblk, 1, (char *) dirbufr, SunLong) == ERROR) return (ERROR); dirbufr[0] = 31; dirbufr[1] = MatFirstDirBlk; dirbufr[2] = dirblk; dirbufr[3] = 0; dirblk = nxtblk; for (i = 4; i < 128; i++) dirbufr[i] = 0; } } dirbufr[i] = matnum; dirbufr[i + 1] = nxtblk; dirbufr[i + 2] = nxtblk + nblks; dirbufr[i + 3] = 1; dirbufr[0]--; dirbufr[3]++; if (write_matrix_data(fptr, dirblk, 1, (char*)dirbufr, SunLong) == ERROR) return (ERROR); if( fseek(fptr, 0, 0) ) return( ERROR ); /* * nfs locks are very time consuming lockf( fileno(fptr), F_UNLOCK, 0); */ return (nxtblk); }