Need some help for the CUDA c codes

hello everyone
I’m a new learner of CUDA and I try to run c codes with cuda. And I get the following problems:

cuda_spline.cu(33): error: no instance of constructor “std::vector<_Tp, _Alloc>::vector [with _Tp=double, _Alloc=std::allocator]” matches the argument list
argument types are: (int *)
cuda_spline.cu(34): error: operand types are incompatible (“double *” and “double”)
cuda_spline.cu(38): error: expression must have arithmetic or enum type
cuda_spline.cu(47): error: operand types are incompatible (“double *” and “double”)
cuda_spline.cu(51): error: expression must have integral or enum type
cuda_spline.cu(51): error: expression must have integral or enum type
cuda_spline.cu(51): error: expression must have integral or enum type
cuda_spline.cu(51): error: expression must have integral or enum type
cuda_spline.cu(51): error: expression must have integral or enum type
cuda_spline.cu(51): error: expression must have integral or enum type
cuda_spline.cu(53): error: expression must have integral or enum type
cuda_spline.cu(53): error: no operator “” matches these operands
operand types are: std::vector<double, std::allocator> [ int * ]
cuda_spline.cu(53): error: expression must have integral or enum type
cuda_spline.cu(54): error: a value of type “int *” cannot be assigned to an entity of type “int”
cuda_spline.cu(140): warning: expression has no effect
cuda_spline.cu(140): error: too few arguments in function call
cuda_spline.cu(160): error: return value type does not match the function type

cuda_spline.cu(89): warning: variable “yp1” was set but never used

cuda_spline.cu(89): warning: variable “ypn” was set but never used
cuda_spline.cu(89): warning: variable “y2” was set but never used
cuda_spline.cu(173): warning: variable “NCUT” was declared but never referenced
16 errors detected in the compilation of “/var/folders/6d/nv995sqn1zq5vzv79xrjd6hrspqd54/T//tmpxft_0001554b_00000000-8_cuda_spline.cpp1.ii”.

I tried many times, but still not working.
I have already put cuda header, and these are problems for me .
Thank you in advance!
my code is here, <cuda_spline.cu>

#include <cuda.h>
#include <complex>
#include <iostream>
#include <fstream>
#include <math.h>
#include <float.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include <stdio.h>
#include <time.h>
#include <iomanip>
#include <vector>
#include <algorithm>
#define THREADS_PER_BLOCK 512
using namespace std;
const double pi = 3.14159265358979323846E+000;

__global__ void spline(double *x, double *y, int *n, double *yp1, double *ypn, double *y2){

    int i= blockIdx.x * blockDim.x + threadIdx.x;
    int k;
    double p, qn, sig, un;//, *u
    
    vector<double> u(n-1);
    if (yp1 > 0.99e30)
        y2[0] = u[0] = 0.0;
    else{
        y2[0] = -0.5;
        u[0] = (3.0/(x[1]-x[0]))*((y[1]-y[0])/(x[1]-x[0])-yp1);
    }
    for (i=1; i<n-1; i++) {
        sig = (x[i]-x[i-1])/(x[i+1]-x[i-1]);
        p = sig*y2[i-1]+2.0;
        y2[i] = (sig-1.0)/p;
        u[i] = (y[i+1]-y[i])/(x[i+1]-x[i])-(y[i]-y[i-1])/(x[i]-x[i-1]);
        u[i] = (6.0*u[i]/(x[i+1]-x[i-1])-sig*u[i-1])/p;
    }
    if (ypn > 0.99e30)
        qn = un =0.0;
    else{
        qn = 0.5;
        un = (3.0/(x[n-1]-x[n-2]))*(ypn-(y[n-1]-y[n-2])/(x[n-1]-x[n-2]));
    }
    y2[n-1] = (un-qn*u[n-2])/(qn*y2[n-2]+1.0);
    for (k=n-2; k>=0; k--)
        y2[k] = y2[k]*y2[k+1]+u[k];

}

void splint(double xa[], double ya[], double y2a[], int n, double x, double yy[2])
{
    
    void nrerror(char error_text[]);
    int klo, khi;
    double h, i, b, a;
    
    klo = 0;
    khi = n-1;
    while (khi-klo>1) {
        if (xa[khi]>x && xa[khi-1]>x)
            khi -=1;
        else if (xa[khi]>x && xa[khi-1]<x) klo =khi-1;
        //cout<<khi<<" "<<klo<<" "<<khi-klo<<endl;           //show this if we wantto know the results of khis  and klos (test)
    }
    h = xa[khi]-xa[klo];
    i = ya[khi]-ya[klo];
    if (h == 0.0) cout<<"Bad Xa input to routine splint"<<endl;
    a = (xa[khi]-x)/h;
    b = (x-xa[klo])/h;
    yy[0] = a*ya[klo]+b*ya[khi]+((a*a*a-a)*y2a[klo]+(b*b*b-b)*y2a[khi])*(h*h)/6.0;     // y at x
    yy[1] =  i/h-(3.0*a*a-1.0)*h*y2a[klo]/6.0+(3.0*b*b-1.0)*h*y2a[khi]/6.0;            // y' at x
}


void chemi(double _gamma, double& _mu_local, double& _d_mu_n, double& _d_mu_gamma){
const double CC = 0.050;
const int M = 100020; double _mu_local_gamma;

//////////////
double *x, *y, *yp1, *ypn, *y2;  int *n;
double *d_x, *d_y, *d_yp1, *d_ypn, *d_y2; int *d_n;
cudaMalloc( (void **) &d_x, M);
cudaMalloc( (void **) &d_y, M);
cudaMalloc( (void **) &d_n, 1);
cudaMalloc( (void **) &d_yp1, 1);
cudaMalloc( (void **) &d_ypn, 1);
cudaMalloc( (void **) &d_y2, M);
x = (double *)malloc(M);
y = (double *)malloc(M);
n = (int *)malloc(1);
yp1 = (double *)malloc(1);
ypn = (double *)malloc(1);
y2 = (double *)malloc(M);
//////////////
static double data_1[M], data_2[M];
//read file1 to data_1 --- gamma
std::ifstream input("gamma.dat");  
for (int i = 0; i < M; i++) {
input >> data_1[i];
x[i] = data_1[i];
}
std::ifstream inputt("mu.dat");
for (int i = 0; i < M; i++) {
inputt >> data_2[i];
y[i] = data_2[i];
}


if (_gamma == 0.0) {
_mu_local = 0.0;
_d_mu_n = 0.0;
_d_mu_gamma = 0.0;

}
else if (_gamma <= 0.008) {
_mu_local = (CC*CC) / (2.0*_gamma*_gamma) * (2.0*_gamma - 2.0*pow(_gamma, 3.0 / 2.0) / pi);
_d_mu_n = CC / (2.0*_gamma) * (2.0*_gamma - pow(_gamma, 3.0 / 2.0) / pi);

}
else if (_gamma > 0.008 && _gamma <= 1000.0){
double y2der[M], res[2]; // 2nd derivative
double yp1 = 1.91564450000017e+000, ypn = 250.032500701103e-003; //1st derivative at n=1,n
res[0] = 0.0;
//////////////
cudaMemcpy( d_x, &x, M, cudaMemcpyHostToDevice );
cudaMemcpy( d_y, &y, M, cudaMemcpyHostToDevice );
cudaMemcpy( d_n, &n, 1, cudaMemcpyHostToDevice );
cudaMemcpy( d_yp1, &yp1, 1, cudaMemcpyHostToDevice );
cudaMemcpy( d_ypn, &ypn, 1, cudaMemcpyHostToDevice );
//////////////
spline<<<( M+(THREADS_PER_BLOCK -1 )/THREADS_PER_BLOCK,THREADS_PER_BLOCK ) >>> (d_x, d_y, d_n, d_yp1, d_ypn, d_y2);
//////////////
cudaMemcpy( y2der, d_y2, M, cudaMemcpyDeviceToHost);
//////////////
splint(data_1, data_2, y2der, M, _gamma, res);
_mu_local_gamma = res[0];
_mu_local = CC*CC / (2.0*_gamma*_gamma) * _mu_local_gamma;
_d_mu_gamma = res[1];
_d_mu_n = CC / (2 * _gamma) * (2.0*_mu_local_gamma - _gamma*_d_mu_gamma);
//////////////


//////////////
}
else if (_gamma > 1000.0){
_mu_local = (pi*pi) * (CC*CC) / (2.0*_gamma*_gamma) * (1.0 - 16.0 / (3.0*_gamma) + 20.0 / (_gamma*_gamma));
_d_mu_n = (pi*pi) * CC / _gamma * (1.0 - 8.0 / _gamma + 40.0 / (_gamma*_gamma));

}
else  cout << "gamma is " << _gamma << " , not real!" << endl;
return 0;
}


int main(){

    ////////////////////////////////////////////////////////////////////////////////////////
    //==> Case 4
    struct tm when;
    time_t now;
    time(&now);
    when = *localtime(&now);
    cout << "current time is " << asctime(&when) << flush;
	const int NCUT = 1596;
	double gamma = 0.0, mu_local = 0.0, d_mu_n = 0.0, d_mu_gamma = 0.0;
	cout << "enter gamma: " << endl;
	cin >> gamma;
	cout << "Gamma is " << gamma << endl;
	chemi(gamma, mu_local, d_mu_n, d_mu_gamma); // we get the local chemical and its derivative
	cout << "mu(n) is " << mu_local << endl;
	cout << "d_mu(n) is " << d_mu_n << endl;
    time(&now);
    when = *localtime(&now);
    cout << "current time is " << asctime(&when) << flush;
    getchar();//system("pause");
}

Some issues:

  1. You cannot use type:

vector<…>

in GPU device code. You might be able to substitute the offending line:

vector<double> u(n-1);

with something like this:

double *u = new double[n-1];

(if you choose to do this conversion, make sure you have enough device heap space for the new operator. Ideally you should test the returned pointer for null in your code.)

However this brings us to our next issue:

  1. n, as used in the kernel code line above, is a pointer parameter. You cannot use a pointer directly as an initializer either for a vector (size) or new. Perhaps you meant something like:
vector<double> u(*n-1);

which, correspondingly, could be converted to:

double *u = new double[*n-1];

and should compile cleanly, at least.

  1. The remainder of your errors appear to stem from a fundamental misunderstanding of the difference between an ordinary variable, and a pointer. In general these issues are not specific to CUDA. For example, in this line:
if (yp1 > 0.99e30)

yp1 is a pointer parameter. It makes little sense to compare it to a floating point value. Perhaps you meant something like this:

if (*yp1 > 0.99e30)

There are other instances like this in your code. It looks like you did a naive conversion of C code to CUDA without taking into account that your parameters to the kernel are being passed by pointer.

  1. Your kernel launch parameters are also not correct:

This:

spline<<<( M+(THREADS_PER_BLOCK -1 )/THREADS_PER_BLOCK,THREADS_PER_BLOCK ) >>> (...);

Should be like this:

spline<<< (M+THREADS_PER_BLOCK -1 )/THREADS_PER_BLOCK,THREADS_PER_BLOCK >>> (...);

Note the parenthesis differences carefully.

I have no comment as to whether or not these changes make sense in light of the intent of your code, which you haven’t described. These suggestions are merely based on the obvious compile errors that I see.

Of great help!
It’s the problem of pointer! i fix all the pointer problem, and now there is only 1 problem left when it’s using the function:
cuda_spline.cu(132): error: too few arguments in function call
but the total input arguments are all there! Do you know why?
Thank you very much!

All problems solved!!
Thank you a lot!
I learn a lot from you!
Thanks again!