-1

A large data file (having 1000000 rows and 2 columns) is required to be accessed from a device function. At every step of computation, the value of a variable changes in the device function and the values of a particular row of the data file is required. So the whole data file should be available in the device function as there is no control over the value of the variable at every step.

In the following program, the value of the variable is sent to a host device function from a device function and then the variable is sent to a host function (where the data file is available) from the host device function.The host function picks up the required values from the entire array and in this way the required values for one particular step may be got at the device function. But this process is not working for the code.

I don't know whether the keyword __host__ __device__ works in cuda V0.2.1221 or not.

Please suggest a way to accss the large data file in a device function.

The required portion of the code is given beow.

__host__
void magnetic( R *xx, R *magfield)
{
  float Bx[1000001],By[1000001];
  R x[3];
  int k;
  FILE *fp; 
  fp = fopen("field.dat", "r");
  for (int i=0;i<=1000000;i++)
  {
  fscanf(fp, "%f%f", &Bx[i], &By[i]);
  } 
  fclose(fp);
  printf("%f\t%f\n",Bx[0],By[0]);

  for( int i=0;i<3;i++){
  x[i]=*xx;
  xx++;
  }
  //printf("%f\t%f\t%f\n",x[0],x[1],x[2]);
  k=round((zi+x[2])/dz);
  magfield[0]=Bx[k];
  magfield[1]=By[k];
  magfield[2]=2.;
}
__host__
void magnetic(R *xx, R *magfield);

__host__ __device__
void field(R *xx, R *magfield){
  R x[3];
  for( int i=0;i<3;i++){
  x[i]=*xx;
  xx++;
  }
 magnetic(x,magfield);
}

__host__ __device__
void field( R *xx, R *magfield);

__device__
void eval_rhs(  R *f, R *df, R time, int istep) {
R magfield[3],vXB[3];
df[0] = f[3];
df[1] = f[4];
df[2] = f[5];

field( &f[0], magfield);

    crossmultiply(&f[3], magfield, vXB);

    df[3] = Ex +  vXB[0];
    df[4] = Ey +  vXB[1];
    df[5] = Ez +  vXB[2];

}
talonmies
  • 70,661
  • 34
  • 192
  • 269
S.Samanta
  • 7
  • 2
  • 2
    What is "cuda V0.2.1221"? – talonmies Apr 29 '16 at 08:56
  • There is a sample code to access large data from device code in the following answer: http://stackoverflow.com/questions/27282273/is-it-possible-to-access-hard-disk-directly-from-gpu/36899573#36899573 Maybe your question is a duplicate. – Florent DUGUET Apr 29 '16 at 09:25

1 Answers1

1

Question : Please suggest a way to accss the large data file in a device function.

Answer : Load the file on host side, copy the buffer from system to device memory, and then use this device-side buffer in your kernels and/or device functions.

int main()
{
    FILE* file;
    float* fileData;

    ... // Allocate buffer here

    ... // Open and load file here

    float* dev_fileData;
    cudaMalloc(/*allocate the same-sized array on device side*/);
    cudaMemcpy(/*copy from host-side fileData to device-side*/);

    kernel<<<blocks, threads>>>(dev_fileData);

    system("pause");
    return 0;
}

__device__ void doSomething(float* dev_fileData)
{
    // Read (and/or write, but avoid concurrent access) data here
}

__global__ void kernel(float* dev_fileData)
{
    // Some kernel code

    doSomething(dev_fileData);

    // Some other kernel code
}
Taro
  • 798
  • 8
  • 18