Heap Memory Size for Cuda

#include <iostream>
using namespace std;

#include <cuda.h>
#include <cuda_runtime.h>

int main( void )
{
    cudaError_t stat = cudaSuccess;

    int devCount = 0;
    cudaGetDeviceCount( &devCount );

    if( !devCount )
    {
        cout << "No Cuda Device." << endl;
        return 0;
    }

    cudaDeviceProp devProp;
    cudaGetDeviceProperties( &devProp, 0 );
    cout << "Global Memory size: " << devProp.totalGlobalMem << " bytes" << endl;

    size_t free_bytes=0, total_bytes=0;
    cudaMemGetInfo( &free_bytes, &total_bytes );
    cout << "Free : " << free_bytes << " bytes" << endl;
    cout << "Total: " << total_bytes << " bytes" << endl;

    // cudaDeviceSetLimit has no effects!
    {
        size_t sizeLimit = 0;
        cudaDeviceGetLimit( &sizeLimit, cudaLimitMallocHeapSize );
        cout << "sizeLimit: " << sizeLimit << endl;

        cudaDeviceSetLimit( cudaLimitMallocHeapSize, free_bytes*1024 );

        cudaDeviceGetLimit( &sizeLimit, cudaLimitMallocHeapSize );
        cout << "sizeLimit: " << sizeLimit << endl;
    }

    size_t* reservedMemory = (size_t*)NULL;
    stat = cudaMalloc( &reservedMemory, free_bytes+1 ); // one more bytes
    if( stat != cudaSuccess )
    {
        cout << "Failed to allocate memory." << endl;
    }
    if( reservedMemory ) { cudaFree( reservedMemory ); }

    cudaDeviceReset();
}

답글 남기기