Matrix and linear algebra in F #, Part II: doing linear algebra via math providers [z]

Source: Internet
Author: User
Tags lapack intel mkl

 

Long story short

Suppose we are using the latest release, 1.9.9.9, which does not support linear algebra anymore in its PowerPack. but linear algebra is supported before in 1.9.7.8. under its bin \ folder, there is a DLL named FSharp. powerPack. math. providers. dll. "Math providers" means F # callother math libraries, to be specific, an incomplete managed implementation in F #, Netlib's Lapack + Blas and Intel MKL. the F # One is immature, while the last two are really good! Plus Netlib's implementation is free.

However, we cannot just use that provider dll because it has conflicts with latest PowerPack. We need to build a new one! Here are the steps:

1. Compile Netlib's Lapack to getLAPACK. dllAndBlas. dll. Refer to this post of mine. You can also search online to download these two dlls, e.g. http://www.stanford.edu /~ Vkl/code/libs.html (I haven't tested this ).

2. Get the source code of 1.9.7.8. Go to folder FSharp-1.9.7.8 \ source \ fsppack \ FSharp. PowerPack. Remove the old reference to FSharp. PowerPack and add two 1.9.9 DLLs to the reference:

FSharp. PowerPack. dll and FSharp. PowerPack. Compatibility (Because in 1.9.9.9, Compatibility module is separated out .)

Compile and you get only a few errors regarding "permutation ":

You coshould just define it explicitly: type permutation = int-> int

Done! Now we move on

Use Lapack library

First make sure your project

1. references

FSharp. PowerPack. Compatibility, FSharp. PowerPack and FSharp. PowerPack. Math. Providers. dll.

2. has LAPACK. DLL and Blas. DLL in the binary Output Folder. e. g. debug \ or release \. (technically we only need to put them under a folder. NET platform is searchable, similar to Java's classpath .)

Finally, let's write a simple program:

open Microsoft.FSharp.Math
let r = new System.Random()
let A = Matrix.init 4 4 (fun i j -> r.NextDouble() * 1000.0)
let Ainv = Experimental.LinearAlgebra.Inverse A
let eigen = Experimental.LinearAlgebra.EigenValues A

Although this program compiles, it gives a not-implemented exception on eigenvalues function. (It can compute inverse of a correctly .) this is because functions in linearalgebra module use managed implementation (see linear_algebra_managed.fs in LAPACK folder) on default. however, the managed implementation is incomplete, and not thoroughly tested.

We needFirst open the LAPACK Service:

let isSucc = Experimental.LinearAlgebra.Lapack.Start()

Put this before any computation. (because there's no documentation at all, I 've read all the code in LAPACK folder to find this line ...)

The returned bool value issucc indicates whether the LAPACK service providers successfully loads DLLs for netlib or not.

If you have Intel MKL

PutMkl_def.dllAndMkl_lapack.dllUnder a. Net searchable folder. notice that the wrapper is for MKL 9.1 not for 10 series.

BTW, The LAPACK service provider will choose MKL if both MKL and netlib are presented.

 

The Long story

 

. Net do have some good math libraries. they are efficient (native performance), easy to use. however, they are not free. for the free ones, there's a math. net open source project, which is still under its alpha-testing phase. these open source projects seem to tend to implement all algorithms from starch in C #, rather than link to some mature ones, like netlib's LAPACK. this certainly has some advantages, like more safe code, better memory management in. net sense and exception handling. however, this strategy wowould have a long development time.

I also suspect their efficiency. I have a L-BFGS-based logistic regression solver written C ++, it runs 3 ~ 4 times slower if compiled. net using C ++/CLI. drawing a conclusion from this single case is too assertive. (I used STL and STL is slow in C ++/CLI, in C ++/CLI we 'd better use CLI STL .) however, I still trust native libraries to perform basic linear algebra for the performance's seek.

I started to use F # from its 1.9.7.8 (The October CTP, 2009 ). in that release, the math provider library is already spitted out from the main PowerPack as a standalone DLL. but there's no documentation on it, doesn't the source code. other math types, e.g. matrix, vector, are encoded ented. so I didn't know there exists such a library at first. by occasion, I found the lapack folder in the F # source. today, I finally have some time to dig into it, get it compiled and know how to start the linear algebra service provider. fortunately, once started, the service provider is able to automatically find the DLLs of Netlib or MKL, which is really nice.

The code is still compliable using 1.9.9.9, but I am not sure it will in the future as there are a lot of warnings mentioning deprecated language usages, e.g. use OCaml-style '^' to connect two strings.

As this code is under MS open source license, there wocould be license problems if we maintain and distribute it. so a possible solution is to rewrite the service provider and make it open source (e.g. MIT or BSD license), or, persuade MSR Cambridge or MS vs team to continue the development.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.