CUDA数据传输到设备提供非法内存访问

时间:2019-07-16 18:31:30

标签: cuda

我正在尝试将一些数据传输到设备上,并得到标题中提到的错误。该代码可以处理文件中的特定点,并且我可以验证该点之前的数据传输是正确的。文件总大小约为80 Mb,明显小于板载内存的总大小(我使用的是GTX 1080 Ti),可以在此处下载文件: https://gofile.io/?c=56fmM4


std::vector<std::vector<std::vector<double>>> table3d;
std::vector<std::vector<double>> x;
std::vector<std::vector<double>> domain;
std::vector<double> range;
int dim;

void ReadTable(char const * tableFileName)
{
   range.push_back(INFINITY); range.push_back(-INFINITY);
   try {
      std::fstream myfile(tableFileName, std::ios_base::in);
      if (!myfile.good()) throw 0;

      int a;
      while (myfile >> a) {
         dim = (int)a;

         std::vector<int> nPoints(dim, 0);
         x.resize(dim);

         domain.resize(dim, std::vector<double>(2, 0));

         for (int i = 0; i < dim; i++) {
            myfile >> nPoints[i];
            x[i].resize(nPoints[i]);
            for (int j = 0; j < nPoints[i]; j++) {
               myfile >> x[i][j];
            }
         }

         table3d.resize(nPoints[0], std::vector<std::vector<double>>(nPoints[1], std::vector<double>(nPoints[2], 0)));
         for (int i = 0; i < nPoints[0]; i++)
            for (int j = 0; j < nPoints[1]; j++)
               for (int k = 0; k < nPoints[2]; k++) {
                  myfile >> table3d[i][j][k];
               }
      }

      myfile.close();
   }
   catch (std::exception&) {
      printf("NUTableInterpolation::ReadTable: failed reading file %s\n", tableFileName);
   }
}

__global__ void copytable3d(const double* data, const unsigned int len)
{
   double* tmp = new double[len]; // tmp is supposed to be a structure defined on device
   for (int i = 0; i < len; ++i) {
      tmp[i] = data[i];
   }
}

int main()
{
   ReadTable("file.csv");

   double* d_table3d = nullptr;
   for (int i = 0; i < table3d.size(); ++i) {
      printf("%d: ", i);
      for (int j = 0; j < table3d[i].size(); ++j) {
         printf("%d, ", j);
         checkCudaErrors(cudaMalloc((void **)&d_table3d, table3d[i][j].size() * sizeof(double)));
         checkCudaErrors(cudaMemcpy(d_table3d, table3d[i][j].data(), table3d[i][j].size() * sizeof(double), cudaMemcpyHostToDevice));
         copytable3d << <1, 1 >> >(d_table3d, table3d[i][j].size());
         checkCudaErrors(cudaDeviceSynchronize());
         checkCudaErrors(cudaGetLastError());
         checkCudaErrors(cudaFree(d_table3d));
      }
      printf("\n");
   }
   printf("done\n");

   return 0;
}

0 个答案:

没有答案