|
| 1 | +// Copyright Matt Borland 2024 |
| 2 | +// Use, modification and distribution are subject to the |
| 3 | +// Boost Software License, Version 1.0. (See accompanying file |
| 4 | +// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
| 5 | + |
| 6 | +#include <cuda_runtime.h> |
| 7 | +#include <boost/crypt2/drbg/sha1_drbg.hpp> |
| 8 | +#include "cuda_managed_ptr.hpp" |
| 9 | +#include "stopwatch.hpp" |
| 10 | +#include "generate_random_strings.hpp" |
| 11 | +#include <iostream> |
| 12 | +#include <iomanip> |
| 13 | +#include <exception> |
| 14 | +#include <memory> |
| 15 | +#include <span> |
| 16 | + |
| 17 | +using digest_type = typename cuda::std::array<cuda::std::byte, 80>; |
| 18 | + |
| 19 | +// The kernel function |
| 20 | +__global__ void cuda_test(char** in, digest_type* out, int numElements) |
| 21 | +{ |
| 22 | + int i = blockIdx.x * blockDim.x + threadIdx.x; |
| 23 | + |
| 24 | + if (i < numElements) |
| 25 | + { |
| 26 | + boost::crypt::sha1_hash_drbg drbg; |
| 27 | + cuda::std::span<char> in_span {in[i], static_cast<cuda::std::size_t>(64)}; |
| 28 | + drbg.init(in_span); |
| 29 | + drbg.generate(out[i], 640U); |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +int main() |
| 34 | +{ |
| 35 | + try |
| 36 | + { |
| 37 | + // Error code to check return values for CUDA calls |
| 38 | + cudaError_t err = cudaSuccess; |
| 39 | + |
| 40 | + // Print the vector length to be used, and compute its size |
| 41 | + constexpr int numElements = 50000; |
| 42 | + constexpr std::size_t elementSize = 64; |
| 43 | + |
| 44 | + std::cout << "[Vector operation on " << numElements << " elements]" << std::endl; |
| 45 | + |
| 46 | + // Allocate the managed input vector A |
| 47 | + char** input_vector1; |
| 48 | + cudaMallocManaged(&input_vector1, numElements * sizeof(char*)); |
| 49 | + |
| 50 | + // Allocate the managed output vector C |
| 51 | + cuda_managed_ptr<digest_type> output_vector(numElements); |
| 52 | + |
| 53 | + for (int i = 0; i < numElements; ++i) |
| 54 | + { |
| 55 | + cudaMallocManaged(&input_vector1[i], elementSize * sizeof(char)); |
| 56 | + if (input_vector1[i] == nullptr) |
| 57 | + { |
| 58 | + throw std::runtime_error("Failed to allocate memory for input_vector1"); |
| 59 | + } |
| 60 | + boost::crypt::generate_random_string(input_vector1[i], elementSize); |
| 61 | + } |
| 62 | + |
| 63 | + // Launch the Vector Add CUDA Kernel |
| 64 | + int threadsPerBlock = 256; |
| 65 | + int blocksPerGrid = (numElements + threadsPerBlock - 1) / threadsPerBlock; |
| 66 | + std::cout << "CUDA kernel launch with " << blocksPerGrid << " blocks of " << threadsPerBlock << " threads" << std::endl; |
| 67 | + |
| 68 | + watch w; |
| 69 | + cuda_test<<<blocksPerGrid, threadsPerBlock>>>(input_vector1, output_vector.get(), numElements); |
| 70 | + cudaDeviceSynchronize(); |
| 71 | + std::cout << "CUDA kernal done in " << w.elapsed() << "s" << std::endl; |
| 72 | + |
| 73 | + err = cudaGetLastError(); |
| 74 | + if (err != cudaSuccess) |
| 75 | + { |
| 76 | + std::cerr << "Failed to launch vectorAdd kernel (error code " << cudaGetErrorString(err) << ")!" << std::endl; |
| 77 | + return EXIT_FAILURE; |
| 78 | + } |
| 79 | + |
| 80 | + // Verify that the result vector is correct |
| 81 | + std::vector<digest_type> results; |
| 82 | + results.reserve(numElements); |
| 83 | + w.reset(); |
| 84 | + for(int i = 0; i < numElements; ++i) |
| 85 | + { |
| 86 | + digest_type out; |
| 87 | + boost::crypt::sha1_hash_drbg drbg; |
| 88 | + std::span<char> in_span(input_vector1[i], static_cast<std::size_t>(64)); |
| 89 | + drbg.init(in_span); |
| 90 | + drbg.generate(out, 640U); |
| 91 | + results.emplace_back(out); |
| 92 | + } |
| 93 | + double t = w.elapsed(); |
| 94 | + |
| 95 | + // check the results |
| 96 | + for(int i = 0; i < numElements; ++i) |
| 97 | + { |
| 98 | + if (output_vector[i][0] != results[i][0]) |
| 99 | + { |
| 100 | + std::cerr << "Result verification failed at element " << i << "!" << std::endl; |
| 101 | + return EXIT_FAILURE; |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + std::cout << "Test PASSED with calculation time: " << t << "s" << std::endl; |
| 106 | + std::cout << "Done\n"; |
| 107 | + |
| 108 | + // Cleanup all the memory we allocated |
| 109 | + for (int i = 0; i < numElements; ++i) |
| 110 | + { |
| 111 | + cudaFree(input_vector1[i]); |
| 112 | + } |
| 113 | + cudaFree(input_vector1); |
| 114 | + } |
| 115 | + catch (const std::exception& e) |
| 116 | + { |
| 117 | + std::cerr << "Terminated with exception: " << e.what() << std::endl; |
| 118 | + } |
| 119 | +} |
0 commit comments