2

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[] 
(* {} *)
J. M.'s missing motivation
  • 124,525
  • 11
  • 401
  • 574
Pinti
  • 6,503
  • 1
  • 17
  • 48
  • I think this is a duplicate: http://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. – Szabolcs Apr 03 '17 at 11:31
  • 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? – Szabolcs Apr 03 '17 at 11:34
  • @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. – Pinti Apr 03 '17 at 11:47
  • 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). – Szabolcs Apr 03 '17 at 11:58
  • Yes, I tried, instructions from the first answer of your linked question solve my problem. But I don't know how to merge the two questions as you suggested. – Pinti Apr 03 '17 at 12:05
  • You cannot merge them, that needs to be done by an administrator. The first step is to mark as duplicate. I have just voted for that. – Szabolcs Apr 03 '17 at 12:35

1 Answers1

4

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`}*)
T_S
  • 180
  • 5
  • 2
    "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. – Szabolcs Apr 03 '17 at 11:35