-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathdistributed.hpp
More file actions
76 lines (67 loc) · 2.47 KB
/
Copy pathdistributed.hpp
File metadata and controls
76 lines (67 loc) · 2.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*
* SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*
* 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.
*/
#pragma once
#include <string>
#include <vector>
#include <memory>
#include <cuda.h>
namespace nve {
/**
* This class abstracts operations in a distributed setting (multi process, multi node, ...).
* Failed ops (barrier, broadcast, all_gather) are expected to throw std::runtime_error
*/
class DistributedEnv {
public:
virtual ~DistributedEnv() = default;
virtual size_t rank() const = 0;
virtual size_t world_size() const = 0;
virtual size_t device_count() const = 0;
virtual int local_device() const = 0;
virtual bool single_host() const = 0;
virtual void barrier() = 0;
virtual void broadcast(uintptr_t buffer, size_t size, int root) = 0;
virtual void all_gather(uintptr_t send_buffer, uintptr_t recv_buffer, size_t size) = 0;
};
enum class BufferLocation {
ALLOCATION_GPU_MEM,
ALLOCATION_SYS_MEM,
};
class CUDADistributedBuffer{
public:
CUDADistributedBuffer(uint64_t size, std::shared_ptr<DistributedEnv> dist_env, BufferLocation location);
~CUDADistributedBuffer();
std::byte* ptr() const { return buffer_; }
uint64_t total_size() const { return total_size_; }
uint64_t shard_size() const { return shard_size_; }
uint64_t num_shards() const { return num_shards_; }
private:
std::shared_ptr<DistributedEnv> env_ = nullptr;
uint64_t total_size_ = 0;
uint64_t shard_size_ = 0;
uint64_t num_shards_ = 0;
std::byte* buffer_ = {};
bool single_host_;
CUmemGenericAllocationHandle alloc_handle_ = {};
std::vector<CUmemGenericAllocationHandle> all_alloc_handles_;
std::vector<int> all_devices_;
size_t get_device_granularity(CUmemAllocationProp prop);
void init_single_host(uint64_t size, BufferLocation location);
void init_multi_host(uint64_t size);
bool check_imex();
uint64_t collect_devices(std::vector<int>& all_devices);
};
} // namespace nve