Skip to content

Commit c6a4420

Browse files
authored
Add hipSPARSELt examples
Signed-off-by: Jan Stephan <[email protected]>
1 parent ef24264 commit c6a4420

File tree

18 files changed

+1249
-1
lines changed

18 files changed

+1249
-1
lines changed

.github/workflows/build_libraries.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ jobs:
4949
rocthrust-dev \
5050
hipblaslt-dev \
5151
hiptensor-dev \
52-
rocwmma-dev
52+
rocwmma-dev \
53+
hipsparselt-dev
5354
rm -rf /var/lib/apt/lists/*
5455
echo "/opt/rocm/bin" >> $GITHUB_PATH
5556
echo "ROCM_PATH=/opt/rocm" >> $GITHUB_ENV

.github/workflows/build_libraries_cuda.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,18 @@ jobs:
7373
cd Libraries/hipFFT
7474
make GPU_RUNTIME=CUDA -j
7575
76+
- name: Install hipSPARSELt
77+
shell: bash
78+
run: |
79+
apt-get install -y hipsparselt-dev
80+
81+
- name: Build hipSPARSELt examples
82+
shell: bash
83+
run: |
84+
cd Libraries/hipSPARSELt
85+
make GPU_RUNTIME=CUDA -j
86+
87+
7688
- name: Build and install rocRAND
7789
shell: bash
7890
run: |

Common/hipsparselt_utils.hpp

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
// MIT License
2+
//
3+
// Copyright (c) 2025 Advanced Micro Devices, Inc. All rights reserved.
4+
//
5+
// Permission is hereby granted, free of charge, to any person obtaining a copy
6+
// of this software and associated documentation files (the "Software"), to deal
7+
// in the Software without restriction, including without limitation the rights
8+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
// copies of the Software, and to permit persons to whom the Software is
10+
// furnished to do so, subject to the following conditions:
11+
//
12+
// The above copyright notice and this permission notice shall be included in all
13+
// copies or substantial portions of the Software.
14+
//
15+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
// SOFTWARE.
22+
23+
#ifndef COMMON_HIPSPARSELT_UTILS_HPP
24+
#define COMMON_HIPSPARSELT_UTILS_HPP
25+
26+
#include <hipsparselt/hipsparselt.h>
27+
28+
#include <cstdlib>
29+
#include <iostream>
30+
31+
/// \brief Converts a \p hipsparseStatus_t to its correspondent string.
32+
inline const char* hipsparseStatusToString(hipsparseStatus_t status)
33+
{
34+
switch(status)
35+
{
36+
case HIPSPARSE_STATUS_SUCCESS:
37+
return "Success";
38+
39+
case HIPSPARSE_STATUS_NOT_INITIALIZED:
40+
return "hipSPARSELt was not initialized";
41+
42+
case HIPSPARSE_STATUS_ALLOC_FAILED:
43+
return "Resource allocation failed";
44+
45+
case HIPSPARSE_STATUS_INVALID_VALUE:
46+
return "Invalid value";
47+
48+
case HIPSPARSE_STATUS_ARCH_MISMATCH:
49+
return "Device architecture not supported";
50+
51+
case HIPSPARSE_STATUS_MAPPING_ERROR:
52+
return "Access to GPU memory space failed";
53+
54+
case HIPSPARSE_STATUS_EXECUTION_FAILED:
55+
return "GPU program failed to execute";
56+
57+
case HIPSPARSE_STATUS_INTERNAL_ERROR:
58+
return "An internal hipSPARSELt operation failed";
59+
60+
case HIPSPARSE_STATUS_MATRIX_TYPE_NOT_SUPPORTED:
61+
return "Matrix type not supported";
62+
63+
case HIPSPARSE_STATUS_ZERO_PIVOT:
64+
return "Zero pivot was computed";
65+
66+
case HIPSPARSE_STATUS_NOT_SUPPORTED:
67+
return "Operation is not supported";
68+
69+
#if !defined(CUDART_VERSION) || (defined(CUDART_VERSION) && CUDART_VERSION >= 11003)
70+
case HIPSPARSE_STATUS_INSUFFICIENT_RESOURCES:
71+
return "Resources are insufficient";
72+
#endif
73+
74+
// We do use default because we are not in control of these enumeration values.
75+
// Ideally this function is something hipSPARSELt would provide
76+
default:
77+
return "<unknown hipsparseStatus_t value>";
78+
}
79+
}
80+
81+
/// \brief Checks if the provided status code is \p HIPSPARSE_STATUS_SUCCESS and if not, prints an error message to the
82+
/// standard error output and terminates the program with an error code.
83+
#define HIPSPARSELT_CHECK(condition) \
84+
{ \
85+
const hipsparseStatus_t status = condition; \
86+
if(status != HIPSPARSE_STATUS_SUCCESS) \
87+
{ \
88+
std::cerr << "hipSPARSELt error encountered: \"" \
89+
<< hipsparseStatusToString(status) \
90+
<< "\" at " << __FILE__ << ':' << __LINE__ \
91+
<< std::endl; \
92+
std::exit(EXIT_FAILURE); \
93+
} \
94+
}
95+
96+
#endif

Libraries/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ if(
3838
add_subdirectory(hipFFT)
3939
add_subdirectory(hipRAND)
4040
add_subdirectory(hipSOLVER)
41+
add_subdirectory(hipSPARSELt)
4142
add_subdirectory(hipTensor)
4243
add_subdirectory(rocBLAS)
4344
add_subdirectory(rocFFT)

Libraries/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ LIBRARIES := \
2626
hipCUB \
2727
hipFFT \
2828
hipSOLVER \
29+
hipSPARSELt \
2930
rocRAND \
3031
hipRAND
3132

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# MIT License
2+
#
3+
# Copyright (c) 2025 Advanced Micro Devices, Inc. All rights reserved.
4+
#
5+
# Permission is hereby granted, free of charge, to any person obtaining a copy
6+
# of this software and associated documentation files (the "Software"), to deal
7+
# in the Software without restriction, including without limitation the rights
8+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
# copies of the Software, and to permit persons to whom the Software is
10+
# furnished to do so, subject to the following conditions:
11+
#
12+
# The above copyright notice and this permission notice shall be included in all
13+
# copies or substantial portions of the Software.
14+
#
15+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
# SOFTWARE.
22+
23+
cmake_minimum_required(VERSION 3.21 FATAL_ERROR)
24+
project(hipSPARSELt_examples LANGUAGES CXX)
25+
include(CTest)
26+
27+
file(RELATIVE_PATH folder_bin ${CMAKE_SOURCE_DIR} ${CMAKE_CURRENT_SOURCE_DIR})
28+
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin/${folder_bin})
29+
30+
if(CMAKE_SYSTEM_NAME MATCHES "Windows")
31+
set(ROCM_ROOT
32+
"$ENV{HIP_PATH}"
33+
CACHE PATH
34+
"Root directory of the ROCm installation"
35+
)
36+
else()
37+
set(ROCM_ROOT
38+
"/opt/rocm"
39+
CACHE PATH
40+
"Root directory of the ROCm installation"
41+
)
42+
endif()
43+
44+
list(APPEND CMAKE_PREFIX_PATH "${ROCM_ROOT}")
45+
46+
if(CMAKE_SYSTEM_NAME STREQUAL "Windows")
47+
message(STATUS "The hipSPARSELt examples are available on Linux only.")
48+
else()
49+
find_package(hipsparselt QUIET)
50+
51+
if(NOT hipsparselt_FOUND)
52+
message(STATUS "hipSPARSELt could not be found, not building hipSPARSELt examples")
53+
else()
54+
add_subdirectory(spmm)
55+
add_subdirectory(spmm_advanced)
56+
endif()
57+
endif()

Libraries/hipSPARSELt/Makefile

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# MIT License
2+
#
3+
# Copyright (c) 2025 Advanced Micro Devices, Inc. All rights reserved.
4+
#
5+
# Permission is hereby granted, free of charge, to any person obtaining a copy
6+
# of this software and associated documentation files (the "Software"), to deal
7+
# in the Software without restriction, including without limitation the rights
8+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
# copies of the Software, and to permit persons to whom the Software is
10+
# furnished to do so, subject to the following conditions:
11+
#
12+
# The above copyright notice and this permission notice shall be included in all
13+
# copies or substantial portions of the Software.
14+
#
15+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
# SOFTWARE.
22+
23+
EXAMPLES := \
24+
spmm \
25+
spmm_advanced
26+
27+
all: $(EXAMPLES)
28+
29+
clean: TARGET=clean
30+
clean: all
31+
32+
$(EXAMPLES):
33+
$(MAKE) -C $@ $(TARGET)
34+
35+
.PHONY: all clean $(EXAMPLES)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
hipsparselt_spmm
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# MIT License
2+
#
3+
# Copyright (c) 2025 Advanced Micro Devices, Inc. All rights reserved.
4+
#
5+
# Permission is hereby granted, free of charge, to any person obtaining a copy
6+
# of this software and associated documentation files (the "Software"), to deal
7+
# in the Software without restriction, including without limitation the rights
8+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
# copies of the Software, and to permit persons to whom the Software is
10+
# furnished to do so, subject to the following conditions:
11+
#
12+
# The above copyright notice and this permission notice shall be included in all
13+
# copies or substantial portions of the Software.
14+
#
15+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
# SOFTWARE.
22+
23+
set(example_name hipsparselt_spmm)
24+
25+
cmake_minimum_required(VERSION 3.21 FATAL_ERROR)
26+
project(${example_name} LANGUAGES CXX)
27+
28+
if(CMAKE_SYSTEM_NAME STREQUAL "Windows")
29+
message(WARNING "The hipSPARSELt examples are available on Linux only.")
30+
return()
31+
endif()
32+
33+
include("${CMAKE_CURRENT_LIST_DIR}/../../../Common/HipPlatform.cmake")
34+
select_gpu_language()
35+
enable_language(${ROCM_EXAMPLES_GPU_LANGUAGE})
36+
select_hip_platform()
37+
38+
if(CMAKE_SYSTEM_NAME MATCHES "Windows")
39+
set(ROCM_ROOT
40+
"$ENV{HIP_PATH}"
41+
CACHE PATH
42+
"Root directory of the ROCm installation"
43+
)
44+
else()
45+
set(ROCM_ROOT
46+
"/opt/rocm"
47+
CACHE PATH
48+
"Root directory of the ROCm installation"
49+
)
50+
endif()
51+
52+
list(APPEND CMAKE_PREFIX_PATH "${ROCM_ROOT}")
53+
54+
find_package(hip REQUIRED)
55+
find_package(hipsparselt REQUIRED)
56+
57+
add_executable(${example_name} main.cpp)
58+
# Make example runnable using CTest
59+
add_test(NAME ${example_name} COMMAND ${example_name})
60+
61+
set_source_files_properties(main.cpp PROPERTIES LANGUAGE ${ROCM_EXAMPLES_GPU_LANGUAGE})
62+
63+
set_target_properties(${example_name} PROPERTIES $<$<COMPILE_LANGUAGE:CUDA>:CUDA_EXTENSIONS OFF>
64+
$<$<COMPILE_LANGUAGE:HIP>:HIP_EXTENSIONS OFF>)
65+
target_compile_features(${example_name} PRIVATE cuda_std_17 hip_std_17)
66+
target_include_directories(${example_name} PRIVATE "${CMAKE_CURRENT_LIST_DIR}/../../../Common"
67+
$<$<COMPILE_LANGUAGE:CUDA>:${ROCM_ROOT}/include>)
68+
69+
# Link to example library
70+
target_link_libraries(${example_name} PRIVATE hip::host roc::hipsparselt)
71+
72+
install(TARGETS ${example_name})
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# MIT License
2+
#
3+
# Copyright (c) 2025 Advanced Micro Devices, Inc. All rights reserved.
4+
#
5+
# Permission is hereby granted, free of charge, to any person obtaining a copy
6+
# of this software and associated documentation files (the "Software"), to deal
7+
# in the Software without restriction, including without limitation the rights
8+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
# copies of the Software, and to permit persons to whom the Software is
10+
# furnished to do so, subject to the following conditions:
11+
#
12+
# The above copyright notice and this permission notice shall be included in all
13+
# copies or substantial portions of the Software.
14+
#
15+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
# SOFTWARE.
22+
23+
EXAMPLE := hipsparselt_spmm
24+
COMMON_INCLUDE_DIR := ../../../Common
25+
GPU_RUNTIME := HIP
26+
27+
# HIP variables
28+
ROCM_INSTALL_DIR := /opt/rocm
29+
CUDA_INSTALL_DIR := /usr/local/cuda
30+
31+
HIP_INCLUDE_DIR := $(ROCM_INSTALL_DIR)/include
32+
HIPSPARSELT_INCLUDE_DIR := $(HIP_INCLUDE_DIR)
33+
34+
HIPCXX ?= $(ROCM_INSTALL_DIR)/bin/hipcc
35+
CUDACXX ?= $(CUDA_INSTALL_DIR)/bin/nvcc
36+
37+
# Common variables and flags
38+
CXX_STD := c++17
39+
ICXXFLAGS := -std=$(CXX_STD)
40+
ICPPFLAGS := -isystem $(HIPSPARSELT_INCLUDE_DIR) -I $(COMMON_INCLUDE_DIR)
41+
ILDFLAGS := -L $(ROCM_INSTALL_DIR)/lib
42+
ILDLIBS := -lhipsparselt
43+
44+
ifeq ($(GPU_RUNTIME), CUDA)
45+
CXXFLAGS += -x cu
46+
CPPFLAGS += -isystem $(HIP_INCLUDE_DIR) -D__HIP_PLATFORM_NVIDIA__
47+
COMPILER := $(CUDACXX)
48+
else ifeq ($(GPU_RUNTIME), HIP)
49+
CXXFLAGS ?= -Wall -Wextra
50+
CPPFLAGS += -D__HIP_PLATFORM_AMD__
51+
COMPILER := $(HIPCXX)
52+
else
53+
$(error GPU_RUNTIME is set to "$(GPU_RUNTIME)". GPU_RUNTIME must be either CUDA or HIP)
54+
endif
55+
56+
ICXXFLAGS += $(CXXFLAGS)
57+
ICPPFLAGS += $(CPPFLAGS)
58+
ILDFLAGS += $(LDFLAGS)
59+
ILDLIBS += $(LDLIBS)
60+
61+
$(EXAMPLE): main.cpp $(COMMON_INCLUDE_DIR)/example_utils.hpp $(COMMON_INCLUDE_DIR)/hipsparselt_utils.hpp
62+
$(COMPILER) $(ICXXFLAGS) $(ICPPFLAGS) $(ILDFLAGS) -o $@ $< $(ILDLIBS)
63+
64+
clean:
65+
$(RM) $(EXAMPLE)
66+
67+
.PHONY: clean

0 commit comments

Comments
 (0)