29
29
#include < cstdlib>
30
30
#include < iostream>
31
31
32
+ #define CHECK_MEMORY (ptr ) \
33
+ if ((ptr) == nullptr ) { \
34
+ std::cerr << " Failed to allocate memory: " << ( #ptr ) << " \n " ; \
35
+ exit (EXIT_FAILURE); \
36
+ }
37
+
32
38
/* *
33
39
* Slope intercept form of a straight line equation: Y = m * X + b
34
40
*/
@@ -46,18 +52,11 @@ void slope_intercept(float *Y, float *X, float m, float b, size_t n) {
46
52
Y[i] = m * X[i] + b;
47
53
}
48
54
49
- void check_memory (void *ptr, std::string msg) {
50
- if (ptr == nullptr ) {
51
- std::cerr << " Failed to allocate memory: " << msg << std::endl;
52
- exit (EXIT_FAILURE);
53
- }
54
- }
55
-
56
55
/* *
57
56
* Program main
58
57
*/
59
58
int main (int argc, char **argv) {
60
- std::cout << " Simple Kernel example" << std::endl ;
59
+ std::cout << " Simple Kernel example" << " \n " ;
61
60
62
61
constexpr size_t n_points = 32 ;
63
62
constexpr float m = 1 .5f ;
@@ -70,18 +69,18 @@ int main(int argc, char **argv) {
70
69
}
71
70
72
71
std::cout << " block_size = " << block_size << " , n_points = " << n_points
73
- << std::endl ;
72
+ << " \n " ;
74
73
75
74
// Allocate host memory for vectors X and Y
76
75
size_t mem_size = n_points * sizeof (float );
77
76
float *h_X = (float *)syclcompat::malloc_host (mem_size);
78
77
float *h_Y = (float *)syclcompat::malloc_host (mem_size);
79
- check_memory (h_X, " h_X allocation failed. " );
80
- check_memory (h_Y, " h_Y allocation failed. " );
78
+ CHECK_MEMORY (h_X);
79
+ CHECK_MEMORY (h_Y);
81
80
82
81
// Alternative templated allocation for the expected output
83
82
float *h_expected = syclcompat::malloc_host<float >(n_points);
84
- check_memory (h_expected, " Not enough for h_expected. " );
83
+ CHECK_MEMORY (h_expected);
85
84
86
85
// Initialize host memory & expected output
87
86
for (size_t i = 0 ; i < n_points; i++) {
@@ -92,8 +91,8 @@ int main(int argc, char **argv) {
92
91
// Allocate device memory
93
92
float *d_X = (float *)syclcompat::malloc (mem_size);
94
93
float *d_Y = (float *)syclcompat::malloc (mem_size);
95
- check_memory (d_X, " d_X allocation failed. " );
96
- check_memory (d_Y, " d_Y allocation failed. " );
94
+ CHECK_MEMORY (d_X);
95
+ CHECK_MEMORY (d_Y);
97
96
98
97
// copy host memory to device
99
98
syclcompat::memcpy (d_X, h_X, mem_size);
@@ -110,7 +109,7 @@ int main(int argc, char **argv) {
110
109
n_points);
111
110
}
112
111
syclcompat::wait ();
113
- std::cout << " DONE" << std::endl ;
112
+ std::cout << " DONE" << " \n " ;
114
113
115
114
// Async copy result from device to host
116
115
syclcompat::memcpy_async (h_Y, d_Y, mem_size).wait ();
@@ -119,7 +118,7 @@ int main(int argc, char **argv) {
119
118
for (size_t i = 0 ; i < n_points; i++) {
120
119
if (std::abs (h_Y[i] - h_expected[i]) >= 1e-6 ) {
121
120
std::cerr << " Mismatch at index " << i << " : expected " << h_expected[i]
122
- << " , but got " << h_Y[i] << std::endl ;
121
+ << " , but got " << h_Y[i] << " \n " ;
123
122
exit (EXIT_FAILURE);
124
123
}
125
124
}
0 commit comments