Add CMake build system
ADD
CMakeLists.txt
+# MIT License
+#
+# Copyright (c) 2018 Advanced Micro Devices, Inc. All rights reserved.
+#
+# Permission is hereby granted, free of charge, to any person obtaining a copy
+# of this software and associated documentation files (the "Software"), to deal
+# in the Software without restriction, including without limitation the rights
+# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+# copies of the Software, and to permit persons to whom the Software is
+# furnished to do so, subject to the following conditions:
+#
+# The above copyright notice and this permission notice shall be included in all
+# copies or substantial portions of the Software.
+#
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+# SOFTWARE.
+
+cmake_minimum_required(VERSION 3.5.1 FATAL_ERROR)
+
+project(rocprim VERSION 0.0.1.0 LANGUAGES CXX)
+
+# CMake modules
+list(APPEND CMAKE_MODULE_PATH
+ ${CMAKE_CURRENT_SOURCE_DIR}/cmake
+ ${HIP_PATH}/cmake /opt/rocm/hip/cmake # FindHIP.cmake
+)
+
+# Set a default build type if none was specified
+if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
+ message(STATUS "Setting build type to 'Release' as none was specified.")
+ set(CMAKE_BUILD_TYPE "Release" CACHE STRING "Choose the type of build." FORCE)
+ set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "" "Debug" "Release" "MinSizeRel" "RelWithDebInfo")
+endif()
+
+set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE CACHE BOOLEAN "Add paths to linker search and installed rpath")
+
+# Verify that hcc compiler is used on ROCM platform
+include(cmake/VerifyCompiler.cmake)
+
+# Set CXX flags
+set(CMAKE_CXX_STANDARD 11)
+set(CMAKE_CXX_STANDARD_REQUIRED ON)
+set(CMAKE_CXX_EXTENSIONS OFF)
+set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Werror")
+
+# Build options
+option(BUILD_TEST "Build tests" ON)
+
+# Get dependencies
+include(cmake/Dependencies.cmake)
+
+# Print configuration summary
+include(cmake/Summary.cmake)
+print_configuration_summary()
+
+# AMD targets
+set(AMDGPU_TARGETS gfx803;gfx900 CACHE STRING "List of specific machine types for library to target")
+
+# Thrust (with HIP backend)
+add_subdirectory(thrust)
+
+# Tests
+if(BUILD_TEST)
+ enable_testing()
+ add_subdirectory(test)
+endif()

ADD
cmake/Dependencies.cmake
+# MIT License
+#
+# Copyright (c) 2018 Advanced Micro Devices, Inc. All rights reserved.
+#
+# Permission is hereby granted, free of charge, to any person obtaining a copy
+# of this software and associated documentation files (the "Software"), to deal
+# in the Software without restriction, including without limitation the rights
+# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+# copies of the Software, and to permit persons to whom the Software is
+# furnished to do so, subject to the following conditions:
+#
+# The above copyright notice and this permission notice shall be included in all
+# copies or substantial portions of the Software.
+#
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+# SOFTWARE.
+
+# ###########################
+# rocPRIM dependencies
+# ###########################
+
+# HIP dependency is handled earlier in the project cmake file
+# when VerifyCompiler.cmake is included.
+
+# For downloading, building, and installing required dependencies
+include(cmake/DownloadProject.cmake)
+
+# Never update automatically from the remote repository
+if(CMAKE_VERSION VERSION_LESS 3.2)
+ set(UPDATE_DISCONNECTED_IF_AVAILABLE "")
+else()
+ set(UPDATE_DISCONNECTED_IF_AVAILABLE "UPDATE_DISCONNECTED TRUE")
+endif()
+
+# GIT
+find_package(Git REQUIRED)
+if (NOT Git_FOUND)
+ message(FATAL_ERROR "Please ensure Git is installed on the system")
+endif()
+
+# Test dependencies
+if(BUILD_TEST)
+ # Google Test (https://github.com/google/googletest)
+ message(STATUS "Downloading and building GTest.")
+ set(GTEST_ROOT ${CMAKE_CURRENT_BINARY_DIR}/gtest CACHE PATH "")
+ download_project(
+ PROJ googletest
+ GIT_REPOSITORY https://github.com/google/googletest.git
+ GIT_TAG master
+ INSTALL_DIR ${GTEST_ROOT}
+ CMAKE_ARGS -DBUILD_GTEST=ON -DINSTALL_GTEST=ON -Dgtest_force_shared_crt=ON -DBUILD_SHARED_LIBS=ON -DCMAKE_INSTALL_PREFIX=<INSTALL_DIR>
+ LOG_DOWNLOAD TRUE
+ LOG_CONFIGURE TRUE
+ LOG_BUILD TRUE
+ LOG_INSTALL TRUE
+ BUILD_PROJECT TRUE
+ ${UPDATE_DISCONNECTED_IF_AVAILABLE}
+ )
+ find_package(GTest REQUIRED)
+endif()

