1
$\begingroup$

I asked a question here and then tried to use Compile for the solution to gain even more speed. Inside Compile there is a loop which calculate matrix multiplications of this type :

Transpose[vector].matrix.vector;

the matrix is a list of matrices.

<< Developer`
n = 24;
m = 1000;

matrix = ToPackedArray@Transpose[Table[RandomReal[], {i, 1, m}, {j, 1, n}, {k, 1, n}]];
(*a tensor*)

vector = N[{IdentityMatrix[n][[1]]}\[Transpose]];

and,

funComplex = Compile[{{mymatrix, _Real, 3}, {myvector, _Complex, 2}, {number, _Integer}}, Do[Flatten[#1\[ConjugateTranspose].mymatrix.#1 &[myvector]], {ite, 1, number}]]

In the Compile I assumed myvector could be Complex. In the below, I assume myvector is Real:

funReal = Compile[{{mymatrix, _Real, 3}, {myvector, _Real, 2}, {number, _Integer}}, Do[Flatten[#1\[ConjugateTranspose].mymatrix.#1 &[myvector]], {ite, 1, number}]]

number is the number of iteration inside Do.

funComplex[matrix, vector, 1000] // AbsoluteTiming
(*{2.970004, Null}*)

funReal[matrix, vector, 1000] // AbsoluteTiming
(*{0.120000, Null}*)

As you see if I use Complex inside Compile it gets 25 times more slow. If inside Compile for funComplex I use Complex type for mymatrix then funComplex becomes considerably faster but not as fast as funReal:

funComplex2 = Compile[{{mymatrix, _Complex, 3}, {myvector, _Complex, 2}, {number, _Integer}}, Do[Flatten[#1\[ConjugateTranspose].mymatrix.#1 &[myvector]], {ite, 1, number}]]

funComplex2[matrix, vector, 1000] // AbsoluteTiming
(*{0.600001, Null}*)

How can I solve this issue?

$\endgroup$
7
  • $\begingroup$ Have you checked whether your function compiled successfully in all details with CompilePrint? $\endgroup$
    – halirutan
    Commented Jan 11, 2015 at 16:50
  • $\begingroup$ I used << CompiledFunctionTools` On[Compile::noinfo] To see if there is any error. I will use CompilePrint. $\endgroup$
    – MOON
    Commented Jan 11, 2015 at 16:54
  • $\begingroup$ I have no idea what the output of CompilePrint means. I can't see any error in the output. $\endgroup$
    – MOON
    Commented Jan 11, 2015 at 17:06
  • $\begingroup$ Can you tell me what happens if you use the following definition of funComplex? I get only very small differences between real and complex now. And can you verify that this code is equivalent to what you want? $\endgroup$
    – halirutan
    Commented Jan 11, 2015 at 23:52
  • $\begingroup$ The code you mentioned is what I want, but its performance is the same as funComplex2 is this question, and it is less than the performance of funReal. $\endgroup$
    – MOON
    Commented Jan 12, 2015 at 8:58

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.