Skip to content

Commit

Permalink
Add make style target and refactor RAJA with clang-format
Browse files Browse the repository at this point in the history
  • Loading branch information
johnbowen42 committed Sep 4, 2024
1 parent 124c33b commit 4d92935
Show file tree
Hide file tree
Showing 722 changed files with 64,544 additions and 58,372 deletions.
5 changes: 3 additions & 2 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
BasedOnStyle : google
BasedOnStyle : LLVM
IndentWidth : 2
BreakBeforeBraces : Linux
KeepEmptyLinesAtTheStartOfBlocks : true
MaxEmptyLinesToKeep : 2
AccessModifierOffset : -2
DerivePointerAlignment: false
PointerAlignment: Left
UseTab: Never
AllowShortIfStatementsOnASingleLine : true
ConstructorInitializerAllOnOneLineOrOnePerLine : true
Expand All @@ -17,7 +19,6 @@ PenaltyBreakBeforeFirstCallParameter : 100
PenaltyReturnTypeOnItsOwnLine : 65000
PenaltyBreakString : 10

# These improve formatting results but require clang 3.6/7 or higher
BreakBeforeBinaryOperators : None
AlignAfterOpenBracket: true
BinPackArguments : false
Expand Down
5 changes: 4 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ project(RAJA LANGUAGES CXX C
VERSION ${RAJA_LOADED})

