[baremetal][aarch64] Build a baremetal OpenCV for AArch64.#19921
[baremetal][aarch64] Build a baremetal OpenCV for AArch64.#19921fpetrogalli wants to merge 8 commits intoopencv:masterfrom
Conversation
|
@vpisarev @alalek, this is a patch we are using to build some components of OpenCV for baremetal execution. We would like to make it available to anyone who needs to deploy baremetal OpenCV. The toolchain file is aarch64 specific, but I believe it could be easily modified to target other architectures. I am posting this as a PR to be able to discuss it with the developers community. |
alalek
left a comment
There was a problem hiding this comment.
The patch is really massive.
This build configuration would probably broken too quickly.
Also it is unclear which limitations should be handled (looks like they are related to file-system, threads, mutexes, anything else?)
Consider writing dedicate "port" file for this configuration with stubs for missing functionality. Such port files can be included here:
opencv/modules/core/include/opencv2/core/cvdef.h
Lines 53 to 55 in 29fb4f9
Add include directory and name of this file in CMake toolchain.
cmake/templates/cvconfig.h.in
Outdated
| #cmakedefine CV_BAREMETAL | ||
|
|
There was a problem hiding this comment.
Please remove this change.
It is enough to have CMake's add_definitions("-DCV_BAREMETAL")
BTW, There are plans to deprecate cvconfig.h.
Thank you @alalek - this is very useful information, I didn't know about this possibility. If I get this right, it means that I can remove all the conditional compilation around One additional question. I would like to make the port file available upstream. Is there a place in the project where I should store it? If yes and once it is there, how can I guard it from regressions? |
I believe it can be stored near CMake toolchain file (toolchain can add extra rules for handling that):
e.g. here: |
|
I have marked some comments as resolved, just to keep track of what I am doing locally. The changes will show up when I am done with the modifications. |
|
@fpetrogalli, sorry for delay. As I described in our e-mail thread, in order to move forward, we would like to ask you to split the patch into 2, and correspondingly replace CV_BAREMETAL with a combination of specific flags, like CV_DISABLE_MULTI_THREADING, OPENCV_HAVE_FILESYSTEM_SUPPORT=OFF etc.
|
PR: opencv#19921 Changes: 1. Add a baremetal port headerfile. 2. Removed the conditional compilation of: - cubic root; - CV_CXX11 specific code; - pthread code in system.cpp; - `mutex` code in logtagmanager.hpp; - `std::thread` code in parallel.cpp. 3. Move the setting OPENCV_HAVE_FILESYSTEM_SUPPORT to zero in the header file of the port.
f2e7320 to
e4230c4
Compare
|
@alalek / @vpisarev - I have updated the PR with the code I was working on last week. I'll use this last version as the starting point for the split you have requested in the email thread and in the last comment because it already addresses some of the issues that @alalek reported. Please consider the current status of this PR as a WIP, it does not need to be reviewed. |
|
Hello - I have a question about the There are situations in which disabling [1] https://github.com/opencv/opencv/blob/master/modules/core/src/parallel.cpp. In other places [2] the code seems to be designed to be multi threaded (please correct me if I am wrong). For places like these, would it make sense to just disable the module ( [2] https://github.com/opencv/opencv/blob/master/modules/videoio/src/cap_gstreamer.cpp |
|
I believe stubs should be created in the Disabling |
PR: opencv#19921 Changes: 1. Add a baremetal port headerfile. 2. Removed the conditional compilation of: - cubic root; - CV_CXX11 specific code; - pthread code in system.cpp; - `mutex` code in logtagmanager.hpp; - `std::thread` code in parallel.cpp. 3. Move the setting OPENCV_HAVE_FILESYSTEM_SUPPORT to zero in the header file of the port.
e4230c4 to
55ffded
Compare
PR: opencv#19921 Changes: 1. Add a baremetal port headerfile. 2. Removed the conditional compilation of: - cubic root; - CV_CXX11 specific code; - pthread code in system.cpp; - `mutex` code in logtagmanager.hpp; - `std::thread` code in parallel.cpp. 3. Move the setting OPENCV_HAVE_FILESYSTEM_SUPPORT to zero in the header file of the port.
55ffded to
c86ae05
Compare
|
Please notice that I have force-rebased this PR on top of a1d7e90, which is the tip of the first feature I factored out from the original patch (the one that adds I will add more commits to make use of the macro at #19985 and remove Please don't review this patch in its totality, as it is a WIP while I work on #19985 . Please let me know if recommend a different approach instead of this workflow (rebase on top of #19985). |
PR: opencv#19921 Changes: 1. Add a baremetal port headerfile. 2. Removed the conditional compilation of: - cubic root; - CV_CXX11 specific code; - pthread code in system.cpp; - `mutex` code in logtagmanager.hpp; - `std::thread` code in parallel.cpp. 3. Move the setting OPENCV_HAVE_FILESYSTEM_SUPPORT to zero in the header file of the port.
c86ae05 to
0e5ef17
Compare
PR: opencv#19921 Changes: 1. Add a baremetal port headerfile. 2. Removed the conditional compilation of: - cubic root; - CV_CXX11 specific code; - pthread code in system.cpp; - `mutex` code in logtagmanager.hpp; - `std::thread` code in parallel.cpp. 3. Move the setting OPENCV_HAVE_FILESYSTEM_SUPPORT to zero in the header file of the port.
0e5ef17 to
dae9254
Compare
PR: opencv#19921 Changes: 1. Add a baremetal port headerfile. 2. Removed the conditional compilation of: - cubic root; - CV_CXX11 specific code; - pthread code in system.cpp; - `mutex` code in logtagmanager.hpp; - `std::thread` code in parallel.cpp. 3. Move the setting OPENCV_HAVE_FILESYSTEM_SUPPORT to zero in the header file of the port.
dae9254 to
ae6b6f2
Compare
PR: opencv#19921 Changes: 1. Add a baremetal port headerfile. 2. Removed the conditional compilation of: - cubic root; - CV_CXX11 specific code; - pthread code in system.cpp; - `mutex` code in logtagmanager.hpp; - `std::thread` code in parallel.cpp. 3. Move the setting OPENCV_HAVE_FILESYSTEM_SUPPORT to zero in the header file of the port.
ae6b6f2 to
9c363cd
Compare
PR: opencv#19921 Changes: 1. Add a baremetal port headerfile. 2. Removed the conditional compilation of: - cubic root; - CV_CXX11 specific code; - pthread code in system.cpp; - `mutex` code in logtagmanager.hpp; - `std::thread` code in parallel.cpp. 3. Move the setting OPENCV_HAVE_FILESYSTEM_SUPPORT to zero in the header file of the port.
9c363cd to
33c4c73
Compare
905f088 to
3f50e14
Compare
3f50e14 to
4af249d
Compare
| #else // CV_DISABLE_MULTI_THREAD | ||
| // Custom (failing) implementation of `std::mutex`. | ||
| struct MutexType { | ||
| void lock(){ |
There was a problem hiding this comment.
@alalek @vpisarev - I am seeing failures like the following in my baremetal runs. I have poked around a bit to see if there is a configure option that allows turning off logging, but I haven't found one so far. Should I come up with a "turn off logging" cmake option? Or is there already one and am I simply missing it?
Francesco
terminate called after throwing an instance of 'cv::Exception'
what(): OpenCV(4.5.2-dev) /home/frapet01/projects/opencv/opencv/modules/core/src/utils/logtagmanager.hpp:46: error: (-213:The function/feature is not implemented) cv::Mutex are disabled by CV_DISABLE_MULTI_THREAD=ON in function 'lock'
There was a problem hiding this comment.
Never mind, I found the macro that needs disabling: #define CV_LOG_WITH_TAG(tag, msgLevel, extra_check0, extra_check1, ...)
Should I disable it under the PR that disables filesystem support, at #20010 , or should I create a CV_DISABLE_LOGGING option? Please let me know what you want me to do, for now I'll just push an extra commit in this PR that disables it under OPENCV_HAVE_FILESYSTEM_SUPPORT.
There was a problem hiding this comment.
Disabling logging is not a best option.
Consider replacing this part:
- using LockType = std::lock_guard<MutexType>;
+ using LockType = cv::AutoLock;There was a problem hiding this comment.
Using cv::AutoLock would require to implement stubs for cv::AutoLock, which seems to raise issues similar to implementing stub for things like mutexes and threads.
I think it would make sense to have a separate option that disable logging if invoked at command line configuration. That would be turned on only manually when targeting baremetal. I agree that disabling logging might not be ideal in the general case, but for baremetal there is no filesystem to log to.
Alternatively, if you really think we should use cv-AutoLock, I need some help in figuring out what to do. In #19985 the AutoLock/Mutex stubs are defined as follows:
#ifndef CV_DISABLE_MULTI_THREAD
typedef std::recursive_mutex Mutex;
typedef std::lock_guard<cv::Mutex> AutoLock;
#else // CV_DISABLE_MULTI_THREAD
// Custom (failing) implementation of `std::recursive_mutex`.
struct Mutex {
void lock(){
CV_Error(cv::Error::StsNotImplemented,
"cv::Mutex are disabled by CV_DISABLE_MULTI_THREAD=ON");
}
void unlock(){
CV_Error(cv::Error::StsNotImplemented,
"cv::Mutex are disabled by CV_DISABLE_MULTI_THREAD=ON");
}
};
// Stub for cv::AutoLock when threads are disabled.
struct AutoLock {
AutoLock(Mutex &) { }
};
#endif // CV_DISABLE_MULTI_THREAD
What should I based the implementation of AutoLock on? As you can see, even if I use cv::AutoLock, I would still be getting exceptions from the stubs of cv::Mutex.
There was a problem hiding this comment.
Right, "no-op" AutoLock stub is required for single-threaded mode ( #19985 (comment) ).
There was a problem hiding this comment.
Ah, right! Thanks @alalek . I forgot about the stubs for AutoLock. With this change, it works, no more exceptions :) I have updated the patch for the single thread execution with the suggestion, see #19985
I have also rebased this one to use that new commit.
One thing that is not clear to me is the reason why we have to raise exceptions for the method of cv::Mutex, and just use stubs for cv::AutoLock. I am asking because I would like to add a comment in the code of #19985 explaining this, as other people my ask the same question when seeing the stub and the exception.
2028358 to
2967c8f
Compare
90d5f9d to
1100ece
Compare
d7da79a to
a85dadd
Compare
The changes in this patch are needed to have the code at opencv#19921 build again. * Move the changes in cmake/OpenCVUtils.cmake into the main CmakeLists.txt file. It seems that the setting underneath the option `OPENCV_DISABLE_THREAD_SUPPORT` need to be set after all the options have been configured, otherwise they won't be picked up. This happens only in combination with the toolchain file needed in the baremetal builds. * Replace `ocv_update` with `ocv_force_update`. The variables need to be re-written in case they have already been set by other configuration steps. The previous invocation `ocv_update` was leaving the variables untouched if already set.
a85dadd to
2f0e78e
Compare
The option forces the library to build without thread support.
- reduce amount of #if conditions
2f0e78e to
fe56fbf
Compare
This patch provide a toolchain file that allows to build the library
for baremetal applications. Minimal changes have been applied to the
code to be able to compile with a baremetal toolchain.
The option `CV_BAREMETAL` is used to guard the bits in the code that
are baremetal-specific.
The toolchain file requires the path to the baremetal
toolchain. Example cmake invocation:
cmake ../opencv/ \
-DCMAKE_TOOLCHAIN_FILE=../opencv/platforms/baremetal/aarch64.toolchain.cmake \
-DBAREMETAL_TOOLCHAIN_PATH=/absolute/path/to/gcc-baremetal-toolchain/bin/ \
-DBUILD_EXAMPLES=ON
A barematel toolchain for targeting aarch64 can be found at [1], under
`aarch64-none-elf`.
[1] https://developer.arm.com/tools-and-software/open-source-software/developer-tools/gnu-toolchain/gnu-a/downloads
The folder `samples/baremetal` provides two example baremetal
applications.
1. Disable threads in openjpeg from the toolchain file. 2. Have the perf tests to build.
…when the filesystem is disabled. This reverts commit fafcd25.
fe56fbf to
174b6b2
Compare
|
Replaced by #20392 |
This patch provide a toolchain file that allows to build the library
for baremetal applications. Minimal changes have been applied to the
code to be able to compile with a baremetal toolchain.
The option
CV_BAREMETALis used to guard the bits in the code thatare baremetal-specific.
The toolchain file requires the path to the baremetal
toolchain. Example cmake invocation:
A barematel toolchain for targeting aarch64 can be found at [1], under
aarch64-none-elf.[1] https://developer.arm.com/tools-and-software/open-source-software/developer-tools/gnu-toolchain/gnu-a/downloads
The current patch covers the following modules
-- OpenCV modules:
-- To be built: core features2d highgui imgcodecs imgproc ml photo video
-- Disabled: flann ts videoio world
-- Disabled by dependency: calib3d objdetect stitching
-- Unavailable: dnn gapi java python2 python3
-- Applications: examples apps
The folder
samples/baremetalprovides two example baremetal applications.Acknowledgment
The patch is based on original code written by @l-oneil and @joshsowerby.
Pull Request Readiness Checklist
See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request
Patch to opencv_extra has the same branch name.