-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 74ca8ce
Showing
9 changed files
with
314 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
[submodule "blt"] | ||
path = blt | ||
url = https://github.com/LLNL/blt.git | ||
[submodule "tpl/raja"] | ||
path = tpl/raja | ||
url = https://github.com/llnl/raja.git |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
############################################################################### | ||
# Copyright (c) 2016-19, Lawrence Livermore National Security, LLC | ||
# and RAJA project contributors. See the RAJA/COPYRIGHT file for details. | ||
# | ||
# SPDX-License-Identifier: (BSD-3-Clause) | ||
############################################################################### | ||
cmake_minimum_required(VERSION 3.8) | ||
|
||
project( raja-example ) | ||
|
||
set (ENABLE_TESTS Off CACHE BOOL "") | ||
set (ENABLE_EXAMPLES Off CACHE BOOL "") | ||
set (ENABLE_REPRODUCERS Off CACHE BOOL "") | ||
set (ENABLE_EXERCISES Off CACHE BOOL "") | ||
set (ENABLE_DOCUMENTATION Off CACHE BOOL "") | ||
set (ENABLE_BENCHMARKS Off CACHE BOOL "") | ||
|
||
include(blt/SetupBLT.cmake) | ||
|
||
if (DEFINED RAJA_DIR) | ||
find_package(RAJA REQUIRED) | ||
blt_print_target_properties(TARGET RAJA) | ||
else () | ||
add_subdirectory(tpl/raja) | ||
endif () | ||
|
||
if (ENABLE_CUDA) | ||
set (CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} --expt-extended-lambda") | ||
endif () | ||
|
||
add_subdirectory(src) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
Copyright (c) 2019, Lawrence Livermore National Security, LLC. All rights | ||
reserved. | ||
|
||
Redistribution and use in source and binary forms, with or without | ||
modification, are permitted provided that the following conditions are met: | ||
|
||
* Redistributions of source code must retain the above copyright notice, this | ||
list of conditions and the following disclaimer. | ||
|
||
* Redistributions in binary form must reproduce the above copyright notice, | ||
this list of conditions and the following disclaimer in the documentation | ||
and/or other materials provided with the distribution. | ||
|
||
* Neither the name of the copyright holder nor the names of its | ||
contributors may be used to endorse or promote products derived from | ||
this software without specific prior written permission. | ||
|
||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE | ||
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | ||
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | ||
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | ||
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
This work was produced under the auspices of the U.S. Department of | ||
Energy by Lawrence Livermore National Laboratory under Contract | ||
DE-AC52-07NA27344. | ||
|
||
This work was prepared as an account of work sponsored by an agency of | ||
the United States Government. Neither the United States Government nor | ||
Lawrence Livermore National Security, LLC, nor any of their employees | ||
makes any warranty, expressed or implied, or assumes any legal liability | ||
or responsibility for the accuracy, completeness, or usefulness of any | ||
information, apparatus, product, or process disclosed, or represents that | ||
its use would not infringe privately owned rights. | ||
|
||
Reference herein to any specific commercial product, process, or service | ||
by trade name, trademark, manufacturer, or otherwise does not necessarily | ||
constitute or imply its endorsement, recommendation, or favoring by the | ||
United States Government or Lawrence Livermore National Security, LLC. | ||
|
||
The views and opinions of authors expressed herein do not necessarily | ||
state or reflect those of the United States Government or Lawrence | ||
Livermore National Security, LLC, and shall not be used for advertising | ||
or product endorsement purposes. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
# RAJA Project Template | ||
|
||
This project is a template that demonstrates how to use RAJA and BLT in an | ||
application project that uses CMake or Make. | ||
|
||
## Quick Start using CMake | ||
|
||
Clone this repository, and all the submodules: | ||
|
||
git clone --recursive https://github.com/llnl/raja-project-template | ||
|
||
Before we describe how to build the project, it is important to note that | ||
it requires out-of-source builds. | ||
|
||
To configure and build this project using a default compiler on your system, | ||
first create a build subdirectory in the top-level directory of this repo and | ||
then run CMake and make: | ||
|
||
mkdir build && cd build | ||
cmake ../ | ||
make | ||
|
||
This will create the binary `example.exe` in the `./bin` directory. | ||
|
||
When you run the executable, you will see that it runs a sequential CPU kernel. | ||
You can also run an OpenMP multithreaded CPU kernel or a CUDA GPU kernel by | ||
enabling those features when you run CMake, specifically, passing the following | ||
options to CMake: | ||
|
||
- `-DENABLE_OPENMP=On` will enable the OpenMP back-end (RAJA default: `On`) | ||
- `-DENABLE_CUDA=On` will enable the CUDA back-end (RAJA default: `Off`) | ||
|
||
If you want to experiment with RAJA before trying it in your application, | ||
you can modify the file `./src/example.cpp`, and rebuild the code by running | ||
`make` in the `build` directory you created earlier. | ||
|
||
## Using CMake and an Installed Version of RAJA | ||
|
||
This project can also be configured to use a pre-installed version of RAJA. | ||
This is the recommended method for using RAJA in most applications. Please | ||
see the [RAJA documentation]() for details on building and installing RAJA. | ||
|
||
Once you have RAJA installed, configure the project by specifying the RAJA | ||
location using the CMake option `RAJA_DIR`: | ||
|
||
cmake -DRAJA_DIR=<path to RAJA install directory>/share/raja/cmake ../ | ||
|
||
Then build as before: | ||
|
||
make | ||
|
||
If you are building your application against an installed version of RAJA, | ||
it's important to make sure that the options you passed to CMake to build | ||
RAJA match the options you use to build this project, apart from the one | ||
indicated above. | ||
|
||
## Using RAJA with Make | ||
|
||
To use RAJA in a project with a make-based build system, you need to add the | ||
`include` directory where RAJA is installed to your compile flags and | ||
link against the installed RAJA library. You must also ensure that you add | ||
the appropriate flags for any RAJA features you have enabled (e.g. `-fopenmp` | ||
or equivalent if you built RAJA with OpenMP enabled). | ||
|
||
For completeness, to compile and link the example in this project using the g++ | ||
compiler, you could do the following on the command line: | ||
|
||
g++ -I <path to RAJA install directory>/include -std=c++11 -fopenmp ./src/example.cpp -o example.exe <path to RAJA install directory>/lib/libRAJA.a | ||
|
||
Since most applications contain more than one source file, you probably want | ||
to create a Makefile to use to build your project. Here are the contents of a | ||
simple Makefile that builds the example in this project: | ||
|
||
CXX=<compiler executable> | ||
CXXFLAGS=-I$(INC_DIR) -std=c++11 -fopenmp | ||
|
||
INC_DIR =<path to RAJA install directory>/include | ||
LIB_DIR =<path to RAJA install directory>/include/lib | ||
|
||
LIBS=-lRAJA | ||
|
||
SRC_DIR=./src | ||
OBJ_DIR=$(SRC_DIR) | ||
|
||
OBJ = example.o | ||
|
||
$(OBJ_DIR)/%.o: $(SRC_DIR)/%.cpp | ||
$(CXX) -c -o $@ $< $(CXXFLAGS) | ||
|
||
example.exe: $(OBJ_DIR)/$(OBJ) | ||
$(CXX) -o $@ $^ $(CXXFLAGS) -L $(LIB_DIR) $(LIBS) | ||
|
||
.PHONY: clean | ||
|
||
clean: | ||
rm -f $(OBJ_DIR)/*.o example.exe | ||
|
||
To try it out, you can copy these lines into a file called Makefile in the | ||
top-level project directory and type 'make'. The executable `example.exe` will | ||
be generated in the same directory. | ||
|
||
## Next Steps | ||
|
||
- For more information on RAJA, check out the RAJA | ||
[tutorial](https://raja.readthedocs.io/en/master) | ||
- For more information on using BLT and CMake, check out the BLT | ||
[tutorial](https://llnl-blt.readthedocs.io/en/develop) | ||
|
||
If you have questions, comments or ideas, please join the RAJA mailing list on | ||
Google Groups [here](https://groups.google.com/forum/#!forum/raja-users). | ||
|
||
## License | ||
|
||
RAJA is licensed under the BSD 3-Clause license, (BSD-3-Clause or | ||
https://opensource.org/licenses/BSD-3-Clause). | ||
|
||
Copyrights and patents in the RAJA project are retained by contributors. No | ||
copyright assignment is required to contribute to RAJA. | ||
|
||
Unlimited Open Source - BSD 3-clause Distribution | ||
`LLNL-CODE-689114` `OCEC-16-063` | ||
|
||
## SPDX usage | ||
|
||
Individual files contain SPDX tags instead of the full license text. | ||
This enables machine processing of license information based on the SPDX | ||
License Identifiers that are available here: https://spdx.org/licenses/ | ||
|
||
Files that are licensed as BSD 3-Clause contain the following | ||
text in the license header: | ||
|
||
SPDX-License-Identifier: (BSD-3-Clause) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
############################################################################### | ||
# Copyright (c) 2016-19, Lawrence Livermore National Security, LLC | ||
# and RAJA project contributors. See the RAJA/COPYRIGHT file for details. | ||
# | ||
# SPDX-License-Identifier: (BSD-3-Clause) | ||
############################################################################### | ||
|
||
set(example_depends RAJA) | ||
|
||
if (ENABLE_OPENMP) | ||
set(example_depends | ||
${example_depends} | ||
openmp) | ||
endif () | ||
|
||
if (ENABLE_CUDA) | ||
set(example_depends | ||
${example_depends} | ||
cuda) | ||
endif () | ||
|
||
blt_add_executable( | ||
NAME example.exe | ||
SOURCES example.cpp | ||
DEPENDS_ON ${example_depends}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
////////////////////////////////////////////////////////////////////////////// | ||
// Copyright (c) 2016-19, Lawrence Livermore National Security, LLC | ||
// and RAJA project contributors. See the RAJA/COPYRIGHT file for details. | ||
// | ||
// SPDX-License-Identifier: (BSD-3-Clause) | ||
////////////////////////////////////////////////////////////////////////////// | ||
#include "RAJA/RAJA.hpp" | ||
|
||
template <typename T> | ||
T *allocate(std::size_t size) | ||
{ | ||
T *ptr; | ||
#if defined(RAJA_ENABLE_CUDA) | ||
cudaErrchk( | ||
cudaMallocManaged((void **)&ptr, sizeof(T) * size, cudaMemAttachGlobal)); | ||
#else | ||
ptr = new T[size]; | ||
#endif | ||
return ptr; | ||
} | ||
|
||
template <typename T> | ||
void deallocate(T *&ptr) | ||
{ | ||
if (ptr) { | ||
#if defined(RAJA_ENABLE_CUDA) | ||
cudaErrchk(cudaFree(ptr)); | ||
#else | ||
delete[] ptr; | ||
#endif | ||
ptr = nullptr; | ||
} | ||
} | ||
|
||
int main(int RAJA_UNUSED_ARG(argc), char** RAJA_UNUSED_ARG(argv[])) | ||
{ | ||
#if defined(RAJA_ENABLE_CUDA) | ||
using policy = RAJA::cuda_exec<256>; | ||
const std::string policy_name = "CUDA"; | ||
#elif defined(RAJA_ENABLE_OPENMP) | ||
using policy = RAJA::omp_parallel_for_exec; | ||
const std::string policy_name = "OpenMP"; | ||
#else | ||
using policy = RAJA::seq_exec; | ||
const std::string policy_name = "sequential"; | ||
#endif | ||
|
||
std::cout << "Running vector addition with RAJA using the " << policy_name << " backend..."; | ||
|
||
constexpr int N = 1000000; | ||
|
||
int *a = allocate<int>(N); | ||
int *b = allocate<int>(N); | ||
int *c = allocate<int>(N); | ||
|
||
RAJA::forall<policy>(RAJA::RangeSegment(0, N), [=] RAJA_HOST_DEVICE (int i) { | ||
a[i] = -i; | ||
b[i] = i; | ||
}); | ||
|
||
RAJA::forall<policy>(RAJA::RangeSegment(0, N), [=] RAJA_HOST_DEVICE (int i) { | ||
c[i] = a[i] + b[i]; | ||
}); | ||
|
||
std::cout << "done." << std::endl; | ||
|
||
deallocate(a); | ||
deallocate(b); | ||
deallocate(c); | ||
} |