arrayfire / arrayfire-dotnet

.NET wrapper for ArrayFire
BSD 3-Clause "New" or "Revised" License
78 stars 21 forks source link

(question) Missing package 'af'/libaf when building for F# on a Mac #32

Closed wclark2052 closed 3 years ago

wclark2052 commented 3 years ago

Hello - I'm presently using FSharp on a Mac, and I've had success previously using ArrayFire libraries in C++, Rust, and Python on this local machine. For my FSharp (dotnet) project I was able to install the ArrayFire dotnet package successfully:

dotnet add package ArrayFire

subsequent App.fsproj:

<ItemGroup>
  <PackageReference Include="ArrayFire" Version="0.0.2" />
</ItemGroup>

However, when running "dotnet run" I get the following build error:

usuario@usuarios-MacBook-Pro ~/u/w/r/h/f/F/s/App (master)> dotnet run
Unhandled exception. System.DllNotFoundException: Unable to load shared library 'af' or one of its dependencies. In order to help diagnose loading problems, consider setting the DYLD_PRINT_LIBRARIES environment variable: dlopen(libaf, 1): image not found
   at ArrayFire.Interop.AFBackend.af_set_backend(af_backend bknd)
   at Program.main(String[] argv) in /Users/usuario/usuario/wpractice/rust/houndnew/fshfe/FSNetCore/src/App/Program.fs:line 11

My libaf dylibs are located at /usr/local/opt/arrayfire/lib, (though I don't know how I might be able to specify that here, in the dotnet build/run stage via terminal).

I've tried setting DYLD_PRINT_LIBRARIES (in fish shell: set -x DYLD_PRINT_LIBRARIES 1) as advised in the error message, and I get a long output of successful "dyld loaded" statements (not including any libaf dylibs), followed by this error which is almost the same as the last:

Unhandled exception. dyld: loaded: /usr/local/share/dotnet/shared/Microsoft.NETCore.App/3.1.12/System.Native.dylib
System.DllNotFoundException: Unable to load shared library 'af' or one of its dependencies. In order to help diagnose loading problems, consider setting the DYLD_PRINT_LIBRARIES environment variable: dlopen(libaf, 1): image not found
   at ArrayFire.Interop.AFBackend.af_set_backend(af_backend bknd)
   at Program.main(String[] argv) in /Users/usuario/usuario/wpractice/rust/houndnew/fshfe/FSNetCore/src/App/Program.fs:line 11

It could very well be an issue more specific to dotnet environments on a Mac (or my own environment configuration), than to ArrayFire itself, so apologies if that's the case. But any insight would be much appreciated, thanks.

wclark2052 commented 3 years ago

Solved -- env DYLD_LIBRARY_PATH=/usr/local/opt/arrayfire/lib/ dotnet run

sanikolov commented 2 years ago

I am getting a similar issue on windows.

> open ArrayFire;;
> let arr = Data.RandNormal<double>(2,2);;
System.DllNotFoundException: Unable to load DLL 'af': The specified module could not be found. (Exception from HRESULT: 0x8007007E)
   at ArrayFire.Interop.AFRandom.af_randn(IntPtr& array_out, UInt32 ndims, Int64[] dim_dims, af_dtype type)
   at <StartupCode$FSI_0004>.$FSI_0004.main@()
Stopped due to error
dir "c:\Program Files\ArrayFire\v3\lib\af.*"
Directory of c:\Program Files\ArrayFire\v3\lib

2021-03-08  12:14         2,970,624 af.dll
2021-03-08  12:14           733,734 af.lib

Obviously a MAC specific env variable won't work on windows. What is the best practice? I'd rather not copy af.dll to the local folder if there is a better way.

sanikolov commented 2 years ago

I was able to answer my own question this morning. On windows you can add the folder where af.dll is located to the PATH and then it will work. e.g. set PATH=%PATH%;c:\Program Files\ArrayFire\v3\lib Furthermore ensure that bitness matches. This probably means you start F# with fsianycpu.exe rather than fsi.exe