ADD
cmake/DownloadProject.CMakeLists.cmake.in
+# Distributed under the OSI-approved MIT License. See accompanying
+# file LICENSE or https://github.com/Crascit/DownloadProject for details.
+
+cmake_minimum_required(VERSION 2.8.2)
+
+project(${DL_ARGS_PROJ}-download NONE)
+
+include(ExternalProject)
+if(${DL_ARGS_BUILD_PROJECT})
+ ExternalProject_Add(${DL_ARGS_PROJ}-download
+ ${DL_ARGS_UNPARSED_ARGUMENTS}
+ SOURCE_DIR "${DL_ARGS_SOURCE_DIR}"
+ BUILD_IN_SOURCE TRUE
+ TEST_COMMAND ""
+ )
+else()
+ ExternalProject_Add(${DL_ARGS_PROJ}-download
+ ${DL_ARGS_UNPARSED_ARGUMENTS}
+ SOURCE_DIR "${DL_ARGS_SOURCE_DIR}"
+ BUILD_IN_SOURCE TRUE
+ TEST_COMMAND ""
+ UPDATE_COMMAND ""
+ CONFIGURE_COMMAND ""
+ BUILD_COMMAND ""
+ INSTALL_COMMAND ""
+ )
+endif()

ADD
cmake/DownloadProject.cmake
+# Distributed under the OSI-approved MIT License. See accompanying
+# file LICENSE or https://github.com/Crascit/DownloadProject for details.
+#
+# MODULE: DownloadProject
+#
+# PROVIDES:
+# download_project( PROJ projectName
+# [PREFIX prefixDir]
+# [DOWNLOAD_DIR downloadDir]
+# [SOURCE_DIR srcDir]
+# [BINARY_DIR binDir]
+# [QUIET]
+# ...
+# )
+#
+# Provides the ability to download and unpack a tarball, zip file, git repository,
+# etc. at configure time (i.e. when the cmake command is run). How the downloaded
+# and unpacked contents are used is up to the caller, but the motivating case is
+# to download source code which can then be included directly in the build with
+# add_subdirectory() after the call to download_project(). Source and build
+# directories are set up with this in mind.
+#
+# The PROJ argument is required. The projectName value will be used to construct
+# the following variables upon exit (obviously replace projectName with its actual
+# value):
+#
+# projectName_SOURCE_DIR
+# projectName_BINARY_DIR
+#
+# The SOURCE_DIR and BINARY_DIR arguments are optional and would not typically
+# need to be provided. They can be specified if you want the downloaded source
+# and build directories to be located in a specific place. The contents of
+# projectName_SOURCE_DIR and projectName_BINARY_DIR will be populated with the
+# locations used whether you provide SOURCE_DIR/BINARY_DIR or not.
+#
+# The DOWNLOAD_DIR argument does not normally need to be set. It controls the
+# location of the temporary CMake build used to perform the download.
+#
+# The PREFIX argument can be provided to change the base location of the default
+# values of DOWNLOAD_DIR, SOURCE_DIR and BINARY_DIR. If all of those three arguments
+# are provided, then PREFIX will have no effect. The default value for PREFIX is
+# CMAKE_BINARY_DIR.
+#
+# The QUIET option can be given if you do not want to show the output associated
+# with downloading the specified project.
+#
+# In addition to the above, any other options are passed through unmodified to
+# ExternalProject_Add() to perform the actual download, patch and update steps.
+#
+# Only those ExternalProject_Add() arguments which relate to downloading, patching
+# and updating of the project sources are intended to be used. Also note that at
+# least one set of download-related arguments are required.
+#
+# If using CMake 3.2 or later, the UPDATE_DISCONNECTED option can be used to
+# prevent a check at the remote end for changes every time CMake is run
+# after the first successful download. See the documentation of the ExternalProject
+# module for more information. It is likely you will want to use this option if it
+# is available to you. Note, however, that the ExternalProject implementation contains
+# bugs which result in incorrect handling of the UPDATE_DISCONNECTED option when
+# using the URL download method or when specifying a SOURCE_DIR with no download
+# method. Fixes for these have been created, the last of which is scheduled for
+# inclusion in CMake 3.8.0. Details can be found here:
+#
+# https://gitlab.kitware.com/cmake/cmake/commit/bdca68388bd57f8302d3c1d83d691034b7ffa70c
+# https://gitlab.kitware.com/cmake/cmake/issues/16428
+#
+# If you experience build errors related to the update step, consider avoiding
+# the use of UPDATE_DISCONNECTED.
+#
+# EXAMPLE USAGE:
+#
+# include(DownloadProject)
+# download_project(PROJ googletest
+# GIT_REPOSITORY https://github.com/google/googletest.git
+# GIT_TAG master
+# UPDATE_DISCONNECTED 1
+# QUIET
+# )
+#
+# add_subdirectory(${googletest_SOURCE_DIR} ${googletest_BINARY_DIR})
+#
+#========================================================================================
+
+
+set(_DownloadProjectDir "${CMAKE_CURRENT_LIST_DIR}")
+
+include(CMakeParseArguments)
+
+function(download_project)
+
+ set(options QUIET)
+ set(oneValueArgs
+ PROJ
+ PREFIX
+ DOWNLOAD_DIR
+ SOURCE_DIR
+ BINARY_DIR
+ BUILD_PROJECT
+ )
+ set(multiValueArgs "")
+
+ cmake_parse_arguments(DL_ARGS "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
+
+ # Hide output if requested
+ if (DL_ARGS_QUIET)
+ set(OUTPUT_QUIET "OUTPUT_QUIET")
+ else()
+ unset(OUTPUT_QUIET)
+ message(STATUS "Downloading/updating ${DL_ARGS_PROJ}")
+ endif()
+
+ # Set up where we will put our temporary CMakeLists.txt file and also
+ # the base point below which the default source and binary dirs will be.
+ # The prefix must always be an absolute path.
+ if (NOT DL_ARGS_PREFIX)
+ set(DL_ARGS_PREFIX "${CMAKE_BINARY_DIR}")
+ else()
+ get_filename_component(DL_ARGS_PREFIX "${DL_ARGS_PREFIX}" ABSOLUTE
+ BASE_DIR "${CMAKE_CURRENT_BINARY_DIR}")
+ endif()
+ if (NOT DL_ARGS_DOWNLOAD_DIR)
+ set(DL_ARGS_DOWNLOAD_DIR "${DL_ARGS_PREFIX}/${DL_ARGS_PROJ}-download")
+ endif()
+
+ # Ensure the caller can know where to find the source and build directories
+ if (NOT DL_ARGS_SOURCE_DIR)
+ set(DL_ARGS_SOURCE_DIR "${DL_ARGS_PREFIX}/${DL_ARGS_PROJ}-src")
+ endif()
+ if (NOT DL_ARGS_BINARY_DIR)
+ set(DL_ARGS_BINARY_DIR "${DL_ARGS_PREFIX}/${DL_ARGS_PROJ}-build")
+ endif()
+ set(${DL_ARGS_PROJ}_SOURCE_DIR "${DL_ARGS_SOURCE_DIR}" PARENT_SCOPE)
+ set(${DL_ARGS_PROJ}_BINARY_DIR "${DL_ARGS_BINARY_DIR}" PARENT_SCOPE)
+
+ # The way that CLion manages multiple configurations, it causes a copy of
+ # the CMakeCache.txt to be copied across due to it not expecting there to
+ # be a project within a project. This causes the hard-coded paths in the
+ # cache to be copied and builds to fail. To mitigate this, we simply
+ # remove the cache if it exists before we configure the new project. It
+ # is safe to do so because it will be re-generated. Since this is only
+ # executed at the configure step, it should not cause additional builds or
+ # downloads.
+ file(REMOVE "${DL_ARGS_DOWNLOAD_DIR}/CMakeCache.txt")
+
+ # Create and build a separate CMake project to carry out the download.
+ # If we've already previously done these steps, they will not cause
+ # anything to be updated, so extra rebuilds of the project won't occur.
+ # Make sure to pass through CMAKE_MAKE_PROGRAM in case the main project
+ # has this set to something not findable on the PATH.
+ configure_file("${_DownloadProjectDir}/DownloadProject.CMakeLists.cmake.in"
+ "${DL_ARGS_DOWNLOAD_DIR}/CMakeLists.txt")
+ execute_process(COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}"
+ -D "CMAKE_MAKE_PROGRAM:FILE=${CMAKE_MAKE_PROGRAM}"
+ .
+ RESULT_VARIABLE result
+ ${OUTPUT_QUIET}
+ WORKING_DIRECTORY "${DL_ARGS_DOWNLOAD_DIR}"
+ )
+ if(result)
+ message(FATAL_ERROR "CMake step for ${DL_ARGS_PROJ} failed: ${result}")
+ endif()
+ execute_process(COMMAND ${CMAKE_COMMAND} --build .
+ RESULT_VARIABLE result
+ ${OUTPUT_QUIET}
+ WORKING_DIRECTORY "${DL_ARGS_DOWNLOAD_DIR}"
+ )
+ if(result)
+ message(FATAL_ERROR "Build step for ${DL_ARGS_PROJ} failed: ${result}")
+ endif()
+endfunction()

