Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

【CINN】Add device query components for cinn #58988

Merged
merged 3 commits into from
Nov 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion paddle/cinn/common/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ gather_srcs(
arithmatic.cc
cas.cc
union_find.cc
python_interpreter_guard.cc)
python_interpreter_guard.cc
dev_info_manager.cc
nvgpu_dev_info.cc)

message(STATUS "srcs: ${cinnapi_src}")

Expand Down
26 changes: 26 additions & 0 deletions paddle/cinn/common/dev_info_base.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright (c) 2023 CINN 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.

#pragma once

namespace cinn {
namespace common {

class DevInfoBase {
public:
virtual ~DevInfoBase() = default;
};

} // namespace common
} // namespace cinn
48 changes: 48 additions & 0 deletions paddle/cinn/common/dev_info_manager.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright (c) 2023 CINN 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 <glog/logging.h>

#include "paddle/cinn/common/dev_info_base.h"
#include "paddle/cinn/common/dev_info_manager.h"

namespace cinn {
namespace common {

DevInfoMgr::DevInfoMgr(Target::Arch arch, int device_num)
: arch_(arch), device_num_(device_num) {
switch (arch) {
case Target::Arch::ARM:
case Target::Arch::X86:
case Target::Arch::Unk:
impl_ = std::make_unique<DevInfoBase>();
break;
case Target::Arch::NVGPU:
#ifdef CINN_WITH_CUDA
impl_ = std::make_unique<NVGPUDevInfo>(device_num);
#endif
default:
CHECK(false) << "Current device can't be recognized!\n";
break;
}
}

std::unique_ptr<DevInfoMgr> DevInfoMgr::GetDevInfo(Target::Arch arch,
int device_num) {
std::unique_ptr<DevInfoMgr> ret(new DevInfoMgr(arch, device_num));
return ret;
}

} // namespace common
} // namespace cinn
53 changes: 53 additions & 0 deletions paddle/cinn/common/dev_info_manager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright (c) 2023 CINN 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.

#pragma once

#include <memory>

#include "paddle/cinn/common/macros.h"
#include "paddle/cinn/common/nvgpu_dev_info.h"
#include "paddle/cinn/common/target.h"

namespace cinn {
namespace common {

class DevInfoMgr final {
private:
explicit DevInfoMgr(Target::Arch arch = Target::Arch::Unk,
int device_num = 0);
std::unique_ptr<DevInfoBase> impl_;
Target::Arch arch_;
int device_num_;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

考虑多卡这里是不是应该搞个vector

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

主要混合设备下不同架构的接口不同,如果异构的情况下,几个设备放在一块感觉Get的时候会比较乱,如果是同种设备,get其中一个也就OK了,所以就没搞这事


public:
static std::unique_ptr<DevInfoMgr> GetDevInfo(
Target::Arch arch = Target::Arch::NVGPU, int device_num = 0);

// Extra device should be added here
#ifdef CINN_WITH_CUDA
using RET_TYPE = NVGPUDevInfo;
const RET_TYPE* operator->() const {
CHECK(!std::is_void<RET_TYPE>()) << "Current device can't be recognized!\n";
return dynamic_cast<const RET_TYPE*>(impl_.get());
}
RET_TYPE* operator->() {
CHECK(!std::is_void<RET_TYPE>()) << "Current device can't be recognized!\n";
return dynamic_cast<RET_TYPE*>(impl_.get());
}
#endif
};

} // namespace common
} // namespace cinn
50 changes: 50 additions & 0 deletions paddle/cinn/common/nvgpu_dev_info.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Copyright (c) 2023 CINN 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.
#ifdef CINN_WITH_CUDA
#include "paddle/cinn/common/nvgpu_dev_info.h"

namespace cinn {
namespace common {

std::array<int, 3> NVGPUDevInfo::GetMaxGridDims() const {
std::array<int, 3> ret;
ret[0] = prop_->maxGridSize[0];
ret[1] = prop_->maxGridSize[1];
ret[2] = prop_->maxGridSize[2];
return ret;
}

std::array<int, 3> NVGPUDevInfo::GetMaxBlockDims() const {
std::array<int, 3> ret;
ret[0] = prop_->maxThreadsDim[0];
ret[1] = prop_->maxThreadsDim[1];
ret[2] = prop_->maxThreadsDim[2];
return ret;
}

int NVGPUDevInfo::GetMultiProcessorCount() const {
return prop_->multiProcessorCount;
}

int NVGPUDevInfo::GetMaxThreadsPerMultiProcessor() const {
return prop_->maxThreadsPerMultiProcessor;
}

int NVGPUDevInfo::GetMaxThreadsPerBlock() const {
return prop_->maxThreadsPerBlock;
}

} // namespace common
} // namespace cinn
#endif
49 changes: 49 additions & 0 deletions paddle/cinn/common/nvgpu_dev_info.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright (c) 2023 CINN 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.

#pragma once

#ifdef CINN_WITH_CUDA

#include <ostream>
#include <string>
#include <vector>

#include "paddle/cinn/backends/cuda_util.h"
#include "paddle/cinn/common/dev_info_base.h"
#include "paddle/cinn/common/macros.h"
#include "paddle/cinn/common/target.h"

namespace cinn {
namespace common {

class NVGPUDevInfo : public DevInfoBase {
public:
explicit NVGPUDevInfo(int device) : device_num_(device) {
CUDA_CALL(cudaGetDeviceProperties(prop_, device));
}

std::array<int, 3> GetMaxGridDims() const;
std::array<int, 3> GetMaxBlockDims() const;
int GetMultiProcessorCount() const;
int GetMaxThreadsPerMultiProcessor() const;
int GetMaxThreadsPerBlock() const;

private:
int device_num_;
cudaDeviceProp* prop_;
};
} // namespace common
} // namespace cinn
#endif
7 changes: 7 additions & 0 deletions paddle/cinn/common/target.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@
namespace cinn {
namespace common {

Target::Target(OS o,
Arch a,
Bit b,
const std::vector<Feature> &features,
const std::vector<Lib> &libs)
: os(o), arch(a), bits(b), features(features), libs(libs) {}

bool Target::operator==(const Target &other) const {
return os == other.os && //
arch == other.arch && //
Expand Down
3 changes: 1 addition & 2 deletions paddle/cinn/common/target.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,7 @@ struct Target {
Arch a = Arch::Unk,
Bit b = Bit::Unk,
const std::vector<Feature>& features = {},
const std::vector<Lib>& libs = {})
: os(o), arch(a), bits(b), features(features), libs(libs) {}
const std::vector<Lib>& libs = {});

bool defined() const {
return os != OS::Unk && arch != Arch::Unk && bits != Bit::Unk;
Expand Down