代码编译没有错误,但调试时出现访问冲突消息。任何人都可以指出下面的代码有什么问题吗?
我的代码实际上运行了同一方程的 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;
}