From: Daniel Arndt Date: Sat, 1 Dec 2018 11:10:38 +0000 (+0100) Subject: Avoid C-style cast in Trilinos-related files X-Git-Tag: v9.1.0-rc1~495^2~2 X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=e6da0f52163753f6dd518dfd266285f1c11edc1d;p=dealii.git Avoid C-style cast in Trilinos-related files --- diff --git a/include/deal.II/lac/trilinos_sparsity_pattern.h b/include/deal.II/lac/trilinos_sparsity_pattern.h index 76bf07e558..dbb039ad17 100644 --- a/include/deal.II/lac/trilinos_sparsity_pattern.h +++ b/include/deal.II/lac/trilinos_sparsity_pattern.h @@ -1504,12 +1504,17 @@ namespace TrilinosWrappers ExcNotImplemented()); TrilinosWrappers::types::int_type *col_index_ptr = - (TrilinosWrappers::types::int_type *)(&*begin); - const int n_cols = static_cast(end - begin); + reinterpret_cast( + const_cast::type *>(&*begin)); + // Check at least for the first index that the conversion actually works + AssertDimension(*col_index_ptr, *begin); + TrilinosWrappers::types::int_type trilinos_row_index = row; + const int n_cols = static_cast(end - begin); int ierr; if (row_is_stored_locally(row)) - ierr = graph->InsertGlobalIndices(row, n_cols, col_index_ptr); + ierr = + graph->InsertGlobalIndices(trilinos_row_index, n_cols, col_index_ptr); else if (nonlocal_graph.get() != nullptr) { // this is the case when we have explicitly set the off-processor rows @@ -1520,11 +1525,15 @@ namespace TrilinosWrappers ExcMessage("Attempted to write into off-processor matrix row " "that has not be specified as being writable upon " "initialization")); - ierr = nonlocal_graph->InsertGlobalIndices(row, n_cols, col_index_ptr); + ierr = nonlocal_graph->InsertGlobalIndices(trilinos_row_index, + n_cols, + col_index_ptr); } else - ierr = graph->InsertGlobalIndices( - 1, (TrilinosWrappers::types::int_type *)&row, n_cols, col_index_ptr); + ierr = graph->InsertGlobalIndices(1, + &trilinos_row_index, + n_cols, + col_index_ptr); AssertThrow(ierr >= 0, ExcTrilinosError(ierr)); } diff --git a/include/deal.II/lac/trilinos_vector.h b/include/deal.II/lac/trilinos_vector.h index 5443954e0a..140f4b7318 100644 --- a/include/deal.II/lac/trilinos_vector.h +++ b/include/deal.II/lac/trilinos_vector.h @@ -1613,7 +1613,8 @@ namespace TrilinosWrappers { const int ierr = vector->ReplaceGlobalValues( 1, - (const TrilinosWrappers::types::int_type *)(&row), + reinterpret_cast( + &row), &values[i]); AssertThrow(ierr == 0, ExcTrilinosError(ierr)); compressed = false; @@ -1688,7 +1689,8 @@ namespace TrilinosWrappers { const int ierr = vector->SumIntoGlobalValues( 1, - (const TrilinosWrappers::types::int_type *)(&row), + reinterpret_cast( + &row), &values[i]); AssertThrow(ierr == 0, ExcTrilinosError(ierr)); compressed = false; @@ -1717,11 +1719,9 @@ namespace TrilinosWrappers Vector::size() const { # ifndef DEAL_II_WITH_64BIT_INDICES - return (size_type)(vector->Map().MaxAllGID() + 1 - - vector->Map().MinAllGID()); + return vector->Map().MaxAllGID() + 1 - vector->Map().MinAllGID(); # else - return (size_type)(vector->Map().MaxAllGID64() + 1 - - vector->Map().MinAllGID64()); + return vector->Map().MaxAllGID64() + 1 - vector->Map().MinAllGID64(); # endif } @@ -1730,7 +1730,7 @@ namespace TrilinosWrappers inline Vector::size_type Vector::local_size() const { - return (size_type)vector->Map().NumMyElements(); + return vector->Map().NumMyElements(); } diff --git a/source/lac/trilinos_epetra_vector.cc b/source/lac/trilinos_epetra_vector.cc index 202fa08f68..52264d727d 100644 --- a/source/lac/trilinos_epetra_vector.cc +++ b/source/lac/trilinos_epetra_vector.cc @@ -575,7 +575,7 @@ namespace LinearAlgebra (unsigned int *)vector->Map().MyGlobalElements(); # else size_type *vector_indices = - (size_type *)vector->Map().MyGlobalElements64(); + reinterpret_cast(vector->Map().MyGlobalElements64()); # endif is.add_indices(vector_indices, vector_indices + n_indices); } diff --git a/source/lac/trilinos_precondition.cc b/source/lac/trilinos_precondition.cc index 2365635f1b..2068166215 100644 --- a/source/lac/trilinos_precondition.cc +++ b/source/lac/trilinos_precondition.cc @@ -121,7 +121,7 @@ namespace TrilinosWrappers 0)); Ifpack_Preconditioner *ifpack = - static_cast(preconditioner.get()); + dynamic_cast(preconditioner.get()); Assert(ifpack != nullptr, ExcMessage("Trilinos could not create this " "preconditioner")); @@ -173,7 +173,7 @@ namespace TrilinosWrappers additional_data.overlap)); Ifpack_Preconditioner *ifpack = - static_cast(preconditioner.get()); + dynamic_cast(preconditioner.get()); Assert(ifpack != nullptr, ExcMessage("Trilinos could not create this " "preconditioner")); @@ -226,7 +226,7 @@ namespace TrilinosWrappers additional_data.overlap)); Ifpack_Preconditioner *ifpack = - static_cast(preconditioner.get()); + dynamic_cast(preconditioner.get()); Assert(ifpack != nullptr, ExcMessage("Trilinos could not create this " "preconditioner")); @@ -288,7 +288,7 @@ namespace TrilinosWrappers 0)); Ifpack_Preconditioner *ifpack = - static_cast(preconditioner.get()); + dynamic_cast(preconditioner.get()); Assert(ifpack != nullptr, ExcMessage("Trilinos could not create this " "preconditioner")); @@ -356,7 +356,7 @@ namespace TrilinosWrappers additional_data.overlap)); Ifpack_Preconditioner *ifpack = - static_cast(preconditioner.get()); + dynamic_cast(preconditioner.get()); Assert(ifpack != nullptr, ExcMessage("Trilinos could not create this " "preconditioner")); @@ -425,7 +425,7 @@ namespace TrilinosWrappers additional_data.overlap)); Ifpack_Preconditioner *ifpack = - static_cast(preconditioner.get()); + dynamic_cast(preconditioner.get()); Assert(ifpack != nullptr, ExcMessage("Trilinos could not create this " "preconditioner")); @@ -484,7 +484,7 @@ namespace TrilinosWrappers additional_data.overlap)); Ifpack_Preconditioner *ifpack = - static_cast(preconditioner.get()); + dynamic_cast(preconditioner.get()); Assert(ifpack != nullptr, ExcMessage("Trilinos could not create this " "preconditioner")); @@ -492,7 +492,7 @@ namespace TrilinosWrappers int ierr; Teuchos::ParameterList parameter_list; - parameter_list.set("fact: level-of-fill", (int)additional_data.ic_fill); + parameter_list.set("fact: level-of-fill", additional_data.ic_fill); parameter_list.set("fact: absolute threshold", additional_data.ic_atol); parameter_list.set("fact: relative threshold", additional_data.ic_rtol); parameter_list.set("schwarz: combine mode", "Add"); @@ -534,7 +534,7 @@ namespace TrilinosWrappers additional_data.overlap)); Ifpack_Preconditioner *ifpack = - static_cast(preconditioner.get()); + dynamic_cast(preconditioner.get()); Assert(ifpack != nullptr, ExcMessage("Trilinos could not create this " "preconditioner")); @@ -587,7 +587,7 @@ namespace TrilinosWrappers additional_data.overlap)); Ifpack_Preconditioner *ifpack = - static_cast(preconditioner.get()); + dynamic_cast(preconditioner.get()); Assert(ifpack != nullptr, ExcMessage("Trilinos could not create this " "preconditioner")); @@ -596,7 +596,8 @@ namespace TrilinosWrappers Teuchos::ParameterList parameter_list; parameter_list.set("fact: drop value", additional_data.ilut_drop); - parameter_list.set("fact: level-of-fill", (int)additional_data.ilut_fill); + parameter_list.set("fact: level-of-fill", + static_cast(additional_data.ilut_fill)); parameter_list.set("fact: absolute threshold", additional_data.ilut_atol); parameter_list.set("fact: relative threshold", additional_data.ilut_rtol); parameter_list.set("schwarz: combine mode", "Add"); @@ -633,7 +634,7 @@ namespace TrilinosWrappers additional_data.overlap)); Ifpack_Preconditioner *ifpack = - static_cast(preconditioner.get()); + dynamic_cast(preconditioner.get()); Assert(ifpack != nullptr, ExcMessage("Trilinos could not create this " "preconditioner")); @@ -683,7 +684,7 @@ namespace TrilinosWrappers std::make_shared(&matrix.trilinos_matrix()); Ifpack_Chebyshev *ifpack = - static_cast(preconditioner.get()); + dynamic_cast(preconditioner.get()); Assert(ifpack != nullptr, ExcMessage("Trilinos could not create this " "preconditioner")); @@ -697,7 +698,8 @@ namespace TrilinosWrappers additional_data.min_eigenvalue); parameter_list.set("chebyshev: max eigenvalue", additional_data.max_eigenvalue); - parameter_list.set("chebyshev: degree", (int)additional_data.degree); + parameter_list.set("chebyshev: degree", + static_cast(additional_data.degree)); parameter_list.set("chebyshev: min diagonal value", additional_data.min_diagonal); parameter_list.set("chebyshev: zero starting solution", @@ -738,7 +740,7 @@ namespace TrilinosWrappers 0)); Ifpack_Preconditioner *ifpack = - static_cast(preconditioner.get()); + dynamic_cast(preconditioner.get()); Assert(ifpack != nullptr, ExcMessage("Trilinos could not create this " "preconditioner")); diff --git a/source/lac/trilinos_sparse_matrix.cc b/source/lac/trilinos_sparse_matrix.cc index 2f1c18e426..b9bb3bbed9 100644 --- a/source/lac/trilinos_sparse_matrix.cc +++ b/source/lac/trilinos_sparse_matrix.cc @@ -143,7 +143,7 @@ namespace TrilinosWrappers } int ierr = matrix->trilinos_matrix().ExtractGlobalRowCopy( - (TrilinosWrappers::types::int_type)this->a_row, + static_cast(this->a_row), colnums, ncols, &((*value_cache)[0]), @@ -205,8 +205,9 @@ namespace TrilinosWrappers : column_space_map(new Epetra_Map(input_map)) , matrix(new Epetra_FECrsMatrix(Copy, *column_space_map, - (int *)const_cast( - &(n_entries_per_row[0])), + reinterpret_cast( + const_cast( + &(n_entries_per_row[0]))), false)) , last_action(Zero) , compressed(false) @@ -235,8 +236,9 @@ namespace TrilinosWrappers : column_space_map(new Epetra_Map(input_col_map)) , matrix(new Epetra_FECrsMatrix(Copy, input_row_map, - (int *)const_cast( - &(n_entries_per_row[0])), + reinterpret_cast( + const_cast( + &(n_entries_per_row[0]))), false)) , last_action(Zero) , compressed(false) @@ -288,7 +290,8 @@ namespace TrilinosWrappers 0, Utilities::Trilinos::comm_self()), *column_space_map, - (int *)const_cast(&(n_entries_per_row[0])), + reinterpret_cast( + const_cast(&(n_entries_per_row[0]))), false)) , last_action(Zero) , compressed(false) @@ -318,8 +321,9 @@ namespace TrilinosWrappers parallel_partitioning.make_trilinos_map(communicator, false))) , matrix(new Epetra_FECrsMatrix(Copy, *column_space_map, - (int *)const_cast( - &(n_entries_per_row[0])), + reinterpret_cast( + const_cast( + &(n_entries_per_row[0]))), false)) , last_action(Zero) , compressed(false) @@ -353,7 +357,8 @@ namespace TrilinosWrappers , matrix(new Epetra_FECrsMatrix( Copy, row_parallel_partitioning.make_trilinos_map(communicator, false), - (int *)const_cast(&(n_entries_per_row[0])), + reinterpret_cast( + const_cast(&(n_entries_per_row[0]))), false)) , last_action(Zero) , compressed(false) @@ -1243,7 +1248,7 @@ namespace TrilinosWrappers int *diag_find = std::find(col_indices, col_indices + num_entries, local_row); - int diag_index = (int)(diag_find - col_indices); + int diag_index = static_cast(diag_find - col_indices); for (TrilinosWrappers::types::int_type j = 0; j < num_entries; ++j) if (diag_index != j || new_diag_value == 0) @@ -1325,7 +1330,7 @@ namespace TrilinosWrappers int *el_find = std::find(col_indices, col_indices + nnz_present, trilinos_j); - int local_col_index = (int)(el_find - col_indices); + int local_col_index = static_cast(el_find - col_indices); // This is actually the only // difference to the el(i,j) @@ -1401,7 +1406,7 @@ namespace TrilinosWrappers int *el_find = std::find(col_indices, col_indices + nnz_present, trilinos_j); - int local_col_index = (int)(el_find - col_indices); + int local_col_index = static_cast(el_find - col_indices); // This is actually the only @@ -1542,7 +1547,8 @@ namespace TrilinosWrappers // we will not modify const data) if (elide_zero_values == false) { - col_index_ptr = (TrilinosWrappers::types::int_type *)col_indices; + col_index_ptr = reinterpret_cast( + const_cast(col_indices)); col_value_ptr = const_cast(values); n_columns = n_cols; } @@ -1566,8 +1572,7 @@ namespace TrilinosWrappers } } - Assert(n_columns <= (TrilinosWrappers::types::int_type)n_cols, - ExcInternalError()); + AssertIndexRange(n_columns, n_cols + 1); } @@ -1585,7 +1590,7 @@ namespace TrilinosWrappers if (matrix->Filled() == false) { ierr = matrix->Epetra_CrsMatrix::InsertGlobalValues( - static_cast(row), + row, static_cast(n_columns), const_cast(col_value_ptr), col_index_ptr); @@ -1616,7 +1621,8 @@ namespace TrilinosWrappers { ierr = matrix->InsertGlobalValues( 1, - (TrilinosWrappers::types::int_type *)&row, + reinterpret_cast( + const_cast(&row)), n_columns, col_index_ptr, &col_value_ptr, @@ -1627,7 +1633,8 @@ namespace TrilinosWrappers else ierr = matrix->ReplaceGlobalValues( 1, - (TrilinosWrappers::types::int_type *)&row, + reinterpret_cast( + const_cast(&row)), n_columns, col_index_ptr, &col_value_ptr, @@ -1739,7 +1746,8 @@ namespace TrilinosWrappers // we will not modify const data) if (elide_zero_values == false) { - col_index_ptr = (TrilinosWrappers::types::int_type *)col_indices; + col_index_ptr = reinterpret_cast( + const_cast(col_indices)); col_value_ptr = const_cast(values); n_columns = n_cols; # ifdef DEBUG @@ -1768,8 +1776,7 @@ namespace TrilinosWrappers } } - Assert(n_columns <= (TrilinosWrappers::types::int_type)n_cols, - ExcInternalError()); + AssertIndexRange(n_columns, n_cols + 1); } // Exit early if there is nothing to do if (n_columns == 0) @@ -1816,13 +1823,14 @@ namespace TrilinosWrappers // a time). compressed = false; - ierr = - matrix->SumIntoGlobalValues(1, - (TrilinosWrappers::types::int_type *)&row, - n_columns, - col_index_ptr, - &col_value_ptr, - Epetra_FECrsMatrix::ROW_MAJOR); + ierr = matrix->SumIntoGlobalValues( + 1, + reinterpret_cast( + const_cast(&row)), + n_columns, + col_index_ptr, + &col_value_ptr, + Epetra_FECrsMatrix::ROW_MAJOR); AssertThrow(ierr == 0, ExcTrilinosError(ierr)); } diff --git a/source/lac/trilinos_sparsity_pattern.cc b/source/lac/trilinos_sparsity_pattern.cc index 2a2bdf71f1..8883e51945 100644 --- a/source/lac/trilinos_sparsity_pattern.cc +++ b/source/lac/trilinos_sparsity_pattern.cc @@ -56,10 +56,11 @@ namespace TrilinosWrappers // get a representation of the present row int ncols; const int ierr = sparsity_pattern->graph->ExtractGlobalRowCopy( - (TrilinosWrappers::types::int_type)this->a_row, + static_cast(this->a_row), colnum_cache->size(), ncols, - (TrilinosWrappers::types::int_type *)&(*colnum_cache)[0]); + reinterpret_cast( + const_cast(&(*colnum_cache)[0]))); AssertThrow(ierr == 0, ExcTrilinosError(ierr)); AssertThrow(static_cast::size_type>(ncols) == colnum_cache->size(), @@ -459,10 +460,10 @@ namespace TrilinosWrappers row_indices.data()); } - int ierr = graph->GlobalAssemble(*column_space_map, - static_cast( - graph->RangeMap()), - true); + // TODO A dynamic_cast fails here, this is suspicious. + const auto &range_map = + static_cast(graph->RangeMap()); + int ierr = graph->GlobalAssemble(*column_space_map, range_map, true); AssertThrow(ierr == 0, ExcTrilinosError(ierr)); ierr = graph->OptimizeStorage(); @@ -786,7 +787,7 @@ namespace TrilinosWrappers SparsityPattern::compress() { int ierr; - Assert(column_space_map.get() != nullptr, ExcInternalError()); + Assert(column_space_map.get(), ExcInternalError()); if (nonlocal_graph.get() != nullptr) { if (nonlocal_graph->IndicesAreGlobal() == false && @@ -801,24 +802,24 @@ namespace TrilinosWrappers Assert(nonlocal_graph->RowMap().NumMyElements() == 0 || nonlocal_graph->IndicesAreGlobal() == true, ExcInternalError()); - nonlocal_graph->FillComplete(*column_space_map, - static_cast( - graph->RangeMap())); + + // TODO A dynamic_cast fails here, this is suspicious. + const auto &range_map = + static_cast(graph->RangeMap()); + nonlocal_graph->FillComplete(*column_space_map, range_map); nonlocal_graph->OptimizeStorage(); Epetra_Export exporter(nonlocal_graph->RowMap(), graph->RowMap()); ierr = graph->Export(*nonlocal_graph, exporter, Add); AssertThrow(ierr == 0, ExcTrilinosError(ierr)); - ierr = graph->FillComplete(*column_space_map, - static_cast( - graph->RangeMap())); + ierr = graph->FillComplete(*column_space_map, range_map); AssertThrow(ierr == 0, ExcTrilinosError(ierr)); } else { - ierr = graph->GlobalAssemble(*column_space_map, - static_cast( - graph->RangeMap()), - true); + // TODO A dynamic_cast fails here, this is suspicious. + const auto &range_map = + static_cast(graph->RangeMap()); + ierr = graph->GlobalAssemble(*column_space_map, range_map, true); AssertThrow(ierr == 0, ExcTrilinosError(ierr)); } @@ -881,8 +882,9 @@ namespace TrilinosWrappers TrilinosWrappers::types::int_type *el_find = std::find(col_indices, col_indices + nnz_present, trilinos_j); - TrilinosWrappers::types::int_type local_col_index = - (TrilinosWrappers::types::int_type)(el_find - col_indices); + const auto &local_col_index = + static_cast(el_find - + col_indices); if (local_col_index == nnz_present) return false; @@ -912,7 +914,7 @@ namespace TrilinosWrappers col_indices + nnz_present, static_cast(trilinos_j)); - int local_col_index = (int)(el_find - col_indices); + const int local_col_index = static_cast(el_find - col_indices); if (local_col_index == nnz_present) return false; @@ -929,12 +931,13 @@ namespace TrilinosWrappers { size_type local_b = 0; TrilinosWrappers::types::int_type global_b = 0; - for (int i = 0; i < (int)local_size(); ++i) + for (int i = 0; i < static_cast(local_size()); ++i) { int *indices; int num_entries; graph->ExtractMyRowView(i, num_entries, indices); - for (unsigned int j = 0; j < (unsigned int)num_entries; ++j) + for (unsigned int j = 0; j < static_cast(num_entries); + ++j) { if (static_cast( std::abs(static_cast( @@ -943,7 +946,8 @@ namespace TrilinosWrappers static_cast(i - indices[j])); } } - graph->Comm().MaxAll((TrilinosWrappers::types::int_type *)&local_b, + graph->Comm().MaxAll(reinterpret_cast( + &local_b), &global_b, 1); return static_cast(global_b); @@ -1039,7 +1043,10 @@ namespace TrilinosWrappers const Epetra_Map & SparsityPattern::domain_partitioner() const { - return static_cast(graph->DomainMap()); + // TODO A dynamic_cast fails here, this is suspicious. + const auto &domain_map = + static_cast(graph->DomainMap()); + return domain_map; } @@ -1047,7 +1054,9 @@ namespace TrilinosWrappers const Epetra_Map & SparsityPattern::range_partitioner() const { - return static_cast(graph->RangeMap()); + // TODO A dynamic_cast fails here, this is suspicious. + const auto &range_map = static_cast(graph->RangeMap()); + return range_map; } @@ -1055,7 +1064,9 @@ namespace TrilinosWrappers const Epetra_Map & SparsityPattern::row_partitioner() const { - return static_cast(graph->RowMap()); + // TODO A dynamic_cast fails here, this is suspicious. + const auto &row_map = static_cast(graph->RowMap()); + return row_map; } @@ -1063,7 +1074,9 @@ namespace TrilinosWrappers const Epetra_Map & SparsityPattern::col_partitioner() const { - return static_cast(graph->ColMap()); + // TODO A dynamic_cast fails here, this is suspicious. + const auto &col_map = static_cast(graph->ColMap()); + return col_map; }