set(CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake/thirdparty" ${CMAKE_MODULE_PATH})

set(BLT_REQUIRED_CLANGFORMAT_VERSION "14" CACHE STRING "")
include(cmake/SetupRajaOptions.cmake)

cmake_minimum_required(VERSION 3.23)
Expand Down Expand Up @@ -136,6 +136,9 @@ include(cmake/SetupCompilers.cmake)
# Macros for building executables and libraries
include (cmake/RAJAMacros.cmake)

# Configure `style` target for enforcing code style
raja_add_code_checks()

set (raja_sources
src/AlignedRangeIndexSetBuilders.cpp
src/DepGraphNode.cpp
Expand Down
67 changes: 67 additions & 0 deletions cmake/RAJAMacros.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -204,3 +204,70 @@ macro(raja_add_benchmark)
NUM_OMP_THREADS ${arg_NUM_OMP_THREADS}
COMMAND ${TEST_DRIVER} ${arg_NAME})
endmacro(raja_add_benchmark)

##------------------------------------------------------------------------------
## raja_add_code_checks()
##
## Adds code checks for all source files recursively in the RAJA repository.
##
## This creates the following parent build targets:
## check - Runs a non file changing style check and CppCheck
## style - In-place code formatting
##
## Creates various child build targets that follow this pattern:
## raja_<check|style>
## raja_<cppcheck|clangformat>_<check|style>
##------------------------------------------------------------------------------
macro(raja_add_code_checks)

set(options)
set(singleValueArgs)
set(multiValueArgs)

# Parse the arguments to the macro
cmake_parse_arguments(arg
"${options}" "${singleValueArgs}" "${multiValueArgs}" ${ARGN})

# Only do code checks if building raja by itself and not included in
# another project
if ("${PROJECT_SOURCE_DIR}" STREQUAL "${CMAKE_SOURCE_DIR}")
# Create file globbing expressions that only include directories that contain source
set(_base_dirs "RAJA" "examples" "exercises" "benchmark" "include" "src" "test")
set(_ext_expressions "*.cpp" "*.hpp" "*.inl"
"*.cxx" "*.hxx" "*.cc" "*.c" "*.h" "*.hh")

set(_glob_expressions)
foreach(_exp ${_ext_expressions})
foreach(_base_dir ${_base_dirs})
list(APPEND _glob_expressions "${PROJECT_SOURCE_DIR}/${_base_dir}/${_exp}")
endforeach()
endforeach()

# Glob for list of files to run code checks on
set(_sources)
file(GLOB_RECURSE _sources ${_glob_expressions})

# Filter out exclusions
#set(_exclude_expressions
# "${PROJECT_SOURCE_DIR}/axom/sidre/examples/lulesh2/*"
# "${PROJECT_SOURCE_DIR}/axom/slam/examples/lulesh2.0.3/*"
# "${PROJECT_SOURCE_DIR}/axom/slam/examples/tinyHydro/*")
#foreach(_exp ${_exclude_expressions})
# list(FILTER _sources EXCLUDE REGEX ${_exp})
#endforeach()
#
blt_add_code_checks(PREFIX RAJA
SOURCES ${_sources}
CLANGFORMAT_CFG_FILE ${PROJECT_SOURCE_DIR}/.clang-format
CPPCHECK_FLAGS --enable=all --inconclusive)

# Set FOLDER property for code check targets
foreach(_suffix clangformat_check clangformat_style clang_tidy_check clang_tidy_style)
set(_tgt ${arg_PREFIX}_${_suffix})
if(TARGET ${_tgt})
set_target_properties(${_tgt} PROPERTIES FOLDER "RAJA/code_checks")
endif()
endforeach()
endif()

endmacro(raja_add_code_checks)
78 changes: 42 additions & 36 deletions examples/dynamic-forall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@
#include <cstring>
#include <iostream>

#include "memoryManager.hpp"

#include "RAJA/RAJA.hpp"
#include "memoryManager.hpp"

/*
* Vector Addition Example with dynamic policy selection
Expand All @@ -28,22 +27,25 @@
void checkResult(int* res, int len);
void printResult(int* res, int len);

using policy_list = camp::list<RAJA::seq_exec
,RAJA::simd_exec
using policy_list = camp::list<RAJA::seq_exec,
RAJA::simd_exec
#if defined(RAJA_ENABLE_OPENMP)
,RAJA::omp_parallel_for_exec
,
RAJA::omp_parallel_for_exec
#endif
#if defined(RAJA_ENABLE_CUDA)
,RAJA::cuda_exec<256>
,RAJA::cuda_exec<512>
,
RAJA::cuda_exec<256>,
RAJA::cuda_exec<512>
#endif
>;

int main(int argc, char *argv[])
int main(int argc, char* argv[])
{

if(argc != 2) {
RAJA_ABORT_OR_THROW("Usage ./dynamic-forall N, where N is the index of the policy to run");
if (argc != 2) {
RAJA_ABORT_OR_THROW("Usage ./dynamic-forall N, where N is the index of the "
"policy to run");
}

//
Expand All @@ -55,27 +57,27 @@ int main(int argc, char *argv[])
const int pol = std::stoi(argv[1]);

std::cout << "\n\nRAJA vector addition example...\n";
std::cout << "Using policy # "<<pol<<std::endl;
std::cout << "Using policy # " << pol << std::endl;

//
// Define vector length
//
//
// Define vector length
//
const int N = 1000000;

//
// Allocate and initialize vector data
//
int *a = memoryManager::allocate<int>(N);
int *b = memoryManager::allocate<int>(N);
int *c = memoryManager::allocate<int>(N);
//
// Allocate and initialize vector data
//
int* a = memoryManager::allocate<int>(N);
int* b = memoryManager::allocate<int>(N);
int* c = memoryManager::allocate<int>(N);

for (int i = 0; i < N; ++i) {
a[i] = -i;
b[i] = i;
}


//----------------------------------------------------------------------------//
//----------------------------------------------------------------------------//

std::cout << "\n Running C-style vector addition...\n";

Expand All @@ -86,27 +88,29 @@ int main(int argc, char *argv[])
// _cstyle_vector_add_end

checkResult(c, N);
//printResult(c, N);
// printResult(c, N);


//----------------------------------------------------------------------------//
// Example of dynamic policy selection for forall
//----------------------------------------------------------------------------//
//----------------------------------------------------------------------------//
// Example of dynamic policy selection for forall
//----------------------------------------------------------------------------//

//policy is chosen from the list
RAJA::expt::dynamic_forall<policy_list>(pol, RAJA::RangeSegment(0, N), [=] RAJA_HOST_DEVICE (int i) {
c[i] = a[i] + b[i];
});
// policy is chosen from the list
RAJA::expt::dynamic_forall<policy_list>(pol,
RAJA::RangeSegment(0, N),
[=] RAJA_HOST_DEVICE(int i) {
c[i] = a[i] + b[i];
});
// _rajaseq_vector_add_end

checkResult(c, N);
//printResult(c, N);
// printResult(c, N);


//----------------------------------------------------------------------------//
//
// Clean up.
//
//----------------------------------------------------------------------------//
//
// Clean up.
//
memoryManager::deallocate(a);
memoryManager::deallocate(b);
memoryManager::deallocate(c);
Expand All @@ -123,9 +127,11 @@ void checkResult(int* res, int len)
{
bool correct = true;
for (int i = 0; i < len; i++) {
if ( res[i] != 0 ) { correct = false; }
if (res[i] != 0) {
correct = false;
}
}
if ( correct ) {
if (correct) {
std::cout << "\n\t result -- PASS\n";
} else {
std::cout << "\n\t result -- FAIL\n";
Expand Down
Loading

0 comments on commit 4d92935

Please sign in to comment.