void UMFPackMatrix::alloc() { _F_ assert(pages != NULL); assert(size > 0); // initialize the arrays Ap and Ai Ap = new int [size + 1]; MEM_CHECK(Ap); int aisize = get_num_indices(); Ai = new int [aisize]; MEM_CHECK(Ai); // sort the indices and remove duplicities, insert into Ai int i, pos = 0; for (i = 0; i < size; i++) { Ap[i] = pos; pos += sort_and_store_indices(pages[i], Ai + pos, Ai + aisize); } Ap[i] = pos; delete [] pages; pages = NULL; nnz = Ap[size]; Ax = new scalar [nnz]; MEM_CHECK(Ax); memset(Ax, 0, sizeof(scalar) * nnz); }
void PetscMatrix::alloc() { _F_ #ifdef WITH_PETSC assert(pages != NULL); // calc nnz int *nnz_array = new int[size]; MEM_CHECK(nnz_array); // fill in nnz_array int aisize = get_num_indices(); int *ai = new int[aisize]; MEM_CHECK(ai); // sort the indices and remove duplicities, insert into ai int pos = 0; for (unsigned int i = 0; i < size; i++) { nnz_array[i] = sort_and_store_indices(pages[i], ai + pos, ai + aisize); pos += nnz_array[i]; } // stote the number of nonzeros nnz = pos; delete [] pages; pages = NULL; delete [] ai; // MatCreateSeqAIJ(PETSC_COMM_SELF, size, size, 0, nnz_array, &matrix); // MatSetOption(matrix, MAT_ROW_ORIENTED); // MatSetOption(matrix, MAT_ROWS_SORTED); delete [] nnz_array; inited = true; #endif }
void CSCMatrix::alloc() { _F_ assert(pages != NULL); if (size <= 0) error("UMFPack failed, matrix size must be greater than 0."); // initialize the arrays Ap and Ai Ap = new int [size + 1]; MEM_CHECK(Ap); int aisize = get_num_indices(); Ai = new int [aisize]; MEM_CHECK(Ai); // sort the indices and remove duplicities, insert into Ai unsigned int i; int pos = 0; for (i = 0; i < size; i++) { Ap[i] = pos; pos += sort_and_store_indices(pages[i], Ai + pos, Ai + aisize); } Ap[i] = pos; delete [] pages; pages = NULL; nnz = Ap[size]; Ax = new scalar [nnz]; MEM_CHECK(Ax); memset(Ax, 0, sizeof(scalar) * nnz); }
void DiscreteProblem::create_matrix() { // remove any previous matrix free_matrix_indices(); free_matrix_values(); // calculate the total number of DOFs ndofs = 0; for (int i = 0; i < neq; i++) ndofs += spaces[i]->get_num_dofs(); if (!quiet) verbose("Ndofs: %d", ndofs); if (!ndofs) return; // get row and column indices of nonzero matrix elements Page** pages = new Page*[ndofs]; memset(pages, 0, sizeof(Page*) * ndofs); if (!quiet) { verbose("Calculating matrix sparse structure..."); begin_time(); } precalculate_sparse_structure(pages); // initialize the arrays Ap and Ai Ap = (int*) malloc(sizeof(int) * (ndofs+1)); int aisize = get_num_indices(pages, ndofs); Ai = (int*) malloc(sizeof(int) * aisize); if (Ai == NULL) error("Out of memory. Could not allocate the array Ai."); // sort the indices and remove duplicities, insert into Ai int i, pos = 0, num; for (i = 0; i < ndofs; i++) { Ap[i] = pos; pos += sort_and_store_indices(pages[i], Ai + pos, Ai + aisize); } Ap[i] = pos; if (!quiet) verbose(" Nonzeros: %d\n Total matrix size: %0.1lf MB\n (time: %g sec)", pos, (double) get_matrix_size() / (1024*1024), end_time()); delete [] pages; // shrink Ai to the actual size int* oldAi = Ai; Ai = (int*) realloc(Ai, sizeof(int) * pos); if (oldAi != Ai) warn("Realloc moved Ai when shrinking."); // this should not happen // UMFPACK: perform symbolic analysis of the matrix if (!quiet) { verbose("Performing UMFPACK symbolic analysis..."); begin_time(); } int status = umfpack_symbolic(ndofs, ndofs, Ap, Ai, NULL, &Symbolic, NULL, NULL); if (status != UMFPACK_OK) umfpack_status(status); if (!quiet) verbose(" (time: %g sec)", end_time()); equi = (double*) malloc(sizeof(double) * ndofs); if (equi == NULL) error("Out of memory. Error allocating the equilibration vector."); for (int i = 0; i < ndofs; i++) equi[i] = 1.0; is_equi = false; }