23 #include <Teuchos_Comm.hpp>
24 #include <Teuchos_CommHelpers.hpp>
25 #include <Teuchos_DefaultComm.hpp>
26 #include <Teuchos_RCP.hpp>
34 using Teuchos::rcp_const_cast;
35 using Teuchos::rcp_dynamic_cast;
37 using ztcrsmatrix_t = Tpetra::CrsMatrix<zscalar_t, zlno_t, zgno_t, znode_t>;
38 using ztrowmatrix_t = Tpetra::RowMatrix<zscalar_t, zlno_t, zgno_t, znode_t>;
44 typename rowAdapter_t::ConstWeightsHostView1D::execution_space;
48 template<
typename offset_t>
50 const zgno_t *rowIds,
const offset_t *offsets,
const zgno_t *colIds) {
51 int rank = comm->getRank();
52 int nprocs = comm->getSize();
54 for (
int p=0; p < nprocs; p++){
56 std::cout << rank <<
":" << std::endl;
57 for (
zlno_t i=0; i < nrows; i++){
58 std::cout <<
" row " << rowIds[i] <<
": ";
59 for (offset_t j=offsets[i]; j < offsets[i+1]; j++){
60 std::cout << colIds[j] <<
" ";
62 std::cout << std::endl;
73 template <
typename adapter_t,
typename matrix_t>
76 using idsHost_t =
typename adapter_t::ConstIdsHostView;
77 using offsetsHost_t =
typename adapter_t::ConstOffsetsHostView;
79 typename adapter_t::user_t::nonconst_local_inds_host_view_type;
81 typename adapter_t::user_t::nonconst_values_host_view_type;
84 const auto nrows = matrix.getLocalNumRows();
85 const auto ncols = matrix.getLocalNumEntries();
86 const auto maxNumEntries = matrix.getLocalMaxNumRowEntries();
88 typename adapter_t::Base::ConstIdsHostView colIdsHost_(
"colIdsHost_", ncols);
89 typename adapter_t::Base::ConstOffsetsHostView offsHost_(
"offsHost_",
92 localInds_t localColInds(
"localColInds", maxNumEntries);
93 localVals_t localVals(
"localVals", maxNumEntries);
95 for (
size_t r = 0; r < nrows; r++) {
96 size_t numEntries = 0;
97 matrix.getLocalRowCopy(r, localColInds, localVals, numEntries);;
99 offsHost_(r + 1) = offsHost_(r) + numEntries;
100 for (
size_t e = offsHost_(r), i = 0; e < offsHost_(r + 1); e++) {
101 colIdsHost_(e) = matrix.getColMap()->getGlobalElement(localColInds(i++));
105 idsHost_t rowIdsHost;
106 ia.getRowIDsHostView(rowIdsHost);
108 const auto matrixIDS = matrix.getRowMap()->getLocalElementList();
112 idsHost_t colIdsHost;
113 offsetsHost_t offsetsHost;
114 ia.getCRSHostView(offsetsHost, colIdsHost);
120 template <
typename adapter_t,
typename matrix_t>
122 using idsDevice_t =
typename adapter_t::ConstIdsDeviceView;
123 using idsHost_t =
typename adapter_t::ConstIdsHostView;
124 using weightsDevice_t =
typename adapter_t::WeightsDeviceView1D;
125 using weightsHost_t =
typename adapter_t::WeightsHostView1D;
127 const auto nrows = ia.getLocalNumIDs();
137 idsDevice_t rowIdsDevice;
138 ia.getRowIDsDeviceView(rowIdsDevice);
139 idsHost_t rowIdsHost;
140 ia.getRowIDsHostView(rowIdsHost);
148 typename adapter_t::WeightsDeviceView1D{}, 50),
151 weightsDevice_t wgts0(
"wgts0", nrows);
152 Kokkos::parallel_for(
153 nrows, KOKKOS_LAMBDA(
const int idx) { wgts0(idx) = idx * 2; });
159 weightsDevice_t wgts1(
"wgts1", nrows);
160 Kokkos::parallel_for(
161 nrows, KOKKOS_LAMBDA(
const int idx) { wgts1(idx) = idx * 3; });
169 weightsDevice_t weightsDevice;
172 weightsHost_t weightsHost;
180 weightsDevice_t weightsDevice;
183 weightsHost_t weightsHost;
191 weightsDevice_t wgtsDevice;
195 weightsHost_t wgtsHost;
196 Z2_TEST_THROW(ia.getRowWeightsHostView(wgtsHost, 2), std::runtime_error);
204 int main(
int narg,
char *arg[]) {
208 Tpetra::ScopeGuard tscope(&narg, &arg);
209 const auto comm = Tpetra::getDefaultComm();
212 Teuchos::ParameterList params;
213 params.set(
"input file",
"simple");
214 params.set(
"file type",
"Chaco");
219 const auto crsMatrix = uinput->getUITpetraCrsMatrix();
220 const auto rowMatrix = rcp_dynamic_cast<
ztrowmatrix_t>(crsMatrix);
222 const auto nrows = rowMatrix->getLocalNumRows();
227 const int nWeights = 2;
236 auto tpetraRowMatrixInput = rcp(
new rowAdapter_t(rowMatrix, nWeights));
240 rowPart_t *p =
new rowPart_t[nrows];
241 memset(p, 0,
sizeof(rowPart_t) * nrows);
242 ArrayRCP<rowPart_t> solnParts(p, 0, nrows,
true);
244 rowSoln_t solution(env, comm, nWeights);
245 solution.setParts(solnParts);
248 tpetraRowMatrixInput->applyPartitioningSolution(*rowMatrix, mMigrate,
250 const auto newM = rcp(mMigrate);
254 PrintFromRoot(
"Input adapter for Tpetra::RowMatrix migrated to proc 0");
258 }
catch (std::exception &e) {
259 std::cout << e.what() << std::endl;
void PrintFromRoot(const std::string &message)
Provides access for Zoltan2 to Tpetra::CrsMatrix data.
int main(int narg, char **arg)
common code used by tests
typename InputTraits< User >::part_t part_t
#define Z2_TEST_COMPARE_ARRAYS(val1, val2)
Provides access for Zoltan2 to Tpetra::RowMatrix data.
A PartitioningSolution is a solution to a partitioning problem.
#define Z2_TEST_DEVICE_HOST_VIEWS(deviceView, hostView)
#define Z2_TEST_EQUALITY(val1, val2)
Defines the TpetraRowMatrixAdapter class.
The user parameters, debug, timing and memory profiling output objects, and error checking methods...
Tpetra::Map::local_ordinal_type zlno_t
#define Z2_TEST_THROW(code, ExceptType)
Tpetra::Map::global_ordinal_type zgno_t
#define Z2_TEST_NOTHROW(code)
Provides access for Zoltan2 to Tpetra::RowGraph data.