-
Notifications
You must be signed in to change notification settings - Fork 14.5k
[Clang][OpenMP] Non-contiguous strided update #144635
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7378,7 +7378,31 @@ class MappableExprsHandler { | |
// dimension. | ||
uint64_t DimSize = 1; | ||
|
||
bool IsNonContiguous = CombinedInfo.NonContigInfo.IsNonContiguous; | ||
// Detects non-contiguous updates due to strided accesses. | ||
// Sets the 'IsNonContiguous' flag so that the 'MapType' bits are set | ||
// correctly when generating information to be passed to the runtime. The | ||
// flag is set to true if any array section has a stride not equal to 1, or | ||
// if the stride is not a constant expression (conservatively assumed | ||
// non-contiguous). | ||
bool IsNonContiguous = [&]() -> bool { | ||
for (const auto &Component : Components) { | ||
const auto *OASE = | ||
dyn_cast<ArraySectionExpr>(Component.getAssociatedExpression()); | ||
if (OASE) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
const Expr *StrideExpr = OASE->getStride(); | ||
if (StrideExpr) { | ||
if (const auto Constant = | ||
StrideExpr->getIntegerConstantExpr(CGF.getContext())) { | ||
if (!Constant->isOne()) { | ||
return true; | ||
} | ||
Comment on lines
+7393
to
+7398
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use early exits to reduce strutured complexity |
||
} | ||
} | ||
} | ||
} | ||
return false; | ||
}(); | ||
|
||
bool IsPrevMemberReference = false; | ||
|
||
bool IsPartialMapped = | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4724,6 +4724,65 @@ void x() { | |
EXPECT_TRUE(matchesWithOpenMP(Source8, Matcher)); | ||
} | ||
|
||
TEST_P(ASTMatchersTest, OMPTargetUpdateDirective_IsStandaloneDirective) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Clang tests are required |
||
auto Matcher = ompTargetUpdateDirective(isStandaloneDirective()); | ||
|
||
StringRef Source0 = R"( | ||
void foo() { | ||
int arr[8]; | ||
#pragma omp target update from(arr[0:8:2]) | ||
; | ||
} | ||
)"; | ||
EXPECT_TRUE(matchesWithOpenMP(Source0, Matcher)); | ||
} | ||
|
||
TEST_P(ASTMatchersTest, OMPTargetUpdateDirective_HasStructuredBlock) { | ||
StringRef Source0 = R"( | ||
void foo() { | ||
int arr[8]; | ||
#pragma omp target update from(arr[0:8:2]) | ||
; | ||
} | ||
)"; | ||
EXPECT_TRUE(notMatchesWithOpenMP( | ||
Source0, ompTargetUpdateDirective(hasStructuredBlock(nullStmt())))); | ||
} | ||
|
||
TEST_P(ASTMatchersTest, OMPTargetUpdateDirective_HasClause) { | ||
auto Matcher = ompTargetUpdateDirective(hasAnyClause(anything())); | ||
|
||
StringRef Source0 = R"( | ||
void foo() { | ||
int arr[8]; | ||
#pragma omp target update from(arr[0:8:2]) | ||
; | ||
} | ||
)"; | ||
EXPECT_TRUE(matchesWithOpenMP(Source0, Matcher)); | ||
} | ||
|
||
TEST_P(ASTMatchersTest, OMPTargetUpdateDirective_IsAllowedToContainClauseKind) { | ||
auto Matcher = ompTargetUpdateDirective( | ||
isAllowedToContainClauseKind(llvm::omp::OMPC_from)); | ||
|
||
StringRef Source0 = R"( | ||
void x() { | ||
; | ||
} | ||
)"; | ||
EXPECT_TRUE(notMatchesWithOpenMP(Source0, Matcher)); | ||
|
||
StringRef Source1 = R"( | ||
void foo() { | ||
int arr[8]; | ||
#pragma omp target update from(arr[0:8:2]) | ||
; | ||
} | ||
)"; | ||
EXPECT_TRUE(matchesWithOpenMP(Source1, Matcher)); | ||
} | ||
|
||
TEST_P(ASTMatchersTest, HasAnyBase_DirectBase) { | ||
if (!GetParam().isCXX()) { | ||
return; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// This test checks that #pragma omp target update from(data1[0:3:4], | ||
// data2[0:2:5]) correctly updates disjoint strided sections of multiple arrays | ||
// from the device to the host. | ||
|
||
// RUN: %libomptarget-compile-run-and-check-generic | ||
#include <omp.h> | ||
#include <stdio.h> | ||
|
||
int main() { | ||
int len = 12; | ||
double data1[len], data2[len]; | ||
|
||
// Initial values | ||
#pragma omp target map(tofrom : data1[0 : len], data2[0 : len]) | ||
{ | ||
for (int i = 0; i < len; i++) { | ||
data1[i] = i; | ||
data2[i] = i * 10; | ||
} | ||
} | ||
|
||
printf("original host array values:\n"); | ||
printf("data1: "); | ||
for (int i = 0; i < len; i++) | ||
printf("%.1f ", data1[i]); | ||
printf("\ndata2: "); | ||
for (int i = 0; i < len; i++) | ||
printf("%.1f ", data2[i]); | ||
printf("\n\n"); | ||
|
||
#pragma omp target data map(to : data1[0 : len], data2[0 : len]) | ||
{ | ||
// Modify arrays on device | ||
#pragma omp target | ||
{ | ||
for (int i = 0; i < len; i++) | ||
data1[i] += i; | ||
for (int i = 0; i < len; i++) | ||
data2[i] += 100; | ||
} | ||
|
||
// data1[0:3:4] // indices 0,4,8 | ||
// data2[0:2:5] // indices 0,5 | ||
#pragma omp target update from(data1[0 : 3 : 4], data2[0 : 2 : 5]) | ||
} | ||
|
||
printf("device array values after update from:\n"); | ||
printf("data1: "); | ||
for (int i = 0; i < len; i++) | ||
printf("%.1f ", data1[i]); | ||
printf("\ndata2: "); | ||
for (int i = 0; i < len; i++) | ||
printf("%.1f ", data2[i]); | ||
printf("\n\n"); | ||
|
||
// CHECK: data1: 0.0 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0 11.0 | ||
// CHECK: data2: 0.0 10.0 20.0 30.0 40.0 50.0 60.0 70.0 80.0 90.0 100.0 110.0 | ||
|
||
// CHECK: data1: 0.0 1.0 2.0 3.0 8.0 5.0 6.0 7.0 16.0 9.0 10.0 11.0 | ||
// CHECK: data2: 100.0 10.0 20.0 30.0 40.0 150.0 60.0 70.0 80.0 90.0 100.0 | ||
// 110.0 | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// This test checks that #pragma omp target update from(data[0:4:3]) correctly | ||
// updates every third element (stride 3) from the device to the host, partially | ||
// across the array | ||
|
||
// RUN: %libomptarget-compile-run-and-check-generic | ||
#include <omp.h> | ||
#include <stdio.h> | ||
|
||
int main() { | ||
int len = 11; | ||
double data[len]; | ||
|
||
#pragma omp target map(tofrom : data[0 : len]) | ||
{ | ||
for (int i = 0; i < len; i++) | ||
data[i] = i; | ||
} | ||
|
||
// Initial values | ||
printf("original host array values:\n"); | ||
for (int i = 0; i < len; i++) | ||
printf("%f\n", data[i]); | ||
printf("\n"); | ||
|
||
#pragma omp target data map(to : data[0 : len]) | ||
{ | ||
// Modify arrays on device | ||
#pragma omp target | ||
for (int i = 0; i < len; i++) | ||
data[i] += i; | ||
|
||
#pragma omp target update from(data[0 : 4 : 3]) // indices 0,3,6,9 | ||
} | ||
|
||
printf("device array values after update from:\n"); | ||
for (int i = 0; i < len; i++) | ||
printf("%f\n", data[i]); | ||
printf("\n"); | ||
|
||
// CHECK: 0.000000 | ||
// CHECK: 1.000000 | ||
// CHECK: 2.000000 | ||
// CHECK: 3.000000 | ||
// CHECK: 4.000000 | ||
// CHECK: 5.000000 | ||
// CHECK: 6.000000 | ||
// CHECK: 7.000000 | ||
// CHECK: 8.000000 | ||
// CHECK: 9.000000 | ||
// CHECK: 10.000000 | ||
|
||
// CHECK: 0.000000 | ||
// CHECK: 1.000000 | ||
// CHECK: 2.000000 | ||
// CHECK: 6.000000 | ||
// CHECK: 4.000000 | ||
// CHECK: 5.000000 | ||
// CHECK: 12.000000 | ||
// CHECK: 7.000000 | ||
// CHECK: 8.000000 | ||
// CHECK: 18.000000 | ||
// CHECK: 10.000000 | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// This test checks that "update from" clause in OpenMP is supported when the | ||
// elements are updated in a non-contiguous manner. This test checks that | ||
// #pragma omp target update from(data[0:4:2]) correctly updates only every | ||
// other element (stride 2) from the device to the host | ||
|
||
// RUN: %libomptarget-compile-run-and-check-generic | ||
#include <omp.h> | ||
#include <stdio.h> | ||
|
||
int main() { | ||
int len = 8; | ||
double data[len]; | ||
#pragma omp target map(tofrom : len, data[0 : len]) | ||
{ | ||
for (int i = 0; i < len; i++) { | ||
data[i] = i; | ||
} | ||
} | ||
// Initial values | ||
printf("original host array values:\n"); | ||
for (int i = 0; i < len; i++) | ||
printf("%f\n", data[i]); | ||
printf("\n"); | ||
|
||
#pragma omp target data map(to : len, data[0 : len]) | ||
{ | ||
// Modify arrays on device | ||
#pragma omp target | ||
for (int i = 0; i < len; i++) { | ||
data[i] += i; | ||
} | ||
|
||
#pragma omp target update from(data[0 : 4 : 2]) | ||
} | ||
// CHECK: 0.000000 | ||
// CHECK: 1.000000 | ||
// CHECK: 4.000000 | ||
// CHECK: 3.000000 | ||
// CHECK: 8.000000 | ||
// CHECK: 5.000000 | ||
// CHECK: 12.000000 | ||
// CHECK: 7.000000 | ||
// CHECK-NOT: 2.000000 | ||
// CHECK-NOT: 6.000000 | ||
// CHECK-NOT: 10.000000 | ||
// CHECK-NOT: 14.000000 | ||
|
||
printf("from target array results:\n"); | ||
for (int i = 0; i < len; i++) | ||
printf("%f\n", data[i]); | ||
printf("\n"); | ||
|
||
return 0; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do you need this?