Skip to content

Commit 4a38d81

Browse files
committed
Simplify check code and use \n instead of endl
1 parent 535faa6 commit 4a38d81

File tree

1 file changed

+15
-16
lines changed

1 file changed

+15
-16
lines changed

sycl/test-e2e/syclcompat/helloworld.cpp

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@
2929
#include <cstdlib>
3030
#include <iostream>
3131

32+
#define CHECK_MEMORY(ptr) \
33+
if ((ptr) == nullptr) { \
34+
std::cerr << "Failed to allocate memory: " << ( #ptr ) << "\n"; \
35+
exit(EXIT_FAILURE); \
36+
}
37+
3238
/**
3339
* Slope intercept form of a straight line equation: Y = m * X + b
3440
*/
@@ -46,18 +52,11 @@ void slope_intercept(float *Y, float *X, float m, float b, size_t n) {
4652
Y[i] = m * X[i] + b;
4753
}
4854

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-
5655
/**
5756
* Program main
5857
*/
5958
int main(int argc, char **argv) {
60-
std::cout << "Simple Kernel example" << std::endl;
59+
std::cout << "Simple Kernel example" << "\n";
6160

6261
constexpr size_t n_points = 32;
6362
constexpr float m = 1.5f;
@@ -70,18 +69,18 @@ int main(int argc, char **argv) {
7069
}
7170

7271
std::cout << "block_size = " << block_size << ", n_points = " << n_points
73-
<< std::endl;
72+
<< "\n";
7473

7574
// Allocate host memory for vectors X and Y
7675
size_t mem_size = n_points * sizeof(float);
7776
float *h_X = (float *)syclcompat::malloc_host(mem_size);
7877
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);
8180

8281
// Alternative templated allocation for the expected output
8382
float *h_expected = syclcompat::malloc_host<float>(n_points);
84-
check_memory(h_expected, "Not enough for h_expected.");
83+
CHECK_MEMORY(h_expected);
8584

8685
// Initialize host memory & expected output
8786
for (size_t i = 0; i < n_points; i++) {
@@ -92,8 +91,8 @@ int main(int argc, char **argv) {
9291
// Allocate device memory
9392
float *d_X = (float *)syclcompat::malloc(mem_size);
9493
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);
9796

9897
// copy host memory to device
9998
syclcompat::memcpy(d_X, h_X, mem_size);
@@ -110,7 +109,7 @@ int main(int argc, char **argv) {
110109
n_points);
111110
}
112111
syclcompat::wait();
113-
std::cout << "DONE" << std::endl;
112+
std::cout << "DONE" << "\n";
114113

115114
// Async copy result from device to host
116115
syclcompat::memcpy_async(h_Y, d_Y, mem_size).wait();
@@ -119,7 +118,7 @@ int main(int argc, char **argv) {
119118
for (size_t i = 0; i < n_points; i++) {
120119
if (std::abs(h_Y[i] - h_expected[i]) >= 1e-6) {
121120
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";
123122
exit(EXIT_FAILURE);
124123
}
125124
}

0 commit comments

Comments
 (0)