152 const PadCrsAction action,
153 const RowPtr& row_ptr_beg,
154 const RowPtr& row_ptr_end,
155 Indices& indices_wdv,
157 const Padding& padding,
162 using Kokkos::view_alloc;
163 using Kokkos::WithoutInitializing;
165 std::unique_ptr<std::string> prefix;
167 const size_t maxNumToPrint = verbose ?
170 std::ostringstream os;
171 os <<
"Proc " << my_rank <<
": Tpetra::...::pad_crs_arrays: ";
172 prefix = std::unique_ptr<std::string>(
new std::string(os.str()));
173 os <<
"Start" << endl;
174 std::cerr << os.str();
176 Kokkos::HostSpace hostSpace;
179 std::ostringstream os;
180 os << *prefix <<
"On input: ";
182 Kokkos::create_mirror_view(hostSpace, row_ptr_beg);
184 Kokkos::deep_copy(row_ptr_beg_h, row_ptr_beg);
189 Kokkos::create_mirror_view(hostSpace, row_ptr_end);
191 Kokkos::deep_copy(row_ptr_end_h, row_ptr_end);
194 os <<
", indices.extent(0): " << indices_wdv.extent(0)
195 <<
", values.extent(0): " << values_wdv.extent(0)
199 std::cerr << os.str();
202 if (row_ptr_beg.size() == 0) {
204 std::ostringstream os;
205 os << *prefix <<
"Done; local matrix has no rows" << endl;
206 std::cerr << os.str();
211 const size_t lclNumRows(row_ptr_beg.size() - 1);
212 RowPtr newAllocPerRow =
213 make_uninitialized_view<RowPtr>(
"newAllocPerRow", lclNumRows,
214 verbose, prefix.get());
216 std::ostringstream os;
217 os << *prefix <<
"Fill newAllocPerRow & compute increase" << endl;
218 std::cerr << os.str();
223 auto row_ptr_end_h = create_mirror_view(
224 hostSpace, row_ptr_end, verbose, prefix.get());
227 auto row_ptr_beg_h = create_mirror_view(
228 hostSpace, row_ptr_beg, verbose, prefix.get());
232 auto newAllocPerRow_h = create_mirror_view(
233 hostSpace, newAllocPerRow, verbose, prefix.get());
234 using host_range_type = Kokkos::RangePolicy<
235 Kokkos::DefaultHostExecutionSpace,
size_t>;
236 Kokkos::parallel_reduce
237 (
"Tpetra::CrsGraph: Compute new allocation size per row",
238 host_range_type(0, lclNumRows),
239 [&] (
const size_t lclRowInd,
size_t& lclIncrease) {
240 const size_t start = row_ptr_beg_h[lclRowInd];
241 const size_t end = row_ptr_beg_h[lclRowInd+1];
242 TEUCHOS_ASSERT( end >= start );
243 const size_t oldAllocSize = end - start;
244 const size_t oldNumEnt = row_ptr_end_h[lclRowInd] - start;
245 TEUCHOS_ASSERT( oldNumEnt <= oldAllocSize );
252 auto result = padding.get_result(lclRowInd);
253 const size_t newNumEnt = oldNumEnt + result.numInSrcNotInTgt;
254 if (newNumEnt > oldAllocSize) {
255 lclIncrease += (newNumEnt - oldAllocSize);
256 newAllocPerRow_h[lclRowInd] = newNumEnt;
259 newAllocPerRow_h[lclRowInd] = oldAllocSize;
264 std::ostringstream os;
265 os << *prefix <<
"increase: " << increase <<
", ";
269 std::cerr << os.str();
276 Kokkos::deep_copy(
execution_space(), newAllocPerRow, newAllocPerRow_h);
279 using inds_value_type =
280 typename Indices::t_dev::non_const_value_type;
281 using vals_value_type =
typename Values::t_dev::non_const_value_type;
284 auto indices_old = indices_wdv.getDeviceView(Access::ReadOnly);
285 const size_t newIndsSize = size_t(indices_old.size()) + increase;
286 auto indices_new = make_uninitialized_view<typename Indices::t_dev>(
287 "Tpetra::CrsGraph column indices", newIndsSize, verbose,
290 typename Values::t_dev values_new;
291 auto values_old = values_wdv.getDeviceView(Access::ReadOnly);
292 if (action == PadCrsAction::INDICES_AND_VALUES) {
293 const size_t newValsSize = newIndsSize;
296 values_new = make_initialized_view<typename Values::t_dev>(
297 "Tpetra::CrsMatrix values", newValsSize, verbose, prefix.get());
301 std::ostringstream os;
302 os << *prefix <<
"Repack" << endl;
303 std::cerr << os.str();
306 using range_type = Kokkos::RangePolicy<execution_space, size_t>;
307 Kokkos::parallel_scan(
308 "Tpetra::CrsGraph or CrsMatrix repack",
309 range_type(
size_t(0),
size_t(lclNumRows+1)),
310 KOKKOS_LAMBDA (
const size_t lclRow,
size_t& newRowBeg,
311 const bool finalPass)
316 const size_t row_beg = row_ptr_beg[lclRow];
317 const size_t row_end =
318 lclRow < lclNumRows ? row_ptr_end[lclRow] : row_beg;
319 const size_t numEnt = row_end - row_beg;
320 const size_t newRowAllocSize =
321 lclRow < lclNumRows ? newAllocPerRow[lclRow] : size_t(0);
323 if (lclRow < lclNumRows) {
324 const Kokkos::pair<size_t, size_t> oldRange(
325 row_beg, row_beg + numEnt);
326 const Kokkos::pair<size_t, size_t> newRange(
327 newRowBeg, newRowBeg + numEnt);
328 auto oldColInds = Kokkos::subview(indices_old, oldRange);
329 auto newColInds = Kokkos::subview(indices_new, newRange);
332 memcpy(newColInds.data(), oldColInds.data(),
333 numEnt *
sizeof(inds_value_type));
334 if (action == PadCrsAction::INDICES_AND_VALUES) {
336 Kokkos::subview(values_old, oldRange);
337 auto newVals = Kokkos::subview(values_new, newRange);
338 memcpy(newVals.data(), oldVals.data(),
339 numEnt *
sizeof(vals_value_type));
343 row_ptr_beg[lclRow] = newRowBeg;
344 if (lclRow < lclNumRows) {
345 row_ptr_end[lclRow] = newRowBeg + numEnt;
348 newRowBeg += newRowAllocSize;
353 std::ostringstream os;
357 Kokkos::create_mirror_view(hostSpace, row_ptr_beg);
359 Kokkos::deep_copy(row_ptr_beg_h, row_ptr_beg);
366 Kokkos::create_mirror_view(hostSpace, row_ptr_end);
368 Kokkos::deep_copy(row_ptr_end_h, row_ptr_end);
373 std::cout << os.str();
376 indices_wdv = Indices(indices_new);
377 values_wdv = Values(values_new);
381 auto indices_h = indices_wdv.getHostView(Access::ReadOnly);
382 auto values_h = values_wdv.getHostView(Access::ReadOnly);
383 std::ostringstream os;
394 std::ostringstream os;
395 os << *prefix <<
"Done" << endl;
396 std::cerr << os.str();
404 typename Pointers::value_type
const row,
405 Pointers
const& row_ptrs,
406 InOutIndices& cur_indices,
407 size_t& num_assigned,
408 InIndices
const& new_indices,
410 std::function<
void(
size_t const,
size_t const,
size_t const)> cb)
412 if (new_indices.size() == 0) {
416 if (cur_indices.size() == 0) {
418 return Teuchos::OrdinalTraits<size_t>::invalid();
421 using offset_type =
typename std::decay<
decltype (row_ptrs[0])>::type;
422 using ordinal_type =
typename std::decay<
decltype (cur_indices[0])>::type;
424 const offset_type start = row_ptrs[row];
425 offset_type end = start +
static_cast<offset_type
> (num_assigned);
426 const size_t num_avail = (row_ptrs[row + 1] < end) ?
size_t (0) :
427 row_ptrs[row + 1] - end;
428 const size_t num_new_indices =
static_cast<size_t> (new_indices.size ());
429 size_t num_inserted = 0;
431 size_t numIndicesLookup = num_assigned + num_new_indices;
434 const size_t useLookUpTableThreshold = 400;
436 if (numIndicesLookup <= useLookUpTableThreshold || num_new_indices == 1) {
439 for (
size_t k = 0; k < num_new_indices; ++k) {
440 const ordinal_type idx = std::forward<IndexMap>(map)(new_indices[k]);
441 offset_type row_offset = start;
442 for (; row_offset < end; ++row_offset) {
443 if (idx == cur_indices[row_offset]) {
448 if (row_offset == end) {
449 if (num_inserted >= num_avail) {
450 return Teuchos::OrdinalTraits<size_t>::invalid();
453 cur_indices[end++] = idx;
457 cb(k, start, row_offset - start);
463 std::unordered_map<ordinal_type, offset_type> idxLookup(numIndicesLookup);
466 for (
size_t k = 0; k < num_assigned; k++) {
467 idxLookup[cur_indices[start+k]] = start+k;
471 for (
size_t k = 0; k < num_new_indices; k++) {
472 const ordinal_type idx = std::forward<IndexMap>(map)(new_indices[k]);
473 offset_type row_offset;
475 auto it = idxLookup.find(idx);
476 if (it == idxLookup.end()) {
477 if (num_inserted >= num_avail) {
478 return Teuchos::OrdinalTraits<size_t>::invalid();
482 cur_indices[end++] = idx;
483 idxLookup[idx] = row_offset;
488 row_offset = it->second;
491 cb(k, start, row_offset - start);
495 num_assigned += num_inserted;