ADD
cmake/Summary.cmake
+# MIT License
+#
+# Copyright (c) 2018 Advanced Micro Devices, Inc. All rights reserved.
+#
+# Permission is hereby granted, free of charge, to any person obtaining a copy
+# of this software and associated documentation files (the "Software"), to deal
+# in the Software without restriction, including without limitation the rights
+# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+# copies of the Software, and to permit persons to whom the Software is
+# furnished to do so, subject to the following conditions:
+#
+# The above copyright notice and this permission notice shall be included in all
+# copies or substantial portions of the Software.
+#
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+# SOFTWARE.
+
+function(print_configuration_summary)
+ message(STATUS "******** Summary ********")
+ message(STATUS "General:")
+ message(STATUS " System : ${CMAKE_SYSTEM_NAME}")
+ message(STATUS " HIP ROOT : ${HIP_ROOT_DIR}")
+ message(STATUS " C++ compiler : ${CMAKE_CXX_COMPILER}")
+ message(STATUS " C++ compiler version : ${CMAKE_CXX_COMPILER_VERSION}")
+ string(STRIP "${CMAKE_CXX_FLAGS}" CMAKE_CXX_FLAGS_STRIP)
+ message(STATUS " CXX flags : ${CMAKE_CXX_FLAGS_STRIP}")
+ message(STATUS " Build type : ${CMAKE_BUILD_TYPE}")
+ message(STATUS " Install prefix : ${CMAKE_INSTALL_PREFIX}")
+ message(STATUS "")
+ message(STATUS " BUILD_TEST : ${BUILD_TEST}")
+endfunction()

