Skip to content

Commit

Permalink
add interval<tangent<T>> example
Browse files Browse the repository at this point in the history
  • Loading branch information
neilkichler committed Jul 16, 2024
1 parent 688f3c2 commit e08c8b8
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 0 deletions.
1 change: 1 addition & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
add_subdirectory(basic)
add_subdirectory(interval)
add_subdirectory(mccormick)
15 changes: 15 additions & 0 deletions examples/interval/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
add_executable(interval interval.cu)

include(FetchContent)
FetchContent_Declare(
cumccormick
GIT_REPOSITORY https://github.com/neilkichler/cuinterval.git
GIT_TAG main
)
FetchContent_MakeAvailable(cuinterval)

target_link_libraries(interval PUBLIC cuinterval)

target_compile_features(interval PRIVATE cxx_std_20 cuda_std_20)
set_target_properties(interval PROPERTIES CUDA_ARCHITECTURES native)
target_link_libraries(interval PRIVATE ${PROJECT_NAME})
91 changes: 91 additions & 0 deletions examples/interval/interval.cu
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
#include "../common.h"

#include <cuda_runtime.h>

#include <cuinterval/cuinterval.h>
#include <cuinterval/format.h>

#include <cutangent/cutangent.cuh>
#include <cutangent/format.h>

#include <iostream>

using cu::tangent;

template<typename T>
using I = cu::interval<T>;

constexpr auto f(auto x, auto y)
{
// Currently supported functions:

// auto a = neg(x);
// auto a = add(x, y);
// auto a = sub(x, y);
auto a = mul(x, y);
// auto a = div(x, y);
// auto a = x + y;
// auto a = x - y;
// auto a = x / y;
// auto a = x * y;
// auto a = sqr(x);
// auto a = sqrt(x);
// auto a = abs(x);
// auto a = exp(x);
// auto a = log(x);
// auto a = recip(x);
// auto a = cos(x);
// auto a = pown(x, 3);
// auto a = pown(x, 4);
// auto a = pow(x, 4);
// auto a = pow(x, y); // not yet supported by McCormick
// auto a = max(x, y);
// auto a = min(x, y);
// auto a = hull(x, y);
return a;
}

__global__ void kernel(I<tangent<double>> *xs, I<tangent<double>> *ys, I<tangent<double>> *res, int n)
{
int i = blockIdx.x * blockDim.x + threadIdx.x;
if (i < n) {
res[i] = f(xs[i], ys[i]);
}
}

int main()
{
constexpr int n = 1;
using T = I<tangent<double>>;
T xs[n], ys[n], res[n];

// generate dummy data

xs[0] = { .lb = { 0.5, 1.0 }, .ub = { 3.0, 1.0 } } ;

ys[0] = { .lb = { 2.0, 0.0 }, .ub = { 5.0, 0.0 } };

std::cout << xs[0] << std::endl;
std::cout << ys[0] << std::endl;

T *d_xs, *d_ys, *d_res;
CUDA_CHECK(cudaMalloc(&d_xs, n * sizeof(*xs)));
CUDA_CHECK(cudaMalloc(&d_ys, n * sizeof(*ys)));
CUDA_CHECK(cudaMalloc(&d_res, n * sizeof(*res)));

CUDA_CHECK(cudaMemcpy(d_xs, xs, n * sizeof(*xs), cudaMemcpyHostToDevice));
CUDA_CHECK(cudaMemcpy(d_ys, ys, n * sizeof(*ys), cudaMemcpyHostToDevice));

kernel<<<n, 1>>>(d_xs, d_ys, d_res, n);

CUDA_CHECK(cudaMemcpy(res, d_res, n * sizeof(*res), cudaMemcpyDeviceToHost));

auto r = res[0];
std::cout << r << std::endl;

CUDA_CHECK(cudaFree(d_xs));
CUDA_CHECK(cudaFree(d_ys));
CUDA_CHECK(cudaFree(d_res));

return 0;
}

0 comments on commit e08c8b8

Please sign in to comment.