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

Kokkos-aware Clad #783

Open
wants to merge 75 commits into
base: master
Choose a base branch
from
Open

Kokkos-aware Clad #783

wants to merge 75 commits into from

Conversation

kliegeois
Copy link

This PR provides the implementation of a first set of features for the automatic generation of gradients of Kokkos-based code.

The content of this PR supports reverse mode of Kokkos parallel_for, deep_copy, and view accesses.

@vgvassilev @brian-kelley

Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

clang-tidy made some suggestions

There were too many comments to post at once. Showing the first 10 out of 136. Check the log or trigger a new build to see more.

@@ -174,6 +179,22 @@ namespace clad {
/// otherwise returns false.
bool HasAnyReferenceOrPointerArgument(const clang::FunctionDecl* FD);

/// Returns true if `constructedTypeName` is a string describing Kokkos::TeamPolicy type.
bool IsKokkosTeamPolicy(const std::string constructedTypeName);
Copy link
Contributor

Choose a reason for hiding this comment

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

warning: parameter 'constructedTypeName' is const-qualified in the function declaration; const-qualification of parameters only has an effect in function definitions [readability-avoid-const-params-in-decls]

Suggested change
bool IsKokkosTeamPolicy(const std::string constructedTypeName);
bool IsKokkosTeamPolicy(std::string constructedTypeName);


/// Returns true if `constructedTypeName` is a string describing Kokkos::TeamThreadRange,
/// Kokkos::ThreadVectorRange, or Kokkos::TeamVectorRange type.
bool IsKokkosRange(const std::string constructedTypeName);
Copy link
Contributor

Choose a reason for hiding this comment

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

warning: parameter 'constructedTypeName' is const-qualified in the function declaration; const-qualification of parameters only has an effect in function definitions [readability-avoid-const-params-in-decls]

Suggested change
bool IsKokkosRange(const std::string constructedTypeName);
bool IsKokkosRange(std::string constructedTypeName);

bool IsKokkosRange(const std::string constructedTypeName);

/// Returns true if `constructedTypeName` is a string describing Kokkos::Member type.
bool IsKokkosMember(const std::string constructedTypeName);
Copy link
Contributor

Choose a reason for hiding this comment

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

warning: parameter 'constructedTypeName' is const-qualified in the function declaration; const-qualification of parameters only has an effect in function definitions [readability-avoid-const-params-in-decls]

Suggested change
bool IsKokkosMember(const std::string constructedTypeName);
bool IsKokkosMember(std::string constructedTypeName);

bool IsKokkosMember(const std::string constructedTypeName);

/// Returns true if `constructedTypeName` is a string describing Kokkos::View type.
bool IsKokkosView(const std::string constructedTypeName);
Copy link
Contributor

Choose a reason for hiding this comment

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

warning: parameter 'constructedTypeName' is const-qualified in the function declaration; const-qualification of parameters only has an effect in function definitions [readability-avoid-const-params-in-decls]

Suggested change
bool IsKokkosView(const std::string constructedTypeName);
bool IsKokkosView(std::string constructedTypeName);

Comment on lines +7 to +8
#ifndef CLAD_KOKKOS_VIEW_ACCESS_VISITOR_H
#define CLAD_KOKKOS_VIEW_ACCESS_VISITOR_H
Copy link
Contributor

Choose a reason for hiding this comment

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

warning: header guard does not follow preferred style [llvm-header-guard]

Suggested change
#ifndef CLAD_KOKKOS_VIEW_ACCESS_VISITOR_H
#define CLAD_KOKKOS_VIEW_ACCESS_VISITOR_H
#ifndef CLAD_DIFFERENTIATOR_KOKKOSVIEWACCESSVISITOR_H
#define CLAD_DIFFERENTIATOR_KOKKOSVIEWACCESSVISITOR_H

include/clad/Differentiator/KokkosViewAccessVisitor.h:423:

- #endif // CLAD_KOKKOS_VIEW_ACCESS_VISITOR_H
+ #endif // CLAD_DIFFERENTIATOR_KOKKOSVIEWACCESSVISITOR_H

using namespace clang;

if (!Stmt1 || !Stmt2) {
return !Stmt1 && !Stmt2;
Copy link
Contributor

Choose a reason for hiding this comment

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

warning: implicit conversion 'const clang::Stmt *' -> bool [readability-implicit-bool-conversion]

Suggested change
return !Stmt1 && !Stmt2;
return (Stmt1 == nullptr) && !Stmt2;

using namespace clang;

if (!Stmt1 || !Stmt2) {
return !Stmt1 && !Stmt2;
Copy link
Contributor

Choose a reason for hiding this comment

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

warning: implicit conversion 'const clang::Stmt *' -> bool [readability-implicit-bool-conversion]

Suggested change
return !Stmt1 && !Stmt2;
return !Stmt1 && (Stmt2 == nullptr);

case Stmt::NullStmtClass:
return true;
case Stmt::CStyleCastExprClass: {
const CStyleCastExpr* CastExpr1 = cast<CStyleCastExpr>(Stmt1);
Copy link
Contributor

Choose a reason for hiding this comment

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

warning: use auto when initializing with a template cast to avoid duplicating the type name [modernize-use-auto]

Suggested change
const CStyleCastExpr* CastExpr1 = cast<CStyleCastExpr>(Stmt1);
const auto* CastExpr1 = cast<CStyleCastExpr>(Stmt1);

return true;
case Stmt::CStyleCastExprClass: {
const CStyleCastExpr* CastExpr1 = cast<CStyleCastExpr>(Stmt1);
const CStyleCastExpr* CastExpr2 = cast<CStyleCastExpr>(Stmt2);
Copy link
Contributor

Choose a reason for hiding this comment

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

warning: use auto when initializing with a template cast to avoid duplicating the type name [modernize-use-auto]

Suggested change
const CStyleCastExpr* CastExpr2 = cast<CStyleCastExpr>(Stmt2);
const auto* CastExpr2 = cast<CStyleCastExpr>(Stmt2);

return CastExpr1->getTypeAsWritten() == CastExpr2->getTypeAsWritten();
}
case Stmt::ReturnStmtClass: {
const ReturnStmt *ReturnStmt1 = cast<ReturnStmt>(Stmt1);
Copy link
Contributor

Choose a reason for hiding this comment

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

warning: use auto when initializing with a template cast to avoid duplicating the type name [modernize-use-auto]

Suggested change
const ReturnStmt *ReturnStmt1 = cast<ReturnStmt>(Stmt1);
const auto *ReturnStmt1 = cast<ReturnStmt>(Stmt1);

@@ -153,6 +153,11 @@ namespace clad {
clang::DeclContext* GetOutermostDC(clang::Sema& semaRef,
clang::DeclContext* DC);

clang::Expr* GetUnresolvedLookup(clang::Sema& semaRef,
Copy link
Collaborator

Choose a reason for hiding this comment

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

ASTContext can be obtained through Sema. We don't need to pass both of them to a function.

@@ -174,6 +179,22 @@ namespace clad {
/// otherwise returns false.
bool HasAnyReferenceOrPointerArgument(const clang::FunctionDecl* FD);

/// Returns true if `constructedTypeName` is a string describing Kokkos::TeamPolicy type.
bool IsKokkosTeamPolicy(const std::string constructedTypeName);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Perhaps kokkos specific functions should be in a separate namespace altogether?


/// Returns true if `constructedTypeName` is a string describing Kokkos::TeamThreadRange,
/// Kokkos::ThreadVectorRange, or Kokkos::TeamVectorRange type.
bool IsKokkosRange(const std::string constructedTypeName);
Copy link
Collaborator

Choose a reason for hiding this comment

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

We should pass std::string as const std::string& instead of const std::string. const in parameters not being passed as reference is meaningless for the caller.

auto MCE = dyn_cast<clang::CXXMemberCallExpr>(CE);

if (utils::IsKokkosView(MCE->getObjectType().getAsString())) {
//Member function called from a Kokkos::View; nothing to do here
Copy link
Collaborator

Choose a reason for hiding this comment

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

For supporting things such as this, we should use custom derivatives instead of modifying the core Clad code. Modifying core clad is not a scalable solution.

return StmtDiff(Clone(CE));
}
}
if (isa<CXXOperatorCallExpr>(CE)) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

This should be implemented as custom derivative as well.

}

// Returning the function call and zero derivative
if (isKokkosViewAccess) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

All these changes can be implemented with more consistency and much less efforts using the custom derivative functionality.

@@ -99,7 +100,9 @@ Expr* getArraySizeExpr(const ArrayType* AT, ASTContext& context,
}

ReverseModeVisitor::ReverseModeVisitor(DerivativeBuilder& builder)
: VisitorBase(builder), m_Result(nullptr) {}
: VisitorBase(builder), m_Result(nullptr) {
m_KVAV = new KokkosViewAccessVisitor(m_Sema, m_Context);
Copy link
Collaborator

Choose a reason for hiding this comment

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

It seems that we are unconditionally creating and deleting m_KVAV and we exactly know when to create and delete it. Why does m_KVAV needs to be a pointer? Why cannot we have m_KVAV as non-pointer instance member?

@vgvassilev vgvassilev added this to the v1.6 milestone Mar 7, 2024
@gojakuch gojakuch mentioned this pull request Mar 18, 2024
@vgvassilev vgvassilev modified the milestones: v1.6, v1.7 Jul 6, 2024
gojakuch added a commit to gojakuch/clad that referenced this pull request Jul 10, 2024
This commit adds a test from the original Kokkos PR vgvassilev#783
and provides the implementation of the simplest Kokkos
features in the form of custom pushforwards. This commit
only addresses the forward mode.
gojakuch added a commit to gojakuch/clad that referenced this pull request Jul 10, 2024
This commit adds a test from the original Kokkos PR vgvassilev#783
and provides the implementation of the simplest Kokkos
features in the form of custom pushforwards. This commit
only addresses the forward mode.
gojakuch added a commit to gojakuch/clad that referenced this pull request Jul 13, 2024
This commit adds a test from the original Kokkos PR vgvassilev#783
and provides the implementation of the simplest Kokkos
features in the form of custom pushforwards. This commit
only addresses the forward mode.
gojakuch added a commit to gojakuch/clad that referenced this pull request Jul 13, 2024
This commit adds a test from the original Kokkos PR vgvassilev#783
and provides the implementation of the simplest Kokkos
features in the form of custom pushforwards. This commit
only addresses the forward mode.
gojakuch added a commit to gojakuch/clad that referenced this pull request Jul 13, 2024
This commit adds a test from the original Kokkos PR vgvassilev#783
and provides the implementation of the simplest Kokkos
features in the form of custom pushforwards. This commit
only addresses the forward mode.
gojakuch added a commit to gojakuch/clad that referenced this pull request Jul 13, 2024
This commit adds a test from the original Kokkos PR vgvassilev#783
and provides the implementation of the simplest Kokkos
features in the form of custom pushforwards. This commit
only addresses the forward mode.
gojakuch added a commit to gojakuch/clad that referenced this pull request Jul 13, 2024
This commit adds a test from the original Kokkos PR vgvassilev#783
and provides the implementation of the simplest Kokkos
features in the form of custom pushforwards. This commit
only addresses the forward mode.
gojakuch added a commit to gojakuch/clad that referenced this pull request Jul 17, 2024
This commit adds a test from the original Kokkos PR vgvassilev#783
and provides the implementation of the simplest Kokkos
features in the form of custom pushforwards. This commit
only addresses the forward mode.
gojakuch added a commit to gojakuch/clad that referenced this pull request Jul 17, 2024
This commit adds a test from the original Kokkos PR vgvassilev#783
and provides the implementation of the simplest Kokkos
features in the form of custom pushforwards. This commit
only addresses the forward mode.
gojakuch added a commit to gojakuch/clad that referenced this pull request Jul 18, 2024
This commit adds a test from the original Kokkos PR vgvassilev#783
and provides the implementation of the simplest Kokkos
features in the form of custom pushforwards. This commit
only addresses the forward mode.
gojakuch added a commit to gojakuch/clad that referenced this pull request Jul 18, 2024
This commit adds a test from the original Kokkos PR vgvassilev#783
and provides the implementation of the simplest Kokkos
features in the form of custom pushforwards. This commit
only addresses the forward mode.
@vgvassilev vgvassilev modified the milestones: v1.7, 1.8 Jul 18, 2024
vgvassilev pushed a commit to gojakuch/clad that referenced this pull request Jul 21, 2024
This commit adds a test from the original Kokkos PR vgvassilev#783
and provides the implementation of the simplest Kokkos
features in the form of custom pushforwards. This commit
only addresses the forward mode.
gojakuch added a commit to gojakuch/clad that referenced this pull request Jul 22, 2024
This commit adds a test from the original Kokkos PR vgvassilev#783
and provides the implementation of the simplest Kokkos
features in the form of custom pushforwards. This commit
only addresses the forward mode.
gojakuch added a commit to gojakuch/clad that referenced this pull request Jul 22, 2024
This commit adds a test from the original Kokkos PR vgvassilev#783
and provides the implementation of the simplest Kokkos
features in the form of custom pushforwards. This commit
only addresses the forward mode.
vgvassilev pushed a commit that referenced this pull request Jul 22, 2024
This commit adds a test from the original Kokkos PR #783
and provides the implementation of the simplest Kokkos
features in the form of custom pushforwards. This commit
only addresses the forward mode.
@vgvassilev
Copy link
Owner

@kliegeois, @brian-kelley, is there a point to try to rebase this PR or @gojakuch should continue picking random things from it as it is now?

@kliegeois
Copy link
Author

@vgvassilev I am not sure what is best.
If we rebase, would @gojakuch still create different PRs that refactor this PR?
If yes, the rebase is not necessarily useful although it will be easier to highlight the remaining pieces.

@vgvassilev vgvassilev modified the milestones: v1.8, v1.9 Dec 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants