packages/CudaUtils/cuda_utils/Hardware.cc
//---------------------------------------------------------------------------//
/*!
 * \brief Choose a CUDA device based on some heuristic.
 */
\brief Choose a CUDA device based on some heuristic.
\ brief Choose a CUDA device based on some heuristic .
void Hardware<Device>::acquire(Acquire_Method method)
{
    INSIST(d_device_id == -1, "A device has already been acquired.");
    VALIDATE(method < END_ACQUIRE_METHOD, "Invalid method " << method);
#ifdef USE_CUDA
    cudaDeviceProp prop;
    int num_devices = -1;
    CudaCall(cudaGetDeviceCount(&num_devices));
    int best_value = -1;
    int this_value = -1;
    for (int device = 0; device < num_devices; ++device)
    {
        CudaCall(cudaGetDeviceProperties(&prop, device));
        if (prop.computeMode == cudaComputeModeProhibited)
            continue;
        switch (method)
        {
            case HIGHEST_FLOPS:
                this_value = prop.multiProcessorCount
                             * num_cores_per_mp(prop.major, prop.minor)
                             * prop.clockRate;
                break;
            case HIGHEST_COMPUTE:
                this_value = (prop.major << 4) + prop.minor;
                break;
            default: 
                break;
        }
        if (this_value > best_value)
        {
            best_value  = this_value;
            d_device_id           = device;
            d_name                = prop.name;
            d_multiProcessorCount = prop.multiProcessorCount;
            d_totalConstMem       = prop.totalConstMem;
            d_sharedMemPerBlock   = prop.sharedMemPerBlock;
            d_cores_per_mp        = num_cores_per_mp(prop.major, prop.minor);
            d_maxThreadsPerMultiProcessor = prop.maxThreadsPerMultiProcessor;
        }
    }
    INSIST(d_device_id != 1, "No valid devices were found.");
    CudaCall(cudaSetDevice(d_device_id));
    CudaCall(cudaDeviceReset());
    CudaCall(cudaFree(0));
    if (profugus::node() == 0)
    {
        std::size_t free, total;
        CudaCall(cudaMemGetInfo(&free, &total));
        std::cout << "*** Initialized device '"
            << d_name
            << "' (Device ID " << d_device_id << ")"
            << " which has " << (free >> 20) << " of "
            << (total >> 20) << "MB free."
            << std::endl;
    }
#else 
    INSIST(false, "CUDA is disabled in this build.");
#endif 
}
void Hardware < Device > :: acquire ( Acquire_Method method ) { INSIST ( d_device_id == -1 , " " ) ; VALIDATE ( method < END_ACQUIRE_METHOD , " " << method ) ; #ifdef USE_CUDA cudaDeviceProp prop ; int num_devices = -1 ; CudaCall ( cudaGetDeviceCount ( & num_devices ) ) ; int best_value = -1 ; int this_value = -1 ; for ( int device = 0 ; device < num_devices ; ++ device ) { CudaCall ( cudaGetDeviceProperties ( & prop , device ) ) ; if ( prop . computeMode == cudaComputeModeProhibited ) continue ; switch ( method ) { case HIGHEST_FLOPS : this_value = prop . multiProcessorCount * num_cores_per_mp ( prop . major , prop . minor ) * prop . clockRate ; break ; case HIGHEST_COMPUTE : this_value = ( prop . major << 4 ) + prop . minor ; break ; default : break ; } if ( this_value > best_value ) { best_value = this_value ; d_device_id = device ; d_name = prop . name ; d_multiProcessorCount = prop . multiProcessorCount ; d_totalConstMem = prop . totalConstMem ; d_sharedMemPerBlock = prop . sharedMemPerBlock ; d_cores_per_mp = num_cores_per_mp ( prop . major , prop . minor ) ; d_maxThreadsPerMultiProcessor = prop . maxThreadsPerMultiProcessor ; } } INSIST ( d_device_id != 1 , " " ) ; CudaCall ( cudaSetDevice ( d_device_id ) ) ; CudaCall ( cudaDeviceReset ( ) ) ; CudaCall ( cudaFree ( 0 ) ) ; if ( profugus :: node ( ) == 0 ) { std :: size_t free , total ; CudaCall ( cudaMemGetInfo ( & free , & total ) ) ; std :: cout << " " << d_name << " " << d_device_id << " " << " " << ( free >> 20 ) << " " << ( total >> 20 ) << " " << std :: endl ; } #else INSIST ( false , " " ) ; #endif }
\brief Choose a CUDA device based on some heuristic.
\ brief Choose a CUDA device based on some heuristic .
// Check whether this device is valid // squelch warning // Set device ID and store properties // Reset the device // Initialize device, causing the ~.2 second penalty to appear in this // function rather than in later initialization // USE_CUDA disabled // USE_CUDA