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

Add alias and assign for Ctxt memory allocations #12

Merged
merged 1 commit into from
Feb 9, 2019
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
Add alias and assign for Ctxt memory allocations
  • Loading branch information
Keith Colbert committed Feb 8, 2019
commit c1374409285c6b17df0daf37b427792e25b1cd06
3 changes: 3 additions & 0 deletions cufhe/include/cufhe.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@ struct PubKey {
struct Ctxt {
Ctxt(bool is_alias = false);
~Ctxt();
Ctxt(const Ctxt& that) = delete;
Ctxt& operator=(const Ctxt& that) = delete;
void assign(void* host_ptr, void* device_ptr);
LWESample* lwe_sample_;
MemoryDeleter lwe_sample_deleter_;
LWESample* lwe_sample_device_;
Expand Down
36 changes: 28 additions & 8 deletions cufhe/lib/cufhe.cc
Original file line number Diff line number Diff line change
Expand Up @@ -279,25 +279,45 @@ PubKey::~PubKey() {
}

Ctxt::~Ctxt() {
if(lwe_sample_ != nullptr)
{
lwe_sample_deleter_(lwe_sample_->data());
lwe_sample_deleter_ = nullptr;
if(lwe_sample_ != nullptr) {
if(lwe_sample_deleter_ != nullptr) {
lwe_sample_deleter_(lwe_sample_->data());
lwe_sample_deleter_ = nullptr;
}

lwe_sample_->set_data(nullptr);
delete lwe_sample_;
lwe_sample_ = nullptr;
}

if(lwe_sample_device_ != nullptr)
{
lwe_sample_device_deleter_(lwe_sample_device_->data());
lwe_sample_device_deleter_ = nullptr;
if(lwe_sample_device_ != nullptr) {
if(lwe_sample_device_deleter_ != nullptr) {
lwe_sample_device_deleter_(lwe_sample_device_->data());
lwe_sample_device_deleter_ = nullptr;
}

lwe_sample_device_->set_data(nullptr);
delete lwe_sample_device_;
lwe_sample_device_ = nullptr;
}
}

void Ctxt::assign(void* host_ptr, void* device_ptr) {
if(lwe_sample_deleter_ != nullptr) {
lwe_sample_deleter_(lwe_sample_->data());
lwe_sample_deleter_ = nullptr;
}

lwe_sample_->set_data((LWESample::PointerType)host_ptr);

if(lwe_sample_device_deleter_ != nullptr) {
lwe_sample_device_deleter_(lwe_sample_device_->data());
lwe_sample_device_deleter_ = nullptr;
}

lwe_sample_device_->set_data((LWESample::PointerType)device_ptr);
}

void SetSeed(uint32_t seed) {
srand(seed);
RandomGeneratorSetSeed(&seed, 1);
Expand Down
24 changes: 16 additions & 8 deletions cufhe/lib/cufhe_gpu.cu
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,23 @@ Ctxt::Ctxt(bool is_alias) {
Param* param = GetDefaultParam();

lwe_sample_ = new LWESample(param->lwe_n_);
//pair = AllocatorBoth::New(lwe_sample_->SizeMalloc());
pair = AllocatorCPU::New(lwe_sample_->SizeMalloc());
lwe_sample_->set_data((LWESample::PointerType)pair.first);
lwe_sample_deleter_ = pair.second;

lwe_sample_device_ = new LWESample(param->lwe_n_);
pair = AllocatorGPU::New(lwe_sample_device_->SizeMalloc());
lwe_sample_device_->set_data((LWESample::PointerType)pair.first);
lwe_sample_device_deleter_ = pair.second;

if (is_alias) {
lwe_sample_->set_data(nullptr);
lwe_sample_deleter_ = nullptr;
lwe_sample_device_->set_data(nullptr);
lwe_sample_device_deleter_ = nullptr;
} else {
//pair = AllocatorBoth::New(lwe_sample_->SizeMalloc());
pair = AllocatorCPU::New(lwe_sample_->SizeMalloc());
lwe_sample_->set_data((LWESample::PointerType)pair.first);
lwe_sample_deleter_ = pair.second;

pair = AllocatorGPU::New(lwe_sample_device_->SizeMalloc());
lwe_sample_device_->set_data((LWESample::PointerType)pair.first);
lwe_sample_device_deleter_ = pair.second;
}
}

} // namespace cufhe