-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
652ccd8
commit 95d19f8
Showing
5 changed files
with
207 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,91 @@ | ||
#include <cuda.h> | ||
#include <cuda_runtime.h> | ||
#include "../common.h" | ||
|
||
#include <cumccormick/cumccormick.cuh> | ||
#include <cuda_runtime.h> | ||
|
||
#include <cutangent/cutangent.cuh> | ||
#include <cutangent/format.h> | ||
|
||
#include <iostream> | ||
|
||
using cu::tangent; | ||
|
||
constexpr auto f(auto x, auto y) | ||
{ | ||
auto print = [](auto x) { printf("{%g, %g}\n", x.v, x.d); }; | ||
|
||
auto a = x + y; | ||
auto b = x - y; | ||
auto c = x * y; | ||
auto d = x / y; | ||
auto e = max(x, y); | ||
auto f = min(x, y); | ||
auto g = mid(x, y, y); | ||
auto h = sin(x); | ||
auto i = cos(x); | ||
auto j = exp(x); | ||
auto k = log(x); | ||
auto l = pown(x, 2); | ||
|
||
template<typename T> | ||
using mc = cu::mccormick<T>; | ||
print(a); | ||
print(b); | ||
print(c); | ||
print(d); | ||
print(e); | ||
print(f); | ||
print(g); | ||
print(h); | ||
print(i); | ||
print(j); | ||
print(k); | ||
print(l); | ||
return a; | ||
} | ||
|
||
__global__ void kernel(tangent<double> *xs, tangent<double> *ys, | ||
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 = 256; | ||
// using T = mc<double>; | ||
// T xs[n], ys[n], res[n]; | ||
// | ||
// // generate dummy data | ||
// for (int i = 0; i < n; i++) { | ||
// double v = i; | ||
// xs[i] = { .cv = -v, .cc = v, .box = { .lb = -v, .ub = v } }; | ||
// ys[i] = { .cv = -v, .cc = v, .box = { .lb = -v, .ub = v } }; | ||
constexpr int n = 16; | ||
using T = tangent<double>; | ||
T xs[n], ys[n], res[n]; | ||
|
||
// generate dummy data | ||
for (int i = 0; i < n; i++) { | ||
double v = i; | ||
xs[i] = { v, 1.0 }; | ||
ys[i] = { v, 0.0 }; | ||
} | ||
|
||
// for (auto el : xs) { | ||
// std::cout << el << 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)); | ||
|
||
// for (auto el : res) { | ||
// std::cout << el << std::endl; | ||
// } | ||
// | ||
// mc<double> *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]; | ||
// printf("beale(0, 0) = " MCCORMICK_FORMAT "\n", r.box.lb, r.cv, r.cc, r.box.ub); | ||
// | ||
// CUDA_CHECK(cudaFree(d_xs)); | ||
// CUDA_CHECK(cudaFree(d_ys)); | ||
// CUDA_CHECK(cudaFree(d_res)); | ||
|
||
CUDA_CHECK(cudaFree(d_xs)); | ||
CUDA_CHECK(cudaFree(d_ys)); | ||
CUDA_CHECK(cudaFree(d_res)); | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
#ifndef CUTANGENT_COMMON_H | ||
#define CUTANGENT_COMMON_H | ||
|
||
#include <cstddef> | ||
#include <cstdio> | ||
#include <cstdlib> | ||
|
||
#include <cuda_runtime.h> | ||
|
||
#define CUDA_CHECK(x) \ | ||
do { \ | ||
cudaError_t err = x; \ | ||
if (err != cudaSuccess) { \ | ||
fprintf(stderr, "CUDA error in %s at %s:%d: %s (%s=%d)\n", __FUNCTION__, \ | ||
__FILE__, __LINE__, cudaGetErrorString(err), \ | ||
cudaGetErrorName(err), err); \ | ||
abort(); \ | ||
} \ | ||
} while (0) | ||
|
||
#endif // CUTANGENT_COMMON_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#ifndef CUTANGENT_FORMAT_H | ||
#define CUTANGENT_FORMAT_H | ||
|
||
#include <cutangent/tangent.h> | ||
|
||
#include <ostream> | ||
|
||
namespace cu | ||
{ | ||
|
||
template<typename T> | ||
std::ostream &operator<<(std::ostream &os, tangent<T> x) | ||
{ | ||
return os << "{v: " << x.v << ", d: " << x.d << "}"; | ||
} | ||
|
||
} // namespace cu | ||
|
||
#endif // CUTANGENT_FORMAT_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters