Skip to content

Commit

Permalink
Replace new with cudaMalloc and cudaFree
Browse files Browse the repository at this point in the history
  • Loading branch information
mborland committed Oct 18, 2024
1 parent 4d9352e commit 52a2c7c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
9 changes: 5 additions & 4 deletions test/test_md5_nvcc.cu
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,15 @@ int main()
std::cout << "[Vector operation on " << numElements << " elements]" << std::endl;

// Allocate the managed input vector A
char** input_vector1 = new char*[numElements];
char** input_vector1;
cudaMallocManaged(&input_vector1, numElements * sizeof(char*));

// Allocate the managed output vector C
cuda_managed_ptr<digest_type> output_vector(numElements);

for (int i = 0; i < numElements; ++i)
{
input_vector1[i] = new char[elementSize];
cudaMallocManaged(&input_vector1[i], elementSize * sizeof(char));
if (input_vector1[i] == nullptr)
{
throw std::runtime_error("Failed to allocate memory for input_vector1");
Expand Down Expand Up @@ -99,9 +100,9 @@ int main()
// Cleanup all the memory we allocated
for (int i = 0; i < numElements; ++i)
{
delete[] input_vector1[i];
cudaFree(input_vector1[i]);
}
delete[] input_vector1;
cudaFree(input_vector1);
}
catch (const std::exception& e)
{
Expand Down
9 changes: 5 additions & 4 deletions test/test_sha1_nvcc.cu
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,15 @@ int main()
std::cout << "[Vector operation on " << numElements << " elements]" << std::endl;

// Allocate the managed input vector A
char** input_vector1 = new char*[numElements];
char** input_vector1;
cudaMallocManaged(&input_vector1, numElements * sizeof(char*));

// Allocate the managed output vector C
cuda_managed_ptr<digest_type> output_vector(numElements);

for (int i = 0; i < numElements; ++i)
{
input_vector1[i] = new char[elementSize];
cudaMallocManaged(&input_vector1[i], elementSize * sizeof(char));
if (input_vector1[i] == nullptr)
{
throw std::runtime_error("Failed to allocate memory for input_vector1");
Expand Down Expand Up @@ -99,9 +100,9 @@ int main()
// Cleanup all the memory we allocated
for (int i = 0; i < numElements; ++i)
{
delete[] input_vector1[i];
cudaFree(input_vector1[i]);
}
delete[] input_vector1;
cudaFree(input_vector1);
}
catch (const std::exception& e)
{
Expand Down

0 comments on commit 52a2c7c

Please sign in to comment.