comp.lang.idl-pvwave archive - RDF feed
type: channel
description: Messages from Usenet group comp.lang.idl-pvwave, compiled by Paulo Penteado
items: Can my big for loop exploit array operations?
items: Re: Can my big for loop exploit array operations?

Can my big for loop exploit array operations?
type: item
description: Hi, I'm writing a little post-processing procedure and need help avoiding a hefty for loop, which will typically require 1000^3 iterations. Its purpose is to take data from a simulation, which expresses three components of a vector field at a different position s.t. x - component is defined on cell boundary in x-direction, cell center in other directions (xb,yc,zc( y - component is defined on cell boundary in y-direction, cell center in other directions (xc,yb,zc) z - component is defined on cell boundary inz-direction, cell center in other directions (xc,yc,zb) and express the field as defined in the cell center for all components (xc,yc,zc). Rather than using interpolation, I think the best way will be to simply average the values across the cell face to head off errors down the line. The problem is im not sure how to avoid a hefty for loop which goes through all elements taking the desired average as follows.... FUNCTION reformat_bcomponents_1,t data = getdata(t,/BX,/BY,/BZ,/GRID) bx = DBLARR(SIZE(data.x,/N_ELEMENTS),SIZE(data.y,/N_ELEMENTS),SIZ E(data.z,/N_ELEMENTS)) by = DBLARR(SIZE(data.x,/N_ELEMENTS),SIZE(data.y,/N_ELEMENTS),SIZ E(data.z,/N_ELEMENTS)) bz = DBLARR(SIZE(data.x,/N_ELEMENTS),SIZE(data.y,/N_ELEMENTS),SIZ E(data.z,/N_ELEMENTS)) FOR iz = 0,SIZE(data.z,/N_ELEMENTS) -1 DO BEGIN FOR iy = 0,SIZE(data.y,/N_ELEMENTS) -1 DO BEGIN FOR ix = 0,SIZE(data.x,/N_ELEMENTS) -1 DO BEGIN bx[ix,iy,iz] = MEAN( [ data.bx[ix,iy,iz], data.bx[ix+1,iy,iz] ] ,/DOUBLE) by[ix,iy,iz] = MEAN( [ data.by[ix,iy,iz], data.by[ix,iy+1,iz] ] ,/DOUBLE) bz[ix,iy,iz] = MEAN( [ data.bz[ix,iy,iz], data.bz[ix,iy,iz+1] ] ,/DOUBLE) ENDFOR ENDFOR ENDFOR mag_str = CREATE_STRUCT('bxp',bx,'byp',by,'bzp',bz) RETURN,mag_str END If anyone can point me in the direction of how to do this exploiting array operations I'd be grateful. Thanks in advance, Jonathan
creator: JTMHD
date: 2015-10-20T12:35:07-00:00

Re: Can my big for loop exploit array operations?
type: item
description: JTMHD wrote on 2015-10-20 5:35am:> Hi, > > I'm writing a little post-processing procedure and need help avoiding a hefty for loop, which will typically require 1000^3 iterations. > > > Its purpose is to take data from a simulation, which expresses three components of a vector field at a different position s.t. > > x - component is defined on cell boundary in x-direction, cell center in other directions (xb,yc,zc( > y - component is defined on cell boundary in y-direction, cell center in other directions (xc,yb,zc) > z - component is defined on cell boundary inz-direction, cell center in other directions (xc,yc,zb) > > and express the field as defined in the cell center for all components (xc,yc,zc). > > Rather than using interpolation, I think the best way will be to simply average the values across the cell face to head off errors down the line. > > The problem is im not sure how to avoid a hefty for loop which goes through all elements taking the desired average as follows.... > > > FUNCTION reformat_bcomponents_1,t > > data = getdata(t,/BX,/BY,/BZ,/GRID) > > bx = DBLARR(SIZE(data.x,/N_ELEMENTS),SIZE(data.y,/N_ELEMENTS),SIZ E(data.z,/N_ELEMENTS)) > by = DBLARR(SIZE(data.x,/N_ELEMENTS),SIZE(data.y,/N_ELEMENTS),SIZ E(data.z,/N_ELEMENTS)) > bz = DBLARR(SIZE(data.x,/N_ELEMENTS),SIZE(data.y,/N_ELEMENTS),SIZ E(data.z,/N_ELEMENTS)) > > FOR iz = 0,SIZE(data.z,/N_ELEMENTS) -1 DO BEGIN > FOR iy = 0,SIZE(data.y,/N_ELEMENTS) -1 DO BEGIN > FOR ix = 0,SIZE(data.x,/N_ELEMENTS) -1 DO BEGIN > > bx[ix,iy,iz] = MEAN( [ data.bx[ix,iy,iz], data.bx[ix+1,iy,iz] ] ,/DOUBLE) > by[ix,iy,iz] = MEAN( [ data.by[ix,iy,iz], data.by[ix,iy+1,iz] ] ,/DOUBLE) > bz[ix,iy,iz] = MEAN( [ data.bz[ix,iy,iz], data.bz[ix,iy,iz+1] ] ,/DOUBLE) > > ENDFOR > ENDFOR > ENDFOR > > mag_str = CREATE_STRUCT('bxp',bx,'byp',by,'bzp',bz) > > RETURN,mag_str > END > > If anyone can point me in the direction of how to do this exploiting array operations I'd be grateful. > > Thanks in advance, > > Jonathan Hi Jonathan, I l…
creator: Dick Jackson
date: 2015-10-20T15:51:01-00:00

Re: Can my big for loop exploit array operations?
type: item
description: Hi Dick, For the test data (256^3) I saw a fantastic speed-up using both methods you suggested - apparently as good as 35x faster for the first method (using the negative indexing) and about 60x faster with the convolution. I was expecting a solution in the spirit of your first answer, exploiting the array indices. It hadn't occurred to me at all to use convolution and was immensely helpful to see how this could be used going forward. I'm now dealing with such huge data that I can't afford to be a lazy programmer so its good to see these different approaches (in the past I've not had to worry about optimization so much!) Thank you for your help, Jonathan
creator: JTMHD
date: 2015-10-21T12:21:01-00:00