Search code examples
c++ccudagputhrust

Cuda Thrust memory management


I wrote this code below:

 unsigned int* addrArray = (unsigned int*)malloc(sizeof(unsigned int)*DATASETROWS);
 thrust::exclusive_scan(binaryDataSet,binaryDataSet+(sizeof(unsigned int)*DATASETROWS),addrArray);
 free(addrArray);

binaryDataSet is unsigned int* type and I have no problems with allocating, processing data in this part of memory and freeing it. But addrArray causes problems. aftter running program i get following error:

*** glibc detected *** ./prog: free(): invalid next size (normal): 0x000000000180be10 *** and memory map.

This code wont generate this error when I use cudaFree(addrArray) instead, but later it will cause segmentation fault or glibc malloc memory corruption problem. For example running this:

unsigned int* addrArray = (unsigned int*)malloc(sizeof(unsigned int)*DATASETROWS);
thrust::exclusive_scan(binaryDataSet,binaryDataSet+(sizeof(unsigned int)*DATASETROWS),addrArray);
const unsigned int compactArraySize = addrArray[DATASETROWS-1] - 1;
printf("%u\n",compactArraySize)
float* compactMinorClass = (float*)malloc(sizeof(float)*DATASETCOLS*compactArraySize);

Will generate segmentation fault when trying to malloc memory on compactMinorClass pointer(printf gives correct output, so prefixsum is calculated correctly and output is saved in addrArray as should be). To build this program I set flags for g++ and nvcc as following:

GXXFLAGS:= -O3 -Wall -Wextra -m64 -std=c++0x

NVCCFLAGS:= -Xcompiler -Wall -Xcompiler -Wextra -m64 -arch=sm_11

(I have GeForce310M so I have to set sm_11) So my question is why running this thrust function generates errors, why I can't free memory allocated on host using free() ?


Solution

  • This is not correct:

    thrust::exclusive_scan(binaryDataSet,binaryDataSet+(sizeof(unsigned int)*DATASETROWS),addrArray);
    

    Try this instead:

    thrust::exclusive_scan(binaryDataSet,binaryDataSet+(DATASETROWS),addrArray);
    

    The intention here is to do pointer arithmetic, not byte arithmetic. And likewise in your second code sample.

    I'm also not sure it's a good idea to specify:

    -std=c++0x
    

    But I don't know for sure that it's the source of any of your problems.

    You definitely should not do cudaFree on an ordinary host pointer.