Questions tagged [compile]

Questions about the generation and use of compiled functions using Compile.

Filter by
Sorted by
Tagged with
165
votes
6answers
19k views

How to compile effectively?

What are the best practices of compiling functions? I understand that this is a vague question, but let me list some aspects that might trigger useful answers. Some of these have already been answered ...
155
votes
8answers
17k views

List of compilable functions

Is there somewhere a list on the functions that Compile can compile, or the cases in which a particular function can be compiled that I haven't found? I'd be glad ...
72
votes
2answers
4k views

Internal`Bag inside Compile

Since Internal`Bag, Internal`StuffBag and Internal`BagPart can be compiled down, it is a ...
61
votes
4answers
4k views

Speeding up this fractal-generating code

I used the code below (which is a sample from this gist containing more similar code) in my answer to my own question about Mandelbrot-like sets for functions other than the simple quadratic on Math....
48
votes
4answers
6k views

How well does Mathematica code exported to C compare to code directly written for C?

Essentially what it says in the title. Mathematica can export its code to C. How much overhead does that inflict on the code, as compared to writing it from scratch in C?
40
votes
4answers
3k views

Compiling more functions that don't call MainEvaluate

I would like to use Compile with functions defined outside Compile. For example if I have the two basic functions ...
32
votes
4answers
1k views

Which Distributions can be Compiled using RandomVariate

Recently, Oleksandr kindly showed a list of Mathematica commands that can be compiled. RandomVariate was part of that list. However, whether this can be compiled depends upon the distribution that is ...
30
votes
5answers
1k views

Can (compiled) matrix permanent evaluation be further sped-up?

Update III  Mathematica 10.2.0 now ships with a predefined System`Permanent function, which the PermanentCode package ...
30
votes
1answer
2k views

Has this implementation of FDM touched the speed limit of Mathematica?

Still, I'll use the implementation of the 1D FDTD method (you can simply understand it as a kind of explicit finite difference scheme for the Maxwell's equation) as the example. Just for completeness, ...
30
votes
2answers
600 views

Why is CompilationTarget -> C slower than directly writing with C?

Probably a hard question, but I think it's better to cry out loud. I've hesitated for a while about whether I should post this in StackOverflow with a c tag or not,...
26
votes
4answers
2k views

How to force Compile to return multiple results?

I would like to return two separate values from a compiled function, but Mathematica refuses to use the compiled version. Here is a narrowed-down example: ...
26
votes
2answers
529 views

Performance regression for Fold?

I noticed an over 10x performance drop on Mathematica v9.0.1 (as Oleksandr R. commented, also v8) compared with v7.01 for this code: ...
26
votes
1answer
5k views

How do I get Mathematica to recognize a C compiler on a 64-bit Windows machine?

Mathematica does not recognize any C compiler on my computer. I run Mathematica 8.0.4.0 on Windows 7. I've tried to make sure various C compilers are installed on my computer, but I'm not sure how to ...
25
votes
2answers
320 views

Listable compiled function crashes the kernel with “no more memory available”

Bug introduced in 9.0.0 and fixed in 10.3 I am trying to speed up a function that involves continued fractions. Since ContinuedFraction cannot be compiled and the ...
24
votes
1answer
1k views

Getting Compile to work with Visual Studio 2017 Community Edition

The new Visual Studio is out since a week and I tried to get it to work with Mathematica (versions 11.0 and 11.1). However, the usual tricks for working with a new VS version fail here. Related ...
23
votes
2answers
3k views

Using a compiled function inside NIntegrate gives “CompiledFunction::cfsa” message

The following function is defined for Real input: ...
23
votes
0answers
405 views

Compile not correctly initializing a variable defined inside Module

Bug introduced in 8.0, fixed in 10.4, intentionally reintroduced in 11.0 and persisting through 12.0. In the following example, inside the minimumX's ...
22
votes
3answers
481 views

sudden increase in timing when summing over 250 entries

I see a sudden increase of Timing by a factor of thousands when I sum over 250 elements of a matrix rather than over 249. So for instance, this table contains sums ...
22
votes
2answers
1k views

Efficiently collecting results inside a compiled function

When we don't know the number of results that will be generated, the usual way to collect results is Reap/Sow. Another ...
22
votes
2answers
460 views

What can be inlined by “InlineExternalDefinitions” -> True?

This not-well-documented option of Compile has been used quite a bit in this site, and based on these examples, I used to think ...
21
votes
3answers
3k views

Fastest way to measure Hamming distance of integers

I am looking for a fast and robust way to calculate the Hamming distance of integers. The Hamming distance of two integers is the number of matching bits in their binary representations. I expect that ...
21
votes
1answer
407 views

Performance of compiled functions

I am trying to find the fastest way to calculate two values where both of them are sum of different expressions. I combine both calculations in one Sum[]. I'm ...
20
votes
1answer
1k views

Use Compile to improve performance

I've recently discovered Compile as a way to optimize my code. I wrote a program to learn something new about Compile. I have a ...
19
votes
2answers
449 views

Third argument of Compile

In his last comment on this answer, halirutan shows an example in which the third argument of Compile does not have any effect. The example is as follows. ...
19
votes
1answer
446 views

In Compile “InlineExternalDefinitions” doesn't work anymore in Mma v10 like in v9

In Mma 9 using CompilationOptions -> {"InlineExternalDefinitions" -> True} inside of Compile inlined the values of the ...
18
votes
1answer
597 views

Strange behavior of Table

Whenever I run the code a = 0; code := (a++); Table[code; a, {1000}] I get a list of 1000 zeros. However, if I replace 1000 with 100, then a get a list equal to ...
18
votes
1answer
555 views

Removing calls to MainEvalute when using inlined compiled closures

This question is tightly related to the answer Shaving the last 50 ms off NMinimize. There @OleksandR shows how inlined closures can be used to eliminate calls to ...
18
votes
1answer
271 views

Why Function is not identical to `&` in autocompilation?

Here is the example, copied from here square = Function[x, x^2]; square1 = #^2 &; the timing and unpacking status shows ...
17
votes
3answers
773 views

Compiling LinearSolve[] or creating a compilable procedural version of it

Earlier today I had a discussion with a representative at Premier Support about the 2 questions I've asked here over the past couple of days: Seeking strategies to deploy a function securely ...
17
votes
2answers
281 views

CompiledFunction returns machine numbers smaller than $MinMachineNumber

When thinking on the workaround for this LogLogPlot bug suggested by halirutan I noticed that CompiledFunction actually can ...
17
votes
1answer
1k views

Parallel compiled functions running on parallel sub-kernels

Question Why is it, that compiled, Listable, parallel functions which work perfectly fine on the main kernel, do not run in parallel on sub-kernels? Details ...
17
votes
1answer
2k views

How to install and configure Intel C++ Compiler for Mathematica correctly?

I wish to try the trial version of the latest Intel C++ Compiler for Windows with Mathematica. I have downloaded the current version of Intel® C++ Composer XE 2011 for Windows and installed only the "...
17
votes
1answer
367 views

Memory usage of compiled function: C vs WVM

Reported to WRI [CASE:3406803] Why does generating an array in "C"-compiled function uses 50% more memory than doing same thing in ...
17
votes
1answer
975 views

What technical obstacles prevent all Mathematica code compiling to C

The following represents an attempt at a very simple view of the levels in Mathematica code: We have had lots of questions about deployment and compiling Mathematica code to C. Some of these for ...
16
votes
2answers
1k views

Speeding up random walk for many particles

I am trying to speed up this code for many particles to take a random walk. I'm not sure why it is so slow for such a simple task. I got a few hints from colleagues to reduce the precision of the ...
16
votes
1answer
1k views
16
votes
1answer
315 views

Is the CompiledFunctionCall WVM opcode efficient?

There is example in Mathematica documentation about Compile ...
16
votes
2answers
545 views

Using Boost with CreateLibrary

I was looking at the answer of Leonid of Faster alternatives for DayOfWeek and wanted to see how hard it would be to do something similar in C++ using an external library like Boost. I tried to ...
15
votes
3answers
530 views

Quick multiple selections from a list

What is the fastest way to make multiple selections from a list? Compiled methods included. For example, here are two methods for selecting a subset, compared:- ...
15
votes
1answer
567 views

Why is MainEvaluate being used when LinearSolve can be compiled?

According to this question LinearSolve can be compiled. However, CompilePrint[] shows a call to ...
15
votes
1answer
758 views

Save a C compiled function without losing the blessing of C compiler

Consider the following example: ...
15
votes
2answers
470 views

Why can't I inject expressions in Compile using (only) With

Generally speaking, With can be used to inject into arbitrarily held expressions, for example: ...
15
votes
1answer
486 views

Is the sharing of variables in Module/Block within Compile documented behavior?

Today I noticed something, I think for the first time. When used inside Compile variable values within Module (and ...
15
votes
1answer
437 views

How can you profile compiled code?

Is it possible to profile compiled code? I am using Workbench 2.0 and the Profile[] command but I do not get a profile of my compiled function. I apologize if the answer is readily available but I ...
14
votes
2answers
458 views

How to define a function inside a Compile?

I want to compile a variety of commands, including a function. I need to map this function, inside the compilation, and I can't include the function without facing errors such as "CompiledFunction::...
14
votes
3answers
2k views

What compilers are “suitable” for CompilationTarget->“C”?

The documentation for CompilationTarget says, "if you do not have a suitable C compiler then you will not be able to set CompilationTarget to C". Well at this time, I don't have a compiler installed ...
14
votes
1answer
274 views

CompiledFunctionCall vs. LibraryFunction

When compiling code to C, and the compiled code calls other compiled functions, I see CompiledFunctionCall in the output of ...
14
votes
2answers
282 views

How do you determine the optimal autocompilation length on your system

When you pack lists there is an overhead therefore packing a list with, say, 2 elements is likely to cost more than you get back in efficiency. Mathematica has default list lengths for which functions ...
14
votes
1answer
455 views

Using Apply inside Compile

In this great answer a compiled version of the Nelder-Mead algorithm is presented. Since it works on arbitrary dimensions (i.e. arbitrary number of arguments), it has to use apply on the objective ...
14
votes
1answer
3k views

Step-by-step instruction to Install C Compiler

I remember using C compiler on my previous computers, and I never had any problems installing them. At this time, however, I failed to work through the instructions provided in Mathematica ...