samedi 28 février 2015

Unhandled exception... Access Violation reading location

I'm following a tutorial on using NVIDIA/CUDA/etc. here: http://ift.tt/1N4nufK


I'm trying to add two vectors in parallel, but I am having trouble with these memory access violations mentioned in the title of my post.


The error is occurring at my printf line (I will post my code below), but if I comment it out I get taken to a file named "dbgheap.c" and I just get the same error message on line 1696 of that file (the file has 3268 lines)


The line is:



if (*pb++ != bCheck)


and the function that is in is:



extern "C" static int __cdecl CheckBytes(
unsigned char * pb,
unsigned char bCheck,
size_t nSize
)
{
while (nSize--)
{
if (*pb++ != bCheck) //this is the line with the error
{
return FALSE;
}
}
return TRUE;
}


And the memory address location it says it can't access, I believe, are the locations of my "a", "b", and "c" variables (will post my code below).


So without further adieu, here is my code (sorry there are no comments):



#include "cuda_runtime.h"
#include "device_launch_parameters.h"

#include <stdio.h>
#include <stdlib.h>

#define N 10

__global__ void kernel() {
}

__global__ void add(int *a, int *b, int *c) {
c[blockIdx.x] = a[blockIdx.x] + b[blockIdx.x];
}

void random_ints(int* a,int num) {
for (int i = 0; i<num; i++)
a[i] = rand();
}

int main () {
int *a,*b,*c;
int *dev_a, *dev_b, *dev_c;
int size = N*sizeof(int);

cudaMalloc((void**)&dev_a,size);
cudaMalloc((void**)&dev_b,size);
cudaMalloc((void**)&dev_c,size);

a = (int*)malloc(size);
b = (int*)malloc(size);
c = (int*)malloc(size);

random_ints(a,N);
random_ints(b,N);



cudaMemcpy(dev_a,&a,size,cudaMemcpyHostToDevice);
cudaMemcpy(dev_b,&b,size,cudaMemcpyHostToDevice);
add<<<N,1>>>(dev_a,dev_b,dev_c);

cudaMemcpy(&c,dev_c,size,cudaMemcpyDeviceToHost);

for (int i = 0; i<N; i++)
printf("%d + %d = %d\n",a[i],b[i],c[i]);

free(a); free(b); free(c);
cudaFree(dev_a);
cudaFree(dev_b);
cudaFree(dev_c);
return 0;
}


If you need any clarifications just ask.


Thanks!


Aucun commentaire:

Enregistrer un commentaire