2
$\begingroup$

Note: This question is about AceGen package for automatic code generation for finite element analysis.

MinGW compiler is recommended for compiling finite element subroutines generated by AceGen and is bundled with the installation. How can I use this compiler for compiling other Mathematica expressions to C (e.g. Compile[...,CompilationTarget->"C"]) and make them faster?
The problem is that my MinGW compiler is not automatically detected by Mathematica. Command CCompilers[] returns empty list.

<< CCompilerDriver`;
CCompilers[] 
(* {} *)
$\endgroup$
7
  • $\begingroup$ Have you seen this? $\endgroup$ Apr 3, 2017 at 10:12
  • $\begingroup$ I think this is a duplicate: mathematica.stackexchange.com/q/55034/12 See also the description of the GenericCCompiler in the documentation. The example is precisely for MinGW 64-bit. Note that this compiler is not officially supported by Mathematica. You have to set it up yourself, as in the linked question. I do not recommend using it unless you know precisely why you cannot just use the Microsoft compiler. $\endgroup$
    – Szabolcs
    Apr 3, 2017 at 11:31
  • $\begingroup$ Also, I am not sure that it is a good idea to make this question AceGen-specific. AceGen is not free and most of us can't try this. MinGW is free. You just need to describe what sort of MinGW is bundled with AceGen (at least: 32-bit or 64-bit). Is there anything beyond that that would make it necessary to bring up AceGen? $\endgroup$
    – Szabolcs
    Apr 3, 2017 at 11:34
  • $\begingroup$ @Szabolcs I agree, my question seems like a duplicate of the link you provided. I am sorry that I didn't do proper research before asking. Should I delete my question? I have very little knowledge about compilers, but probably this question is not necessary AceGen specific. I have heard that one can use different compilers with AceGen, just that MinGW is the most common. $\endgroup$
    – Pinti
    Apr 3, 2017 at 11:47
  • $\begingroup$ It's a duplicate only if you can get it working using the instructions there. If yes, it is probably best to merge the two questions (since there is already an answer here). You cannot delete this because there is an answer (and even if you could, you shouldn't delete this because there is an answer). $\endgroup$
    – Szabolcs
    Apr 3, 2017 at 11:58

1 Answer 1

4
$\begingroup$

I don't know why the MinGW compiler used by the AceGen is not automatically found by Mathematica. Maybe because it is not installed to some specific location or because it is missing some metadata?

This is a workaround. We choose generic compiler and then set its installation path and name to MinGW.

(*  This is valid for 64-bit Windows OS *)
<< CCompilerDriver`;
$CCompiler = {
   "Compiler" -> CCompilerDriver`GenericCCompiler`GenericCCompiler, 
   "CompilerInstallation" -> 
    FileNameJoin[{$UserBaseDirectory, "Applications", "MinGW", 
      "MinGW64", "bin"}], 
   "CompilerName" -> "x86_64-w64-mingw32-gcc.exe"
   };

This example is taken from documentation for Compile. For some functions compiling to C can give really significant speedup.

(* Function without compilation. *)
f[x_, n_] := Module[ {sum, inc},
  sum = 1.; inc = 1.;
  Do[inc = inc*x/i; sum = sum + inc, {i, n}];
  sum
  ]

(* Compile to Wolfram virtual machine *)
fCompile = Compile[ {{x, _Real}, {n, _Integer}},
   Module[ {sum, inc},
    sum = 1.; inc = 1.;
    Do[inc = inc*x/i; sum = sum + inc, {i, n}];
    sum
    ]
   ];

(* Compile to C *)
fCompileToC = Compile[ {{x, _Real}, {n, _Integer}},
   Module[ {sum, inc},
    sum = 1.; inc = 1.;
    Do[inc = inc*x/i; sum = sum + inc, {i, n}];
    sum
    ], CompilationTarget -> "C"
   ];

f[1.5, 1000000] // AbsoluteTiming
(*{3.1352802071330084`, 4.481689070338066`}*)

fCompile[1.5, 1000000] // AbsoluteTiming
(*{0.06240285331664793`, 4.481689070338066`}*)

fCompileToC[1.5, 1000000] // AbsoluteTiming
(*{0.008644816153883178`, 4.481689070338066`}*)
$\endgroup$
1
  • 2
    $\begingroup$ "I don't know why the MinGW compiler used by the AceGen is not automatically found by Mathematica." Because MinGW (64-bit) is simply not supported by Mathematica. As far as I know, it has never been supported. It doesn't mean one cannot get it to work (in fact there are hints in the documentation under the description of the GenericCCompiler), but it is not straightforward. Last time I looked at this it was also necessary to convert the MathLink libraries (if MathLink was involved—in your example it isn't). I am not sure if that is still the case. $\endgroup$
    – Szabolcs
    Apr 3, 2017 at 11:35

Not the answer you're looking for? Browse other questions tagged or ask your own question.