forked from daphne-eu/daphne
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[DAPHNE-191] Refactor CUDA buffer mgmt (aka introducing Object Meta D…
…ata) This change introduces a major change how external storage buffers (CUDA memory specifically) are handled. In that regard, the following noteworthy changes are implemented: * Factor out CUDA allocations from DenseMatrix (one of the initial motivations of issue daphne-eu#191) * Introduce a mechanism to handle several storage backends and track ranges of a Structure's data. * To make use of the mechanism, an AllocationDescriptor is passed to create() and getValues() (at the moment only DenseMatrix is supported). * Allocation descriptors need to implement the IAllocationDescriptor interface. This decouples backend specific dependencies. * AllocationDescriptorHost and AllocationDescriptorCUDA are implemented atm. The former is more or less a no-op for now. * The CUDA memory allocation and data movement is moved to the CUDAContext class. It keeps track of its allocations per device. For now this does nothing but can be used to reuse allocations in the future. Closes daphne-eu#191, Closes daphne-eu#334
- Loading branch information
1 parent
40ca604
commit f02fa9c
Showing
45 changed files
with
657 additions
and
441 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,6 @@ | ||
#ifndef DAPHNE_PROTOTYPE_ICONTEXT_H | ||
#define DAPHNE_PROTOTYPE_ICONTEXT_H | ||
|
||
#pragma once | ||
|
||
class IContext { | ||
public: | ||
virtual void destroy() = 0; | ||
}; | ||
|
||
#endif //DAPHNE_PROTOTYPE_ICONTEXT_H |
69 changes: 69 additions & 0 deletions
69
src/runtime/local/datastructures/AllocationDescriptorCUDA.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* | ||
* Copyright 2022 The DAPHNE Consortium | ||
* | ||
* 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 <cstdint> | ||
#include "ObjectMetaData.h" | ||
#include "runtime/local/context/CUDAContext.h" | ||
|
||
class AllocationDescriptorCUDA : public IAllocationDescriptor { | ||
ALLOCATION_TYPE type = ALLOCATION_TYPE::GPU_CUDA; | ||
uint32_t device_id{}; | ||
DaphneContext* dctx{}; | ||
std::shared_ptr<std::byte> data{}; | ||
size_t alloc_id{}; | ||
|
||
public: | ||
AllocationDescriptorCUDA() = delete; | ||
|
||
AllocationDescriptorCUDA(DaphneContext* ctx, uint32_t device_id) : device_id(device_id), dctx(ctx) { } | ||
|
||
~AllocationDescriptorCUDA() override { | ||
// ToDo: for now we free if this is the last context-external ref to the buffer | ||
if(data.use_count() == 2) { | ||
CUDAContext::get(dctx, device_id)->free(alloc_id); | ||
} | ||
} | ||
|
||
[[nodiscard]] ALLOCATION_TYPE getType() const override { return type; } | ||
|
||
[[nodiscard]] uint32_t getLocation() const { return device_id; } | ||
|
||
void createAllocation(size_t size, bool zero) override { | ||
auto ctx = CUDAContext::get(dctx, device_id); | ||
data = ctx->malloc(size, zero, alloc_id); | ||
} | ||
|
||
std::shared_ptr<std::byte> getData() override { return data; } | ||
|
||
[[nodiscard]] std::unique_ptr<IAllocationDescriptor> clone() const override { | ||
return std::make_unique<AllocationDescriptorCUDA>(*this); | ||
} | ||
|
||
void transferTo(std::byte* src, size_t size) override { | ||
CHECK_CUDART(cudaMemcpy(data.get(), src, size, cudaMemcpyHostToDevice)); | ||
} | ||
void transferFrom(std::byte* dst, size_t size) override { | ||
CHECK_CUDART(cudaMemcpy(dst, data.get(), size, cudaMemcpyDeviceToHost)); | ||
}; | ||
|
||
bool operator==(const IAllocationDescriptor* other) const override { | ||
if(getType() == other->getType()) | ||
return(getLocation() == dynamic_cast<const AllocationDescriptorCUDA *>(other)->getLocation()); | ||
return false; | ||
} | ||
}; |
34 changes: 34 additions & 0 deletions
34
src/runtime/local/datastructures/AllocationDescriptorHost.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* | ||
* Copyright 2022 The DAPHNE Consortium | ||
* | ||
* 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 "ObjectMetaData.h" | ||
#include <cstdint> | ||
|
||
class AllocationDescriptorHost : public IAllocationDescriptor { | ||
public: | ||
~AllocationDescriptorHost() override = default; | ||
[[nodiscard]] ALLOCATION_TYPE getType() const override { return ALLOCATION_TYPE::HOST; } | ||
void createAllocation(size_t size, bool zero) override { } | ||
std::shared_ptr<std::byte> getData() override { return nullptr; } | ||
void transferTo(std::byte* src, size_t size) override { } | ||
void transferFrom(std::byte* dst, size_t size) override {} | ||
[[nodiscard]] std::unique_ptr<IAllocationDescriptor> clone() const override { | ||
return std::make_unique<AllocationDescriptorHost>(*this); | ||
} | ||
bool operator==(const IAllocationDescriptor* other) const override { return (getType() == other->getType()); } | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.