c - C CUDA代码中的访问冲突

标签 c cuda

代码编译没有错误,但调试时出现访问冲突消息。任何人都可以指出下面的代码有什么问题吗?

我的代码实际上运行了相同方程的 1000 个实例的 1000 次迭代 它是一个递归非线性方程。目的只是欣赏能力 并行运行多个(迭代)方程。

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <conio.h>
#include <cuda.h>
#include <cutil.h>
#include <time.h> 

#define TOTAL_THREADS 1024
#define THREADS_PER_BLOCK 256
#define TOTAL_BLOCKS 4
#define VALUES_PER_THREAD 1000
#define THETA_VALUES_PER_THREAD 15

__global__ void my_compute(float *y_d, float *theta_d, float *u_d) 
{
   int offset = ((blockIdx.x * blockDim.x) + threadIdx.x) * VALUES_PER_THREAD;
   int theta_offset = ((blockIdx.x * blockDim.x) + threadIdx.x) * THETA_VALUES_PER_THREAD;

   for (int i = 7; i < 1000; i++) {
      y_d[offset + i] = theta_d[theta_offset + 0] * y_d[offset + i - 1] +
                        theta_d[theta_offset + 1] * y_d[offset + i - 3] +
                        theta_d[theta_offset + 2] * u_d[offset + i - 5] * u_d[offset + i - 4] +
                        theta_d[theta_offset + 3] + 
                        theta_d[theta_offset + 4] * u_d[offset + i - 6] +
                        theta_d[theta_offset + 5] * u_d[offset + i - 4] * y_d[offset + i - 6] +
                        theta_d[theta_offset + 6] * u_d[offset + i - 7] +
                        theta_d[theta_offset + 7] * u_d[offset + i - 7] * u_d[offset + i - 6] +
                        theta_d[theta_offset + 8] * y_d[offset + i - 4] +
                        theta_d[theta_offset + 9] * y_d[offset + i - 5] +
                        theta_d[theta_offset + 10] * u_d[offset + i - 4] * y_d[offset + i - 5] +
                        theta_d[theta_offset + 11] * u_d[offset + i - 4] * y_d[offset + i - 2] +
                        theta_d[theta_offset + 12] * u_d[offset + i - 7] * u_d[offset + i - 3] +
                        theta_d[theta_offset + 13] * u_d[offset + i - 5] +
                        theta_d[theta_offset + 14] * u_d[offset + i - 4];
   }
}

int main(void) 
{
   float y[1000000];
   FILE * fpoo;
   FILE * u;
   float theta[15000];
   float u_data[1000000];
   float *y_d;
   float *theta_d;
   float *u_d;
   cudaEvent_t start, stop;
   float time;

   cudaEventCreate(&start);
   cudaEventCreate(&stop);

   //memory allocation
   cudaMalloc((void **) &y_d, 1000000 * sizeof(float));
   cudaMalloc((void **) &theta_d, 15000 * sizeof(float));
   cudaMalloc((void **) &u_d, 1000000 * sizeof(float));
   cudaEventRecord(start, 0);

   // importing data for theta and input of model//
   fpoo = fopen("c:\\Fly_theta.txt", "r");
   u = fopen("c:\\Fly_u.txt", "r");

   for (int i = 0; i < 1000; i++) {
      for (int j = 0; j < 15; j++)
         fscanf(fpoo, "%f\n", &theta[15 * i + j]);
   } 
   for (int i = 0; i < 1000; i++) {
      for (int j = 0; j < 1000; j++)
         fscanf(u, "%f\n", &u_data[1000 * i + j]);
   }
   //initialising past input with the value of zero//
   for (int i = 0; i < 1000; i++) {
      for (int j = 0; j < 8; j++)
         y[8 * i + j] = 0;
   }
   cudaMemcpy(y_d, y, 1000000 * sizeof(float), cudaMemcpyHostToDevice);

   cudaMemcpy(theta_d, theta, 15000 * sizeof(float), cudaMemcpyHostToDevice);
   cudaMemcpy(u_d, u_data, 1000000 * sizeof(float), cudaMemcpyHostToDevice);

   //calling kernel function//
   my_compute <<< 4, 256 >>> (y_d, theta_d, u_d);

   cudaMemcpy(y, y_d, 1000000 * sizeof(float), cudaMemcpyDeviceToHost);

   for (int i = 0; i < 1000; i++) {
      for (int j = 0; j < 1000; j++)
         printf("%f", y[1000 * i + j]);
   } 
   cudaEventRecord(stop, 0);
   cudaEventSynchronize(stop);
   cudaEventElapsedTime(&time, start, stop);
   cudaEventDestroy(start);
   cudaEventDestroy(stop);
   printf("Time to generate:  %3.1f ms \n", time);

   cudaFree(y_d);
   cudaFree(theta_d);
   cudaFree(u_d);

   fclose(u);
   fclose(fpoo);

   //fclose();
   _getche();
   return 0;
}

最佳答案

你可能有堆栈溢出。

尝试在数组声明中添加一个 static 说明符:

static float y[1000000];
static float theta[15000];
static float u_data[1000000];

关于c - C CUDA代码中的访问冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11960905/

相关文章:

java - 如何将 16 位字符数组转换为 8 位字符数组并使用 JNA 发送到 C 代码?

c - 如何在 C/C++ 中使用 pthread 而不仅仅是带有 void 参数的 void 函数?

c++ - 在 GPU 上计算特征值和特征向量的性能不佳

c - 未接收来自 TCP 连接的文件传输

C-错误 : incomplete definition of type 'struct name_s'

c++ - 在 C++ 项目中使用基于 CUDA 的库代码

c++ - 无法打开 .cu 中的包含文件

c++ - main.obj : fatal error LNK1143: invalid or corrupt file: no symbol for COMDAT section 0x6

c++ - CUDA 执行时间

PHP扩展库依赖