Everything is undefined ? First time cuda programming

I am currently writing my first CUDA program. I am transferring my serial Jacobi Method code to a parallel code. I left some for loops in my host code and when I tried compiling my code I received a lot of undefined errors. These errors only occur for my non-array values. For instance using i or j to iterate in a for loop. The only solution I can find is to redefine the variable every time I use it, which doesn’t make sense. Can someone explain this error.

Also what is this error?
calling a host function from a device/global function is only allowed in device emulation mode

Show a small code fragment which gives the error…

You must be trying to call a function such as printf from within a device or global function. Again, can you show an example?

WITH EXAMPLES:

I am currently writing my first CUDA program. I am transferring my serial Jacobi Method code to a parallel code. I left some for loops in my host code and when I tried compiling my code I received a lot of undefined errors. These errors only occur for my non-array values. For instance using i or j to iterate in a for loop. The only solution I can find is to redefine the variable every time I use it, which doesn’t make sense. Can someone explain this error. This is the same code I used in my serial version so I am unsure why they are acting this way.

[codebox]unsigned int j, i;

for (i=0; i<row; i++)

{

for (j=0; j<col; j++)

{

fscanf(inFile,"%f", &Mat[i][j]);

}

}

for (i=0; i<row; i++)

fscanf(inFile,"%f",&BMat[i]);[/codebox]

Also what is this error?

(118): calling a host function from a device/global function is only allowed in device emulation mode

I’m not not using any printf or scanf in the kernel Here is my kernel for dividing the A matrix to the Up, Low, and Inverse Diagonal matrices.

[codebox]#ifndef DIVIMATRIX_KERNEL

#define DIVIMATRIX_KERNEL

#include <stdio.h>

#include <math.h>

global void Jacobi_It(float* A, float* Up, float* Low, float* InvDia, float* Xold, unsigned int row, unsigned int col)

{

unsigned int n;

// Block Index

int bx= blockIdx.x;

int by= blockIdx.y;

// Thread Index

int tx= threadIdx.x;

int ty= threadIdx.y;

// Step Size

unsigned int stpsz=BLOCK_SIZE;

// Start index for each sub-matrix to be processed

int aBegin=rowBLOCK_SIZEby;

// End index for sub-matrix

int aEnd=aBegin+row-1;

for (n=aBegin; n<=aEnd; n+=stpsz)

{__shared__ float SubA[BLOCK_SIZE][BLOCK_SIZE];

 __shared__ float SubUp[BLOCK_SIZE][BLOCK_SIZE];

 __shared__ float SubLow[BLOCK_SIZE][BLOCK_SIZE];

 __shared__ float SubInD[BLOCK_SIZE][BLOCK_SIZE];

 __shared__ float SubX[BLOCK_SIZE];

SubA[tx][ty]=A[n+row*tx+ty];

if (bx == by)

   {if (SubA[tx][tx]==0)

   SubInD[tx][tx]=0;

    else

      {SubInD[tx][tx]=1/SubA[tx][tx];

   SubUp[tx][tx]=0;

   SubLow[tx][tx]=0;

      }

if (ty > tx)

  {SubUp[tx][ty]=SubA[tx][ty];

   SubLow[tx][ty]=0;

   SubInD[tx][ty]=0;

  }

if (tx > ty)

  {SubUp[tx][ty]=0;

   SubLow[tx][ty]=SubA[tx][ty];

   SubInD[tx][ty]=0;

   }

 if (bx > by)

   {SubInD[tx][ty]=0;

SubLow[tx][ty]=SubA[tx][ty];

SubUp[tx][ty]=0;

   }

 if (by > bx)

   {SubInD[tx][ty]=0;

    SubLow[tx][ty]=0;

SubInD[tx][ty]=SubA[tx][ty];

   }

 SubX[tx]=0;

__syncthreads();

 Up[n+row*tx+ty]=SubUp[tx][ty];

 Low[n+row*tx+ty]=SubLow[tx][ty];

 InvDia[n+row*tx+ty]=SubInD[tx][ty];

 Xold[n+row*tx]=SubX[tx];

}

#endif // ifndef DIVIMATRIX_KERNEL

[/codebox]

Ok I feel as if the compiler thinks my host file is a device file (not sure what that means). At function in the host code (printf, fscanf, etc) I receive an error from the compiler:

calling a host function from a device/global function is only allowed in device emulation mode

At the beginning I receive:

error: identifier “main” is undefined

Here is first few lines of my host code

[codebox]#include <stdio.h>

#include <stdlib.h>

#include <math.h>

#include <DiviMatrix.cu>

define CONVERGENCE 0.001

define MAXITERATIONS 50

define BLOCK_SIZE 16

main(){

float UpSum=0; /* UpMat sum used for Jacobi It */

float LowSum=0; /* LowMat sum used for Jacobi It */

unsigned int count_it=1; /* Number of iterations */

unsigned int j, i;

unsigned int count_conv=0; /* Count_conv argument if all converge */

unsigned int row, col; /* Number of Rows and Columns in A Matrix */

unsigned int placeholder, n; /* Placeholder - tells which rows to switch */

float SubMat /* Substitution Variable */

FILE *inFile;

/******************** Read in A and B Matrix ***********************/

inFile=fopen(“JacobiMatrix.dat”,“r”); /* Matrix File Extension */

if (inFile == NULL) /* If the program doesn’t exist */

{

printf(“\n The file was not successfully opened”);

printf(“\n Please Check that the file JacobiMatrix.dat exists. \n”);

exit(1);

}

fscanf(inFile,“%d”, &row);

fscanf(inFile,“%d”, &col);

float Mat[row][col]; /* A matrix */

float BMat[row]; /* B matrix */

float Inv_DiaMat [row][col]; /* Inverse Diagonal Matrix */

float UpMat [row][col]; /* Upper Matrix */

float LowMat [row][col]; /* Lower Matrix */

float Xold [col]; /* Vector of previous x values */

float XVec [col]; /* Vector of current x values */

for (i=0; i<row; i++)

{

for (j=0; j<col; j++)

{

fscanf(inFile,"%f", &Mat[i][j]);

}

}

for (i=0; i<row; i++)

fscanf(inFile,"%f",&BMat[i]);

fclose(inFile);

[/codebox]

I think it would be useful to know what’s on line 118 of the file :)

N.