Moved basic built-in functions for vector operations to math_utils.h from integration.cuh so that they are shared with the CPU and GPU

This commit is contained in:
jpekkila
2019-08-15 11:04:22 +03:00
parent d5b2e5bb42
commit 36fea70560
6 changed files with 92 additions and 86 deletions

View File

@@ -26,6 +26,8 @@
*/ */
#pragma once #pragma once
#include "math_utils.h"
#include <assert.h> #include <assert.h>
static __device__ __forceinline__ int static __device__ __forceinline__ int
@@ -321,65 +323,6 @@ read_data(const int i, const int j, const int k,
* ============================================================================= * =============================================================================
*/ */
static __host__ __device__ __forceinline__ AcReal3
operator-(const AcReal3& a, const AcReal3& b)
{
return (AcReal3){a.x - b.x, a.y - b.y, a.z - b.z};
}
static __host__ __device__ __forceinline__ AcReal3
operator+(const AcReal3& a, const AcReal3& b)
{
return (AcReal3){a.x + b.x, a.y + b.y, a.z + b.z};
}
static __host__ __device__ __forceinline__ AcReal3
operator-(const AcReal3& a)
{
return (AcReal3){-a.x, -a.y, -a.z};
}
static __host__ __device__ __forceinline__ AcReal3 operator*(const AcReal a, const AcReal3& b)
{
return (AcReal3){a * b.x, a * b.y, a * b.z};
}
static __host__ __device__ __forceinline__ AcReal
dot(const AcReal3& a, const AcReal3& b)
{
return a.x * b.x + a.y * b.y + a.z * b.z;
}
static __host__ __device__ __forceinline__ AcReal3
mul(const AcMatrix& aa, const AcReal3& x)
{
return (AcReal3){dot(aa.row[0], x), dot(aa.row[1], x), dot(aa.row[2], x)};
}
static __host__ __device__ __forceinline__ AcReal3
cross(const AcReal3& a, const AcReal3& b)
{
AcReal3 c;
c.x = a.y * b.z - a.z * b.y;
c.y = a.z * b.x - a.x * b.z;
c.z = a.x * b.y - a.y * b.x;
return c;
}
static __host__ __device__ __forceinline__ bool
is_valid(const AcReal& a)
{
return !isnan(a) && !isinf(a);
}
static __host__ __device__ __forceinline__ bool
is_valid(const AcReal3& a)
{
return is_valid(a.x) && is_valid(a.y) && is_valid(a.z);
}
/* /*
* ============================================================================= * =============================================================================
* Level 1 (Stencil Processing Stage) * Level 1 (Stencil Processing Stage)

View File

@@ -64,16 +64,6 @@ sum(const T& a, const T& b)
return a + b; return a + b;
} }
template <class T>
static inline const T
is_valid(const T& val)
{
if (isnan(val) || isinf(val))
return false;
else
return true;
}
template <class T> template <class T>
static inline const T static inline const T
clamp(const T& val, const T& min, const T& max) clamp(const T& val, const T& min, const T& max)
@@ -87,20 +77,85 @@ randr()
return AcReal(rand()) / AcReal(RAND_MAX); return AcReal(rand()) / AcReal(RAND_MAX);
} }
static inline int3
operator+(const int3& a, const int3& b)
{
return (int3){a.x + b.x, a.y + b.y, a.z + b.z};
}
static inline int3
operator-(const int3& a, const int3& b)
{
return (int3){a.x - b.x, a.y - b.y, a.z - b.z};
}
static inline bool static inline bool
is_power_of_two(const unsigned val) is_power_of_two(const unsigned val)
{ {
return val && !(val & (val - 1)); return val && !(val & (val - 1));
} }
#ifdef __CUDACC__
#define HOST_DEVICE_INLINE __host__ __device__ __forceinline__
#else
#define HOST_DEVICE_INLINE inline
#endif // __CUDACC__
static HOST_DEVICE_INLINE AcReal3
operator+(const AcReal3& a, const AcReal3& b)
{
return (AcReal3){a.x + b.x, a.y + b.y, a.z + b.z};
}
static HOST_DEVICE_INLINE int3
operator+(const int3& a, const int3& b)
{
return (int3){a.x + b.x, a.y + b.y, a.z + b.z};
}
static HOST_DEVICE_INLINE AcReal3
operator-(const AcReal3& a, const AcReal3& b)
{
return (AcReal3){a.x - b.x, a.y - b.y, a.z - b.z};
}
static HOST_DEVICE_INLINE int3
operator-(const int3& a, const int3& b)
{
return (int3){a.x - b.x, a.y - b.y, a.z - b.z};
}
static HOST_DEVICE_INLINE AcReal3
operator-(const AcReal3& a)
{
return (AcReal3){-a.x, -a.y, -a.z};
}
static HOST_DEVICE_INLINE AcReal3 operator*(const AcReal& a, const AcReal3& b)
{
return (AcReal3){a * b.x, a * b.y, a * b.z};
}
static HOST_DEVICE_INLINE AcReal
dot(const AcReal3& a, const AcReal3& b)
{
return a.x * b.x + a.y * b.y + a.z * b.z;
}
static HOST_DEVICE_INLINE AcReal3
mul(const AcMatrix& aa, const AcReal3& x)
{
return (AcReal3){dot(aa.row[0], x), dot(aa.row[1], x), dot(aa.row[2], x)};
}
static HOST_DEVICE_INLINE AcReal3
cross(const AcReal3& a, const AcReal3& b)
{
AcReal3 c;
c.x = a.y * b.z - a.z * b.y;
c.y = a.z * b.x - a.x * b.z;
c.z = a.x * b.y - a.y * b.x;
return c;
}
static HOST_DEVICE_INLINE bool
is_valid(const AcReal a)
{
return !isnan(a) && !isinf(a);
}
static HOST_DEVICE_INLINE bool
is_valid(const AcReal3& a)
{
return is_valid(a.x) && is_valid(a.y) && is_valid(a.z);
}

View File

@@ -75,6 +75,12 @@ static const InitType test_cases[] = {INIT_TYPE_RANDOM, INIT_TYPE_XWAVE,
INIT_TYPE_GAUSSIAN_RADIAL_EXPL, INIT_TYPE_ABC_FLOW}; INIT_TYPE_GAUSSIAN_RADIAL_EXPL, INIT_TYPE_ABC_FLOW};
// #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof(arr[0])) // #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof(arr[0]))
static inline bool
is_valid(const ModelScalar a)
{
return !isnan(a) && !isinf(a);
}
#if TEST_TYPE == \ #if TEST_TYPE == \
QUICK_TEST // REGULAR TEST START HERE QUICK_TEST // REGULAR TEST START HERE
// -------------------------------------------------------------------------------------------------------------- // --------------------------------------------------------------------------------------------------------------

View File

@@ -26,7 +26,8 @@
*/ */
#include "host_forcing.h" #include "host_forcing.h"
#include "src/core/math_utils.h" // #include "src/core/math_utils.h"
#include "math.h"
// The is a wrapper for genering random numbers with a chosen system. // The is a wrapper for genering random numbers with a chosen system.
AcReal AcReal

View File

@@ -32,13 +32,13 @@
AcReal get_random_number_01(); AcReal get_random_number_01();
AcReal3 cross(const AcReal3& a, const AcReal3& b); // AcReal3 cross(const AcReal3& a, const AcReal3& b);
AcReal dot(const AcReal3& a, const AcReal3& b); // AcReal dot(const AcReal3& a, const AcReal3& b);
AcReal3 vec_norm(const AcReal3& a); // AcReal3 vec_norm(const AcReal3& a);
AcReal3 vec_multi_scal(const AcReal scal, const AcReal3& a); // AcReal3 vec_multi_scal(const AcReal scal, const AcReal3& a);
AcReal3 helical_forcing_k_generator(const AcReal kmax, const AcReal kmin); AcReal3 helical_forcing_k_generator(const AcReal kmax, const AcReal kmin);

View File

@@ -27,6 +27,7 @@
#pragma once #pragma once
#include "astaroth.h" #include "astaroth.h"
#include "math.h"
typedef long double ModelScalar; typedef long double ModelScalar;