Skip to content

Commit a296975

Browse files
authored
[HIP] add test builtin-logb-scalbn (#240)
* [HIP] add test builtin-logb-scalbn * Modified to test array inputs for float and exp parameters. * fix reference_output and return errs * Fix indexing and uncomment printf on error
1 parent acbf1d5 commit a296975

File tree

3 files changed

+108
-0
lines changed

3 files changed

+108
-0
lines changed

External/HIP/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ macro(create_local_hip_tests VariantSuffix)
2222
list(APPEND HIP_LOCAL_TESTS saxpy)
2323
list(APPEND HIP_LOCAL_TESTS memmove)
2424
list(APPEND HIP_LOCAL_TESTS split-kernel-args)
25+
list(APPEND HIP_LOCAL_TESTS builtin-logb-scalbn)
2526

2627
# TODO: Re-enable InOneWeekend after it is fixed
2728
#list(APPEND HIP_LOCAL_TESTS InOneWeekend)

External/HIP/builtin-logb-scalbn.hip

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
#include <hip/hip_runtime.h>
2+
#include <cmath>
3+
#include <cstdio>
4+
#include <iostream>
5+
6+
// Simple error check macro
7+
#define HIP_CHECK(call) \
8+
do { \
9+
hipError_t err = call; \
10+
if (err != hipSuccess) { \
11+
std::cerr << "HIP error: " << hipGetErrorString(err) \
12+
<< " at " << __FILE__ << ":" << __LINE__ << std::endl; \
13+
std::exit(EXIT_FAILURE); \
14+
} \
15+
} while (0)
16+
17+
__global__ void my_kernel(float a[], int alen, int exp[], int explen, float *t_res) {
18+
for (int i = 0; i < alen; i++) {
19+
t_res[4*i] = logbf(a[i]);
20+
t_res[4*i + 1] = logb(a[i]);
21+
t_res[4*i + 2] = __builtin_logbf(a[i]);
22+
t_res[4*i + 3] = __builtin_logb(a[i]);
23+
}
24+
25+
for (int i = 0; i < alen; i++) {
26+
for (int j = 0; j < explen; j++) {
27+
t_res[4*alen + 4*explen*i + 4*j] = scalbnf(a[i], exp[j]);
28+
t_res[4*alen + 4*explen*i + 4*j + 1] = scalbn(a[i], exp[j]);
29+
t_res[4*alen + 4*explen*i + 4*j + 2] = __builtin_scalbnf(a[i], exp[j]);
30+
t_res[4*alen + 4*explen*i + 4*j + 3] = __builtin_scalbn(a[i], exp[j]);
31+
}
32+
}
33+
}
34+
35+
void __attribute__((noinline)) test(float a[], int alen, int exp[], int explen, float *h_res) {
36+
for (int i = 0; i < alen; i++) {
37+
h_res[4*i] = logbf(a[i]);
38+
h_res[4*i + 1] = logb(a[i]);
39+
h_res[4*i + 2] = __builtin_logbf(a[i]);
40+
h_res[4*i + 3] = __builtin_logb(a[i]);
41+
}
42+
43+
for (int i = 0; i < alen; i++) {
44+
for (int j = 0; j < explen; j++) {
45+
h_res[4*alen + 4*explen*i + 4*j] = scalbnf(a[i], exp[j]);
46+
h_res[4*alen + 4*explen*i + 4*j + 1] = scalbn(a[i], exp[j]);
47+
h_res[4*alen + 4*explen*i + 4*j + 2] = __builtin_scalbnf(a[i], exp[j]);
48+
h_res[4*alen + 4*explen*i + 4*j + 3] = __builtin_scalbn(a[i], exp[j]);
49+
}
50+
}
51+
}
52+
53+
int main(int argc, char **argv) {
54+
// Init input data
55+
float a[] = {16.0f, 3.14f, 0.0f, -0.0f, INFINITY, NAN};
56+
int alen = sizeof(a) / sizeof(a[0]);
57+
int exp[] = {10, 0, -5};
58+
int explen = sizeof(exp) / sizeof(exp[0]);
59+
60+
// Compute on CPU
61+
int res_len = 4 * alen + 4 * alen * explen; // logb + scalbn
62+
int res_bsize = sizeof(float) * res_len;
63+
float *h_res = (float *)malloc(res_bsize);
64+
test(a, alen, exp, explen, h_res);
65+
66+
// Make a copy for GPU
67+
float *d_a;
68+
int *d_exp;
69+
float *t_res;
70+
HIP_CHECK(hipMalloc((void**)&d_a, sizeof(a)));
71+
HIP_CHECK(hipMalloc((void**)&d_exp, sizeof(exp)));
72+
HIP_CHECK(hipMalloc((void**)&t_res, res_bsize));
73+
HIP_CHECK(hipMemcpy(d_a, a, sizeof(a), hipMemcpyHostToDevice));
74+
HIP_CHECK(hipMemcpy(d_exp, exp, sizeof(exp), hipMemcpyHostToDevice));
75+
HIP_CHECK(hipMemset(t_res, 0, res_bsize));
76+
77+
// Launch a GPU kernel
78+
my_kernel<<<1,1>>>(d_a, alen, d_exp, explen, t_res);
79+
80+
// Copy the device results to host
81+
float *d_res = (float *)malloc(res_bsize);
82+
HIP_CHECK(hipDeviceSynchronize());
83+
HIP_CHECK(hipMemcpy(d_res, t_res, res_bsize, hipMemcpyDeviceToHost));
84+
85+
// Verify the results match CPU.
86+
int errs = 0;
87+
for(int i = 0; i < res_len; i++) {
88+
if (fabs(h_res[i] - d_res[i]) > fabs(h_res[i] * 0.0001f)) {
89+
printf("found error i=%i h=%f d=%f\n", i, h_res[i], d_res[i]);
90+
errs++;
91+
}
92+
}
93+
if (errs != 0)
94+
printf("%i errors\n", errs);
95+
else
96+
printf("PASSED!\n");
97+
98+
free(h_res);
99+
HIP_CHECK(hipFree(d_a));
100+
HIP_CHECK(hipFree(d_exp));
101+
HIP_CHECK(hipFree(t_res));
102+
free(d_res);
103+
return errs;
104+
}
105+
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
PASSED!
2+
exit 0

0 commit comments

Comments
 (0)