1# Building and Running Applications Using OpenMP 2 3The OpenHarmony NDK provides dynamic and static library files of OpenMP so that you can use OpenMP in native applications. This topic guides you through on how to call library files in [DevEco Studio](https://developer.huawei.com/consumer/en/deveco-studio/) to use OpenMP. For details about the APIs and examples, see [clang-OpenMPSupport](https://clang.llvm.org/docs/OpenMPSupport.html). 4 5## How to Develop 6 7### 1. Creating a Native C++ Project 8[Create an NDK Project](create-with-ndk.md). 9 10### 2. Adding Dependencies 11 12There are two ways to incorporate the OpenMP library: static linking and dynamic linking. 13 14> **NOTE** 15> 16> The [OpenMP Tools Interface (OMPT)](https://www.openmp.org/spec-html/5.0/openmpsu15.html#x25-240001.5.1) tool can be used only for static linking. 17> 18 19#### Static Linking 20 21(1) Open the **entry/src/main/cpp/CMakeLists.txt** file, and add the static library **libomp.a** and log dependency **libhilog\_ndk.z.so** to **target\_link\_libraries**. 22 23```makelists 24target_link_libraries(entry PUBLIC libomp.a libace_napi.z.so libhilog_ndk.z.so) 25``` 26 27(2) Open the **entry/build-profile.json5** file, and add **-static-openmp -fopenmp** to **cppFlags** under **buildOption** > **externalNativeOptions**. 28 29``` 30"buildOption": { 31 "externalNativeOptions": { 32 "path": "./src/main/cpp/CMakeLists.txt", 33 "arguments": "", 34 "cppFlags": "-static-openmp -fopenmp", 35 } 36 } 37``` 38 39#### Dynamic Linking 40 41(1) Open the **entry/src/main/cpp/CMakeLists.txt** file, and add the dynamic library **libomp.so** and log dependency **libhilog\_ndk.z.so** to **target\_link\_libraries**. 42 43```makelists 44target_link_libraries(entry PUBLIC libomp.so libace_napi.z.so libhilog_ndk.z.so) 45``` 46 47(2) Open the **entry/build-profile.json5** file, and add **-fopenmp** to **cppFlags** under **buildOption** > **externalNativeOptions**. 48 49``` 50"buildOption": { 51 "externalNativeOptions": { 52 "path": "./src/main/cpp/CMakeLists.txt", 53 "arguments": "", 54 "cppFlags": "-fopenmp", 55 } 56 } 57``` 58 59(3) Copy the dynamic library file **libomp.so** in the {*SDK installation directory*}\{*Version number*}**\openharmony\native\llvm\lib\aarch64-linux-ohos** directory to the **entry/libs/arm64-v8a** directory of the project. 60 61### 3. Modifying Source Files 62 63(1) In **entry/src/main/cpp/napi_init.cpp**, include the **omp.h** header file, and add the **OmpTest** function. 64 65```cpp 66#include "napi/native_api.h" 67#include "omp.h" 68#include "hilog/log.h" 69 70#undef LOG_DOMAIN 71#undef LOG_TAG 72#define LOG_DOMAIN 0x3200 // Global domain, which identifies the service domain. 73#define LOG_TAG "MY_TAG" // Global tag, which identifies the module log tag. 74 75static napi_value OmpTest(napi_env env, napi_callback_info info) 76{ 77 78 OH_LOG_INFO(LOG_APP, "=================Hello OpenMP test.===================="); 79 #pragma omp parallel 80 { 81 OH_LOG_INFO(LOG_APP, "Hello OpenMP!"); 82 } 83 return nullptr; 84} 85 86EXTERN_C_START 87static napi_value Init(napi_env env, napi_value exports) 88{ 89 napi_property_descriptor desc[] = { 90 { "ompTest", nullptr, OmpTest, nullptr, nullptr, nullptr, napi_default, nullptr } 91 }; 92 napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); 93 return exports; 94} 95EXTERN_C_END 96 97static napi_module demoModule = { 98 .nm_version = 1, 99 .nm_flags = 0, 100 .nm_filename = nullptr, 101 .nm_register_func = Init, 102 .nm_modname = "entry", 103 .nm_priv = ((void*)0), 104 .reserved = { 0 }, 105}; 106 107extern "C" __attribute__((constructor)) void RegisterEntryModule(void) 108{ 109 napi_module_register(&demoModule); 110} 111 112``` 113 114(2) In **entry/src/main/cpp/types/libentry/Index.d.ts**, export the **ompTest** function. 115 116```TS 117export const ompTest: () => null; 118``` 119 120(3) In **entry/src/main/ets/pages/Index.ets**, call the **ompTest** function. 121 122```TS 123import testNapi from 'libentry.so'; 124 125@Entry 126@Component 127struct Index { 128 @State message: string = 'Hello OpenMP'; 129 130 build() { 131 Row() { 132 Column() { 133 Text(this.message) 134 .fontSize(50) 135 .fontWeight(FontWeight.Bold) 136 .onClick(() => { 137 testNapi.ompTest(); 138 }) 139 } 140 .width('100%') 141 } 142 .height('100%') 143 } 144} 145``` 146 147### 4. Running the Application and Verifying the Result 148 149Check the device connection and [sign the application](https://developer.huawei.com/consumer/en/doc/harmonyos-guides-V5/ide-signing-V5) information. Click the **Run** button in the upper right corner of DevEco Studio. After the application is started, the **Hello OpenMP** page is displayed on the device. Tap **Hello OpenMP** and view the **Log** page on DevEco Studio. You can see multiple "Hello OpenMP!" messages. 150 151 152 153> **NOTE** 154> 155> When an OpenMP application is running, the error message "dlopen_impl load library header failed for libarcher.so" will be displayed in HiLog, as shown in the following figure. The **libarcher.so** file in the error information is used only when the OpenMP application has Tsan enabled. Currently, OpenHarmony does not support Tsan of OpenMP applications. Therefore, this error message does not affect the normal running of the application and can be ignored. 156> 157>  158