Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for sin #1

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Open

Conversation

AuroraPerego
Copy link

to run the tests:

cd test
make all
make runAll

Comment on lines 1 to 67
#define CATCH_CONFIG_MAIN
#include <catch.hpp>

#include "math.h"
#include <cuda_runtime.h>
#include <limits>
#include <vector>

template <typename T> __global__ void sinKernel(double *result, T input) {
result[0] = static_cast<double>(xtd::sin(input));
}

template <typename T> __global__ void sinfKernel(double *result, T input) {
result[0] = static_cast<double>(xtd::sinf(input));
}

TEST_CASE("sinCuda", "[sin]") {
int deviceCount;
cudaError_t cudaStatus = cudaGetDeviceCount(&deviceCount);

if (cudaStatus != cudaSuccess || deviceCount == 0) {
exit(EXIT_SUCCESS);
}

cudaSetDevice(0);
cudaStream_t q;
cudaStreamCreate(&q);

std::vector<double> values{-1., 0., M_PI / 2, M_PI, 42.};

double *result;
int constexpr N = 6;
cudaMallocAsync(&result, N * sizeof(double), q);

for (auto v : values) {

cudaMemsetAsync(&result, 0x00, N * sizeof(double), q);

sinKernel<<<1, 1, 0, q>>>(&result[0], static_cast<int>(v));
sinKernel<<<1, 1, 0, q>>>(&result[1], static_cast<float>(v));
sinKernel<<<1, 1, 0, q>>>(&result[2], static_cast<double>(v));
sinfKernel<<<1, 1, 0, q>>>(&result[3], static_cast<int>(v));
sinfKernel<<<1, 1, 0, q>>>(&result[4], static_cast<float>(v));
sinfKernel<<<1, 1, 0, q>>>(&result[5], static_cast<double>(v));

double resultHost[N];
cudaMemcpyAsync(resultHost, result, N * sizeof(double),
cudaMemcpyDeviceToHost, q);

cudaStreamSynchronize(q);

auto const epsilon = std::numeric_limits<double>::epsilon();
auto const epsilon_f = std::numeric_limits<float>::epsilon();
REQUIRE_THAT(resultHost[0], Catch::Matchers::WithinAbs(
std::sin(static_cast<int>(v)), epsilon));
REQUIRE_THAT(resultHost[1],
Catch::Matchers::WithinAbs(std::sin(v), epsilon_f));
REQUIRE_THAT(resultHost[2],
Catch::Matchers::WithinAbs(std::sin(v), epsilon));
REQUIRE_THAT(resultHost[3], Catch::Matchers::WithinAbs(
sinf(static_cast<int>(v)), epsilon_f));
REQUIRE_THAT(resultHost[4], Catch::Matchers::WithinAbs(sinf(v), epsilon_f));
REQUIRE_THAT(resultHost[5], Catch::Matchers::WithinAbs(sinf(v), epsilon_f));
}

cudaFreeAsync(result, q);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#define CATCH_CONFIG_MAIN
#include <catch.hpp>
#include "math.h"
#include <cuda_runtime.h>
#include <limits>
#include <vector>
template <typename T> __global__ void sinKernel(double *result, T input) {
result[0] = static_cast<double>(xtd::sin(input));
}
template <typename T> __global__ void sinfKernel(double *result, T input) {
result[0] = static_cast<double>(xtd::sinf(input));
}
TEST_CASE("sinCuda", "[sin]") {
int deviceCount;
cudaError_t cudaStatus = cudaGetDeviceCount(&deviceCount);
if (cudaStatus != cudaSuccess || deviceCount == 0) {
exit(EXIT_SUCCESS);
}
cudaSetDevice(0);
cudaStream_t q;
cudaStreamCreate(&q);
std::vector<double> values{-1., 0., M_PI / 2, M_PI, 42.};
double *result;
int constexpr N = 6;
cudaMallocAsync(&result, N * sizeof(double), q);
for (auto v : values) {
cudaMemsetAsync(&result, 0x00, N * sizeof(double), q);
sinKernel<<<1, 1, 0, q>>>(&result[0], static_cast<int>(v));
sinKernel<<<1, 1, 0, q>>>(&result[1], static_cast<float>(v));
sinKernel<<<1, 1, 0, q>>>(&result[2], static_cast<double>(v));
sinfKernel<<<1, 1, 0, q>>>(&result[3], static_cast<int>(v));
sinfKernel<<<1, 1, 0, q>>>(&result[4], static_cast<float>(v));
sinfKernel<<<1, 1, 0, q>>>(&result[5], static_cast<double>(v));
double resultHost[N];
cudaMemcpyAsync(resultHost, result, N * sizeof(double),
cudaMemcpyDeviceToHost, q);
cudaStreamSynchronize(q);
auto const epsilon = std::numeric_limits<double>::epsilon();
auto const epsilon_f = std::numeric_limits<float>::epsilon();
REQUIRE_THAT(resultHost[0], Catch::Matchers::WithinAbs(
std::sin(static_cast<int>(v)), epsilon));
REQUIRE_THAT(resultHost[1],
Catch::Matchers::WithinAbs(std::sin(v), epsilon_f));
REQUIRE_THAT(resultHost[2],
Catch::Matchers::WithinAbs(std::sin(v), epsilon));
REQUIRE_THAT(resultHost[3], Catch::Matchers::WithinAbs(
sinf(static_cast<int>(v)), epsilon_f));
REQUIRE_THAT(resultHost[4], Catch::Matchers::WithinAbs(sinf(v), epsilon_f));
REQUIRE_THAT(resultHost[5], Catch::Matchers::WithinAbs(sinf(v), epsilon_f));
}
cudaFreeAsync(result, q);
}
#include <limits>
#include <vector>
#include <cuda_runtime.h>
#define CATCH_CONFIG_MAIN
#include <catch.hpp>
#include "math.h"
#include "cuda_check.h"
template <typename T> __global__ void sinKernel(double *result, T input) {
*result = static_cast<double>(xtd::sin(input));
}
template <typename T> __global__ void sinfKernel(double *result, T input) {
*result = static_cast<double>(xtd::sinf(input));
}
TEST_CASE("sinCuda", "[sin]") {
int deviceCount;
cudaError_t cudaStatus = cudaGetDeviceCount(&deviceCount);
if (cudaStatus != cudaSuccess || deviceCount == 0) {
exit(EXIT_SUCCESS);
}
CUDA_CHECK(cudaSetDevice(0));
cudaStream_t q;
CUDA_CHECK(cudaStreamCreate(&q));
std::vector<double> values{-1., 0., M_PI / 2, M_PI, 42.};
double *result;
int constexpr N = 6;
CUDA_CHECK(cudaMallocAsync(&result, N * sizeof(double), q));
for (auto v : values) {
CUDA_CHECK(cudaMemsetAsync(result, 0x00, N * sizeof(double), q));
sinKernel<<<1, 1, 0, q>>>(result + 0, static_cast<int>(v));
CUDA_CHECK(cudaGetLastError());
sinKernel<<<1, 1, 0, q>>>(result + 1, static_cast<float>(v));
CUDA_CHECK(cudaGetLastError());
sinKernel<<<1, 1, 0, q>>>(result + 2, static_cast<double>(v));
CUDA_CHECK(cudaGetLastError());
sinfKernel<<<1, 1, 0, q>>>(result + 3, static_cast<int>(v));
CUDA_CHECK(cudaGetLastError());
sinfKernel<<<1, 1, 0, q>>>(result + 4, static_cast<float>(v));
CUDA_CHECK(cudaGetLastError());
sinfKernel<<<1, 1, 0, q>>>(result + 5, static_cast<double>(v));
CUDA_CHECK(cudaGetLastError());
double resultHost[N];
CUDA_CHECK(cudaMemcpyAsync(resultHost, result, N * sizeof(double), cudaMemcpyDeviceToHost, q));
CUDA_CHECK(cudaStreamSynchronize(q));
auto const epsilon = std::numeric_limits<double>::epsilon();
auto const epsilon_f = std::numeric_limits<float>::epsilon();
REQUIRE_THAT(resultHost[0], Catch::Matchers::WithinAbs(std::sin(static_cast<int>(v)), epsilon));
REQUIRE_THAT(resultHost[1], Catch::Matchers::WithinAbs(std::sin(v), epsilon_f));
REQUIRE_THAT(resultHost[2], Catch::Matchers::WithinAbs(std::sin(v), epsilon));
REQUIRE_THAT(resultHost[3], Catch::Matchers::WithinAbs(sinf(static_cast<int>(v)), epsilon_f));
REQUIRE_THAT(resultHost[4], Catch::Matchers::WithinAbs(sinf(v), epsilon_f));
REQUIRE_THAT(resultHost[5], Catch::Matchers::WithinAbs(sinf(v), epsilon_f));
}
CUDA_CHECK(cudaFreeAsync(result, q));
CUDA_CHECK(cudaStreamDestroy(q));
}

@fwyzard
Copy link
Contributor

fwyzard commented Mar 2, 2024

On my notebook I get

fwyzard@fools:~/src/xtd/test$ make
Running /home/fwyzard/src/xtd/test/sin/bin/sin_t_cc
===============================================================================
All tests passed (30 assertions in 1 test case)


Running /home/fwyzard/src/xtd/test/sin/bin/sin_t_hip
No AMD GPUs found, the test will be skipped.

Running /home/fwyzard/src/xtd/test/sin/bin/sin_t_cuda
===============================================================================
All tests passed (30 assertions in 1 test case)


Running /home/fwyzard/src/xtd/test/sin/bin/sin_t_cpusycl
===============================================================================
All tests passed (30 assertions in 1 test case)


Running /home/fwyzard/src/xtd/test/sin/bin/sin_t_gpusycl
Native API failed. Native API returns: -2 (PI_ERROR_DEVICE_NOT_AVAILABLE) -2 (PI_ERROR_DEVICE_NOT_AVAILABLE)Exception caught at file:sin/sin_t.sycl.cc, line:60

@AuroraPerego
Copy link
Author

Thanks for the improvements :D
The tests pass on:

  • NVIDIA L4, V100 and T4
  • Intel GPU Flex

The hip test hangs on the AMD GPU Radeon PRO WX 9100 (don't know yet if it's due to the GPU or the test)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants