# HELP! Errors :\[

**URL:** <https://forums.developer.nvidia.com/t/help-errors/2841>\
**Category:** CUDA Programming and Performance\
**Created:** [March 1, 2008, 10:51pm UTC](https://forums.developer.nvidia.com/t/help-errors/2841 "2008-03-01T22:51:25Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![casybaby](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@casybaby](https://forums.developer.nvidia.com/u/casybaby)\
**Post date:** [March 1, 2008, 10:51pm UTC](https://forums.developer.nvidia.com/t/help-errors/2841/1 "2008-03-01T22:51:25Z")

</div>

Hi all,

I have been working on this code for 2 days. I have tried everything I can, but still have the same error. At first I thought it is because I used global vars, but now I have moved all of them to function parameters. The problem still cannot be solved. Could you please help :[ ~~

The error I got:

Error 1 error LNK2019: unresolved external symbol \_\_Znaj referenced in function \__Z5runSWPcS_ SW2D.obj

Error 2 fatal error LNK1120: 1 unresolved externals …/…/bin/win32/Debug/SW2D.exe

```auto
#include <stdlib.h>

#include <stdio.h>

#include <string.h>

#include <math.h>

#include <cutil.h>

// includes, kernels

#include <SW2D_kernel.cu>

#include <SM2D.h>

////////////////////////////////////////////////////////////////////////////////

// declaration, forward

/*struct matrix

{

  int **p;

  int row;

  int cols;

};

*/

int getAntiDiagnolSize(int,int,int);

char *getAntiDiagnolArray(int,int,int,int distance[MAXLENGTH][MAXLENGTH]);

//void UpdateMatrix(int,char*,int,int,int,int,int distance[MAXLENGTH][MAXLENGTH],int trace[MAXLENGTH][MAXLENGTH]);

void TraceBack(char*,char*,int,int,int,int,int trace[MAXLENGTH][MAXLENGTH]);

void runSW(char*,char*);

//matrix* func_struc(int,int);

int main(int,char**);

////////////////////////////////////////////////////

//Program main

int main(int argc, char** argv){

     //printf("Target DNA: %s\n",Y);

     //printf("Query DNA:  %s\n\n", X);

     char Y[MAXLENGTH];

     char X[MAXLENGTH];

    printf("Enter the target DNA sequence :");

     scanf("%c",&Y);

     printf("\nEnter the query DNA sequence :");

     scanf("%c",&X);

     runSW(X,Y);

     CUT_EXIT(argc, argv);

}

////////////////////////////////////////////////////

////////////////////////////////////////////////////

void runSW(char* X,char* Y){

     /* check how long Y is */

     int n=0;

     while(Y[n] != 0) n++;

    /* check length of X */

     int m=0;

     while(X[m] != 0) m++;

     

     int i=0,j=0;

     int minMN = min(m,n);

     int distance[MAXLENGTH][MAXLENGTH],trace[MAXLENGTH][MAXLENGTH];

     //matrix *distance1=func_struc(n,m),*trace1=func_struc(n,m);

     int gMaxValue[3],hMaxValue[3];

 unsigned int hTimer;

     double timerValue;

     const int DATA_N = 100000000;

     const int DATA_SIZE = DATA_N * sizeof(unsigned char);

  

  CUT_DEVICE_INIT();

     CUT_SAFE_CALL( cutCreateTimer(&hTimer) );

    m=(m==0?1:m);

     n=(n==0?1:n);

     

  char* gX = new char[m];

  char* gY = new char[n];

  

  int maxI=0,maxJ=0,maxValue=0;

     

  printf("Allocating GPU memory for two DNA sequence arrays...\n");

  CUDA_SAFE_CALL(cudaMalloc((void**)&gX,sizeof(char)*m));

  CUDA_SAFE_CALL(cudaMalloc((void**)&gY,sizeof(char)*n));

  printf("Copying these two DNA sequence arrays to GPU memory...\n");

  CUDA_SAFE_CALL(cudaMemcpy(gX,X,sizeof(char)*m,cudaMemcpyHostToDevice));

  CUDA_SAFE_CALL(cudaMemcpy(gY,Y,sizeof(char)*n,cudaMemcpyHostToDevice));

    

     printf("Starting running calculations on GPU recursively...\n");

  /* CPU control the loop for GPU */

  for(i=0; i<m+n-1; i++){

   int size = 0; 

         

   printf("  Preparing k-2 diagonal array...\n");

   //Prepare 'a' array for input,size is the length of k-2 anti-diagonal

   size = getAntiDiagnolSize(i-2,m,n);

   int x = size;

   // put the values to a

   char* a = new char;

   a = getAntiDiagnolArray(i-2,m,n,distance);

  char* aD = new char;

   size = size*sizeof(char);

   CUDA_SAFE_CALL(cudaMalloc((void**)&aD, size));

   CUDA_SAFE_CALL(cudaMemcpy(aD,a,size,cudaMemcpyHostToDevice));

  printf("  Preparing k-1 diagonal array...\n");

   //Prepare 'b' array for input, size is the length of k-1 anti-diagonal

   size = getAntiDiagnolSize(i-1,m,n);

   int y = size;

   // put the values to b

   char* b = new char; 

   b = getAntiDiagnolArray(i-1,m,n,distance);

  char* bD = new char;

   size = size*sizeof(char);

   CUDA_SAFE_CALL(cudaMalloc((void**)&bD,size));

   CUDA_SAFE_CALL(cudaMemcpy(bD,b,size,cudaMemcpyHostToDevice));

  printf("  Preparing k diagonal array...\n");

   /*

     Prepare 'c' array for input, c is a 1D array, which the 1st 'size' number of elements 

     are the score, the 2nd 'size' number of elements are the direction, size is the length of k anti-diagonal

   */

   size = getAntiDiagnolSize(i,m,n);

   int z = size;

   char* c = new char;

   for(i=0; i<size*2; i++){

    c[i] = '0';

   }

   char* cD = new char;

   CUDA_SAFE_CALL(cudaMalloc((void**)&cD,size*sizeof(char)*2));

   CUDA_SAFE_CALL(cudaMemcpy(cD,c,size*2,cudaMemcpyHostToDevice));

  

   printf("  Preparing maximum value, maxI & maxJ...\n");

   hMaxValue[0] = maxValue;

   hMaxValue[1] = maxI;

   hMaxValue[2] = maxJ;

   

   CUDA_SAFE_CALL(cudaMalloc((void**)&gMaxValue,sizeof(int)*3));

  // CUDA_SAFE_CALL(cudaMemcpyToSymbol(gMaxValue,hMaxValue,sizeof(int)*3));

   CUDA_SAFE_CALL(cudaMemcpy(gMaxValue,hMaxValue,sizeof(hMaxValue),cudaMemcpyHostToDevice));

  // CUDA_SAFE_CALL(cudaMemcpy(gMaxI,maxI,sizeof(maxI),cudaMemcpyHostToDevice));

  // CUDA_SAFE_CALL(cudaMemcpy(gMaxJ,maxJ,sizeof(maxJ),cudaMemcpyHostToDevice));

      

   dim3 dimBlock(BLOCK_SIZE);

   dim3 dimGrid(size/BLOCK_SIZE); 

        CUT_SAFE_CALL( cutStartTimer(hTimer) );

         

   printf("  Call GPU to calculate one anti-diagonal values...\n");

   //SWOnDevice<<<dimGrid,dimBlock>>>(aD,x,bD,y,cD,z,i,m,n,gX,gY);

   SWOnDevice<<<dimGrid,dimBlock>>>(aD,x,bD,y,cD,z,i,m,n,gX,gY,gMaxValue);

   

         CUT_SAFE_CALL( cutStopTimer(hTimer) );

         timerValue = cutGetTimerValue(hTimer);

         printf("  gpu() time : %f msec //%f MB/sec\n", timerValue, DATA_SIZE / (1048576.0 * timerValue * 0.001));

        

   printf("  Copy k diagonal, maxValue, maxI & maxJ back to CPU...\n");

   CUDA_SAFE_CALL(cudaMemcpy(c,cD, size*2, cudaMemcpyHostToDevice));

   CUDA_SAFE_CALL(cudaMemcpy(hMaxValue, gMaxValue, sizeof(gMaxValue), cudaMemcpyHostToDevice));

  printf("  Update distance and trace matrixs...\n");

 if(0<=i<minMN){

    for(j=0;j<size;j++){

    	distance[minMN-1-j][j] = c[j];

    	trace[minMN-size-j][j] = c;

  	}

  }else if(minMN<=i<m+n-1){

    if(m<=n){

      for(j=0;j<size;j++) {

      	distance[minMN-j-1][i-minMN+i+1] = c[j];

      	trace[minMN-j-1][i-minMN+j+1] = c;

      }

    }else for(j=0;j<size;j++){

          distance[i-j][j] = c[j];

       trace[minMN-j-1][j] = c[i*2+i];

    }

  }

         

   printf("  Free memories of three arrays in GPU...\n");

   cudaFree(aD);

   cudaFree(bD);

   cudaFree(cD);

   

      printf("-------------------------------------------------\n");

  }

  printf("Trace back trace matrix...\n");  

  TraceBack(X,Y,m,n,hMaxValue[1],hMaxValue[2],trace);

}

int getAntiDiagnolSize(int position,int m,int n){

      

   int size = 0;

   int minMN = min(m,n);

   int maxMN = max(m,n);

   if(position==-2) size=1;

   else if(position==-1) size=2;

    else if(position>=0){

    if(position<minMN) size = position+1;

    else if(position>m+n-minMN-1) 

      //size = maxMN-distMN-(position-minMN);

      size = m+n-1-position;

      else size = maxMN-maxMN+minMN;

    }

   return size;

}

char *getAntiDiagnolArray(int loop,int m,int n,int distance[MAXLENGTH][MAXLENGTH]){

       int i=0;

        int minMN = min(m,n);

        int l = getAntiDiagnolSize(loop,m,n);

        char *res = new char[l];

  if(loop==-2) res[0] = 0;

  else if(loop==-1) res[0]=res[1]=0;

  	else if(loop>=0){

    if(loop<minMN){

    	for(i=0;i<l;i++){

      res[i] = distance[minMN-1-i][i];

    	}

    }else if(minMN<=loop<m+n-1){

    	if(m<=n){

       for(i=0;i<l;i++) res[i]=distance[minMN-i-1][loop-minMN+i+1];

    	}else 

      for(i=0;i<l;i++) res[i]=distance[loop-i][i];

    }

  	}

     return res;

}

```

Thousands of thanks!!!

---

<div class="post-metadata">

**Author:** ![Anjul\_Patney](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/anjul_patney/32/47470_2.png) [@Anjul\_Patney](https://forums.developer.nvidia.com/u/Anjul_Patney)\
**Post date:** [March 2, 2008, 1:44am UTC](https://forums.developer.nvidia.com/t/help-errors/2841/2 "2008-03-02T01:44:38Z")

</div>

Hi,

What kind of code do you have in SW2D\_kernel.cu, SM2D.h? I think the problem might have to do with what you have in the header file.

---

<div class="post-metadata">

**Author:** ![davidchan](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@davidchan](https://forums.developer.nvidia.com/u/davidchan)\
**Post date:** [March 2, 2008, 4:06am UTC](https://forums.developer.nvidia.com/t/help-errors/2841/3 "2008-03-02T04:06:17Z")

</div>

> [@](#):
>
> Hi,
> 
> What kind of code do you have in SW2D\_kernel.cu, SM2D.h? I think the problem might have to do with what you have in the header file.
> 
> [snapback]336325[/snapback]

There is no definition of the device func,when I comment the call of SWOnDevice\<\<\<\>\>\> ,I found no err compile in Release mode.

SWOnDevice\<\<\<dimGrid,dimBlock\>\>\>(aD,x,bD,y,cD,z,i,m,n,gX,gY,gMaxValue);

---

<div class="post-metadata">

**Author:** ![casybaby](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@casybaby](https://forums.developer.nvidia.com/u/casybaby)\
**Post date:** [March 2, 2008, 4:38am UTC](https://forums.developer.nvidia.com/t/help-errors/2841/4 "2008-03-02T04:38:33Z")

</div>

> [@](#):
>
> There is no definition of the device func,when I comment the call of SWOnDevice\<\<\<\>\>\> ,I found no err compile in Release mode.
> 
> SWOnDevice\<\<\<dimGrid,dimBlock\>\>\>(aD,x,bD,y,cD,z,i,m,n,gX,gY,gMaxValue);
> 
> [snapback]336389[/snapback]

Thx for all your replies.

I still find the same errors when I commented the kernel function. The following is the rest of my code:

SM2D.h

```auto
#define STOP 0

#define UP 1

#define LEFT 2

#define DIAG 3

#define MATCHSCORE 10

#define MISMATCHSCORE -7

#define GAPSCORE -4

#define BLOCK_SIZE 512

#define M 100

#define N 100

#define MAXLENGTH 200

#define max(a,b) ((a) < (b) ? (b) : (a))

#define min(a,b) ((a) < (b) ? (a) : (b))

```

SW2D\_kernel.cu

```auto
#include <stdio.h>

#include <SM2D.h>

////////////////////////////////////////////////////////////////////////////////

//! Device kernel

////////////////////////////////////////////////////////////////////////////////

// __global__ void SWOnDevice(char* a,int x,char* b,int y,char* c,int z,int loop,int m,int n,char* gX,char* gY){

__global__ void SWOnDevice(char* a,int x,char* b,int y,char* c,int z,int loop,int m,int n,char* gX,char* gY,int* gMaxValue){           

        int id = blockIdx.x*BLOCK_SIZE+threadIdx.x;

        int dist = 0;

        

        // __local__ is removed?

  // __local__ int gTmpValue;

        // __local__ int gTmpDir = STOP;

        

        int gTmpValue;

        int gTmpDir = 0;

        

  int tmp = 0;

 //X is query DNA, Y is target DNA

  if(loop<m){

    

     if(id-1>=0) tmp = a[id-1];

     //get value from diagonal element

     if(gX[m-id]==gY[id]) gTmpValue = tmp+MATCHSCORE;

     else gTmpValue = tmp+MISMATCHSCORE;

  

     if(gTmpValue>dist){

    dist = gTmpValue;

      	gTmpDir = DIAG;

     }

              

     if(id!=y) tmp = b[id];

     //get value from upper element

     gTmpValue = tmp+GAPSCORE;

     if(gTmpValue>dist){

    dist = gTmpValue;

    gTmpDir = UP;

     }

              

     if(id-1>=0) tmp = b[id-1];

     //get value from left element

       gTmpValue = tmp+GAPSCORE;

     if(gTmpValue>dist){

    dist = tmp;

    gTmpDir = LEFT;

     }

         }else{

        if(id+1!=x) tmp = a[id+1];

     //get value from diagonal element

     if(gX[m-id]==gY[id])

    gTmpValue = tmp+MATCHSCORE;

     else gTmpValue = tmp+MISMATCHSCORE;

  

     if(gTmpValue>dist){

    dist = gTmpValue;

      	gTmpDir = DIAG;

     }

              

     if(id+1==y) tmp = b[id+1];

     //get value from upper element

     gTmpValue = tmp+GAPSCORE;

     if(gTmpValue>dist){

    dist = gTmpValue;

    gTmpDir = UP;

     }

             

              tmp = b[id];

     //get value from left element

       gTmpValue = tmp+GAPSCORE;

     if(gTmpValue>dist){

    dist = gTmpValue;

    gTmpDir = LEFT;

     }        

   }

   c[id] = dist;

   c[id*2] = gTmpDir;

         

   /* keep track of where the minimum score is */

   if(dist>gMaxValue[0]) { 

         gMaxValue[0] = dist;

         gMaxValue[1] = m-id;

      gMaxValue[2] = id;

      }

}

```

---

<div class="post-metadata">

**Author:** ![davidchan](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@davidchan](https://forums.developer.nvidia.com/u/davidchan)\
**Post date:** [March 2, 2008, 6:18am UTC](https://forums.developer.nvidia.com/t/help-errors/2841/5 "2008-03-02T06:18:13Z")

</div>

I found your problem:

1. 

```auto
char* a = new char;

```

should be :

```auto
char *a = (char*)malloc(sizeof(char) * xxx);

```

This is the Direct cause,you cannot use ‘new’ to…

2.Before cudaMalloc(),there’s no need to malloc a host mem,

```auto
char *aD = NULL;

cudaMalloc((void**)&aD,...);

```

is enough.

1. 

```auto
char *getAntiDiagnolArray(int loop,int m,int n,int distance[MAXLENGTH][MAXLENGTH]/*, char *res*/){

	...

	int l = getAntiDiagnolSize(loop,m,n);

	char *res = new char[l];//

 Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â ...

Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â return res;//

}

```

```auto
char *res = new char[l];

```

res is a local temp var,the return value is not valid,you should use func param to pass the addr.

---

<div class="post-metadata">

**Author:** ![casybaby](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@casybaby](https://forums.developer.nvidia.com/u/casybaby)\
**Post date:** [March 2, 2008, 8:08am UTC](https://forums.developer.nvidia.com/t/help-errors/2841/6 "2008-03-02T08:08:35Z")

</div>

> [@](#):
>
> I found your problem:
> 
> 1. 
> 
> ```auto
> char* a = new char;
> 
> ```
> 
> should be :
> 
> ```auto
> char *a = (char*)malloc(sizeof(char) * xxx);
> 
> ```
> 
> This is the Direct cause,you cannot use ‘new’ to…
> 
> 2.Before cudaMalloc(),there’s no need to malloc a host mem,
> 
> ```auto
> char *aD = NULL;
> 
> cudaMalloc((void**)&aD,...);
> 
> ```
> 
> is enough.
> 
> 1. 
> 
> ```auto
> char *getAntiDiagnolArray(int loop,int m,int n,int distance[MAXLENGTH][MAXLENGTH]/*, char *res*/){
> 
> ...
> 
> int l = getAntiDiagnolSize(loop,m,n);
> 
> char *res = new char[l];//
> 
>                ...
> 
>                return res;//
> 
> }
> 
> ```
> 
> ```auto
> char *res = new char[l];
> 
> ```
> 
> res is a local temp var,the return value is not valid,you should use func param to pass the addr.
> 
> [snapback]336428[/snapback]

Oh, my God!!! davidchan, I almost cry~~~ It finally works!!! I wish I could give you a hug!!! Finally I can go to bed!!! I really really appreciate your help!!! Thx!!

---

<div class="post-metadata">

**Author:** ![davidchan](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@davidchan](https://forums.developer.nvidia.com/u/davidchan)\
**Post date:** [March 2, 2008, 8:58am UTC](https://forums.developer.nvidia.com/t/help-errors/2841/7 "2008-03-02T08:58:01Z")

</div>

ä¸ç”¨å®¢æ°”
