-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCMakeLists.txt
169 lines (133 loc) · 6.21 KB
/
CMakeLists.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
cmake_minimum_required(VERSION 3.21) # HIP language support requires 3.21 or greater
cmake_policy(VERSION 3.21...3.27)
# Make sure ROCM_PATH is defined
if(NOT DEFINED ROCM_PATH)
if(NOT DEFINED ENV{ROCM_PATH})
set(ROCM_PATH "/opt/rocm/" CACHE PATH "Path to which ROCm has been installed")
else()
set(ROCM_PATH $ENV{ROCM_PATH} CACHE PATH "Path to which ROCm has been installed")
endif()
endif()
if(NOT DEFINED HIPFORT_ROOT)
if(NOT DEFINED ENV{HIPFORT_ROOT})
set(HIPFORT_ROOT "/opt/rocm/" CACHE PATH "Path to which Hipfort has been installed")
else()
set(HIPFORT_ROOT $ENV{HIPFORT_ROOT} CACHE PATH "Path to which Hipfort has been installed")
endif()
endif()
project(hipfort_course VERSION 1.0.0
DESCRIPTION "Example codes to accompany a course in hipfort"
LANGUAGES C CXX Fortran
)
# Default Fortran compiler flags
# Fortran compiler flags
if( "${CMAKE_Fortran_COMPILER_ID}" STREQUAL "GNU" )
set( CMAKE_Fortran_FLAGS "-cpp -ffree-line-length-512 -fPIC -Wno-missing-include-dirs" )
set( CMAKE_Fortran_FLAGS_DEBUG "-g -O1 -C -Wall -fbounds-check -fbacktrace -ffpe-trap=invalid,zero,overflow" )
set( CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -lgfortran -static-libstdc++")
set( CMAKE_Fortran_FLAGS_COVERAGE "${CMAKE_Fortran_FLAGS_DEBUG} --coverage")
set( CMAKE_Fortran_FLAGS_PROFILE "-pg -O3")
set( CMAKE_Fortran_FLAGS_RELEASE "-O3 -g" )
endif()
if( "${CMAKE_Fortran_COMPILER_ID}" STREQUAL "Cray" )
set( CMAKE_Fortran_FLAGS "-eZ -eT -ef -ffree -fPIC" )
set( CMAKE_Fortran_FLAGS_DEBUG "-g -O1 -h bounds" )
#set( CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -lgfortran")
set( CMAKE_Fortran_FLAGS_COVERAGE "${CMAKE_Fortran_FLAGS_DEBUG} --coverage")
set( CMAKE_Fortran_FLAGS_PROFILE "-pg -O3")
set( CMAKE_Fortran_FLAGS_RELEASE "-O3 -g" )
endif()
# ------ dependencies ------ #
# HIP
find_package(hip REQUIRED)
# Choose to use compilation for NVIDIA or AMD
if ("$ENV{HIP_PLATFORM}" STREQUAL "nvidia")
message("Performing compilation for an NVIDIA backend.")
# Set GPU architectures
if (DEFINED ENV{GPU_ARCH})
# Read GPU_ARCH environment variable into a CMake variable
set(gpu_arch_list $ENV{GPU_ARCH})
# Set NVIDIA architectures to compile for
set(CMAKE_CUDA_ARCHITECTURES ${gpu_arch_list})
# Add compiler flags if hipfc is used
if(CMAKE_Fortran_COMPILER MATCHES "hipfc$")
foreach(arch IN LISTS gpu_arch_list)
set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} --offload-arch=sm_${arch}")
endforeach()
endif()
endif()
# Enable the CUDA language
enable_language(CUDA)
# Set separable compilation on
set(CUDA_SEPARABLE_COMPILATION ON)
## Find the CUDA toolkit,
## it must be present if we use HIP_PLATFORM=nvidia
find_package(CUDAToolkit REQUIRED)
# Set the kernel language for device code
set(kernel_lang CUDA)
# Set compiler flags
set(CMAKE_CUDA_FLAGS "-g -Xcompiler -fPIC")
set(CMAKE_CUDA_FLAGS_DEBUG "-G -O0")
set(CMAKE_CUDA_FLAGS_PROFILE "-pg -O3")
set(CMAKE_CUDA_FLAGS_RELEASE "-O3")
## Change global preprocessor defintions for CUDA sources
## Remove any preprocessor definitions for AMD
remove_definitions(-D__HIP_PLATFORM_HCC__ -D__HIP_PLATFORM_AMD__)
## Replace it with CUDA precprocessor definitions
add_definitions(-D__HIP_PLATFORM_NVCC__ -D__HIP_PLATFORM_NVIDIA__)
# Locate the hipfort library
find_library(HIPFORT_LIBRARIES NAMES libhipfort-nvptx.a HINTS ENV HIPFORT_ROOT PATH_SUFFIXES lib/ REQUIRED)
find_path(HIPFORT_INCLUDE_DIRS NAMES hipfort.mod HIPFORT.mod HINTS ENV HIPFORT_ROOT PATH_SUFFIXES include/hipfort/nvptx/ REQUIRED)
## Set libraries to link to for device code
set(kernel_libs ${HIPFORT_LIBRARIES} CUDA::cuda_driver CUDA::cudart)
include_directories(${HIPFORT_INCLUDE_DIRS})
else()
message("Performing compilation for an AMD backend.")
# Enable the HIP language
enable_language(HIP)
# Set language for device code
set(kernel_lang HIP)
# Set GPU architectures
if (DEFINED ENV{GPU_ARCH})
# Read GPU_ARCH environment variable into a CMake variable
set(gpu_arch_list $ENV{GPU_ARCH})
# Set NVIDIA architectures to compile for
set(CMAKE_HIP_ARCHITECTURES ${gpu_arch_list})
# Add compiler flags if hipfc is used
if(CMAKE_Fortran_COMPILER MATCHES "hipfc$")
foreach(arch IN LISTS gpu_arch_list)
set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} --offload-arch=${arch}")
endforeach()
endif()
endif()
# Set HIP flags
set(CMAKE_HIP_FLAGS "${CMAKE_HIP_FLAGS} -g")
set(CMAKE_HIP_FLAGS_DEBUG, "-ggdb -O1")
set(CMAKE_HIP_FLAGS_PROFILE, "-pg -O3")
set(CMAKE_HIP_FLAGS_RELEASE, "-O3")
# Locate the hipfort library
find_library(HIPFORT_LIBRARIES NAMES libhipfort-amdgcn.a HINTS ENV HIPFORT_ROOT PATH_SUFFIXES lib/ REQUIRED)
find_path(HIPFORT_INCLUDE_DIRS NAMES hipfort.mod HIPFORT.mod HINTS ENV HIPFORT_ROOT PATH_SUFFIXES include/hipfort/amdgcn/ REQUIRED)
## Set libraries to link to for device code
set(kernel_libs ${HIPFORT_LIBRARIES} hip::device)
include_directories(${HIPFORT_INCLUDE_DIRS})
endif()
## Debugging and display
##get_cmake_property(_variableNames VARIABLES)
##list (SORT _variableNames)
##foreach (_variableName ${_variableNames})
## message(STATUS "${_variableName}=${${_variableName}}")
##endforeach()
# Set the place to put all built Fortran module files
set(CMAKE_Fortran_MODULE_DIRECTORY ${CMAKE_BINARY_DIR}/mod)
# Set the location of the course material
set(COURSE_MATERIAL_DIR ${CMAKE_SOURCE_DIR}/course_material)
# Directories
add_subdirectory(${COURSE_MATERIAL_DIR}/src)
add_subdirectory(${COURSE_MATERIAL_DIR}/L1_Fortran_Refresher)
add_subdirectory(${COURSE_MATERIAL_DIR}/L5_hipFORT_Examples)
add_subdirectory(${COURSE_MATERIAL_DIR}/Exercises/E1_Chessboard_CPU)
add_subdirectory(${COURSE_MATERIAL_DIR}/Exercises/E2_Chessboard_GPU)
add_subdirectory(${COURSE_MATERIAL_DIR}/Exercises/E3_Managed_Memory)
add_subdirectory(${COURSE_MATERIAL_DIR}/L6_Memory_Management)
add_subdirectory(${COURSE_MATERIAL_DIR}/L6_Memory_Management/memcpy_benchmark)