Skip to content

Commit cecb695

Browse files
committed
[ONEMKL_SYCL] rearrange MKL, switch MKL to int32_t and fix spgemm to work with nnz as int64_t
* [Examples] Add comments to simple examples * [LOG][ONEMKL_SYCL] Add log system to repository and shift mkl->onemkl_sycl with some separations out into individual files for implementations
1 parent 8fb0c48 commit cecb695

22 files changed

+377
-111
lines changed

CMakeLists.txt

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ endif()
1616
# Download dependencies
1717
include(FetchContent)
1818

19-
if (ENABLE_ONEMKL)
19+
if (ENABLE_ONEMKL_SYCL)
2020
find_package(MKL REQUIRED)
21-
target_link_libraries(spblas INTERFACE MKL::MKL_DPCPP)
22-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DSPBLAS_ENABLE_ONEMKL")
21+
target_link_libraries(spblas INTERFACE MKL::MKL_SYCL) # SYCL APIs
22+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DSPBLAS_ENABLE_ONEMKL_SYCL")
2323
endif()
2424

2525
if (ENABLE_ARMPL)
@@ -31,6 +31,11 @@ if (ENABLE_ARMPL)
3131
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DSPBLAS_ENABLE_ARMPL")
3232
endif()
3333

34+
# turn on/off debug logging
35+
if (LOG_LEVEL)
36+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DLOG_LEVEL=${LOG_LEVEL}") # SPBLAS_DEBUG | SPBLAS_WARNING | SPBLAS_TRACE | SPBLAS_INFO
37+
endif()
38+
3439
# mdspan
3540
FetchContent_Declare(
3641
mdspan

examples/simple_spgemm.cpp

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#include <iostream>
2+
13
#include <spblas/spblas.hpp>
24

35
#include <fmt/core.h>
@@ -12,6 +14,24 @@ int main(int argc, char** argv) {
1214
spblas::index_t k = 100;
1315
spblas::index_t nnz = 100;
1416

17+
std::cout << "\n\t###########################################################"
18+
"######################"
19+
<< "\n\t### Running SpGEMM Example:"
20+
<< "\n\t###"
21+
<< "\n\t### C = A * B"
22+
<< "\n\t###"
23+
<< "\n\t### with "
24+
<< "\n\t### A in CSR format of size (" << m << ", " << k
25+
<< ") with nnz = " << nnz << "\n\t### B in CSR format of size ("
26+
<< k << ", " << n << ") with nnz = " << nnz
27+
<< "\n\t### C in CSR format of size (" << m << ", " << n
28+
<< ") with nnz to be determined"
29+
<< "\n\t### using float and spblas::index_t (size = "
30+
<< sizeof(spblas::index_t) << " bytes)"
31+
<< "\n\t###########################################################"
32+
"######################"
33+
<< std::endl;
34+
1535
auto&& [a_values, a_rowptr, a_colind, a_shape, as] =
1636
generate_csr<float>(m, k, nnz);
1737
auto&& [b_values, b_rowptr, b_colind, b_shape, bs] =
@@ -26,9 +46,9 @@ int main(int argc, char** argv) {
2646

2747
auto info = multiply_inspect(scaled(1.f, a), b, c);
2848

49+
std::cout << "\t\t C_nnz = " << info.result_nnz() << std::endl;
2950
std::vector<float> c_values(info.result_nnz());
3051
std::vector<spblas::index_t> c_colind(info.result_nnz());
31-
3252
c.update(c_values, c_rowptr, c_colind);
3353

3454
multiply_execute(info, scaled(1.f, a), b, c);
@@ -37,5 +57,7 @@ int main(int argc, char** argv) {
3757
fmt::print("{}: {}\n", i, row);
3858
}
3959

60+
std::cout << "\tExample is completed!" << std::endl;
61+
4062
return 0;
4163
}

examples/simple_spmm.cpp

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#include <iostream>
12
#include <spblas/spblas.hpp>
23

34
#include <fmt/core.h>
@@ -12,21 +13,43 @@ int main(int argc, char** argv) {
1213
spblas::index_t k = 100;
1314
spblas::index_t nnz = 10;
1415

16+
std::cout << "\n\t###########################################################"
17+
"######################"
18+
<< "\n\t### Running SpMM Example:"
19+
<< "\n\t###"
20+
<< "\n\t### Y = alpha * A * X"
21+
<< "\n\t###"
22+
<< "\n\t### with "
23+
<< "\n\t### A in CSR format of size (" << m << ", " << k
24+
<< ") with nnz = " << nnz
25+
<< "\n\t### X, a dense matrix in rowmajor format of size (" << k
26+
<< ", " << n << ")"
27+
<< "\n\t### Y, a dense matrix in rowmajor format of size (" << m
28+
<< ", " << n << ")"
29+
<< "\n\t### using float and spblas::index_t (size = "
30+
<< sizeof(spblas::index_t) << " bytes)"
31+
<< "\n\t###########################################################"
32+
"######################"
33+
<< std::endl;
34+
1535
auto&& [values, rowptr, colind, shape, _] = generate_csr<float>(m, k, nnz);
1636

1737
csr_view<float> a(values, rowptr, colind, shape, nnz);
1838

19-
std::vector<float> b_values(k * n, 1);
20-
std::vector<float> c_values(m * n, 0);
39+
std::vector<float> x_values(k * n, 1);
40+
std::vector<float> y_values(m * n, 0);
2141

22-
md::mdspan b(b_values.data(), k, n);
23-
md::mdspan c(c_values.data(), m, n);
42+
md::mdspan x(x_values.data(), k, n);
43+
md::mdspan y(y_values.data(), m, n);
2444

2545
auto a_view = scaled(2.f, a);
2646

27-
multiply(a_view, scaled(2.f, b), c);
47+
// y = A * (alpha * x)
48+
multiply(a_view, scaled(2.f, x), y);
49+
50+
fmt::print("{}\n", spblas::__backend::values(y));
2851

29-
fmt::print("{}\n", spblas::__backend::values(c));
52+
std::cout << "\tExample is completed!" << std::endl;
3053

3154
return 0;
3255
}

examples/simple_spmv.cpp

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,53 @@
1+
#include <iostream>
12
#include <spblas/spblas.hpp>
23

4+
#include <fmt/core.h>
35
#include <fmt/ranges.h>
46

57
int main(int argc, char** argv) {
68
using namespace spblas;
79

10+
using DATA_TYPE = float;
11+
12+
spblas::index_t m = 100;
13+
spblas::index_t n = 100;
14+
spblas::index_t nnz_in = 10;
15+
16+
std::cout << "\n\t###########################################################"
17+
"######################"
18+
<< "\n\t### Running SpMV Example:"
19+
<< "\n\t###"
20+
<< "\n\t### y = alpha * A * x"
21+
<< "\n\t###"
22+
<< "\n\t### with "
23+
<< "\n\t### A in CSR format of size (" << m << ", " << n
24+
<< ") with nnz = " << nnz_in
25+
<< "\n\t### x, a dense vector of size (" << n << ", " << 1 << ")"
26+
<< "\n\t### y, a dense vector of size (" << m << ", " << 1 << ")"
27+
<< "\n\t### using float and spblas::index_t (size = "
28+
<< sizeof(spblas::index_t) << " bytes)"
29+
<< "\n\t###########################################################"
30+
"######################"
31+
<< std::endl;
32+
833
auto&& [values, rowptr, colind, shape, nnz] =
9-
generate_csr<float>(100, 100, 10);
34+
generate_csr<DATA_TYPE, spblas::index_t>(m, n, nnz_in);
1035

11-
csr_view<float> a(values, rowptr, colind, shape, nnz);
36+
csr_view<DATA_TYPE, spblas::index_t> a(values, rowptr, colind, shape, nnz);
1237

1338
// Scale every value of `a` by 5 in place.
1439
// scale(5.f, a);
1540

16-
std::vector<float> b(100, 1);
17-
std::vector<float> c(100, 0);
41+
std::vector<DATA_TYPE> x(n, 1);
42+
std::vector<DATA_TYPE> y(m, 0);
1843

19-
float alpha = 1.2f;
44+
DATA_TYPE alpha = 1.2f;
2045
auto a_scaled = scaled(alpha, a);
2146

22-
// c = a * alpha * b
23-
multiply(a_scaled, b, c);
47+
// y = alpha * A * x
48+
multiply(a_scaled, x, y);
49+
50+
std::cout << "\tExample is completed!" << std::endl;
2451

2552
return 0;
2653
}

include/spblas/algorithms/multiply_impl.hpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#pragma once
22

33
#include <spblas/backend/backend.hpp>
4+
#include <spblas/backend/log.hpp>
45
#include <spblas/concepts.hpp>
56

67
#include <spblas/backend/csr_builder.hpp>
@@ -15,6 +16,7 @@ namespace spblas {
1516
// SpMV
1617
template <matrix A, vector B, vector C>
1718
void multiply(A&& a, B&& b, C&& c) {
19+
log_trace("");
1820
if (__backend::shape(a)[0] != __backend::shape(c) ||
1921
__backend::shape(a)[1] != __backend::shape(b)) {
2022
throw std::invalid_argument(
@@ -38,6 +40,7 @@ void multiply(A&& a, B&& b, C&& c) {
3840
template <matrix A, matrix B, matrix C>
3941
requires(__backend::lookupable<B> && __backend::lookupable<C>)
4042
void multiply(A&& a, B&& b, C&& c) {
43+
log_trace("");
4144
if (__backend::shape(a)[0] != __backend::shape(c)[0] ||
4245
__backend::shape(b)[1] != __backend::shape(c)[1] ||
4346
__backend::shape(a)[1] != __backend::shape(b)[0]) {
@@ -65,6 +68,7 @@ template <matrix A, matrix B, matrix C>
6568
requires(__backend::row_iterable<A> && __backend::row_iterable<B> &&
6669
__detail::is_csr_view_v<C>)
6770
void multiply(A&& a, B&& b, C&& c) {
71+
log_trace("");
6872
if (__backend::shape(a)[0] != __backend::shape(c)[0] ||
6973
__backend::shape(b)[1] != __backend::shape(c)[1] ||
7074
__backend::shape(a)[1] != __backend::shape(b)[0]) {
@@ -104,6 +108,7 @@ template <matrix A, matrix B, matrix C>
104108
requires(__backend::row_iterable<A> && __backend::row_iterable<B> &&
105109
__detail::is_csr_view_v<C>)
106110
operation_info_t multiply_inspect(A&& a, B&& b, C&& c) {
111+
log_trace("");
107112
if (__backend::shape(a)[0] != __backend::shape(c)[0] ||
108113
__backend::shape(b)[1] != __backend::shape(c)[1] ||
109114
__backend::shape(a)[1] != __backend::shape(b)[0]) {
@@ -113,8 +118,9 @@ operation_info_t multiply_inspect(A&& a, B&& b, C&& c) {
113118

114119
using T = tensor_scalar_t<C>;
115120
using I = tensor_index_t<C>;
121+
using O = tensor_offset_t<C>;
116122

117-
std::size_t nnz = 0;
123+
O nnz = 0;
118124
__backend::spa_set<I> c_row(__backend::shape(c)[1]);
119125

120126
for (auto&& [i, a_row] : __backend::rows(a)) {
@@ -135,6 +141,7 @@ operation_info_t multiply_inspect(A&& a, B&& b, C&& c) {
135141
// C = AB
136142
template <matrix A, matrix B, matrix C>
137143
void multiply_execute(operation_info_t info, A&& a, B&& b, C&& c) {
144+
log_trace("");
138145
multiply(a, b, c);
139146
}
140147

include/spblas/backend/backend.hpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,12 @@
66
#include <spblas/backend/generate.hpp>
77
#include <spblas/backend/view_customizations.hpp>
88

9-
#ifdef SPBLAS_ENABLE_ONEMKL
10-
#include <spblas/vendor/mkl/mkl.hpp>
9+
#ifdef SPBLAS_ENABLE_ONEMKL_IESPBLAS
10+
#include <spblas/vendor/onemkl_iespblas/onemkl_iespblas.hpp>
11+
#endif
12+
13+
#ifdef SPBLAS_ENABLE_ONEMKL_SYCL
14+
#include <spblas/vendor/onemkl_sycl/onemkl_sycl.hpp>
1115
#endif
1216

1317
#ifdef SPBLAS_ENABLE_ARMPL

include/spblas/backend/log.hpp

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
#pragma once
2+
3+
//
4+
// This defines a logging system that can be turned on and off by command line
5+
//
6+
// -DLOG_LEVEL=SPBLAS_TRACE (or any other level or undefined LOG_LEVEL to
7+
// turn it off)
8+
//
9+
// SPBLAS_DEBUG (level 0) not meant to be kept in code, but good for debugging
10+
// SPBLAS_WARNING (level 1) for giving developers a more complete warning
11+
// message before throwing error or exiting
12+
// SPBLAS_TRACE (level 2) provides trace comments of files and line numbers
13+
// SPBLAS_INFO (level 3) for providing deep information about algorithm or
14+
// details
15+
//
16+
//
17+
// Add to code any of the following
18+
//
19+
// log_debug("formatted message"); // behaves like printf so can add formatting
20+
// log_warning("formated message"); // warnings or early exit extra detail
21+
// log_trace(""); // can also add formatted message, but often file/line is
22+
// // sufficient
23+
// log_info("formatted message") // any extra info or data we find useful for
24+
// // analyzing algorithm or debugging
25+
//
26+
// when LOG_LEVEL is not defined, it does nothing, but when defined, it prints
27+
// as desired for all levels up to specified one
28+
//
29+
30+
enum { SPBLAS_DEBUG, SPBLAS_WARNING, SPBLAS_TRACE, SPBLAS_INFO };
31+
32+
#define log_debug(fmt, ...) \
33+
_log_(SPBLAS_DEBUG, __FILE__, __LINE__, __PRETTY_FUNCTION__, fmt, \
34+
##__VA_ARGS__)
35+
#define log_warning(fmt, ...) \
36+
_log_(SPBLAS_WARNING, __FILE__, __LINE__, __PRETTY_FUNCTION__, fmt, \
37+
##__VA_ARGS__)
38+
#define log_trace(fmt, ...) \
39+
_log_(SPBLAS_TRACE, __FILE__, __LINE__, __PRETTY_FUNCTION__, fmt, \
40+
##__VA_ARGS__)
41+
#define log_info(fmt, ...) \
42+
_log_(SPBLAS_INFO, __FILE__, __LINE__, __PRETTY_FUNCTION__, fmt, \
43+
##__VA_ARGS__)
44+
45+
#if defined(LOG_LEVEL)
46+
#define _log_(l, file, line, func, fmt, ...) \
47+
spblas_log_(l, #l, file, line, func, fmt, ##__VA_ARGS__)
48+
#else
49+
#define _log_(l, file, line, func, fmt, ...) \
50+
do { \
51+
} while (0)
52+
#endif
53+
54+
#ifdef LOG_LEVEL
55+
56+
#include <stdarg.h> // va_start, va_list, va_end
57+
#include <stdio.h> // printf, vprintf
58+
59+
static void spblas_log_(int level, const char* pref, const char* file,
60+
const int line, const char* func, const char* fmt,
61+
...) {
62+
va_list args;
63+
va_start(args, fmt);
64+
65+
if (level <= LOG_LEVEL) { // add all smaller logtype enums
66+
67+
if (isatty(1)) {
68+
// color log if isatty(1) ie file descriptor is from terminal
69+
printf("\x1b[48;5;14m"); // background is high intensity light blue
70+
printf("\x1b[38;5;0m"); // foreground is black
71+
}
72+
73+
// printf("[%s] %s:%d: %s()", pref, file, line, func); // print out
74+
// preamble: [logtype] file:<line#>: functionname() message
75+
printf("[%s] %s:%d: ", pref, file, line);
76+
// print out preamble: [logtype] file:<line#>: message
77+
vprintf(fmt, args); // print out message
78+
79+
// end color for log printing
80+
if (isatty(1)) {
81+
printf("\x1b[0m");
82+
}
83+
printf("\n");
84+
}
85+
fflush(0);
86+
va_end(args);
87+
}
88+
#endif // LOG_LEVEL

include/spblas/detail/index.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@
77
#include <tuple>
88

99
#include <spblas/detail/tuple_concept.hpp>
10+
#include <spblas/detail/types.hpp>
1011

1112
namespace spblas {
1213

13-
template <std::integral T = std::size_t>
14+
template <std::integral T = spblas::index_t>
1415
class index {
1516
public:
1617
using index_type = T;

0 commit comments

Comments
 (0)