Здравствуйте, я пытаюсь реализовать функцию интеграции в CUDA, но я продолжаю получать нарушение прав доступа в ядре, и я просто не понимаю, почему!
#include <iomanip>
#include "cuda_runtime.h"
#include "device_launch_parameters.h"
#define R 10000
#define leftBound 1.0
#define rightBound 3.0
#define P 10
#define threads 512
#define MaxBlocks 65535
__global__ void cudaKernal(float *M, int x, int leftbound, float width)
{
unsigned int index = blockIdx.x * threads + threadIdx.x;
while(index < x)
{
int x = leftBound + width*index;
M[index] = (float)((exp(-pow((float)x,2))*cos((float)(P*x))) * width);
// Next run
index += blockDim.x * gridDim.x;
}
}
int main ()
{
float width = (rightBound - leftBound) / R;
int x = ceil((rightBound - leftBound) / width);
float total = 0;
// Trick for celin the total blocks
int TotalBlocks = (x+threads)/threads;
if(TotalBlocks > MaxBlocks)
TotalBlocks = MaxBlocks;
float *dev_M;
cudaMalloc((void**)&dev_M, x*sizeof(float));
cudaKernal<<<TotalBlocks,threads>>>(dev_M, x, leftBound, width);
float *M;
cudaMemcpy( M, dev_M, x*sizeof(float), cudaMemcpyDeviceToHost);
cudaFree(dev_M);
for (int i = 0; i < x; ++i) {
printf("M[i]=%f", M[i]);
total += M[i];
}
printf("The integral is: %f", total);
scanf_s("%f",123);
return 0;
}