Step-by-Step Guide: Integrating OptiVec into Lazarus Projects

Step-by-Step Guide: Integrating OptiVec into Lazarus Projects

This guide shows a concise, practical workflow to add OptiVec (vectorized math libraries) to Lazarus projects on Windows and Linux, including installation, linking, simple usage examples, and basic benchmarking.

Prerequisites

  • Lazarus IDE installed (recommend current stable release).
  • Free Pascal Compiler (FPC) matching your Lazarus build.
  • OptiVec package for your platform (standalone or bundled with your compiler/runtime). Use the edition compatible with FPC (32-bit vs 64-bit, CPU architecture: x86/x8664/ARM).

1. Install OptiVec

  1. Download the OptiVec binary package for your OS/architecture from the vendor.
  2. Unpack to a known location:
    • Windows example: C:\OptiVec</li>
    • Linux example: /opt/optivec/
  3. Note the following paths you’ll need:
    • include/header directory (Pascal units or C headers)
    • library (.lib/.a/.so/.dll) directory
    • sample/demo directory (optional)

2. Prepare Lazarus Project

  1. Open Lazarus and create a new Project → Application.
  2. Save the project to a folder.

3. Add OptiVec Units / Headers

OptiVec may provide Pascal units (.pas) or only C headers and libraries. Use the appropriate approach:

A. If OptiVec provides Pascal units

  • Copy the provided .pas units into your project folder or a shared units path.
  • In Lazarus: Project → Project Options → Compiler Options → Paths → Add the units directory to “Other unit files (-Fu)”.

B. If OptiVec only provides C headers + libraries

  • Use Free Pascal’s external function declarations to link to the library. Create a Pascal unit that declares the external functions (example below).
  • Add the unit file to your project and add the library path to Project → Project Options → Compiler Options → Linking → Other linking options or Libraries path.

4. Link Libraries

  1. In Project → Project Options → Compiler Options → Linking:
    • Add library path where .dll/.so/.a/.lib reside.
    • For Windows DLLs, copy the .dll next to the executable or ensure it’s in PATH.
  2. If static linking (.a/.lib), add the library name to “Other linking options” with -l prefix as needed. Example for Linux: -loptivec

5. Example: Minimal Pascal Wrapper (C-style API)

Create a unit OptiVecAPI.pas containing external declarations. Adjust names to match actual OptiVec exported functions.

pascal

unit OptiVecAPI; interface uses SysUtils; const {\(ifdef Windows}</span><span> </span><span> OPTIVEC_LIB </span><span class="token" style="color: rgb(57, 58, 52);">=</span><span> </span><span class="token" style="color: rgb(163, 21, 21);">'optivec.dll'</span><span class="token" style="color: rgb(57, 58, 52);">;</span><span> </span><span> </span><span class="token marco" style="color: rgb(255, 0, 0);">{\)else} OPTIVEC_LIB = ‘liboptivec.so’; {$endif} function ov_add_f32(const a: PSingle; const b: PSingle; outres: PSingle; n: LongInt): Integer; cdecl; external OPTIVEC_LIB name ‘ov_addf32’; implementation end.

Note: Replace function names with actual exported symbols from your OptiVec version. Use C header or vendor docs to match signatures and calling convention (cdecl/stdcall).

6. Example: Using OptiVec in a Lazarus Form

Add a unit with a simple usage example in your main program:

pascal

uses OptiVecAPI; procedure TestOptiVec; var a, b, c: array of Single; i: Integer; begin SetLength(a, 1000); SetLength(b, 1000); SetLength(c, 1000); for i := 0 to 999 do begin a[i] := i; b[i] := i * 0.5; end; // Call OptiVec vector add: c = a + b ov_add_f32(@a[0], @b[0], @c[0], 1000); // Verify for i := 0 to 9 do WriteLn(Format(‘c[%d]=%f’,[i, c[i]])); end;

Call TestOptiVec from your FormCreate or main program.

7. Verify Correctness and Performance

  • Validate outputs against pure Pascal implementations for correctness.
  • Benchmark with high-resolution timers:
    • Use SysUtils/GetTickCount64 or better platform-specific timers.
    • Compare large-array operations (e.g., 1e6 elements) with and without OptiVec to measure speedup.

8. Troubleshooting

  • Link errors: ensure library path and exact exported symbol names/calling conventions match.
  • Missing DLL/.so: place in executable folder or add to PATH/LD_LIBRARY_PATH.
  • Mismatched bitness: ensure 32-bit vs 64-bit consistency between FPC, Lazarus, and OptiVec.
  • Calling convention: try cdecl vs stdcall if functions fail.

9. Packaging and Deployment

  • Include required runtime libraries (.dll/.so) with your executable or document dependency installation steps.
  • For Linux, add rpath or update LD_LIBRARY_PATH in launch scripts if needed.

10. Further Steps

  • Explore OptiVec advanced functions: FFT, BLAS-like routines, statistics, filters — via provided docs.
  • Replace simple wrappers with more complete Pascal units covering all required APIs.
  • Profile hotspots in your app to target vectorization opportunities.

That’s it — following these steps should let you integrate OptiVec into Lazarus projects and leverage vectorized math for faster numeric code.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *