From 52a2c7c38bbdddeab3e1f4081688393f05fb1f4a Mon Sep 17 00:00:00 2001 From: Matt Borland Date: Fri, 18 Oct 2024 16:37:10 -0400 Subject: [PATCH] Replace new with cudaMalloc and cudaFree --- test/test_md5_nvcc.cu | 9 +++++---- test/test_sha1_nvcc.cu | 9 +++++---- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/test/test_md5_nvcc.cu b/test/test_md5_nvcc.cu index 32f070cd..3778d76e 100644 --- a/test/test_md5_nvcc.cu +++ b/test/test_md5_nvcc.cu @@ -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 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"); @@ -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) { diff --git a/test/test_sha1_nvcc.cu b/test/test_sha1_nvcc.cu index 8a370a15..bfa5b3bd 100644 --- a/test/test_sha1_nvcc.cu +++ b/test/test_sha1_nvcc.cu @@ -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 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"); @@ -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) {