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

Add embedded compiler-explorer example #444

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
5 changes: 5 additions & 0 deletions .github/workflows/deploy_docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ jobs:

steps:
- uses: actions/checkout@v3
- name: Checkout kokkos
uses: actions/checkout@v3
with:
repository: kokkos/kokkos
path: kokkos
- name: Set up Python ${{ env.python-version }}
uses: actions/setup-python@v4
with:
Expand Down
15 changes: 15 additions & 0 deletions docs/source/API/core/parallel-dispatch/parallel_for.rst
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ More Detailed Examples are provided in the ExecutionPolicy documentation.

* ``IntegerType`` policy with lambda as the functor. Note that KOKKOS_LAMBDA is the same as [=] KOKKOS_FUNCTION, which means that all of the variables used within the lambda are captured by value. Also, the KOKKOS_LAMBDA and KOKKOS_FUNCTION macros add all of the function specifiers necessary for the target execution space.

.. note::
Code snippet + a short link to compiler explorer.

.. code-block:: cpp

#include<Kokkos_Core.hpp>
Expand All @@ -81,6 +84,18 @@ More Detailed Examples are provided in the ExecutionPolicy documentation.
Kokkos::finalize();
}

.. raw:: html

<a href="https://godbolt.org/z/q9h339vob" target="_blank">Edit on Compiler Explorer</a>
Comment on lines +87 to +89
Copy link
Contributor Author

Choose a reason for hiding this comment

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

code-block above + a manually created shortlink to Compiler Explorer:
image


.. note::
Code snippet taken directly from Kokkos Core (``hello_world_lambda.cpp``) + a link containing full source code of the example.

.. ceinclude:: ../../../../../kokkos/example/tutorial/01_hello_world_lambda/hello_world_lambda.cpp
:language: cpp
:start-after: Kokkos::initialize
:end-before: Kokkos::finalize
Comment on lines +94 to +97
Copy link
Contributor Author

@cz4rs cz4rs Oct 25, 2023

Choose a reason for hiding this comment

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

helo_world_lambda.cpp + an auto-generated link using clientstate API:

image

See live version.

See ceinclude.py for extension details.

Copy link
Contributor

Choose a reason for hiding this comment

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

Given the number of comments in this file, I would rather not include that one specifically. Other than that, it's a good idea to use examples we run through our CI. I could see that we just reduce the verbosity in these files and refer to kokkos-tutorials for more extensive tutorials.


* ``TeamPolicy`` policy with C++ struct as functor. Note that the KOKKOS_INLINE_FUNCTION macro adds all of the function specifiers necessary for the target execution space. The TagA/B structs also provide the ability to 'overload' the operators within the same functor. Much like the lambda example, the functor and any member variables contained within are captured by value, which means they must have either implicit or explicit copy constructors.

.. code-block:: cpp
Expand Down
50 changes: 50 additions & 0 deletions docs/source/_ext/ceinclude.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import base64
import json

from docutils import nodes
from sphinx.directives.code import LiteralInclude, container_wrapper
from os import path

class CeIncludeDirective(LiteralInclude):
""" LiteralInclude with a Compiler Explorer link"""

def run(self):
retnode = super().run()[0]

_, filename = self.env.relfn2path(self.arguments[0])
retnode = container_wrapper(self, retnode, path.basename(filename))

source = open(filename, "r").read()
client_state = {
"sessions": [
{
"id": 1,
"language": "c++",
"source": source,
"compilers": [
{
"id": "g132",
"options": "-O3",
"libs": [{'name': 'kokkos', 'ver': '4100'}],
}
],
}
]
}

encoded = base64.urlsafe_b64encode(json.dumps(client_state).encode())
ce_link = "https://godbolt.org/clientstate/" + encoded.decode()
doc_link = f'<a href="{ce_link}" target="_blank">Edit on Compiler Explorer</a>'

retnode += nodes.raw(rawsource = doc_link, text = doc_link, format="html")
return [retnode]


def setup(app):
app.add_directive("ceinclude", CeIncludeDirective)

return {
'version': '0.1',
'parallel_read_safe': True,
'parallel_write_safe': True,
}
3 changes: 2 additions & 1 deletion docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@
"sphinx.ext.viewcode",
"sphinx.ext.intersphinx",
"sphinx_copybutton",
"cppkokkos"]
"cppkokkos",
"ceinclude"]

# Add any paths that contain templates here, relative to this directory.
templates_path = ['_templates']
Expand Down