> Thanks Mauro - really useful to know. So it seems that in this > particular instance the number of nested loops does change with N, so > I think the code would fall into the latter case.
Sorry, I wasn't clear: no, only the number of iterations would change. With numbers of nested loops I meant, say two: for i=1:n for j=1:m ... end end to three for i=1:n for j=1:m for k=1:l ... end end end which could be done with a generated function (look for Base.Cartesian). >> Well, it is always hard to tell what's best. If you need certainty, >> then you need to benchmark. Note that in below example, Julia will >> compile a specialized version of the function for each NTuple anyway. >> This may be good or bad, depending on how much time compilation vs >> running takes. And above was also not clear, I should have written: "Note that in MY example below, Julia will compile a specialized version of the function for each NTuple with a different N anyway." >> A case for a generated function would be if, say, the number of nested >> loops would change depending on some parameter, say the dimensionality >> of an array. >> >>> Thanks, Alan >>> >>> >>> was just an illustration of a the larger piece of code where I am making >>> using >>> On 29 Sep 2015, at 13:48, Mauro <mauro...@runbox.com> wrote: >>> >>>> I don't think that you need nor that you should use generated functions. >>>> But maybe I'm wrong, what are you trying to achieve? This should work >>>> as you want: >>>> >>>> function testfun2!{N}(X,Y::NTuple{N,Float64}) >>>> for i in eachindex(X), j in 1:N # much better to have the loop this way >>>> X[i][j] = Y[j] >>>> end >>>> return X >>>> end >>>> >>>> # Setup for function call >>>> InnerArrayPts = 3 >>>> OuterArrayPts = 10 >>>> Xinput = [Array{Float64}(InnerArrayPts) for r in 1:OuterArrayPts] >>>> Yinput = rand(InnerArrayPts) >>>> >>>> testfun2!(Xinput,tuple(Yinput...)) >>>> >>>> On Tue, 2015-09-29 at 13:20, Alan Crawford <a.r.crawf...@gmail.com> wrote: >>>>> I would like to preallocate memory of an array of arrays and pass it to a >>>>> function to be filled in. I have created an example below that illustrates >>>>> my question(s). >>>>> >>>>> Based on my (probably incorrect) understanding that it would be desirable >>>>> to fix the type in my function, I would like to be able to pass my array >>>>> of >>>>> arrays, X, in a type stable way. However, I can't seem to pass >>>>> Array{Array{Float64,N},1}. If, however, i do not attempt to impose the >>>>> type >>>>> on the function, it works. >>>>> >>>>> Is there a way to pass Array{Array{Float64,N},1 to my function? Do I even >>>>> need to fix the type in the function to get good performance? >>>>> >>>>> # version of Julia: 0.4.0-rc3 >>>>> >>>>> @generated function >>>>> testfun1!{N}(X::Array{Array{Float64,1},1},Y::NTuple{N,Float64}) >>>>> quote >>>>> for j in 1:$N, i in eachindex(X) >>>>> X[i][j] = Y[j] >>>>> end >>>>> return X >>>>> end >>>>> end >>>>> >>>>> @generated function testfun2!{N}(X,Y::NTuple{N,Float64}) >>>>> quote >>>>> for j in 1:$N, i in eachindex(X) >>>>> X[i][j] = Y[j] >>>>> end >>>>> return X >>>>> end >>>>> end >>>>> >>>>> # Setup for function call >>>>> InnerArrayPts = 3 >>>>> OuterArrayPts = 10 >>>>> Xinput = [Array{Float64}(InnerArrayPts) for r in 1:OuterArrayPts] >>>>> Yinput = rand(InnerArrayPts) >>>>> >>>>> # Method Error Problem >>>>> testfun1!(Xinput,tuple(Yinput...)) >>>>> >>>>> # This works >>>>> testfun2!(Xinput,tuple(Yinput...)) >>>>> >>>>> I also tried with the following version of testfun1!() and again got a >>>>> method error. >>>>> >>>>> @generated function >>>>> testfun1!{N}(X::Array{Array{Float64,N},1},Y::NTuple{N,Float64}) >>>>> quote >>>>> for j in 1:$N, i in eachindex(X) >>>>> X[i][j] = Y[j] >>>>> end >>>>> return X >>>>> end >>>>> end >>>>> >>>>> >>>>> I am sure i am misunderstanding something quite fundamental and/or missing >>>>> something straightforward... >>>>> >>>>> Thanks, >>>>> Alan