Android Native开发中,编译优化手动配置

本文讲述了在Android项目中如何应对大数据处理需求,选择使用A72大核+算法优化,同时探讨了CMake编译优化的困惑与解决,包括Debug与Release编译下-Ofast配置失效的问题。作者发现需修改NDK中的android.toolchain.cmake,最终手动指定-Ofast提升算法性能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

问题背景

        在近期的一个Android App项目中,遇到了需要快速处理大批量数据的需求。对于上述需求,考虑使用目标SOC上的硬件加速单元进行加速,如本项目使用RockChip的RK3399 SOC,搭载了2颗A72大核,主频默认1.8Ghz,以及四颗A53小核,主频最高默认1.4GHz,由于是ARM V8架构,还搭载了128bits的SIMD的NEON协处理器。此外还有一颗Mali T860 MP4,最高主频800MHz的GPU,支持OpenGL ES3.2(最高)和OpenCL 1。因此本项目就有三个备选方案可以尝试:


  1. 使用A72大核+算法优化的方案,该方案实现难度较低,同时可以把代码算法中优化中的一部分交给编译器来做(之后要着重说的,本项目最后也采用了此方案)
  2. 使用Neon协处理,对数据进行并行处理,但是由于本项目中处理数据的方式不仅有运算量大的要求,还存在对内存的分散写入,因此不适合使用该方案进行加速。
  3. 使用OpenGL中的Compute Shader或OpenCL进行加速,但是由于MaliT860该SOC不支持ComputeShader,同时OpenCL对目标算法的性能优化有限,因此该方案也被弃用。

确定方案后,迎面而来的又是一个新的问题:


        在算法定型后,仍有两条路可以进行优化,一是手动进行汇编优化,着重优化循环控制、Cache和寄存器变量复用、流水控制、内存页优化等;二是将代码在高级语言层面进行时间复杂度优化后,剩下的交给强大的Clang编译器进行。

        对于前者,经过手动优化,数据吞吐量从20MByte/s提升到150MByte/s,就在笔者准备使用编译器自动优化时发现了问题。

        在Android Studio中,配置了CMake&Ninja&Clang进行Native层的编译,在CMakeList.txt中对项目的编译参数进行了配置,分别测试了 -O0,-O1,-O2, -Os, -O3和-Ofast对算法运行效率的影响。在测试过程中发现,处于Debug编译时,无论编译优化选项为任何配置,均不生效,即都为-O0的速度,同时由于启用了debug -g的选项,运行速度约只有17MByte/s。

        在配置为Release编译后,系统吞吐率得到了提高,能达到约100Mbyte/s,但是仍发现配置的编译优化选项并不生效。便对此展开了研究。

参考文章中的介绍,依次分析过程文件。https://blog.csdn.net/shift_wwx/article/details/84770716https://blog.csdn.net/shift_wwx/article/details/84770716

分析过程中发现了一个奇怪的现象,在CMakeList中指定了使用-Ofast优化选项,如图

 但是在最后的build.ninja中发现在配置的 -Ofast之后紧跟了一个-O2

        在编译参数中,多次配置一个选项 如-O0 -O2,只有最后一个配置选项会生效,也就验证了为什么无论如何改变CMakeList中的参数,最后的效果都没有变化的现象了。于是找到这个-O2是在哪里被拼接到编译指令中的也就可以解决问题了。

        通过一步步分析,发现所有的编译指令是通过CMakeCache控制生成的,而CMakeCache中的配置又是由.ExternalNativeBuild目录中的cmake_build_command.txt配置生成的,继续看发现其连关联了AndroidSDK中NDK中的android.toolchain.cmake文件

查看文件中内容发现了端倪(此处文件已经被修改,原本-O2的部分已经被修改为了-Ofast)

再次编译

-O2选项消失,我们手动指定的-Ofast生效

        生效后算法吞吐率提升到110MByte/s,可以看到哪怕已经使用了-Ofast的方式编译,仍不如手动进行优化。

总结:

         因此若要手动配置Android Native中的编译优化配置,需要在对应的SDK-NDK中修改,可以手动指定需要的编译优化配置(如上图中修改的-Ofast部分),也可直接去掉“-O2”的配置,在CMakeList中手动添加即可。

