82#if (PETSC_VERSION_MAJOR == 3 && PETSC_VERSION_MINOR >= 7)
83 PetscViewerPushFormat(PETSC_VIEWER_STDOUT_WORLD, PETSC_VIEWER_ASCII_MATLAB);
85 PetscViewerSetFormat(PETSC_VIEWER_STDOUT_WORLD, PETSC_VIEWER_ASCII_MATLAB);
87 MatView(matrix,PETSC_VIEWER_STDOUT_WORLD);
88#if (PETSC_VERSION_MAJOR == 3 && PETSC_VERSION_MINOR >= 7)
89 PetscViewerPopFormat(PETSC_VIEWER_STDOUT_WORLD);
122#if (PETSC_VERSION_MAJOR == 3 && PETSC_VERSION_MINOR >= 1)
123 MatSetOption(matrix, MAT_KEEP_NONZERO_PATTERN, PETSC_TRUE);
124#elif (PETSC_VERSION_MAJOR == 3 && PETSC_VERSION_MINOR == 0)
125 MatSetOption(matrix, MAT_KEEP_ZEROED_ROWS, PETSC_TRUE);
127 MatSetOption(matrix, MAT_KEEP_ZEROED_ROWS);
131 for (
unsigned i=0; i<rRows.size(); i++)
135#if (PETSC_VERSION_MAJOR == 2 && PETSC_VERSION_MINOR == 2)
137 ISCreateGeneral(PETSC_COMM_WORLD, rRows.size(), rows, &is);
138 MatZeroRows(matrix, is, &diagonalValue);
156 for (unsigned index=0; index<rRows.size(); index++)
158 PetscInt row = rRows[index];
159 if (row >= lo && row < hi)
161 std::vector<unsigned> non_zero_cols;
162 // This row is local, so zero it.
163 for (PetscInt column = 0; column < size; column++)
165 if (GetElement(matrix, row, column) != 0.0)
167 non_zero_cols.push_back(column);
170 // Separate "gets" from "sets" so that we don't have to keep going into "assembled" mode
171 for (unsigned i=0; i<non_zero_cols.size();i++)
173 SetElement(matrix, row, non_zero_cols[i], 0.0);
176 SetElement(matrix, row, row, diagonalValue);
178 // Everyone communicate after row is finished
179 AssembleFinal(matrix);
182#elif (PETSC_VERSION_MAJOR == 3 && PETSC_VERSION_MINOR >= 2)
183 MatZeroRows(matrix, rRows.size(), rows, diagonalValue ,
nullptr,
nullptr);
185 MatZeroRows(matrix, rRows.size(), rows, diagonalValue);
196 std::sort(rowColIndices.begin(), rowColIndices.end());
200 unsigned size = hi-lo;
202 std::vector<unsigned>* cols_to_zero_per_row =
new std::vector<unsigned>[size];
206 for (
PetscInt row = lo; row < hi; row++)
216 if (std::binary_search(rowColIndices.begin(), rowColIndices.end(), cols[i]))
218 cols_to_zero_per_row[row-lo].push_back(cols[i]);
227 for (
PetscInt row = lo; row < hi; row++)
229 unsigned num_cols_to_zero_this_row = cols_to_zero_per_row[row-lo].size();
231 if (num_cols_to_zero_this_row>0)
234 double* zeros =
new double[num_cols_to_zero_this_row];
235 for (
unsigned i=0; i<num_cols_to_zero_this_row; i++)
237 cols_to_zero[i] = cols_to_zero_per_row[row-lo][i];
243 MatSetValues(matrix, 1, rows, num_cols_to_zero_this_row, cols_to_zero, zeros, INSERT_VALUES);
244 delete [] cols_to_zero;
249 delete [] cols_to_zero_per_row;
263 std::vector<unsigned> nonzero_rows;
264 for (
PetscInt row = lo; row < hi; row++)
268 nonzero_rows.push_back(row);
273 unsigned size = nonzero_rows.size();
276 double* zeros =
new double[size];
280 for (
unsigned i=0; i<size; i++)
282 rows[i] = nonzero_rows[i];
286 MatSetValues(matrix, size, rows, 1, cols, zeros, INSERT_VALUES);
352 const PetscScalar* values;
362 MatGetRow(matrix, rowIndex, &num_entries, &column_indices, &values);
363 VecSetValues(mat_ith_row, num_entries, column_indices, values, INSERT_VALUES);
366 VecAssemblyBegin(mat_ith_row);
367 VecAssemblyEnd(mat_ith_row);
371 MatRestoreRow(matrix, rowIndex, &num_entries, &column_indices, &values);
381 MatDuplicate(mat2, MAT_COPY_VALUES, &y);
383 double minus_one = -1.0;
384#if (PETSC_VERSION_MAJOR == 2 && PETSC_VERSION_MINOR == 2)
386 MatAYPX(&minus_one, mat1, y);
387#elif (PETSC_VERSION_MAJOR == 2 && PETSC_VERSION_MINOR == 3 && PETSC_VERSION_SUBMINOR == 1)
389 MatAYPX(y, minus_one, mat1);
392 MatAYPX(y, minus_one, mat1, DIFFERENT_NONZERO_PATTERN);
395 MatNorm(y, NORM_INFINITY, &norm);