Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Copy of C Array to Dash Array across multiple units #347

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 97 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
---
Language: Cpp
# BasedOnStyle: Google
AccessModifierOffset: -1
AlignAfterOpenBracket: Align
AlignConsecutiveAssignments: false
AlignConsecutiveDeclarations: false
AlignEscapedNewlinesLeft: true
AlignOperands: true
AlignTrailingComments: true
AllowAllParametersOfDeclarationOnNextLine: true
AllowShortBlocksOnASingleLine: false
AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine: All
AllowShortIfStatementsOnASingleLine: true
AllowShortLoopsOnASingleLine: true
AlwaysBreakAfterDefinitionReturnType: None
AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings: true
AlwaysBreakTemplateDeclarations: true
BinPackArguments: true
BinPackParameters: true
BraceWrapping:
AfterClass: false
AfterControlStatement: false
AfterEnum: false
AfterFunction: false
AfterNamespace: false
AfterObjCDeclaration: false
AfterStruct: false
AfterUnion: false
BeforeCatch: false
BeforeElse: false
IndentBraces: false
BreakBeforeBinaryOperators: None
BreakBeforeBraces: Stroustrup
BreakBeforeTernaryOperators: true
#BreakConstructorInitializersBeforeComma: false
BreakConstructorInitializersBeforeComma: true
BreakAfterJavaFieldAnnotations: false
BreakStringLiterals: true
ColumnLimit: 80
CommentPragmas: '^ IWYU pragma:'
ConstructorInitializerAllOnOneLineOrOnePerLine: false
ConstructorInitializerIndentWidth: 2
ContinuationIndentWidth: 4
Cpp11BracedListStyle: true
DerivePointerAlignment: true
DisableFormat: false
ExperimentalAutoDetectBinPacking: false
ForEachMacros: [ foreach, Q_FOREACH, BOOST_FOREACH ]
IncludeCategories:
- Regex: '^<.*\.h>'
Priority: 1
- Regex: '^<.*'
Priority: 2
- Regex: '.*'
Priority: 3
IncludeIsMainRegex: '([-_](test|unittest))?$'
IndentCaseLabels: true
IndentWidth: 2
IndentWrappedFunctionNames: false
JavaScriptQuotes: Leave
JavaScriptWrapImports: true
KeepEmptyLinesAtTheStartOfBlocks: false
MacroBlockBegin: ''
MacroBlockEnd: ''
MaxEmptyLinesToKeep: 1
NamespaceIndentation: None
ObjCBlockIndentWidth: 2
ObjCSpaceAfterProperty: false
ObjCSpaceBeforeProtocolList: false
PenaltyBreakBeforeFirstCallParameter: 1
PenaltyBreakComment: 300
PenaltyBreakFirstLessLess: 120
PenaltyBreakString: 1000
PenaltyExcessCharacter: 1000000
PenaltyReturnTypeOnItsOwnLine: 200
PointerAlignment: Left
ReflowComments: true
SortIncludes: true
SpaceAfterCStyleCast: false
SpaceBeforeAssignmentOperators: true
SpaceBeforeParens: ControlStatements
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 2
SpacesInAngles: false
SpacesInContainerLiterals: true
SpacesInCStyleCastParentheses: false
SpacesInParentheses: false
SpacesInSquareBrackets: false
#Standard: Auto
Standard: Cpp11
TabWidth: 8
UseTab: Never
...

52 changes: 39 additions & 13 deletions dash/include/dash/algorithm/Copy.h
Original file line number Diff line number Diff line change
Expand Up @@ -493,17 +493,42 @@ GlobOutputIt copy_impl(
"l_in_last:", in_last,
"g_out_first:", out_first.pos());



auto num_elements = std::distance(in_first, in_last);
dart_storage_t ds = dash::dart_storage<ValueType>(num_elements);
DASH_ASSERT_RETURNS(
dart_put_blocking(
out_first.dart_gptr(),
in_first,
ds.nelem,
ds.dtype),
DART_OK);

auto out_last = out_first + num_elements;
if (num_elements < 1) return out_first;

auto nremaining = static_cast<dash::default_size_t>(num_elements);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dangerous! dash::default_size_t may be 32 bit wide while num_elements is of type size_t.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

std::distance cannot return size_t because it may be negative. And then I check explicitly for a non-negative number.
Apart from that I would expect that dash::default_size_t is the same as size_t if available on the underlying platform. Is there any argument against this?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right, it is ssize_t. The case I had in mind is the following: the user might decides to compile with the flag DASH_ENABLE_DEFAULT_INDEX_TYPE_INT (for whatever reason) and then copy more than 4GB into the array, which causes num_elements to be truncated into nremaining. He might even pass size_t as template parameter IndexType to the global data structure so it's a valid operation. I think using the pattern's index-type is the safest bet. But that is certainly a highly pathologic case :)

auto pattern = out_first.pattern();

while (nremaining) {
// global index to local unit and index
auto local_pos = pattern.local(out_first.pos());
// number of elements in unit
auto local_size = pattern.local_extents(team_unit_t{local_pos.unit});

auto ncopy = std::min(
std::initializer_list<dash::default_size_t>{local_size[0],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See above.

nremaining});

dart_storage_t ds = dash::dart_storage<ValueType>(ncopy);

DASH_ASSERT_RETURNS(
dart_put_blocking(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not use dart_put and a final dart_flush after the loop?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Of course you are right, thanks. I will do this. Did not think too much about this, I simply extended the already existing code.

out_first.dart_gptr(),
in_first,
ds.nelem,
ds.dtype),
DART_OK);

std::advance(in_first, ncopy);
std::advance(out_first, ncopy);

nremaining = std::distance(in_first, in_last);
}

auto out_last = out_first;
DASH_LOG_TRACE("dash::copy_impl >",
"g_out_last:", out_last.dart_gptr());

Expand Down Expand Up @@ -867,7 +892,7 @@ ValueType * copy(
auto total_copy_elem = in_last - in_first;

// Instead of testing in_first.local() and in_last.local(), this test for
// a local-only range only requires one call to in_first.local() which
// a local-only range only requires one call to in_first.local() which
// increases throughput by ~10% for local ranges.
if (num_local_elem == total_copy_elem) {
// Entire input range is local:
Expand Down Expand Up @@ -1113,10 +1138,11 @@ GlobOutputIt copy(
// Copy to remote elements succeeding the local subrange:
if (g_l_offset_end < out_h_last.pos()) {
DASH_LOG_TRACE("dash::copy", "copy to global succeeding local subrange");

out_last = dash::internal::copy_impl(
in_first + l_elem_offset + num_local_elem,
in_last,
out_first + num_local_elem);
in_first + l_elem_offset + num_local_elem,
in_last,
out_first + num_local_elem);
}
} else {
// All elements in output range are remote
Expand Down
19 changes: 19 additions & 0 deletions dash/test/algorithm/CopyTest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -803,6 +803,25 @@ TEST_F(CopyTest, AsyncGlobalToLocalBlock)
}
}

TEST_F(CopyTest, CArrayToDashArray)
{
dash::Array<int> arr(100);

if (dash::myid() == 0) {
int buf[100];
std::iota(buf, buf + 100, 0);
// copy local buffer to global array
dash::copy(buf, buf + 100, arr.begin());
}

arr.barrier();

if (dash::myid() == 0) {
for (size_t idx = 0; idx < 100; ++idx) {
EXPECT_EQ_U(idx, static_cast<int>(arr[idx]));
}
}
}
#if 0
// TODO
TEST_F(CopyTest, AsyncAllToLocalVector)
Expand Down