-
Notifications
You must be signed in to change notification settings - Fork 15
Add helper function for fixed points #71
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
Conversation
src/core/jbpf_fixpoint_utils.h
Outdated
* Converts an unsigned integer to fixedpt, preserving accuracy. | ||
*/ | ||
inline fixedpt | ||
fixedpt_from_uint(unsigned int value) |
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.
Why do we need this one? It seems to already exist in src/core/jbpf_helper_utils.h (under name fixedpt_toint)? If we need uint, shouldn't we add it as a macro in the headerfile, rather than a function?
{ | ||
float value = 1.0; | ||
int32_t fixed_value = float_to_fixed(value); | ||
float float_value = fixed_to_float(fixed_value); |
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.
Can you do a test to compare fixed_value with the expected one here too, here and elsewhere in this file? These are cheap tests that can be useful.
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.
@bradunov I think I have those already: the tests (Test 14 to 17) in this same file
@doctorlai-msrc, can you please add a more detailed description about this PR? Specifically the assumptions and capabilities supported by the proposed conversions. |
Added @xfoukas |
@@ -0,0 +1,327 @@ | |||
// Copyright (c) Microsoft Corporation. All rights reserved. | |||
/** |
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.
@doctorlai-msrc, we should have some tests, where you start with a float, which is converted to fixedpt, then we do some operations on it, like addition, subtraction, multiplication, etc, and then we convert back to float and check that we get the expected value.
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.
@xfoukas sure, added.
return 1; | ||
} | ||
|
||
float a = 1.0; |
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.
Why not use decimals here (e.g,, 1.25 and 3.124)?
float b = 2.0; | ||
|
||
// 3.0 in fixedpt | ||
data->test_passed = float_to_fixed(a) + float_to_fixed(b); |
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.
We should be testing all the operations here, not just addition.
// the values are computed in the codelet | ||
// Test case 1: convert two floats to fixedpt and add them, then convert back to float | ||
// 1.23 + 2.34 = 3.57 | ||
assert(fabs(data.test_float_1 - 3.57) < 0.01); |
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.
Can you please explain why this needs to be tested with inequalities? Is the expected accuracy of the test not known a priori? The same question applies for all the other tests below.
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.
because there is an precision loss performing float addition, especially, the floats are converted to fixedpt and then converted back.
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.
But isn't the loss of precision known? Can it change depending on where the test runs? If not, why not check against specific values?
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.
I don't understand. This is how we compare floats in the test. See here
// Test case 5: test float_to_fixed | ||
assert(data.test_int_1 == fixedpt_rconst(1.2)); | ||
// Test case 6: test double_to_fixed | ||
assert(data.test_int64_1 == fixedpt_rconst(2.345)); |
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.
Shouldn't this be converted to float and then do a comparison between floats?
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.
Test 3 to 6 are for each function, which are sufficient.
* Add helper function for fixed points * Addressing the comments * Revert * Continued work * Fix the algorithms and tests * Minor tweaks * Cleanup * Avoid clang optimise the code * Fix implementations * Test fixed_to_float and fixed_to_double * Fix type * Add 2 more tests * Cleanup * Clang format inconsistent fix * Add more tests in the codelet Co-authored-by: Zhihua Lai <[email protected]>
* Add helper function for fixed points (#71) * Add helper function for fixed points * Addressing the comments * Revert * Continued work * Fix the algorithms and tests * Minor tweaks * Cleanup * Avoid clang optimise the code * Fix implementations * Test fixed_to_float and fixed_to_double * Fix type * Add 2 more tests * Cleanup * Clang format inconsistent fix * Add more tests in the codelet * Increase MAX_NUM_HOOKS from 64 to 128 (#90) * Increase MAX_NUM_HOOKS from 64 to 96 * Increase MAX_NUM_HOOKS to 128 --------- Co-authored-by: Zhihua Lai <[email protected]>
* Add helper function for fixed points (#71) * Add helper function for fixed points * Addressing the comments * Revert * Continued work * Fix the algorithms and tests * Minor tweaks * Cleanup * Avoid clang optimise the code * Fix implementations * Test fixed_to_float and fixed_to_double * Fix type * Add 2 more tests * Cleanup * Clang format inconsistent fix * Add more tests in the codelet * Increase MAX_NUM_HOOKS from 64 to 128 (#90) * Increase MAX_NUM_HOOKS from 64 to 96 * Increase MAX_NUM_HOOKS to 128 --------- Co-authored-by: Zhihua Lai <[email protected]> Co-authored-by: matthewbalkwill <[email protected]>
* Add helper function for fixed points (#71) * Add helper function for fixed points * Addressing the comments * Revert * Continued work * Fix the algorithms and tests * Minor tweaks * Cleanup * Avoid clang optimise the code * Fix implementations * Test fixed_to_float and fixed_to_double * Fix type * Add 2 more tests * Cleanup * Clang format inconsistent fix * Add more tests in the codelet * Increase MAX_NUM_HOOKS from 64 to 128 (#90) * Increase MAX_NUM_HOOKS from 64 to 96 * Increase MAX_NUM_HOOKS to 128 --------- Co-authored-by: Zhihua Lai <[email protected]> Co-authored-by: matthewbalkwill <[email protected]>
* Add branch instructions in CONTRIBUTING.md and Update ADO pipeline triggers (#78) * Add Github Action Pipeline and Fixed INITIALIZE_SUBMODULES (#64) * Add Debug build configuration testing to CI pipeline (#57) --------- Co-authored-by: Zhihua Lai <[email protected]> Co-authored-by: Xenofon Foukas <[email protected]> * Add helper function for fixed points (#71) (#85) Co-authored-by: Zhihua Lai <[email protected]> * Fixes for tests to pass when cmake build type is Release (#86) This fixes issue #79. When the cmake build type is not defined it defaults to Release. Given that the tests rely on assert() to pass, the PR also undefs NDEBUG in the release build flags to prevent the optimizer from removing the assert() calls. * Improve Validation Error Messages in Linked Map (#82) * Matt/dev: MAX_NUM_HOOKS to 128 (#91) * Add helper function for fixed points (#71) * Increase MAX_NUM_HOOKS from 64 to 128 (#90) --------- Co-authored-by: Zhihua Lai <[email protected]> * Refactor run_lcm_subproc by removing memory allocation with new/delete. (#93) * Fix build/package error in ubuntu22.04 (#96) * Add native support for ARM builds (#59) --------- Co-authored-by: Zhihua Lai <[email protected]> Co-authored-by: Xenofon Foukas <[email protected]> Co-authored-by: Connor Settle <[email protected]> Co-authored-by: Zhihua Lai <[email protected]> * Bugfix: perf time measurement API with invalid times (#102) * Bump ubpf to the latest version (#103) * Matt/issue94 (#95) * Remove ado correctness + move to cloud agents (#100) * Sync dev with main (#105) * Add helper function for fixed points (#71) * Increase MAX_NUM_HOOKS from 64 to 128 (#90) --------- Co-authored-by: Zhihua Lai <[email protected]> Co-authored-by: matthewbalkwill <[email protected]> --------- Co-authored-by: Zhihua Lai <[email protected]> Co-authored-by: Xiaochuan Ye <[email protected]> Co-authored-by: matthewbalkwill <[email protected]> Co-authored-by: Anita Tenjarla <[email protected]> Co-authored-by: Zhihua Lai <[email protected]> Co-authored-by: Connor Settle <[email protected]>
* Adding a emulator * Add Python/ctypesgen dependencies for emulator * Add jbpf_agent_hooks.h and generate py * Add time helper functions * Test emulator * Add missing file * Add missing fields * Fix time event initialization * Refactor * Remove protobuf, update jbpf_stats_report.o * Add xran helper functions, and create random stream_id * Clang-format * Fix cppcheck issue * Refactor default values * Default num_codelet_descriptors to len(codelet_descriptors) * Enable ASAN in tests, ignore ODR, cleanup * Clangformat * Support custom helper functions and refactor * Fix clang-format * Move definitions above users include file * Remove XRAN * Add Custom init code * Add yaml_to_json, refactor * Fix path * Add debug option to emulator_utils.jbpf_handle_out_bufs * Match codelet_descriptor in yaml * Add missing define agent hooks * Revert * jbpf_handle_out_bufs should return how many messages that have been processed * Add some debug output when calling periodic_call and report_stats hook * Add a test to make sure the report_stats is actually called * Add comment * Clang-format -i * Add debug printf * Continued work * Add matching ck_epoch_end * Clang * Continued testing * Add more debugging * Clang * Debug * Debug print * Add more debugging message * More debugging * Add tests for report_stats and periodic_call, add debugging * Clang-format * Pipeline integration * simple_output2.o * Add missing files * Refactor & debug * COntinued work * Continued work * Revert and Cleanup * Revert * Fix * Ignore dynamic builds * Fix * Fix syntax * Fix syntax * Keep static mode only * Revert * Emulate time and add a test * Add test * Fix clang-format * Add missing file * Fix compilation under ubuntu20 * Init draft of adding both options * Fix * Pipeline * Move autogen wrappers * Fix cppcheck issue * Build both by default * Fix JBPF_SHARED_LIB * Fix build under ubuntu24.04 * Fix segfault at Static * Add emulator tests to github action, fix RELEASE * Typo * First round of addressing comments * Improve test description comments * Add some more doc * Add to ARM pipeline * Sync dev with main (#105) * Add helper function for fixed points (#71) * Add helper function for fixed points * Addressing the comments * Revert * Continued work * Fix the algorithms and tests * Minor tweaks * Cleanup * Avoid clang optimise the code * Fix implementations * Test fixed_to_float and fixed_to_double * Fix type * Add 2 more tests * Cleanup * Clang format inconsistent fix * Add more tests in the codelet * Increase MAX_NUM_HOOKS from 64 to 128 (#90) * Increase MAX_NUM_HOOKS from 64 to 96 * Increase MAX_NUM_HOOKS to 128 --------- Co-authored-by: Zhihua Lai <[email protected]> Co-authored-by: matthewbalkwill <[email protected]> * Refactor docker files * Add to github actions * Add System Information * Merge and fix * Fix comments * Readme * More doc * Update docs * Move emulator tests to the end * Add debug messages * Add more debug * Update codelet to provide more information * Comment * Fix print error * Revert "Update codelet to provide more information" This reverts commit 95dc9e4. * Fix segfault * Use smart ptr * Revert "Adding a emulator" * Revert "Enable Building Both libjbpf.a and libjbpf.so with cmake option -DJBPF_STATIC=Both" --------- Co-authored-by: Xenofon Foukas <[email protected]> Co-authored-by: matthewbalkwill <[email protected]>
Context
In jbpf codelets, the float point operations are not supported e.g. adding two floats would make the codelet unverifiable.
To overcome this limitation, we have added helper functions to convert float to fixed and vice versa.
Changes in this PR
This PR adds the following four helper functions in
jbpf_helper_utils.h
to convert the single/double to/from thefixedpt
type.This PR also adds tests to the fixedpt library aka.
jbpf_helper_utils.h
Sample usages in codelet
The following is a verified codelet that shows the usage of the helper functions. In the codelet, two floats are defined, and converted to fixedpt for additions. Similarly two doubles are converted to fixedpt for the addition as well. It also demonstrates converting a float and a double from fixedpt to float and double respectively. All these values are passed back to C test for verification of correctness.
#70