Skip to content

Commit adf7a9a

Browse files
committed
Add CUDA testing
1 parent 5e6326b commit adf7a9a

7 files changed

+666
-0
lines changed

test/cuda_jamfile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,12 @@ run test_landau_pdf_double.cu ;
5353
run test_landau_pdf_float.cu ;
5454
run test_landau_quan_double.cu;
5555
run test_landau_quan_float.cu ;
56+
run test_mapairy_cdf_double.cu ;
57+
run test_mapairy_cdf_float.cu ;
58+
run test_mapairy_pdf_double.cu ;
59+
run test_mapairy_pdf_float.cu ;
60+
run test_mapairy_quan_double.cu ;
61+
run test_mapairy_quan_float.cu ;
5662

5763
# Special Functions
5864
# run test_beta_simple.cpp ;

test/test_mapairy_cdf_double.cu

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
// Copyright John Maddock 2016.
2+
// Copyright Matt Borland 2024.
3+
// Use, modification and distribution are subject to the
4+
// Boost Software License, Version 1.0. (See accompanying file
5+
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6+
7+
#define BOOST_MATH_OVERFLOW_ERROR_POLICY ignore_error
8+
9+
#include <iostream>
10+
#include <iomanip>
11+
#include <vector>
12+
#include <boost/math/distributions/mapairy.hpp>
13+
#include <boost/math/special_functions/relative_difference.hpp>
14+
#include <boost/random/mersenne_twister.hpp>
15+
#include <boost/random/uniform_real_distribution.hpp>
16+
#include "cuda_managed_ptr.hpp"
17+
#include "stopwatch.hpp"
18+
19+
// For the CUDA runtime routines (prefixed with "cuda_")
20+
#include <cuda_runtime.h>
21+
22+
typedef double float_type;
23+
24+
/**
25+
* CUDA Kernel Device code
26+
*
27+
*/
28+
__global__ void cuda_test(const float_type *in1, float_type *out, int numElements)
29+
{
30+
using std::cos;
31+
int i = blockDim.x * blockIdx.x + threadIdx.x;
32+
33+
if (i < numElements)
34+
{
35+
out[i] = cdf(boost::math::mapairy_distribution<float_type>(), in1[i]);
36+
}
37+
}
38+
39+
/**
40+
* Host main routine
41+
*/
42+
int main(void)
43+
{
44+
try{
45+
46+
// Error code to check return values for CUDA calls
47+
cudaError_t err = cudaSuccess;
48+
49+
// Print the vector length to be used, and compute its size
50+
int numElements = 50000;
51+
std::cout << "[Vector operation on " << numElements << " elements]" << std::endl;
52+
53+
// Allocate the managed input vector A
54+
cuda_managed_ptr<float_type> input_vector1(numElements);
55+
56+
// Allocate the managed output vector C
57+
cuda_managed_ptr<float_type> output_vector(numElements);
58+
59+
boost::random::mt19937 gen;
60+
boost::random::uniform_real_distribution<float_type> dist(0, 1);
61+
// Initialize the input vectors
62+
for (int i = 0; i < numElements; ++i)
63+
{
64+
input_vector1[i] = dist(gen);
65+
}
66+
67+
// Launch the Vector Add CUDA Kernel
68+
int threadsPerBlock = 512;
69+
int blocksPerGrid =(numElements + threadsPerBlock - 1) / threadsPerBlock;
70+
std::cout << "CUDA kernel launch with " << blocksPerGrid << " blocks of " << threadsPerBlock << " threads" << std::endl;
71+
72+
watch w;
73+
cuda_test<<<blocksPerGrid, threadsPerBlock>>>(input_vector1.get(), output_vector.get(), numElements);
74+
cudaDeviceSynchronize();
75+
std::cout << "CUDA kernal done in " << w.elapsed() << "s" << std::endl;
76+
77+
err = cudaGetLastError();
78+
if (err != cudaSuccess)
79+
{
80+
std::cerr << "Failed to launch vectorAdd kernel (error code " << cudaGetErrorString(err) << ")!" << std::endl;
81+
return EXIT_FAILURE;
82+
}
83+
84+
// Verify that the result vector is correct
85+
std::vector<float_type> results;
86+
results.reserve(numElements);
87+
w.reset();
88+
for(int i = 0; i < numElements; ++i)
89+
results.push_back(cdf(boost::math::mapairy_distribution<float_type>(), input_vector1[i]));
90+
double t = w.elapsed();
91+
// check the results
92+
for(int i = 0; i < numElements; ++i)
93+
{
94+
if (boost::math::epsilon_difference(output_vector[i], results[i]) > 100.0)
95+
{
96+
std::cerr << "Result verification failed at element " << i << "!" << std::endl;
97+
std::cerr << "Error rate was: " << boost::math::epsilon_difference(output_vector[i], results[i]) << "eps" << std::endl;
98+
return EXIT_FAILURE;
99+
}
100+
}
101+
102+
std::cout << "Test PASSED with calculation time: " << t << "s" << std::endl;
103+
std::cout << "Done\n";
104+
}
105+
catch(const std::exception& e)
106+
{
107+
std::cerr << "Stopped with exception: " << e.what() << std::endl;
108+
}
109+
return 0;
110+
}

