-
Notifications
You must be signed in to change notification settings - Fork 250
Initial implementation of find_non_centrality for non_central_f_distribution
#1345
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
Open
JacobHass8
wants to merge
10
commits into
boostorg:develop
Choose a base branch
from
JacobHass8:non-central-f-inverse
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+375
−1
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
89f8dd3
Initial implementation of [skip ci]
JacobHass8 ed3289d
Added tests and changed namescapes
JacobHass8 55e2b17
Added boost tuple support
JacobHass8 b330dd7
Updated code to match non_central_chi_squared syntax
JacobHass8 2464bcc
Added documentation; changed parameter naming
JacobHass8 a487eaa
Merge remote-tracking branch 'origin/develop' into non-central-f-inverse
mborland b794ab7
Add GPU markers and change std::pair to boost::math::pair
mborland b223789
Add CUDA tests
mborland e51d71a
Consistency of GPU markers in the docs
mborland 59a4e7a
Added catch for edge case nc=0 and testing for this
JacobHass8 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,114 @@ | ||
| // Copyright John Maddock 2016. | ||
| // Copyright Matt Borland 2024. | ||
| // Use, modification and distribution are subject to the | ||
| // Boost Software License, Version 1.0. (See accompanying file | ||
| // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | ||
|
|
||
| #define BOOST_MATH_OVERFLOW_ERROR_POLICY ignore_error | ||
|
|
||
| #include <iostream> | ||
| #include <iomanip> | ||
| #include <vector> | ||
| #include <boost/math/distributions/non_central_f.hpp> | ||
| #include <boost/math/special_functions/relative_difference.hpp> | ||
| #include <boost/random/mersenne_twister.hpp> | ||
| #include <boost/random/uniform_real_distribution.hpp> | ||
| #include "cuda_managed_ptr.hpp" | ||
| #include "stopwatch.hpp" | ||
|
|
||
| // For the CUDA runtime routines (prefixed with "cuda_") | ||
| #include <cuda_runtime.h> | ||
|
|
||
| typedef double float_type; | ||
|
|
||
| /** | ||
| * CUDA Kernel Device code | ||
| * | ||
| */ | ||
| __global__ void cuda_test(const float_type *in1, float_type *out, int numElements) | ||
| { | ||
| int i = blockDim.x * blockIdx.x + threadIdx.x; | ||
|
|
||
| if (i < numElements) | ||
| { | ||
| boost::math::non_central_f_distribution<float_type> dist(5, 5, 5); | ||
| out[i] = dist.find_non_centrality(5, 5, 5, in1[i]); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Host main routine | ||
| */ | ||
| int main(void) | ||
| { | ||
| try{ | ||
|
|
||
| // Error code to check return values for CUDA calls | ||
| cudaError_t err = cudaSuccess; | ||
|
|
||
| // Print the vector length to be used, and compute its size | ||
| int numElements = 50000; | ||
| std::cout << "[Vector operation on " << numElements << " elements]" << std::endl; | ||
|
|
||
| // Allocate the managed input vector A | ||
| cuda_managed_ptr<float_type> input_vector1(numElements); | ||
|
|
||
| // Allocate the managed output vector C | ||
| cuda_managed_ptr<float_type> output_vector(numElements); | ||
|
|
||
| boost::random::mt19937 gen; | ||
| boost::random::uniform_real_distribution<float_type> dist(0.1, 0.9); | ||
| // Initialize the input vectors | ||
| for (int i = 0; i < numElements; ++i) | ||
| { | ||
| input_vector1[i] = dist(gen); | ||
| } | ||
|
|
||
| // Launch the Vector Add CUDA Kernel | ||
| int threadsPerBlock = 256; | ||
| int blocksPerGrid =(numElements + threadsPerBlock - 1) / threadsPerBlock; | ||
| std::cout << "CUDA kernel launch with " << blocksPerGrid << " blocks of " << threadsPerBlock << " threads" << std::endl; | ||
|
|
||
| watch w; | ||
| cuda_test<<<blocksPerGrid, threadsPerBlock>>>(input_vector1.get(), output_vector.get(), numElements); | ||
| cudaDeviceSynchronize(); | ||
| std::cout << "CUDA kernal done in " << w.elapsed() << "s" << std::endl; | ||
|
|
||
| err = cudaGetLastError(); | ||
| if (err != cudaSuccess) | ||
| { | ||
| std::cerr << "Failed to launch non_central_f distribution kernel (error code " << cudaGetErrorString(err) << ")!" << std::endl; | ||
| return EXIT_FAILURE; | ||
| } | ||
|
|
||
| // Verify that the result vector is correct | ||
| std::vector<float_type> results; | ||
| results.reserve(numElements); | ||
| w.reset(); | ||
|
|
||
| boost::math::non_central_f_distribution<float_type> non_central_dist(5, 5, 5); | ||
| for(int i = 0; i < numElements; ++i) | ||
| { | ||
| results.push_back(non_central_dist.find_non_centrality(5, 5, 5, input_vector1[i])); | ||
| } | ||
| double t = w.elapsed(); | ||
| // check the results | ||
| for(int i = 0; i < numElements; ++i) | ||
| { | ||
| if (boost::math::epsilon_difference(output_vector[i], results[i]) > 100.0) | ||
| { | ||
| std::cerr << "Result verification failed at element " << i << "!" << std::endl; | ||
| std::cerr << "Error rate was: " << boost::math::epsilon_difference(output_vector[i], results[i]) << "eps" << std::endl; | ||
| return EXIT_FAILURE; | ||
| } | ||
| } | ||
|
|
||
| std::cout << "Test PASSED with calculation time: " << t << "s" << std::endl; | ||
| std::cout << "Done\n"; | ||
| } | ||
| catch(const std::exception& e) | ||
| { | ||
| std::cerr << "Stopped with exception: " << e.what() << std::endl; | ||
| } | ||
| return 0; | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks suspicious to me, some possibly naive questions:
One possible alternative would be to call
f(tools::min_value<T>())and if the answer lies to the left of that, then return 0. Hopefully bracket_and_solve_root will handle all answers to the right ofmin_value(). Hope that makes sense!