mesos支持gpu代码分析以及capos支持gpu实现

这篇文章涉及mesos如何在原生的mesoscontainerizer和docker containerizer上支持gpu的,以及如果自己实现一个mesos之上的framework capos支持gpu调度的实现原理,(capos是hulu内部的资源调度平台 refer to https://www.cnblogs.com/yanghuahui/p/9304302.html)。

mesos slave在启动的时候需要初始化containerizer的resource,包含cpu/mem/gpu等,这对于mesos containerizer和docker containerizer都是通用的

void Slave::initialize() {
   ...
   Try<Resources> resources = Containerizer::resources(flags);
   ...
}

然后到了src/slave/containerizer/containerizer.cpp 代码块中, 根据mesos-slave/agent的启动参数flags,调用allocator逻辑

Try<Resources> Containerizer::resources(const Flags& flags)
{
  ...
  // GPU resource.
  Try<Resources> gpus = NvidiaGpuAllocator::resources(flags);
  if (gpus.isError()) {
    return Error("Failed to obtain GPU resources: " + gpus.error());
  }

  // When adding in the GPU resources, make sure that we filter out
  // the existing GPU resources (if any) so that we do not double
  // allocate GPUs.
  resources = gpus.get() + resources.filter(
      [](const Resource& resource) {
        return resource.name() != "gpus";
      });
  ...
}

src/slave/containerizer/mesos/isolators/gpu/allocator.cpp 会用nvidia的管理gpu的命令nvml以及根据启动参数,返回这台机器上gpu的资源,供之后的调度使用。

// To determine the proper number of GPU resources to return, we
// need to check both --resources and --nvidia_gpu_devices.
// There are two cases to consider:
//
//   (1) --resources includes "gpus" and --nvidia_gpu_devices is set.
//       The number of GPUs in --resources must equal the number of
//       GPUs within --nvidia_gpu_resources.
//
//   (2) --resources does not include "gpus" and --nvidia_gpu_devices
//       is not specified. Here we auto-discover GPUs using the
//       NVIDIA management Library (NVML). We special case specifying
//       `gpus:0` explicitly to not perform auto-discovery.
//
static Try<Resources> enumerateGpuResources(const Flags& flags)
{
 ...
}

因为gpu资源是需要绑定gpu卡number的,gpu资源在调度的数据结构中,是一个set<Gpu>, allocator.go提供allocate和deallocate接口的实现

  Future<Nothing> allocate(const set<Gpu>& gpus)
  {
    set<Gpu> allocation = available & gpus;

    if (allocation.size() < gpus.size()) {
      return Failure(stringify(gpus - allocation) + " are not available");
    }

    available = available - allocation;
    allocated = allocated | allocation;

    return Nothing();
  }

  Future<Nothing> deallocate(const set<Gpu>& gpus)
  {
    set<Gpu> deallocation = allocated & gpus;

    if (deallocation.size() < gpus.size()) {
      return Failure(stringify(gpus - deallocation) + " are not allocated");
    }

    allocated = allocated - deallocation;
    available = available | deallocation;

    return Nothing();
  }

但是封装到上层,供containerizer调用的时候,指定需要allocate的gpu number就可以

Future<set<Gpu>> NvidiaGpuAllocator::allocate(size_t count)
{
  // Need to disambiguate for the compiler.
  Future<set<Gpu>> (NvidiaGpuAllocatorProcess::*allocate)(size_t) =
    &NvidiaGpuAllocatorProcess::allocate;

  return process::dispatch(data->process, allocate, count);
}

但是deallocate仍然需要显示指定需要释放哪些gpu

Future<Nothing> NvidiaGpuAllocator::deallocate(const set<Gpu>& gpus)
{
  return process::dispatch(
      data->process,
      &NvidiaGpuAllocatorProcess::deallocate,
      gpus);
}

然后如果作业是用docker containerizer,可以看到src/slave/containerizer/docker.cpp中调用gpu的逻辑

Future<Nothing> DockerContainerizerProcess::allocateNvidiaGpus(
    const ContainerID& containerId,
    const size_t count)
{
  if (!nvidia.isSome()) {
    return Failure("Attempted to allocate GPUs"
                   " without Nvidia libraries available");
  }

  if (!containers_.contains(containerId)) {
    return Failure("Container is already destroyed");
  }

  return nvidia->allocator.allocate(count)
    .then(defer(
        self(),
        &Self::_allocateNvidiaGpus,
        containerId,
        lambda::_1));
}

所以之上,就是在slave中启动的时候加载确认gpu资源,然后在启动containerizer的时候,可以利用slave中维护的gpu set资源池,去拿到资源,之后启动作业。

那capos是如何实现的呢,capos是hulu内部的资源调度平台(refer to https://www.cnblogs.com/yanghuahui/p/9304302.html),因为自己实现了mesos的capos containerizer,我们的做法是,在mesos slave注册的时候显示的通过参数或者自动探测的机制,发现gpu资源,然后用--resources=gpu range的形式启动mesos agent,这样offer资源的gpu在capos看来就是一个range,可以类似使用port资源的方式,来调度gpu,在capos containerizer中,根据调度器指定的gpu range,去绑定一个或者多个gpu资源到docker nvidia runtime中。完成gpu调度功能。

原文地址:https://www.cnblogs.com/yanghuahui/p/9381857.html

时间: 2024-08-28 10:31:35

mesos支持gpu代码分析以及capos支持gpu实现的相关文章

Mesos原理与代码分析(4) Mesos Master的启动之三

3. ModuleManager::load(flags.modules.get())如果有参数--modules或者--modules_dir=dirpath,则会将路径中的so文件load进来 ? 代码中加载模块的代码如下 ? 对应的命令行参数如下: ? ? 都可以写什么Module呢? ? 首先是Allocator ? 默认是内置的Hierarchical Dominant Resource Fairness allocator ? 要写一个自己的Allocator: 通过--module

Mesos原理与代码分析(5): Mesos Master的启动之四

? 5. Create an instance of allocator. ? 代码如下 ? Mesos源码中默认的Allocator,即HierarchicalDRFAllocator的位置在$MESOS_HOME/src/master/allocator/mesos/hierarchical.hpp,而DRF中对每个Framework排序的Sorter位于$MESOS_HOME/src/master/allocator/sorter/drf/sorter.cpp,可以查看其源码了解它的工作原

Mesos原理与代码分析(3): Mesos Master的启动之二

2. process::firewall::install(move(rules));如果有参数--firewall_rules则会添加规则 ? 对应的代码如下: // Initialize firewall rules. if (flags.firewall_rules.isSome()) { vector<Owned<FirewallRule>> rules; ? const Firewall firewall = flags.firewall_rules.get(); ? i

Mesos原理与代码分析(2): Mesos Master的启动之一

Mesos Master的启动参数如下: /usr/sbin/mesos-master --zk=zk://127.0.0.1:2181/mesos --port=5050 --log_dir=/var/log/mesos --hostname=192.168.56.101 --hostname_lookup=false --ip=192.168.56.101 --quorum=1 --registry=replicated_log --work_dir=/var/lib/mesos/maste

JQuery html API支持解析执行Javascript脚本功能实现-代码分析

JQuery html用法(功能类似innerHTML) 开发中需要使用Ajax技术来更新页面局部区域, 使用的方法是ajax获取html代码段(字符串),然后将这个html代码段作为参数,传入目标DOM(JQuery对象)的JQuery html接口,此语句执行后, 会将html代码段解释执行, 显示出html代码段描述的页面控件. 例如: <html> <head> <script type="text/javascript" src="./

【云分析】《“支持和运维服务”仍然困扰着企业云的应用》

2015-09-06 张晓东 东方云洞察 点击上面的链接文字,可以快速关注"东方云洞察"公众号 云服务目前很火,那么让我们畅想一下:做为企业IT部门负责人,你采购并在企业内部部署了一个云存储服务,某一天存储服务突然出现了故障.那么你服务的用户应该打电话向谁求助?内部IT支持人员?云存储服务供应商?外包的服务团队?还是他/她玩技术的表兄弟? 这些都是采用公有云或者混合云的企业IT部门目前面临的问题.对最终用户来说,IT部门实际上承担了云服务代理角色,并通过转嫁IT服务成本到最终用户的部门

在PHPStorm中支持ThinkPHP代码提示

在phpstorm中开发ThinkPHP应用程序时,没有代码提示,困扰了很长时间,后来参考网上一些关于在phpstorm中支持YII框架代码提示的相关帖子,尝试在ThinkPHP中进行测试,目前测试成功,现留帖记录过程. 第1步:在phpstorm的settings选项菜单中,设置当前应用程序的包含目录(要包含ThinkPHP框架包). 第2步:在ThinkPHP框架包中,取消无关文件的代码提示影响. 到此,代码提示成功. 在PHPStorm中支持ThinkPHP代码提示

phpStorm支持CodeIgniter代码提示/自动完成

下载这个文件phpstorm-ci-ac 或者去github下载解压里面的三个文件到ci根目录下然后找到这三个文件 system\core\Controller.phpsystem\core\Model.phpsystem\database\DB_active_rec.php 点击右键, 选择Make as Plain Text. 如图: 至此phpstorm已经可以支持ci代码提示了 修改my_models.php文件可以使你自定义的类实现自动完成按照如下方法操作: Models to Loa

如何为javascript代码编写注释以支持智能感知

在使用Visual Studio做开发的时候,智能感知是非常方便的.从VS2008开始,提供了对javascript的智能感知支持.例如 上述代码中,我们先用document对象的getElementById的方式取得了文档中的一个Id为form1的元素,实际上就是默认的那个窗体元素.然后,我们将其赋给一个名为f的变量. 然后,我们在使用f 这个变量的时候,就能自动地列出该form元素所应该有的一些成员,例如action等等. 这些属于是默认的元素和方法的智能感知,假设我们自己有一些自定义js代