|
| 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 | + |
0 commit comments