test/test_mapairy_cdf_float.cu

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
// Copyright John Maddock 2016.
2+
// Copyright Matt Borland 2024.
3+
// Use, modification and distribution are subject to the
4+
// Boost Software License, Version 1.0. (See accompanying file
5+
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6+
7+
#define BOOST_MATH_OVERFLOW_ERROR_POLICY ignore_error
8+
9+
#include <iostream>
10+
#include <iomanip>
11+
#include <vector>
12+
#include <boost/math/distributions/mapairy.hpp>
13+
#include <boost/math/special_functions/relative_difference.hpp>
14+
#include <boost/random/mersenne_twister.hpp>
15+
#include <boost/random/uniform_real_distribution.hpp>
16+
#include "cuda_managed_ptr.hpp"
17+
#include "stopwatch.hpp"
18+
19+
// For the CUDA runtime routines (prefixed with "cuda_")
20+
#include <cuda_runtime.h>
21+
22+
typedef float float_type;
23+
24+
/**
25+
* CUDA Kernel Device code
26+
*
27+
*/
28+
__global__ void cuda_test(const float_type *in1, float_type *out, int numElements)
29+
{
30+
using std::cos;
31+
int i = blockDim.x * blockIdx.x + threadIdx.x;
32+
33+
if (i < numElements)
34+
{
35+
out[i] = cdf(boost::math::mapairy_distribution<float_type>(), in1[i]);
36+
}
37+
}
38+
39+
/**
40+
* Host main routine
41+
*/
42+
int main(void)
43+
{
44+
try{
45+
46+
// Error code to check return values for CUDA calls
47+
cudaError_t err = cudaSuccess;
48+
49+
// Print the vector length to be used, and compute its size
50+
int numElements = 50000;
51+
std::cout << "[Vector operation on " << numElements << " elements]" << std::endl;
52+
53+
// Allocate the managed input vector A
54+
cuda_managed_ptr<float_type> input_vector1(numElements);
55+
56+
// Allocate the managed output vector C
57+
cuda_managed_ptr<float_type> output_vector(numElements);
58+
59+
boost::random::mt19937 gen;
60+
boost::random::uniform_real_distribution<float_type> dist(0, 1);
61+
// Initialize the input vectors
62+
for (int i = 0; i < numElements; ++i)
63+
{
64+
input_vector1[i] = dist(gen);
65+
}
66+
67+
// Launch the Vector Add CUDA Kernel
68+
int threadsPerBlock = 512;
69+
int blocksPerGrid =(numElements + threadsPerBlock - 1) / threadsPerBlock;
70+
std::cout << "CUDA kernel launch with " << blocksPerGrid << " blocks of " << threadsPerBlock << " threads" << std::endl;
71+
72+
watch w;
73+
cuda_test<<<blocksPerGrid, threadsPerBlock>>>(input_vector1.get(), output_vector.get(), numElements);
74+
cudaDeviceSynchronize();
75+
std::cout << "CUDA kernal done in " << w.elapsed() << "s" << std::endl;
76+
77+
err = cudaGetLastError();
78+
if (err != cudaSuccess)
79+
{
80+
std::cerr << "Failed to launch vectorAdd kernel (error code " << cudaGetErrorString(err) << ")!" << std::endl;
81+
return EXIT_FAILURE;
82+
}
83+
84+
// Verify that the result vector is correct
85+
std::vector<float_type> results;
86+
results.reserve(numElements);
87+
w.reset();
88+
for(int i = 0; i < numElements; ++i)
89+
results.push_back(cdf(boost::math::mapairy_distribution<float_type>(), input_vector1[i]));
90+
double t = w.elapsed();
91+
// check the results
92+
for(int i = 0; i < numElements; ++i)
93+
{
94+
if (boost::math::epsilon_difference(output_vector[i], results[i]) > 100.0)
95+
{
96+
std::cerr << "Result verification failed at element " << i << "!" << std::endl;
97+
std::cerr << "Error rate was: " << boost::math::epsilon_difference(output_vector[i], results[i]) << "eps" << std::endl;
98+
return EXIT_FAILURE;
99+
}
100+
}
101+
102+
std::cout << "Test PASSED with calculation time: " << t << "s" << std::endl;
103+
std::cout << "Done\n";
104+
}
105+
catch(const std::exception& e)
106+
{
107+
std::cerr << "Stopped with exception: " << e.what() << std::endl;
108+
}
109+
return 0;
110+
}

