I am trying to calculate relative distance between points with user defined classes in CUDA. I have a Coordinates class and a Relative Coordinate class to store coordinates and calculate Relative Coodinates.
// class.cuh
#pragma once
#ifdef __CUDACC__
#include <cuda_runtime.h>
#define CUDA_HOST __host__
#define CUDA_DEVICE __device__
#else
#define CUDA_HOST
#define CUDA_DEVICE
#endif
#define _USE_MATH_DEFINES
#include <math.h>
template <class Typ>
class Coordinates {
public:
Typ x{}, y{}, z{};
Coordinates () = default;
Coordinates (const Typ&, const Typ&, const Typ&);
};
template <class Typ>
class Rel_Coordinates : public Coordinates<Typ> {
public:
// unsigned idx_a{}, idx_b{};
Typ dist2{}, dist{}, dist_inv{};
Rel_Coordinates () = default;
CUDA_HOST CUDA_DEVICE
void calculate (const Coordinates<Typ>&, const Coordinates<Typ>&);
};
template <class Typ>
Coordinates<Typ>::Coordinates(const Typ& X, const Typ& Y, const Typ& Z)
: x{X}, y{Y}, z{Z}
{}
template <class Typ>
__host__ __device__
void Rel_Coordinates<Typ>::calculate (
const Coordinates<Typ>& element_A,
const Coordinates<Typ>& element_B
){
this -> x = element_B.x - element_A.x;
this -> y = element_B.y - element_A.y;
this -> z = element_B.z - element_A.z;
this -> dist2 = 0.;
this -> dist2 += std::pow(this -> x, 2);
this -> dist2 += std::pow(this -> y, 2);
this -> dist2 += std::pow(this -> z, 2);
this -> dist = std::sqrt(this -> dist2);
this -> dist_inv = 1 / this -> dist;
}
// main.cu
#include "class.cuh"
__device__ Coordinates<double> a {}, b {};
__device__ Rel_Coordinates<double> ba {};
__global__ void cal (
Coordinates<double> & a,
Coordinates<double> & b,
Rel_Coordinates<double> & ba
){
a.x = 0.;
a.y = 0.;
a.z = 0.;
b.x = 0.;
b.y = 3.;
b.z = 4.;
ba.calculate (a, b);
}
int main () {
cal <<<1,1>>> (a, b, ba);
}
and i am compiling it using:
nvcc main.cu class.cu -o main
I am runing this on Win11 with RTX 3070 mobile GPU.
I was expecing it to calculate relative position and distance. But i got this error:
main.cu
ptxas fatal : Unresolved extern function '_ZN15Rel_CoordinatesIdE9calculateERK11CoordinatesIdES4_'