数据集介绍:神经元细胞核检测数据集 一、基础信息 数据集名称:神经元细胞核检测数据集 图片数量: - 训练集:16,353张 - 测试集:963张 分类类别: - Neuron(神经元细胞核):中枢神经系统的基本功能单位,检测其形态特征对神经科学研究具有重要意义。 标注格式: - YOLO格式,包含边界框坐标及类别标签,适用于目标检测任务 - 数据来源于显微镜成像,覆盖多种细胞分布形态和成像条件 二、适用场景 神经科学研究: 支持构建神经元定位分析工具,助力脑科学研究和神经系统疾病机理探索 医学影像分析: 适用于开发自动化细胞核检测系统,辅助病理诊断和细胞计数任务 AI辅助诊断工具开发: 可用于训练检测神经元退行性病变的模型,支持阿尔茨海默症等神经疾病的早期筛查 生物教育及研究: 提供标准化的神经元检测数据,适用于高校生物学实验室和科研机构的教学实验 三、数据集优势 大规模训练样本: 包含超1.6万张训练图像,充分覆盖细胞核的多样分布状态,支持模型深度学习 精准定位标注: 所有标注框均严格贴合细胞核边缘,确保目标检测模型的训练精度 任务适配性强: 原生YOLO格式可直接应用于主流检测框架(YOLOv5/v7/v8等),支持快速模型迭代 生物学特性突出: 专注神经元细胞核的形态特征,包含密集分布、重叠细胞等真实生物场景样本 跨领域应用潜力: 检测结果可延伸应用于细胞计数、病理分析、药物研发等多个生物医学领域
### DeepSeek RAG Knowledge Base Implementation and Usage #### Understanding the Components of a RAG System with Text and Knowledge Embeddings A Retrieval-Augmented Generation (RAG) system combines retrieval-based methods with generative models to improve context-awareness in language processing tasks. In this setup, text embeddings provide semantic information about textual content while knowledge graph embeddings capture structured relationships between entities[^1]. For instance, when implementing a RAG model like those mentioned under projects such as AnythingLLM or MaxKB[^2], integrating both types of embeddings can significantly enhance performance by allowing the model not only to understand unstructured data but also leverage structured relational information from knowledge graphs. #### Implementing DeepSeek RAG Knowledge Base To implement a DeepSeek RAG knowledge base effectively: - **Data Preparation**: Start by preparing your dataset which includes documents that will be indexed for retrieval along with any associated metadata. - **Knowledge Graph Construction**: Construct a knowledge graph using domain-specific ontologies where nodes represent concepts/entities within the document corpus and edges denote relations among them. This step is crucial because it forms the backbone upon which more sophisticated operations are built later on. - **Embedding Models Selection**: Choose appropriate pre-trained embedding models capable of generating high-quality representations for texts and entities present inside the constructed KG. For example, BERT could serve well here due to its proven effectiveness across many NLP benchmarks[^4]. - **Index Building & Query Processing Pipeline Setup**: Utilize tools provided through frameworks similar to FlashRAG to facilitate efficient indexing over large-scale datasets alongside streamlined query handling mechanisms designed specifically around supporting complex search queries involving multiple criteria simultaneously[^3]. Here's an illustrative Python snippet demonstrating how one might set up parts of this pipeline programmatically: ```python from transformers import BertTokenizer, BertModel import torch tokenizer = BertTokenizer.from_pretrained('bert-base-uncased') model = BertModel.from_pretrained('bert-base-uncased') def encode_text(text): inputs = tokenizer(text, return_tensors="pt", padding=True, truncation=True) outputs = model(**inputs)[0][:, 0, :] return outputs.detach().numpy() encoded_texts = [encode_text(doc['content']) for doc in documents] ``` This code initializes a BERT tokenizer and model before defining `encode_text`, a function used to convert raw strings into numerical vectors suitable for downstream machine learning applications including similarity searches against stored indices during inference time. --related questions-- 1. How does constructing a detailed knowledge graph contribute to improving RAG systems' ability to generate accurate responses? 2. What specific advantages do knowledge graph embeddings offer compared to traditional text-only approaches in enhancing LLM capabilities? 3. Can you elaborate on the role played by entity linking processes within these hybrid architectures combining text and knowledge elements? 4. Are there particular challenges encountered when scaling up implementations incorporating extensive external resources like comprehensive knowledge bases?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值