/*
Copyright 2020 The OneFlow Authors. All rights reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include "oneflow/core/job/id_manager.h"
#include "oneflow/core/common/id_util.h"
#include "oneflow/core/graph/id_serialization.h"

namespace oneflow {

int64_t IDMgr::MachineId4ActorId(int64_t actor_id) const {
  // TODO: change this inferface semantics, rank does not indicate machine_id in multi-client
  return DeserializeTaskIdFromInt64(actor_id).stream_id().device_id().rank();
}

int64_t IDMgr::ThrdId4ActorId(int64_t actor_id) const {
  return SerializeStreamIdToInt64(DeserializeTaskIdFromInt64(actor_id).stream_id());
}

int64_t IDMgr::PickCpuThrdIdEvenly(int64_t machine_id) {
  DeviceId device_id{static_cast<DeviceId::rank_t>(machine_id), DeviceType::kCPU,
                     DeviceId::kCPUDeviceIndex};
  auto* stream_index_generator = GetStreamIndexGeneratorManager()->GetGenerator(device_id);
  StreamId stream_id{device_id, stream_index_generator->GenerateComputeStreamIndex()};
  return SerializeStreamIdToInt64(stream_id);
}

IDMgr::IDMgr() {
  CHECK_LT((Global<ResourceDesc, ForSession>::Get()->process_ranks().size()),
           static_cast<int64_t>(1) << machine_id_bit_num_);
  gpu_device_num_ = Global<ResourceDesc, ForSession>::Get()->GpuDeviceNum();
  cpu_device_num_ = Global<ResourceDesc, ForSession>::Get()->CpuDeviceNum();
  CHECK_LT(gpu_device_num_ + cpu_device_num_, (static_cast<int64_t>(1) << thread_id_bit_num_) - 3);
  regst_desc_id_count_ = 0;
  mem_block_id_count_ = 0;
  chunk_id_count_ = 0;
}

}  // namespace oneflow