test/test_mapairy_pdf_double.cu

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
// Copyright John Maddock 2016.
2+
// Copyright Matt Borland 2024.
3+
// Use, modification and distribution are subject to the
4+
// Boost Software License, Version 1.0. (See accompanying file
5+
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6+
7+
#define BOOST_MATH_OVERFLOW_ERROR_POLICY ignore_error
8+
9+
#include <iostream>
10+
#include <iomanip>
11+
#include <vector>
12+
#include <boost/math/distributions/mapairy.hpp>
13+
#include <boost/math/special_functions/relative_difference.hpp>
14+
#include <boost/random/mersenne_twister.hpp>
15+
#include <boost/random/uniform_real_distribution.hpp>
16+
#include "cuda_managed_ptr.hpp"
17+
#include "stopwatch.hpp"
18+
19+
// For the CUDA runtime routines (prefixed with "cuda_")
20+
#include <cuda_runtime.h>
21+
22+
typedef double float_type;
23+
24+
/**
25+
* CUDA Kernel Device code
26+
*
27+
*/
28+
__global__ void cuda_test(const float_type *in1, float_type *out, int numElements)
29+
{
30+
using std::cos;
31+
int i = blockDim.x * blockIdx.x + threadIdx.x;
32+
33+
if (i < numElements)
34+
{
35+
out[i] = pdf(boost::math::mapairy_distribution<float_type>(), in1[i]);
36+
}
37+
}
38+
39+
/**
40+
* Host main routine
41+
*/
42+
int main(void)
43+
{
44+
try{
45+
46+
// Error code to check return values for CUDA calls
47+
cudaError_t err = cudaSuccess;
48+
49+
// Print the vector length to be used, and compute its size
50+
int numElements = 50000;
51+
std::cout << "[Vector operation on " << numElements << " elements]" << std::endl;
52+
53+
// Allocate the managed input vector A
54+
cuda_managed_ptr<float_type> input_vector1(numElements);
55+
56+
// Allocate the managed output vector C
57+
cuda_managed_ptr<float_type> output_vector(numElements);
58+
59+
boost::random::mt19937 gen;
60+
boost::random::uniform_real_distribution<float_type> dist(0, 1);
61+
// Initialize the input vectors
62+
for (int i = 0; i < numElements; ++i)
63+
{
64+
input_vector1[i] = dist(gen);
65+
}
66+
67+
// Launch the Vector Add CUDA Kernel
68+
int threadsPerBlock = 512;
69+
int blocksPerGrid =(numElements + threadsPerBlock - 1) / threadsPerBlock;
70+
std::cout << "CUDA kernel launch with " << blocksPerGrid << " blocks of " << threadsPerBlock << " threads" << std::endl;
71+
72+
watch w;
73+
cuda_test<<<blocksPerGrid, threadsPerBlock>>>(input_vector1.get(), output_vector.get(), numElements);
74+
cudaDeviceSynchronize();
75+
std::cout << "CUDA kernal done in " << w.elapsed() << "s" << std::endl;
76+
77+
err = cudaGetLastError();
78+
if (err != cudaSuccess)
79+
{
80+
std::cerr << "Failed to launch vectorAdd kernel (error code " << cudaGetErrorString(err) << ")!" << std::endl;
81+
return EXIT_FAILURE;
82+
}
83+
84+
// Verify that the result vector is correct
85+
std::vector<float_type> results;
86+
results.reserve(numElements);
87+
w.reset();
88+
for(int i = 0; i < numElements; ++i)
89+
results.push_back(pdf(boost::math::mapairy_distribution<float_type>(), input_vector1[i]));
90+
double t = w.elapsed();
91+
// check the results
92+
for(int i = 0; i < numElements; ++i)
93+
{
94+
if (boost::math::epsilon_difference(output_vector[i], results[i]) > 100.0)
95+
{
96+
std::cerr << "Result verification failed at element " << i << "!" << std::endl;
97+
std::cerr << "Error rate was: " << boost::math::epsilon_difference(output_vector[i], results[i]) << "eps" << std::endl;
98+
return EXIT_FAILURE;
99+
}
100+
}
101+
102+
std::cout << "Test PASSED with calculation time: " << t << "s" << std::endl;
103+
std::cout << "Done\n";
104+
}
105+
catch(const std::exception& e)
106+
{
107+
std::cerr << "Stopped with exception: " << e.what() << std::endl;
108+
}
109+
return 0;
110+
}

0 commit comments

Comments
 (0)