caffe2/core/hip/context_hip.cc
///////////////////////////////////////////////////////////////////////////////
// A wrapper to allow us to lazily initialize all HIP environments that Caffe
// uses. This gets done the first time a caffe2::HIPContext::New() gets called
// which is probably the decisive indication that this caffe2 run is going to
// use GPUs. We avoid HIP initialization with core/init.h functionalities so
// that we have minimal resource impact in case we will need to run multiple
// caffe2 instances on a GPU machine.
///////////////////////////////////////////////////////////////////////////////
A wrapper to allow us to lazily initialize all HIP environments that Caffe
uses. This gets done the first time a caffe2::HIPContext::New() gets called
which is probably the decisive indication that this caffe2 run is going to
use GPUs. We avoid HIP initialization with core/init.h functionalities so
that we have minimal resource impact in case we will need to run multiple
caffe2 instances on a GPU machine.
A wrapper to allow us to lazily initialize all HIP environments that Caffe uses . This gets done the first time a caffe2 :: HIPContext :: New () gets called which is probably the decisive indication that this caffe2 run is going to use GPUs . We avoid HIP initialization with core / init . h functionalities so that we have minimal resource impact in case we will need to run multiple caffe2 instances on a GPU machine .
static void Caffe2InitializeHip()
{
    if(!HasHipGPU())
    {
        VLOG(1) << "No HIP gpu present. Skipping.";
        return;
    }
    CAFFE_ENFORCE_LE(NumHipDevices(),
                     CAFFE2_COMPILE_TIME_MAX_HIP_GPUS,
                     "Number of HIP devices on the machine is larger than the compiled "
                     "max number of gpus expected (",
                     CAFFE2_COMPILE_TIME_MAX_HIP_GPUS,
                     "). Increase that and recompile the caffe binary.");
    for(int i = 0; i < NumHipDevices(); ++i)
    {
        DeviceGuard g(i);
        const int peer_group = i / CAFFE2_HIP_MAX_PEER_SIZE;
        const int peer_start = peer_group * CAFFE2_HIP_MAX_PEER_SIZE;
        const int peer_end = std::min(NumHipDevices(), (peer_group + 1) * CAFFE2_HIP_MAX_PEER_SIZE);
        VLOG(1) << "Enabling peer access within group #" << peer_group << ", from gpuid "
                << peer_start << " to " << peer_end - 1 << ", for gpuid " << i << ".";
        for(int j = peer_start; j < peer_end; ++j)
        {
            if(i == j)
                continue;
            int can_access;
            HIP_ENFORCE(hipDeviceCanAccessPeer(&can_access, i, j));
            if(can_access)
            {
                VLOG(1) << "Enabling peer access from " << i << " to " << j;
                HIP_ENFORCE(hipDeviceEnablePeerAccess(j, 0));
            }
        }
    }
}
static void Caffe2InitializeHip ( ) { if ( ! HasHipGPU ( ) ) { VLOG ( 1 ) << " " ; return ; } CAFFE_ENFORCE_LE ( NumHipDevices ( ) , CAFFE2_COMPILE_TIME_MAX_HIP_GPUS , " " " " , CAFFE2_COMPILE_TIME_MAX_HIP_GPUS , " " ) ; for ( int i = 0 ; i < NumHipDevices ( ) ; ++ i ) { DeviceGuard g ( i ) ; const int peer_group = i / CAFFE2_HIP_MAX_PEER_SIZE ; const int peer_start = peer_group * CAFFE2_HIP_MAX_PEER_SIZE ; const int peer_end = std :: min ( NumHipDevices ( ) , ( peer_group + 1 ) * CAFFE2_HIP_MAX_PEER_SIZE ) ; VLOG ( 1 ) << " " << peer_group << " " << peer_start << " " << peer_end - 1 << " " << i << " " ; for ( int j = peer_start ; j < peer_end ; ++ j ) { if ( i == j ) continue ; int can_access ; HIP_ENFORCE ( hipDeviceCanAccessPeer ( & can_access , i , j ) ) ; if ( can_access ) { VLOG ( 1 ) << " " << i << " " << j ; HIP_ENFORCE ( hipDeviceEnablePeerAccess ( j , 0 ) ) ; } } } }
A wrapper to allow us to lazily initialize all HIP environments that Caffe
uses.
A wrapper to allow us to lazily initialize all HIP environments that Caffe uses .
// If the current run does not have any HIP devices, do nothing. // Check if the number of GPUs matches the expected compile-time max number // of GPUs. // Enable peer access. // Note: just for future reference, the 0 here is not a gpu id, it is // a reserved flag for hipDeviceEnablePeerAccess that should always be // zero currently. // CheckMiOpenVersions();