ADD
cmake/VerifyCompiler.cmake
+# MIT License
+#
+# Copyright (c) 2018 Advanced Micro Devices, Inc. All rights reserved.
+#
+# Permission is hereby granted, free of charge, to any person obtaining a copy
+# of this software and associated documentation files (the "Software"), to deal
+# in the Software without restriction, including without limitation the rights
+# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+# copies of the Software, and to permit persons to whom the Software is
+# furnished to do so, subject to the following conditions:
+#
+# The above copyright notice and this permission notice shall be included in all
+# copies or substantial portions of the Software.
+#
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+# SOFTWARE.
+
+# Find HIP package and verify that correct C++ compiler was selected for available
+# platfrom. On ROCm platform host and device code is compiled by the same compiler:
+# hcc. On CUDA host can be compiled by any C++ compiler while device code is compiled
+# by nvcc compiler (CMake's CUDA package handles this).
+
+# Find HIP package
+find_package(HIP REQUIRED)
+
+if(HIP_PLATFORM STREQUAL "nvcc")
+ message(FATAL_ERROR "`nvcc` is not supported.")
+elseif(HIP_PLATFORM STREQUAL "hcc")
+ if(NOT (CMAKE_CXX_COMPILER MATCHES ".*/hcc$"))
+ message(FATAL_ERROR "On ROCm platform 'hcc' must be used as C++ compiler.")
+ else()
+ # Workaround until hcc & hip cmake modules fixes symlink logic in their config files.
+ # (Thanks to rocBLAS devs for finding workaround for this problem.)
+ list(APPEND CMAKE_PREFIX_PATH /opt/rocm/hcc /opt/rocm/hip)
+ # Ignore hcc warning: argument unused during compilation: '-isystem /opt/rocm/hip/include'
+ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-unused-command-line-argument")
+ find_package(hcc REQUIRED CONFIG PATHS /opt/rocm)
+ find_package(hip REQUIRED CONFIG PATHS /opt/rocm)
+ endif()
+else()
+ message(FATAL_ERROR "HIP_PLATFORM must be 'hcc' (AMD ROCm platform).")
+endif()

