Message Boards Message Boards

0
|
9280 Views
|
6 Replies
|
2 Total Likes
View groups...
Share
Share this post:

How to incorporate a package written in C, like the FFTW package?

Posted 11 years ago

Hello!

I'm student and develop a Mathematica programm for my thesis. For speeding it up I want to incorporate an in C written package (NFFT developed by Prof. Potts in Chemitz). This comprised a FFT for nonequispaced nodes and is analogous to the popular FFTW package. Can someone give my a hint how to incorporate packages like FFTW without changing it? Is it possible to do this?

Best regards and thanks for helping,

Jan

(I have never programming in C)

POSTED BY: Jan P
6 Replies

Now I got an following error: CreateLibrary::cmperr

When you see this error, the first thing to do is to set the options "ShellCommandFunction" -> Print, "ShellOutputFunction" -> Print in CreateLibrary. This will show you the compilation command line and the compiler output. You will be able to see the full error message.

That said, in this case the error could very well be because you failed to explicitly link to the library you are using (fftw). With compilers like gcc or clang, this is done through the -l option (with the -L option specifying where to look for the library, if necessary). It might be something like -lfftw, but you need to look this up in the fftw documentation (it will be different for each package).

The equivalent option of CreateLibrary is "Libraries", i.e. "Libraries" -> {"fftw"} or similar.

POSTED BY: Szabolcs Horvát

For calling executables in Windows, I wrote the function

rundos[s_String] := ReadList["!" <> s <> " 2>&1", String]

where s is a string argument for the program I'm calling.

the "2>&1" enables error messages to be returned as well.

POSTED BY: Frank Kampas
Posted 11 years ago

I have to call the function from time to time.

I wish to have a C-file that concatenates the C programs I need. (I can write that one in C). This C-file I want to connect with Mathematica to call the C programs.

Therefore Mathematica has to send a two lists and one integer n. One List has the Dimension 3xdim, the other one 1xdim where dim ist an integer. Out of the C file I want to get a 1xd list where d=n^3 and the elements are complex numbers.

Can someone help me which functions I on C side to handle the problem?

With CreateLibrary I get the error shown above. Is it better to use MathLink? Which functions I need?

@Frank Kampas, I don't know how to handle Run or ! for this problem. I think these function I should use when I have a single command line, don't I?

It would give me great pleasure if someone can help me to solve the problem.

POSTED BY: Jan P

If you don't need to call the function very often, it might make sense to call it from Mathematica as an external operating system call using Run or !.

http://reference.wolfram.com/language/guide/CallingExternalPrograms.html

POSTED BY: Frank Kampas
Posted 11 years ago

Thank you for answering!

Up to now I have programmed C code which call the C-function I need (see the added file, the c code save ass .txt). Now I got an following error:

CreateLibrary::cmperr: "Compile error: \!\(\"clang: error: linker command failed with exit code 1 (use -v to see invocation)\"\)"

Do you have an idea to fix the error?

If I comment nfft_init_3d(&plan,n,n,n,dims[0]);, nfft_adjoint(&p); and nfft_finalize(&p); out the error doesn't occur. These are the functions I called from the NFFT package written in C.

I use

myLibrary = CreateLibrary[{"/Users/.../example.c"}, "myLibrary", "IncludeDirectories" -> {"..."}, "Debug" -> False];

to integrate the C file. The "..." stand for the file/ directory path which I cut out for better readability.

Attachments:
POSTED BY: Jan P
Posted 11 years ago

See the documentation for MathLink and LibraryLink. Both allow you to interface with external C code. If you haven't programmed in C it's not going to be trivial.

POSTED BY: Eric Meyers
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract