| 379 | DevicePropRec device_prop_rec[CudaCompNodeImpl::MAX_NR_DEVICE]; |
| 380 | |
| 381 | void CudaCompNodeImpl::init(const Locator& locator, const Locator& locator_logical) { |
| 382 | m_locator = locator; |
| 383 | m_locator_logical = locator_logical; |
| 384 | m_initialized = true; |
| 385 | |
| 386 | #if defined(__linux__) || defined(TARGET_OS_MAC) |
| 387 | FILE* fp; |
| 388 | fp = fopen("/dev/urandom", "r"); |
| 389 | mgb_assert(fread(&m_uid, sizeof(m_uid), 1, fp) == 1); |
| 390 | fclose(fp); |
| 391 | #else |
| 392 | m_uid = std::chrono::duration_cast<std::chrono::nanoseconds>( |
| 393 | std::chrono::system_clock::now().time_since_epoch()) |
| 394 | .count(); |
| 395 | #endif |
| 396 | |
| 397 | auto on_succ = [this](cudaStream_t stream) { |
| 398 | auto locator = m_locator; |
| 399 | log_comp_node_created(locator, m_locator_logical); |
| 400 | |
| 401 | MGB_LOCK_GUARD(sd->mtx); |
| 402 | DeviceInfo* dev_info = nullptr; |
| 403 | for (int i = 0; i < sd->nr_dev_used; ++i) { |
| 404 | if (sd->dev_info[i].dev_num == locator.device) { |
| 405 | dev_info = &sd->dev_info[i]; |
| 406 | break; |
| 407 | } |
| 408 | } |
| 409 | |
| 410 | if (!dev_info) { |
| 411 | dev_info = &sd->dev_info[sd->nr_dev_used]; |
| 412 | dev_info->init(m_env); |
| 413 | // note: add nr_dev_used only after init succeeds |
| 414 | ++sd->nr_dev_used; |
| 415 | } |
| 416 | m_device_info = dev_info; |
| 417 | m_mem_alloc = dev_info->mem_alloc->add_stream(static_cast<void*>(stream)); |
| 418 | }; |
| 419 | |
| 420 | auto on_error = [this](std::exception&) { |
| 421 | MGB_LOCK_GUARD(sd->mtx); |
| 422 | m_initialized = false; |
| 423 | }; |
| 424 | |
| 425 | m_env.init_cuda_async( |
| 426 | locator.device, make_comp_node_from_impl(this), {on_succ, on_error}); |
| 427 | #if MEGDNN_WITH_CUDA |
| 428 | auto cur_prop = CudaCompNode::get_device_prop(locator.device); |
| 429 | auto cur_sm = |
| 430 | std::string("sm_") + std::to_string(cur_prop.major * 10 + cur_prop.minor); |
| 431 | const std::string mge_gen_code = MGE_CUDA_GENCODE; |
| 432 | std::regex re("sm_([0-9]+)"); |
| 433 | std::vector<std::string> build_sm( |
| 434 | std::sregex_token_iterator(mge_gen_code.begin(), mge_gen_code.end(), re), |
| 435 | std::sregex_token_iterator()); |
| 436 | |
| 437 | if (std::find(build_sm.begin(), build_sm.end(), cur_sm) == build_sm.end()) { |
| 438 | std::string build_sm_info = ""; |
no test coverage detected