Make the resource manager independent, new base class DeviceResource and new derived class AscendResource
parent
3d2195bea3
commit
f2dce335f2
@ -0,0 +1,98 @@
|
|||||||
|
/**
|
||||||
|
* Copyright 2021 Huawei Technologies Co., Ltd
|
||||||
|
*
|
||||||
|
* 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 "include/api/context.h"
|
||||||
|
#include "include/api/types.h"
|
||||||
|
#include "minddata/dataset/include/type_id.h"
|
||||||
|
#include "minddata/dataset/core/ascend_resource.h"
|
||||||
|
|
||||||
|
namespace mindspore {
|
||||||
|
namespace dataset {
|
||||||
|
|
||||||
|
Status AscendResource::InitResource() {
|
||||||
|
ResourceInfo resource;
|
||||||
|
resource.aclConfigPath = "";
|
||||||
|
resource.deviceIds.insert(mindspore::GlobalContext::GetGlobalDeviceID());
|
||||||
|
ascend_resource_ = ResourceManager::GetInstance();
|
||||||
|
APP_ERROR ret = ascend_resource_->InitResource(resource);
|
||||||
|
if (ret != APP_ERR_OK) {
|
||||||
|
ascend_resource_->Release();
|
||||||
|
std::string err_msg = "Error in Init D-chip:" + std::to_string(ret);
|
||||||
|
MS_LOG(ERROR) << err_msg;
|
||||||
|
RETURN_STATUS_UNEXPECTED(err_msg);
|
||||||
|
}
|
||||||
|
int device_id = *(resource.deviceIds.begin());
|
||||||
|
aclrtContext context = ascend_resource_->GetContext(device_id);
|
||||||
|
processor_ = std::make_shared<MDAclProcess>(context, false);
|
||||||
|
ret = processor_->InitResource();
|
||||||
|
if (ret != APP_ERR_OK) {
|
||||||
|
ascend_resource_->Release();
|
||||||
|
std::string err_msg = "Error in Init resource:" + std::to_string(ret);
|
||||||
|
MS_LOG(ERROR) << err_msg;
|
||||||
|
RETURN_STATUS_UNEXPECTED(err_msg);
|
||||||
|
}
|
||||||
|
MS_LOG(INFO) << "Ascend resource all initialized!";
|
||||||
|
return Status::OK();
|
||||||
|
}
|
||||||
|
|
||||||
|
Status AscendResource::FinalizeResource() {
|
||||||
|
processor_->Release();
|
||||||
|
return Status::OK();
|
||||||
|
}
|
||||||
|
|
||||||
|
Status AscendResource::Sink(const mindspore::MSTensor &host_input, std::shared_ptr<DeviceTensor> *device_input) {
|
||||||
|
std::shared_ptr<mindspore::dataset::Tensor> de_input;
|
||||||
|
Status rc = dataset::Tensor::CreateFromMemory(dataset::TensorShape(host_input.Shape()),
|
||||||
|
MSTypeToDEType(static_cast<TypeId>(host_input.DataType())),
|
||||||
|
(const uchar *)(host_input.Data().get()), &de_input);
|
||||||
|
RETURN_IF_NOT_OK(rc);
|
||||||
|
APP_ERROR ret = processor_->H2D_Sink(de_input, *device_input);
|
||||||
|
if (ret != APP_ERR_OK) {
|
||||||
|
ascend_resource_->Release();
|
||||||
|
std::string err_msg = "Error in data sink process:" + std::to_string(ret);
|
||||||
|
MS_LOG(ERROR) << err_msg;
|
||||||
|
RETURN_STATUS_UNEXPECTED(err_msg);
|
||||||
|
}
|
||||||
|
MS_LOG(INFO) << "Process data sink successfully";
|
||||||
|
return Status::OK();
|
||||||
|
}
|
||||||
|
|
||||||
|
Status AscendResource::Pop(const std::shared_ptr<DeviceTensor> &device_output, std::shared_ptr<Tensor> *host_output) {
|
||||||
|
APP_ERROR ret = processor_->D2H_Pop(device_output, *host_output);
|
||||||
|
if (ret != APP_ERR_OK) {
|
||||||
|
ascend_resource_->Release();
|
||||||
|
std::string err_msg = "Error in data pop processing:" + std::to_string(ret);
|
||||||
|
MS_LOG(ERROR) << err_msg;
|
||||||
|
RETURN_STATUS_UNEXPECTED(err_msg);
|
||||||
|
}
|
||||||
|
return Status::OK();
|
||||||
|
}
|
||||||
|
|
||||||
|
Status AscendResource::DeviceDataRelease() {
|
||||||
|
APP_ERROR ret = processor_->device_memory_release();
|
||||||
|
if (ret != APP_ERR_OK) {
|
||||||
|
ascend_resource_->Release();
|
||||||
|
std::string err_msg = "Error in device data release:" + std::to_string(ret);
|
||||||
|
MS_LOG(ERROR) << err_msg;
|
||||||
|
RETURN_STATUS_UNEXPECTED(err_msg);
|
||||||
|
}
|
||||||
|
return Status::OK();
|
||||||
|
}
|
||||||
|
|
||||||
|
std::shared_ptr<void> AscendResource::GetInstance() { return processor_; }
|
||||||
|
|
||||||
|
} // namespace dataset
|
||||||
|
} // namespace mindspore
|
@ -0,0 +1,59 @@
|
|||||||
|
/**
|
||||||
|
* Copyright 2021 Huawei Technologies Co., Ltd
|
||||||
|
*
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef MINDSPORE_CCSRC_MINDDATA_DATASET_CORE_ASCEND_RESOURCE_H_
|
||||||
|
#define MINDSPORE_CCSRC_MINDDATA_DATASET_CORE_ASCEND_RESOURCE_H_
|
||||||
|
|
||||||
|
#include <memory>
|
||||||
|
#include <string>
|
||||||
|
#include "acl/acl.h"
|
||||||
|
#include "minddata/dataset/core/device_resource.h"
|
||||||
|
#include "minddata/dataset/core/device_tensor.h"
|
||||||
|
#include "minddata/dataset/core/tensor.h"
|
||||||
|
#include "minddata/dataset/kernels/image/dvpp/utils/CommonDataType.h"
|
||||||
|
#include "minddata/dataset/kernels/image/dvpp/utils/ErrorCode.h"
|
||||||
|
#include "minddata/dataset/kernels/image/dvpp/utils/MDAclProcess.h"
|
||||||
|
#include "minddata/dataset/kernels/image/dvpp/utils/ResourceManager.h"
|
||||||
|
|
||||||
|
namespace mindspore {
|
||||||
|
namespace dataset {
|
||||||
|
|
||||||
|
class AscendResource : public DeviceResource {
|
||||||
|
public:
|
||||||
|
AscendResource() = default;
|
||||||
|
~AscendResource() = default;
|
||||||
|
|
||||||
|
Status InitResource() override;
|
||||||
|
|
||||||
|
Status FinalizeResource() override;
|
||||||
|
|
||||||
|
Status Sink(const mindspore::MSTensor &host_input, std::shared_ptr<DeviceTensor> *device_input) override;
|
||||||
|
|
||||||
|
Status Pop(const std::shared_ptr<DeviceTensor> &device_output, std::shared_ptr<Tensor> *host_output) override;
|
||||||
|
|
||||||
|
std::shared_ptr<void> GetInstance() override;
|
||||||
|
|
||||||
|
Status DeviceDataRelease() override;
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::shared_ptr<MDAclProcess> processor_;
|
||||||
|
std::shared_ptr<ResourceManager> ascend_resource_;
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace dataset
|
||||||
|
} // namespace mindspore
|
||||||
|
|
||||||
|
#endif // MINDSPORE_CCSRC_MINDDATA_DATASET_CORE_ASCEND_RESOURCE_H_
|
@ -0,0 +1,58 @@
|
|||||||
|
/**
|
||||||
|
* Copyright 2021 Huawei Technologies Co., Ltd
|
||||||
|
*
|
||||||
|
* 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 "minddata/dataset/core/device_resource.h"
|
||||||
|
|
||||||
|
namespace mindspore {
|
||||||
|
namespace dataset {
|
||||||
|
|
||||||
|
Status DeviceResource::InitResource() {
|
||||||
|
return Status(StatusCode::kMDUnexpectedError,
|
||||||
|
"Is this a valid device? If yes, please implement this InitResource() in the derived class.");
|
||||||
|
}
|
||||||
|
|
||||||
|
Status DeviceResource::FinalizeResource() {
|
||||||
|
return Status(StatusCode::kMDUnexpectedError,
|
||||||
|
"Is this a valid device? If yes, please implement this FinalizeResource() in the derived class.");
|
||||||
|
}
|
||||||
|
|
||||||
|
Status DeviceResource::Sink(const mindspore::MSTensor &host_input, std::shared_ptr<DeviceTensor> *device_input) {
|
||||||
|
return Status(StatusCode::kMDUnexpectedError,
|
||||||
|
"Is this a valid device whose device memory is available? If yes, please implement this Sink() in the "
|
||||||
|
"derived class.");
|
||||||
|
}
|
||||||
|
|
||||||
|
Status DeviceResource::Pop(const std::shared_ptr<DeviceTensor> &device_output, std::shared_ptr<Tensor> *host_output) {
|
||||||
|
return Status(StatusCode::kMDUnexpectedError,
|
||||||
|
"Is this a valid device whose device memory is available? If yes, please implement this Pop() in the "
|
||||||
|
"derived class.");
|
||||||
|
}
|
||||||
|
|
||||||
|
Status DeviceResource::DeviceDataRelease() {
|
||||||
|
return Status(
|
||||||
|
StatusCode::kMDUnexpectedError,
|
||||||
|
"Is this a valid device whose device memory is available? If yes, please implement this DeviceDataRelease() in the "
|
||||||
|
"derived class.");
|
||||||
|
}
|
||||||
|
|
||||||
|
std::shared_ptr<void> DeviceResource::GetInstance() {
|
||||||
|
MS_LOG(ERROR) << "Is this a device which contains a processor object? If yes, please implement this GetInstance() in "
|
||||||
|
"the derived class";
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace dataset
|
||||||
|
} // namespace mindspore
|
@ -0,0 +1,51 @@
|
|||||||
|
/**
|
||||||
|
* Copyright 2021 Huawei Technologies Co., Ltd
|
||||||
|
*
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef MINDSPORE_CCSRC_MINDDATA_DATASET_CORE_DEVICE_RESOURCE_H_
|
||||||
|
#define MINDSPORE_CCSRC_MINDDATA_DATASET_CORE_DEVICE_RESOURCE_H_
|
||||||
|
|
||||||
|
#include <memory>
|
||||||
|
#include "include/api/context.h"
|
||||||
|
#include "include/api/status.h"
|
||||||
|
#include "include/api/types.h"
|
||||||
|
#include "minddata/dataset/core/device_tensor.h"
|
||||||
|
#include "minddata/dataset/core/tensor.h"
|
||||||
|
|
||||||
|
namespace mindspore {
|
||||||
|
namespace dataset {
|
||||||
|
|
||||||
|
class DeviceResource {
|
||||||
|
public:
|
||||||
|
DeviceResource() = default;
|
||||||
|
|
||||||
|
virtual ~DeviceResource() = default;
|
||||||
|
|
||||||
|
virtual Status InitResource();
|
||||||
|
|
||||||
|
virtual Status FinalizeResource();
|
||||||
|
|
||||||
|
virtual Status Sink(const mindspore::MSTensor &host_input, std::shared_ptr<DeviceTensor> *device_input);
|
||||||
|
|
||||||
|
virtual Status Pop(const std::shared_ptr<DeviceTensor> &device_output, std::shared_ptr<Tensor> *host_output);
|
||||||
|
|
||||||
|
virtual std::shared_ptr<void> GetInstance();
|
||||||
|
|
||||||
|
virtual Status DeviceDataRelease();
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace dataset
|
||||||
|
} // namespace mindspore
|
||||||
|
#endif // MINDSPORE_DEVICE_RESOURCE_H
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in new issue