У меня проблема с копированием данных с устройства на хост. Выделение памяти в C начинается с индекса -1. Матричная функция
nx1 = 0; nx2 = NG[1] - 1;
ny1 = 0; ny2 = NG[2] - 1;
nx1 = -nd + nx1; nx2 = nd + nx2;//value of nd=1 so nx1 becomes its initial value -1 and nx2=nx2+1
ny1 = -nd + ny1; ny2 = nd + ny2;// same follows here
// Memory allocation for static arrays
(*psv).sxx = matrix(ny1, ny2, nx1, nx2);
(*psv).sxy = matrix(ny1, ny2, nx1, nx2);
(*psv).syy = matrix(ny1, ny2, nx1, nx2);
(*psv).vx = matrix(ny1, ny2, nx1, nx2);
(*psv).vy = matrix(ny1, ny2, nx1, nx2);
: -
float** matrix(int nrl, int nrh, int ncl, int nch) {
/* allocate a float matrix with subscript range m[nrl..nrh][ncl..nch]
and intializing the matrix, e.g. m[nrl..nrh][ncl..nch]=0.0 */
int i, j, nrow = nrh - nrl + 1, ncol = nch - ncl + 1;
float** m;
/* allocate pointers to rows */
m = (float**)malloc((size_t)((nrow + NR_END) * sizeof(float*)));
if (!m) err("allocation failure 1 in function matrix() ");
m += NR_END;
m -= nrl;
/* allocation rows and set pointers to them */
m[nrl] = (float*)malloc((size_t)((nrow * ncol + NR_END) * sizeof(float)));
if (!m[nrl]) err("allocation failure 2 in function matrix() ");
m[nrl] += NR_END;
m[nrl] -= ncl;
for (i = nrl + 1; i <= nrh; i++) m[i] = m[i - 1] + ncol;
/* initializing matrix */
for (i = nrl; i <= nrh; i++)
for (j = ncl; j <= nch; j++) m[i][j] = 0.0;
/* return pointer to array of pointer to rows */
return m;
}
Я выделил это следующим образом, используя memcpy ..
//( after allocation the values nx & ny changes as follows
nx2 = NG[1] - 1; ny2=NG[2]; nx1=0; nx2=0;
//******My CUDA Code Starts***********
long int size1=((ny2+1 - ny1 + 2) * (nx2+1 - nx1 + 2)+1);// ny2 +1 to compensate
gpuErrchk(cudaMemcpy(d_vy, (*psv).vy[-1], size1 * sizeof(float), cudaMemcpyHostToDevice));
gpuErrchk(cudaMemcpy(d_sxx, (*psv).sxx[-1], size1 * sizeof(float), cudaMemcpyHostToDevice));
gpuErrchk(cudaMemcpy(d_sxy, (*psv).sxy[-1], size1 * sizeof(float), cudaMemcpyHostToDevice));
Мой код ядра выглядит следующим образом: -
if (j <= ny2 && j >= ny1&& i >= nx1 && i <= nx2) {
vxx = (hc[1] * (vx[j * ny2 + i] - vx[j * ny2 + i - 1])) * dtdx;// You can see i=0 j=0 val=-1
vyx = (hc[1] * (vy[j * ny2 + i + 1] - vy[j * ny2 + i])) * dtdx;//here it is going out of bound
vxy = (hc[1] * (vx[(j + 1) * ny2 + i] - vx[j * ny2 + i])) * dtdy;
vyy = (hc[1] * (vy[j * ny2 + i] - vy[(j - 1) * ny2 + i])) * dtdy;
}
Моя ошибка Снимок экрана с моей ошибкой Если кто-то хочет прояснить ситуацию, он может посмотреть мое репозиторий на github здесь