20 #ifndef JDFTX_CORE_SCALAR_H 21 #define JDFTX_CORE_SCALAR_H 25 #ifndef __device__ //in .cpp files 26 #define __hostanddev__ inline 28 #define __hostanddev__ inline __device__ __host__ 29 #define __in_a_cu_file__ 30 #include <cuda_runtime.h> 34 template<
class T> T ceildiv(T num, T den) {
return (num + den - 1)/den; }
37 template<
class T> T floorMultiple(T num, T den) {
return (num/den)*den; }
40 #ifndef __in_a_cu_file__ 41 inline void sincos(
double x,
double* s,
double* c)
53 __hostanddev__
double& real() {
return x; }
54 __hostanddev__
double& imag() {
return y; }
55 __hostanddev__
const double& real()
const {
return x; }
56 __hostanddev__
const double& imag()
const {
return y; }
59 __hostanddev__
complex(
double x=0,
double y=0) : x(x), y(y) {}
60 #ifdef __in_a_cu_file__ 61 __hostanddev__ complex(
const double2& c) : x(c.x), y(c.y) {}
62 __hostanddev__
operator double2()
const { double2 ret; ret.x=x; ret.y=y;
return ret;}
66 __hostanddev__ complex& operator+=(
const complex& c) { x+=c.x; y+=c.y;
return *
this; }
67 __hostanddev__ complex& operator+=(
double r) { x+=r;
return *
this; }
68 __hostanddev__ complex operator+(
const complex& c)
const {
return complex(x+c.x, y+c.y); }
69 __hostanddev__ complex operator+(
double r)
const {
return complex(x+r, y); }
70 __hostanddev__ complex& operator-=(
const complex& c) { x-=c.x; y-=c.y;
return *
this; }
71 __hostanddev__ complex& operator-=(
double r) { x-=r;
return *
this; }
72 __hostanddev__ complex operator-(
const complex& c)
const {
return complex(x-c.x, y-c.y); }
73 __hostanddev__ complex operator-(
double r)
const {
return complex(x-r, y); }
74 __hostanddev__ complex operator-()
const {
return complex(-x, -y); }
75 __hostanddev__ complex& operator*=(
const complex& c) {
return (*
this = *
this * c); }
76 __hostanddev__ complex& operator*=(
double r) { x*=r; y*=r;
return *
this; }
77 __hostanddev__ complex operator*(
const complex& c)
const {
return complex(x*c.x-y*c.y, y*c.x+x*c.y); }
78 __hostanddev__ complex operator*(
double r)
const {
return complex(x*r, y*r); }
79 __hostanddev__ complex& operator/=(
const complex& c) {
return (*
this = *
this / c); }
80 __hostanddev__ complex& operator/=(
double r) {
return (*
this *= 1.0/r); }
81 __hostanddev__ complex operator/(
const complex& c)
const {
return complex(x*c.x+y*c.y, y*c.x-x*c.y) / c.norm(); }
82 __hostanddev__ complex operator/(
double r)
const {
return *
this * (1.0/r); }
84 __hostanddev__
double norm()
const {
return x*x + y*y; }
85 __hostanddev__
double abs()
const {
return sqrt(norm()); }
86 __hostanddev__
double arg()
const {
return atan2(y,x); }
87 __hostanddev__ complex conj()
const {
return complex(x,-y); }
90 __hostanddev__
double real(
const complex& c) {
return c.real(); }
91 __hostanddev__
double imag(
const complex& c) {
return c.imag(); }
92 __hostanddev__
double norm(
const complex& c) {
return c.norm(); }
93 __hostanddev__
double abs(
const complex& c) {
return c.abs(); }
94 __hostanddev__
double arg(
const complex& c) {
return c.arg(); }
96 __hostanddev__
double conj(
const double& c) {
return c; }
98 __hostanddev__
complex operator+(
double r,
const complex& c) {
return c+r; }
99 __hostanddev__
complex operator-(
double r,
const complex& c) {
return -c+r; }
100 __hostanddev__
complex operator*(
double r,
const complex& c) {
return c*r; }
104 __hostanddev__
complex cis(
double x)
105 {
double s, c; sincos(x, &s, &c);
109 #endif // JDFTX_CORE_SCALAR_H ScalarField sqrt(const ScalarField &)
Elementwise square root (preserve input)
Complex number (need to define our own because we need operators for gpu code as well) ...
Definition: scalar.h:49