I think array slices (on right hand side of assignment) create new arrays,
hence the allocation.
Try writing an explicit loop instead, something like:
for j = 1:length(counter1)
for i = 1:size(file,1)
mat[counter1[j],i,counter2[j]] = file[i,j]
end
end
On Saturday, March 12, 2016 at 12:25:00 PM UTC+11, Tim Loderhose wrote:
> Hi,
>
> I have a question regarding some allocation in my code I would like to get
> rid of.
> I am memory mapping a file (which could be very large) which is part of a
> complex 3D matrix, and then put its contents into the preallocated matrix
> along the second dimension. I need the counters because the contents of
> file are only a subset of the full matrix.
>
> Here's a profiled snippet, where the file which is loaded has 120619520
> bytes.
>
> 153705063 mat = Array{Complex64}(dims...)
> 4721282 file = Mmap.mmap(filename, Array{Complex64,2},
> (dims[2],length(counter1)))
> 16 for i = 1:length(counter1)
> 148179531 mat[counter1[i],:,counter2[i]] = file[:,i]
> - end
>
> Why does the code allocate so much memory inside the for-loop (even more
> bytes than the contents of file)?
> It seems like this is a trivial matter, right now I just can't get my head
> around it, any help is appreciated :)
>
> Thanks,
> Tim
>