ADD
thrust/CMakeLists.txt
+# MIT License
+#
+# Copyright (c) 2018 Advanced Micro Devices, Inc. All rights reserved.
+#
+# Permission is hereby granted, free of charge, to any person obtaining a copy
+# of this software and associated documentation files (the "Software"), to deal
+# in the Software without restriction, including without limitation the rights
+# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+# copies of the Software, and to permit persons to whom the Software is
+# furnished to do so, subject to the following conditions:
+#
+# The above copyright notice and this permission notice shall be included in all
+# copies or substantial portions of the Software.
+#
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+# SOFTWARE.
+
+# Only header target, does not include dependencies
+add_library(thrust_headers INTERFACE)
+target_include_directories(thrust_headers
+ INTERFACE
+ $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/..>
+)
+
+add_library(hipthrust INTERFACE)
+target_link_libraries(hipthrust
+ INTERFACE
+ thrust_headers
+ hcc::hccrt
+ hcc::hc_am
+)
+# Remove this check when we no longer build with older rocm stack(ie < 1.8.2)
+if(TARGET hip::device)
+ target_link_libraries(hipthrust INTERFACE hip::device)
+else()
+ target_link_libraries(hipthrust INTERFACE hip::hip_hcc hip::hip_device)
+endif()
+
+# We will use rocPRIM HIP API
+target_compile_definitions(hipthrust
+ INTERFACE
+ ROCPRIM_HIP_API=1
+)

Add CMake build system